Difference between revisions of "Contribute: Plugins/ExportPluginsHelloOld"

From EPrints Documentation
Jump to: navigation, search
m
 
(Hello, World!)
Line 1: Line 1:
 
= Hello, World! =
 
= Hello, World! =
 +
 +
== HelloExport.pm ==
 +
 +
<pre>
 +
package EPrints::Plugin::Export::Foo::HelloExport;
 +
 +
# Export plugins need to inherit from EPrints::Plugin::Export
 +
@ISA = ("EPrints::Plugin::Export");
 +
 +
use strict;
 +
 +
# The Constructor for our plugin
 +
sub new
 +
{
 +
        # In addition to the class reference passed to the constructor
 +
        # a hash of options is also passed to the constructor.
 +
        my ($class, %opts) = @_;
 +
 +
        # We create a new export plugin by calling the
 +
        # Eprints::Plugin::Export constructor
 +
        my $self = $class->SUPER::new(%opts);
 +
        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!";
 +
 +
        # 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
 +
        # eprints.
 +
        $self->{accept} = [ 'dataobj/eprint', 'list/eprint' ];
 +
 +
        # What class(es) of user 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 that will be appended to files exported by the plugin.
 +
        # For instance ".bib", ".txt" or ".xls"
 +
        $self->{suffix} = ".txt";
 +
 +
        # This defines the MIME type of the files exported by the plugin,
 +
        # this should be chosen so the file can be handled appropriately.
 +
        # You can also specify file encoding, for example
 +
        # "text/plain; charset=utf-8" to specify plaintext, encoded using
 +
        # utf-8.
 +
        $self->{mimetype} = "text/plain; charset=utf-8";
 +
 +
        return $self;
 +
}
 +
 +
# This subroutine handles the export of each DataObj.
 +
# It returns a scalar.
 +
sub output_dataobj
 +
{
 +
        # Besides a reference to the plugin, this subroutine is
 +
        # also provided with a reference to an individual DataObj
 +
        my ($plugin, $dataobj) = @_;
 +
 +
        # Format a string for the eprint, in this case we'll simply print
 +
        # its title.
 +
        my $retval = "TITLE>\t".$dataobj->get_value("title")."\n\n";
 +
 +
        # Return a scalar.
 +
        return $retval
 +
}
 +
 +
# This subroutine handles the export of lists of DataObjs.
 +
# If it is not provided output_dataobj will be run on every item in the list.
 +
# A common reason for implementing this function is to provide column headings.
 +
sub output_list
 +
{
 +
        # %opts contains arguments as well as the list of objects itself.
 +
        my ($plugin, %opts) = @_;
 +
 +
        # These headings will be printed before the list.
 +
        my $results = "Column\tHeading\n";
 +
 +
        # %opts may contain a filehandle that must be written to in $opts{fh}
 +
        # Otherwise we leave the above scalar for later.
 +
        if (defined $opts{fh})
 +
        {
 +
                print {$opts{fh}} $results;
 +
        }
 +
 +
        # Now we deal with the contents of the list.
 +
        foreach my $dataobj ($opts{list}->get_records)
 +
        {
 +
                # We call our output_dataobj to process the item
 +
                my $tmpdata = $plugin->output_dataobj($dataobj, %opts);
 +
 +
                if (defined $opts{fh})
 +
                {
 +
                        print {$opts{fh}} $results;
 +
                }
 +
                else
 +
                {
 +
                        $results = $results.$tmpdata;
 +
                }
 +
        }
 +
 +
        # If we've been using a filehandle we return nothing,
 +
        # otherwise we return a scalar.
 +
        if (defined $opts{fh})
 +
        {
 +
                return;
 +
        }
 +
        else
 +
        {
 +
                return $results;
 +
        }
 +
}
 +
 +
# Standard Perl package fayre.
 +
1;
 +
 +
</pre>

Revision as of 11:13, 9 August 2007

Hello, World!

HelloExport.pm

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

# Export plugins need to inherit from EPrints::Plugin::Export
@ISA = ("EPrints::Plugin::Export");

use strict;

# The Constructor for our plugin
sub new
{
        # In addition to the class reference passed to the constructor
        # a hash of options is also passed to the constructor.
        my ($class, %opts) = @_;

        # We create a new export plugin by calling the
        # Eprints::Plugin::Export constructor
        my $self = $class->SUPER::new(%opts);
        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!";

        # 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
        # eprints.
        $self->{accept} = [ 'dataobj/eprint', 'list/eprint' ];

        # What class(es) of user 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 that will be appended to files exported by the plugin.
        # For instance ".bib", ".txt" or ".xls"
        $self->{suffix} = ".txt";

        # This defines the MIME type of the files exported by the plugin,
        # this should be chosen so the file can be handled appropriately.
        # You can also specify file encoding, for example
        # "text/plain; charset=utf-8" to specify plaintext, encoded using
        # utf-8.
        $self->{mimetype} = "text/plain; charset=utf-8";

        return $self;
}

# This subroutine handles the export of each DataObj.
# It returns a scalar.
sub output_dataobj
{
        # Besides a reference to the plugin, this subroutine is
        # also provided with a reference to an individual DataObj
        my ($plugin, $dataobj) = @_;

        # Format a string for the eprint, in this case we'll simply print
        # its title.
        my $retval = "TITLE>\t".$dataobj->get_value("title")."\n\n";

        # Return a scalar.
        return $retval
}

# This subroutine handles the export of lists of DataObjs.
# If it is not provided output_dataobj will be run on every item in the list.
# A common reason for implementing this function is to provide column headings.
sub output_list
{
        # %opts contains arguments as well as the list of objects itself.
        my ($plugin, %opts) = @_;

        # These headings will be printed before the list.
        my $results = "Column\tHeading\n";

        # %opts may contain a filehandle that must be written to in $opts{fh}
        # Otherwise we leave the above scalar for later.
        if (defined $opts{fh})
        {
                print {$opts{fh}} $results;
        }

        # Now we deal with the contents of the list.
        foreach my $dataobj ($opts{list}->get_records)
        {
                # We call our output_dataobj to process the item
                my $tmpdata = $plugin->output_dataobj($dataobj, %opts);

                if (defined $opts{fh})
                {
                        print {$opts{fh}} $results;
                }
                else
                {
                        $results = $results.$tmpdata;
                }
        }

        # If we've been using a filehandle we return nothing,
        # otherwise we return a scalar.
        if (defined $opts{fh})
        {
                return;
        }
        else
        {
                return $results;
        }
}

# Standard Perl package fayre.
1;