Jump to content
Wiki Home
EPrints Home
Bazaar
Files
Main menu
Main menu
move to sidebar
hide
Navigation
Front Page
EPrints 3 Manual
EPrints Training Course
EPrints Glossary
Installing EPrints
How-to Configure EPrints
EPrints 3.2+ API
EPrints 3 Reference
EPrints Plugins
Wiki
All categories
Recent changes
Main Page
Help
EPrints Documentation
Search
Search
Appearance
Log in
Request account
Personal tools
Log in
Request account
Pages for logged out editors
learn more
Contributions
Talk
Editing
Contribute: Plugins/ExportPluginsZip
(section)
Page
Discussion
British English
Read
Edit
View history
Tools
Tools
move to sidebar
hide
Actions
Read
Edit
View history
General
What links here
Related changes
Special pages
Page information
Appearance
move to sidebar
hide
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
== List Handling == === Setting Up === Here we setup an in-memory file for the Zip, and create an Archive object. <pre> my $archive = ''; open (my $FH, '>', \$archive) or die("Could not create filehandle: $!"); my $zip = Archive::Any::Create->new; </pre> === Navigation === Here we begin to setup the HTML file that we'll add to our archive for navigation. First we setup a header. <pre> my $index = <<END; <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"> <head> <meta http-equiv="Content-Type" content="text/html;charset=utf-8"/> <title>EPrints Search Results</title> </head> <body> END </pre> Now we get the Session object, we'll be using it to manipulate [http://en.wikipedia.org/wiki/Document_Object_Model DOM] objects later. <pre> my $session = $plugin->{session}; </pre> === Handling DataObjs === We loop over the DataObjs as we have done before. This time we setup some [http://en.wikipedia.org/wiki/Document_Object_Model DOM] objects to be added to our index. Each eprint will have it's title printed out followed by a list of documents. <pre> my $div = $session->make_element('div'); my $heading = $session->make_element('h2'); $heading->appendChild($session->make_text($dataobj->get_value('title'))); $div->appendChild($heading); my $uldoc = $session->make_element('ul'); $div->appendChild($uldoc); </pre> We create a directory for each eprint. Note it is not necessary to explicitly create a directory, we simply have to set the appropriate file path. However this means that if you do not add files to a certain directory it will not be created, rather than having an empty directory for a given eprint. <pre> my $dirpath = 'eprints-search/'.$dataobj->get_id().'/'; </pre> ==== Dealing With Documents ==== We then loop over all the documents belonging to each DataObj. The get_all_documents method returns an array of Document objects. <pre> my $i = 1; foreach my $doc ($dataobj->get_all_documents) { my $subdirpath = $dirpath."doc$i/"; </pre> Here we create a list item for the document containing a link to the main file. <pre> my $lidoc = $session->make_element('li'); $uldoc->appendChild($lidoc); my $adoc = $session->make_element('a', href=>$dataobj->get_id."/doc$i/".$doc->get_main); $lidoc->appendChild($adoc); </pre> If a description of the main file has been set we use that as the link text, otherwise we use the filename. <pre> if ($doc->exists_and_set('formatdesc')) { $adoc->appendChild($session->make_text($doc->get_value('formatdesc'))); } else { $adoc->appendChild($session->make_text($doc->get_main)); } </pre> ==== Dealing With Files ==== The files method of the Document object returns a hash whose keys are file names and values are file sizes. <pre> my %files = $doc->files; </pre> We loop over each file belonging to the document, in most cases there will only be one file. <pre> foreach my $filename (sort keys %files) { my $filepath = $subdirpath.$filename; my $file = $doc->local_path.'/'.$filename; </pre> We need to read the contents of the file and add it to a file in the zip. First we'll create another in-memory file to hold the contents. <pre> my $data = ''; open (my $datafh ,'>', \$data); </pre> We open our file and print it straight out to our in-memory file. <pre> open (INFH, "<$file") or die ("Could not open file $file"); while (<INFH>) { print {$datafh} $_; } close INFH; </pre> Then we add the file data to our file. <pre> $zip->add_file($filepath, $data); </pre> Finally we add the [http://en.wikipedia.org/wiki/Document_Object_Model DOM] object for our eprint to the index. <pre> $index .= EPrints::XML::to_string($div); </pre> === Finishing Off === After finishing off our index file we add it to the zip file. <pre> $index .= '</body></html>'; $zip->add_file('eprints-search/index.htm',$index); </pre> If a file handle has been provided we write to it, otherwise we write to the scalar file handle created earlier. We then return in the usual fashion. <pre> if (defined $opts{fh}) { $zip->write_filehandle($opts{fh},'zip'); return undef; } $zip->write_filehandle($FH,'zip'); return $archive; </pre>
Summary:
Please note that all contributions to EPrints Documentation may be edited, altered, or removed by other contributors. If you do not want your writing to be edited mercilessly, then do not submit it here.
You are also promising us that you wrote this yourself, or copied it from a public domain or similar free resource (see
EPrints Documentation:Copyrights
for details).
Do not submit copyrighted work without permission!
Cancel
Editing help
(opens in new window)