Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Change DataObj name from Citation to CitationCache to avoid any confu…
…sion. Integrarated citation caching with generate/refresh views/abstracts. Allow citations to turn of caching in XML markup.
  • Loading branch information
drn05r authored and EPrints Services committed Jul 12, 2019
1 parent 71a322b commit 2d11e7c
Show file tree
Hide file tree
Showing 13 changed files with 202 additions and 115 deletions.
19 changes: 18 additions & 1 deletion bin/epadmin
Expand Up @@ -1402,6 +1402,15 @@ webserver will now be told to reload the apache configuration, although
restarting the server at this point is slightly more efficient.
END
if ( defined $repo->config( "citation_caching", "enabled" ) && $repo->config( "citation_caching", "enabled" ) )
{
print <<END;
You will also need to running "refresh_citations" if you have made changes
to any of the citation files that are used by view (browse) pages.
END
}

}

reload( $repoid );
Expand Down Expand Up @@ -1429,6 +1438,14 @@ webserver will now be told to reload the apache configuration, although
restarting the server at this point is slightly more efficient.
END
if ( defined $repo->config( "citation_caching", "enabled" ) && $repo->config( "citation_caching", "enabled" ) )
{
print <<END;
You will also need to run "refresh_citations" if you have made changes
to any of the citation files that are used by abstract (summary) pages.
END
}
}

reload( $repoid );
Expand All @@ -1451,7 +1468,7 @@ sub refresh_citations
if( $noise > 0 )
{
print <<END;
Citations will be updated when they are next requested. You may wish to run
Citation caches 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
Expand Down
1 change: 1 addition & 0 deletions bin/generate_abstracts
Expand Up @@ -144,6 +144,7 @@ exit;
sub make_static
{
my( $session, $dataset, $eprint, $info ) = @_;
$eprint->clear_citationcaches() if defined $session->config( "citation_caching", "enabled" ) && $session->config( "citation_caching", "enabled" );
$eprint->generate_static();
if( $noise > 1 )
{
Expand Down
17 changes: 17 additions & 0 deletions bin/generate_views
Expand Up @@ -69,6 +69,10 @@ May be repeated B<--view> I<viewid_1> B<--view> I<viewid_2>
Generate only pages for the language with this ID.
=item B<--refreshcitations>
Set citations to recached before generating view(s).
=item B<--help>
Print a brief help message and exit.
Expand Down Expand Up @@ -108,6 +112,7 @@ my $verbose = 0;
my $quiet = 0;
my $help = 0;
my $man = 0;
my $refreshcitations = 0;
my $generate_opt;
my @view_opts;
my $lang_opt;
Expand All @@ -121,6 +126,7 @@ GetOptions(
'verbose+' => \$verbose,
'silent' => \$quiet,
'quiet' => \$quiet,
'refreshcitations' => \$refreshcitations,
'generate=s' => \$generate_opt,
'view=s' => \@view_opts,
'lang=s' => \$lang_opt,
Expand Down Expand Up @@ -165,6 +171,17 @@ if( !defined $session )

$session->cache_subjects;

if ( $refreshcitations )
{
my $citations_file = $session->config( "variables_path" )."/citations.timestamp";
unless( open( CHANGEDFILE, ">$citations_file" ) )
{
EPrints::abort( "Cannot write to file $citations_file" );
}
print CHANGEDFILE "This file last poked at: ".EPrints::Time::human_time()."\n";
close CHANGEDFILE;
}

my $repository = $session->get_repository;

my $views = $repository->get_conf( "browse_views" );
Expand Down
7 changes: 7 additions & 0 deletions flavours/pub_lib/defaultcfg/cfg.d/citationcaches.pl
@@ -0,0 +1,7 @@
##List Dataset and Fields##
$c->{datasets}->{citationcache} = {
class => "EPrints::DataObj::CitationCache",
sqlname => "citationcache",
index => 0,
};
$c->{citation_caching}->{enabled} = 0;
7 changes: 0 additions & 7 deletions flavours/pub_lib/defaultcfg/cfg.d/citations.pl

This file was deleted.

7 changes: 7 additions & 0 deletions lib/defaultcfg_zero/cfg.d/citationcaches.pl
@@ -0,0 +1,7 @@
##List Dataset and Fields##
$c->{datasets}->{citationcache} = {
class => "EPrints::DataObj::CitationCache",
sqlname => "citationcache",
index => 0,
};
$c->{citation_caching}->{enabled} = 0;
7 changes: 0 additions & 7 deletions lib/defaultcfg_zero/cfg.d/citations.pl

This file was deleted.

2 changes: 1 addition & 1 deletion perl_lib/EPrints.pm
Expand Up @@ -284,7 +284,7 @@ use EPrints::DataObj;
use EPrints::DataObj::SubObject;
use EPrints::DataObj::Access;
use EPrints::DataObj::Cachemap;
use EPrints::DataObj::Citation;
use EPrints::DataObj::CitationCache;
use EPrints::DataObj::Document;
use EPrints::DataObj::EPrint;
use EPrints::DataObj::File;
Expand Down
2 changes: 2 additions & 0 deletions perl_lib/EPrints/Citation/EPC.pm
Expand Up @@ -44,11 +44,13 @@ sub load_source
$type = "default" unless EPrints::Utils::is_set( $type );

my $whitespace = $citation->getAttributeNodeNS( EP_NS_CITATION, "trim-whitespace" );
my $disable_caching = $citation->getAttributeNodeNS( EP_NS_CITATION, "disable-caching" );

$self->{type} = $type;
$self->{style} = $repo->xml->contents_of( $citation );
$self->{mtime} = EPrints::Utils::mtime( $file );
$self->{trim_whitespace} = defined $whitespace && lc($whitespace->nodeValue) eq "yes";
$self->{disable_caching} = defined $disable_caching && lc($disable_caching->nodeValue) eq "yes";

$repo->xml->dispose( $doc );

Expand Down
109 changes: 68 additions & 41 deletions perl_lib/EPrints/DataObj.pm
Expand Up @@ -621,26 +621,26 @@ sub clear_changed
=begin InternalDoc
=item $dataobj->clear_citations( )
=item $dataobj->clear_citationcaches( )
Clear any citations associated with the DataObj.
Clear any citationcaches associated with the DataObj.
=end InternalDoc
=cut
######################################################################

sub clear_citations
sub clear_citationcaches
{
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);
my $citationcaches_sql = "SELECT `citationcacheid` FROM citationcache WHERE `datasetid` = '" . $self->{dataset}->confid . "' AND `objectid` = '" . $self->id . "'";
my $statement = $db->prepare( $citationcaches_sql );
$db->execute($statement, $citationcaches_sql);
while ( my $res = $statement->fetchrow_hashref )
{
$self->{session}->dataset( "citation" )->dataobj( $res->{citationid} )->delete();
$self->{session}->dataset( "citationcache" )->dataobj( $res->{citationcacheid} )->delete();
}
}

Expand Down Expand Up @@ -704,7 +704,7 @@ sub commit
$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";
$self->clear_citationcaches() if defined $self->{session}->config( "citation_caching", "enabled" ) && $self->{session}->config( "citation_caching", "enabled" ) && $self->{dataset}->confid ne "citationcache";

return $success;
}
Expand Down Expand Up @@ -1179,53 +1179,80 @@ sub render_citation

my $citation_htmlobj = undef;
my $res = undef;
my $citation_caching_enabled = defined $self->{session}->config( "citation_caching", "enabled" ) && $self->{session}->config( "citation_caching", "enabled" );
my $citation_caching_enabled = defined $self->{session}->config( "citation_caching", "enabled" ) && $self->{session}->config( "citation_caching", "enabled" ) && !$citation->{disable_caching};

my $has_cached_version = undef; # Set if a cached version exists.
my $use_cached_version = undef; # Set if the cached version is to be used.
my $update_cached_version = undef; # Set if a cached version needs to be created/updated.

# Try and load a cached version of the citation.

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 $citations_sql = "SELECT `citationcacheid`, `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 `citationcache` 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} );
$db->execute($statement, $citations_sql);
$res = $statement->fetchrow_hashref;

my $timestampfile = $self->{session}->get_conf( "variables_path" )."/citations.timestamp";
my $refresh_unixtime = ( -e $timestampfile ) ? (stat( $timestampfile ))[9] : 0;
# If there is a cached version then check to see if is not older than the citations timestamp (generated by "epadmin refresh_citations").

if ( $citation_unixtime > $refresh_unixtime )
if (EPrints::Utils::is_set($res))
{
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;

}
}
$has_cached_version = 1;

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 )
{
$use_cached_version = 1;
}
else
{
$update_cached_version = 1;
}
}
else
{
$update_cached_version = 1;
}
}

if( $use_cached_version )
{
my $citation_text = "<citation>" . $res->{ 'citation_text' } . "</citation>";
$citation_htmlobj = EPrints::XML->contents_of( EPrints::XML->parse_string( $citation_text )->documentElement() ); # convert to HTML Object
}
else
{
$citation_htmlobj = $citation->render( $self, in=>"citation ".$self->{dataset}->confid."/".$style, %params );
if ( $citation_caching_enabled )
}

if( $update_cached_version )
{
my $citation_dataobj;

if( $has_cached_version )
{
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 );
$citation_dataobj = $self->{session}->dataset( "citationcache" )->dataobj( $res->{citationcacheid} );
}
else
{
$citation_dataobj = EPrints::DataObj::CitationCache::create( $self->{session}, { } );
}

$citation_dataobj->set_value( 'datasetid', $self->{dataset}->confid );
$citation_dataobj->set_value( 'objectid', $self->id );
$citation_dataobj->set_value( 'style', $style );
$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;
}

return $citation_htmlobj;
}

Expand Down

0 comments on commit 2d11e7c

Please sign in to comment.