whikloj / BagItTools

A PHP BagIt library
MIT License
11 stars 5 forks source link

BagItTools

Minimum PHP Version Github Actions LICENSE codecov

Introduction

BagItTools is a PHP implementation of the BagIt v1.0 specification (RFC-8493).

Features:

Installation

Composer

composer require "whikloj/bagittools"

Clone from Github

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

Dependencies

All dependencies are installed or identified by composer.

Some PHP extensions are required and this library will not install if they cannot be found in the default PHP installation (the one used by composer).

The required extensions are:

Usage

You can integrate BagItTools into your own code as a library using the API, or use the CLI commands for some simple functionality.

Command line

Validating a bag

./bin/console validate <path to bag>

This will output a message as to whether the bag is or is NOT valid. It will also respond with an appropriate exit code (0 == valid, 1 == invalid).

If you add the -v flag it will also print any errors or warnings.

This can command can be used with the bagit-conformance-suite like this

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

API

API Documentation

Create a new bag

As this is a v1.0 implementation, by default bags created use the UTF-8 file encoding and the SHA-512 hash algorithm.


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');

Maintainer

Jared Whiklo

License

MIT

Development

To-Do: