Difference between revisions of "Contribute: Plugins/ExportPluginsHello"

From EPrints Documentation
Jump to: navigation, search
m (reformatted and some typos corrected)
 
(2 intermediate revisions by 2 users not shown)
Line 1: Line 1:
There is an old version of this tutorial here[[Contribute: Plugins/ExportPluginsHelloOld]] Use this if your not on 3.2+ yet.
+
There is an [[Contribute:_Plugins/ExportPluginsHelloOld|old version]] of this tutorial here for all installations which aren't upgraded to 3.2+ yet.
  
 
[[Category:Contribute]]
 
[[Category:Contribute]]
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, <tt>''<EPrints_root>''/archives/''<archive_name>''/cfg/plugins/EPrints/Plugin/Export</tt>. Note <tt>archives/''<archive_name>''/cfg/plugins/</tt> 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 <tt>MyPlugins</tt>.
  
 
= HelloExport.pm =
 
= HelloExport.pm =
  
Now place the code below in a file called HelloExport.pm in that directory.
+
Now place the code below in a file called <tt>HelloExport.pm</tt> in that directory.
  
 
<pre>
 
<pre>
Line 48: Line 48:
  
 
</pre>
 
</pre>
 
  
 
= In More Detail =
 
= In More Detail =
Line 54: Line 53:
 
== Housekeeping ==
 
== Housekeeping ==
 
<pre>
 
<pre>
package EPrints::Plugin::Export::Foo::HelloExport;
+
package EPrints::Plugin::Export::MyPlugins::HelloExport;
 
</pre>
 
</pre>
 
Export plugins need to inherit from the EPrints::Plugin::Export class.
 
Export plugins need to inherit from the EPrints::Plugin::Export class.
Line 85: Line 84:
 
</pre>
 
</pre>
 
The visible field denotes the class of user which will be able to see the plugin.
 
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
+
For most export plugins the value <tt>all</tt> will be required, allowing
all users to see and use the plugin. A value of 'staff' would
+
all users to see and use the plugin. A value of <tt>staff</tt> would
 
make the plugin visible only to repository staff.
 
make the plugin visible only to repository staff.
 
<pre>
 
<pre>
Line 94: Line 93:
  
 
The mimetype field defines the [http://en.wikipedia.org/wiki/MIME MIME] type of the files exported by the plugin
 
The mimetype field defines the [http://en.wikipedia.org/wiki/MIME 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.[http://en.wikipedia.org/wiki/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 [http://en.wikipedia.org/wiki/ASCII ASCII]
+
You can also specify file encoding, for example 'text/plain; charset=utf-8' to specify plain text, encoded using [http://en.wikipedia.org/wiki/UTF-8 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 [http://en.wikipedia.org/wiki/ASCII ASCII].
 
<pre>
 
<pre>
 
         $self->{suffix} = '.txt';
 
         $self->{suffix} = '.txt';
Line 107: Line 106:
 
== Processing DataObjs ==
 
== 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.
+
This method handles the export of each DataObj. DataObjs make up most of the content of an EPrint repository. The three main types are <tt>EPrint</tt> defining individual eprints, <tt>Document</tt> defining collections of one or more files belonging to an EPrints, and <tt>User</tt> 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 [http://en.wikipedia.org/wiki/Common_Gateway_Interface 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 [[Contribute:_Plugins/ExportPluginsList|the next tutorial]].
 
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 [http://en.wikipedia.org/wiki/Common_Gateway_Interface 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 [[Contribute:_Plugins/ExportPluginsList|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.
+
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 <tt>title</tt> to <tt>abstract</tt> to print the abstract of each eprint.
  
 
<pre>
 
<pre>
Line 132: Line 131:
  
 
= Testing Your Plugin =
 
= 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>.  
+
Remember you must always '''restart your web server''' when you change perl code in EPrints to reload the perl. Alternatively you can call <tt>bin/epadmin reload ''<archive_id>''</tt>.  
  
 
Now  perform a search.
 
Now  perform a search.

Latest revision as of 12:10, 7 September 2015

There is an old version of this tutorial here for all installations which aren't upgraded to 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 archives/<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::MyPlugins::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