whikloj/bagittools

一个用于操作和验证BagIt包的PHP库。

4.2.3 2023-01-24 17:23 UTC

README

Minimum PHP Version Github Actions LICENSE codecov

介绍

BagItTools是BagIt v1.0规范(RFC-8493)的PHP实现。

功能

  • 创建新包
  • 将现有目录加载为包。
  • 加载存档文件(*.zip, *.tar, *.tar.gz, *.tgz, *.tar.bz2)
  • 验证包
  • 添加/删除文件
  • 添加/删除获取URL
  • 添加/删除哈希算法(md5, sha1, sha224, sha256, sha384, sha512, sha3-224, sha3-256, sha3-384, sha3-512)
  • 为所有数据/文件生成所有哈希算法的有效负载(取决于PHP支持)
  • 为所有根级文件和任何额外的标签目录/文件生成标签清单
  • 从bag-info.txt文件中添加/删除标签,保持加载标签的顺序。
  • 生成/更新payload-oxum和bagging-date。
  • 通过所有bagit-conformance-suite测试。
  • 创建存档(zip, tar, tar.gz, tgz, tar.bz2)
  • 将包从v0.97就地升级到v1.0

安装

Composer

composer require "whikloj/bagittools"

从Github克隆

git clone https://github.com/whikloj/BagItTools
cd BagItTools
composer install --no-dev

依赖项

所有依赖项都由composer安装或识别。

需要一些PHP扩展,如果它们在默认PHP安装(由composer使用的)中找不到,则此库将无法安装。

所需的扩展包括

用法

您可以使用API将BagItTools集成到自己的代码中作为库,或使用CLI命令进行一些简单的功能。

命令行

验证包

./bin/console validate <path to bag>

这将输出一条消息,说明包是否有效。它还将返回适当的退出代码(0 = 有效,1 = 无效)。

如果添加了-v标志,它还将打印任何错误或警告。

此命令可以与bagit-conformance-suite一起使用,如下所示

./test-harness <path to BagItTools>/bin/console -- -v validate

API

API文档

创建新包

作为v1.0实现,默认情况下创建的包使用UTF-8文件编码和SHA-512哈希算法。

require_once './vendor/autoload.php';

use \whikloj\BagItTools\Bag;

$dir = "./newbag";

// Create new bag as directory $dir
$bag = Bag::create($dir);

// Add a file
$bag->addFile('../README.md', 'data/documentation/myreadme.md');

// Add another algorithm
$bag->addAlgorithm('sha1');

// Get the algorithms
$algos = $bag->getAlgorithms();
var_dump($algos); // array(
                  //   'sha512',
                  //   'sha1',
                  // )

// Add a fetch url
$bag->addFetchFile('http://www.google.ca', 'data/mywebsite.html');

// Add some bag-info tags
$bag->addBagInfoTag('Contact-Name', 'Jared Whiklo');
$bag->addBagInfoTag('CONTACT-NAME', 'Additional admins');

// Check for tags.
if ($bag->hasBagInfoTag('contact-name')) {

    // Get tags
    $tags = $bag->getBagInfoByTag('contact-name');
    
    var_dump($tags); // array(
                     //    'Jared Whiklo',
                     //    'Additional admins',
                     // )

    // Remove a specific tag value using array index from the above listing.
    $bag->removeBagInfoTagIndex('contact-name', 1); 

    // Get tags
    $tags = $bag->getBagInfoByTag('contact-name');

    var_dump($tags); // array(
                     //    'Jared Whiklo',
                     // )

    $bag->addBagInfoTag('Contact-NAME', 'Bob Saget');
    // Get tags
    $tags = $bag->getBagInfoByTag('contact-name');
    
    var_dump($tags); // array(
                     //    'Jared Whiklo',
                     //    'Bob Saget',
                     // )
    // Without the case sensitive flag as true, you must be exact.
    $bag->removeBagInfoTagValue('contact-name', 'bob saget');
    $tags = $bag->getBagInfoByTag('contact-name');

    var_dump($tags); // array(
                     //    'Jared Whiklo',
                     //    'Bob Saget',
                     // )

    // With the case sensitive flag set to false, you can be less careful
    $bag->removeBagInfoTagValue('contact-name', 'bob saget', false);
    $tags = $bag->getBagInfoByTag('contact-name');

    var_dump($tags); // array(
                     //    'Jared Whiklo',
                     // )

    // Remove all values for the specified tag.
    $bag->removeBagInfoTag('contact-name');
}

// Write bagit support files (manifests, bag-info, etc)
$bag->update();

// Write the bag to the specified path and filename using the expected archiving method.
$bag->package('./archive.tar.bz2');

维护者

Jared Whiklo

许可

MIT

开发

待办事项

  • CLI界面以处理简单的包CRUD(创建/更新/删除)功能。