Difference between revisions of "Contribute: Plugins/ExportPluginsZip"

From EPrints Documentation
Jump to: navigation, search
m (Export Plugin Tutorial 5: Zip)
m (In More Detail)
Line 102: Line 102:
  
 
= In More Detail =
 
= In More Detail =
 +
 
<pre>
 
<pre>
 
package EPrints::Plugin::Export::MyPlugins::Zip;
 
package EPrints::Plugin::Export::MyPlugins::Zip;
Line 112: Line 113:
 
sub new
 
sub new
 
{
 
{
        my ($class, %opts) = @_;
+
  my ($class, %opts) = @_;
  
        my $self = $class->SUPER::new(%opts);
+
  my $self = $class->SUPER::new(%opts);
  
        $self->{name} = "Zip";
+
  $self->{name} = "Zip";
        $self->{accept} = [ 'list/eprint' ];
+
  $self->{accept} = [ 'list/eprint' ];
        $self->{visible} = "all";
+
  $self->{visible} = "all";
        $self->{suffix} = ".zip";
+
  $self->{suffix} = ".zip";
        $self->{mimetype} = "application/zip";
+
  $self->{mimetype} = "application/zip";
  
        return $self;
+
  return $self;
 
}
 
}
  
 
sub output_list
 
sub output_list
 
{
 
{
        my ($plugin, %opts) = @_;
+
  my ($plugin, %opts) = @_;
  
        my $archive = '';
+
  my $archive = '';
        open (my $FH, '>', \$archive) or
+
  open (my $FH, '>', \$archive) or
                die("Could not create filehandle: $!");
+
    die("Could not create filehandle: $!");
        my $zip = Archive::Any::Create->new;
+
  my $zip = Archive::Any::Create->new;
  
        my $otherplugin = $plugin->{session}->plugin("Export::MyPlugins::Excel");
+
  my $otherplugin = $plugin->{session}->plugin("Export::MyPlugins::Excel");
  
        my %optscopy = %opts;
+
  my %optscopy = %opts;
        if (defined $opts{"fh"})
+
  if (defined $opts{"fh"})
        {
+
  {
                $optscopy{"fh"} = undef;
+
    $optscopy{"fh"} = undef;
        }
+
  }
  
        my $mdata = $otherplugin->output_list(%optscopy);
+
  my $mdata = $otherplugin->output_list(%optscopy);
  
        $zip->add_file("eprints-search/metadata".$otherplugin->{suffix},$mdata);
+
  $zip->add_file("eprints-search/metadata".$otherplugin->{suffix},$mdata);
  
        foreach my $dataobj ($opts{"list"}->get_records)
+
  foreach my $dataobj ($opts{"list"}->get_records)
        {
+
  {
                my $dirpath = "eprints-search/".$dataobj->get_id()."/";
+
    my $dirpath = "eprints-search/".$dataobj->get_id()."/";
  
                my $i = 1;
+
    my $i = 1;
                foreach my $doc ($dataobj->get_all_documents)
+
    foreach my $doc ($dataobj->get_all_documents)
                {
+
    {
                        my $subdirpath = $dirpath."doc$i/";
+
      my $subdirpath = $dirpath."doc$i/";
                        my %files = $doc->files;
+
      my %files = $doc->files;
  
                        foreach my $filename (sort keys %files)
+
      foreach my $filename (sort keys %files)
                        {
+
      {
                                my $filepath = $subdirpath.$filename;
+
        my $filepath = $subdirpath.$filename;
                                my $file = $doc->local_path."/".$filename;
+
        my $file = $doc->local_path."/".$filename;
  
                                if (-d $file)
+
        if (-d $file)
                                {
+
        {
                                        next;
+
          next;
                                }
+
        }
  
                                my $data = '';
+
        my $data = '';
                                open (my $datafh ,'>', \$data);
+
        open (my $datafh ,'>', \$data);
  
                                open (INFH, "<$file") or die ("Could not open file $file");
+
        open (INFH, "<$file") or die ("Could not open file $file");
                                while (<INFH>)
+
        while (<INFH>)
                                {
+
        {
                                        print {$datafh} $_;
+
          print {$datafh} $_;
                                }
+
        }
                                close INFH;
+
        close INFH;
  
                                $zip->add_file($filepath, $data);
+
        $zip->add_file($filepath, $data);
                        }
+
      }
                        $i++;
+
      $i++;
                }
+
    }
        }
+
  }
  
        if (defined $opts{"fh"})
+
  if (defined $opts{"fh"})
        {
+
  {
                $zip->write_filehandle($opts{"fh"},"zip");
+
    $zip->write_filehandle($opts{"fh"},"zip");
                return undef;
+
    return undef;
        }
+
  }
        $zip->write_filehandle($FH,"zip");
+
  $zip->write_filehandle($FH,"zip");
        return $archive;
+
  return $archive;
 
}
 
}
  

Revision as of 21:13, 2 September 2007

Export Plugin Tutorial 5: Zip

To prepare for this tutorial you should install the Archive::Any::Create module. The following command as root, or using sudo should work.

cpan Archive::Any::Create

Zip.pm

package EPrints::Plugin::Export::MyPlugins::Zip;

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

use strict;
use Archive::Any::Create;

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

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

  $self->{name} = "Zip";
  $self->{accept} = [ 'list/eprint' ];
  $self->{visible} = "all";
  $self->{suffix} = ".zip";
  $self->{mimetype} = "application/zip";

  return $self;
}

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

  my $archive = '';
  open (my $FH, '>', \$archive) or
    die("Could not create filehandle: $!");
  my $zip = Archive::Any::Create->new;

  my $otherplugin = $plugin->{session}->plugin("Export::MyPlugins::Excel");

  my %optscopy = %opts;
  if (defined $opts{"fh"})
  {
    $optscopy{"fh"} = undef;
  }

  my $mdata = $otherplugin->output_list(%optscopy);

  $zip->add_file("eprints-search/metadata".$otherplugin->{suffix},$mdata);

  foreach my $dataobj ($opts{"list"}->get_records)
  {
    my $dirpath = "eprints-search/".$dataobj->get_id()."/";

    my $i = 1;
    foreach my $doc ($dataobj->get_all_documents)
    {
      my $subdirpath = $dirpath."doc$i/";
      my %files = $doc->files;

      foreach my $filename (sort keys %files)
      {
        my $filepath = $subdirpath.$filename;
        my $file = $doc->local_path."/".$filename;

        if (-d $file)
        {
          next;
        }

        my $data = '';
        open (my $datafh ,'>', \$data);

        open (INFH, "<$file") or die ("Could not open file $file");
        while (<INFH>)
        {
          print {$datafh} $_;
        }
        close INFH;

        $zip->add_file($filepath, $data);
      }
      $i++;
    }
  }

  if (defined $opts{"fh"})
  {
    $zip->write_filehandle($opts{"fh"},"zip");
    return undef;
  }
  $zip->write_filehandle($FH,"zip");
  return $archive;
}

1;

In More Detail

package EPrints::Plugin::Export::MyPlugins::Zip;

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

use strict;
use Archive::Any::Create;

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

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

  $self->{name} = "Zip";
  $self->{accept} = [ 'list/eprint' ];
  $self->{visible} = "all";
  $self->{suffix} = ".zip";
  $self->{mimetype} = "application/zip";

  return $self;
}

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

  my $archive = '';
  open (my $FH, '>', \$archive) or
    die("Could not create filehandle: $!");
  my $zip = Archive::Any::Create->new;

  my $otherplugin = $plugin->{session}->plugin("Export::MyPlugins::Excel");

  my %optscopy = %opts;
  if (defined $opts{"fh"})
  {
    $optscopy{"fh"} = undef;
  }

  my $mdata = $otherplugin->output_list(%optscopy);

  $zip->add_file("eprints-search/metadata".$otherplugin->{suffix},$mdata);

  foreach my $dataobj ($opts{"list"}->get_records)
  {
    my $dirpath = "eprints-search/".$dataobj->get_id()."/";

    my $i = 1;
    foreach my $doc ($dataobj->get_all_documents)
    {
      my $subdirpath = $dirpath."doc$i/";
      my %files = $doc->files;

      foreach my $filename (sort keys %files)
      {
        my $filepath = $subdirpath.$filename;
        my $file = $doc->local_path."/".$filename;

        if (-d $file)
        {
          next;
        }

        my $data = '';
        open (my $datafh ,'>', \$data);

        open (INFH, "<$file") or die ("Could not open file $file");
        while (<INFH>)
        {
          print {$datafh} $_;
        }
        close INFH;

        $zip->add_file($filepath, $data);
      }
      $i++;
    }
  }

  if (defined $opts{"fh"})
  {
    $zip->write_filehandle($opts{"fh"},"zip");
    return undef;
  }
  $zip->write_filehandle($FH,"zip");
  return $archive;
}

1;

Testing Your Plugin

Restart your webserver and test the plugin as in the previous tutorial.