Difference between revisions of "Contribute: Plugins/ExportPluginsList"

From EPrints Documentation
Jump to: navigation, search
(Housekeeping)
(Filehandles: Bringing code in line.)
Line 99: Line 99:
 
         my ($plugin, %opts) = @_;
 
         my ($plugin, %opts) = @_;
  
         my $output = "";
+
         my $r = [];
  
         $output .= "ID\tTitle\n\n";
+
         my $header = "ID\tTitle\n\n";
 +
        if (defined $opts{"fh"})
 +
        {
 +
                print {$opts{fh}} $header;
 +
        }
 +
        else
 +
        {
 +
                push @{$r}, $header;
 +
        }
  
 
         foreach my $dataobj ($opts{"list"}->get_records)
 
         foreach my $dataobj ($opts{"list"}->get_records)
 
         {
 
         {
                 $output .= $plugin->output_dataobj($dataobj, %opts);
+
                 my $part = $plugin->output_dataobj($dataobj, %opts);
 +
                if (defined $opts{"fh"})
 +
                {
 +
                        print {$opts{fh}} $part;
 +
                }
 +
                else
 +
                {
 +
                        push @{$r}, $part;
 +
                }
 
         }
 
         }
  
 
         if (defined $opts{"fh"})
 
         if (defined $opts{"fh"})
 
         {
 
         {
                print {$opts{"fh"}} $output;
+
                 return undef;
                 return;
 
        }
 
        else
 
        {
 
                return $output;
 
 
         }
 
         }
 +
        return join('', @{$r});
 
}
 
}
 
</pre>
 
</pre>

Revision as of 12:10, 14 August 2007

Export Plugin Tutorial 2: Hello, Lists

In this tutorial you will learn to create a slightly more complex export plugin than the one created in the previous tutorial by overriding the default handling of lists.

HelloList.pm

The code in the section below should be placed in a file called HelloList.pm in the directory created previously, and MyPlugins should be changed to the name of that directory.

package EPrints::Plugin::Export::MyPlugins::HelloList;

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

use strict;

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

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

        $self->{name} = "Hello, List!";
        $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_id()."\t".$dataobj->get_value("title")."\n";
}

sub output_list
{
        my ($plugin, %opts) = @_;

        my $output = "";

        $output .= "ID\tTitle\n\n";

        foreach my $dataobj ($opts{"list"}->get_records)
        {
                $output .= $plugin->output_dataobj($dataobj, %opts);
        }

        return $output;
}

1;

In More Detail

The above code is very similar to the HelloExport.pm file in the previous tutorial so only the points where it deviates significantly from that file will be discussed below.

Housekeeping

The package name has been changed to reflect the filename.

package EPrints::Plugin::Export::Foo::HelloList;

Constructor

Make sure you give each plugin a unique name.

        $self->{name} = "Hello, List!";

Dealing With Lists

sub output_list
{
        my ($plugin, %opts) = @_;

        my $output = "";

        $output .= "ID\tTitle\n\n";

        foreach my $dataobj ($opts{"list"}->get_records)
        {
                $output .= $plugin->output_dataobj($dataobj, %opts);
        }

        return $output;
}

1;

Filehandles

sub output_list
{
        my ($plugin, %opts) = @_;

        my $r = [];

        my $header = "ID\tTitle\n\n";
        if (defined $opts{"fh"})
        {
                print {$opts{fh}} $header;
        }
        else
        {
                push @{$r}, $header;
        }

        foreach my $dataobj ($opts{"list"}->get_records)
        {
                my $part = $plugin->output_dataobj($dataobj, %opts);
                if (defined $opts{"fh"})
                {
                        print {$opts{fh}} $part;
                }
                else
                {
                        push @{$r}, $part;
                }
        }

        if (defined $opts{"fh"})
        {
                return undef;
        }
        return join('', @{$r});
}