Difference between revisions of "Contribute: Plugins/ExportPluginsHelloOld"

From EPrints Documentation
Jump to: navigation, search
(In More Detail)
(In More Detail)
Line 40: Line 40:
 
package EPrints::Plugin::Export::Foo::HelloExport;
 
package EPrints::Plugin::Export::Foo::HelloExport;
 
</pre>
 
</pre>
Export plugins need to inherit from EPrints::Plugin::Export
+
Export plugins need to inherit from the EPrints::Plugin::Export class.
 
<pre>
 
<pre>
 
@ISA = ("EPrints::Plugin::Export");
 
@ISA = ("EPrints::Plugin::Export");
 
</pre>
 
</pre>
  
The Constructor for our plugin
+
The Constructor for our Plugin. After the class reference the second parameter is a hash of options.
 
<pre>
 
<pre>
 
sub new
 
sub new
 
{
 
{
</pre>
 
 
In addition to the class reference passed to the constructor a hash of options is also passed to the constructor.
 
<pre>
 
 
         my ($class, %opts) = @_;
 
         my ($class, %opts) = @_;
 
</pre>
 
</pre>
Line 60: Line 56:
 
</pre>
 
</pre>
 
Now we set a number of fields in our new plugin object.
 
Now we set a number of fields in our new plugin object.
 +
 +
This is the name that will appear in the export dropdown menu. The name should therefore be short and descriptive.
 
<pre>
 
<pre>
 
         $self->{name} = "Hello, World!";
 
         $self->{name} = "Hello, World!";
 
</pre>
 
</pre>
This is the name that will appear in the export dropdown menu. The name should therefore be short and descriptive.
+
The accept field is set to an array containing the type of objects this
<pre>
 
        $self->{accept} = [ 'dataobj/eprint', 'list/eprint' ];
 
</pre>
 
This field is set to an array containing the type of objects this
 
 
plugin can deal with. In this case lists of eprints and individual
 
plugin can deal with. In this case lists of eprints and individual
 
eprints.
 
eprints.
 
<pre>
 
<pre>
         $self->{visible} = "all";
+
         $self->{accept} = [ 'dataobj/eprint', 'list/eprint' ];
 
</pre>
 
</pre>
What class(es) of user will be able to see the plugin.
+
The visible field denoes 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 "all" 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 "staff" would
 
make the plugin only visible to repository staff.
 
make the plugin only visible to repository staff.
 +
<pre>
 +
        $self->{visible} = "all";
 +
</pre>
 +
The suffix field contains the extension of files exported by the plugin.
 
<pre>
 
<pre>
 
         $self->{suffix} = ".txt";
 
         $self->{suffix} = ".txt";
 
</pre>
 
</pre>
The suffix that will be appended to files exported by the plugin. For instance ".bib", ".txt" or ".xls"
+
This 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.
 
<pre>
 
<pre>
 
         $self->{mimetype} = "text/plain; charset=utf-8";
 
         $self->{mimetype} = "text/plain; charset=utf-8";
 
</pre>
 
</pre>
This defines the MIME type of the files exported by the plugin, this should be chosen so the file can be handled appropriately.
+
We then return our object reference.
You can also specify file encoding, for example "text/plain; charset=utf-8" to specify plaintext, encoded using utf-8.
 
 
<pre>
 
<pre>
 
         return $self;
 
         return $self;
 
}
 
}
 
</pre>
 
</pre>
 +
This subroutine handles the export of each DataObj.
 +
Besides a reference to the plugin object, this subroutine is also provided with a reference to an individual DataObj
 
<pre>
 
<pre>
# This subroutine handles the export of each DataObj.
 
# It returns a scalar.
 
 
sub output_dataobj
 
sub output_dataobj
 
{
 
{
        # Besides a reference to the plugin, this subroutine is
 
        # also provided with a reference to an individual DataObj
 
 
         my ($plugin, $dataobj) = @_;
 
         my ($plugin, $dataobj) = @_;
  
Line 102: Line 98:
 
         return $dataobj->get_value("title")."\n";
 
         return $dataobj->get_value("title")."\n";
 
}
 
}
 +
</pre>
  
# Standard Perl package fayre.
+
Standard Perl package fayre.
 +
<pre>
 
1;
 
1;
  
 
</pre>
 
</pre>

Revision as of 11:56, 9 August 2007

Hello, World!

HelloExport.pm

package EPrints::Plugin::Export::Foo::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

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

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

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

The Constructor for our Plugin. After the class reference the second parameter is a hash of options.

sub new
{
        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 in our new plugin object.

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 set to an array containing the type 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 denoes 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 only visible to repository staff.

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

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

        $self->{suffix} = ".txt";

This 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.

        $self->{mimetype} = "text/plain; charset=utf-8";

We then return our object reference.

        return $self;
}

This subroutine handles the export of each DataObj. Besides a reference to the plugin object, this subroutine is also provided with a reference to an individual DataObj

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

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

Standard Perl package fayre.

1;