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); }
/** * Read the Bag, unpack it, drop files into the Dropbox files directory. * * @param string $filename The name of the uploaded bag. * * @return boolean $success True if the read succeeds. */ function bagithelpers_doReadBagIt($filename) { $success = false; $bag = new BagIt(BAGIT_TMP_DIRECTORY . '/' . $filename); $bag->fetch->download(); $bag->update(); $bag->validate(); if (count($bag->getBagErrors()) == 0) { // Copy each of the files. foreach ($bag->getBagContents() as $file) { copy($file, BASE_DIR . '/plugins/Dropbox/files/' . basename($file)); } $success = true; } return $success; }
/** * This is the use case for consuming a bag from someone else. The user * does these actions: * * <ol> * <li>Open the bag;</li> * <li>Validate the downloaded contents;</li> * <li>Fetch on-line items in the bag;</li> * <li>Validate the bag's contents; and</li> * <li>Copy items from the bag onto the local disk.</li> * </ol> */ public function testBagConsumer() { $srcbag = __DIR__ . '/TestBag.tgz'; // 1. Open the bag; $bag = new BagIt($srcbag); $this->queueRm($bag->bagDirectory); $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('sha1', $bag->getHashEncoding()); $this->assertEquals(7, count($bag->getBagContents())); $this->assertEquals(0, count($bag->getBagErrors())); // 2. Validate the downloaded contents; $bag->validate(); $this->assertEquals(0, count($bag->getBagErrors())); // 3. Fetch on-line items in the bag; $bag->fetch->download(); $bag->update(); $bag->validate(); $this->assertEquals(8, count($bag->getBagContents())); // 4. Validate the bag's contents; and $this->assertEquals(0, count($bag->getBagErrors())); // 5. Copy items from the bag onto the local disk. $tmpdir = tmpdir(); mkdir($tmpdir); $this->queueRm($tmpdir); foreach ($bag->getBagContents() as $bagFile) { $basename = basename($bagFile); copy($bagFile, "{$tmpdir}/{$basename}"); } $this->assertEquals(count($bag->getBagContents()) + 2, count(scandir($tmpdir))); $this->assertEquals(count($bag->manifest->getData()) + 2, count(scandir($tmpdir))); }
/** * 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; }
public function testNewBagEmpty() { $bagdir = "{$this->tmpdir}/_bag"; $bag = new BagIt($bagdir); $this->assertFileNotExists("{$bagdir}/fetch.txt"); $bag->update(); $this->assertFileNotExists("{$bagdir}/fetch.txt"); }