-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrefresher.module
321 lines (245 loc) · 9.45 KB
/
refresher.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
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
309
310
311
312
313
314
315
316
317
318
319
320
321
<?php
/**
* @file
* Outlines how a module can use the Batch API.
*/
/**
* @defgroup Refresher
* @ingroup EVP
* @{
* Batch importer builder for facebook\'s social graph query string to current timestamp (since date).
*
* The @link refresher.install .install file @endlink also shows how the
* Batch API can be used to handle long-running hook_update_N() functions.
*
* @see batch
*/
function refresher_permission() {
$perms = array(
'update socialgraph query' => array(
'title' => t('update socialgraph query'),
),
);
return $perms;
}
/**
* Implements hook_menu().
*/
function refresher_menu() {
$items['refresher'] = array(
'title' => 'Batch update socialgraph queries',
'description' => t('Refreshes feeds'),
'page callback' => 'drupal_get_form',
'page arguments' => array('refresher_simple_form'),
'access arguments' => array('update socialgraph query'),
'type' => MENU_CALLBACK,
);
return $items;
}
/**
* Form builder function to allow choice of which batch to run.
*/
function refresher_simple_form() {
$form['description'] = array(
'#type' => 'markup',
'#markup' => t('This example offers two different batches. The first does 1000 identical operations, each completed in on run; the second does 20 operations, but each takes more than one run to operate if there are more than 5 nodes.'),
);
$form['batch'] = array(
'#type' => 'select',
'#title' => 'Choose batch',
'#options' => array(
'batch_1' => t('Batch importer for facebook\'s social graph (since)'),
'batch_2' => t('Batch Lastfm.'),
'batch_3' => t('Metric query.'),
),
);
$form['submit'] = array(
'#type' => 'submit',
'#value' => 'Go import feed items',
);
return $form;
}
function refresher_simple_form_submit($form, &$form_state) {
if ($form_state['values']['batch']=='batch_1') {
$provider = 'facebook_feed';
variable_set('facebook_feed', $provider);
//dsm(variable_get('provider'));
}
if ($form_state['values']['batch']=='batch_2') {
$provider = 'lastfm';
variable_set('lastfm', $provider);
//dsm(variable_get('provider'));
}
// If no nodes, prevent submission.
// Find out if we have a node to work with. Otherwise it won't work.
$nid = refresher_get_nids($provider);
if (empty($nid)) {
drupal_set_message(t("You don't currently have any nodes, and this example requires a node to work with. As a result, this form is disabled."));
$form['submit']['#disabled'] = TRUE;
exit();
}
$function = 'refresher_' . $form_state['values']['batch'];
$_SESSION['http_request_count'] = 0; // reset counter for debug information.
// Execute the function named refresher_1 or refresher_2.
$batch = $function();
batch_set($batch);
}
/**
* Batch 1 definition: Goes through all the node ids and .
* This creates an operations array defining what batch 1 should do, including
* what it should do when it's finished. In this case, each operation is the
* same and by chance even has the same $nid to operate on, but we could have
* a mix of different types of operations in the operations array.
*/
function refresher_batch_1() {
$nodeids = refresher_get_nids('facebook_feed');
$num_operations = count($nodeids);
drupal_set_message(t('Creating an array of @num operations', array('@num' => $num_operations)));
$operations = array();
$i = 1;
foreach ($nodeids as $nid) {
$operations[] = array('refresher_op_1', array($nid, t('(Operation @operation)', array('@operation' => $i))));
}
$batch = array(
'operations' => $operations,
'finished' => 'refresher_finished',
);
return $batch;
}
function refresher_batch_2() {
$nodeids = refresher_get_nids(variable_get('provider'));
$num_operations = count($nodeids);
drupal_set_message(t('Creating an array of @num operations', array('@num' => $num_operations)));
$operations = array();
$i = 1;
foreach ($nodeids as $nid) {
$operations[] = array('refresher_op_2', array($nid, t('(Operation @operation)', array('@operation' => $i))));
}
$batch = array(
'operations' => $operations,
'finished' => 'refresher_finished',
);
return $batch;
}
function refresher_batch_3() {
$termids = metric_query_load('taxonomy_term_vocab_name');
$num_operations = count($nodeids);
drupal_set_message(t('Creating an array of @num operations', array('@num' => $num_operations)));
$operations = array();
$i = 1;
foreach ($nodeids as $nid) {
$operations[] = array('refresher_op_3', array($nid, t('(Operation @operation)', array('@operation' => $i))));
}
$batch = array(
'operations' => $operations,
'finished' => 'refresher_finished',
);
return $batch;
}
/**
* Batch operation for batch 1: load a node.
* This is the function that is called on each operation in batch 1.
*/
function refresher_op_1($nid, $operation_details, &$context) {
$node = node_load($nid, NULL, TRUE);
$title = $node->title;
$cityname = str_replace(" ","+", $title);
$node->feeds['OAuth2HTTPSFetcher']['source'] = 'https://graph.facebook.com/search?q='. $title .'&type=event&fields=' . 'cover,description,location,name,owner,start_time,timezone,end_time,ticket_uri,venue&since=' . $_SERVER['REQUEST_TIME'];
node_save($node);
feeds_batch('import', 'fb', $node->nid, $context);
// Store some results for post-processing in the 'finished' callback.
// The contents of 'results' will be available as $results in the
// 'finished' function (in this example, refresher_finished()).
$context['results'][] = $node->nid . ' : ' . check_plain($node->title);
// Optional message displayed under the progressbar.
$context['message'] = t('Loading node "@title"', array('@title' => $node->title)) . ' ' . $operation_details;
_refresher_update_http_requests();
}
/*
* Last.fm Operations
*
*/
function refresher_op_2($nid, $operation_details, &$context) {
$node = node_load($nid, NULL, TRUE);
$title = $node->title;
// feeds_batch('import', 'lastfm', $node->nid, $context);
node_save($node);
// Store some results for post-processing in the 'finished' callback.
// The contents of 'results' will be available as $results in the
// 'finished' function (in this example, refresher_finished()).
$context['results'][] = $node->nid . ' : ' . check_plain($node->title);
// Optional message displayed under the progressbar.
$context['message'] = t('Loading node "@title"', array('@title' => $node->title)) . ' ' . $operation_details;
_refresher_update_http_requests();
}
function refresher_op_3($fb_object_id, $operation_details, &$context){
// metric_query_load('last_fm_artists');
$node = new stdClass(); // Create a new node object
$node->type = $provider; // Or any other content type you want
$node->title = $subject;
$node->language = LANGUAGE_NONE; // Or any language code if Locale module is enabled. More on this below *
$node->uid = 1; // Or any id you wish
$cityname = str_replace(" ","+", $title);
$node->feeds['OAuth2HTTPSFetcher']['source'] = 'https://graph.facebook.com/search?q='. $title .'/{object-id}/insights/{metric-name}';
node_save($node);
feeds_batch('import', 'fb', $node->nid, $context);
// Store some results for post-processing in the 'finished' callback.
// The contents of 'results' will be available as $results in the
// 'finished' function (in this example, refresher_finished()).
$context['results'][] = $node->nid . ' : ' . check_plain($node->title);
// Optional message displayed under the progressbar.
$context['message'] = t('Loading node "@title"', array('@title' => $node->title)) . ' ' . $operation_details;
_refresher_update_http_requests();
}
/**
* Batch 'finished' callback used by both batch 1 and batch 2.
*/
function refresher_finished($success, $results, $operations) {
if ($success) {
// Here we could do something meaningful with the results.
// We just display the number of nodes we processed...
drupal_set_message(t('@count results processed in @requests HTTP requests.', array('@count' => count($results), '@requests' => _refresher_get_http_requests())));
drupal_set_message(t('The final result was "%final"', array('%final' => end($results))));
dpr($results);
}
else {
// An error occurred.
// $operations contains the operations that remained unprocessed.
$error_operation = reset($operations);
drupal_set_message(t('An error occurred while processing @operation with arguments : @args', array('@operation' => $error_operation[0], '@args' => print_r($error_operation[0], TRUE))));
}
}
/**
* Utility function - simply queries and loads the lowest nid.
* @return A nid or NULL if there are no nodes.
*/
function refresher_get_nids($provider) {
$query = new EntityFieldQuery();
$query->entityCondition('entity_type', 'node')
->entityCondition('bundle', 'facebook_feed') // could be facebook_feed
->propertyCondition('status', 1);
$result = $query->execute();
if (!empty($result['node'])) {
$nids = array_keys($result['node']);
}
return $nids;
}
/**
* @} End of "defgroup batch_definitions".
*/
/**
* Utility function to count the HTTP requests in a session variable.
*/
function _refresher_update_http_requests() {
$_SESSION['http_request_count']++;
}
function _refresher_get_http_requests() {
return !empty($_SESSION['http_request_count']) ? $_SESSION['http_request_count'] : 0;
}
/**
* @} End of "defgroup refresher".
*/
function refresher_cron() {
drupal_set_message(t('Running refresher programmaticall batch import - needs testing'), TRUE);
refresher_batch_1();
}