-
Notifications
You must be signed in to change notification settings - Fork 0
/
islandora_label_maker.drush.inc
308 lines (284 loc) · 9.87 KB
/
islandora_label_maker.drush.inc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
<?php
/**
* @file
* Functions for managing label creation via drush.
*
*
* Copyright 2018 Leiden University Library
*
* This file is part of islandora_label_maker.
*
* islandora_label_maker is free software: you can redistribute it and/or
* modify it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
/**
* Implements hook_drush_command().
*/
function islandora_label_maker_drush_command() {
$items['islandora_label_maker_list'] = array(
'description' => 'List the new labels for the identifiers. If the label can not be made, an error is generated.',
'options' => array(
'ids_file' => 'The absolute filepath to a file containing a list of Islandora identifiers that will be used.',
'format' => 'Optionally, the format of the output. Allowed values are: identifier, oldlabel, newlabel. Other values will be unchanged. Defaults to identifier;newlabel;oldlabel',
'include_related' => 'Optionally, also include the related items',
'skip_unchanged' => 'Optionally, skip when the new label is the same as the old label',
'silent' => 'Optionally, does not report when a label can not be made.',
),
'aliases' => array('ilmlist'),
'examples' => array(
'drush --user=admin islandora_label_maker_list --ids_file=/path/to/idsfile.csv',
'drush --user=admin ilmlist --ids_file=/path/to/idsfile.csv --format="identifier;newlabel" --skip_unchanged',
),
);
$items['islandora_label_maker_queue'] = array(
'description' => 'Adds the items to the queue, to be generated later.',
'options' => array(
'ids_file' => 'The absolute filepath to a file containing a list of Islandora identifiers that will be used.',
'include_related' => 'Optionally, also add the related items to the queue',
),
'aliases' => array('ilmqueue'),
'examples' => array(
'drush --user=admin islandora_label_maker_qeueu --ids_file=/path/to/idsfile.csv',
'drush --user=admin ilmqeueu --ids_file=/path/to/idsfile.csv',
),
);
$items['islandora_label_maker_update'] = array(
'description' => 'Updates the labels for the identifiers. If the label can not be made, an error is generated. Only run this when you are sure!',
'options' => array(
'ids_file' => 'The absolute filepath to a file containing a list of Islandora identifiers that will be used.',
'include_related' => 'Optionally, also update the related items',
'silent' => 'Optionally, does not report when a label can or should not be updated.',
),
'aliases' => array('ilmupdate'),
'examples' => array(
'drush --user=admin islandora_label_maker_update --ids_file=/path/to/idsfile.csv',
'drush --user=admin ilmupdate --ids_file=/path/to/idsfile.csv --silent',
),
);
return $items;
}
/**
* Implements drush_hook_COMMAND_validate().
*/
function drush_islandora_label_maker_list_validate() {
return _islandora_label_maker_drush_validate();
}
/**
* Implements drush_hook_COMMAND_validate().
*/
function drush_islandora_label_maker_queue_validate() {
return _islandora_label_maker_drush_validate();
}
/**
* Implements drush_hook_COMMAND_validate().
*/
function drush_islandora_label_maker_update_validate() {
return _islandora_label_maker_drush_validate();
}
/**
* Helper function for validation.
*/
function _islandora_label_maker_drush_validate() {
$idsfile = drush_get_option('ids_file');
if (!isset($idsfile)) {
return drush_set_error("Argument ids_file missing");
}
if (!file_exists($idsfile)) {
return drush_set_error("ids_file '$idsfile' does not exist");
}
}
/**
* Implements drush_hook_COMMAND().
*/
function drush_islandora_label_maker_list() {
module_load_include('inc', 'islandora_label_maker', 'includes/utilities');
$idsfile = drush_get_option('ids_file');
$includerelated = drush_get_option('include_related', FALSE);
$skipunchanged = drush_get_option('skip_unchanged', FALSE);
$silent = drush_get_option('silent', FALSE);
$ids = islandora_label_maker_ids_from_file($idsfile);
$columns = explode(';', drush_get_option('format', 'identifier;newlabel;oldlabel'));
$columns = array_combine($columns, $columns);
$f = fopen('php://stdout', 'w');
foreach ($ids as $id) {
islandora_label_maker_putcsv_columns_for_id($f, $columns, $id, $skipunchanged, $silent);
if ($includerelated) {
$related = islandora_label_maker_related_solrobj_for_object($id);
foreach ($related as $solrrel) {
$relid = $solrrel['PID'];
islandora_label_maker_putcsv_columns_for_id($f, $columns, $relid, $skipunchanged, $silent);
}
}
}
fclose($f);
}
/**
* Helper function for printing of CSV line to file pointer.
*
* @param resource $f
* A file pointer to write to.
* @param array $columns
* An associated array containing column names for both keys and values.
* @param string $id
* The id of the object used.
* @param bool $skipunchanged
* If TRUE, don't write anything if the new label is the same as the old.
* @param bool $silent
* If TRUE, does not log when the label cannot be generated.
*
* @return bool
* True if handled succesfully, FALSE if printing failed.
*/
function islandora_label_maker_putcsv_columns_for_id($f, $columns, $id, $skipunchanged = FALSE, $silent = FALSE) {
$newlabel = islandora_label_maker_label_for_object_id($id);
if ($newlabel === FALSE) {
if (!$silent) {
drush_log(t("Cannot generate label for @id", array('@id' => $id)), 'warning');
}
return TRUE;
}
$fields = $columns;
$object = NULL;
if ($skipunchanged) {
$object = islandora_object_load($id);
if ($object->label === $newlabel) {
return TRUE;
}
}
if (isset($columns['oldlabel'])) {
if (!isset($object)) {
$object = islandora_object_load($id);
}
$fields['oldlabel'] = $object->label;
}
if (isset($columns['identifier'])) {
$fields['identifier'] = $id;
}
$fields['newlabel'] = $newlabel;
if (fputcsv($f, $fields, ';') === FALSE) {
return FALSE;
}
return TRUE;
}
/**
* Implements drush_hook_COMMAND().
*/
function drush_islandora_label_maker_queue() {
module_load_include('inc', 'islandora_label_maker', 'includes/utilities');
if (!variable_get('islandora_label_maker_change_label_on_modify', FALSE)) {
if (!drush_confirm(t("Automatic label generation is disabled in the configuration. Are you sure you want to manually add these ids to the queue and make new labels for them?"))) {
drush_user_abort();
}
}
$idsfile = drush_get_option('ids_file');
$includerelated = drush_get_option('include_related', FALSE);
$ids = islandora_label_maker_ids_from_file($idsfile);
foreach ($ids as $id) {
islandora_label_maker_add_to_label_queue($id, 0, $includerelated);
}
}
/**
* Implements drush_hook_COMMAND().
*/
function drush_islandora_label_maker_update() {
module_load_include('inc', 'islandora_label_maker', 'includes/utilities');
$idsfile = drush_get_option('ids_file');
$silent = drush_get_option('silent', FALSE);
$includerelated = drush_get_option('include_related', FALSE);
$ids = islandora_label_maker_ids_from_file($idsfile);
foreach ($ids as $id) {
islandora_label_maker_drush_update_label_for_object_id($id, $silent);
if ($includerelated) {
$related = islandora_label_maker_related_solrobj_for_object($id);
foreach ($related as $solrrel) {
$relid = $solrrel['PID'];
islandora_label_maker_drush_update_label_for_object_id($relid, $silent);
}
}
}
}
/**
* Helper function for updating label.
*/
function islandora_label_maker_drush_update_label_for_object_id($id, $silent) {
$result = islandora_label_maker_update_label_for_object_id($id);
if ($result === FALSE) {
if (!$silent) {
drush_log(t("Cannot generate label for @id", array('@id' => $id)), 'warning');
}
}
else {
if ($result['oldlabel'] === $result['newlabel']) {
if (!$silent) {
drush_log(t("Label for @id is already up-to-date.", array('@id' => $id)));
}
}
else {
drush_log(t(
"Changed label for @id from '@oldlabel' to '@newlabel'.",
array(
'@id' => $id,
'@oldlabel' => $result['oldlabel'],
'@newlabel' => $result['newlabel'],
)
));
}
}
}
/**
* Helper function for retrieving ids from file.
*/
function islandora_label_maker_ids_from_file($idsfile) {
$foundids = array();
$idsstr = file_get_contents($idsfile);
$fieldseparator = "";
$ids = str_getcsv($idsstr, "\n");
foreach ($ids as $index => $id) {
if (strlen($id) == 0) {
continue;
}
if ($fieldseparator === "") {
$fieldseparator = "\t";
$comps = str_getcsv($id, $fieldseparator);
if (count($comps) > 1) {
// Fields are separated by a tab.
}
else {
$fieldseparator = ",";
$comps = str_getcsv($id, $fieldseparator);
if (count($comps) > 1) {
// Fields are separated by a comma.
}
else {
$fieldseparator = ";";
$comps = str_getcsv($id, $fieldseparator);
if (count($comps) > 1) {
// Fields are separated by a semicolon.
}
else {
// Fields are not separated.
$fieldseparator = FALSE;
}
}
}
}
if ($fieldseparator !== FALSE) {
$comps = str_getcsv($id, $fieldseparator);
if (count($comps) > 1) {
$id = $comps[0];
}
}
$foundids[] = $id;
}
return $foundids;
}