Skip to content

Commit

Permalink
Provides caching for citations to speed up and reduce load on browse …
Browse files Browse the repository at this point in the history
…view generation. Disabled by default. Copy default_zero/cfg.d/citations.pl to local archive and set enabled=1 and run epadmin update
  • Loading branch information
drn05r authored and EPrints Services committed May 31, 2019
1 parent 37209b0 commit 6aedd1c
Show file tree
Hide file tree
Showing 7 changed files with 409 additions and 7 deletions.
34 changes: 33 additions & 1 deletion bin/epadmin
Expand Up @@ -61,6 +61,8 @@ Where I<command> is one of:
=item refresh_abstracts
=item refresh_citations
=item refresh_views
=item rehash
Expand Down Expand Up @@ -169,6 +171,10 @@ Tell the webserver that all views pages must be regenerated. The webserver will
Tell the webserver that all abstract summary pages must be regenerated. The webserver will update them next , but won't update them again unless something on the EPrint changes or you re-run refresh abstracts. Also causes config to be reloaded.
=item B<epadmin> refresh_citations I<repository_id>
Tell the webserver that all citations must be regenerated. The webserver will update them next , but won't update them again unless something on the citationable object changes or you re-run refresh citation.
=item B<epadmin> redo_mime_type I<repository_id> dataset [ objectid, ... ]
Re-run the file format identification. Dataset may be one of 'document' or
Expand Down Expand Up @@ -322,6 +328,7 @@ else
elsif( $action eq "erase_fulltext_index" ) { erase_fulltext_index( $repoid ); }
elsif( $action eq "reload" ) { reload( $repoid ); }
elsif( $action eq "refresh_abstracts" ) { refresh_abstracts( $repoid ); }
elsif( $action eq "refresh_citations" ) { refresh_citations( $repoid ); }
elsif( $action eq "refresh_views" ) { refresh_views( $repoid ); }
elsif( $action eq "redo_mime_type" ) { redo_mime_type( $repoid, @ARGV ); }
elsif( $action eq "redo_thumbnails" ) { redo_thumbnails( $repoid, @ARGV ); }
Expand Down Expand Up @@ -1425,7 +1432,32 @@ END
}

reload( $repoid );
}
}


sub refresh_citations
{
my( $repoid ) = @_;

my $repo = &repository( $repoid );

my $file = $repo->config( "variables_path" )."/citations.timestamp";
unless( open( CHANGEDFILE, ">$file" ) )
{
EPrints::abort( "Cannot write to file $file" );
}
print CHANGEDFILE "This file last poked at: ".EPrints::Time::human_time()."\n";
close CHANGEDFILE;
if( $noise > 0 )
{
print <<END;
Citations will be updated when they are next requested. You may wish to run
refresh_abstracts and/or refresh_views as citations will only be updated on
non-cached pages.
END
}
}


sub reload
{
Expand Down
7 changes: 7 additions & 0 deletions flavours/pub_lib/defaultcfg/cfg.d/citations.pl
@@ -0,0 +1,7 @@
##List Dataset and Fields##
$c->{datasets}->{citation} = {
class => "EPrints::DataObj::Citation",
sqlname => "citation",
index => 0,
};
$c->{citation_caching}->{enabled} = 0;
7 changes: 7 additions & 0 deletions lib/defaultcfg_zero/cfg.d/citations.pl
@@ -0,0 +1,7 @@
##List Dataset and Fields##
$c->{datasets}->{citation} = {
class => "EPrints::DataObj::Citation",
sqlname => "citation",
index => 0,
};
$c->{citation_caching}->{enabled} = 0;
1 change: 1 addition & 0 deletions perl_lib/EPrints.pm
Expand Up @@ -284,6 +284,7 @@ use EPrints::DataObj;
use EPrints::DataObj::SubObject;
use EPrints::DataObj::Access;
use EPrints::DataObj::Cachemap;
use EPrints::DataObj::Citation;
use EPrints::DataObj::Document;
use EPrints::DataObj::EPrint;
use EPrints::DataObj::File;
Expand Down
90 changes: 84 additions & 6 deletions perl_lib/EPrints/DataObj.pm
Expand Up @@ -619,6 +619,34 @@ sub clear_changed
######################################################################
=pod
=begin InternalDoc
=item $dataobj->clear_citations( )
Clear any citations associated with the DataObj.
=end InternalDoc
=cut
######################################################################

sub clear_citations
{
my( $self ) = @_;

my $db = $self->{session}->get_database();
my $citations_sql = "SELECT `citationid` WHERE `datasetid` = '" . $self->{dataset}->confid . "' AND `objectid` = '" . $self->id . "'";
my $statement = $db->prepare( $citations_sql );
$db->execute($statement, $citations_sql);
while ( my $res = $statement->fetchrow_hashref )
{
$self->{session}->dataset( "citation" )->dataobj( $res->{citationid} )->delete();
}
}

######################################################################
=pod
=item $success = $dataobj->commit( [$force] )
Write this object to the database and reset the changed fields.
Expand Down Expand Up @@ -675,6 +703,9 @@ sub commit
# clear changed fields
$self->clear_changed();

# clear citations unless this is a citation
$self->clear_citations() if defined $self->{session}->config( "citation_caching", "enabled" ) && $self->{session}->config( "citation_caching", "enabled" ) && $self->{dataset}->confid != "citation";

return $success;
}

Expand Down Expand Up @@ -1146,9 +1177,56 @@ sub render_citation
);
}

return $citation->render( $self,
in=>"citation ".$self->{dataset}->confid."/".$style,
%params );
my $citation_htmlobj = undef;
my $res = undef;
my $citation_caching_enabled = defined $self->{session}->config( "citation_caching", "enabled" ) && $self->{session}->config( "citation_caching", "enabled" );

if ( $citation_caching_enabled )
{
my $db = $self->{session}->get_database();
my $citations_sql = "SELECT `citationid`, `citation_text`, `timestamp_year` AS `year`, `timestamp_month` AS `month`, `timestamp_day` AS `day`, `timestamp_hour` AS `hour`, `timestamp_minute` AS `minute`, `timestamp_second` AS `second` FROM `citation` WHERE `datasetid` = '" . $self->{dataset}->confid . "' AND `objectid` = '" . $self->id . "' AND `style` = '" . $style . "' ORDER BY `timestamp_year`, `timestamp_month`, `timestamp_day`, `timestamp_hour`, `timestamp_minute`, `timestamp_second` DESC LIMIT 1";
my $statement = $db->prepare($citations_sql);
$db->execute($statement, $citations_sql);
my $res = $statement->fetchrow_hashref;
}
if (EPrints::Utils::is_set($res))
{
my $citation_unixtime = EPrints::Time::datetime_utc( $res->{year}, $res->{month}, $res->{day}, $res->{hour}, $res->{minute}, $res->{second} );

my $timestampfile = $self->{session}->get_conf( "variables_path" )."/citations.timestamp";
my $refresh_unixtime = ( -e $timestampfile ) ? (stat( $timestampfile ))[9] : 0;

if ( $citation_unixtime > $refresh_unixtime )
{
my $citation_text = "<citation>" . $res->{ 'citation_text' } . "</citation>";
return EPrints::XML->contents_of( EPrints::XML->parse_string( $citation_text )->documentElement() ); # convert to HTML Object
}
else
{
my $citation_dataobj = $self->{session}->dataset( "citation" )->dataobj( $res->{citationid} );
$citation_htmlobj = $citation->render( $self, in=>"citation ".$self->{dataset}->confid."/".$style, %params );
$citation_dataobj->set_value( 'citation_text', $self->{session}->xml->to_string( $citation_htmlobj ) );
$citation_dataobj->set_value( 'timestamp', EPrints::Time::get_iso_timestamp() );
$citation_dataobj->commit;

}
}
else
{
$citation_htmlobj = $citation->render( $self, in=>"citation ".$self->{dataset}->confid."/".$style, %params );
if ( $citation_caching_enabled )
{
my $citation_data = {
datasetid => $self->{dataset}->confid,
objectid => $self->id,
style => $style,
citation_text => $self->{session}->xml->to_string( $citation_htmlobj ),
timestamp => EPrints::Time::get_iso_timestamp(),
};
my $citation_dataobj = $self->{session}->dataset('citation')->create_object( $self->{session}, $citation_data );
}
}
return $citation_htmlobj;
}


Expand Down Expand Up @@ -1404,18 +1482,18 @@ sub uri
my( $self ) = @_;

return undef if !EPrints::Utils::is_set( $self->get_id );

my $ds_id = $self->get_dataset->confid;
if( $self->get_session->get_repository->can_call( "dataobj_uri", $ds_id ) )
{
return $self->get_session->get_repository->call( [ "dataobj_uri", $ds_id ], $self );
}

if ( EPrints::Utils::is_set( $self->get_session->get_repository->get_conf( "uri_url" ) ) )
{
return $self->get_session->get_repository->get_conf( "uri_url" ).$self->internal_uri;
}

return $self->get_session->get_repository->get_conf( "base_url" ).$self->internal_uri;
}

Expand Down

0 comments on commit 6aedd1c

Please sign in to comment.