Skip to content

Commit 2cec32b

Browse files
committed
small changes and fixes for several drush scripts
1 parent 1047921 commit 2cec32b

5 files changed

+249
-46
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,134 @@
1+
<?php
2+
3+
/*
4+
* put this file where ever you put your drush commands
5+
*
6+
*
7+
*
8+
*/
9+
10+
//drush hook
11+
12+
function islandora_image_batch_drush_command() {
13+
$items = array();
14+
15+
$items['islandora_image_batch'] = array(
16+
'description' => "scans a directory for files with a given mimetype and ingests them into the collection provided",
17+
'arguments' => array(
18+
'directory' => 'The path to a directory that contains image files that have extension like .jpg, .gif or .png files',
19+
'mimetype' => 'the mimetype accepted, should correspond to the files in the directory specified above',
20+
'collection' => 'the pid of the object that these will be related',
21+
'pid_namespace' => 'the pid namespace for the new objects',
22+
'cmodel' => 'the cmodel for the object',
23+
),
24+
'examples' => array(
25+
'drush -u 1 --uri=http://137.149.200.19/scholardev islandoraib /home/user/dir/of/images/ image/png islandora:sp_basic_image_collection islandora islandora:sp_basic_image',
26+
),
27+
'aliases' => array('islandoraib'),
28+
'bootstrap' => DRUSH_BOOTSTRAP_DRUPAL_LOGIN, // we can pass in users id on the command line using drush -u.
29+
);
30+
31+
return $items;
32+
}
33+
34+
/**
35+
* This is the drush command specified in the array create by the drush entry point.
36+
*
37+
* This function checks to make sure parameters are supplied and if everything is ok
38+
* calls the doAction function
39+
*
40+
* @param string $query_file
41+
* path to a text file that contains an itql query
42+
* @param boolean $interactive
43+
*
44+
*/
45+
function drush_islandora_image_batch($directory, $mimetype = 'image/jpeg', $collection = 'islandora:sp_basic_image_collection', $pid_namespace = 'islandora', $cmodel = 'islandora:sp_basic_image') {
46+
drush_print('Current working directory ' . getcwd());
47+
if (isset($directory)) {
48+
drush_print(" using directory" . $directory);
49+
}
50+
islandora_image_batch_go($directory, $mimetype, $collection, $pid_namespace, $cmodel);
51+
}
52+
53+
/**
54+
* Iterates through all the files that match the given mimetype
55+
*
56+
*/
57+
function islandora_image_batch_go($directory, $mimetype, $collection, $pid_namespace, $cmodel) {
58+
$files = scandir($directory);
59+
foreach ($files as $file) {
60+
if (mime_content_type($directory . '/' . $file) == $mimetype) {
61+
islandora_image_batch_ingest($directory, $file, $mimetype, $collection, $pid_namespace, $cmodel);
62+
}
63+
}
64+
}
65+
66+
function islandora_image_batch_ingest($directory, $file, $mimetype, $collection, $pid_namespace, $cmodel) {
67+
$connection = islandora_get_tuque_connection();
68+
$repository = $connection->repository;
69+
$pid = $repository->api->m->getNextPid($pid_namespace);
70+
$object = new NewFedoraObject($pid, $repository);
71+
$object->label = $file;
72+
$obj_datastream = new NewFedoraDatastream('OBJ', 'M', $object, $repository);
73+
$obj_datastream->setContentFromFile($directory . '/' . $file, FALSE);
74+
$obj_datastream->label = 'OBJ';
75+
$obj_datastream->mimetype = $mimetype;
76+
$obj_datastream->checksum = TRUE;
77+
$obj_datastream->checksumType = 'MD5';
78+
$obj_datastream->logMessage = "added by drush script";
79+
$object->ingestDatastream($obj_datastream);
80+
81+
$xml = <<<PAGEMODS
82+
<?xml version="1.0" encoding="UTF-8"?>
83+
<mods xmlns="http://www.loc.gov/mods/v3" xmlns:mods="http://www.loc.gov/mods/v3" xmlns:xlink="http://www.w3.org/1999/xlink" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
84+
<titleInfo>
85+
<title>$file</title>
86+
</titleInfo>
87+
<identifier type="PID">$pid</identifier>
88+
</mods>
89+
PAGEMODS;
90+
91+
$mods_datastream = new NewFedoraDatastream('MODS', 'X', $object, $connection->repository);
92+
$mods_datastream->content = $xml;
93+
$mods_datastream->mimetype = 'text/xml';
94+
$mods_datastream->label = 'MODS record';
95+
$mods_datastream->checksum = TRUE;
96+
$mods_datastream->checksumType = 'MD5';
97+
$mods_datastream->logMessage = 'MODS datastream created using islandora drush image batch ingest script';
98+
$object->ingestDatastream($mods_datastream);
99+
100+
$rdf_string = <<<RDF
101+
<rdf:RDF xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:fedora="info:fedora/fedora-system:def/relations-external#" xmlns:fedora-model="info:fedora/fedora-system:def/model#" xmlns:islandora="http://islandora.ca/ontology/relsext#">
102+
<rdf:Description rdf:about="info:fedora/$pid">
103+
<fedora-model:hasModel rdf:resource="info:fedora/$cmodel"></fedora-model:hasModel>
104+
<fedora:isMemberOfCollection rdf:resource="info:fedora/$collection"></fedora:isMemberOfCollection>
105+
</rdf:Description>
106+
</rdf:RDF>
107+
RDF;
108+
109+
$rels_datastream = new NewFedoraDatastream('RELS-EXT', 'X', $object, $connection->repository);
110+
$rels_datastream->setContentFromString($rdf_string);
111+
$rels_datastream->label = 'Fedora Object to Object Relationship Metadata.';
112+
$rels_datastream->mimetype = 'text/xml';
113+
$rels_datastream->logMessage = 'RELS-EXT datastream created using islandora drush image batch ingest script';
114+
115+
$object->ingestDatastream($rels_datastream);
116+
117+
118+
try{
119+
$ingest = $repository->ingestObject($object);
120+
drush_print("successfully processed $pid ");
121+
} catch (Exception $e){
122+
unset($object);
123+
unset($rels_datastream);
124+
unset($mods_datastream);
125+
unset($obj_datastream);
126+
drush_print("failed processing rels of $pid # " . $e->getMessage());
127+
}
128+
unset($object);
129+
unset($rels_datastream);
130+
unset($mods_datastream);
131+
unset($obj_datastream);
132+
}
133+
134+
?>

drush/drupal7/islandora_update_broken_newspapers.drush.inc

+93-29
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,18 @@ function islandora_update_broken_newspapers_drush_command() {
1313

1414
$items['islandora_update_broken_newspapers'] = array(
1515
'description' => "Reads a csv file first column is pid updates the MODS datastream so derivatives can be regenerated by micro services.
16-
Currently requires Drupal7 Islandora with Tuque.",
16+
Currently requires Drupal7 Islandora with Tuque. By using an action of UPDATERELS this will update the RELS-EXT datastream of the newspaper
17+
object with the given cmodel and also update the newspapers pages RELS-EXT (the page level stuff [like cmodel] is currently hardcoded).
18+
Each pid in the txt/csv file should represent a book or newspaper (the script looks up the pages base on the parent newspaper or book).
19+
This is pretty specific to UPEI's newspapers right now)",
1720
'arguments' => array(
1821
'csv_file' => 'The path to a csv file that contains columns as described in the description.',
1922
'interactive' => 'if TRUE then you will be asked to confirm the update for each object',
2023
'action' => 'the action to do, UPDATEMODS or UPDATERELS. The default is UPDATEMODS',
24+
'$cmodel' => 'the cmodel to update to, this is only used if action = UPDATERELS',
2125
),
2226
'examples' => array(
23-
'drush -u 1 --uri=http://137.149.200.19/scholardev islandoraubn /var/www/html/drupal/sites/137.149.200.19.scholardev/files/csv.txt TRUE UPDATEMODS',
27+
'drush -u 1 --uri=http://137.149.200.19/scholardev islandoraubn /var/www/html/drupal/sites/137.149.200.19.scholardev/files/csv.txt TRUE UPDATEMODS islandora:newspaperPageCModel',
2428
),
2529
'aliases' => array('islandoraubn'),
2630
'bootstrap' => DRUSH_BOOTSTRAP_DRUPAL_LOGIN, // we can pass in users id on the command line using drush -u.
@@ -40,7 +44,7 @@ function islandora_update_broken_newspapers_drush_command() {
4044
* @param boolean $interactive
4145
*
4246
*/
43-
function drush_islandora_update_broken_newspapers($csv_file, $interactive, $action = 'UPDATEMODS') {
47+
function drush_islandora_update_broken_newspapers($csv_file, $interactive, $action = 'UPDATEMODS', $cmodel = NULL) {
4448
drush_print('Current working directory ' . getcwd());
4549
if (isset($csv_file)) {
4650
drush_print(" using csv file" . $csv_file);
@@ -49,9 +53,7 @@ function drush_islandora_update_broken_newspapers($csv_file, $interactive, $acti
4953
drush_print(" no csv file found");
5054
return;
5155
}
52-
53-
54-
islandora_update_newspaper_go($csv_file, $interactive, $action);
56+
islandora_update_newspaper_go($csv_file, $interactive, $action, $cmodel);
5557
}
5658

5759
/**
@@ -62,53 +64,115 @@ function drush_islandora_update_broken_newspapers($csv_file, $interactive, $acti
6264
* @param string $action
6365
*
6466
*/
65-
function islandora_update_newspaper_go($csv_file, $interactive = FALSE, $action = 'UPDATEMODS') {
67+
function islandora_update_newspaper_go($csv_file, $interactive = FALSE, $action = 'UPDATEMODS', $cmodel) {
6668
module_load_include('inc', 'fedora_repository', 'api/fedora_item');
6769
ini_set("auto_detect_line_endings", "1");
6870

6971
$line_number = 0;
7072
$lines = file($csv_file);
73+
$count = 0;
7174
foreach ($lines as $pid) {
75+
$count++;
7276
$pid = trim($pid);
73-
if ($action == 'UPDATEMODS') {
74-
if ($interactive == 'TRUE') {
75-
if (drush_confirm(dt('update this @pid ?', array('@pid' => $pid)))) {
76-
//$item = new Fedora_Item($object);
77+
if (!empty($pid)) {
78+
if ($action == 'UPDATEMODS') {
79+
if ($interactive == 'TRUE') {
80+
if (drush_confirm(dt('update this @pid ?', array('@pid' => $pid)))) {
81+
//$item = new Fedora_Item($object);
82+
islandora_update_newspaper_page($pid);
83+
drush_print("processed MODS $pid # " . ++$line_number);
84+
}
85+
}
86+
else {
7787
islandora_update_newspaper_page($pid);
7888
drush_print("processed MODS $pid # " . ++$line_number);
7989
}
8090
}
8191
else {
82-
islandora_update_newspaper_page($pid);
83-
drush_print("processed MODS $pid # " . ++$line_number);
84-
}
85-
}
86-
else {
87-
if ($interactive == 'TRUE') {
88-
if (drush_confirm(dt('update this @pid ?', array('@pid' => $pid)))) {
89-
//$item = new Fedora_Item($object);
90-
islandora_update_newspaper_rels($pid);
92+
if ($interactive == 'TRUE') {
93+
if (drush_confirm(dt('update this @pid ?', array('@pid' => $pid)))) {
94+
//$item = new Fedora_Item($object);
95+
islandora_update_newspaper_rels($pid, $cmodel);
96+
drush_print("processed rels of $pid # " . ++$line_number);
97+
}
98+
}
99+
else {
100+
islandora_update_newspaper_rels($pid, $cmodel);
91101
drush_print("processed rels of $pid # " . ++$line_number);
92102
}
93103
}
94-
else {
95-
islandora_update_newspaper_rels($pid);
96-
drush_print("processed rels of $pid # " . ++$line_number);
104+
if($count > 100){
105+
time_nanosleep(0, 500000000);
106+
$count = 0;
97107
}
98108
}
99109
}
100110
}
101111

102112
/**
103113
* removes the cmodel from an object and adds a new cmodel. Currenlty
104-
* the cmodel is hardcoded to issueCModel.
114+
*
105115
* @param string $pid
106116
*/
107-
function islandora_update_newspaper_rels($pid) {
108-
$item = islandora_object_load($pid);
109-
$item->relationships->remove('info:fedora/fedora-system:def/model#','hasModel');
110-
$item->relationships->remove("info:fedora/fedora-system:def/relations-external#",'hasModel');
111-
$item->relationships->add('info:fedora/fedora-system:def/model#', 'hasModel', 'islandora:issueCModel');
117+
function islandora_update_newspaper_rels($pid, $cmodel) {
118+
try{
119+
$object = islandora_object_load($pid);
120+
if (isset($cmodel)) {
121+
$object->relationships->remove('info:fedora/fedora-system:def/model#', 'hasModel');
122+
$object->relationships->remove("info:fedora/fedora-system:def/relations-external#", 'hasModel');
123+
$object->relationships->add('info:fedora/fedora-system:def/model#', 'hasModel', $cmodel);
124+
}
125+
$object->relationships->remove("http://islandora.ca/ontology/relsext#", 'isIssueOf');
126+
$object->relationships->remove('http://islandora.ca/ontology/relsext#', 'dateIssued');
127+
//<isIssueOf xmlns="http://islandora.ca/ontology/relsext#" rdf:resource="info:fedora/newspapers:guardian"></isIssueOf>
128+
$object->relationships->add("http://islandora.ca/ontology/relsext#", 'isIssueOf', 'newspapers:guardian');
129+
$dateIssued = islandora_drush_get_date_issued($object);
130+
131+
$object->relationships->add('http://islandora.ca/ontology/relsext#','dateIssued', $dateIssued, RELS_TYPE_DATETIME);
132+
//DON"T touch pages could wrap this in a parameter check
133+
//$pages = islandora_basic_collection_get_objects($object);
134+
//islandora_drush_update_newspaper_pages($pages, $pid);
135+
} catch (Exception $e){
136+
drush_print("error creating book or newspaper object " . $e->getMessage());
137+
}
138+
unset($object);
139+
}
140+
141+
function islandora_drush_get_date_issued($object){
142+
$mods = $object['MODS']->content;
143+
$modsxml = simplexml_load_string(trim($mods));
144+
$modsxml->registerXPathNamespace('mods', 'http://www.loc.gov/mods/v3');
145+
$elements = $modsxml->xpath('//mods:dateIssued');
146+
$dateIssued = (string)$elements[0];
147+
return $dateIssued;
148+
149+
}
150+
151+
function islandora_drush_update_newspaper_pages($pages, $parent_pid) {
152+
$count = 0;
153+
foreach ($pages as $page) {
154+
$count++;
155+
//we make a big assumption here THAT sorting by title does the right thing
156+
//for UPEI the titles are Page 1, Page 2 etc so this works but will not work
157+
//if the titles don't sort as expected.
158+
drush_print("processing rels of page # " . $page['object']['value']);
159+
$page_object = islandora_object_load($page['object']['value']);
160+
$pid = $page_object->id;
161+
$rdf_string = <<<RDF
162+
<rdf:RDF xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#">
163+
<rdf:Description rdf:about="info:fedora/$pid">
164+
<hasModel xmlns="info:fedora/fedora-system:def/model#" rdf:resource="info:fedora/islandora:newspaperPageCModel"></hasModel>
165+
<isSequenceNumber xmlns="http://islandora.ca/ontology/relsext#">$count</isSequenceNumber>
166+
<isMemberOf xmlns="info:fedora/fedora-system:def/relations-external#" rdf:resource="info:fedora/$parent_pid"></isMemberOf>
167+
<isSection xmlns="http://islandora.ca/ontology/relsext#">1</isSection>
168+
<isPageOf xmlns="http://islandora.ca/ontology/relsext#" rdf:resource="info:fedora/$parent_pid"></isPageOf>
169+
<isPageNumber xmlns="http://islandora.ca/ontology/relsext#">$count</isPageNumber>
170+
</rdf:Description>
171+
</rdf:RDF>
172+
RDF;
173+
$page_object['RELS-EXT']->content = $rdf_string;
174+
unset($page_object);
175+
}
112176
}
113177

114178
/**

drush/islandora_update_book_rels.drush.inc

+1-1
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,7 @@ function islandora_update_book_create_rels($book_pid, $pid, $page_number) {
165165
<isSequenceNumber xmlns="http://islandora.ca/ontology/relsext#">$page_number</isSequenceNumber>
166166
<isMemberOf xmlns="info:fedora/fedora-system:def/relations-external#" rdf:resource="info:fedora/$book_pid"></isMemberOf>
167167
<isSection xmlns="http://islandora.ca/ontology/relsext#">1</isSection>
168-
<isPageOf xmlns="http://islandora.ca/ontology/relsext#" rdf:resource="inf:fedora/$book_pid"></isPageOf>
168+
<isPageOf xmlns="http://islandora.ca/ontology/relsext#" rdf:resource="info:fedora/$book_pid"></isPageOf>
169169
<isPageNumber xmlns="http://islandora.ca/ontology/relsext#">$page_number</isPageNumber>
170170
</rdf:Description>
171171
</rdf:RDF>

0 commit comments

Comments
 (0)