@@ -13,14 +13,18 @@ function islandora_update_broken_newspapers_drush_command() {
13
13
14
14
$ items ['islandora_update_broken_newspapers ' ] = array (
15
15
'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) " ,
17
20
'arguments ' => array (
18
21
'csv_file ' => 'The path to a csv file that contains columns as described in the description. ' ,
19
22
'interactive ' => 'if TRUE then you will be asked to confirm the update for each object ' ,
20
23
'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 ' ,
21
25
),
22
26
'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 ' ,
24
28
),
25
29
'aliases ' => array ('islandoraubn ' ),
26
30
'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() {
40
44
* @param boolean $interactive
41
45
*
42
46
*/
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 ) {
44
48
drush_print ('Current working directory ' . getcwd ());
45
49
if (isset ($ csv_file )) {
46
50
drush_print (" using csv file " . $ csv_file );
@@ -49,9 +53,7 @@ function drush_islandora_update_broken_newspapers($csv_file, $interactive, $acti
49
53
drush_print (" no csv file found " );
50
54
return ;
51
55
}
52
-
53
-
54
- islandora_update_newspaper_go ($ csv_file , $ interactive , $ action );
56
+ islandora_update_newspaper_go ($ csv_file , $ interactive , $ action , $ cmodel );
55
57
}
56
58
57
59
/**
@@ -62,53 +64,115 @@ function drush_islandora_update_broken_newspapers($csv_file, $interactive, $acti
62
64
* @param string $action
63
65
*
64
66
*/
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 ) {
66
68
module_load_include ('inc ' , 'fedora_repository ' , 'api/fedora_item ' );
67
69
ini_set ("auto_detect_line_endings " , "1 " );
68
70
69
71
$ line_number = 0 ;
70
72
$ lines = file ($ csv_file );
73
+ $ count = 0 ;
71
74
foreach ($ lines as $ pid ) {
75
+ $ count ++;
72
76
$ 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 {
77
87
islandora_update_newspaper_page ($ pid );
78
88
drush_print ("processed MODS $ pid # " . ++$ line_number );
79
89
}
80
90
}
81
91
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 );
91
101
drush_print ("processed rels of $ pid # " . ++$ line_number );
92
102
}
93
103
}
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 ;
97
107
}
98
108
}
99
109
}
100
110
}
101
111
102
112
/**
103
113
* removes the cmodel from an object and adds a new cmodel. Currenlty
104
- * the cmodel is hardcoded to issueCModel.
114
+ *
105
115
* @param string $pid
106
116
*/
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
+ }
112
176
}
113
177
114
178
/**
0 commit comments