forked from artesis/os_convert
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathos_convert.module
241 lines (207 loc) · 6.3 KB
/
os_convert.module
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
<?php
/**
* @file
* Converts ID's from current OS version, to OS 3.1
*/
define('OS_CONVERT_STEP', 2000);
/**
* Implments hook_menu().
*/
function os_convert_menu() {
$menu = array();
$menu['admin/config/ting/os_convert'] = array(
'title' => 'OpenSearch convert',
'description' => 'Convert id\'s from one format to another',
'access arguments' => array('administer ting settings'),
'page callback' => 'drupal_get_form',
'page arguments' => array('os_convert_admin_form'),
'file' => 'os_convert.admin.inc',
);
$menu['admin/config/ting/os_convert/progress'] = array(
'title' => 'Conversion progress',
'access arguments' => array('administer ting settings'),
'page callback' => 'os_convert_count',
'type' => MENU_CALLBACK,
);
return $menu;
}
/**
* AJAX callback.
*
* Mainly return current conversion status. Invokes next item id conversion
* if needed.
*/
function os_convert_count() {
$count = os_convert_objects_count();
$processed = $count - os_convert_leftover_count(TRUE);
$percentage = $processed * 100 / $count;
$t = array(
'@processed' => $processed,
'@count' => $count,
'@percentage' => sprintf("%.4f", $percentage),
);
echo drupal_json_encode(array(
'percentage' => $t['@percentage'],
'message' => t('Overall progress: @processed/@count (@percentage%)', $t),
));
if ($processed < $count) {
os_convert_update_objects();
}
drupal_exit();
}
/**
* Fetch the item from the well and update it's id
* in the ting_object/ting_object_revision tables.
*/
function os_convert_update_objects() {
$processed = variable_get('os_tmp_processed', 0);
$query = db_select('ting_object_leftover', 'tiol')
->fields('tiol', array('tid', 'ding_entity_id'))
->range(0, 1);
$result = $query->execute()->fetchAll();
$ting_query_string = array();
foreach ($result as $entry) {
$ting_query_string[$entry->tid] = $entry->ding_entity_id;
}
$url = variable_get('ting_search_url', '');
$agency = variable_get('ting_agency', '');
$profile = variable_get('ting_search_profile', '');
try {
$client = new NanoSOAPClient($url);
$parameters = array(
'agency' => $agency,
'profile' => $profile,
'outputType' => 'json',
'start' => '1',
'stepValue' => '1',
'query' => 'rec.id=' . os_convert_generate_local_id_from_id($result[0]->ding_entity_id),
);
$response = $client->call('searchRequest', $parameters);
}
catch (Exception $e) {
$wt = array(
'@url' => $url,
'@agency' => $agency,
'@profile' => $profile,
'@query' => $parameters['query'],
);
watchdog('os_convert', 'Call failed. @url/@agency/@profile/@query', $wt, WATCHDOG_NOTICE);
}
$data = json_decode($response);
$temp = array();
if (is_object($data->searchResponse->result) && $data->searchResponse->result->hitCount->{'$'} > 0) {
foreach ($data->searchResponse->result->searchResult as $item) {
$object = $item->collection->object[0];
$os_3x_id = $object->identifier->{'$'};
$temp[$result[0]->ding_entity_id] = $os_3x_id;
}
}
foreach ($ting_query_string as $tid => $ding_entitiy_id) {
if (isset($temp[$ding_entitiy_id])) {
db_update('ting_object')
->fields(array(
'ding_entity_id' => $temp[$ding_entitiy_id],
))
->condition('tid', $tid, '=')
->execute();
db_merge('ting_object_convert')
->fields(array(
'tid' => $tid,
'old_id' => $result[0]->ding_entity_id,
'new_id' => $temp[$ding_entitiy_id],
))
->condition('tid', $tid, '=')
->execute();
}
else {
// @todo
// Probably write a log of items that could not be converted.
}
}
db_delete('ting_object_leftover')
->condition('tid', $result[0]->tid, '=')
->execute();
$processed++;
variable_set('os_tmp_processed', $processed);
}
/**
* Get the local from full id (faust number).
*/
function os_convert_generate_local_id_from_id($item_id) {
$id = explode(':', $item_id);
return isset($id[1]) ? $id[1] : $item_id;
}
/**
* Fill the table with objects id's that are pending conversion.
*
* Plain sql for this would be:
* SELECT t1.tid FROM `ting_object` t1
* LEFT JOIN `ting_object_convert` t2
* ON t1.tid = t2.tid
* WHERE t2.tid IS NULL AND t1.ding_entity_id not regexp '^[0-9]{6}-[^:]+:.*'
*
* In plain words - what exists in t1 and not in t2.
*/
function os_convert_update_left_over() {
db_query('TRUNCATE `ting_object_leftover`');
db_query("INSERT INTO `ting_object_leftover` (`tid`, `ding_entity_id`)
SELECT t1.tid, t1.ding_entity_id
FROM `ting_object` t1
LEFT JOIN `ting_object_convert` t2
ON t1.tid = t2.tid
WHERE t2.tid IS NULL AND t1.ding_entity_id not regexp '^[0-9]{6}-[^:]+:.*'");
}
/**
* COunt how many objects had been processed.
*
* Statically cached, call whenever we want it.
*/
function os_convert_processed_count($reset = FALSE) {
$count = &drupal_static(__FUNCTION__, NULL);
if (!is_int($count) || $reset) {
$query = db_select('ting_object_convert')->countQuery();
$result = $query->execute()->fetchCol('expression');
$count = $result[0];
variable_set('os_convert_count', $count);
}
else {
$count = variable_get('os_convert_count', 0);
}
return $count;
}
/**
* Count how many objects are there.
*
* Statically cached, call whenever we want it.
*/
function os_convert_objects_count($reset = FALSE) {
$count = &drupal_static(__FUNCTION__, NULL);
if (!is_int($count) || $reset) {
$query = db_select('ting_object')->countQuery();
$result = $query->execute()->fetchCol('expression');
$count = $result[0];
variable_set('os_objects_count', $count);
}
else {
$count = variable_get('os_objects_count', 0);
}
return $count;
}
/**
* Count how many objects pending conversion.
*
* Statically cached, call whenever we want it.
*/
function os_convert_leftover_count($reset = FALSE) {
$count = &drupal_static(__FUNCTION__, NULL);
if (!is_int($count) || $reset) {
$query = db_select('ting_object_leftover')->countQuery();
$result = $query->execute()->fetchCol('expression');
$count = $result[0];
variable_set('os_objects_leftover_count', $count);
}
else {
$count = variable_get('os_objects_leftover_count', 0);
}
return $count;
}