-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathuofm_batch_index.drush.inc
210 lines (192 loc) · 6.23 KB
/
uofm_batch_index.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
<?php
/**
* @file
* Drush commands to add items
* to the reindexing queue.
*/
/**
* Implements hook_drush_command().
*/
function uofm_batch_index_drush_command() {
$items = array();
$items['uofm_batch_index_preprocess'] = array(
'options' => array(
'query' => array(
'description' => 'The SPARQL where clause, uses "?object" as the returned variable.',
),
'pid' => array(
'description' => 'A PID to re-index',
),
'pidlist' => array(
'description' => 'A comma seperated list of PIDs to re-index',
),
'pidfile' => array(
'description' => 'Path to a textfile of PIDs to re-index, one per line',
),
'recursive' => array(
'description' => 'Re-index any children objects of those listed/queried',
'value' => 'optional',
),
'force' => array(
'description' => 'Force indexing even if already in Solr, CAREFUL this could re-index ALOT of stuff.',
'value' => 'optional',
),
),
'aliases' => array('uofm_bi_pp'),
'callback' => 'uofm_batch_index_preprocess_queue',
'description' => 'Add items to the queue to be re-indexed on cron runs.',
'drupal dependencies' => array(
'islandora',
'islandora_solr',
),
'bootstrap' => DRUSH_BOOTSTRAP_DRUPAL_LOGIN,
);
return $items;
}
/**
* Load pids into the queue.
*/
function uofm_batch_index_preprocess_queue() {
if (is_null(drush_get_option('query', NULL)) &&
is_null(drush_get_option('pid', NULL)) &&
is_null(drush_get_option("pidlist", NULL)) &&
is_null(drush_get_option("pidfile", NULL))) {
drush_set_error('no value', 'You must enter one of --query, --pid, --pidlist or --pidfile');
}
$user = variable_get('islandora_collection_search_gsearch_user', NULL);
$passwd = variable_get('islandora_collection_search_gsearch_password', NULL);
if (is_null($user) || is_null($passwd)) {
drush_log('GSearch username or passwd has not been set, check the Collection Search admin page.', 'error');
return;
}
$results = array();
$force = (bool) (!is_null(drush_get_option('force', NULL)));
$recursive = (bool) (!is_null(drush_get_option('recursive', NULL)));
if (!is_null(drush_get_option('query', NULL))) {
$query_param = drush_get_option('query');
$results = __uofm_batch_index_sparql_query($query_param);
}
elseif (!is_null(drush_get_option('pid', NULL))) {
$pid = drush_get_option('pid', "");
$results[] = $pid;
}
elseif (!is_null(drush_get_option('pidlist', NULL))) {
$list = drush_get_option('pidlist', "");
$tmp_res = explode($list, ',');
foreach ($tmp_res as $t) {
$t = trim($t);
if (strlen($t) > 0) {
$results[] = $t;
}
}
}
elseif (!is_null(drush_get_option('pidfile', NULL))) {
if (file_exists(drush_get_option('pidfile', ''))) {
$fp = @fopen(drush_get_option('pidfile', ''), 'r');
if (!$fp) {
drush_set_error("Unable to open file " . drush_get_option('pidfile', ''));
}
while (!feof($fp)) {
$line = trim(fgets($fp, 4096));
drush_log("Processing line ($line)", "notice");
if (is_array($line)) {
ob_start();
var_dump($line);
$x = ob_get_clean();
drush_set_error("Trying to process an array ($x)");
}
if (!empty($line)) {
$results[] = $line;
}
}
}
else {
drush_set_error("File: " . drush_get_option('pidfile', '') . " does not exist.");
}
}
if ($recursive) {
$new_results = __uofm_batch_index_recursive_query($results);
$results = array_unique($new_results);
}
__uofm_batch_index_add_to_queue($results, $force);
}
/**
* Recurse down to the children.
*/
function __uofm_batch_index_recursive_query($pids, $results = array()) {
foreach ($pids as $parent) {
$results[] = $parent;
$children = __uofm_batch_index_get_children($parent);
if (is_array($children) && count($children) > 0) {
$results = __uofm_batch_index_recursive_query($children, $results);
}
}
return $results;
}
/**
* Add an array of items to the Drupal queue.
*/
function __uofm_batch_index_add_to_queue(array $items, $force = FALSE) {
module_load_include('inc', 'islandora', 'includes/utilities');
module_load_include('php', 'islandora_solr', 'SolrPhpClient/Apache/Solr/Service');
$queue = DrupalQueue::get(UOFM_BATCH_INDEX_QUEUE);
$path_parts = parse_url(variable_get('islandora_solr_url', 'localhost:8080/solr'));
$solr = new Apache_Solr_Service($path_parts['host'], $path_parts['port'], $path_parts['path'] . '/');
$solr->setCreateDocuments(0);
foreach ($items as $object) {
if (is_string($object) && islandora_is_valid_pid($object)) {
$add = FALSE;
if ($force) {
$add = TRUE;
}
else {
// Search for all the possible PIDs.
$solr_query = '+PID:("' . $object . '")';
$solr_params = array('fl' => 'PID');
$res = $solr->search($solr_query, 0, 10, $solr_params);
if ($res && $res->response->numFound <= 0) {
// Only add if not in Solr index.
$add = TRUE;
}
}
if ($add) {
$queue->createItem($object);
}
}
}
}
/**
* Perform our Sparql query and return an array of PIDs.
*/
function __uofm_batch_index_sparql_query($where) {
drupal_static_reset('islandora_get_tuque_connection');
$connection = islandora_get_tuque_connection();
$query = 'select ?object from <#ri> where { ';
$query .= $where;
$query .= ' }';
$temp = $connection->repository->ri->sparqlQuery($query, 'unlimited');
$get_pid = function($o) {
if (is_array($o) && array_key_exists('object', $o)) {
return $o['object']['value'];
}
else {
ob_start();
print_r($o);
$x = ob_get_clean();
drush_log("Error trying to get value of sparql object ($x)", 'error');
}
};
return array_map($get_pid, $temp);
}
/**
* Query for the children of an object.
*/
function __uofm_batch_index_get_children($parent) {
module_load_include('inc', 'islandora', 'includes/utilities');
$results = array();
if (islandora_is_valid_pid($parent)) {
$query = "?object <fedora-rels-ext:isMemberOf> <info:fedora/$parent>";
$results = __uofm_batch_index_sparql_query($query);
}
return $results;
}