Difference between revisions of "Add repository metrics to homepage"

From EPrints Documentation
Jump to: navigation, search
(Created page with "This page was created in response to a question on the EPrints Tech List. There are a number of ways to achieve this. The approach below is a fairly minimal solution. It uses...")
 
(More properly... use dataset count)
Line 14: Line 14:
  
 
use strict;
 
use strict;
use warnings;
 
  
my $session = EPrints::Session->new();
+
my $eprints = EPrints->new;
 +
my $repo = $eprints->current_repository;
 +
exit( 0 ) unless( defined $repo );
  
my $r = $session->get_request;
+
$repo->send_http_header( content_type=>"text/plain; charset=UTF-8" );
  
my $sql = "SELECT COUNT(*) FROM eprint WHERE eprint_status = 'archive'";
+
my $dataset = $repo->dataset( "archive" );
 +
print sprintf("<p>There are %d records in this archive.</p>",
 +
                $dataset->count);
  
my $sth = $session->get_database->prepare_select( $sql );
 
$session->get_database->execute( $sth , $sql );
 
 
my ($count) = $sth->fetchrow_array;
 
 
$session->send_http_header( content_type => "text/html; charset=UTF-8" );
 
if( $count ){
 
        print "<p>There are $count records.</p>";
 
}
 
 
exit;
 
exit;
 
 
</source>
 
</source>
  
The script above isn't perfect - it should possibly use a phrase for the returned paragraph (with the $count added using a pin), and the script could search the 'archive' dataset, rather than looking into the database.
+
The script above isn't perfect - it should possibly use a phrase for the returned paragraph (with the $count added using a pin).
There is no error checking (so if the database doesn't respond, you'll probably get an ugly message).
 
  
  

Revision as of 17:13, 13 August 2019

This page was created in response to a question on the EPrints Tech List.

There are a number of ways to achieve this. The approach below is a fairly minimal solution. It uses a small piece of javascript to pull data back from a custom CGI script.

One alternative solution would be to create an EPScript method, and include the output of that in the index page. The problem with that approach is that the index gets saved as a static document, and wouldn't by default include an up-to-date count of items.


Required components

1. Script to return paragraph element detailing total items - saved as [EPRINTS_ROOT]/archives/[ARCHIVEID]/cgi/item_count

use EPrints;

use strict;

my $eprints = EPrints->new;
my $repo = $eprints->current_repository;
exit( 0 ) unless( defined $repo );

$repo->send_http_header( content_type=>"text/plain; charset=UTF-8" );

my $dataset = $repo->dataset( "archive" );
print sprintf("<p>There are %d records in this archive.</p>",
                $dataset->count);

exit;

The script above isn't perfect - it should possibly use a phrase for the returned paragraph (with the $count added using a pin).


2. Addition to the index page [EPRINTS_ROOT]/archives/[ARCHIVEID]/cfg/lang/en/static/index.xpage This calls the script above and adds the returned text to the element added

<div id="item_count" />
<script>
document.observe( "dom:loaded", function() {
    // The first parameter below is the id of the element added above.
    // The second is the URL to call
    // The third is a hash of Ajax options
    // The 'Updater' class adds the returned html to the specified element.
    new Ajax.Updater( "item_count", 'cgi/total_items', { 'method': 'GET' } );
});
</script>