/** * This is a use case for creating and populating a new bag. The user * does these actions: * * <ol> * <li>Create a new bag;</li> * <li>Add files to the bag;</li> * <li>Add fetch entries;</li> * <li>Update the bag; and</li> * <li>Package the bag.</li> * </ol> */ public function testBagProducer() { $tmpdir = tmpdir(); mkdir($tmpdir); $this->queueRm($tmpdir); $tmpbag = "{$tmpdir}/BagProducer"; // 1. Create a new bag; $bag = new BagIt($tmpbag); $this->assertTrue($bag->isValid()); $this->assertTrue($bag->isExtended()); $bagInfo = $bag->getBagInfo(); $this->assertEquals('0.96', $bagInfo['version']); $this->assertEquals('UTF-8', $bagInfo['encoding']); $this->assertEquals('sha1', $bagInfo['hash']); $this->assertEquals("{$tmpbag}/data", $bag->getDataDirectory()); $this->assertEquals('sha1', $bag->getHashEncoding()); $this->assertEquals(0, count($bag->getBagContents())); $this->assertEquals(0, count($bag->getBagErrors())); // 2. Add files to the bag; $srcdir = __DIR__ . '/TestBag/data'; $bag->addFile("{$srcdir}/README.txt", 'data/README.txt'); $bag->addFile("{$srcdir}/imgs/uvalib.png", 'data/payloads/uvalib.png'); // This needs to add data/ to the beginning of the file. $bag->addFile("{$srcdir}/imgs/fibtriangle-110x110.jpg", 'payloads/fibtri.jpg'); // 3. Add fetch entries; $bag->fetch->add('http://www.scholarslab.org/', 'data/index.html'); // 4. Update the bag; and $bag->update(); // 5. Package the bag. $pkgfile = "{$tmpdir}/BagProducer.tgz"; $bag->package($pkgfile); // Finally, we need to validate the contents of the package. $dest = new BagIt($pkgfile); $this->queueRm($dest->bagDirectory); // First, verify that the data files are correct. $this->assertEquals("BagIt-Version: 0.96\n" . "Tag-File-Character-Encoding: UTF-8\n", file_get_contents($dest->bagitFile)); // Second, verify that everything was uncompressed OK. $dest->validate(); $this->assertEquals(0, count($dest->bagErrors)); // Now, check that the file was fetched. $dest->fetch->download(); $this->assertFileExists("{$dest->bagDirectory}/data/index.html"); // Also, check that fibtri.jpg was added in the data/ directory. $this->assertFalse(file_exists("{$dest->bagDirectory}/payloads/fibtri.jpg")); $this->assertFileExists("{$dest->bagDirectory}/data/payloads/fibtri.jpg"); }
public function testPackageTGz() { $tmp = tmpdir(); try { mkdir($tmp); mkdir($tmp . '/data'); file_put_contents($tmp . '/data/missing.txt', 'This space intentionally left blank.\\n'); file_put_contents($tmp . "/fetch.txt", "http://www.google.com - data/google/index.html\n" . "http://www.yahoo.com - data/yahoo/index.html\n"); $bag = new BagIt($tmp); $bag->update(); $bag->package($tmp . '/../bagtmp1.tgz', 'tgz'); $this->assertFileExists($tmp . '/../bagtmp1.tgz'); rename("{$tmp}/../bagtmp1.tgz", "/tmp/bagtmp1.tgz"); $bag->package($tmp . '/../bagtmp2', 'tgz'); $this->assertFileExists($tmp . '/../bagtmp2.tgz'); rename("{$tmp}/../bagtmp2.tgz", "/tmp/bagtmp2.tgz"); // TODO: Test the contents of the package. } catch (Exception $e) { rrmdir($tmp); throw $e; } rrmdir($tmp); }
/** * Create the bag, generate tar. * * @param array $file_ids Array of ids, posted from the form. * @param string $name The name of the bag. * * @return boolean $success True if the new bag validates. */ function bagithelpers_doBagIt($collection_id, $collection_name) { $db = get_db(); $key = sha1(microtime(true) . mt_rand(10000, 90000)); $collection_name = $collection_name . '-' . $key; // Instantiate the bag, get the collection. $bag = new BagIt(BAGIT_BAG_DIRECTORY . '/' . $collection_name); $collection = $db->getTable('BagitFileCollection')->find($collection_id); // Get the files associated with the collection. $files = $collection->getFiles(); // Retrieve the files and add them to the new bag. foreach ($files as $file) { $bagurl = file_display_url($file); $bag->fetch->add($bagurl, 'data/' . $file->original_filename); } // Update the hashes. $bag->update(); // Tar it up. $bag->package(BAGIT_BAG_DIRECTORY . '/' . $collection_name); return $bag->isValid() ? $collection_name : false; }
/** * Create a package containing the serialized deposit objects * @return string The full path of the created zip archive */ function generatePackage() { // get DAOs, plugins and settings $journalDao =& DAORegistry::getDAO('JournalDAO'); $issueDao =& DAORegistry::getDAO('IssueDAO'); $sectionDao =& DAORegistry::getDAO('SectionDAO'); $publishedArticleDao =& DAORegistry::getDAO('PublishedArticleDAO'); PluginRegistry::loadCategory('importexport'); $exportPlugin =& PluginRegistry::getPlugin('importexport', 'NativeImportExportPlugin'); $plnPlugin =& PluginRegistry::getPlugin('generic', PLN_PLUGIN_NAME); $fileManager = new JournalFileManager($journalDao->getById($this->_deposit->getJournalId())); $journal =& $journalDao->getById($this->_deposit->getJournalId()); $depositObjects = $this->_deposit->getDepositObjects(); // set up folder and file locations $bagDir = $this->getDepositDir() . DIRECTORY_SEPARATOR . 'bag'; $packageFile = $this->getPackageFilePath(); $exportFile = $bagDir . DIRECTORY_SEPARATOR . $this->_deposit->getObjectType() . '.xml'; $termsFile = $bagDir . DIRECTORY_SEPARATOR . 'terms.xml'; $bag = new BagIt($bagDir); switch ($this->_deposit->getObjectType()) { case PLN_PLUGIN_DEPOSIT_OBJECT_ARTICLE: $articles = array(); // we need to add all of the relevant articles to an array to export as a batch while ($depositObject =& $depositObjects->next()) { $article =& $publishedArticleDao->getPublishedArticleByArticleId($this->_deposit->getObjectId(), $journal->getId()); $issue =& $issueDao->getIssueById($article->getIssueId(), $journal->getId()); $section =& $sectionDao->getSection($article->getSectionId()); // add the article to the array we'll pass for export $articles[] = array('publishedArticle' => $article, 'section' => $section, 'issue' => $issue, 'journal' => $journal); unset($depositObject); } // export all of the articles together if ($exportPlugin->exportArticles($articles, $exportFile) !== true) { return false; } break; case PLN_PLUGIN_DEPOSIT_OBJECT_ISSUE: // we only ever do one issue at a time, so get that issue $depositObject =& $depositObjects->next(); $issue =& $issueDao->getIssueByBestIssueId($depositObject->getObjectId(), $journal->getId()); // export the issue if ($exportPlugin->exportIssue($journal, $issue, $exportFile) !== true) { return false; } break; default: } // add the current terms to the bag $termsXml = new DOMDocument('1.0', 'utf-8'); $entry = $termsXml->createElementNS('http://www.w3.org/2005/Atom', 'entry'); $entry->setAttributeNS('http://www.w3.org/2000/xmlns/', 'xmlns:dcterms', 'http://purl.org/dc/terms/'); $entry->setAttributeNS('http://www.w3.org/2000/xmlns/', 'xmlns:pkp', PLN_PLUGIN_NAME); $terms = unserialize($plnPlugin->getSetting($this->_deposit->getJournalId(), 'terms_of_use')); $pkpTermsOfUse = $termsXml->createElementNS(PLN_PLUGIN_NAME, 'pkp:terms_of_use'); foreach ($terms as $termName => $termData) { $element = $termsXml->createElementNS(PLN_PLUGIN_NAME, $termName, $termData['term']); $element->setAttribute('updated', $termData['updated']); $pkpTermsOfUse->appendChild($element); } $entry->appendChild($pkpTermsOfUse); $termsXml->appendChild($entry); $termsXml->save($termsFile); // add the exported content to the bag $bag->addFile($exportFile, $this->_deposit->getObjectType() . $this->_deposit->getUUID() . '.xml'); // add the exported content to the bag $bag->addFile($termsFile, 'terms' . $this->_deposit->getUUID() . '.xml'); $bag->update(); // delete files $fileManager->deleteFile($exportFile); $fileManager->deleteFile($termsFile); // create the bag $bag->package($packageFile, 'zip'); // remove the temporary bag directory $fileManager->rmtree($bagDir); return $packageFile; }