Difference between revisions of "Contribute: Plugins/ExportPluginsHello"

From EPrints Documentation
Jump to: navigation, search
(HelloExport.pm)
m (Two identical typos corrected (missing slash in directory name))
Line 10: Line 10:
 
= Before You Start =
 
= Before You Start =
  
Create a directory for your export plugins in the local repository cfg directory, <EPrints_root>/archives/<archive_name>cfg/plugins/EPrints/Plugin/Export. Note archvies/<archive_name>cfg/plugins/ is a special directory which is added to the perl path when your webserver starts. Then in the directory create a directory for your plugins. The directory used for these examples is called "MyPlugins".
+
Create a directory for your export plugins in the local repository cfg directory, <EPrints_root>/archives/<archive_name>/cfg/plugins/EPrints/Plugin/Export. Note archvies/<archive_name>/cfg/plugins/ is a special directory which is added to the perl path when your webserver starts. Then in the directory create a directory for your plugins. The directory used for these examples is called "MyPlugins".
  
 
= HelloExport.pm =
 
= HelloExport.pm =

Revision as of 13:42, 28 May 2015

There is an old version of this tutorial hereContribute: Plugins/ExportPluginsHelloOld Use this if your not on 3.2+ yet.

Export Plugin Tutorial 1: "Hello, World!"

In this tutorial you will learn how to create a simple export plugin for EPrints, which will generate a list of titles from the results of a search. A basic knowledge of Perl is needed, but the code will be explained fully.

Before You Start

Create a directory for your export plugins in the local repository cfg directory, <EPrints_root>/archives/<archive_name>/cfg/plugins/EPrints/Plugin/Export. Note archvies/<archive_name>/cfg/plugins/ is a special directory which is added to the perl path when your webserver starts. Then in the directory create a directory for your plugins. The directory used for these examples is called "MyPlugins".

HelloExport.pm

Now place the code below in a file called HelloExport.pm in that directory.

package EPrints::Plugin::Export::MyPlugins::HelloExport;

@ISA = ('EPrints::Plugin::Export');

use strict;

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

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

        $self->{name} = 'Hello, World!';
        $self->{accept} = [ 'dataobj/eprint', 'list/eprint' ];
        $self->{visible} = 'all';
        $self->{suffix} = '.txt';
        $self->{mimetype} = 'text/plain; charset=utf-8';

        return $self;
}

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

        return $dataobj->get_value('title')."\n";
}

1;

In More Detail

Housekeeping

package EPrints::Plugin::Export::Foo::HelloExport;

Export plugins need to inherit from the EPrints::Plugin::Export class.

@ISA = ('EPrints::Plugin::Export');

Constructor

After the implicit class reference, a hash of options is given.

        my ($class, %opts) = @_;

We create a new export plugin by calling the Eprints::Plugin::Export constructor

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

Now we set a number of fields to register our new plugin.

This is the name that will appear in the export dropdown menu. The name should therefore be short and descriptive.

        $self->{name} = 'Hello, World!';

The accept field is a list containing the types of objects this plugin can deal with. In this case lists of eprints and individual eprints.

        $self->{accept} = [ 'dataobj/eprint', 'list/eprint' ];

The visible field denotes the class of user which will be able to see the plugin. For most export plugins the value 'all' will be required, allowing all users to see and use the plugin. A value of 'staff' would make the plugin visible only to repository staff.

        $self->{visible} = 'all';

The suffix field contains the extension of files exported by the plugin.

The mimetype field defines the MIME type of the files exported by the plugin You can also specify file encoding, for example 'text/plain; charset=utf-8' to specify plain text, encoded using UTF-8.UTF-8 is a Unicode character encoding capable of expressing characters from a large number of character sets and so is usually preferable to ASCII

        $self->{suffix} = '.txt';
        $self->{mimetype} = 'text/plain; charset=utf-8';

We then return our plugin reference.

        return $self;

Processing DataObjs

This method handles the export of each DataObj. DataObjs make up most of the content of an EPrint repository. The three main types are EPrint defining individual eprints, Document defining collections of one or more files belonging to an EPrints, and User which defines users of the repository.

Besides an implicit reference to the Plugin object, this method is also provided with a reference to an individual DataObj. It is called by several CGI and command line scripts to export single DataObjs, for instance the item control screen for repository staff. It is also called by the list handling method on each DataObj in a list, for example the results of a search. That will be explained in the next tutorial.

In the example below we get the title of each DataObj, but there are large number of fields which you can extract from each DataObj. For example try changing "title" to "abstract" to print the abstract of each eprint.

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

        # Return a scalar containing the title.
        return $dataobj->get_value('title')."\n";
}

Finishing Off

Standard Perl package requirement.

1;

Testing Your Plugin

Remember you must always restart your web server when you change perl code in EPrints to reload the perl. Alternatively you can call bin/epadmin reload <archive_id>.

Now perform a search.

If all is well your plugin should appear in the dropdown menu. Select it and click export. As long as the search provided some results, you should get a list of EPrint titles returned.

Sample Output

Exphello.png