Skip to content

Commit

Permalink
Work on #10.
Browse files Browse the repository at this point in the history
  • Loading branch information
mjordan committed Apr 29, 2019
1 parent f0d6464 commit b5b0651
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 102 deletions.
2 changes: 1 addition & 1 deletion sample_config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ bag-info:
# Required. Whether or not to log Bag creation. Set log output path in config/packages/{environment}/monolog.yaml.
log_bag_creation: true

# Optional. Which has algorithm to use. One of 'sha1' or 'md5'. Default is sha1.
# Optional. Which hash algorithm to use. One of 'sha1' or 'md5'. Default is sha1.
# hash_algorithm: md5

# Optional. Timeout to use for Guzzle requests, in seconds. Default is 60.
Expand Down
108 changes: 7 additions & 101 deletions src/Command/CreateBagCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,9 @@
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Yaml\Yaml;
use Symfony\Component\Console\Style\SymfonyStyle;
use App\Service\IslandoraBagger;

use Psr\Log\LoggerInterface;
use GuzzleHttp\Client;
use GuzzleHttp\Exception\RequestException;

require 'vendor/scholarslab/bagit/lib/bagit.php';

class CreateBagCommand extends ContainerAwareCommand
{
Expand Down Expand Up @@ -44,105 +41,14 @@ protected function execute(InputInterface $input, OutputInterface $output)
$settings_path = $input->getOption('settings');
$this->settings = Yaml::parseFile($settings_path);

// Set some configuration defaults.
$this->settings['http_timeout'] = (!isset($this->settings['http_timeout'])) ?
60 : $this->settings['http_timeout'];
$this->settings['verify_ca'] = (!isset($this->settings['verify_ca'])) ?
true : $this->settings['verify_ca'];
$this->settings['hash_algorithm'] = (!isset($this->settings['hash_algorithm'])) ?
'sha1' : $this->settings['hash_algorithm'];

if (!file_exists($this->settings['output_dir'])) {
mkdir($this->settings['output_dir']);
}
if (!file_exists($this->settings['temp_dir'])) {
mkdir($this->settings['temp_dir']);
}

$client = new \GuzzleHttp\Client();

// Get the node's UUID from Drupal.
$drupal_url = $this->settings['drupal_base_url'] . '/node/' . $nid . '?_format=json';
$response = $client->get($drupal_url);
$response_body = (string) $response->getBody();
$node_json = $response_body;
$body_array = json_decode($response_body, true);
$uuid = $body_array['uuid'][0]['value'];

if ($this->settings['bag_name'] == 'uuid') {
$bag_name = $uuid;
} else {
$bag_name = $nid;
}

// Create directories.
$bag_dir = $this->settings['output_dir'] . DIRECTORY_SEPARATOR . $bag_name;
if (!file_exists($bag_dir)) {
mkdir($bag_dir);
}
$bag_temp_dir = $this->settings['temp_dir'] . DIRECTORY_SEPARATOR . $bag_name;
if (!file_exists($bag_temp_dir)) {
mkdir($bag_temp_dir);
}

// Create the Bag.
$bag_info = array();
$bag = new \BagIt($bag_dir, true, true, true, $bag_info);
$bag->setHashEncoding($this->settings['hash_algorithm']);

// Add tags registered in the config file.
foreach ($this->settings['bag-info'] as $key => $value) {
$bag->setBagInfoData($key, $value);
}

// Execute registered plugins.
foreach ($this->settings['plugins'] as $plugin) {
$plugin_name = 'App\Plugin\\' . $plugin;
$bag_plugin = new $plugin_name($this->settings, $this->logger);
$bag = $bag_plugin->execute($bag, $bag_temp_dir, $nid, $node_json);
}

$bag->update();
$this->removeDir($bag_temp_dir);
$islandora_bagger = new IslandoraBagger($this->settings, $this->logger);
$bag_dir = $islandora_bagger->createBag($nid);

$package = isset($this->settings['serialize']) ? $this->settings['serialize'] : false;
if ($package) {
$bag->package($bag_dir, $package);
$this->removeDir($bag_dir);
$bag_name = $bag_name . '.' . $package;
if ($bag_dir) {
$io->success("Bag created for " . $this->settings['drupal_base_url'] . '/node/' . $nid . " at " . $bag_dir);
}

$io->success("Bag created for " . $this->settings['drupal_base_url'] . '/node/' . $nid . " at " . $bag_dir);
if ($this->settings['log_bag_creation']) {
$this->logger->info(
"Bag created.",
array(
'node URL' => $this->settings['drupal_base_url'] . '/node/' . $nid,
'node UUID' => $uuid,
'Bag location' => $this->settings['output_dir'],
'Bag name' => $bag_name
)
);
}
}

/**
* Deletes a directory and all of its contents.
*
* @param $dir string
* Path to the directory.
*
* @return bool
* True if the directory was deleted, false if not.
*
*/
protected function removeDir($dir)
{
// @todo: Add list here of invalid $dir values, e.g., /, /tmp.
$files = array_diff(scandir($dir), array('.','..'));
foreach ($files as $file) {
(is_dir("$dir/$file")) ? $this->removeDir("$dir/$file") : unlink("$dir/$file");
else {
$io->error("Bag not created for " . $this->settings['drupal_base_url'] . '/node/' . $nid . " at " . $bag_dir);
}
return rmdir($dir);
}
}

0 comments on commit b5b0651

Please sign in to comment.