Difference between revisions of "Contribute: Plugins/ExportPluginsZip"

From EPrints Documentation
Jump to: navigation, search
m (In More Detail)
m (Zip.pm)
Line 28: Line 28:
 
sub output_list
 
sub output_list
 
{
 
{
my ($plugin, %opts) = @_;
+
        my ($plugin, %opts) = @_;
  
my $archive = "";
+
        my $archive = '';
open(my $FH, '>', \$archive);
+
        open (my $FH, '>', \$archive) or
my $zip = Archive::Any::Create->new;
+
                die("Could not create filehandle: $!");
 +
        my $zip = Archive::Any::Create->new;
  
foreach my $dataobj ($opts{"list"}->get_records)
+
        my $otherplugin = $plugin->{session}->plugin("Export::MyPlugins::Excel");
{
 
my $dirpath = "eprints-search/".$dataobj->get_id()."/";
 
  
my $i = 1;
+
        my %optscopy = %opts;
foreach my $doc ($dataobj->get_all_documents)
+
        if (defined $opts{"fh"})
{
+
        {
my $subdirpath = $dirpath."doc$i/";
+
                $optscopy{"fh"} = undef;
my %files = $doc->files;
+
        }
 +
 
 +
        my $mdata = $otherplugin->output_list(%optscopy);
  
foreach my $filename (sort keys %files)
+
        $zip->add_file("eprints-search/metadata".$otherplugin->{suffix},$mdata);
{
 
my $filepath = $subdirpath.$filename;
 
my $file = $doc->local_path."/".$filename;
 
  
if (-d $file)
+
        foreach my $dataobj ($opts{"list"}->get_records)
{
+
        {
next;
+
                my $dirpath = "eprints-search/".$dataobj->get_id()."/";
}
 
  
my $data = '';
+
                my $i = 1;
open(my $datafh, '>', \$data);
+
                foreach my $doc ($dataobj->get_all_documents)
 +
                {
 +
                        my $subdirpath = $dirpath."doc$i/";
 +
                        my %files = $doc->files;
  
open (INFH, "<$file") or die ("Could not open file $file");
+
                        foreach my $filename (sort keys %files)
while (<INFH>)  
+
                        {
{
+
                                my $filepath = $subdirpath.$filename;
print {$datafh} $_;
+
                                my $file = $doc->local_path."/".$filename;
}
+
 
close INFH;
+
                                if (-d $file)
+
                                {
$zip->add_file($filepath, $data);
+
                                        next;
}
+
                                }
$i++;
+
 
}
+
                                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"})
 
         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;
 
}
 
}
  
 
1;
 
1;
 
  
 
</pre>
 
</pre>

Revision as of 19:01, 2 September 2007

Export Plugin Tutorial 5: Zip

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);
	my $zip = Archive::Any::Create->new;

	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.