Difference between revisions of "Create Export Plugins"

From EPrints Documentation
Jump to: navigation, search
Line 11: Line 11:
 
  use Unicode::String qw( utf8 );
 
  use Unicode::String qw( utf8 );
 
  use EPrints::Plugin::Export;
 
  use EPrints::Plugin::Export;
 +
use EPrints::DataObj::User;
 
  @ISA = ( "EPrints::Plugin::Export" );
 
  @ISA = ( "EPrints::Plugin::Export" );
 
  use strict;
 
  use strict;
Line 22: Line 23:
 
         $self->{name} = "Depositor Activity";
 
         $self->{name} = "Depositor Activity";
 
         $self->{accept} = [ 'list/eprint', 'dataobj/eprint' ];
 
         $self->{accept} = [ 'list/eprint', 'dataobj/eprint' ];
         $self->{visible} = "staff";
+
         $self->{visible} = "all";
 
         $self->{suffix} = ".csv";
 
         $self->{suffix} = ".csv";
 
         $self->{mimetype} = "text/csv";
 
         $self->{mimetype} = "text/csv";
Line 33: Line 34:
 
* name - The name of the filter
 
* name - The name of the filter
 
* accept - A list detailing what the filter will take as inputs.  In this case, a list of eprints or a single eprint.  It is possible to write filters for dataobj types 'eprint', 'user', 'subject', 'history', 'access' and '*' (all).
 
* accept - A list detailing what the filter will take as inputs.  In this case, a list of eprints or a single eprint.  It is possible to write filters for dataobj types 'eprint', 'user', 'subject', 'history', 'access' and '*' (all).
* visible - Who can see this filter.  It's set to staff above so that only repository staff can use it.  It could be set to 'All' to allow everyone to use it.  If set to 'API' then the filter is not available through the web interface.
+
* visible - Who can see this filter.  It's set to 'all' above so that anyone can use it.  It could be set to 'staff' to only allow repository staff to use it.  If set to 'API' then the filter is not available through the web interface.
 
* suffix - Appended to the url to create a filename extension.
 
* suffix - Appended to the url to create a filename extension.
 
* mimetype - Should be set to the correct mime type for the output of the filter.
 
* mimetype - Should be set to the correct mime type for the output of the filter.
  
 
Note that 'name' and 'accept' are essential.  These allow the filter to register itself with EPrints.
 
Note that 'name' and 'accept' are essential.  These allow the filter to register itself with EPrints.
 +
 +
We will be extracting the username of the depositor, so we need to use 'EPrints::DataObj::User'.
  
 
== Converting the dataobj ==
 
== Converting the dataobj ==
Line 47: Line 50:
 
  sub output_dataobj
 
  sub output_dataobj
 
  {
 
  {
        my( $plugin, $dataobj ) = @_;
+
      my( $plugin, $dataobj ) = @_;
       
+
        my $r = "";
+
      my $r = "";
        if ($dataobj->exists_and_set("userid"))
+
      if ($dataobj->exists_and_set("userid"))
        {
+
      {
                my $depositor = $dataobj->get_value( "userid" );
+
              my $session = $plugin->{"session"};
                 if ($depositor =~ m/[\n" ,]/)  #Check for illegal CSV characters
+
              my $userid = $dataobj->get_value( "userid" );
                {
+
 
                        $depositor =~ s/"/""/g; #escape quotes
+
              my $depositor_obj = new EPrints::DataObj::User($session, $userid);
                        $depositor = '"' . $depositor . '"'; #delimit text
+
                 my $depositor = $depositor_obj->get_value( "username" );
                }
+
              if ($depositor =~ m/[\n" ,]/)  #Check for illegal CSV characters
                $r .= $depositor;
+
              {
        }
+
                      $depositor =~ s/"/""/g; #escape quotes
        else
+
                      $depositor = '"' . $depositor . '"'; #delimit text
        {
+
              }
                $r .= '"Depositor Unknown"';
+
              $r .= $depositor;
        }
+
      }
        $r .= ',' . $dataobj->get_value( "datestamp" ) . "\n";
+
      else
       
+
      {
        return $r;
+
              $r .= '"Depositor Unknown"';
 +
      }
 +
      $r .= ',' . $dataobj->get_value( "datestamp" ) . "\n";
 +
 +
      return $r;
 
  }
 
  }
  

Revision as of 11:51, 12 February 2007

Export plugins for anything but eprints are beyond the scope of this howto.

Imagine we want to create an export plugin that will take a group of eprints (or a single eprint) and output a csv file containing a list of who deposited the eprints, and the dates on which they were deposited.

Essentials

The top of the plugin should look like this:

package EPrints::Plugin::Export::DepositorActivity;

use Unicode::String qw( utf8 );
use EPrints::Plugin::Export;
use EPrints::DataObj::User;
@ISA = ( "EPrints::Plugin::Export" );
use strict;

sub new
{
       my( $class, %params ) = @_;

       my $self = $class->SUPER::new( %params );

       $self->{name} = "Depositor Activity";
       $self->{accept} = [ 'list/eprint', 'dataobj/eprint' ];
       $self->{visible} = "all";
       $self->{suffix} = ".csv";
       $self->{mimetype} = "text/csv";

       return $self;
}

This will create a filter object, and set a number of configuration constants:

  • name - The name of the filter
  • accept - A list detailing what the filter will take as inputs. In this case, a list of eprints or a single eprint. It is possible to write filters for dataobj types 'eprint', 'user', 'subject', 'history', 'access' and '*' (all).
  • visible - Who can see this filter. It's set to 'all' above so that anyone can use it. It could be set to 'staff' to only allow repository staff to use it. If set to 'API' then the filter is not available through the web interface.
  • suffix - Appended to the url to create a filename extension.
  • mimetype - Should be set to the correct mime type for the output of the filter.

Note that 'name' and 'accept' are essential. These allow the filter to register itself with EPrints.

We will be extracting the username of the depositor, so we need to use 'EPrints::DataObj::User'.

Converting the dataobj

The 'output_dataobj' function takes a dataobj (in our case an eprint object) and returns a perl scalar which will be the output. We are going to extract some data from the dataobj using EPrints API calls.

Note that by convention, '$plugin' is used instead of '$self'.

sub output_dataobj
{
      my( $plugin, $dataobj ) = @_;

      my $r = "";
      if ($dataobj->exists_and_set("userid"))
      {
              my $session = $plugin->{"session"};
              my $userid = $dataobj->get_value( "userid" );
              my $depositor_obj = new EPrints::DataObj::User($session, $userid);
               my $depositor = $depositor_obj->get_value( "username" );
              if ($depositor =~ m/[\n" ,]/)  #Check for illegal CSV characters
              {
                      $depositor =~ s/"/""/g; #escape quotes
                      $depositor = '"' . $depositor . '"'; #delimit text
              }
              $r .= $depositor;
      }
      else
      {
              $r .= '"Depositor Unknown"';
      }
      $r .= ',' . $dataobj->get_value( "datestamp" ) . "\n";

      return $r;
}

Put it in a Module

Put all this into a file called 'DepositorActivity.pm' and save the file into the 'eprints3/perl_lib/EPrints/Plugin/Export/' directory. Don't forget to add this to the bottom of the file:

1;