-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfeeds_dashboard.module
More file actions
520 lines (459 loc) · 16.3 KB
/
Copy pathfeeds_dashboard.module
File metadata and controls
520 lines (459 loc) · 16.3 KB
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
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
<?php
/**
* Implements hook_menu().
*/
function feeds_dashboard_menu() {
$items['import/%feeds_importer/history'] = array(
'title' => 'History',
'type' => MENU_LOCAL_TASK,
'page callback' => 'feeds_dashboard_show_history',
'page arguments' => array(1),
'access callback' => 'feeds_access',
'access arguments' => array('import', 1),
'file' => 'feeds_dashboard.pages.inc',
'weight' => -9,
);
$items['import/%feeds_importer/history/operation/%'] = array(
'title' => 'Operation',
'type' => MENU_CALLBACK,
'page callback' => 'feeds_dashboard_show_operation',
'page arguments' => array(1,4),
'access callback' => 'feeds_access',
'access arguments' => array('import', 1),
'file' => 'feeds_dashboard.pages.inc',
);
$items['import/%feeds_importer/history/operation/%/revert'] = array(
'title' => 'Operation',
'type' => MENU_CALLBACK,
'page callback' => 'feeds_dashboard_revert_operation',
'page arguments' => array(1,4),
'access callback' => 'feeds_access',
'access arguments' => array('import', 1)
);
$items['import/%feeds_importer/history/operation/%/%'] = array(
'title' => 'Operation content',
'type' => MENU_CALLBACK,
'page callback' => 'feeds_dashboard_show_operation_content',
'page arguments' => array(1,4,5),
'access callback' => 'feeds_access',
'access arguments' => array('import', 1),
'file' => 'feeds_dashboard.pages.inc',
);
$items['import/%feeds_importer/init'] = array(
'title' => 'Initialize content',
'type' => MENU_LOCAL_TASK,
'page callback' => 'feeds_dashboard_initialize_content',
'page arguments' => array(1),
'access callback' => 'feeds_access',
'access arguments' => array('import', 1)
);
return $items;
}
/**
* Invoked before a feed source import starts.
*
* @param FeedsSource $source
* FeedsSource object that describes the source that is going to be imported.
*/
function feeds_dashboard_feeds_before_import(FeedsSource $source) {
// Create log in database and get dashboard id.
$source->did = db_insert('feeds_dashboard')
->fields(array(
'operation_description' => 'Started importing but the import failed (may be locked).',
'id' => $source->id,
'file' => '',
'date' => date('Y-m-d H:i:s'),
))
->execute();
// Set variable using $source_id
// since there can only be one
// import at a time.
variable_set($source->id, $source->did);
}
/**
* Invoked after a feed source has been imported.
*
* @param FeedsSource $source
* FeedsSource object that describes the source that has been imported.
*/
function feeds_dashboard_feeds_after_import(FeedsSource $source) {
// Delete temp variable.
$did = variable_get($source->id);
// Copy the source file to specific folder for history purpose.
if (isset($source->config['FeedsFileFetcher']['fid'])) {
$file = file_load($source->config['FeedsFileFetcher']['fid']);
$destination = drupal_realpath('public://') . '/feeds_dashboard/';
if (file_prepare_directory($destination, FILE_CREATE_DIRECTORY)) {
$destination = file_stream_wrapper_uri_normalize('public://feeds_dashboard/' . drupal_basename($file));
$copied_file = file_copy($file, $destination, FILE_EXISTS_RENAME);
$source_uri = $copied_file->uri;
} else {
drupal_set_message(t("Error while creating import folder, please check file writing permission to public:// folder"), 'error', FALSE);
}
} elseif (isset($source->config['FeedsOAIHTTPFetcher']['source'])) {
$source_uri = $source->config['FeedsOAIHTTPFetcher']['source'];
} elseif (isset($source->config['FeedsHTTPFetcher']['source'])) {
$source_uri = $source->config['FeedsHTTPFetcher']['source'];
} else {
$source_uri = t("No source found.");
}
// Log results.
$results = $source->state['process'];
$operation_description = t('@skipped skipped, @created created, @updated updated, @deleted deleted, @failed failed, @blocked blocked.', array('@skipped' => $results->skipped, '@created' => $results->created, '@updated' => $results->updated, '@deleted' => $results->deleted, '@failed' => $results->failed, '@blocked' => $results->blocked));
db_update('feeds_dashboard')
->fields(array(
'operation_description' => $operation_description,
'file' => $source_uri,
'date' => date('Y-m-d H:i:s'),
))
->condition('did', $did, '=')
->execute();
// Delete temp variable.
variable_del($source->id);
}
/**
* Invoked after a feed source has been cleared of its items.
*
* @param FeedsSource $source
* FeedsSource object that describes the source that has been cleared.
*/
function feeds_dashboard_feeds_after_clear(FeedsSource $source) {
// Log feed deletion.
$results = $source->state['process_clear'];
$operation_description = t('<span class="fd-content-clear">Feed content clear</span>: @skipped skipped, @created created, @updated updated, @deleted deleted.', array('@skipped' => $results->skipped, '@created' => $results->created, '@updated' => $results->updated, '@deleted' => $results->deleted));
db_insert('feeds_dashboard')
->fields(array(
'id' => $source->id,
'operation_description' => $operation_description,
'date' => date('Y-m-d H:i:s'),
))
->execute();
}
/**
* Implements hook_node_delete().
*/
function feeds_dashboard_node_delete($node) {
db_update('feeds_dashboard_operations')
->fields(array(
'entity_id' => -1, // -1 is deleted.
'state' => 'deleted'
))
->condition('entity_id', $node->nid, '=')
->execute();
}
/**
* Invoked after a feed item has been saved.
*
* @param FeedsSource $source
* FeedsSource object that describes the source that is being imported.
* @param $entity
* The entity object that has just been saved.
* @param array $item
* The parser result for this entity.
* @param int|null $entity_id
* The id of the current item which is going to be updated. If this is a new
* item, then NULL is passed.
*/
function feeds_dashboard_feeds_after_save(FeedsSource $source, $entity, $item, $entity_id) {
$did = variable_get($source->id);
// State of content.
$state = 'created';
if($entity->feeds_item->is_new === FALSE) {
$state = 'updated';
}
// Log created content.
db_insert('feeds_dashboard_operations')
->fields(array(
'did' => $did,
'entity' => serialize($entity),
'content' => serialize($item),
'state' => $state,
'entity_id' => $entity->nid,
))
->execute();
}
/**
* Implements hook_theme().
*/
function feeds_dashboard_theme() {
$theme_subfolder = drupal_get_path('module', 'feeds_dashboard') . '/theme';
$items['feeds_dashboard_history_page'] = array(
'variables' => array('operations' => array()),
'path' => $theme_subfolder,
'template' => 'feeds-dashboard--history',
);
$items['feeds_dashboard_details'] = array(
'variables' => array('data' => array()),
'path' => $theme_subfolder,
'template' => 'feeds-dashboard--table',
);
return $items;
}
/**
* Returns an array of operations given a feed id.
*
* @param string $id
* Id of the feed configuration.
*/
function feeds_dashboard_get_operations_for_feed($id) {
$operations = db_select('feeds_dashboard', 'fd')
->fields('fd')
->condition('id', $id, '=')
->orderBy('date', 'DESC')
->execute()
->fetchAllAssoc('did');
return $operations;
}
/**
* Returns an operation given an operation id.
*
* @param int $did
* operation id.
*/
function feeds_dashboard_get_operation($did) {
$operation = db_select('feeds_dashboard', 'fd')
->fields('fd')
->condition('did', $did, '=')
->execute()
->fetchAllAssoc('did');
return $operation;
}
/**
* Returns import data given an import id.
*
* @param int $imid
* import id.
*/
function feeds_dashboard_get_imported_data($imid) {
$data = db_select('feeds_dashboard_operations', 'fdo')
->fields('fdo')
->condition('imid', $imid, '=')
->execute()
->fetchAssoc();
return $data;
}
/**
* Returns an array of imported content
* given an operation id.
*
* @param string $did
* Id of the operation.
*/
function feeds_dashboard_get_data_for_operation($did) {
$data = db_select('feeds_dashboard_operations', 'fdo')
->fields('fdo')
->condition('did', $did, '=')
->orderBy('imid', 'ASC')
->execute()
->fetchAllAssoc('imid');
return $data;
}
/**
* Deletes revisions for a specific operation
* given an operation id.
*
* @param FeedsImporter $feed
* Feed configuration.
* @param string $did
* Id of the operation.
* @param bool $confirm
* Used to display confirmation page, if $confirm is
* TRUE, the revert will be launched.
*/
function feeds_dashboard_revert_operation(FeedsImporter $feed_importer, $did, $confirm = '') {
$data = feeds_dashboard_get_data_for_operation($did);
if (!empty($data)) {
// Confirm screen.
$output = t("The following revisions are about to be deleted:");
$list = array();
// Display revisions which are about to be deleted.
foreach ($data as $imid => $row) {
$entity = unserialize($row->entity);
$list['items'][] = l($entity->title, $entity->feeds_item->entity_type . '/' . $entity->feeds_item->entity_id . '/revisions/' . $entity->vid . '/view');
}
$output .= theme('item_list', $list);
$output .= l(t("Confirm"), current_path() . "/confirm", array('attributes' => array('class' => 'button')));
if ($confirm === '') {
return $output;
}
if ($confirm === 'confirm') {
$success = $error = 0;
// Delete latest revision.
foreach ($data as $imid => $row) {
$entity = unserialize($row->entity);
$state = feeds_dashboard_node_revision_revert($entity);
// Update operation table with state.
if($state !== FALSE) {
db_update('feeds_dashboard_operations')
->fields(array(
'state' => 'deleted'
))
->condition('did', $did, '=')
->execute();
$success += 1;
}
}
if ($error > 0) {
drupal_set_message(t("@success revisions were successfully deleted, @error revisions couldn't be deleted", array('@success' => $success, '@error' => $error)), 'warning', FALSE);
} else {
drupal_set_message(t("@success revisions were successfully deleted.", array('@success' => $success, '@error' => $error)), 'status', FALSE);
}
// Go back to dashboard.
drupal_goto('import/' . arg(1) . '/history');
}
}
return t("No data for this operation.");
}
/**
* Revert a node to previous revision.
*
* @param Entity $entity
* Entity object to revert.
*/
function feeds_dashboard_node_revision_revert(&$entity) {
if($entity->feeds_item->entity_type == 'node') {
// Try to delete revision, node_revision_delete()
// will return FALSE if revision is current.
if (node_revision_delete($old_vid) !== FALSE) {
return TRUE;
} else {
// If we can't delete the revision, we have
// to find the second most recent revision
// and duplicate it into our new on in order
// to revert to it.
$revision_list = node_revision_list($entity);
$old_vid = $entity->vid;
unset($revision_list[$entity->vid]);
// If multiple revisions revert to last.
if (count($revision_list) > 0) {
$last_revision = max(array_keys($revision_list));
$entity = entity_revision_load('node', $last_revision);
$entity->log = "Reverted via revert feed.";
$saved = entity_save('node', $entity);
// Invalidate hash.
db_update('feeds_item')
->fields(array(
'hash' => 'invalidated'
))
->condition('entity_id', $entity->nid)
->execute();
if ($saved !== FALSE) {
// Delete previous revision now that
// we have saved the new one.
if(node_revision_delete($old_vid) !== FALSE) {
return TRUE;
} else {
return FALSE;
}
} else {
return FALSE;
}
} else {
return entity_delete('node', $entity->nid);
}
}
} else {
drupal_set_message(t("Couldn't revert, some entities are not nodes."), 'error', FALSE);
}
return FALSE;
}
/**
* Initialize a content type (and its
* GUID) into Feeds.
* Useful if you want to update entities
* that have not been imported with Feeds.
*
* @param FeedsImporter $feed
* Feed configuration.
* @param bool $confirm
* Used to display confirmation page, if $confirm is
* TRUE, the revert will be launched.
*/
function feeds_dashboard_initialize_content(FeedsImporter $feed_importer, $confirm = '') {
// Get config from importer and check for
// mapping configuration.
$config = $feed_importer->getConfig();
if (!isset($config['processor']['config']['mappings']) || !empty($config['processor']['config']['mappings'])) {
// Look for GUID.
$guid = FALSE;
foreach ($config['processor']['config']['mappings'] as $mapping) {
if($mapping['target'] == 'guid') {
$guid = $mapping['source'];
}
}
if($guid !== FALSE) {
// Presentation.
$output = t("<p>Launching content initialization can be useful if you need to update entities that were not imported with Feeds. If you click the \"Confirm\" button, the \"feeds_item\" table will be populated with the content from the <strong>%bundle</strong> bundle with the GUID <strong>%guid</strong>.</p><p><strong>Do you want to initialize your content?</strong></p>", array('%bundle' => $feed_importer->processor->config['bundle'], '%guid' => $guid));
$output .= l(t("Confirm"), current_path() . "/confirm", array('attributes' => array('class' => 'button')));
if ($confirm === '') {
return $output;
}
if ($confirm === 'confirm') {
// Gather info to be inserted in table.
$bundle = $feed_importer->processor->config['bundle'];
$processor_name = $feed_importer->config['processor']['plugin_key'];
$importer_id = $feed_importer->id;
$imported = time();
switch ($processor_name) {
case 'FeedsNodeProcessor':
$entity_type = 'node';
break;
case 'FeedsUserProcessor':
$entity_type = 'user';
break;
// TODO: Handle other entity types.
default:
$entity_type = 'node';
break;
}
$query = new EntityFieldQuery;
$query->entityCondition('entity_type', $entity_type)
->entityCondition('bundle', $bundle);
// TODO: Handle other entity types.
$results = $query->execute();
if (isset($results['node'])) {
$nodes = node_load_multiple(array_keys($results['node']));
$success = $skipped = 0;
foreach ($nodes as $nid => $node) {
$already_initialized = db_select('feeds_item', 'f')
->fields('f')
->condition('entity_type', $entity_type)
->condition('entity_id', $nid)
->execute()
->fetchAssoc();
if(empty($already_initialized)) {
// Get GUID.
if(isset($node->{'field_' . $guid}[LANGUAGE_NONE][0]['value'])) {
$guid_value = $node->{'field_' . $guid}[LANGUAGE_NONE][0]['value'];
} else {
$guid_value = '';
}
// Insert content in feeds_item table.
db_insert('feeds_item')
->fields(array(
'entity_type' => $entity_type,
'entity_id' => $nid,
'id' => $importer_id,
'feed_nid' => 0,
'imported' => $imported,
'url' => '',
'guid' => $guid_value,
'hash' => ''
))
->execute();
$success++;
} else {
$skipped++;
}
}
}
drupal_set_message(t("@success entities were successfully initialized, @skipped were skipped because already initialized.", array('@success' => $success, '@skipped' => $skipped)), 'status', FALSE);
// Go back to dashboard.
drupal_goto('import/' . arg(1));
}
} else {
return t("No GUID found in mappings, please add a GUID.");
}
}
return t("No config found for this importer, please make sure that your mapping is properly set up.");
}