Projet

Général

Profil

Paste
Télécharger (37,8 ko) Statistiques
| Branche: | Révision:

root / drupal7 / sites / all / modules / feeds / feeds.module @ 41cc1b08

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
  // If node title is empty, try to retrieve title from feed.
608
  if (trim($node->title) == '') {
609
    try {
610
      $source->addConfig($last_feeds);
611
      if (!$last_title = $source->preview()->title) {
612
        throw new Exception();
613
      }
614
    }
615
    catch (Exception $e) {
616
      drupal_set_message($e->getMessage(), 'error');
617
      form_set_error('title', t('Could not retrieve title from feed.'));
618
    }
619
  }
620
}
621

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

    
639
/**
640
 * Implements hook_node_insert().
641
 */
642
function feeds_node_insert($node) {
643
  // Source attached to node.
644
  feeds_node_update($node);
645
  if (isset($node->feeds) && $importer_id = feeds_get_importer_id($node->type)) {
646
    $source = feeds_source($importer_id, $node->nid);
647
    // Start import if requested.
648
    if (feeds_importer($importer_id)->config['import_on_create'] && !isset($node->feeds['suppress_import'])) {
649
      $source->startImport();
650
    }
651
    // Schedule the source.
652
    $source->schedule();
653
  }
654
}
655

    
656
/**
657
 * Implements hook_node_update().
658
 */
659
function feeds_node_update($node) {
660
  // Source attached to node.
661
  if (isset($node->feeds) && $importer_id = feeds_get_importer_id($node->type)) {
662
    $source = feeds_source($importer_id, $node->nid);
663
    $source->addConfig($node->feeds);
664
    $source->save();
665
  }
666
}
667

    
668
/**
669
 * Implements hook_node_delete().
670
 */
671
function feeds_node_delete($node) {
672
  // Source attached to node.
673
  // Make sure we don't leave any orphans behind: Do not use
674
  // feeds_get_importer_id() to determine importer id as the importer may have
675
  // been deleted.
676
  if ($importer_id = db_query("SELECT id FROM {feeds_source} WHERE feed_nid = :nid", array(':nid' => $node->nid))->fetchField()) {
677
    feeds_source($importer_id, $node->nid)->delete();
678
  }
679
}
680

    
681
/**
682
 * Implements hook_form_BASE_FORM_ID_alter().
683
 */
684
function feeds_form_node_form_alter(&$form, $form_state) {
685
  if ($importer_id = feeds_get_importer_id($form['#node']->type)) {
686
    // Set title to not required, try to retrieve it from feed.
687
    if (isset($form['title'])) {
688
      $form['title']['#required'] = FALSE;
689
    }
690

    
691
    // Enable uploads.
692
    $form['#attributes']['enctype'] = 'multipart/form-data';
693

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

    
707
/**
708
 * Implements hook_field_extra_fields().
709
 */
710
function feeds_field_extra_fields() {
711
  $extras = array();
712
  foreach (node_type_get_names() as $type => $name) {
713
    if (feeds_get_importer_id($type)) {
714
      $extras['node'][$type]['form']['feeds'] = array(
715
        'label' => t('Feed'),
716
        'description' => t('Feeds module form elements'),
717
        'weight' => 0,
718
      );
719
    }
720
  }
721
  return $extras;
722
}
723

    
724
/**
725
 * Implements hook_features_pipe_COMPONENT_alter() for component "feeds_importer".
726
 *
727
 * Automatically adds dependencies when a Feed importer is selected in Features.
728
 */
729
function feeds_features_pipe_feeds_importer_alter(&$pipe, $data, &$export) {
730
  foreach ($data as $importer_id) {
731
    if ($importer = feeds_importer_load($importer_id)) {
732
      $export['dependencies'] = array_merge($export['dependencies'], $importer->dependencies());
733
    }
734
  }
735
}
736

    
737
/**
738
 * Implements hook_system_info_alter().
739
 *
740
 * Goes through a list of all modules that provide Feeds plugins and makes them
741
 * required if there are any importers using those plugins.
742
 */
743
function feeds_system_info_alter(array &$info, $file, $type) {
744
  if ($type !== 'module' || !module_hook($file->name, 'feeds_plugins')) {
745
    return;
746
  }
747

    
748
  // Don't make Feeds require itself, otherwise you can't disable Feeds until
749
  // all importers are deleted.
750
  if ($file->name === 'feeds' || !function_exists('ctools_include')) {
751
    return;
752
  }
753

    
754
  // Get the plugins that belong to the current module.
755
  ctools_include('plugins');
756
  $module_plugins = array();
757
  foreach (ctools_get_plugins('feeds', 'plugins') as $plugin_id => $plugin) {
758
    if ($file->name === $plugin['module']) {
759
      $module_plugins[$plugin_id] = TRUE;
760
    }
761
  }
762

    
763
  // Check if any importers are using any plugins from the current module.
764
  foreach (feeds_importer_load_all(TRUE) as $importer) {
765

    
766
    // Skip importers that are defined in code and are provided by the current
767
    // module. This ensures that modules that define both an importer and a
768
    // plugin can still be disabled.
769
    if ($importer->export_type == EXPORT_IN_CODE) {
770
      $configs = ctools_export_load_object('feeds_importer', 'names', array($importer->id));
771
      if (isset($configs[$importer->id]) && $configs[$importer->id]->export_module === $file->name) {
772
        continue;
773
      }
774
    }
775

    
776
    $configuration = $importer->getConfig();
777

    
778
    foreach (array('fetcher', 'parser', 'processor') as $plugin_type) {
779
      $plugin_key = $configuration[$plugin_type]['plugin_key'];
780
      if (isset($module_plugins[$plugin_key])) {
781
        $info['required'] = TRUE;
782
        break 2;
783
      }
784
    }
785
  }
786

    
787
  if (empty($info['required'])) {
788
    return;
789
  }
790

    
791
  if (module_exists('feeds_ui') && user_access('administer feeds')) {
792
    $info['explanation'] = t('Feeds is currently using this module for one or more <a href="@link">importers</a>', array('@link' => url('admin/structure/feeds')));
793
  }
794
  else {
795
    $info['explanation'] = t('Feeds is currently using this module for one or more importers');
796
  }
797
}
798

    
799
/**
800
 * Implements hook_module_implements_alter().
801
 */
802
function feeds_module_implements_alter(array &$implementations, $hook) {
803
  if ($hook === 'feeds_processor_targets_alter') {
804
    // We need two implementations of this hook, so we add one that gets
805
    // called first, and move the normal one to last.
806
    $implementations = array('_feeds' => FALSE) + $implementations;
807

    
808
    // Move normal implementation to last.
809
    $group = $implementations['feeds'];
810
    unset($implementations['feeds']);
811
    $implementations['feeds'] = $group;
812
  }
813
}
814

    
815
/**
816
 * Implements hook_feeds_processor_targets_alter().
817
 *
818
 * @see feeds_feeds_processor_targets()
819
 * @see feeds_feeds_processor_targets_alter()
820
 */
821
function _feeds_feeds_processor_targets_alter(array &$targets, $entity_type, $bundle) {
822
  // If hook_feeds_processor_targets() hasn't been called, for instance, by
823
  // older processors, invoke it ourself.
824
  if (!drupal_static('feeds_feeds_processor_targets', FALSE)) {
825
    $targets += module_invoke_all('feeds_processor_targets', $entity_type, $bundle);
826
  }
827
}
828

    
829
/**
830
 * Implements hook_flush_caches().
831
 */
832
function feeds_flush_caches() {
833
  return array('cache_feeds_http');
834
}
835

    
836
/**
837
 * @}
838
 */
839

    
840
/**
841
 * @defgroup utility Utility functions
842
 * @{
843
 */
844

    
845
/**
846
 * Loads all importers.
847
 *
848
 * @param $load_disabled
849
 *   Pass TRUE to load all importers, enabled or disabled, pass FALSE to only
850
 *   retrieve enabled importers.
851
 *
852
 * @return
853
 *   An array of all feed configurations available.
854
 */
855
function feeds_importer_load_all($load_disabled = FALSE) {
856
  $feeds = array();
857
  // This function can get called very early in install process through
858
  // menu_router_rebuild(). Do not try to include CTools if not available.
859
  if (function_exists('ctools_include')) {
860
    ctools_include('export');
861
    $configs = ctools_export_load_object('feeds_importer', 'all');
862
    foreach ($configs as $config) {
863
      if (!empty($config->id) && ($load_disabled || empty($config->disabled))) {
864
        $feeds[$config->id] = feeds_importer($config->id);
865
      }
866
    }
867
  }
868
  return $feeds;
869
}
870

    
871
/**
872
 * Gets an array of enabled importer ids.
873
 *
874
 * @return
875
 *   An array where the values contain ids of enabled importers.
876
 */
877
function feeds_enabled_importers() {
878
  return array_keys(_feeds_importer_digest());
879
}
880

    
881
/**
882
 * Gets an enabled importer configuration by content type.
883
 *
884
 * @param $content_type
885
 *   A node type string.
886
 *
887
 * @return
888
 *   A FeedsImporter id if there is an importer for the given content type,
889
 *   FALSE otherwise.
890
 */
891
function feeds_get_importer_id($content_type) {
892
  $importers = array_flip(_feeds_importer_digest());
893
  return isset($importers[$content_type]) ? $importers[$content_type] : FALSE;
894
}
895

    
896
/**
897
 * Helper function for feeds_get_importer_id() and feeds_enabled_importers().
898
 */
899
function _feeds_importer_digest() {
900
  $importers = &drupal_static(__FUNCTION__);
901
  if ($importers === NULL) {
902
    if ($cache = cache_get(__FUNCTION__)) {
903
      $importers = $cache->data;
904
    }
905
    else {
906
      $importers = array();
907
      foreach (feeds_importer_load_all() as $importer) {
908
        $importers[$importer->id] = isset($importer->config['content_type']) ? $importer->config['content_type'] : '';
909
      }
910
      cache_set(__FUNCTION__, $importers);
911
    }
912
  }
913
  return $importers;
914
}
915

    
916
/**
917
 * Resets importer caches. Call when enabling/disabling importers.
918
 */
919
function feeds_cache_clear($rebuild_menu = TRUE) {
920
  cache_clear_all('_feeds_importer_digest', 'cache');
921
  drupal_static_reset('_feeds_importer_digest');
922
  cache_clear_all('plugins:feeds:plugins', 'cache');
923
  ctools_include('export');
924
  ctools_export_load_object_reset('feeds_importer');
925
  drupal_static_reset('_node_types_build');
926
  if ($rebuild_menu) {
927
    menu_rebuild();
928
  }
929
}
930

    
931
/**
932
 * Exports a FeedsImporter configuration to code.
933
 */
934
function feeds_export($importer_id, $indent = '') {
935
  ctools_include('export');
936
  $result = ctools_export_load_object('feeds_importer', 'names', array('id' => $importer_id));
937
  if (isset($result[$importer_id])) {
938
    return ctools_export_object('feeds_importer', $result[$importer_id], $indent);
939
  }
940
}
941

    
942
/**
943
 * Logs to a file like /tmp/feeds_my_domain_org.log in temporary directory.
944
 */
945
function feeds_dbg($msg) {
946
  if (variable_get('feeds_debug', FALSE)) {
947
    if (!is_string($msg)) {
948
      $msg = var_export($msg, TRUE);
949
    }
950
    $filename = trim(str_replace('/', '_', $_SERVER['HTTP_HOST'] . base_path()), '_');
951
    $handle = fopen("temporary://feeds_$filename.log", 'a');
952
    fwrite($handle, gmdate('c') . "\t$msg\n");
953
    fclose($handle);
954
  }
955
}
956

    
957
/**
958
 * Writes to feeds log.
959
 */
960
function feeds_log($importer_id, $feed_nid, $type, $message, $variables = array(), $severity = WATCHDOG_NOTICE) {
961
  if ($severity < WATCHDOG_NOTICE) {
962
    $error = &drupal_static('feeds_log_error', FALSE);
963
    $error = TRUE;
964
  }
965
  db_insert('feeds_log')
966
    ->fields(array(
967
      'id' => $importer_id,
968
      'feed_nid' => $feed_nid,
969
      'log_time' => time(),
970
      'request_time' => REQUEST_TIME,
971
      'type' => $type,
972
      'message' => $message,
973
      'variables' => serialize($variables),
974
      'severity' => $severity,
975
    ))
976
    ->execute();
977
}
978

    
979
/**
980
 * Loads an item info object.
981
 *
982
 * Example usage:
983
 *
984
 * $info = feeds_item_info_load('node', $node->nid);
985
 */
986
function feeds_item_info_load($entity_type, $entity_id) {
987
  return db_select('feeds_item')
988
    ->fields('feeds_item')
989
    ->condition('entity_type', $entity_type)
990
    ->condition('entity_id', $entity_id)
991
    ->execute()
992
    ->fetchObject();
993
}
994

    
995
/**
996
 * Inserts an item info object into the feeds_item table.
997
 */
998
function feeds_item_info_insert($entity, $entity_id) {
999
  if (isset($entity->feeds_item)) {
1000
    $entity->feeds_item->entity_id = $entity_id;
1001
    drupal_write_record('feeds_item', $entity->feeds_item);
1002
  }
1003
}
1004

    
1005
/**
1006
 * Inserts or updates an item info object in the feeds_item table.
1007
 */
1008
function feeds_item_info_save($entity, $entity_id) {
1009
  if (isset($entity->feeds_item)) {
1010
    $entity->feeds_item->entity_id = $entity_id;
1011
    if (feeds_item_info_load($entity->feeds_item->entity_type, $entity_id)) {
1012
      drupal_write_record('feeds_item', $entity->feeds_item, array('entity_type', 'entity_id'));
1013
    }
1014
    else {
1015
      feeds_item_info_insert($entity, $entity_id);
1016
    }
1017
  }
1018
}
1019

    
1020
/**
1021
 * @}
1022
 */
1023

    
1024
/**
1025
 * @defgroup instantiators Instantiators
1026
 * @{
1027
 */
1028

    
1029
/**
1030
 * Gets an importer instance.
1031
 *
1032
 * @param $id
1033
 *   The unique id of the importer object.
1034
 *
1035
 * @return
1036
 *   A FeedsImporter object or an object of a class defined by the Drupal
1037
 *   variable 'feeds_importer_class'. There is only one importer object
1038
 *   per $id system-wide.
1039
 */
1040
function feeds_importer($id) {
1041
  return FeedsConfigurable::instance(variable_get('feeds_importer_class', 'FeedsImporter'), $id);
1042
}
1043

    
1044
/**
1045
 * Gets an instance of a source object.
1046
 *
1047
 * @param $importer_id
1048
 *   A FeedsImporter id.
1049
 * @param $feed_nid
1050
 *   The node id of a feed node if the source is attached to a feed node.
1051
 *
1052
 * @return
1053
 *   A FeedsSource object or an object of a class defiend by the Drupal
1054
 *   variable 'source_class'.
1055
 */
1056
function feeds_source($importer_id, $feed_nid = 0) {
1057
  return FeedsSource::instance($importer_id, $feed_nid);
1058
}
1059

    
1060
/**
1061
 * Gets an instance of a class for a given plugin and id.
1062
 *
1063
 * @param string $plugin
1064
 *   A string that is the key of the plugin to load.
1065
 * @param string $id
1066
 *   A string that is the id of the object.
1067
 *
1068
 * @return FeedsPlugin
1069
 *   A FeedsPlugin object.
1070
 */
1071
function feeds_plugin($plugin, $id) {
1072
  ctools_include('plugins');
1073

    
1074
  if ($class = ctools_plugin_load_class('feeds', 'plugins', $plugin, 'handler')) {
1075
    return FeedsPlugin::instance($class, $id, ctools_get_plugins('feeds', 'plugins', $plugin));
1076
  }
1077

    
1078
  $args = array('%plugin' => $plugin, '@id' => $id);
1079
  if (user_access('administer feeds')) {
1080
    $args['@link'] = url('admin/structure/feeds/' . $id);
1081
    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);
1082
  }
1083
  else {
1084
    drupal_set_message(t('Missing Feeds plugin %plugin. Please contact your site administrator.', $args), 'warning', FALSE);
1085
  }
1086

    
1087
  $class = ctools_plugin_load_class('feeds', 'plugins', 'FeedsMissingPlugin', 'handler');
1088

    
1089
  return FeedsPlugin::instance($class, $id);
1090
}
1091

    
1092
/**
1093
 * @}
1094
 */
1095

    
1096
/**
1097
 * @defgroup include Funtions for loading libraries
1098
 * @{
1099
 */
1100

    
1101
/**
1102
 * Includes a library file.
1103
 *
1104
 * @param string $file
1105
 *   The filename to load from.
1106
 * @param string $library
1107
 *   The name of the library. If libraries module is installed,
1108
 *   feeds_include_library() will look for libraries with this name managed by
1109
 *   libraries module.
1110
 */
1111
function feeds_include_library($file, $library) {
1112
  static $included = array();
1113

    
1114
  $key = $library . '/' . $file;
1115

    
1116
  if (!isset($included[$key])) {
1117
    $included[$key] = FALSE;
1118

    
1119
    $library_dir = variable_get('feeds_library_dir', FALSE);
1120
    $feeds_library_path = DRUPAL_ROOT . '/' . drupal_get_path('module', 'feeds') . "/libraries/$file";
1121
    $libraries_path = module_exists('libraries') ? libraries_get_path($library) : FALSE;
1122

    
1123
    // Try first whether libraries module is present and load the file from
1124
    // there. If this fails, require the library from the local path.
1125
    if ($libraries_path && is_file("$libraries_path/$file")) {
1126
      require "$libraries_path/$file";
1127
      $included[$key] = TRUE;
1128
    }
1129
    elseif (is_file(DRUPAL_ROOT . '/sites/all/libraries/' . $key)) {
1130
      require DRUPAL_ROOT . '/sites/all/libraries/' . $key;
1131
      $included[$key] = TRUE;
1132
    }
1133
    elseif ($library_dir && is_file($library_dir . '/' . $key)) {
1134
      require $library_dir . '/' . $key;
1135
      $included[$key] = TRUE;
1136
    }
1137
    elseif (is_file($feeds_library_path)) {
1138
      // @todo: Throws "Deprecated function: Assigning the return value of new
1139
      // by reference is deprecated."
1140
      require $feeds_library_path;
1141
      $included[$key] = TRUE;
1142
    }
1143
  }
1144

    
1145
  return $included[$key];
1146
}
1147

    
1148
/**
1149
 * Checks whether a library is present.
1150
 *
1151
 * @param string $file
1152
 *   The filename to load from.
1153
 * @param string $library
1154
 *   The name of the library. If libraries module is installed,
1155
 *   feeds_library_exists() will look for libraries with this name managed by
1156
 *   libraries module.
1157
 */
1158
function feeds_library_exists($file, $library) {
1159
  $path = module_exists('libraries') ? libraries_get_path($library) : FALSE;
1160
  if ($path && is_file($path . '/' . $file)) {
1161
    return TRUE;
1162
  }
1163
  elseif (is_file(DRUPAL_ROOT . "/sites/all/libraries/$library/$file")) {
1164
    return TRUE;
1165
  }
1166
  elseif (is_file(DRUPAL_ROOT . '/' . drupal_get_path('module', 'feeds') . "/libraries/$file")) {
1167
    return TRUE;
1168
  }
1169
  elseif ($library_dir = variable_get('feeds_library_dir', FALSE)) {
1170
    if (is_file("$library_dir/$library/$file")) {
1171
      return TRUE;
1172
    }
1173
  }
1174

    
1175
  return FALSE;
1176
}
1177

    
1178
 /**
1179
 * Checks whether simplepie exists.
1180
 */
1181
function feeds_simplepie_exists() {
1182
  return (
1183
    feeds_library_exists('autoloader.php', 'simplepie') ||
1184
    feeds_library_exists('simplepie.compiled.php', 'simplepie') ||
1185
    feeds_library_exists('simplepie.mini.php', 'simplepie') ||
1186
    feeds_library_exists('simplepie.inc', 'simplepie')
1187
  );
1188
}
1189

    
1190
/**
1191
 * Includes the simplepie library.
1192
 */
1193
function feeds_include_simplepie() {
1194
  $files = array(
1195
    'autoloader.php',
1196
    'simplepie.mini.php',
1197
    'simplepie.compiled.php',
1198
    'simplepie.inc',
1199
  );
1200

    
1201
  foreach ($files as $file) {
1202
    if (feeds_include_library($file, 'simplepie')) {
1203
      return TRUE;
1204
    }
1205
  }
1206

    
1207
  return FALSE;
1208
}
1209

    
1210
/**
1211
 * @deprecated
1212
 *
1213
 * Simplified drupal_alter().
1214
 *
1215
 * - None of that 'multiple parameters by ref' crazyness.
1216
 * - Don't use module_implements() to allow hot including on behalf
1217
 *   implementations (see mappers/).
1218
 *
1219
 * @todo This needs to be removed and drupal_alter() used. This is crazy dumb.
1220
 */
1221
function feeds_alter($type, &$data) {
1222
  $args = array(&$data);
1223
  $additional_args = func_get_args();
1224
  array_shift($additional_args);
1225
  array_shift($additional_args);
1226
  $args = array_merge($args, $additional_args);
1227

    
1228
  $hook = $type . '_alter';
1229
  foreach (module_list() as $module) {
1230
    if (module_hook($module, $hook)) {
1231
      call_user_func_array($module . '_' . $hook, $args);
1232
    }
1233
  }
1234
}
1235

    
1236
/**
1237
 * @}
1238
 */
1239

    
1240
/**
1241
 * Copy of valid_url() that supports the webcal scheme.
1242
 *
1243
 * @see valid_url().
1244
 *
1245
 * @todo Replace with valid_url() when http://drupal.org/node/295021 is fixed.
1246
 */
1247
function feeds_valid_url($url, $absolute = FALSE) {
1248
  if ($absolute) {
1249
    return (bool) preg_match("
1250
      /^                                                      # Start at the beginning of the text
1251
      (?:ftp|https?|feed|webcal):\/\/                         # Look for ftp, http, https, feed or webcal schemes
1252
      (?:                                                     # Userinfo (optional) which is typically
1253
        (?:(?:[\w\.\-\+!$&'\(\)*\+,;=]|%[0-9a-f]{2})+:)*      # a username or a username and password
1254
        (?:[\w\.\-\+%!$&'\(\)*\+,;=]|%[0-9a-f]{2})+@          # combination
1255
      )?
1256
      (?:
1257
        (?:[a-z0-9\-\.]|%[0-9a-f]{2})+                        # A domain name or a IPv4 address
1258
        |(?:\[(?:[0-9a-f]{0,4}:)*(?:[0-9a-f]{0,4})\])         # or a well formed IPv6 address
1259
      )
1260
      (?::[0-9]+)?                                            # Server port number (optional)
1261
      (?:[\/|\?]
1262
        (?:[|\w#!:\.\?\+=&@$'~*,;\/\(\)\[\]\-]|%[0-9a-f]{2})   # The path and query (optional)
1263
      *)?
1264
    $/xi", $url);
1265
  }
1266
  else {
1267
    return (bool) preg_match("/^(?:[\w#!:\.\?\+=&@$'~*,;\/\(\)\[\]\-]|%[0-9a-f]{2})+$/i", $url);
1268
  }
1269
}
1270

    
1271
/**
1272
 * Registers a feed subscription job for execution on feeds_exit().
1273
 *
1274
 * @param array $job
1275
 *   Information about a new job to queue; or if set to NULL (default), leaves
1276
 *   the current queued jobs unchanged.
1277
 *
1278
 * @return
1279
 *   An array of subscribe jobs to process.
1280
 *
1281
 * @see feeds_exit()
1282
 * @see feeds_get_subscription_jobs()
1283
 */
1284
function feeds_set_subscription_job(array $job = NULL) {
1285
  $jobs = &drupal_static(__FUNCTION__, array());
1286
  if (isset($job)) {
1287
    $jobs[] = $job;
1288
  }
1289
  return $jobs;
1290
}
1291

    
1292
/**
1293
 * Returns the list of queued jobs to be run.
1294
 *
1295
 * @return
1296
 *   An array of subscribe jobs to process.
1297
 *
1298
 * @see feeds_set_subscription_job()
1299
 */
1300
function feeds_get_subscription_jobs() {
1301
  return feeds_set_subscription_job();
1302
}
1303

    
1304
/**
1305
 * Implements hook_entity_property_info_alter().
1306
 */
1307
function feeds_entity_property_info_alter(&$info) {
1308

    
1309
  foreach ($info as $entity_type => $entity_info) {
1310
    $info[$entity_type]['properties']['feed_nid'] = array(
1311
      'label' => 'Feed NID',
1312
      'type' => 'integer',
1313
      'description' => t('Nid of the Feed Node that imported this entity.'),
1314
      'getter callback' => 'feeds_get_feed_nid_entity_callback',
1315
    );
1316
    $info[$entity_type]['properties']['feed_node'] = array(
1317
      'label' => 'Feed node',
1318
      'type' => 'node',
1319
      'description' => t('Feed Node that imported this entity.'),
1320
      'getter callback' => 'feeds_get_feed_nid_entity_callback',
1321
    );
1322
  }
1323
}
1324

    
1325
/**
1326
 * Gets the feed_nid for an entity for use in entity metadata.
1327
 */
1328
function feeds_get_feed_nid_entity_callback($entity, array $options, $name, $entity_type) {
1329
  list($entity_id, , ) = entity_extract_ids($entity_type, $entity);
1330

    
1331
  $feed_nid = NULL;
1332
  if ($entity_id) {
1333
    $feed_nid = feeds_get_feed_nid($entity_id, $entity_type);
1334
    if ($feed_nid === FALSE) {
1335
      return NULL;
1336
    }
1337
  }
1338
  // If the entity has no ID (yet) try read the feed nid from the object
1339
  // directly.
1340
  elseif (isset($entity->feeds_item->feed_nid)) {
1341
    $feed_nid = $entity->feeds_item->feed_nid;
1342
  }
1343
  return $feed_nid;
1344
}
1345

    
1346
/**
1347
 * Implements hook_file_download().
1348
 */
1349
function feeds_file_download($uri) {
1350
  $id = db_query("SELECT id FROM {feeds_source} WHERE source = :uri", array(':uri' => $uri))->fetchField();
1351

    
1352
  if (!$id) {
1353
    // File is not associated with a feed.
1354
    return;
1355
  }
1356

    
1357
   // Get the file record based on the URI. If not in the database just return.
1358
  $files = file_load_multiple(array(), array('uri' => $uri));
1359
  foreach ($files as $item) {
1360
    // Since some database servers sometimes use a case-insensitive comparison
1361
    // by default, double check that the filename is an exact match.
1362
    if ($item->uri === $uri) {
1363
      $file = $item;
1364
      break;
1365
    }
1366
  }
1367
  if (!isset($file)) {
1368
    return;
1369
  }
1370

    
1371
  // Check if this file belongs to Feeds.
1372
  $usage_list = file_usage_list($file);
1373
  if (!isset($usage_list['feeds'])) {
1374
    return;
1375
  }
1376

    
1377
  if (!feeds_access('import', $id)) {
1378
    // User does not have permission to import this feed.
1379
    return -1;
1380
  }
1381

    
1382
  // Return file headers.
1383
  return file_get_content_headers($file);
1384
}
1385

    
1386
/**
1387
 * Feeds API version.
1388
 */
1389
function feeds_api_version() {
1390
  $version = feeds_ctools_plugin_api('feeds', 'plugins');
1391
  return $version['version'];
1392
}