Add repository metrics to homepage

From EPrints Documentation
Revision as of 14:11, 19 August 2020 by Libjlrs (talk | contribs)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search

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.

Another method would be to use Server Side Includes.

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>