Projet

Général

Profil

Paste
Télécharger (41,2 ko) Statistiques
| Branche: | Révision:

root / drupal7 / sites / all / modules / feeds / feeds.module @ 651307cd

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
  );
42

    
43
  return array_fill_keys($hooks, array('group' => 'feeds'));
44
}
45

    
46
/**
47
 * Implements hook_cron().
48
 */
49
function feeds_cron() {
50
  // Expire old log entries.
51
  db_delete('feeds_log')
52
    ->condition('request_time', REQUEST_TIME - 604800, '<')
53
    ->execute();
54

    
55
  // Find importers that need to be rescheduled.
56
  if (!$importers = feeds_reschedule()) {
57
    return;
58
  }
59

    
60
  // @todo Maybe we should queue this somehow as well. This could be potentially
61
  // very long.
62
  $sources = db_query("SELECT feed_nid, id FROM {feeds_source} WHERE id IN (:ids)", array(':ids' => $importers));
63

    
64
  foreach ($sources as $source) {
65
    feeds_source($source->id, $source->feed_nid)->schedule();
66
  }
67

    
68
  feeds_reschedule(FALSE);
69
}
70

    
71
/**
72
 * Implements hook_cron_job_scheduler_info().
73
 *
74
 * Compare queue names with key names in feeds_cron_queue_info().
75
 */
76
function feeds_cron_job_scheduler_info() {
77
  $info = array();
78
  $info['feeds_source_import'] = array(
79
    'queue name' => 'feeds_source_import',
80
  );
81
  $info['feeds_source_clear'] = array(
82
    'queue name' => 'feeds_source_clear',
83
  );
84
  $info['feeds_source_expire'] = array(
85
    'queue name' => 'feeds_source_expire',
86
  );
87
  $info['feeds_push_unsubscribe'] = array(
88
    'queue name' => 'feeds_push_unsubscribe',
89
  );
90
  return $info;
91
}
92

    
93
/**
94
 * Implements hook_cron_queue_info().
95
 */
96
function feeds_cron_queue_info() {
97
  $queues = array();
98
  $queues['feeds_source_import'] = array(
99
    'worker callback' => 'feeds_source_import',
100
    'time' => 60,
101
  );
102
  $queues['feeds_source_clear'] = array(
103
    'worker callback' => 'feeds_source_clear',
104
  );
105
  $queues['feeds_source_expire'] = array(
106
    'worker callback' => 'feeds_source_expire',
107
  );
108
  $queues['feeds_push_unsubscribe'] = array(
109
    'worker callback' => 'feeds_push_unsubscribe',
110
  );
111

    
112
  return $queues;
113
}
114

    
115
/**
116
 * Scheduler callback for importing from a source.
117
 */
118
function feeds_source_import(array $job) {
119
  $source = _feeds_queue_worker_helper($job, 'import');
120
  $source->scheduleImport();
121
}
122

    
123
/**
124
 * Scheduler callback for deleting all items from a source.
125
 */
126
function feeds_source_clear(array $job) {
127
  $source = _feeds_queue_worker_helper($job, 'clear');
128
  $source->scheduleClear();
129
}
130

    
131
/**
132
 * Scheduler callback for expiring content.
133
 */
134
function feeds_source_expire(array $job) {
135
  $source = _feeds_queue_worker_helper($job, 'expire');
136
  $source->scheduleExpire();
137
}
138

    
139
/**
140
 * Executes a method on a feed source.
141
 *
142
 * @param array $job
143
 *   The job being run.
144
 * @param string $method
145
 *   The method to execute.
146
 */
147
function _feeds_queue_worker_helper(array $job, $method) {
148
  $source = feeds_source($job['type'], $job['id']);
149
  try {
150
    $source->existing()->$method();
151
  }
152
  catch (FeedsNotExistingException $e) {
153
    // Do nothing.
154
  }
155
  catch (Exception $e) {
156
    $source->log($method, $e->getMessage(), array(), WATCHDOG_ERROR);
157
  }
158

    
159
  return $source;
160
}
161

    
162
/**
163
 * Scheduler callback for unsubscribing from PuSH hubs.
164
 */
165
function feeds_push_unsubscribe($job) {
166
  $source = feeds_source($job['type'], $job['id']);
167
  $fetcher = feeds_plugin('FeedsHTTPFetcher', $source->importer->id);
168
  $fetcher->unsubscribe($source);
169
}
170

    
171
/**
172
 * Batch API worker callback. Used by FeedsSource::startBatchAPIJob().
173
 *
174
 * @see FeedsSource::startBatchAPIJob().
175
 *
176
 * @todo Harmonize Job Scheduler API callbacks with Batch API callbacks?
177
 *
178
 * @param $method
179
 *   Method to execute on importer; one of 'import' or 'clear'.
180
 * @param $importer_id
181
 *   Identifier of a FeedsImporter object.
182
 * @param $feed_nid
183
 *   If importer is attached to content type, feed node id identifying the
184
 *   source to be imported.
185
 * @param $context
186
 *   Batch context.
187
 */
188
function feeds_batch($method, $importer_id, $feed_nid = 0, &$context) {
189
  $context['finished'] = FEEDS_BATCH_COMPLETE;
190
  try {
191
    $context['finished'] = feeds_source($importer_id, $feed_nid)->$method();
192
  }
193
  catch (Exception $e) {
194
    drupal_set_message($e->getMessage(), 'error');
195
  }
196
}
197

    
198
/**
199
 * Reschedule one or all importers.
200
 *
201
 * @param string $importer_id
202
 *   If TRUE, all importers will be rescheduled, if FALSE, no importers will
203
 *   be rescheduled, if an importer id, only importer of that id will be
204
 *   rescheduled.
205
 *
206
 * @return array
207
 *   An list of importers that need rescheduling.
208
 */
209
function feeds_reschedule($importer_id = NULL) {
210
  $reschedule = variable_get('feeds_reschedule', FALSE);
211

    
212
  if ($importer_id === TRUE || $importer_id === FALSE) {
213
    $reschedule = $importer_id;
214
  }
215
  elseif (is_string($importer_id) && $reschedule !== TRUE) {
216
    $reschedule = array_filter((array) $reschedule);
217
    $reschedule[$importer_id] = $importer_id;
218
  }
219

    
220
  if (isset($importer_id)) {
221
    variable_set('feeds_reschedule', $reschedule);
222
  }
223

    
224
  if ($reschedule === TRUE) {
225
    return feeds_enabled_importers();
226
  }
227
  elseif ($reschedule === FALSE) {
228
    return array();
229
  }
230

    
231
  return $reschedule;
232
}
233

    
234
/**
235
 * Implements feeds_permission().
236
 */
237
function feeds_permission() {
238
  $perms = array(
239
    'administer feeds' => array(
240
      'title' => t('Administer Feeds'),
241
      'description' => t('Create, update, delete importers, execute import and delete tasks on any importer.')
242
    ),
243
  );
244
  foreach (feeds_importer_load_all() as $importer) {
245
    $perms["import $importer->id feeds"] = array(
246
      'title' => t('Import @name feeds', array('@name' => $importer->config['name'])),
247
    );
248
    $perms["clear $importer->id feeds"] = array(
249
      'title' => t('Delete items from @name feeds', array('@name' => $importer->config['name'])),
250
    );
251
    $perms["unlock $importer->id feeds"] = array(
252
      'title' => t('Unlock imports from @name feeds', array('@name' => $importer->config['name'])),
253
      'description' => t('If a feed importation breaks for some reason, users with this permission can unlock them.')
254
    );
255
  }
256
  return $perms;
257
}
258

    
259
/**
260
 * Implements hook_forms().
261
 *
262
 * Declare form callbacks for all known classes derived from FeedsConfigurable.
263
 */
264
function feeds_forms() {
265
  $forms = array();
266
  $forms['FeedsImporter_feeds_form']['callback'] = 'feeds_form';
267
  $plugins = FeedsPlugin::all();
268
  foreach ($plugins as $plugin) {
269
    $forms[$plugin['handler']['class'] . '_feeds_form']['callback'] = 'feeds_form';
270
  }
271
  return $forms;
272
}
273

    
274
/**
275
 * Implements hook_menu().
276
 */
277
function feeds_menu() {
278
  $items = array();
279
  $items['import'] = array(
280
    'title' => 'Import',
281
    'page callback' => 'feeds_page',
282
    'access callback' => 'feeds_page_access',
283
    'file' => 'feeds.pages.inc',
284
  );
285
  $items['import/%feeds_importer'] = array(
286
    'title callback' => 'feeds_importer_title',
287
    'title arguments' => array(1),
288
    'page callback' => 'drupal_get_form',
289
    'page arguments' => array('feeds_import_form', 1),
290
    'access callback' => 'feeds_access',
291
    'access arguments' => array('import', 1),
292
    'file' => 'feeds.pages.inc',
293
  );
294
  $items['import/%feeds_importer/import'] = array(
295
    'title' => 'Import',
296
    'type' => MENU_DEFAULT_LOCAL_TASK,
297
    'weight' => -10,
298
  );
299
  $items['import/%feeds_importer/delete-items'] = array(
300
    'title' => 'Delete items',
301
    'page callback' => 'drupal_get_form',
302
    'page arguments' => array('feeds_delete_tab_form', 1),
303
    'access callback' => 'feeds_access',
304
    'access arguments' => array('clear', 1),
305
    'file' => 'feeds.pages.inc',
306
    'type' => MENU_LOCAL_TASK,
307
  );
308
  $items['import/%feeds_importer/unlock'] = array(
309
    'title' => 'Unlock',
310
    'page callback' => 'drupal_get_form',
311
    'page arguments' => array('feeds_unlock_tab_form', 1),
312
    'access callback' => 'feeds_access',
313
    'access arguments' => array('unlock', 1),
314
    'file' => 'feeds.pages.inc',
315
    'type' => MENU_LOCAL_TASK,
316
  );
317
  $items['import/%feeds_importer/template'] = array(
318
    'page callback' => 'feeds_importer_template',
319
    'page arguments' => array(1),
320
    'access callback' => 'feeds_access',
321
    'access arguments' => array('import', 1),
322
    'file' => 'feeds.pages.inc',
323
    'type' => MENU_CALLBACK,
324
  );
325
  $items['node/%node/import'] = array(
326
    'title' => 'Import',
327
    'page callback' => 'drupal_get_form',
328
    'page arguments' => array('feeds_import_tab_form', 1),
329
    'access callback' => 'feeds_access',
330
    'access arguments' => array('import', 1),
331
    'file' => 'feeds.pages.inc',
332
    'type' => MENU_LOCAL_TASK,
333
    'weight' => 10,
334
  );
335
  $items['node/%node/delete-items'] = array(
336
    'title' => 'Delete items',
337
    'page callback' => 'drupal_get_form',
338
    'page arguments' => array('feeds_delete_tab_form', NULL, 1),
339
    'access callback' => 'feeds_access',
340
    'access arguments' => array('clear', 1),
341
    'file' => 'feeds.pages.inc',
342
    'type' => MENU_LOCAL_TASK,
343
    'weight' => 11,
344
  );
345
  $items['node/%node/unlock'] = array(
346
    'title' => 'Unlock',
347
    'page callback' => 'drupal_get_form',
348
    'page arguments' => array('feeds_unlock_tab_form', NULL, 1),
349
    'access callback' => 'feeds_access',
350
    'access arguments' => array('unlock', 1),
351
    'file' => 'feeds.pages.inc',
352
    'type' => MENU_LOCAL_TASK,
353
    'weight' => 11,
354
  );
355
  // @todo Eliminate this step and thus eliminate clearing menu cache when
356
  // manipulating importers.
357
  foreach (feeds_importer_load_all() as $importer) {
358
    $items += $importer->fetcher->menuItem();
359
  }
360
  return $items;
361
}
362

    
363
/**
364
 * Implements hook_admin_paths().
365
 */
366
function feeds_admin_paths() {
367
  $paths = array(
368
    'import' => TRUE,
369
    'import/*' => TRUE,
370
    'node/*/import' => TRUE,
371
    'node/*/delete-items' => TRUE,
372
    'node/*/log' => TRUE,
373
  );
374
  return $paths;
375
}
376

    
377
/**
378
 * Menu loader callback.
379
 */
380
function feeds_importer_load($id) {
381
  try {
382
    return feeds_importer($id)->existing();
383
  }
384
  catch (FeedsNotExistingException $e) {}
385
  catch (InvalidArgumentException $e) {}
386

    
387
  return FALSE;
388
}
389

    
390
/**
391
 * Title callback.
392
 */
393
function feeds_importer_title(FeedsImporter $importer) {
394
  return $importer->config['name'];
395
}
396

    
397
/**
398
 * Implements hook_theme().
399
 */
400
function feeds_theme() {
401
  return array(
402
    'feeds_upload' => array(
403
      'file' => 'feeds.pages.inc',
404
      'render element' => 'element',
405
    ),
406
    'feeds_source_status' => array(
407
      'file' => 'feeds.pages.inc',
408
      'variables' => array(
409
        'progress_importing' => NULL,
410
        'progress_clearing' => NULL,
411
        'imported' => NULL,
412
        'count' => NULL,
413
      ),
414
    ),
415
  );
416
}
417

    
418
/**
419
 * Menu access callback.
420
 *
421
 * @param $action
422
 *   The action to be performed. Possible values are:
423
 *   - import
424
 *   - clear
425
 *   - unlock
426
 * @param $param
427
 *   Node object or FeedsImporter id.
428
 */
429
function feeds_access($action, $param) {
430
  if (!in_array($action, array('import', 'clear', 'unlock'))) {
431
    // If $action is not one of the supported actions, we return access denied.
432
    return FALSE;
433
  }
434

    
435
  $importer_id = FALSE;
436
  if (is_string($param)) {
437
    $importer_id = $param;
438
  }
439
  elseif ($param instanceof FeedsImporter) {
440
    $importer_id = $param->id;
441
  }
442
  elseif ($param->type) {
443
    $importer_id = feeds_get_importer_id($param->type);
444
  }
445

    
446
  // Check for permissions if feed id is present, otherwise return FALSE.
447
  if ($importer_id) {
448
    if (user_access('administer feeds') || user_access("{$action} {$importer_id} feeds")) {
449
      return TRUE;
450
    }
451
  }
452
  return FALSE;
453
}
454

    
455
/**
456
 * Access callback to determine if the user can import Feeds importers.
457
 *
458
 * Feeds imports require an additional access check because they are PHP
459
 * code and PHP is more locked down than administer feeds.
460
 */
461
function feeds_importer_import_access() {
462
  return user_access('administer feeds') && user_access('use PHP for settings');
463
}
464

    
465
/**
466
 * Menu access callback.
467
 */
468
function feeds_page_access() {
469
  if (user_access('administer feeds')) {
470
    return TRUE;
471
  }
472
  foreach (feeds_enabled_importers() as $id) {
473
    if (user_access("import $id feeds")) {
474
      return TRUE;
475
    }
476
  }
477
  return FALSE;
478
}
479

    
480
/**
481
 * Implements hook_exit().
482
 */
483
function feeds_exit() {
484
  // Process any pending PuSH subscriptions.
485
  $jobs = feeds_get_subscription_jobs();
486
  foreach ($jobs as $job) {
487
    if (!isset($job['fetcher']) || !isset($job['source'])) {
488
      continue;
489
     }
490
    $job['fetcher']->subscribe($job['source']);
491
  }
492

    
493
  if (drupal_static('feeds_log_error', FALSE)) {
494
    watchdog('feeds', 'Feeds reported errors, visit the Feeds log for details.', array(), WATCHDOG_ERROR, 'admin/reports/dblog/feeds');
495
  }
496
}
497

    
498
/**
499
 * Implements hook_views_api().
500
 */
501
function feeds_views_api() {
502
  return array(
503
    'api' => 3,
504
    'path' => drupal_get_path('module', 'feeds') . '/views',
505
  );
506
}
507

    
508
/**
509
 * Implements hook_ctools_plugin_api().
510
 */
511
function feeds_ctools_plugin_api($owner, $api) {
512
  if ($owner == 'feeds' && $api == 'plugins') {
513
    return array('version' => 1);
514
  }
515
}
516

    
517
/**
518
 * Implements hook_ctools_plugin_type().
519
 */
520
function feeds_ctools_plugin_type() {
521
  return array(
522
    'plugins' => array(
523
      'cache' => TRUE,
524
      'use hooks' => TRUE,
525
      'classes' => array('handler'),
526
    ),
527
  );
528
}
529

    
530
/**
531
 * Implements hook_feeds_plugins().
532
 */
533
function feeds_feeds_plugins() {
534
  module_load_include('inc', 'feeds', 'feeds.plugins');
535
  return _feeds_feeds_plugins();
536
}
537

    
538
/**
539
 * Gets the feed_nid for a single entity.
540
 *
541
 * @param int $entity_id
542
 *   The entity id.
543
 * @param string $entity_type
544
 *   The type of entity.
545
 *
546
 * @return int|bool
547
 *   The feed_nid of the entity, or FALSE if the entity doesn't belong to a
548
 *   feed.
549
 */
550
function feeds_get_feed_nid($entity_id, $entity_type) {
551
  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();
552
}
553

    
554
/**
555
 * Implements hook_entity_insert().
556
 */
557
function feeds_entity_insert($entity, $type) {
558
  list($id) = entity_extract_ids($type, $entity);
559
  feeds_item_info_insert($entity, $id);
560
}
561

    
562
/**
563
 * Implements hook_entity_update().
564
 */
565
function feeds_entity_update($entity, $type) {
566
  list($id) = entity_extract_ids($type, $entity);
567
  feeds_item_info_save($entity, $id);
568
}
569

    
570
/**
571
 * Implements hook_entity_delete().
572
 */
573
function feeds_entity_delete($entity, $type) {
574
  list($id) = entity_extract_ids($type, $entity);
575

    
576
  // Delete any imported items produced by the source.
577
  db_delete('feeds_item')
578
    ->condition('entity_type', $type)
579
    ->condition('entity_id', $id)
580
    ->execute();
581
}
582

    
583
/**
584
 * Implements hook_node_validate().
585
 */
586
function feeds_node_validate($node, $form, &$form_state) {
587
  if (!$importer_id = feeds_get_importer_id($node->type)) {
588
    return;
589
  }
590
  // Keep a copy of the title for subsequent node creation stages.
591
  // @todo: revisit whether $node still looses all of its properties
592
  // between validate and insert stage.
593
  $last_title = &drupal_static('feeds_node_last_title');
594
  $last_feeds = &drupal_static('feeds_node_last_feeds');
595

    
596
  // On validation stage we are working with a FeedsSource object that is
597
  // not tied to a nid - when creating a new node there is no
598
  // $node->nid at this stage.
599
  $source = feeds_source($importer_id);
600

    
601
  // Node module magically moved $form['feeds'] to $node->feeds :P.
602
  // configFormValidate may modify $last_feed, smuggle it to update/insert stage
603
  // through a static variable.
604
  $last_feeds = $node->feeds;
605
  $source->configFormValidate($last_feeds);
606

    
607
  // Check if title form element is hidden.
608
  $title_hidden = (isset($form['title']['#access']) && !$form['title']['#access']);
609

    
610
  // If the node title is empty and the title form element wasn't hidden, try to
611
  // retrieve the title from the feed.
612
  if (isset($node->title) && trim($node->title) == '' && !$title_hidden) {
613
    try {
614
      $source->addConfig($last_feeds);
615
      if (!$last_title = $source->preview()->title) {
616
        throw new Exception();
617
      }
618
    }
619
    catch (Exception $e) {
620
      drupal_set_message($e->getMessage(), 'error');
621
      form_set_error('title', t('Could not retrieve title from feed.'));
622
    }
623
  }
624
}
625

    
626
/**
627
 * Implements hook_node_presave().
628
 */
629
function feeds_node_presave($node) {
630
  // Populate $node->title and $node->feed from result of validation phase.
631
  $last_title = &drupal_static('feeds_node_last_title');
632
  $last_feeds = &drupal_static('feeds_node_last_feeds');
633
  if (empty($node->title) && !empty($last_title)) {
634
    $node->title = $last_title;
635
  }
636
  if (!empty($last_feeds)) {
637
    $node->feeds = $last_feeds;
638
  }
639
  $last_title = NULL;
640
  $last_feeds = NULL;
641

    
642
  // Update "changed" value if there was mapped to that.
643
  if (isset($node->feeds_item->node_changed)) {
644
    $node->changed = $node->feeds_item->node_changed;
645
  }
646
}
647

    
648
/**
649
 * Implements hook_node_insert().
650
 */
651
function feeds_node_insert($node) {
652
  // Source attached to node.
653
  feeds_node_update($node);
654
  if (isset($node->feeds) && $importer_id = feeds_get_importer_id($node->type)) {
655
    $source = feeds_source($importer_id, $node->nid);
656
    // Start import if requested.
657
    if (feeds_importer($importer_id)->config['import_on_create'] && !isset($node->feeds['suppress_import'])) {
658
      $source->startImport();
659
    }
660
    // Schedule the source.
661
    $source->schedule();
662
  }
663
}
664

    
665
/**
666
 * Implements hook_node_update().
667
 */
668
function feeds_node_update($node) {
669
  // Source attached to node.
670
  if (isset($node->feeds) && $importer_id = feeds_get_importer_id($node->type)) {
671
    $source = feeds_source($importer_id, $node->nid);
672
    $source->addConfig($node->feeds);
673
    $source->save();
674
  }
675
}
676

    
677
/**
678
 * Implements hook_node_delete().
679
 */
680
function feeds_node_delete($node) {
681
  // Source attached to node.
682
  // Make sure we don't leave any orphans behind: Do not use
683
  // feeds_get_importer_id() to determine importer id as the importer may have
684
  // been deleted.
685
  if ($importer_id = db_query("SELECT id FROM {feeds_source} WHERE feed_nid = :nid", array(':nid' => $node->nid))->fetchField()) {
686
    feeds_source($importer_id, $node->nid)->delete();
687
  }
688
}
689

    
690
/**
691
 * Implements hook_form_BASE_FORM_ID_alter().
692
 */
693
function feeds_form_node_form_alter(&$form, $form_state) {
694
  if ($importer_id = feeds_get_importer_id($form['#node']->type)) {
695
    // Enable uploads.
696
    $form['#attributes']['enctype'] = 'multipart/form-data';
697

    
698
    // Build form.
699
    $source = feeds_source($importer_id, empty($form['#node']->nid) ? 0 : $form['#node']->nid);
700
    $form['feeds'] = array(
701
      '#type' => 'fieldset',
702
      '#title' => t('Feed'),
703
      '#tree' => TRUE,
704
      '#weight' => 0,
705
    );
706
    $form['feeds'] += $source->configForm($form_state);
707
    $form['#feed_id'] = $importer_id;
708

    
709
    // If the parser has support for delivering a source title, set node title
710
    // to not required and try to retrieve it from the source if the node title
711
    // is left empty by the user.
712
    // @see feeds_node_validate()
713
    if (isset($form['title']) && $source->importer()->parser->providesSourceTitle()) {
714
      $form['title']['#required'] = FALSE;
715
    }
716
  }
717
}
718

    
719
/**
720
 * Implements hook_field_extra_fields().
721
 */
722
function feeds_field_extra_fields() {
723
  $extras = array();
724
  foreach (node_type_get_names() as $type => $name) {
725
    if (feeds_get_importer_id($type)) {
726
      $extras['node'][$type]['form']['feeds'] = array(
727
        'label' => t('Feed'),
728
        'description' => t('Feeds module form elements'),
729
        'weight' => 0,
730
      );
731
    }
732
  }
733
  return $extras;
734
}
735

    
736
/**
737
 * Implements hook_features_pipe_COMPONENT_alter() for component "feeds_importer".
738
 *
739
 * Automatically adds dependencies when a Feed importer is selected in Features.
740
 */
741
function feeds_features_pipe_feeds_importer_alter(&$pipe, $data, &$export) {
742
  foreach ($data as $importer_id) {
743
    if ($importer = feeds_importer_load($importer_id)) {
744
      $export['dependencies'] = array_merge($export['dependencies'], $importer->dependencies());
745
    }
746
  }
747
}
748

    
749
/**
750
 * Implements hook_system_info_alter().
751
 *
752
 * Goes through a list of all modules that provide Feeds plugins and makes them
753
 * required if there are any importers using those plugins.
754
 */
755
function feeds_system_info_alter(array &$info, $file, $type) {
756
  if ($type !== 'module' || !module_hook($file->name, 'feeds_plugins')) {
757
    return;
758
  }
759

    
760
  // Don't make Feeds require itself, otherwise you can't disable Feeds until
761
  // all importers are deleted.
762
  if ($file->name === 'feeds' || !function_exists('ctools_include')) {
763
    return;
764
  }
765

    
766
  // Get the plugins that belong to the current module.
767
  ctools_include('plugins');
768
  $module_plugins = array();
769
  foreach (ctools_get_plugins('feeds', 'plugins') as $plugin_id => $plugin) {
770
    if ($file->name === $plugin['module']) {
771
      $module_plugins[$plugin_id] = TRUE;
772
    }
773
  }
774

    
775
  // Check if any importers are using any plugins from the current module.
776
  foreach (feeds_importer_load_all(TRUE) as $importer) {
777

    
778
    // Skip importers that are defined in code and are provided by the current
779
    // module. This ensures that modules that define both an importer and a
780
    // plugin can still be disabled.
781
    if ($importer->export_type == EXPORT_IN_CODE) {
782
      $configs = ctools_export_load_object('feeds_importer', 'names', array($importer->id));
783
      if (isset($configs[$importer->id]) && $configs[$importer->id]->export_module === $file->name) {
784
        continue;
785
      }
786
    }
787

    
788
    $configuration = $importer->getConfig();
789

    
790
    foreach (array('fetcher', 'parser', 'processor') as $plugin_type) {
791
      $plugin_key = $configuration[$plugin_type]['plugin_key'];
792
      if (isset($module_plugins[$plugin_key])) {
793
        $info['required'] = TRUE;
794
        break 2;
795
      }
796
    }
797
  }
798

    
799
  if (empty($info['required'])) {
800
    return;
801
  }
802

    
803
  if (module_exists('feeds_ui') && user_access('administer feeds')) {
804
    $info['explanation'] = t('Feeds is currently using this module for one or more <a href="@link">importers</a>', array('@link' => url('admin/structure/feeds')));
805
  }
806
  else {
807
    $info['explanation'] = t('Feeds is currently using this module for one or more importers');
808
  }
809
}
810

    
811
/**
812
 * Implements hook_module_implements_alter().
813
 */
814
function feeds_module_implements_alter(array &$implementations, $hook) {
815
  if ($hook === 'feeds_processor_targets_alter') {
816
    // We need two implementations of this hook, so we add one that gets
817
    // called first, and move the normal one to last.
818
    $implementations = array('_feeds' => FALSE) + $implementations;
819

    
820
    // Move normal implementation to last.
821
    $group = $implementations['feeds'];
822
    unset($implementations['feeds']);
823
    $implementations['feeds'] = $group;
824
  }
825
}
826

    
827
/**
828
 * Implements hook_feeds_processor_targets_alter().
829
 *
830
 * @see feeds_feeds_processor_targets()
831
 * @see feeds_feeds_processor_targets_alter()
832
 */
833
function _feeds_feeds_processor_targets_alter(array &$targets, $entity_type, $bundle) {
834
  // If hook_feeds_processor_targets() hasn't been called, for instance, by
835
  // older processors, invoke it ourself.
836
  if (!drupal_static('feeds_feeds_processor_targets', FALSE)) {
837
    $targets += module_invoke_all('feeds_processor_targets', $entity_type, $bundle);
838
  }
839
}
840

    
841
/**
842
 * Implements hook_flush_caches().
843
 */
844
function feeds_flush_caches() {
845
  // The update to add the table needs to have run. Taken from
846
  // https://www.drupal.org/node/2511858
847
  include_once DRUPAL_ROOT . '/includes/install.inc';
848

    
849
  if (drupal_get_installed_schema_version('feeds') >= 7212) {
850
    return array('cache_feeds_http');
851
  }
852
  return array();
853
}
854

    
855
/**
856
 * Implements hook_admin_menu_output_build().
857
 *
858
 * Shows available importers in the "Content" section of the admin menu.
859
 * Requires the "admin_menu" module to be enabled.
860
 */
861
function feeds_admin_menu_output_build(array &$content) {
862
  // Add new top-level item to the menu.
863
  if (!isset($content['menu'])) {
864
    return;
865
  }
866
  $access = FALSE;
867
  foreach (feeds_enabled_importers() as $importer_id) {
868
    if (user_access('administer feeds') || user_access("import $importer_id feeds")) {
869
      $access = TRUE;
870
      break;
871
    }
872
  }
873

    
874
  if (!$access) {
875
    return;
876
  }
877

    
878
  $content['menu']['admin/content']['admin/content/feeds_import'] = array(
879
    '#title' => t('Import'),
880
    '#href' => 'import',
881
  );
882

    
883
  foreach (feeds_importer_load_all() as $importer) {
884
    $content['menu']['admin/content']['admin/content/feeds_import'][$importer->id] = array(
885
      '#title' => t($importer->config['name']),
886
      '#href' => !empty($importer->config['content_type']) ? 'node/add/' . $importer->config['content_type'] : 'import/' . check_plain($importer->id),
887
      '#access' => user_access('administer feeds') || user_access("import $importer->id feeds"),
888
    );
889
  }
890
}
891

    
892
/**
893
 * Implements hook_menu_local_tasks_alter().
894
 *
895
 * Adds "Import" link as local action on content overview page.
896
 */
897
function feeds_menu_local_tasks_alter(&$data, $router_item, $root_path) {
898
  if ($root_path == 'admin/content') {
899
    $data['actions']['output'][] = array(
900
      '#theme' => 'menu_local_task',
901
      '#link' => array(
902
        'title' => t('Import'),
903
        'href' => 'import',
904
        'localized_options' => array(
905
          'attributes' => array(
906
            'title' => t('Import'),
907
          ),
908
        ),
909
      ),
910
      '#access' => feeds_page_access(),
911
      // Add weight so it appears after the local action "Add content".
912
      '#weight' => 1,
913
    );
914
  }
915
}
916

    
917

    
918
/**
919
 * @}
920
 */
921

    
922
/**
923
 * @defgroup utility Utility functions
924
 * @{
925
 */
926

    
927
/**
928
 * Loads all importers.
929
 *
930
 * @param $load_disabled
931
 *   Pass TRUE to load all importers, enabled or disabled, pass FALSE to only
932
 *   retrieve enabled importers.
933
 *
934
 * @return
935
 *   An array of all feed configurations available.
936
 */
937
function feeds_importer_load_all($load_disabled = FALSE) {
938
  $feeds = array();
939
  // This function can get called very early in install process through
940
  // menu_router_rebuild(). Do not try to include CTools if not available.
941
  if (function_exists('ctools_include')) {
942
    ctools_include('export');
943
    $configs = ctools_export_load_object('feeds_importer', 'all');
944
    foreach ($configs as $config) {
945
      if (!empty($config->id) && ($load_disabled || empty($config->disabled))) {
946
        $feeds[$config->id] = feeds_importer($config->id);
947
      }
948
    }
949
    uasort($feeds, 'feeds_importer_name_sort');
950
  }
951
  return $feeds;
952
}
953

    
954
/**
955
 * Sorts importers by name.
956
 *
957
 * Callback for uasort().
958
 *
959
 * @param FeedsImporter $a
960
 *   The first FeedsImporter for comparison.
961
 * @param FeedsImporter $b
962
 *   The second FeedsImporter for comparison.
963
 *
964
 * @return int
965
 *   The comparison result for uasort().
966
 */
967
function feeds_importer_name_sort(FeedsImporter $a, FeedsImporter $b) {
968
  return strcasecmp($a->config['name'], $b->config['name']);
969
}
970

    
971
/**
972
 * Gets an array of enabled importer ids.
973
 *
974
 * @return
975
 *   An array where the values contain ids of enabled importers.
976
 */
977
function feeds_enabled_importers() {
978
  return array_keys(_feeds_importer_digest());
979
}
980

    
981
/**
982
 * Gets an enabled importer configuration by content type.
983
 *
984
 * @param $content_type
985
 *   A node type string.
986
 *
987
 * @return
988
 *   A FeedsImporter id if there is an importer for the given content type,
989
 *   FALSE otherwise.
990
 */
991
function feeds_get_importer_id($content_type) {
992
  $importers = array_flip(_feeds_importer_digest());
993
  return isset($importers[$content_type]) ? $importers[$content_type] : FALSE;
994
}
995

    
996
/**
997
 * Helper function for feeds_get_importer_id() and feeds_enabled_importers().
998
 */
999
function _feeds_importer_digest() {
1000
  $importers = &drupal_static(__FUNCTION__);
1001
  if ($importers === NULL) {
1002
    if ($cache = cache_get(__FUNCTION__)) {
1003
      $importers = $cache->data;
1004
    }
1005
    else {
1006
      $importers = array();
1007
      foreach (feeds_importer_load_all() as $importer) {
1008
        $importers[$importer->id] = isset($importer->config['content_type']) ? $importer->config['content_type'] : '';
1009
      }
1010
      cache_set(__FUNCTION__, $importers);
1011
    }
1012
  }
1013
  return $importers;
1014
}
1015

    
1016
/**
1017
 * Resets importer caches. Call when enabling/disabling importers.
1018
 */
1019
function feeds_cache_clear($rebuild_menu = TRUE) {
1020
  cache_clear_all('_feeds_importer_digest', 'cache');
1021
  drupal_static_reset('_feeds_importer_digest');
1022
  cache_clear_all('plugins:feeds:plugins', 'cache');
1023
  ctools_include('export');
1024
  ctools_export_load_object_reset('feeds_importer');
1025
  drupal_static_reset('_node_types_build');
1026
  if ($rebuild_menu) {
1027
    menu_rebuild();
1028
  }
1029
}
1030

    
1031
/**
1032
 * Exports a FeedsImporter configuration to code.
1033
 */
1034
function feeds_export($importer_id, $indent = '') {
1035
  ctools_include('export');
1036
  $result = ctools_export_load_object('feeds_importer', 'names', array('id' => $importer_id));
1037
  if (isset($result[$importer_id])) {
1038
    return ctools_export_object('feeds_importer', $result[$importer_id], $indent);
1039
  }
1040
}
1041

    
1042
/**
1043
 * Logs to a file like /tmp/feeds_my_domain_org.log in temporary directory.
1044
 */
1045
function feeds_dbg($msg) {
1046
  if (variable_get('feeds_debug', FALSE)) {
1047
    if (!is_string($msg)) {
1048
      $msg = var_export($msg, TRUE);
1049
    }
1050
    $filename = trim(str_replace('/', '_', $_SERVER['HTTP_HOST'] . base_path()), '_');
1051
    $handle = fopen("temporary://feeds_$filename.log", 'a');
1052
    fwrite($handle, gmdate('c') . "\t$msg\n");
1053
    fclose($handle);
1054
  }
1055
}
1056

    
1057
/**
1058
 * Writes to feeds log.
1059
 */
1060
function feeds_log($importer_id, $feed_nid, $type, $message, $variables = array(), $severity = WATCHDOG_NOTICE) {
1061
  if ($severity < WATCHDOG_NOTICE) {
1062
    $error = &drupal_static('feeds_log_error', FALSE);
1063
    $error = TRUE;
1064
  }
1065
  db_insert('feeds_log')
1066
    ->fields(array(
1067
      'id' => $importer_id,
1068
      'feed_nid' => $feed_nid,
1069
      'log_time' => time(),
1070
      'request_time' => REQUEST_TIME,
1071
      'type' => $type,
1072
      'message' => $message,
1073
      'variables' => serialize($variables),
1074
      'severity' => $severity,
1075
    ))
1076
    ->execute();
1077
}
1078

    
1079
/**
1080
 * Loads an item info object.
1081
 *
1082
 * Example usage:
1083
 *
1084
 * $info = feeds_item_info_load('node', $node->nid);
1085
 */
1086
function feeds_item_info_load($entity_type, $entity_id) {
1087
  return db_select('feeds_item')
1088
    ->fields('feeds_item')
1089
    ->condition('entity_type', $entity_type)
1090
    ->condition('entity_id', $entity_id)
1091
    ->execute()
1092
    ->fetchObject();
1093
}
1094

    
1095
/**
1096
 * Inserts an item info object into the feeds_item table.
1097
 */
1098
function feeds_item_info_insert($entity, $entity_id) {
1099
  if (isset($entity->feeds_item)) {
1100
    $entity->feeds_item->entity_id = $entity_id;
1101
    drupal_write_record('feeds_item', $entity->feeds_item);
1102
  }
1103
}
1104

    
1105
/**
1106
 * Inserts or updates an item info object in the feeds_item table.
1107
 */
1108
function feeds_item_info_save($entity, $entity_id) {
1109
  if (isset($entity->feeds_item)) {
1110
    $entity->feeds_item->entity_id = $entity_id;
1111
    if (feeds_item_info_load($entity->feeds_item->entity_type, $entity_id)) {
1112
      drupal_write_record('feeds_item', $entity->feeds_item, array('entity_type', 'entity_id'));
1113
    }
1114
    else {
1115
      feeds_item_info_insert($entity, $entity_id);
1116
    }
1117
  }
1118
}
1119

    
1120
/**
1121
 * @}
1122
 */
1123

    
1124
/**
1125
 * @defgroup instantiators Instantiators
1126
 * @{
1127
 */
1128

    
1129
/**
1130
 * Gets an importer instance.
1131
 *
1132
 * @param $id
1133
 *   The unique id of the importer object.
1134
 *
1135
 * @return
1136
 *   A FeedsImporter object or an object of a class defined by the Drupal
1137
 *   variable 'feeds_importer_class'. There is only one importer object
1138
 *   per $id system-wide.
1139
 */
1140
function feeds_importer($id) {
1141
  return FeedsConfigurable::instance(variable_get('feeds_importer_class', 'FeedsImporter'), $id);
1142
}
1143

    
1144
/**
1145
 * Gets an instance of a source object.
1146
 *
1147
 * @param $importer_id
1148
 *   A FeedsImporter id.
1149
 * @param $feed_nid
1150
 *   The node id of a feed node if the source is attached to a feed node.
1151
 *
1152
 * @return
1153
 *   A FeedsSource object or an object of a class defiend by the Drupal
1154
 *   variable 'source_class'.
1155
 */
1156
function feeds_source($importer_id, $feed_nid = 0) {
1157
  return FeedsSource::instance($importer_id, $feed_nid);
1158
}
1159

    
1160
/**
1161
 * Gets an instance of a class for a given plugin and id.
1162
 *
1163
 * @param string $plugin
1164
 *   A string that is the key of the plugin to load.
1165
 * @param string $id
1166
 *   A string that is the id of the object.
1167
 *
1168
 * @return FeedsPlugin
1169
 *   A FeedsPlugin object.
1170
 */
1171
function feeds_plugin($plugin, $id) {
1172
  ctools_include('plugins');
1173

    
1174
  if ($class = ctools_plugin_load_class('feeds', 'plugins', $plugin, 'handler')) {
1175
    return FeedsPlugin::instance($class, $id, ctools_get_plugins('feeds', 'plugins', $plugin));
1176
  }
1177

    
1178
  $args = array('%plugin' => $plugin, '@id' => $id);
1179
  if (user_access('administer feeds')) {
1180
    if (module_exists('feeds_ui')) {
1181
      $args['@link'] = url('admin/structure/feeds/' . $id);
1182
      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);
1183
    }
1184
    else {
1185
      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);
1186
    }
1187
  }
1188
  else {
1189
    drupal_set_message(t('Missing Feeds plugin %plugin. Please contact your site administrator.', $args), 'warning', FALSE);
1190
  }
1191

    
1192
  $class = ctools_plugin_load_class('feeds', 'plugins', 'FeedsMissingPlugin', 'handler');
1193

    
1194
  return FeedsPlugin::instance($class, $id);
1195
}
1196

    
1197
/**
1198
 * @}
1199
 */
1200

    
1201
/**
1202
 * @defgroup include Funtions for loading libraries
1203
 * @{
1204
 */
1205

    
1206
/**
1207
 * Includes a library file.
1208
 *
1209
 * @param string $file
1210
 *   The filename to load from.
1211
 * @param string $library
1212
 *   The name of the library. If libraries module is installed,
1213
 *   feeds_include_library() will look for libraries with this name managed by
1214
 *   libraries module.
1215
 */
1216
function feeds_include_library($file, $library) {
1217
  static $included = array();
1218

    
1219
  $key = $library . '/' . $file;
1220

    
1221
  if (!isset($included[$key])) {
1222
    $included[$key] = FALSE;
1223

    
1224
    $library_dir = variable_get('feeds_library_dir', FALSE);
1225
    $feeds_library_path = DRUPAL_ROOT . '/' . drupal_get_path('module', 'feeds') . "/libraries/$file";
1226
    $libraries_path = module_exists('libraries') ? libraries_get_path($library) : FALSE;
1227

    
1228
    // Try first whether libraries module is present and load the file from
1229
    // there. If this fails, require the library from the local path.
1230
    if ($libraries_path && is_file("$libraries_path/$file")) {
1231
      require "$libraries_path/$file";
1232
      $included[$key] = TRUE;
1233
    }
1234
    elseif (is_file(DRUPAL_ROOT . '/sites/all/libraries/' . $key)) {
1235
      require DRUPAL_ROOT . '/sites/all/libraries/' . $key;
1236
      $included[$key] = TRUE;
1237
    }
1238
    elseif ($library_dir && is_file($library_dir . '/' . $key)) {
1239
      require $library_dir . '/' . $key;
1240
      $included[$key] = TRUE;
1241
    }
1242
    elseif (is_file($feeds_library_path)) {
1243
      // @todo: Throws "Deprecated function: Assigning the return value of new
1244
      // by reference is deprecated."
1245
      require $feeds_library_path;
1246
      $included[$key] = TRUE;
1247
    }
1248
  }
1249

    
1250
  return $included[$key];
1251
}
1252

    
1253
/**
1254
 * Checks whether a library is present.
1255
 *
1256
 * @param string $file
1257
 *   The filename to load from.
1258
 * @param string $library
1259
 *   The name of the library. If libraries module is installed,
1260
 *   feeds_library_exists() will look for libraries with this name managed by
1261
 *   libraries module.
1262
 */
1263
function feeds_library_exists($file, $library) {
1264
  $path = module_exists('libraries') ? libraries_get_path($library) : FALSE;
1265
  if ($path && is_file($path . '/' . $file)) {
1266
    return TRUE;
1267
  }
1268
  elseif (is_file(DRUPAL_ROOT . "/sites/all/libraries/$library/$file")) {
1269
    return TRUE;
1270
  }
1271
  elseif (is_file(DRUPAL_ROOT . '/' . drupal_get_path('module', 'feeds') . "/libraries/$file")) {
1272
    return TRUE;
1273
  }
1274
  elseif ($library_dir = variable_get('feeds_library_dir', FALSE)) {
1275
    if (is_file("$library_dir/$library/$file")) {
1276
      return TRUE;
1277
    }
1278
  }
1279

    
1280
  return FALSE;
1281
}
1282

    
1283
 /**
1284
 * Checks whether simplepie exists.
1285
 */
1286
function feeds_simplepie_exists() {
1287
  return (
1288
    feeds_library_exists('autoloader.php', 'simplepie') ||
1289
    feeds_library_exists('simplepie.compiled.php', 'simplepie') ||
1290
    feeds_library_exists('simplepie.mini.php', 'simplepie') ||
1291
    feeds_library_exists('simplepie.inc', 'simplepie')
1292
  );
1293
}
1294

    
1295
/**
1296
 * Includes the simplepie library.
1297
 */
1298
function feeds_include_simplepie() {
1299
  $files = array(
1300
    'autoloader.php',
1301
    'simplepie.mini.php',
1302
    'simplepie.compiled.php',
1303
    'simplepie.inc',
1304
  );
1305

    
1306
  foreach ($files as $file) {
1307
    if (feeds_include_library($file, 'simplepie')) {
1308
      return TRUE;
1309
    }
1310
  }
1311

    
1312
  return FALSE;
1313
}
1314

    
1315
/**
1316
 * @deprecated
1317
 *
1318
 * Simplified drupal_alter().
1319
 *
1320
 * - None of that 'multiple parameters by ref' crazyness.
1321
 * - Don't use module_implements() to allow hot including on behalf
1322
 *   implementations (see mappers/).
1323
 *
1324
 * @todo This needs to be removed and drupal_alter() used. This is crazy dumb.
1325
 */
1326
function feeds_alter($type, &$data) {
1327
  $args = array(&$data);
1328
  $additional_args = func_get_args();
1329
  array_shift($additional_args);
1330
  array_shift($additional_args);
1331
  $args = array_merge($args, $additional_args);
1332

    
1333
  $hook = $type . '_alter';
1334
  foreach (module_list() as $module) {
1335
    if (module_hook($module, $hook)) {
1336
      call_user_func_array($module . '_' . $hook, $args);
1337
    }
1338
  }
1339
}
1340

    
1341
/**
1342
 * @}
1343
 */
1344

    
1345
/**
1346
 * Copy of valid_url() that supports the webcal scheme.
1347
 *
1348
 * @see valid_url().
1349
 *
1350
 * @todo Replace with valid_url() when http://drupal.org/node/295021 is fixed.
1351
 */
1352
function feeds_valid_url($url, $absolute = FALSE) {
1353
  if ($absolute) {
1354
    return (bool) preg_match("
1355
      /^                                                      # Start at the beginning of the text
1356
      (?:ftp|https?|feed|webcal):\/\/                         # Look for ftp, http, https, feed or webcal schemes
1357
      (?:                                                     # Userinfo (optional) which is typically
1358
        (?:(?:[\w\.\-\+!$&'\(\)*\+,;=]|%[0-9a-f]{2})+:)*      # a username or a username and password
1359
        (?:[\w\.\-\+%!$&'\(\)*\+,;=]|%[0-9a-f]{2})+@          # combination
1360
      )?
1361
      (?:
1362
        (?:[a-z0-9\-\.]|%[0-9a-f]{2})+                        # A domain name or a IPv4 address
1363
        |(?:\[(?:[0-9a-f]{0,4}:)*(?:[0-9a-f]{0,4})\])         # or a well formed IPv6 address
1364
      )
1365
      (?::[0-9]+)?                                            # Server port number (optional)
1366
      (?:[\/|\?]
1367
        (?:[|\w#!:\.\?\+=&@$'~*,;\/\(\)\[\]\-]|%[0-9a-f]{2})   # The path and query (optional)
1368
      *)?
1369
    $/xi", $url);
1370
  }
1371
  else {
1372
    return (bool) preg_match("/^(?:[\w#!:\.\?\+=&@$'~*,;\/\(\)\[\]\-]|%[0-9a-f]{2})+$/i", $url);
1373
  }
1374
}
1375

    
1376
/**
1377
 * Registers a feed subscription job for execution on feeds_exit().
1378
 *
1379
 * @param array $job
1380
 *   Information about a new job to queue; or if set to NULL (default), leaves
1381
 *   the current queued jobs unchanged.
1382
 *
1383
 * @return
1384
 *   An array of subscribe jobs to process.
1385
 *
1386
 * @see feeds_exit()
1387
 * @see feeds_get_subscription_jobs()
1388
 */
1389
function feeds_set_subscription_job(array $job = NULL) {
1390
  $jobs = &drupal_static(__FUNCTION__, array());
1391
  if (isset($job)) {
1392
    $jobs[] = $job;
1393
  }
1394
  return $jobs;
1395
}
1396

    
1397
/**
1398
 * Returns the list of queued jobs to be run.
1399
 *
1400
 * @return
1401
 *   An array of subscribe jobs to process.
1402
 *
1403
 * @see feeds_set_subscription_job()
1404
 */
1405
function feeds_get_subscription_jobs() {
1406
  return feeds_set_subscription_job();
1407
}
1408

    
1409
/**
1410
 * Implements hook_entity_property_info_alter().
1411
 */
1412
function feeds_entity_property_info_alter(&$info) {
1413

    
1414
  foreach ($info as $entity_type => $entity_info) {
1415
    $info[$entity_type]['properties']['feed_nid'] = array(
1416
      'label' => 'Feed NID',
1417
      'type' => 'integer',
1418
      'description' => t('Nid of the Feed Node that imported this entity.'),
1419
      'getter callback' => 'feeds_get_feed_nid_entity_callback',
1420
    );
1421
    $info[$entity_type]['properties']['feed_node'] = array(
1422
      'label' => 'Feed node',
1423
      'type' => 'node',
1424
      'description' => t('Feed Node that imported this entity.'),
1425
      'getter callback' => 'feeds_get_feed_nid_entity_callback',
1426
    );
1427
  }
1428
}
1429

    
1430
/**
1431
 * Gets the feed_nid for an entity for use in entity metadata.
1432
 */
1433
function feeds_get_feed_nid_entity_callback($entity, array $options, $name, $entity_type) {
1434
  list($entity_id, , ) = entity_extract_ids($entity_type, $entity);
1435

    
1436
  $feed_nid = NULL;
1437
  if ($entity_id) {
1438
    $feed_nid = feeds_get_feed_nid($entity_id, $entity_type);
1439
    if ($feed_nid === FALSE) {
1440
      return NULL;
1441
    }
1442
  }
1443
  // If the entity has no ID (yet) try read the feed nid from the object
1444
  // directly.
1445
  elseif (isset($entity->feeds_item->feed_nid)) {
1446
    $feed_nid = $entity->feeds_item->feed_nid;
1447
  }
1448
  return $feed_nid;
1449
}
1450

    
1451
/**
1452
 * Implements hook_file_download().
1453
 */
1454
function feeds_file_download($uri) {
1455
  $id = db_query("SELECT id FROM {feeds_source} WHERE source = :uri", array(':uri' => $uri))->fetchField();
1456

    
1457
  if (!$id) {
1458
    // File is not associated with a feed.
1459
    return;
1460
  }
1461

    
1462
   // Get the file record based on the URI. If not in the database just return.
1463
  $files = file_load_multiple(array(), array('uri' => $uri));
1464
  foreach ($files as $item) {
1465
    // Since some database servers sometimes use a case-insensitive comparison
1466
    // by default, double check that the filename is an exact match.
1467
    if ($item->uri === $uri) {
1468
      $file = $item;
1469
      break;
1470
    }
1471
  }
1472
  if (!isset($file)) {
1473
    return;
1474
  }
1475

    
1476
  // Check if this file belongs to Feeds.
1477
  $usage_list = file_usage_list($file);
1478
  if (!isset($usage_list['feeds'])) {
1479
    return;
1480
  }
1481

    
1482
  if (!feeds_access('import', $id)) {
1483
    // User does not have permission to import this feed.
1484
    return -1;
1485
  }
1486

    
1487
  // Return file headers.
1488
  return file_get_content_headers($file);
1489
}
1490

    
1491
/**
1492
 * Feeds API version.
1493
 */
1494
function feeds_api_version() {
1495
  $version = feeds_ctools_plugin_api('feeds', 'plugins');
1496
  return $version['version'];
1497
}