1
|
<?php
|
2
|
|
3
|
/**
|
4
|
* @file
|
5
|
* Feeds - basic API functions and hook implementations.
|
6
|
*/
|
7
|
|
8
|
// Common request time, use as point of reference and to avoid calls to time().
|
9
|
define('FEEDS_REQUEST_TIME', time());
|
10
|
// Do not schedule a feed for refresh.
|
11
|
define('FEEDS_SCHEDULE_NEVER', -1);
|
12
|
// Never expire feed items.
|
13
|
define('FEEDS_EXPIRE_NEVER', -1);
|
14
|
// An object that is not persistent. Compare EXPORT_IN_DATABASE, EXPORT_IN_CODE.
|
15
|
define('FEEDS_EXPORT_NONE', 0x0);
|
16
|
// Status of batched operations.
|
17
|
define('FEEDS_BATCH_COMPLETE', 1.0);
|
18
|
define('FEEDS_BATCH_ACTIVE', 0.0);
|
19
|
|
20
|
/**
|
21
|
* @defgroup hooks Hook and callback implementations
|
22
|
* @{
|
23
|
*/
|
24
|
|
25
|
/**
|
26
|
* Implements hook_hook_info().
|
27
|
*/
|
28
|
function feeds_hook_info() {
|
29
|
$hooks = array(
|
30
|
'feeds_plugins',
|
31
|
'feeds_after_parse',
|
32
|
'feeds_before_import',
|
33
|
'feeds_before_update',
|
34
|
'feeds_presave',
|
35
|
'feeds_after_save',
|
36
|
'feeds_after_import',
|
37
|
'feeds_after_clear',
|
38
|
'feeds_processor_targets',
|
39
|
'feeds_processor_targets_alter',
|
40
|
'feeds_parser_sources_alter',
|
41
|
'feeds_config_defaults',
|
42
|
'feeds_fetcher_config_defaults',
|
43
|
'feeds_parser_config_defaults',
|
44
|
'feeds_processor_config_defaults',
|
45
|
);
|
46
|
|
47
|
return array_fill_keys($hooks, array('group' => 'feeds'));
|
48
|
}
|
49
|
|
50
|
/**
|
51
|
* Implements hook_cron().
|
52
|
*/
|
53
|
function feeds_cron() {
|
54
|
// Expire old log entries.
|
55
|
db_delete('feeds_log')
|
56
|
->condition('request_time', REQUEST_TIME - 604800, '<')
|
57
|
->execute();
|
58
|
|
59
|
// Find importers that need to be rescheduled.
|
60
|
$importers = feeds_reschedule();
|
61
|
if ($importers) {
|
62
|
// @todo Maybe we should queue this somehow as well. This could be potentially
|
63
|
// very long.
|
64
|
$sources = db_query("SELECT feed_nid, id FROM {feeds_source} WHERE id IN (:ids)", array(':ids' => $importers));
|
65
|
|
66
|
foreach ($sources as $source) {
|
67
|
feeds_source($source->id, $source->feed_nid)->schedule();
|
68
|
}
|
69
|
|
70
|
feeds_reschedule(FALSE);
|
71
|
}
|
72
|
|
73
|
// Sync the files in the cache directory with entries in the cache every now
|
74
|
// and then. By default: every six hours.
|
75
|
$last_check = variable_get('feeds_sync_cache_feeds_http_last_check');
|
76
|
$interval = variable_get('feeds_sync_cache_feeds_http_interval', 21600);
|
77
|
if ($last_check < (REQUEST_TIME - $interval)) {
|
78
|
// Check first if the task isn't already queued.
|
79
|
$queue = DrupalQueue::get('feeds_sync_cache_feeds_http');
|
80
|
if ($queue->numberOfItems() < 1) {
|
81
|
// Queue sync task.
|
82
|
FeedsHTTPCache::getInstance('cache_feeds_http')->startSync();
|
83
|
}
|
84
|
variable_set('feeds_sync_cache_feeds_http_last_check', REQUEST_TIME);
|
85
|
}
|
86
|
}
|
87
|
|
88
|
/**
|
89
|
* Implements hook_cron_job_scheduler_info().
|
90
|
*
|
91
|
* Compare queue names with key names in feeds_cron_queue_info().
|
92
|
*/
|
93
|
function feeds_cron_job_scheduler_info() {
|
94
|
$info = array();
|
95
|
$info['feeds_source_import'] = array(
|
96
|
'queue name' => 'feeds_source_import',
|
97
|
);
|
98
|
// feeds_source_clear never gets called, since we now use the queue directly.
|
99
|
// This is left in case any background jobs still running after an
|
100
|
// upgrade.
|
101
|
$info['feeds_source_clear'] = array(
|
102
|
'queue name' => 'feeds_source_clear',
|
103
|
);
|
104
|
$info['feeds_source_expire'] = array(
|
105
|
'queue name' => 'feeds_source_expire',
|
106
|
);
|
107
|
$info['feeds_push_unsubscribe'] = array(
|
108
|
'queue name' => 'feeds_push_unsubscribe',
|
109
|
);
|
110
|
return $info;
|
111
|
}
|
112
|
|
113
|
/**
|
114
|
* Implements hook_cron_queue_info().
|
115
|
*/
|
116
|
function feeds_cron_queue_info() {
|
117
|
$queues = array();
|
118
|
$queues['feeds_source_import'] = array(
|
119
|
'worker callback' => 'feeds_source_import',
|
120
|
'time' => 60,
|
121
|
);
|
122
|
$queues['feeds_source_clear'] = array(
|
123
|
'worker callback' => 'feeds_source_clear',
|
124
|
);
|
125
|
$queues['feeds_source_expire'] = array(
|
126
|
'worker callback' => 'feeds_source_expire',
|
127
|
);
|
128
|
$queues['feeds_push_unsubscribe'] = array(
|
129
|
'worker callback' => 'feeds_push_unsubscribe',
|
130
|
);
|
131
|
$queues['feeds_sync_cache_feeds_http'] = array(
|
132
|
'worker callback' => 'feeds_sync_cache_feeds_http',
|
133
|
);
|
134
|
|
135
|
return $queues;
|
136
|
}
|
137
|
|
138
|
/**
|
139
|
* Scheduler callback for importing from a source.
|
140
|
*/
|
141
|
function feeds_source_import(array $job) {
|
142
|
$source = _feeds_queue_worker_helper($job, 'import');
|
143
|
if ($source->doesExist()) {
|
144
|
$source->scheduleImport();
|
145
|
}
|
146
|
}
|
147
|
|
148
|
/**
|
149
|
* Scheduler callback for deleting all items from a source.
|
150
|
*/
|
151
|
function feeds_source_clear(array $job) {
|
152
|
$source = _feeds_queue_worker_helper($job, 'clear');
|
153
|
if ($source->doesExist()) {
|
154
|
$source->scheduleClear();
|
155
|
}
|
156
|
}
|
157
|
|
158
|
/**
|
159
|
* Scheduler callback for expiring content.
|
160
|
*/
|
161
|
function feeds_source_expire(array $job) {
|
162
|
$source = _feeds_queue_worker_helper($job, 'expire');
|
163
|
if ($source->doesExist()) {
|
164
|
$source->scheduleExpire();
|
165
|
}
|
166
|
}
|
167
|
|
168
|
/**
|
169
|
* Executes a method on a feed source.
|
170
|
*
|
171
|
* @param array $job
|
172
|
* The job being run.
|
173
|
* @param string $method
|
174
|
* The method to execute.
|
175
|
*/
|
176
|
function _feeds_queue_worker_helper(array $job, $method) {
|
177
|
$source = feeds_source($job['type'], $job['id']);
|
178
|
try {
|
179
|
$source->existing()->$method();
|
180
|
}
|
181
|
catch (FeedsNotExistingException $e) {
|
182
|
// Eventually remove the job from job scheduler.
|
183
|
JobScheduler::get('feeds_source_import')->remove($job);
|
184
|
}
|
185
|
catch (Exception $e) {
|
186
|
$source->log($method, $e->getMessage(), array(), WATCHDOG_ERROR);
|
187
|
}
|
188
|
|
189
|
return $source;
|
190
|
}
|
191
|
|
192
|
/**
|
193
|
* Scheduler callback for syncing the feeds cache directory with the entries in
|
194
|
* the cache.
|
195
|
*/
|
196
|
function feeds_sync_cache_feeds_http(array $job) {
|
197
|
FeedsHTTPCache::getInstance('cache_feeds_http')->sync($job['files']);
|
198
|
}
|
199
|
|
200
|
/**
|
201
|
* Scheduler callback for unsubscribing from PuSH hubs.
|
202
|
*/
|
203
|
function feeds_push_unsubscribe($job) {
|
204
|
$source = feeds_source($job['type'], $job['id']);
|
205
|
$fetcher = feeds_plugin('FeedsHTTPFetcher', $source->importer->id);
|
206
|
$fetcher->unsubscribe($source);
|
207
|
}
|
208
|
|
209
|
/**
|
210
|
* Batch API worker callback. Used by FeedsSource::startBatchAPIJob().
|
211
|
*
|
212
|
* @see FeedsSource::startBatchAPIJob().
|
213
|
*
|
214
|
* @todo Harmonize Job Scheduler API callbacks with Batch API callbacks?
|
215
|
*
|
216
|
* @param $method
|
217
|
* Method to execute on importer; one of 'import' or 'clear'.
|
218
|
* @param $importer_id
|
219
|
* Identifier of a FeedsImporter object.
|
220
|
* @param $feed_nid
|
221
|
* If importer is attached to content type, feed node id identifying the
|
222
|
* source to be imported.
|
223
|
* @param $context
|
224
|
* Batch context.
|
225
|
*/
|
226
|
function feeds_batch($method, $importer_id, $feed_nid = 0, &$context) {
|
227
|
$context['finished'] = FEEDS_BATCH_COMPLETE;
|
228
|
try {
|
229
|
$context['finished'] = feeds_source($importer_id, $feed_nid)->$method();
|
230
|
}
|
231
|
catch (Exception $e) {
|
232
|
drupal_set_message($e->getMessage(), 'error');
|
233
|
}
|
234
|
}
|
235
|
|
236
|
/**
|
237
|
* Reschedule one or all importers.
|
238
|
*
|
239
|
* @param string $importer_id
|
240
|
* If TRUE, all importers will be rescheduled, if FALSE, no importers will
|
241
|
* be rescheduled, if an importer id, only importer of that id will be
|
242
|
* rescheduled.
|
243
|
*
|
244
|
* @return array
|
245
|
* An list of importers that need rescheduling.
|
246
|
*/
|
247
|
function feeds_reschedule($importer_id = NULL) {
|
248
|
$reschedule = variable_get('feeds_reschedule', FALSE);
|
249
|
|
250
|
if ($importer_id === TRUE || $importer_id === FALSE) {
|
251
|
$reschedule = $importer_id;
|
252
|
}
|
253
|
elseif (is_string($importer_id) && $reschedule !== TRUE) {
|
254
|
$reschedule = array_filter((array) $reschedule);
|
255
|
$reschedule[$importer_id] = $importer_id;
|
256
|
}
|
257
|
|
258
|
if (isset($importer_id)) {
|
259
|
variable_set('feeds_reschedule', $reschedule);
|
260
|
}
|
261
|
|
262
|
if ($reschedule === TRUE) {
|
263
|
return feeds_enabled_importers();
|
264
|
}
|
265
|
elseif ($reschedule === FALSE) {
|
266
|
return array();
|
267
|
}
|
268
|
|
269
|
return $reschedule;
|
270
|
}
|
271
|
|
272
|
/**
|
273
|
* Implements feeds_permission().
|
274
|
*/
|
275
|
function feeds_permission() {
|
276
|
$perms = array(
|
277
|
'administer feeds' => array(
|
278
|
'title' => t('Administer Feeds'),
|
279
|
'description' => t('Create, update, delete importers, execute import and delete tasks on any importer.')
|
280
|
),
|
281
|
);
|
282
|
foreach (feeds_importer_load_all() as $importer) {
|
283
|
$perms["import $importer->id feeds"] = array(
|
284
|
'title' => t('Import @name feeds', array('@name' => $importer->config['name'])),
|
285
|
);
|
286
|
$perms["clear $importer->id feeds"] = array(
|
287
|
'title' => t('Delete items from @name feeds', array('@name' => $importer->config['name'])),
|
288
|
);
|
289
|
$perms["unlock $importer->id feeds"] = array(
|
290
|
'title' => t('Unlock imports from @name feeds', array('@name' => $importer->config['name'])),
|
291
|
'description' => t('If a feed importation breaks for some reason, users with this permission can unlock them.')
|
292
|
);
|
293
|
}
|
294
|
return $perms;
|
295
|
}
|
296
|
|
297
|
/**
|
298
|
* Implements hook_forms().
|
299
|
*
|
300
|
* Declare form callbacks for all known classes derived from FeedsConfigurable.
|
301
|
*/
|
302
|
function feeds_forms($form_id, $args) {
|
303
|
// Check if the requested form is a Feeds form.
|
304
|
if (!stripos($form_id, '_feeds_form')) {
|
305
|
return;
|
306
|
}
|
307
|
|
308
|
$forms = array();
|
309
|
$forms['FeedsImporter_feeds_form']['callback'] = 'feeds_form';
|
310
|
$plugins = FeedsPlugin::all();
|
311
|
foreach ($plugins as $plugin) {
|
312
|
$forms[$plugin['handler']['class'] . '_feeds_form']['callback'] = 'feeds_form';
|
313
|
}
|
314
|
return $forms;
|
315
|
}
|
316
|
|
317
|
/**
|
318
|
* Implements hook_menu().
|
319
|
*/
|
320
|
function feeds_menu() {
|
321
|
$items = array();
|
322
|
$items['import'] = array(
|
323
|
'title' => 'Import',
|
324
|
'page callback' => 'feeds_page',
|
325
|
'access callback' => 'feeds_page_access',
|
326
|
'file' => 'feeds.pages.inc',
|
327
|
);
|
328
|
$items['import/%feeds_importer'] = array(
|
329
|
'title callback' => 'feeds_importer_title',
|
330
|
'title arguments' => array(1),
|
331
|
'page callback' => 'drupal_get_form',
|
332
|
'page arguments' => array('feeds_import_form', 1),
|
333
|
'access callback' => 'feeds_access',
|
334
|
'access arguments' => array('import', 1),
|
335
|
'file' => 'feeds.pages.inc',
|
336
|
);
|
337
|
$items['import/%feeds_importer/import'] = array(
|
338
|
'title' => 'Import',
|
339
|
'type' => MENU_DEFAULT_LOCAL_TASK,
|
340
|
'weight' => -10,
|
341
|
);
|
342
|
$items['import/%feeds_importer/delete-items'] = array(
|
343
|
'title' => 'Delete items',
|
344
|
'page callback' => 'drupal_get_form',
|
345
|
'page arguments' => array('feeds_delete_tab_form', 1),
|
346
|
'access callback' => 'feeds_access',
|
347
|
'access arguments' => array('clear', 1),
|
348
|
'file' => 'feeds.pages.inc',
|
349
|
'type' => MENU_LOCAL_TASK,
|
350
|
);
|
351
|
$items['import/%feeds_importer/unlock'] = array(
|
352
|
'title' => 'Unlock',
|
353
|
'page callback' => 'drupal_get_form',
|
354
|
'page arguments' => array('feeds_unlock_tab_form', 1),
|
355
|
'access callback' => 'feeds_access',
|
356
|
'access arguments' => array('unlock', 1),
|
357
|
'file' => 'feeds.pages.inc',
|
358
|
'type' => MENU_LOCAL_TASK,
|
359
|
);
|
360
|
$items['import/%feeds_importer/template'] = array(
|
361
|
'page callback' => 'feeds_importer_template',
|
362
|
'page arguments' => array(1),
|
363
|
'access callback' => 'feeds_access',
|
364
|
'access arguments' => array('import', 1),
|
365
|
'file' => 'feeds.pages.inc',
|
366
|
'type' => MENU_CALLBACK,
|
367
|
);
|
368
|
$items['node/%node/import'] = array(
|
369
|
'title' => 'Import',
|
370
|
'page callback' => 'drupal_get_form',
|
371
|
'page arguments' => array('feeds_import_tab_form', 1),
|
372
|
'access callback' => 'feeds_access',
|
373
|
'access arguments' => array('import', 1),
|
374
|
'file' => 'feeds.pages.inc',
|
375
|
'type' => MENU_LOCAL_TASK,
|
376
|
'weight' => 10,
|
377
|
);
|
378
|
$items['node/%node/delete-items'] = array(
|
379
|
'title' => 'Delete items',
|
380
|
'page callback' => 'drupal_get_form',
|
381
|
'page arguments' => array('feeds_delete_tab_form', NULL, 1),
|
382
|
'access callback' => 'feeds_access',
|
383
|
'access arguments' => array('clear', 1),
|
384
|
'file' => 'feeds.pages.inc',
|
385
|
'type' => MENU_LOCAL_TASK,
|
386
|
'weight' => 11,
|
387
|
);
|
388
|
$items['node/%node/unlock'] = array(
|
389
|
'title' => 'Unlock',
|
390
|
'page callback' => 'drupal_get_form',
|
391
|
'page arguments' => array('feeds_unlock_tab_form', NULL, 1),
|
392
|
'access callback' => 'feeds_access',
|
393
|
'access arguments' => array('unlock', 1),
|
394
|
'file' => 'feeds.pages.inc',
|
395
|
'type' => MENU_LOCAL_TASK,
|
396
|
'weight' => 11,
|
397
|
);
|
398
|
// @todo Eliminate this step and thus eliminate clearing menu cache when
|
399
|
// manipulating importers.
|
400
|
foreach (feeds_importer_load_all() as $importer) {
|
401
|
$items += $importer->fetcher->menuItem();
|
402
|
}
|
403
|
return $items;
|
404
|
}
|
405
|
|
406
|
/**
|
407
|
* Implements hook_admin_paths().
|
408
|
*/
|
409
|
function feeds_admin_paths() {
|
410
|
$paths = array(
|
411
|
'import' => TRUE,
|
412
|
'import/*' => TRUE,
|
413
|
'node/*/import' => TRUE,
|
414
|
'node/*/delete-items' => TRUE,
|
415
|
'node/*/log' => TRUE,
|
416
|
);
|
417
|
return $paths;
|
418
|
}
|
419
|
|
420
|
/**
|
421
|
* Menu loader callback.
|
422
|
*/
|
423
|
function feeds_importer_load($id) {
|
424
|
try {
|
425
|
$importer = feeds_importer($id);
|
426
|
if ($importer->doesExist()) {
|
427
|
return $importer;
|
428
|
}
|
429
|
}
|
430
|
catch (InvalidArgumentException $e) {}
|
431
|
|
432
|
return FALSE;
|
433
|
}
|
434
|
|
435
|
/**
|
436
|
* Title callback.
|
437
|
*/
|
438
|
function feeds_importer_title(FeedsImporter $importer) {
|
439
|
return $importer->config['name'];
|
440
|
}
|
441
|
|
442
|
/**
|
443
|
* Implements hook_theme().
|
444
|
*/
|
445
|
function feeds_theme() {
|
446
|
return array(
|
447
|
'feeds_upload' => array(
|
448
|
'file' => 'feeds.pages.inc',
|
449
|
'render element' => 'element',
|
450
|
),
|
451
|
'feeds_source_status' => array(
|
452
|
'file' => 'feeds.pages.inc',
|
453
|
'variables' => array(
|
454
|
'progress_importing' => NULL,
|
455
|
'progress_clearing' => NULL,
|
456
|
'imported' => NULL,
|
457
|
'count' => NULL,
|
458
|
),
|
459
|
),
|
460
|
);
|
461
|
}
|
462
|
|
463
|
/**
|
464
|
* Menu access callback.
|
465
|
*
|
466
|
* @param $action
|
467
|
* The action to be performed. Possible values are:
|
468
|
* - import
|
469
|
* - clear
|
470
|
* - unlock
|
471
|
* @param $param
|
472
|
* Node object or FeedsImporter id.
|
473
|
*/
|
474
|
function feeds_access($action, $param) {
|
475
|
if (!in_array($action, array('import', 'clear', 'unlock'))) {
|
476
|
// If $action is not one of the supported actions, we return access denied.
|
477
|
return FALSE;
|
478
|
}
|
479
|
|
480
|
$importer_id = FALSE;
|
481
|
if (is_string($param)) {
|
482
|
$importer_id = $param;
|
483
|
}
|
484
|
elseif ($param instanceof FeedsImporter) {
|
485
|
$importer_id = $param->id;
|
486
|
}
|
487
|
elseif ($param->type) {
|
488
|
$importer_id = feeds_get_importer_id($param->type);
|
489
|
}
|
490
|
|
491
|
// Check for permissions if feed id is present, otherwise return FALSE.
|
492
|
if ($importer_id) {
|
493
|
if (user_access('administer feeds') || user_access("{$action} {$importer_id} feeds")) {
|
494
|
return TRUE;
|
495
|
}
|
496
|
}
|
497
|
return FALSE;
|
498
|
}
|
499
|
|
500
|
/**
|
501
|
* Access callback to determine if the user can import Feeds importers.
|
502
|
*
|
503
|
* Feeds imports require an additional access check because they are PHP
|
504
|
* code and PHP is more locked down than administer feeds.
|
505
|
*/
|
506
|
function feeds_importer_import_access() {
|
507
|
return user_access('administer feeds') && user_access('use PHP for settings');
|
508
|
}
|
509
|
|
510
|
/**
|
511
|
* Menu access callback.
|
512
|
*/
|
513
|
function feeds_page_access() {
|
514
|
if (user_access('administer feeds')) {
|
515
|
return TRUE;
|
516
|
}
|
517
|
foreach (feeds_enabled_importers() as $id) {
|
518
|
if (user_access("import $id feeds")) {
|
519
|
return TRUE;
|
520
|
}
|
521
|
}
|
522
|
return FALSE;
|
523
|
}
|
524
|
|
525
|
/**
|
526
|
* Implements hook_exit().
|
527
|
*/
|
528
|
function feeds_exit() {
|
529
|
// Process any pending PuSH subscriptions.
|
530
|
$jobs = feeds_get_subscription_jobs();
|
531
|
foreach ($jobs as $job) {
|
532
|
if (!isset($job['fetcher']) || !isset($job['source'])) {
|
533
|
continue;
|
534
|
}
|
535
|
$job['fetcher']->subscribe($job['source']);
|
536
|
}
|
537
|
|
538
|
if (drupal_static('feeds_log_error', FALSE)) {
|
539
|
watchdog('feeds', 'Feeds reported errors, visit the Feeds log for details.', array(), WATCHDOG_ERROR, l(t('view'), 'admin/reports/feeds'));
|
540
|
}
|
541
|
}
|
542
|
|
543
|
/**
|
544
|
* Implements hook_views_api().
|
545
|
*/
|
546
|
function feeds_views_api() {
|
547
|
return array(
|
548
|
'api' => 3,
|
549
|
'path' => drupal_get_path('module', 'feeds') . '/views',
|
550
|
);
|
551
|
}
|
552
|
|
553
|
/**
|
554
|
* Implements hook_ctools_plugin_api().
|
555
|
*/
|
556
|
function feeds_ctools_plugin_api($owner, $api) {
|
557
|
if ($owner == 'feeds' && $api == 'plugins') {
|
558
|
return array('version' => 1);
|
559
|
}
|
560
|
}
|
561
|
|
562
|
/**
|
563
|
* Implements hook_ctools_plugin_type().
|
564
|
*/
|
565
|
function feeds_ctools_plugin_type() {
|
566
|
return array(
|
567
|
'plugins' => array(
|
568
|
'cache' => TRUE,
|
569
|
'use hooks' => TRUE,
|
570
|
'classes' => array('handler'),
|
571
|
),
|
572
|
);
|
573
|
}
|
574
|
|
575
|
/**
|
576
|
* Implements hook_feeds_plugins().
|
577
|
*/
|
578
|
function feeds_feeds_plugins() {
|
579
|
module_load_include('inc', 'feeds', 'feeds.plugins');
|
580
|
return _feeds_feeds_plugins();
|
581
|
}
|
582
|
|
583
|
/**
|
584
|
* Gets the feed_nid for a single entity.
|
585
|
*
|
586
|
* @param int $entity_id
|
587
|
* The entity id.
|
588
|
* @param string $entity_type
|
589
|
* The type of entity.
|
590
|
*
|
591
|
* @return int|bool
|
592
|
* The feed_nid of the entity, or FALSE if the entity doesn't belong to a
|
593
|
* feed.
|
594
|
*/
|
595
|
function feeds_get_feed_nid($entity_id, $entity_type) {
|
596
|
return db_query("SELECT feed_nid FROM {feeds_item} WHERE entity_type = :type AND entity_id = :id", array(':type' => $entity_type, ':id' => $entity_id))->fetchField();
|
597
|
}
|
598
|
|
599
|
/**
|
600
|
* Implements hook_entity_insert().
|
601
|
*/
|
602
|
function feeds_entity_insert($entity, $type) {
|
603
|
list($id) = entity_extract_ids($type, $entity);
|
604
|
feeds_item_info_insert($entity, $id);
|
605
|
}
|
606
|
|
607
|
/**
|
608
|
* Implements hook_entity_update().
|
609
|
*/
|
610
|
function feeds_entity_update($entity, $type) {
|
611
|
list($id) = entity_extract_ids($type, $entity);
|
612
|
feeds_item_info_save($entity, $id);
|
613
|
}
|
614
|
|
615
|
/**
|
616
|
* Implements hook_entity_delete().
|
617
|
*/
|
618
|
function feeds_entity_delete($entity, $type) {
|
619
|
list($id) = entity_extract_ids($type, $entity);
|
620
|
|
621
|
// Delete any imported items produced by the source.
|
622
|
db_delete('feeds_item')
|
623
|
->condition('entity_type', $type)
|
624
|
->condition('entity_id', $id)
|
625
|
->execute();
|
626
|
}
|
627
|
|
628
|
/**
|
629
|
* Implements hook_node_validate().
|
630
|
*/
|
631
|
function feeds_node_validate($node, $form, &$form_state) {
|
632
|
if (!$importer_id = feeds_get_importer_id($node->type)) {
|
633
|
return;
|
634
|
}
|
635
|
// Keep a copy of the title for subsequent node creation stages.
|
636
|
// @todo: revisit whether $node still looses all of its properties
|
637
|
// between validate and insert stage.
|
638
|
$last_title = &drupal_static('feeds_node_last_title');
|
639
|
$last_feeds = &drupal_static('feeds_node_last_feeds');
|
640
|
|
641
|
// On validation stage we are working with a FeedsSource object that is
|
642
|
// not tied to a nid - when creating a new node there is no
|
643
|
// $node->nid at this stage.
|
644
|
$source = feeds_source($importer_id);
|
645
|
|
646
|
// Node module magically moved $form['feeds'] to $node->feeds :P.
|
647
|
// configFormValidate may modify $last_feed, smuggle it to update/insert stage
|
648
|
// through a static variable.
|
649
|
$last_feeds = $node->feeds;
|
650
|
$source->configFormValidate($last_feeds);
|
651
|
|
652
|
// Check if title form element is hidden.
|
653
|
$title_hidden = (isset($form['title']['#access']) && !$form['title']['#access']);
|
654
|
|
655
|
// If the node title is empty and the title form element wasn't hidden, try to
|
656
|
// retrieve the title from the feed.
|
657
|
if (isset($node->title) && trim($node->title) == '' && !$title_hidden) {
|
658
|
try {
|
659
|
$source->addConfig($last_feeds);
|
660
|
if (!$last_title = $source->preview()->title) {
|
661
|
throw new Exception();
|
662
|
}
|
663
|
}
|
664
|
catch (Exception $e) {
|
665
|
drupal_set_message($e->getMessage(), 'error');
|
666
|
form_set_error('title', t('Could not retrieve title from feed.'));
|
667
|
}
|
668
|
}
|
669
|
}
|
670
|
|
671
|
/**
|
672
|
* Implements hook_node_presave().
|
673
|
*/
|
674
|
function feeds_node_presave($node) {
|
675
|
// Populate $node->title and $node->feed from result of validation phase.
|
676
|
$last_title = &drupal_static('feeds_node_last_title');
|
677
|
$last_feeds = &drupal_static('feeds_node_last_feeds');
|
678
|
if (empty($node->title) && !empty($last_title)) {
|
679
|
$node->title = $last_title;
|
680
|
}
|
681
|
if (!empty($last_feeds)) {
|
682
|
$node->feeds = $last_feeds;
|
683
|
}
|
684
|
$last_title = NULL;
|
685
|
$last_feeds = NULL;
|
686
|
|
687
|
// Update "changed" value if there was mapped to that.
|
688
|
if (isset($node->feeds_item->node_changed)) {
|
689
|
$node->changed = $node->feeds_item->node_changed;
|
690
|
}
|
691
|
}
|
692
|
|
693
|
/**
|
694
|
* Implements hook_node_insert().
|
695
|
*/
|
696
|
function feeds_node_insert($node) {
|
697
|
// Source attached to node.
|
698
|
if (isset($node->feeds) && $importer_id = feeds_get_importer_id($node->type)) {
|
699
|
$source = feeds_source($importer_id, $node->nid);
|
700
|
$source->addConfig($node->feeds);
|
701
|
$source->save();
|
702
|
|
703
|
// Start import if requested.
|
704
|
if (feeds_importer($importer_id)->config['import_on_create'] && !isset($node->feeds['suppress_import'])) {
|
705
|
$source->startImport();
|
706
|
}
|
707
|
// Schedule the source.
|
708
|
$source->schedule();
|
709
|
}
|
710
|
}
|
711
|
|
712
|
/**
|
713
|
* Implements hook_node_update().
|
714
|
*/
|
715
|
function feeds_node_update($node) {
|
716
|
// Source attached to node.
|
717
|
if (isset($node->feeds) && $importer_id = feeds_get_importer_id($node->type)) {
|
718
|
$source = feeds_source($importer_id, $node->nid);
|
719
|
$source->addConfig($node->feeds);
|
720
|
$source->save();
|
721
|
$source->ensureSchedule();
|
722
|
}
|
723
|
}
|
724
|
|
725
|
/**
|
726
|
* Implements hook_node_delete().
|
727
|
*/
|
728
|
function feeds_node_delete($node) {
|
729
|
// Source attached to node.
|
730
|
// Make sure we don't leave any orphans behind: Do not use
|
731
|
// feeds_get_importer_id() to determine importer id as the importer may have
|
732
|
// been deleted.
|
733
|
if ($importer_id = db_query("SELECT id FROM {feeds_source} WHERE feed_nid = :nid", array(':nid' => $node->nid))->fetchField()) {
|
734
|
feeds_source($importer_id, $node->nid)->delete();
|
735
|
}
|
736
|
}
|
737
|
|
738
|
/**
|
739
|
* Implements hook_form_BASE_FORM_ID_alter().
|
740
|
*/
|
741
|
function feeds_form_node_form_alter(&$form, $form_state) {
|
742
|
if ($importer_id = feeds_get_importer_id($form['#node']->type)) {
|
743
|
// Enable uploads.
|
744
|
$form['#attributes']['enctype'] = 'multipart/form-data';
|
745
|
|
746
|
// Build form.
|
747
|
$source = feeds_source($importer_id, empty($form['#node']->nid) ? NULL : $form['#node']->nid);
|
748
|
$form['feeds'] = array(
|
749
|
'#type' => 'fieldset',
|
750
|
'#title' => t('Feed'),
|
751
|
'#tree' => TRUE,
|
752
|
'#weight' => 0,
|
753
|
);
|
754
|
$form['feeds'] += $source->configForm($form_state);
|
755
|
$form['#feed_id'] = $importer_id;
|
756
|
|
757
|
// If the parser has support for delivering a source title, set node title
|
758
|
// to not required and try to retrieve it from the source if the node title
|
759
|
// is left empty by the user.
|
760
|
// @see feeds_node_validate()
|
761
|
if (isset($form['title']) && $source->importer()->parser->providesSourceTitle()) {
|
762
|
$form['title']['#required'] = FALSE;
|
763
|
}
|
764
|
}
|
765
|
}
|
766
|
|
767
|
/**
|
768
|
* Implements hook_field_extra_fields().
|
769
|
*/
|
770
|
function feeds_field_extra_fields() {
|
771
|
$extras = array();
|
772
|
foreach (node_type_get_names() as $type => $name) {
|
773
|
if (feeds_get_importer_id($type)) {
|
774
|
$extras['node'][$type]['form']['feeds'] = array(
|
775
|
'label' => t('Feed'),
|
776
|
'description' => t('Feeds module form elements'),
|
777
|
'weight' => 0,
|
778
|
);
|
779
|
}
|
780
|
}
|
781
|
return $extras;
|
782
|
}
|
783
|
|
784
|
/**
|
785
|
* Implements hook_features_pipe_COMPONENT_alter() for component "feeds_importer".
|
786
|
*
|
787
|
* Automatically adds dependencies when a Feed importer is selected in Features.
|
788
|
*/
|
789
|
function feeds_features_pipe_feeds_importer_alter(&$pipe, $data, &$export) {
|
790
|
foreach ($data as $importer_id) {
|
791
|
if ($importer = feeds_importer_load($importer_id)) {
|
792
|
$export['dependencies'] = array_merge($export['dependencies'], $importer->dependencies());
|
793
|
}
|
794
|
}
|
795
|
}
|
796
|
|
797
|
/**
|
798
|
* Implements hook_system_info_alter().
|
799
|
*
|
800
|
* Goes through a list of all modules that provide Feeds plugins and makes them
|
801
|
* required if there are any importers using those plugins.
|
802
|
*/
|
803
|
function feeds_system_info_alter(array &$info, $file, $type) {
|
804
|
if ($type !== 'module' || !module_exists($file->name) || !module_hook($file->name, 'feeds_plugins')) {
|
805
|
return;
|
806
|
}
|
807
|
|
808
|
// Don't make Feeds require itself, otherwise you can't disable Feeds until
|
809
|
// all importers are deleted.
|
810
|
if ($file->name === 'feeds' || !function_exists('ctools_include')) {
|
811
|
return;
|
812
|
}
|
813
|
|
814
|
// Get the plugins that belong to the current module.
|
815
|
ctools_include('plugins');
|
816
|
$module_plugins = array();
|
817
|
foreach (ctools_get_plugins('feeds', 'plugins') as $plugin_id => $plugin) {
|
818
|
if ($file->name === $plugin['module']) {
|
819
|
$module_plugins[$plugin_id] = TRUE;
|
820
|
}
|
821
|
}
|
822
|
|
823
|
// Check if any importers are using any plugins from the current module.
|
824
|
foreach (feeds_importer_load_all(TRUE) as $importer) {
|
825
|
|
826
|
// Skip importers that are defined in code and are provided by the current
|
827
|
// module. This ensures that modules that define both an importer and a
|
828
|
// plugin can still be disabled.
|
829
|
if ($importer->export_type == EXPORT_IN_CODE) {
|
830
|
$configs = ctools_export_load_object('feeds_importer', 'names', array($importer->id));
|
831
|
if (isset($configs[$importer->id]) && $configs[$importer->id]->export_module === $file->name) {
|
832
|
continue;
|
833
|
}
|
834
|
}
|
835
|
|
836
|
$configuration = $importer->getConfig();
|
837
|
|
838
|
foreach (array('fetcher', 'parser', 'processor') as $plugin_type) {
|
839
|
$plugin_key = $configuration[$plugin_type]['plugin_key'];
|
840
|
if (isset($module_plugins[$plugin_key])) {
|
841
|
$info['required'] = TRUE;
|
842
|
break 2;
|
843
|
}
|
844
|
}
|
845
|
}
|
846
|
|
847
|
if (empty($info['required'])) {
|
848
|
return;
|
849
|
}
|
850
|
|
851
|
if (module_exists('feeds_ui') && user_access('administer feeds')) {
|
852
|
$info['explanation'] = t('Feeds is currently using this module for one or more <a href="@link">importers</a>', array('@link' => url('admin/structure/feeds')));
|
853
|
}
|
854
|
else {
|
855
|
$info['explanation'] = t('Feeds is currently using this module for one or more importers');
|
856
|
}
|
857
|
}
|
858
|
|
859
|
/**
|
860
|
* Implements hook_module_implements_alter().
|
861
|
*/
|
862
|
function feeds_module_implements_alter(array &$implementations, $hook) {
|
863
|
if ($hook === 'feeds_processor_targets_alter') {
|
864
|
// We need two implementations of this hook, so we add one that gets
|
865
|
// called first, and move the normal one to last.
|
866
|
$implementations = array('_feeds' => FALSE) + $implementations;
|
867
|
|
868
|
// Move normal implementation to last.
|
869
|
$group = $implementations['feeds'];
|
870
|
unset($implementations['feeds']);
|
871
|
$implementations['feeds'] = $group;
|
872
|
}
|
873
|
}
|
874
|
|
875
|
/**
|
876
|
* Implements hook_feeds_processor_targets_alter().
|
877
|
*
|
878
|
* @see feeds_feeds_processor_targets()
|
879
|
* @see feeds_feeds_processor_targets_alter()
|
880
|
*/
|
881
|
function _feeds_feeds_processor_targets_alter(array &$targets, $entity_type, $bundle) {
|
882
|
// If hook_feeds_processor_targets() hasn't been called, for instance, by
|
883
|
// older processors, invoke it ourself.
|
884
|
if (!drupal_static('feeds_feeds_processor_targets', FALSE)) {
|
885
|
$targets += module_invoke_all('feeds_processor_targets', $entity_type, $bundle);
|
886
|
}
|
887
|
}
|
888
|
|
889
|
/**
|
890
|
* Implements hook_flush_caches().
|
891
|
*/
|
892
|
function feeds_flush_caches() {
|
893
|
// The update to add the table needs to have run. Taken from
|
894
|
// https://www.drupal.org/node/2511858
|
895
|
include_once DRUPAL_ROOT . '/includes/install.inc';
|
896
|
|
897
|
if (drupal_get_installed_schema_version('feeds') >= 7212) {
|
898
|
return array('cache_feeds_http');
|
899
|
}
|
900
|
return array();
|
901
|
}
|
902
|
|
903
|
/**
|
904
|
* Implements hook_admin_menu_output_build().
|
905
|
*
|
906
|
* Shows available importers in the "Content" section of the admin menu.
|
907
|
* Requires the "admin_menu" module to be enabled.
|
908
|
*/
|
909
|
function feeds_admin_menu_output_build(array &$content) {
|
910
|
// Add new top-level item to the menu.
|
911
|
if (!isset($content['menu'])) {
|
912
|
return;
|
913
|
}
|
914
|
$access = FALSE;
|
915
|
foreach (feeds_enabled_importers() as $importer_id) {
|
916
|
if (user_access('administer feeds') || user_access("import $importer_id feeds")) {
|
917
|
$access = TRUE;
|
918
|
break;
|
919
|
}
|
920
|
}
|
921
|
|
922
|
if (!$access) {
|
923
|
return;
|
924
|
}
|
925
|
|
926
|
$content['menu']['admin/content']['admin/content/feeds_import'] = array(
|
927
|
'#title' => t('Import'),
|
928
|
'#href' => 'import',
|
929
|
);
|
930
|
|
931
|
foreach (feeds_importer_load_all() as $importer) {
|
932
|
$content['menu']['admin/content']['admin/content/feeds_import'][$importer->id] = array(
|
933
|
'#title' => t($importer->config['name']),
|
934
|
'#href' => !empty($importer->config['content_type']) ? 'node/add/' . str_replace('_', '-', $importer->config['content_type']) : 'import/' . check_plain($importer->id),
|
935
|
'#access' => user_access('administer feeds') || user_access("import $importer->id feeds"),
|
936
|
);
|
937
|
}
|
938
|
}
|
939
|
|
940
|
/**
|
941
|
* Implements hook_menu_local_tasks_alter().
|
942
|
*
|
943
|
* Adds "Import" link as local action on content overview page.
|
944
|
*/
|
945
|
function feeds_menu_local_tasks_alter(&$data, $router_item, $root_path) {
|
946
|
if ($root_path == 'admin/content') {
|
947
|
$data['actions']['output'][] = array(
|
948
|
'#theme' => 'menu_local_task',
|
949
|
'#link' => array(
|
950
|
'title' => t('Import'),
|
951
|
'href' => 'import',
|
952
|
'localized_options' => array(
|
953
|
'attributes' => array(
|
954
|
'title' => t('Import'),
|
955
|
),
|
956
|
),
|
957
|
),
|
958
|
'#access' => feeds_page_access(),
|
959
|
// Add weight so it appears after the local action "Add content".
|
960
|
'#weight' => 1,
|
961
|
);
|
962
|
}
|
963
|
}
|
964
|
|
965
|
|
966
|
/**
|
967
|
* @}
|
968
|
*/
|
969
|
|
970
|
/**
|
971
|
* @defgroup utility Utility functions
|
972
|
* @{
|
973
|
*/
|
974
|
|
975
|
/**
|
976
|
* Loads all importers.
|
977
|
*
|
978
|
* @param $load_disabled
|
979
|
* Pass TRUE to load all importers, enabled or disabled, pass FALSE to only
|
980
|
* retrieve enabled importers.
|
981
|
*
|
982
|
* @return
|
983
|
* An array of all feed configurations available.
|
984
|
*/
|
985
|
function feeds_importer_load_all($load_disabled = FALSE) {
|
986
|
$feeds = array();
|
987
|
// This function can get called very early in install process through
|
988
|
// menu_router_rebuild(). Do not try to include CTools if not available.
|
989
|
if (function_exists('ctools_include')) {
|
990
|
ctools_include('export');
|
991
|
$configs = ctools_export_load_object('feeds_importer', 'all');
|
992
|
foreach ($configs as $config) {
|
993
|
if (!empty($config->id) && ($load_disabled || empty($config->disabled))) {
|
994
|
$feeds[$config->id] = feeds_importer($config->id);
|
995
|
}
|
996
|
}
|
997
|
uasort($feeds, 'feeds_importer_name_sort');
|
998
|
}
|
999
|
return $feeds;
|
1000
|
}
|
1001
|
|
1002
|
/**
|
1003
|
* Sorts importers by name.
|
1004
|
*
|
1005
|
* Callback for uasort().
|
1006
|
*
|
1007
|
* @param FeedsImporter $a
|
1008
|
* The first FeedsImporter for comparison.
|
1009
|
* @param FeedsImporter $b
|
1010
|
* The second FeedsImporter for comparison.
|
1011
|
*
|
1012
|
* @return int
|
1013
|
* The comparison result for uasort().
|
1014
|
*/
|
1015
|
function feeds_importer_name_sort(FeedsImporter $a, FeedsImporter $b) {
|
1016
|
return strcasecmp($a->config['name'], $b->config['name']);
|
1017
|
}
|
1018
|
|
1019
|
/**
|
1020
|
* Gets an array of enabled importer ids.
|
1021
|
*
|
1022
|
* @return
|
1023
|
* An array where the values contain ids of enabled importers.
|
1024
|
*/
|
1025
|
function feeds_enabled_importers() {
|
1026
|
return array_keys(_feeds_importer_digest());
|
1027
|
}
|
1028
|
|
1029
|
/**
|
1030
|
* Gets an enabled importer configuration by content type.
|
1031
|
*
|
1032
|
* @param $content_type
|
1033
|
* A node type string.
|
1034
|
*
|
1035
|
* @return
|
1036
|
* A FeedsImporter id if there is an importer for the given content type,
|
1037
|
* FALSE otherwise.
|
1038
|
*/
|
1039
|
function feeds_get_importer_id($content_type) {
|
1040
|
$importers = array_flip(_feeds_importer_digest());
|
1041
|
return isset($importers[$content_type]) ? $importers[$content_type] : FALSE;
|
1042
|
}
|
1043
|
|
1044
|
/**
|
1045
|
* Helper function for feeds_get_importer_id() and feeds_enabled_importers().
|
1046
|
*/
|
1047
|
function _feeds_importer_digest() {
|
1048
|
$importers = &drupal_static(__FUNCTION__);
|
1049
|
if ($importers === NULL) {
|
1050
|
if ($cache = cache_get(__FUNCTION__)) {
|
1051
|
$importers = $cache->data;
|
1052
|
}
|
1053
|
else {
|
1054
|
$importers = array();
|
1055
|
foreach (feeds_importer_load_all() as $importer) {
|
1056
|
$importers[$importer->id] = isset($importer->config['content_type']) ? $importer->config['content_type'] : '';
|
1057
|
}
|
1058
|
cache_set(__FUNCTION__, $importers);
|
1059
|
}
|
1060
|
}
|
1061
|
return $importers;
|
1062
|
}
|
1063
|
|
1064
|
/**
|
1065
|
* Resets importer caches. Call when enabling/disabling importers.
|
1066
|
*/
|
1067
|
function feeds_cache_clear($rebuild_menu = TRUE) {
|
1068
|
cache_clear_all('_feeds_importer_digest', 'cache');
|
1069
|
drupal_static_reset('_feeds_importer_digest');
|
1070
|
cache_clear_all('plugins:feeds:plugins', 'cache');
|
1071
|
ctools_include('export');
|
1072
|
ctools_export_load_object_reset('feeds_importer');
|
1073
|
drupal_static_reset('_node_types_build');
|
1074
|
if ($rebuild_menu) {
|
1075
|
menu_rebuild();
|
1076
|
}
|
1077
|
}
|
1078
|
|
1079
|
/**
|
1080
|
* Exports a FeedsImporter configuration to code.
|
1081
|
*/
|
1082
|
function feeds_export($importer_id, $indent = '') {
|
1083
|
ctools_include('export');
|
1084
|
$result = ctools_export_load_object('feeds_importer', 'names', array('id' => $importer_id));
|
1085
|
if (isset($result[$importer_id])) {
|
1086
|
return ctools_export_object('feeds_importer', $result[$importer_id], $indent);
|
1087
|
}
|
1088
|
}
|
1089
|
|
1090
|
/**
|
1091
|
* Logs to a file like /tmp/feeds_my_domain_org.log in temporary directory.
|
1092
|
*/
|
1093
|
function feeds_dbg($msg) {
|
1094
|
if (variable_get('feeds_debug', FALSE)) {
|
1095
|
if (!is_string($msg)) {
|
1096
|
$msg = var_export($msg, TRUE);
|
1097
|
}
|
1098
|
$filename = trim(str_replace('/', '_', $_SERVER['HTTP_HOST'] . base_path()), '_');
|
1099
|
$handle = fopen("temporary://feeds_$filename.log", 'a');
|
1100
|
fwrite($handle, gmdate('c') . "\t$msg\n");
|
1101
|
fclose($handle);
|
1102
|
}
|
1103
|
}
|
1104
|
|
1105
|
/**
|
1106
|
* Writes to feeds log.
|
1107
|
*/
|
1108
|
function feeds_log($importer_id, $feed_nid, $type, $message, $variables = array(), $severity = WATCHDOG_NOTICE) {
|
1109
|
if ($severity < WATCHDOG_NOTICE) {
|
1110
|
$error = &drupal_static('feeds_log_error', FALSE);
|
1111
|
$error = TRUE;
|
1112
|
}
|
1113
|
db_insert('feeds_log')
|
1114
|
->fields(array(
|
1115
|
'id' => $importer_id,
|
1116
|
'feed_nid' => $feed_nid,
|
1117
|
'log_time' => time(),
|
1118
|
'request_time' => REQUEST_TIME,
|
1119
|
'type' => $type,
|
1120
|
'message' => $message,
|
1121
|
'variables' => serialize($variables),
|
1122
|
'severity' => $severity,
|
1123
|
))
|
1124
|
->execute();
|
1125
|
}
|
1126
|
|
1127
|
/**
|
1128
|
* Loads an item info object.
|
1129
|
*
|
1130
|
* Example usage:
|
1131
|
*
|
1132
|
* $info = feeds_item_info_load('node', $node->nid);
|
1133
|
*/
|
1134
|
function feeds_item_info_load($entity_type, $entity_id) {
|
1135
|
return db_select('feeds_item')
|
1136
|
->fields('feeds_item')
|
1137
|
->condition('entity_type', $entity_type)
|
1138
|
->condition('entity_id', $entity_id)
|
1139
|
->execute()
|
1140
|
->fetchObject();
|
1141
|
}
|
1142
|
|
1143
|
/**
|
1144
|
* Inserts an item info object into the feeds_item table.
|
1145
|
*/
|
1146
|
function feeds_item_info_insert($entity, $entity_id) {
|
1147
|
if (isset($entity->feeds_item)) {
|
1148
|
$entity->feeds_item->entity_id = $entity_id;
|
1149
|
drupal_write_record('feeds_item', $entity->feeds_item);
|
1150
|
}
|
1151
|
}
|
1152
|
|
1153
|
/**
|
1154
|
* Inserts or updates an item info object in the feeds_item table.
|
1155
|
*/
|
1156
|
function feeds_item_info_save($entity, $entity_id) {
|
1157
|
if (isset($entity->feeds_item)) {
|
1158
|
$entity->feeds_item->entity_id = $entity_id;
|
1159
|
if (feeds_item_info_load($entity->feeds_item->entity_type, $entity_id)) {
|
1160
|
drupal_write_record('feeds_item', $entity->feeds_item, array('entity_type', 'entity_id'));
|
1161
|
}
|
1162
|
else {
|
1163
|
feeds_item_info_insert($entity, $entity_id);
|
1164
|
}
|
1165
|
}
|
1166
|
}
|
1167
|
|
1168
|
/**
|
1169
|
* @}
|
1170
|
*/
|
1171
|
|
1172
|
/**
|
1173
|
* @defgroup instantiators Instantiators
|
1174
|
* @{
|
1175
|
*/
|
1176
|
|
1177
|
/**
|
1178
|
* Gets an importer instance.
|
1179
|
*
|
1180
|
* @param $id
|
1181
|
* The unique id of the importer object.
|
1182
|
*
|
1183
|
* @return
|
1184
|
* A FeedsImporter object or an object of a class defined by the Drupal
|
1185
|
* variable 'feeds_importer_class'. There is only one importer object
|
1186
|
* per $id system-wide.
|
1187
|
*/
|
1188
|
function feeds_importer($id) {
|
1189
|
return FeedsConfigurable::instance(variable_get('feeds_importer_class', 'FeedsImporter'), $id);
|
1190
|
}
|
1191
|
|
1192
|
/**
|
1193
|
* Gets an instance of a source object.
|
1194
|
*
|
1195
|
* @param $importer_id
|
1196
|
* A FeedsImporter id.
|
1197
|
* @param $feed_nid
|
1198
|
* The node id of a feed node if the source is attached to a feed node.
|
1199
|
*
|
1200
|
* @return
|
1201
|
* A FeedsSource object or an object of a class defiend by the Drupal
|
1202
|
* variable 'source_class'.
|
1203
|
*/
|
1204
|
function feeds_source($importer_id, $feed_nid = 0) {
|
1205
|
return FeedsSource::instance($importer_id, $feed_nid);
|
1206
|
}
|
1207
|
|
1208
|
/**
|
1209
|
* Gets an instance of a class for a given plugin and id.
|
1210
|
*
|
1211
|
* @param string $plugin
|
1212
|
* A string that is the key of the plugin to load.
|
1213
|
* @param string $id
|
1214
|
* A string that is the id of the object.
|
1215
|
*
|
1216
|
* @return FeedsPlugin
|
1217
|
* A FeedsPlugin object.
|
1218
|
*/
|
1219
|
function feeds_plugin($plugin, $id) {
|
1220
|
ctools_include('plugins');
|
1221
|
|
1222
|
if ($class = ctools_plugin_load_class('feeds', 'plugins', $plugin, 'handler')) {
|
1223
|
return FeedsPlugin::instance($class, $id, ctools_get_plugins('feeds', 'plugins', $plugin));
|
1224
|
}
|
1225
|
|
1226
|
$args = array('%plugin' => $plugin, '@id' => $id);
|
1227
|
if (user_access('administer feeds')) {
|
1228
|
if (module_exists('feeds_ui')) {
|
1229
|
$args['@link'] = url('admin/structure/feeds/' . $id);
|
1230
|
drupal_set_message(t('Missing Feeds plugin %plugin. See <a href="@link">@id</a>. Check whether all required libraries and modules are installed properly.', $args), 'warning', FALSE);
|
1231
|
}
|
1232
|
else {
|
1233
|
drupal_set_message(t('Missing Feeds plugin %plugin used by the importer "@id". Check whether all required libraries and modules are installed properly. Enable the Feeds Admin UI module to check the importer\'s settings.', $args), 'warning', FALSE);
|
1234
|
}
|
1235
|
}
|
1236
|
else {
|
1237
|
drupal_set_message(t('Missing Feeds plugin %plugin. Please contact your site administrator.', $args), 'warning', FALSE);
|
1238
|
}
|
1239
|
|
1240
|
$class = ctools_plugin_load_class('feeds', 'plugins', 'FeedsMissingPlugin', 'handler');
|
1241
|
|
1242
|
return FeedsPlugin::instance($class, $id);
|
1243
|
}
|
1244
|
|
1245
|
/**
|
1246
|
* @}
|
1247
|
*/
|
1248
|
|
1249
|
/**
|
1250
|
* @defgroup include Funtions for loading libraries
|
1251
|
* @{
|
1252
|
*/
|
1253
|
|
1254
|
/**
|
1255
|
* Includes a library file.
|
1256
|
*
|
1257
|
* @param string $file
|
1258
|
* The filename to load from.
|
1259
|
* @param string $library
|
1260
|
* The name of the library. If libraries module is installed,
|
1261
|
* feeds_include_library() will look for libraries with this name managed by
|
1262
|
* libraries module.
|
1263
|
*/
|
1264
|
function feeds_include_library($file, $library) {
|
1265
|
static $included = array();
|
1266
|
|
1267
|
$key = $library . '/' . $file;
|
1268
|
|
1269
|
if (!isset($included[$key])) {
|
1270
|
$included[$key] = FALSE;
|
1271
|
|
1272
|
$library_dir = variable_get('feeds_library_dir', FALSE);
|
1273
|
$feeds_library_path = DRUPAL_ROOT . '/' . drupal_get_path('module', 'feeds') . "/libraries/$file";
|
1274
|
$libraries_path = module_exists('libraries') ? libraries_get_path($library) : FALSE;
|
1275
|
|
1276
|
// Try first whether libraries module is present and load the file from
|
1277
|
// there. If this fails, require the library from the local path.
|
1278
|
if ($libraries_path && is_file("$libraries_path/$file")) {
|
1279
|
require "$libraries_path/$file";
|
1280
|
$included[$key] = TRUE;
|
1281
|
}
|
1282
|
elseif (is_file(DRUPAL_ROOT . '/sites/all/libraries/' . $key)) {
|
1283
|
require DRUPAL_ROOT . '/sites/all/libraries/' . $key;
|
1284
|
$included[$key] = TRUE;
|
1285
|
}
|
1286
|
elseif ($library_dir && is_file($library_dir . '/' . $key)) {
|
1287
|
require $library_dir . '/' . $key;
|
1288
|
$included[$key] = TRUE;
|
1289
|
}
|
1290
|
elseif (is_file($feeds_library_path)) {
|
1291
|
// @todo: Throws "Deprecated function: Assigning the return value of new
|
1292
|
// by reference is deprecated."
|
1293
|
require $feeds_library_path;
|
1294
|
$included[$key] = TRUE;
|
1295
|
}
|
1296
|
}
|
1297
|
|
1298
|
return $included[$key];
|
1299
|
}
|
1300
|
|
1301
|
/**
|
1302
|
* Checks whether a library is present.
|
1303
|
*
|
1304
|
* @param string $file
|
1305
|
* The filename to load from.
|
1306
|
* @param string $library
|
1307
|
* The name of the library. If libraries module is installed,
|
1308
|
* feeds_library_exists() will look for libraries with this name managed by
|
1309
|
* libraries module.
|
1310
|
*/
|
1311
|
function feeds_library_exists($file, $library) {
|
1312
|
$path = module_exists('libraries') ? libraries_get_path($library) : FALSE;
|
1313
|
if ($path && is_file($path . '/' . $file)) {
|
1314
|
return TRUE;
|
1315
|
}
|
1316
|
elseif (is_file(DRUPAL_ROOT . "/sites/all/libraries/$library/$file")) {
|
1317
|
return TRUE;
|
1318
|
}
|
1319
|
elseif (is_file(DRUPAL_ROOT . '/' . drupal_get_path('module', 'feeds') . "/libraries/$file")) {
|
1320
|
return TRUE;
|
1321
|
}
|
1322
|
elseif ($library_dir = variable_get('feeds_library_dir', FALSE)) {
|
1323
|
if (is_file("$library_dir/$library/$file")) {
|
1324
|
return TRUE;
|
1325
|
}
|
1326
|
}
|
1327
|
|
1328
|
return FALSE;
|
1329
|
}
|
1330
|
|
1331
|
/**
|
1332
|
* Checks whether simplepie exists.
|
1333
|
*/
|
1334
|
function feeds_simplepie_exists() {
|
1335
|
return (
|
1336
|
feeds_library_exists('autoloader.php', 'simplepie') ||
|
1337
|
feeds_library_exists('simplepie.compiled.php', 'simplepie') ||
|
1338
|
feeds_library_exists('simplepie.mini.php', 'simplepie') ||
|
1339
|
feeds_library_exists('simplepie.inc', 'simplepie')
|
1340
|
);
|
1341
|
}
|
1342
|
|
1343
|
/**
|
1344
|
* Includes the simplepie library.
|
1345
|
*/
|
1346
|
function feeds_include_simplepie() {
|
1347
|
$files = array(
|
1348
|
'autoloader.php',
|
1349
|
'simplepie.mini.php',
|
1350
|
'simplepie.compiled.php',
|
1351
|
'simplepie.inc',
|
1352
|
);
|
1353
|
|
1354
|
foreach ($files as $file) {
|
1355
|
if (feeds_include_library($file, 'simplepie')) {
|
1356
|
return TRUE;
|
1357
|
}
|
1358
|
}
|
1359
|
|
1360
|
return FALSE;
|
1361
|
}
|
1362
|
|
1363
|
/**
|
1364
|
* @deprecated
|
1365
|
*
|
1366
|
* Simplified drupal_alter().
|
1367
|
*
|
1368
|
* - None of that 'multiple parameters by ref' crazyness.
|
1369
|
* - Don't use module_implements() to allow hot including on behalf
|
1370
|
* implementations (see mappers/).
|
1371
|
*
|
1372
|
* @todo This needs to be removed and drupal_alter() used. This is crazy dumb.
|
1373
|
*/
|
1374
|
function feeds_alter($type, &$data) {
|
1375
|
$args = array(&$data);
|
1376
|
$additional_args = func_get_args();
|
1377
|
array_shift($additional_args);
|
1378
|
array_shift($additional_args);
|
1379
|
$args = array_merge($args, $additional_args);
|
1380
|
|
1381
|
$hook = $type . '_alter';
|
1382
|
foreach (module_list() as $module) {
|
1383
|
if (module_hook($module, $hook)) {
|
1384
|
call_user_func_array($module . '_' . $hook, $args);
|
1385
|
}
|
1386
|
}
|
1387
|
}
|
1388
|
|
1389
|
/**
|
1390
|
* @}
|
1391
|
*/
|
1392
|
|
1393
|
/**
|
1394
|
* Copy of valid_url() that supports the webcal scheme.
|
1395
|
*
|
1396
|
* @see valid_url().
|
1397
|
*
|
1398
|
* @todo Replace with valid_url() when http://drupal.org/node/295021 is fixed.
|
1399
|
*/
|
1400
|
function feeds_valid_url($url, $absolute = FALSE) {
|
1401
|
if ($absolute) {
|
1402
|
return (bool) preg_match("
|
1403
|
/^ # Start at the beginning of the text
|
1404
|
(?:ftp|https?|feed|webcal):\/\/ # Look for ftp, http, https, feed or webcal schemes
|
1405
|
(?: # Userinfo (optional) which is typically
|
1406
|
(?:(?:[\w\.\-\+!$&'\(\)*\+,;=]|%[0-9a-f]{2})+:)* # a username or a username and password
|
1407
|
(?:[\w\.\-\+%!$&'\(\)*\+,;=]|%[0-9a-f]{2})+@ # combination
|
1408
|
)?
|
1409
|
(?:
|
1410
|
(?:[a-z0-9\-\.]|%[0-9a-f]{2})+ # A domain name or a IPv4 address
|
1411
|
|(?:\[(?:[0-9a-f]{0,4}:)*(?:[0-9a-f]{0,4})\]) # or a well formed IPv6 address
|
1412
|
)
|
1413
|
(?::[0-9]+)? # Server port number (optional)
|
1414
|
(?:[\/|\?]
|
1415
|
(?:[|\w#!:\.\?\+=&@$'~*,;\/\(\)\[\]\-]|%[0-9a-f]{2}) # The path and query (optional)
|
1416
|
*)?
|
1417
|
$/xi", $url);
|
1418
|
}
|
1419
|
else {
|
1420
|
return (bool) preg_match("/^(?:[\w#!:\.\?\+=&@$'~*,;\/\(\)\[\]\-]|%[0-9a-f]{2})+$/i", $url);
|
1421
|
}
|
1422
|
}
|
1423
|
|
1424
|
/**
|
1425
|
* Registers a feed subscription job for execution on feeds_exit().
|
1426
|
*
|
1427
|
* @param array $job
|
1428
|
* Information about a new job to queue; or if set to NULL (default), leaves
|
1429
|
* the current queued jobs unchanged.
|
1430
|
*
|
1431
|
* @return
|
1432
|
* An array of subscribe jobs to process.
|
1433
|
*
|
1434
|
* @see feeds_exit()
|
1435
|
* @see feeds_get_subscription_jobs()
|
1436
|
*/
|
1437
|
function feeds_set_subscription_job(array $job = NULL) {
|
1438
|
$jobs = &drupal_static(__FUNCTION__, array());
|
1439
|
if (isset($job)) {
|
1440
|
$jobs[] = $job;
|
1441
|
}
|
1442
|
return $jobs;
|
1443
|
}
|
1444
|
|
1445
|
/**
|
1446
|
* Returns the list of queued jobs to be run.
|
1447
|
*
|
1448
|
* @return
|
1449
|
* An array of subscribe jobs to process.
|
1450
|
*
|
1451
|
* @see feeds_set_subscription_job()
|
1452
|
*/
|
1453
|
function feeds_get_subscription_jobs() {
|
1454
|
return feeds_set_subscription_job();
|
1455
|
}
|
1456
|
|
1457
|
/**
|
1458
|
* Implements hook_entity_property_info_alter().
|
1459
|
*/
|
1460
|
function feeds_entity_property_info_alter(&$info) {
|
1461
|
|
1462
|
foreach ($info as $entity_type => $entity_info) {
|
1463
|
$info[$entity_type]['properties']['feeds_item_guid'] = array(
|
1464
|
'label' => 'Feeds Item GUID',
|
1465
|
'type' => 'text',
|
1466
|
'description' => t('Feeds Item GUID.'),
|
1467
|
'getter callback' => 'feeds_get_feeds_item_property',
|
1468
|
);
|
1469
|
$info[$entity_type]['properties']['feeds_item_url'] = array(
|
1470
|
'label' => 'Feeds Item URL',
|
1471
|
'type' => 'text',
|
1472
|
'description' => t('Feeds Item URL.'),
|
1473
|
'getter callback' => 'feeds_get_feeds_item_property',
|
1474
|
);
|
1475
|
$info[$entity_type]['properties']['feed_nid'] = array(
|
1476
|
'label' => 'Feed NID',
|
1477
|
'type' => 'integer',
|
1478
|
'description' => t('Nid of the Feed Node that imported this entity.'),
|
1479
|
'getter callback' => 'feeds_get_feeds_item_property',
|
1480
|
);
|
1481
|
$info[$entity_type]['properties']['feed_node'] = array(
|
1482
|
'label' => 'Feed node',
|
1483
|
'type' => 'node',
|
1484
|
'description' => t('Feed Node that imported this entity.'),
|
1485
|
'getter callback' => 'feeds_get_feeds_item_property',
|
1486
|
);
|
1487
|
}
|
1488
|
}
|
1489
|
|
1490
|
/**
|
1491
|
* Entity API getter callback for getting a feeds_item property on an entity.
|
1492
|
*
|
1493
|
* @param object $entity
|
1494
|
* An entity object.
|
1495
|
* @param array $options
|
1496
|
* Options are ignored, but it is a param that is given by the Entiy API.
|
1497
|
* @param string $name
|
1498
|
* The name of the property to get.
|
1499
|
* @param string $entity_type
|
1500
|
* The entity's type.
|
1501
|
*
|
1502
|
* @return mixed
|
1503
|
* The feeds_item property.
|
1504
|
*/
|
1505
|
function feeds_get_feeds_item_property($entity, array $options, $name, $entity_type) {
|
1506
|
// Map property name to actual feeds_item column.
|
1507
|
$property_map = array(
|
1508
|
'feed_node' => 'feed_nid',
|
1509
|
'feed_nid' => 'feed_nid',
|
1510
|
'feeds_item_guid' => 'guid',
|
1511
|
'feeds_item_url' => 'url',
|
1512
|
);
|
1513
|
$property_name = $property_map[$name];
|
1514
|
|
1515
|
// First check if the entity has already the feeds item loaded with the
|
1516
|
// requested property and return its value if it does.
|
1517
|
if (isset($entity->feeds_item->$property_name)) {
|
1518
|
return $entity->feeds_item->$property_name;
|
1519
|
}
|
1520
|
|
1521
|
// No feed item. Try to load the feed item if the entity has an ID.
|
1522
|
list($entity_id, , ) = entity_extract_ids($entity_type, $entity);
|
1523
|
if ($entity_id) {
|
1524
|
$feeds_item = feeds_item_info_load($entity_type, $entity_id);
|
1525
|
|
1526
|
// Check again for the requested property and return its value if it exists.
|
1527
|
if (isset($feeds_item->$property_name)) {
|
1528
|
return $feeds_item->$property_name;
|
1529
|
}
|
1530
|
}
|
1531
|
}
|
1532
|
|
1533
|
/**
|
1534
|
* Implements hook_file_download().
|
1535
|
*/
|
1536
|
function feeds_file_download($uri) {
|
1537
|
$id = db_query("SELECT id FROM {feeds_source} WHERE source = :uri", array(':uri' => $uri))->fetchField();
|
1538
|
|
1539
|
if (!$id) {
|
1540
|
// File is not associated with a feed.
|
1541
|
return;
|
1542
|
}
|
1543
|
|
1544
|
// Get the file record based on the URI. If not in the database just return.
|
1545
|
$files = file_load_multiple(array(), array('uri' => $uri));
|
1546
|
foreach ($files as $item) {
|
1547
|
// Since some database servers sometimes use a case-insensitive comparison
|
1548
|
// by default, double check that the filename is an exact match.
|
1549
|
if ($item->uri === $uri) {
|
1550
|
$file = $item;
|
1551
|
break;
|
1552
|
}
|
1553
|
}
|
1554
|
if (!isset($file)) {
|
1555
|
return;
|
1556
|
}
|
1557
|
|
1558
|
// Check if this file belongs to Feeds.
|
1559
|
$usage_list = file_usage_list($file);
|
1560
|
if (!isset($usage_list['feeds'])) {
|
1561
|
return;
|
1562
|
}
|
1563
|
|
1564
|
if (!feeds_access('import', $id)) {
|
1565
|
// User does not have permission to import this feed.
|
1566
|
return -1;
|
1567
|
}
|
1568
|
|
1569
|
// Return file headers.
|
1570
|
return file_get_content_headers($file);
|
1571
|
}
|
1572
|
|
1573
|
/**
|
1574
|
* Feeds API version.
|
1575
|
*/
|
1576
|
function feeds_api_version() {
|
1577
|
$version = feeds_ctools_plugin_api('feeds', 'plugins');
|
1578
|
return $version['version'];
|
1579
|
}
|