32 require_once __DIR__ .
"/../vendor/autoload.php";
33 require_once
'bagit_fetch.php';
34 require_once
'bagit_manifest.php';
35 require_once
'bagit_utils.php';
195 $this->bagVersion = array(
'major' => 0,
'minor' => 96);
196 $this->tagFileEncoding =
'UTF-8';
197 $this->bagDirectory =
null;
198 $this->bagitFile =
null;
199 $this->manifest =
null;
200 $this->tagManifest =
null;
202 $this->bagInfoFile =
null;
204 $this->bagCompression =
null;
205 $this->bagErrors = array();
208 file_exists($this->bag) &&
209 ($this->_isCompressed() || file_exists(
"{$this->bag}/bagit.txt"))
217 $this->fetch->download();
232 return (
count($this->bagErrors) == 0);
254 $major = $this->bagVersion[
'major'];
255 $minor = $this->bagVersion[
'minor'];
258 'version' =>
"$major.$minor",
259 'encoding' => $this->tagFileEncoding,
272 return "{$this->bagDirectory}/data";
282 return $this->manifest->getHashEncoding();
295 $hashAlgorithm = strtolower($hashAlgorithm);
296 if ($hashAlgorithm !=
'md5' && $hashAlgorithm !=
'sha1') {
297 throw new InvalidArgumentException(
"Invalid hash algorithim: '$hashAlgorithm'.");
300 $this->manifest->setHashEncoding($hashAlgorithm);
301 if ($this->tagManifest !==
null) {
302 $this->tagManifest->setHashEncoding($hashAlgorithm);
346 BagIt_validateExists($this->bagitFile, $errors);
348 $this->manifest->validate($errors);
350 $this->bagErrors = $errors;
373 $this->manifest->clear();
374 if ($this->tagManifest !==
null) {
375 $this->tagManifest->clear();
380 foreach ($dataFiles as $dataFile) {
381 $baseName = basename($dataFile);
382 if ($baseName ==
'.' || $baseName ==
'..') {
386 $cleanName = BagIt_sanitizeFileName($baseName);
387 if ($baseName != $cleanName) {
388 $dirName = dirname($dataFile);
389 rename($dataFile,
"$dirName/$cleanName");
393 if ($this->extended ||
count($this->bagInfoData) > 0) {
394 $this->_writeBagInfo();
399 if ($this->tagManifest !==
null) {
403 "$bagdir/bag-info.txt",
404 $this->fetch->fileName,
405 $this->manifest->getFileName()
407 $this->tagManifest->update($tagFiles);
424 $dataPref =
'data' . DIRECTORY_SEPARATOR;
425 $prefLen = strlen($dataPref);
426 if ((strncasecmp($dest, $dataPref, $prefLen) != 0)
427 && (strncasecmp($dest, $dataPref, $prefLen) != 0)
429 $dest = $dataPref . $dest;
432 $fulldest =
"{$this->bagDirectory}/$dest";
433 $dirname = dirname($fulldest);
434 if (! is_dir($dirname)) {
435 mkdir($dirname, 0777,
true);
438 copy($src, $fulldest);
455 $dataPref =
'data' . DIRECTORY_SEPARATOR;
456 $prefLen = strlen($dataPref);
457 if ((strncasecmp($dest, $dataPref, $prefLen) != 0)) {
458 $dest = $dataPref . $dest;
461 $fulldest =
"{$this->bagDirectory}/$dest";
463 if(file_exists($fulldest)) {
464 throw new BagitException(
"File already exists: '$dest'");
467 $dirname = dirname($fulldest);
468 if (! is_dir($dirname)) {
469 mkdir($dirname, 0777,
true);
471 file_put_contents($fulldest, $content);
483 function package($destination, $method=
'tgz')
485 $method = strtolower($method);
486 if ($method !=
'zip' && $method !=
'tgz') {
487 throw new BagItException(
"Invalid compression method: '$method'.");
490 if (substr_compare($destination,
".$method", -4, 4,
true) != 0) {
491 $destination =
"$destination.$method";
511 $this->_ensureBagInfoData();
512 return array_key_exists($key, $this->bagInfoData);
526 $this->_ensureBagInfoData();
527 $this->bagInfoData[$key] = BagIt_getAccumulatedValue(
528 $this->bagInfoData, $key, $value
542 if (array_key_exists($key, $this->bagInfoData)) {
543 unset($this->bagInfoData[$key]);
557 $this->_ensureBagInfoData();
558 return array_key_exists($key, $this->bagInfoData) ? $this->bagInfoData[$key] :
null;
570 private function _openBag()
572 if ($this->_isCompressed()) {
573 $this->bagDirectory = BagIt_uncompressBag($this->bag);
575 $this->bagDirectory = realpath($this->bag);
578 $this->bagitFile =
"{$this->bagDirectory}/bagit.txt";
579 list($version, $fileEncoding, $errors) = BagIt_readBagItFile(
582 $this->bagVersion = $version;
583 $this->tagFileEncoding = $fileEncoding;
584 $this->bagErrors = array_merge($this->bagErrors, $errors);
586 $files = scandir($this->bagDirectory);
587 if (count($files) > 0) {
589 $manifestFile = findFirstExisting(
590 array(
"$bagdir/manifest-sha1.txt",
"$bagdir/manifest-md5.txt"),
591 "$bagdir/manifest-sha1.txt"
596 $this->bagDirectory .
'/',
597 $this->tagFileEncoding
599 }
catch (Exception $exc) {
602 array(
'manifest',
"Error reading $manifestFile.")
607 $manifestFile = findFirstExisting(
608 array(
"$bagdir/tagmanifest-sha1.txt",
609 "$bagdir/tagmanifest-md5.txt"),
610 "$bagdir/tagmanifest-sha1.txt"
614 $this->bagDirectory .
'/',
615 $this->tagFileEncoding
620 "{$this->bagDirectory}/fetch.txt",
621 $this->tagFileEncoding
623 }
catch (Exception $exc) {
626 array(
'fetch',
'Error reading fetch file.')
630 $this->bagInfoFile =
"{$this->bagDirectory}/bag-info.txt";
631 $this->_readBagInfo();
641 private function _createBag()
643 if (!is_dir($this->bag)) {
646 $this->bagDirectory = realpath($this->bag);
649 if (!is_dir($dataDir)) {
653 $this->bagitFile = $this->bagDirectory .
'/bagit.txt';
655 "{$this->bagDirectory}/manifest-sha1.txt",
656 $this->bagDirectory .
'/',
657 $this->tagFileEncoding
660 $major = $this->bagVersion[
'major'];
661 $minor = $this->bagVersion[
'minor'];
663 =
"BagIt-Version: $major.$minor\n" .
664 "Tag-File-Character-Encoding: {$this->tagFileEncoding}\n";
665 writeFileText($this->bagitFile, $this->tagFileEncoding, $bagItData);
667 $this->_createExtendedBag();
675 private function _createExtendedBag()
677 if ($this->extended) {
680 "{$this->bagDirectory}/tagmanifest-$hashEncoding.txt",
681 $this->bagDirectory .
'/',
682 $this->tagFileEncoding
685 $fetchFile = $this->bagDirectory .
'/fetch.txt';
686 $this->fetch =
new BagItFetch($fetchFile, $this->tagFileEncoding);
688 $this->bagInfoFile = $this->bagDirectory .
'/bag-info.txt';
689 touch($this->bagInfoFile);
690 if (is_null($this->bagInfoData)) {
691 $this->bagInfoData = array();
701 private function _readBagInfo()
704 $lines = readLines($this->bagInfoFile, $this->tagFileEncoding);
705 $this->bagInfoData = BagIt_parseBagInfo($lines);
706 }
catch (Exception $exc) {
709 array(
'baginfo',
'Error reading bag info file.')
720 private function _writeBagInfo()
724 if (
count($this->bagInfoData)) {
725 foreach ($this->bagInfoData as $label => $value) {
726 if (is_array($value)) {
727 foreach ($value as $v) {
728 $lines[] =
"$label: $v\n";
731 $lines[] =
"$label: $value\n";
736 writeFileText($this->bagInfoFile, $this->tagFileEncoding, join(
'', $lines));
744 private function _isCompressed()
746 if (is_dir($this->bag)) {
749 $bag = strtolower($this->bag);
750 if (endsWith(
$bag,
'.zip')) {
751 $this->bagCompression =
'zip';
753 }
else if (endsWith(
$bag,
'.tar.gz') || endsWith(
$bag,
'.tgz')) {
754 $this->bagCompression =
'tgz';
767 private function _ensureBagInfoData()
769 if (is_null($this->bagInfoData)) {
770 $this->bagInfoData = array();