Projet

Général

Profil

Paste
Télécharger (33,7 ko) Statistiques
| Branche: | Révision:

root / htmltest / sites / all / modules / feeds / feeds.module @ a5572547

1 85ad3d82 Assos Assos
<?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_alter',
39
    'feeds_parser_sources_alter',
40
  );
41
42
  return array_fill_keys($hooks, array('group' => 'feeds'));
43
}
44
45
/**
46
 * Implements hook_cron().
47
 */
48
function feeds_cron() {
49
  if ($importers = feeds_reschedule()) {
50
    foreach ($importers as $id) {
51
      feeds_importer($id)->schedule();
52
      $rows = db_query("SELECT feed_nid FROM {feeds_source} WHERE id = :id", array(':id' => $id));
53
      foreach ($rows as $row) {
54
        feeds_source($id, $row->feed_nid)->schedule();
55
      }
56
    }
57
    feeds_reschedule(FALSE);
58
  }
59
  // Expire old log entries.
60
  db_delete('feeds_log')
61
    ->condition('request_time', REQUEST_TIME - 604800, '<')
62
    ->execute();
63
}
64
65
/**
66
 * Implements hook_cron_job_scheduler_info().
67
 *
68
 * Compare queue names with key names in feeds_cron_queue_info().
69
 */
70
function feeds_cron_job_scheduler_info() {
71
  $info = array();
72
  $info['feeds_source_import'] = array(
73
    'queue name' => 'feeds_source_import',
74
  );
75
  $info['feeds_source_clear'] = array(
76
    'queue name' => 'feeds_source_clear',
77
  );
78
  $info['feeds_importer_expire'] = array(
79
    'queue name' => 'feeds_importer_expire',
80
  );
81
  $info['feeds_push_unsubscribe'] = array(
82
    'queue name' => 'feeds_push_unsubscribe',
83
  );
84
  return $info;
85
}
86
87
/**
88
 * Implements hook_cron_queue_info().
89
 */
90
function feeds_cron_queue_info() {
91
  $queues = array();
92
  $queues['feeds_source_import'] = array(
93
    'worker callback' => 'feeds_source_import',
94
    'time' => 15,
95
  );
96
  $queues['feeds_source_clear'] = array(
97
    'worker callback' => 'feeds_source_clear',
98
    'time' => 15,
99
  );
100
  $queues['feeds_importer_expire'] = array(
101
    'worker callback' => 'feeds_importer_expire',
102
    'time' => 15,
103
  );
104
  $queues['feeds_push_unsubscribe'] = array(
105
    'worker callback' => 'feeds_push_unsubscribe',
106
    'time' => 15,
107
  );
108
  return $queues;
109
}
110
111
/**
112
 * Scheduler callback for importing from a source.
113
 */
114
function feeds_source_import($job) {
115
  $source = feeds_source($job['type'], $job['id']);
116
  try {
117
    $source->existing()->import();
118
  }
119
  catch (FeedsNotExistingException $e) {
120
    // Do nothing.
121
  }
122
  catch (Exception $e) {
123
    $source->log('import', $e->getMessage(), array(), WATCHDOG_ERROR);
124
  }
125
  $source->scheduleImport();
126
}
127
128
/**
129
 * Scheduler callback for deleting all items from a source.
130
 */
131
function feeds_source_clear($job) {
132
  $source = feeds_source($job['type'], $job['id']);
133
  try {
134
    $source->existing()->clear();
135
  }
136
  catch (FeedsNotExistingException $e) {
137
    // Do nothing.
138
  }
139
  catch (Exception $e) {
140
    $source->log('clear', $e->getMessage(), array(), WATCHDOG_ERROR);
141
  }
142
  $source->scheduleClear();
143
}
144
145
/**
146
 * Scheduler callback for expiring content.
147
 */
148
function feeds_importer_expire($job) {
149
  $importer = feeds_importer($job['type']);
150
  try {
151
    $importer->existing()->expire();
152
  }
153
  catch (FeedsNotExistingException $e) {
154
    // Do nothing.
155
  }
156
  catch (Exception $e) {
157
    $importer->log('expire', $e->getMessage(), array(), WATCHDOG_ERROR);
158
  }
159
  $importer->scheduleExpire();
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 $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
207
 *   TRUE if all importers need rescheduling. FALSE if no rescheduling is
208
 *   required. An array of importers that need rescheduling.
209
 */
210
function feeds_reschedule($importer_id = NULL) {
211
  $reschedule = variable_get('feeds_reschedule', FALSE);
212
  if ($importer_id === TRUE || $importer_id === FALSE) {
213
    $reschedule = $importer_id;
214
  }
215
  elseif (is_string($importer_id) && $reschedule !== TRUE) {
216
    $reschedule = is_array($reschedule) ? $reschedule : array();
217
    $reschedule[$importer_id] = $importer_id;
218
  }
219
  variable_set('feeds_reschedule', $reschedule);
220
  if ($reschedule === TRUE) {
221
    return feeds_enabled_importers();
222
  }
223
  return $reschedule;
224
}
225
226
/**
227
 * Implements feeds_permission().
228
 */
229
function feeds_permission() {
230
  $perms = array(
231
    'administer feeds' => array(
232
      'title' => t('Administer Feeds'),
233
      'description' => t('Create, update, delete importers, execute import and delete tasks on any importer.')
234
    ),
235
  );
236
  foreach (feeds_importer_load_all() as $importer) {
237
    $perms["import $importer->id feeds"] = array(
238
      'title' => t('Import @name feeds', array('@name' => $importer->config['name'])),
239
    );
240
    $perms["clear $importer->id feeds"] = array(
241
      'title' => t('Delete items from @name feeds', array('@name' => $importer->config['name'])),
242
    );
243
    $perms["unlock $importer->id feeds"] = array(
244
      'title' => t('Unlock imports from @name feeds', array('@name' => $importer->config['name'])),
245
      'description' => t('If a feed importation breaks for some reason, users with this permission can unlock them.')
246
    );
247
  }
248
  return $perms;
249
}
250
251
/**
252
 * Implements hook_forms().
253
 *
254
 * Declare form callbacks for all known classes derived from FeedsConfigurable.
255
 */
256
function feeds_forms() {
257
  $forms = array();
258
  $forms['FeedsImporter_feeds_form']['callback'] = 'feeds_form';
259
  $plugins = FeedsPlugin::all();
260
  foreach ($plugins as $plugin) {
261
    $forms[$plugin['handler']['class'] . '_feeds_form']['callback'] = 'feeds_form';
262
  }
263
  return $forms;
264
}
265
266
/**
267
 * Implements hook_menu().
268
 */
269
function feeds_menu() {
270
  $items = array();
271
  $items['import'] = array(
272
    'title' => 'Import',
273
    'page callback' => 'feeds_page',
274
    'access callback' => 'feeds_page_access',
275
    'file' => 'feeds.pages.inc',
276
  );
277
  $items['import/%'] = array(
278
    'title callback' => 'feeds_importer_title',
279
    'title arguments' => array(1),
280
    'page callback' => 'drupal_get_form',
281
    'page arguments' => array('feeds_import_form', 1),
282
    'access callback' => 'feeds_access',
283
    'access arguments' => array('import', 1),
284
    'file' => 'feeds.pages.inc',
285
  );
286
  $items['import/%/import'] = array(
287
    'title' => 'Import',
288
    'type' => MENU_DEFAULT_LOCAL_TASK,
289
    'weight' => -10,
290
  );
291
  $items['import/%/delete-items'] = array(
292
    'title' => 'Delete items',
293
    'page callback' => 'drupal_get_form',
294
    'page arguments' => array('feeds_delete_tab_form', 1),
295
    'access callback' => 'feeds_access',
296
    'access arguments' => array('clear', 1),
297
    'file' => 'feeds.pages.inc',
298
    'type' => MENU_LOCAL_TASK,
299
  );
300
  $items['import/%/unlock'] = array(
301
    'title' => 'Unlock',
302
    'page callback' => 'drupal_get_form',
303
    'page arguments' => array('feeds_unlock_tab_form', 1),
304
    'access callback' => 'feeds_access',
305
    'access arguments' => array('unlock', 1),
306
    'file' => 'feeds.pages.inc',
307
    'type' => MENU_LOCAL_TASK,
308
  );
309
  $items['import/%/template'] = array(
310
    'page callback' => 'feeds_importer_template',
311
    'page arguments' => array(1),
312
    'access callback' => 'feeds_access',
313
    'access arguments' => array('import', 1),
314
    'file' => 'feeds.pages.inc',
315
    'type' => MENU_CALLBACK,
316
  );
317
  $items['node/%node/import'] = array(
318
    'title' => 'Import',
319
    'page callback' => 'drupal_get_form',
320
    'page arguments' => array('feeds_import_tab_form', 1),
321
    'access callback' => 'feeds_access',
322
    'access arguments' => array('import', 1),
323
    'file' => 'feeds.pages.inc',
324
    'type' => MENU_LOCAL_TASK,
325
    'weight' => 10,
326
  );
327
  $items['node/%node/delete-items'] = array(
328
    'title' => 'Delete items',
329
    'page callback' => 'drupal_get_form',
330
    'page arguments' => array('feeds_delete_tab_form', NULL, 1),
331
    'access callback' => 'feeds_access',
332
    'access arguments' => array('clear', 1),
333
    'file' => 'feeds.pages.inc',
334
    'type' => MENU_LOCAL_TASK,
335
    'weight' => 11,
336
  );
337
  $items['node/%node/unlock'] = array(
338
    'title' => 'Unlock',
339
    'page callback' => 'drupal_get_form',
340
    'page arguments' => array('feeds_unlock_tab_form', NULL, 1),
341
    'access callback' => 'feeds_access',
342
    'access arguments' => array('unlock', 1),
343
    'file' => 'feeds.pages.inc',
344
    'type' => MENU_LOCAL_TASK,
345
    'weight' => 11,
346
  );
347
  // @todo Eliminate this step and thus eliminate clearing menu cache when
348
  // manipulating importers.
349
  foreach (feeds_importer_load_all() as $importer) {
350
    $items += $importer->fetcher->menuItem();
351
  }
352
  return $items;
353
}
354
355
/**
356
 * Menu loader callback.
357
 */
358
function feeds_importer_load($id) {
359
  return feeds_importer($id);
360
}
361
362
/**
363
 * Title callback.
364
 */
365
function feeds_importer_title($id) {
366
  $importer = feeds_importer($id);
367
  return $importer->config['name'];
368
}
369
370
/**
371
 * Implements hook_theme().
372
 */
373
function feeds_theme() {
374
  return array(
375
    'feeds_upload' => array(
376
      'file' => 'feeds.pages.inc',
377
      'render element' => 'element',
378
    ),
379
    'feeds_source_status' => array(
380
      'file' => 'feeds.pages.inc',
381
      'variables' => array(
382
        'progress_importing' => NULL,
383
        'progress_clearing' => NULL,
384
        'imported' => NULL,
385
        'count' => NULL,
386
      ),
387
    ),
388
  );
389
}
390
391
/**
392
 * Menu access callback.
393
 *
394
 * @param $action
395
 *   The action to be performed. Possible values are:
396
 *   - import
397
 *   - clear
398
 *   - unlock
399
 * @param $param
400
 *   Node object or FeedsImporter id.
401
 */
402
function feeds_access($action, $param) {
403
  if (!in_array($action, array('import', 'clear', 'unlock'))) {
404
    // If $action is not one of the supported actions, we return access denied.
405
    return FALSE;
406
  }
407
408
  if (is_string($param)) {
409
    $importer_id = $param;
410
  }
411
  elseif ($param->type) {
412
    $importer_id = feeds_get_importer_id($param->type);
413
  }
414
415
  // Check for permissions if feed id is present, otherwise return FALSE.
416
  if ($importer_id) {
417
    if (user_access('administer feeds') || user_access("{$action} {$importer_id} feeds")) {
418
      return TRUE;
419
    }
420
  }
421
  return FALSE;
422
}
423
424
/**
425
 * Access callback to determine if the user can import Feeds importers.
426
 *
427
 * Feeds imports require an additional access check because they are PHP
428
 * code and PHP is more locked down than administer feeds.
429
 */
430
function feeds_importer_import_access() {
431
  return user_access('administer feeds') && user_access('use PHP for settings');
432
}
433
434
/**
435
 * Menu access callback.
436
 */
437
function feeds_page_access() {
438
  if (user_access('administer feeds')) {
439
    return TRUE;
440
  }
441
  foreach (feeds_enabled_importers() as $id) {
442
    if (user_access("import $id feeds")) {
443
      return TRUE;
444
    }
445
  }
446
  return FALSE;
447
}
448
449
/**
450
 * Implements hook_exit().
451
 */
452
function feeds_exit() {
453
  // Process any pending PuSH subscriptions.
454
  $jobs = feeds_get_subscription_jobs();
455
  foreach ($jobs as $job) {
456
    if (!isset($job['fetcher']) || !isset($job['source'])) {
457
      continue;
458
     }
459
    $job['fetcher']->subscribe($job['source']);
460
  }
461
462
  if (drupal_static('feeds_log_error', FALSE)) {
463
    watchdog('feeds', 'Feeds reported errors, visit the Feeds log for details.', array(), WATCHDOG_ERROR, 'admin/reports/dblog/feeds');
464
  }
465
}
466
467
/**
468
 * Implements hook_views_api().
469
 */
470
function feeds_views_api() {
471
  return array(
472
    'api' => 3,
473
    'path' => drupal_get_path('module', 'feeds') . '/views',
474
  );
475
}
476
477
/**
478
 * Implements hook_ctools_plugin_api().
479
 */
480
function feeds_ctools_plugin_api($owner, $api) {
481
  if ($owner == 'feeds' && $api == 'plugins') {
482
    return array('version' => 1);
483
  }
484
}
485
486
/**
487
 * Implements hook_ctools_plugin_type().
488
 */
489
function feeds_ctools_plugin_type() {
490
  return array(
491
    'plugins' => array(
492
      'cache' => TRUE,
493
      'use hooks' => TRUE,
494
      'classes' => array('handler'),
495
    ),
496
  );
497
}
498
499
/**
500
 * Implements hook_feeds_plugins().
501
 */
502
function feeds_feeds_plugins() {
503
  module_load_include('inc', 'feeds', 'feeds.plugins');
504
  return _feeds_feeds_plugins();
505
}
506
507
/**
508
 * Gets the feed_nid for a single entity.
509
 *
510
 * @param int $entity_id
511
 *   The entity id.
512
 * @param string $entity_type
513
 *   The type of entity.
514
 *
515
 * @return int|bool
516
 *   The feed_nid of the entity, or FALSE if the entity doesn't belong to a
517
 *   feed.
518
 */
519
function feeds_get_feed_nid($entity_id, $entity_type) {
520
  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();
521
}
522
523
/**
524
 * Implements hook_entity_insert().
525
 */
526
function feeds_entity_insert($entity, $type) {
527
  list($id) = entity_extract_ids($type, $entity);
528
  feeds_item_info_insert($entity, $id);
529
}
530
531
/**
532
 * Implements hook_entity_update().
533
 */
534
function feeds_entity_update($entity, $type) {
535
  list($id) = entity_extract_ids($type, $entity);
536
  feeds_item_info_save($entity, $id);
537
}
538
539
/**
540
 * Implements hook_entity_delete().
541
 */
542
function feeds_entity_delete($entity, $type) {
543
  list($id) = entity_extract_ids($type, $entity);
544
545
  // Delete any imported items produced by the source.
546
  db_delete('feeds_item')
547
    ->condition('entity_type', $type)
548
    ->condition('entity_id', $id)
549
    ->execute();
550
}
551
552
/**
553
 * Implements hook_node_validate().
554
 */
555
function feeds_node_validate($node, $form, &$form_state) {
556
  if (!$importer_id = feeds_get_importer_id($node->type)) {
557
    return;
558
  }
559
  // Keep a copy of the title for subsequent node creation stages.
560
  // @todo: revisit whether $node still looses all of its properties
561
  // between validate and insert stage.
562
  $last_title = &drupal_static('feeds_node_last_title');
563
  $last_feeds = &drupal_static('feeds_node_last_feeds');
564
565
  // On validation stage we are working with a FeedsSource object that is
566
  // not tied to a nid - when creating a new node there is no
567
  // $node->nid at this stage.
568
  $source = feeds_source($importer_id);
569
570
  // Node module magically moved $form['feeds'] to $node->feeds :P.
571
  // configFormValidate may modify $last_feed, smuggle it to update/insert stage
572
  // through a static variable.
573
  $last_feeds = $node->feeds;
574
  $source->configFormValidate($last_feeds);
575
576
  // If node title is empty, try to retrieve title from feed.
577
  if (trim($node->title) == '') {
578
    try {
579
      $source->addConfig($last_feeds);
580
      if (!$last_title = $source->preview()->title) {
581
        throw new Exception();
582
      }
583
    }
584
    catch (Exception $e) {
585
      drupal_set_message($e->getMessage(), 'error');
586
      form_set_error('title', t('Could not retrieve title from feed.'));
587
    }
588
  }
589
}
590
591
/**
592
 * Implements hook_node_presave().
593
 */
594
function feeds_node_presave($node) {
595
  // Populate $node->title and $node->feed from result of validation phase.
596
  $last_title = &drupal_static('feeds_node_last_title');
597
  $last_feeds = &drupal_static('feeds_node_last_feeds');
598
  if (empty($node->title) && !empty($last_title)) {
599
    $node->title = $last_title;
600
  }
601
  if (!empty($last_feeds)) {
602
    $node->feeds = $last_feeds;
603
  }
604
  $last_title = NULL;
605
  $last_feeds = NULL;
606
}
607
608
/**
609
 * Implements hook_node_insert().
610
 */
611
function feeds_node_insert($node) {
612
  // Source attached to node.
613
  feeds_node_update($node);
614
  if (isset($node->feeds) && $importer_id = feeds_get_importer_id($node->type)) {
615
    $source = feeds_source($importer_id, $node->nid);
616
    // Start import if requested.
617
    if (feeds_importer($importer_id)->config['import_on_create'] && !isset($node->feeds['suppress_import'])) {
618
      $source->startImport();
619
    }
620
    // Schedule source and importer.
621
    $source->schedule();
622
    feeds_importer($importer_id)->schedule();
623
  }
624
}
625
626
/**
627
 * Implements hook_node_update().
628
 */
629
function feeds_node_update($node) {
630
  // Source attached to node.
631
  if (isset($node->feeds) && $importer_id = feeds_get_importer_id($node->type)) {
632
    $source = feeds_source($importer_id, $node->nid);
633
    $source->addConfig($node->feeds);
634
    $source->save();
635
  }
636
}
637
638
/**
639
 * Implements hook_node_delete().
640
 */
641
function feeds_node_delete($node) {
642
  // Source attached to node.
643
  // Make sure we don't leave any orphans behind: Do not use
644
  // feeds_get_importer_id() to determine importer id as the importer may have
645
  // been deleted.
646
  if ($importer_id = db_query("SELECT id FROM {feeds_source} WHERE feed_nid = :nid", array(':nid' => $node->nid))->fetchField()) {
647
    feeds_source($importer_id, $node->nid)->delete();
648
  }
649
}
650
651
/**
652
 * Implements hook_form_BASE_FORM_ID_alter().
653
 */
654
function feeds_form_node_form_alter(&$form, $form_state) {
655
  if ($importer_id = feeds_get_importer_id($form['#node']->type)) {
656
    // Set title to not required, try to retrieve it from feed.
657
    if (isset($form['title'])) {
658
      $form['title']['#required'] = FALSE;
659
    }
660
661
    // Enable uploads.
662
    $form['#attributes']['enctype'] = 'multipart/form-data';
663
664
    // Build form.
665
    $source = feeds_source($importer_id, empty($form['#node']->nid) ? 0 : $form['#node']->nid);
666
    $form['feeds'] = array(
667
      '#type' => 'fieldset',
668
      '#title' => t('Feed'),
669
      '#tree' => TRUE,
670
      '#weight' => 0,
671
    );
672
    $form['feeds'] += $source->configForm($form_state);
673
    $form['#feed_id'] = $importer_id;
674
  }
675
}
676
677
/**
678
 * Implements hook_field_extra_fields().
679
 */
680
function feeds_field_extra_fields() {
681
  $extras = array();
682
  foreach (node_type_get_names() as $type => $name) {
683
    if (feeds_get_importer_id($type)) {
684
      $extras['node'][$type]['form']['feeds'] = array(
685
        'label' => t('Feed'),
686
        'description' => t('Feeds module form elements'),
687
        'weight' => 0,
688
      );
689
    }
690
  }
691
  return $extras;
692
}
693
694
/**
695
 * @}
696
 */
697
698
/**
699
 * @defgroup utility Utility functions
700
 * @{
701
 */
702
703
/**
704
 * Loads all importers.
705
 *
706
 * @param $load_disabled
707
 *   Pass TRUE to load all importers, enabled or disabled, pass FALSE to only
708
 *   retrieve enabled importers.
709
 *
710
 * @return
711
 *   An array of all feed configurations available.
712
 */
713
function feeds_importer_load_all($load_disabled = FALSE) {
714
  $feeds = array();
715
  // This function can get called very early in install process through
716
  // menu_router_rebuild(). Do not try to include CTools if not available.
717
  if (function_exists('ctools_include')) {
718
    ctools_include('export');
719
    $configs = ctools_export_load_object('feeds_importer', 'all');
720
    foreach ($configs as $config) {
721
      if (!empty($config->id) && ($load_disabled || empty($config->disabled))) {
722
        $feeds[$config->id] = feeds_importer($config->id);
723
      }
724
    }
725
  }
726
  return $feeds;
727
}
728
729
/**
730
 * Gets an array of enabled importer ids.
731
 *
732
 * @return
733
 *   An array where the values contain ids of enabled importers.
734
 */
735
function feeds_enabled_importers() {
736
  return array_keys(_feeds_importer_digest());
737
}
738
739
/**
740
 * Gets an enabled importer configuration by content type.
741
 *
742
 * @param $content_type
743
 *   A node type string.
744
 *
745
 * @return
746
 *   A FeedsImporter id if there is an importer for the given content type,
747
 *   FALSE otherwise.
748
 */
749
function feeds_get_importer_id($content_type) {
750
  $importers = array_flip(_feeds_importer_digest());
751
  return isset($importers[$content_type]) ? $importers[$content_type] : FALSE;
752
}
753
754
/**
755
 * Helper function for feeds_get_importer_id() and feeds_enabled_importers().
756
 */
757
function _feeds_importer_digest() {
758
  $importers = &drupal_static(__FUNCTION__);
759
  if ($importers === NULL) {
760
    if ($cache = cache_get(__FUNCTION__)) {
761
      $importers = $cache->data;
762
    }
763
    else {
764
      $importers = array();
765
      foreach (feeds_importer_load_all() as $importer) {
766
        $importers[$importer->id] = isset($importer->config['content_type']) ? $importer->config['content_type'] : '';
767
      }
768
      cache_set(__FUNCTION__, $importers);
769
    }
770
  }
771
  return $importers;
772
}
773
774
/**
775
 * Resets importer caches. Call when enabling/disabling importers.
776
 */
777
function feeds_cache_clear($rebuild_menu = TRUE) {
778
  cache_clear_all('_feeds_importer_digest', 'cache');
779
  drupal_static_reset('_feeds_importer_digest');
780
  ctools_include('export');
781
  ctools_export_load_object_reset('feeds_importer');
782
  drupal_static_reset('_node_types_build');
783
  if ($rebuild_menu) {
784
    menu_rebuild();
785
  }
786
}
787
788
/**
789
 * Exports a FeedsImporter configuration to code.
790
 */
791
function feeds_export($importer_id, $indent = '') {
792
  ctools_include('export');
793
  $result = ctools_export_load_object('feeds_importer', 'names', array('id' => $importer_id));
794
  if (isset($result[$importer_id])) {
795
    return ctools_export_object('feeds_importer', $result[$importer_id], $indent);
796
  }
797
}
798
799
/**
800
 * Logs to a file like /tmp/feeds_my_domain_org.log in temporary directory.
801
 */
802
function feeds_dbg($msg) {
803
  if (variable_get('feeds_debug', FALSE)) {
804
    if (!is_string($msg)) {
805
      $msg = var_export($msg, TRUE);
806
    }
807
    $filename = trim(str_replace('/', '_', $_SERVER['HTTP_HOST'] . base_path()), '_');
808
    $handle = fopen("temporary://feeds_$filename.log", 'a');
809
    fwrite($handle, gmdate('c') . "\t$msg\n");
810
    fclose($handle);
811
  }
812
}
813
814
/**
815
 * Writes to feeds log.
816
 */
817
function feeds_log($importer_id, $feed_nid, $type, $message, $variables = array(), $severity = WATCHDOG_NOTICE) {
818
  if ($severity < WATCHDOG_NOTICE) {
819
    $error = &drupal_static('feeds_log_error', FALSE);
820
    $error = TRUE;
821
  }
822
  db_insert('feeds_log')
823
    ->fields(array(
824
      'id' => $importer_id,
825
      'feed_nid' => $feed_nid,
826
      'log_time' => time(),
827
      'request_time' => REQUEST_TIME,
828
      'type' => $type,
829
      'message' => $message,
830
      'variables' => serialize($variables),
831
      'severity' => $severity,
832
    ))
833
    ->execute();
834
}
835
836
/**
837
 * Loads an item info object.
838
 *
839
 * Example usage:
840
 *
841
 * $info = feeds_item_info_load('node', $node->nid);
842
 */
843
function feeds_item_info_load($entity_type, $entity_id) {
844
  return db_select('feeds_item')
845
    ->fields('feeds_item')
846
    ->condition('entity_type', $entity_type)
847
    ->condition('entity_id', $entity_id)
848
    ->execute()
849
    ->fetchObject();
850
}
851
852
/**
853
 * Inserts an item info object into the feeds_item table.
854
 */
855
function feeds_item_info_insert($entity, $entity_id) {
856
  if (isset($entity->feeds_item)) {
857
    $entity->feeds_item->entity_id = $entity_id;
858
    drupal_write_record('feeds_item', $entity->feeds_item);
859
  }
860
}
861
862
/**
863
 * Inserts or updates an item info object in the feeds_item table.
864
 */
865
function feeds_item_info_save($entity, $entity_id) {
866
  if (isset($entity->feeds_item)) {
867
    $entity->feeds_item->entity_id = $entity_id;
868
    if (feeds_item_info_load($entity->feeds_item->entity_type, $entity_id)) {
869
      drupal_write_record('feeds_item', $entity->feeds_item, array('entity_type', 'entity_id'));
870
    }
871
    else {
872
      feeds_item_info_insert($entity, $entity_id);
873
    }
874
  }
875
}
876
877
/**
878
 * @}
879
 */
880
881
/**
882
 * @defgroup instantiators Instantiators
883
 * @{
884
 */
885
886
/**
887
 * Gets an importer instance.
888
 *
889
 * @param $id
890
 *   The unique id of the importer object.
891
 *
892
 * @return
893
 *   A FeedsImporter object or an object of a class defined by the Drupal
894
 *   variable 'feeds_importer_class'. There is only one importer object
895
 *   per $id system-wide.
896
 */
897
function feeds_importer($id) {
898
  return FeedsConfigurable::instance(variable_get('feeds_importer_class', 'FeedsImporter'), $id);
899
}
900
901
/**
902
 * Gets an instance of a source object.
903
 *
904
 * @param $importer_id
905
 *   A FeedsImporter id.
906
 * @param $feed_nid
907
 *   The node id of a feed node if the source is attached to a feed node.
908
 *
909
 * @return
910
 *   A FeedsSource object or an object of a class defiend by the Drupal
911
 *   variable 'source_class'.
912
 */
913
function feeds_source($importer_id, $feed_nid = 0) {
914
  return FeedsSource::instance($importer_id, $feed_nid);
915
}
916
917
/**
918
 * Gets an instance of a class for a given plugin and id.
919
 *
920
 * @param $plugin
921
 *   A string that is the key of the plugin to load.
922
 * @param $id
923
 *   A string that is the id of the object.
924
 *
925
 * @return
926
 *   A FeedsPlugin object.
927
 *
928
 * @throws Exception
929
 *   If plugin can't be instantiated.
930
 */
931
function feeds_plugin($plugin, $id) {
932
  ctools_include('plugins');
933
  if ($class = ctools_plugin_load_class('feeds', 'plugins', $plugin, 'handler')) {
934
    return FeedsConfigurable::instance($class, $id);
935
  }
936
  $args = array('%plugin' => $plugin, '@id' => $id);
937
  if (user_access('administer feeds')) {
938
    $args['@link'] = url('admin/structure/feeds/' . $id);
939
    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);
940
  }
941
  else {
942
    drupal_set_message(t('Missing Feeds plugin %plugin. Please contact your site administrator.', $args), 'warning', FALSE);
943
  }
944
  $class = ctools_plugin_load_class('feeds', 'plugins', 'FeedsMissingPlugin', 'handler');
945
  return FeedsConfigurable::instance($class, $id);
946
}
947
948
/**
949
 * @}
950
 */
951
952
/**
953
 * @defgroup include Funtions for loading libraries
954
 * @{
955
 */
956
957
/**
958
 * Includes a library file.
959
 *
960
 * @param $file
961
 *   The filename to load from.
962
 * @param $library
963
 *   The name of the library. If libraries module is installed,
964
 *   feeds_include_library() will look for libraries with this name managed by
965
 *   libraries module.
966
 */
967
function feeds_include_library($file, $library) {
968
  static $included = array();
969
  static $ignore_deprecated = array('simplepie');
970
971
  if (!isset($included[$file])) {
972
    // Disable deprecated warning for libraries known for throwing them
973
    if (in_array($library, $ignore_deprecated)) {
974
       $level = error_reporting();
975
       // We can safely use E_DEPRECATED since Drupal 7 requires PHP 5.3+
976
       error_reporting($level ^ E_DEPRECATED ^ E_STRICT);
977
    }
978
979
    $library_dir = variable_get('feeds_library_dir', FALSE);
980
    $feeds_library_path = DRUPAL_ROOT . '/' . drupal_get_path('module', 'feeds') . "/libraries/$file";
981
982
    // Try first whether libraries module is present and load the file from
983
    // there. If this fails, require the library from the local path.
984
    if (module_exists('libraries') && file_exists(libraries_get_path($library) . "/$file")) {
985
      require libraries_get_path($library) . "/$file";
986
      $included[$file] = TRUE;
987
    }
988
    elseif ($library_dir && file_exists("$library_dir/$library/$file")) {
989
      require "$library_dir/$library/$file";
990
      $included[$file] = TRUE;
991
    }
992
    elseif (file_exists($feeds_library_path)) {
993
      // @todo: Throws "Deprecated function: Assigning the return value of new
994
      // by reference is deprecated."
995
      require $feeds_library_path;
996
      $included[$file] = TRUE;
997
    }
998
    // Restore error reporting level
999
    if (isset($level)) {
1000
      error_reporting($level);
1001
    }
1002
  }
1003
  if (isset($included[$file])) {
1004
    return TRUE;
1005
  }
1006
  return FALSE;
1007
}
1008
1009
/**
1010
 * Checks whether a library is present.
1011
 *
1012
 * @param $file
1013
 *   The filename to load from.
1014
 * @param $library
1015
 *   The name of the library. If libraries module is installed,
1016
 *   feeds_library_exists() will look for libraries with this name managed by
1017
 *   libraries module.
1018
 */
1019
function feeds_library_exists($file, $library) {
1020
1021
  if (module_exists('libraries') && file_exists(libraries_get_path($library) . "/$file")) {
1022
    return TRUE;
1023
  }
1024
1025
  elseif (file_exists(DRUPAL_ROOT . '/' . drupal_get_path('module', 'feeds') . "/libraries/$file")) {
1026
    return TRUE;
1027
  }
1028
1029
  elseif ($library_dir = variable_get('feeds_library_dir', FALSE)) {
1030
    if (file_exists("$library_dir/$library/$file")) {
1031
      return TRUE;
1032
    }
1033
  }
1034
1035
  return FALSE;
1036
}
1037
1038
 /**
1039
 * Checks whether simplepie exists.
1040
 */
1041
function feeds_simplepie_exists() {
1042
  return (feeds_library_exists('simplepie.compiled.php', 'simplepie') ||
1043
    feeds_library_exists('simplepie.mini.php', 'simplepie') ||
1044
    feeds_library_exists('simplepie.inc', 'simplepie')
1045
  );
1046
}
1047
1048
/**
1049
 * Includes the simplepie library.
1050
 */
1051
function feeds_include_simplepie() {
1052
  $files = array('simplepie.mini.php', 'simplepie.compiled.php', 'simplepie.inc');
1053
1054
  foreach ($files as $file) {
1055
    if (feeds_include_library($file, 'simplepie')) {
1056
      return TRUE;
1057
    }
1058
  }
1059
  return FALSE;
1060
}
1061
1062
/**
1063
 * @deprecated
1064
 *
1065
 * Simplified drupal_alter().
1066
 *
1067
 * - None of that 'multiple parameters by ref' crazyness.
1068
 * - Don't use module_implements() to allow hot including on behalf
1069
 *   implementations (see mappers/).
1070
 *
1071
 * @todo This needs to be removed and drupal_alter() used. This is crazy dumb.
1072
 */
1073
function feeds_alter($type, &$data) {
1074
  $args = array(&$data);
1075
  $additional_args = func_get_args();
1076
  array_shift($additional_args);
1077
  array_shift($additional_args);
1078
  $args = array_merge($args, $additional_args);
1079
1080
  $hook = $type . '_alter';
1081
  foreach (module_list() as $module) {
1082
    if (module_hook($module, $hook)) {
1083
      call_user_func_array($module . '_' . $hook, $args);
1084
    }
1085
  }
1086
}
1087
1088
/**
1089
 * @}
1090
 */
1091
1092
/**
1093
 * Copy of valid_url() that supports the webcal scheme.
1094
 *
1095
 * @see valid_url().
1096
 *
1097
 * @todo Replace with valid_url() when http://drupal.org/node/295021 is fixed.
1098
 */
1099
function feeds_valid_url($url, $absolute = FALSE) {
1100
  if ($absolute) {
1101
    return (bool) preg_match("
1102
      /^                                                      # Start at the beginning of the text
1103
      (?:ftp|https?|feed|webcal):\/\/                         # Look for ftp, http, https, feed or webcal schemes
1104
      (?:                                                     # Userinfo (optional) which is typically
1105
        (?:(?:[\w\.\-\+!$&'\(\)*\+,;=]|%[0-9a-f]{2})+:)*      # a username or a username and password
1106
        (?:[\w\.\-\+%!$&'\(\)*\+,;=]|%[0-9a-f]{2})+@          # combination
1107
      )?
1108
      (?:
1109
        (?:[a-z0-9\-\.]|%[0-9a-f]{2})+                        # A domain name or a IPv4 address
1110
        |(?:\[(?:[0-9a-f]{0,4}:)*(?:[0-9a-f]{0,4})\])         # or a well formed IPv6 address
1111
      )
1112
      (?::[0-9]+)?                                            # Server port number (optional)
1113
      (?:[\/|\?]
1114
        (?:[|\w#!:\.\?\+=&@$'~*,;\/\(\)\[\]\-]|%[0-9a-f]{2})   # The path and query (optional)
1115
      *)?
1116
    $/xi", $url);
1117
  }
1118
  else {
1119
    return (bool) preg_match("/^(?:[\w#!:\.\?\+=&@$'~*,;\/\(\)\[\]\-]|%[0-9a-f]{2})+$/i", $url);
1120
  }
1121
}
1122
1123
/**
1124
 * Registers a feed subscription job for execution on feeds_exit().
1125
 *
1126
 * @param array $job
1127
 *   Information about a new job to queue; or if set to NULL (default), leaves
1128
 *   the current queued jobs unchanged.
1129
 *
1130
 * @return
1131
 *   An array of subscribe jobs to process.
1132
 *
1133
 * @see feeds_exit()
1134
 * @see feeds_get_subscription_jobs()
1135
 */
1136
function feeds_set_subscription_job(array $job = NULL) {
1137
  $jobs = &drupal_static(__FUNCTION__, array());
1138
  if (isset($job)) {
1139
    $jobs[] = $job;
1140
  }
1141
  return $jobs;
1142
}
1143
1144
/**
1145
 * Returns the list of queued jobs to be run.
1146
 *
1147
 * @return
1148
 *   An array of subscribe jobs to process.
1149
 *
1150
 * @see feeds_set_subscription_job()
1151
 */
1152
function feeds_get_subscription_jobs() {
1153
  return feeds_set_subscription_job();
1154
}
1155
1156
/**
1157
 * Implements hook_entity_property_info_alter().
1158
 */
1159
function feeds_entity_property_info_alter(&$info) {
1160
  // Gather entities supported by Feeds processors.
1161
  $processors = FeedsPlugin::byType('processor');
1162
  $supported_entities = array();
1163
  foreach ($processors as $processor) {
1164
    $instance = feeds_plugin($processor['handler']['class'], '__none__');
1165
    if (method_exists($instance, 'entityType')) {
1166
      $supported_entities[] = $instance->entityType();
1167
    }
1168
  }
1169
  // Feeds processors can fake the entity info. Only set the property for
1170
  // defined entities.
1171
  $supported_entities = array_intersect(array_keys($info), $supported_entities);
1172
1173
  foreach ($supported_entities as $entity_type) {
1174
    $info[$entity_type]['properties']['feed_nid'] = array(
1175
      'label' => 'Feed NID',
1176
      'type' => 'integer',
1177
      'description' => t('Nid of the Feed Node that imported this entity.'),
1178
      'getter callback' => 'feeds_get_feed_nid_entity_callback',
1179
    );
1180
  }
1181
}
1182
1183
/**
1184
 * Gets the feed_nid for an entity for use in entity metadata.
1185
 */
1186
function feeds_get_feed_nid_entity_callback($entity, array $options, $name, $entity_type) {
1187
  list($entity_id, , ) = entity_extract_ids($entity_type, $entity);
1188
1189
  $feed_nid = feeds_get_feed_nid($entity_id, $entity_type);
1190
1191
  if ($feed_nid === FALSE) {
1192
    return NULL;
1193
  }
1194
  return $feed_nid;
1195
}
1196
1197
/**
1198
 * Implements hook_file_download().
1199
 */
1200
function feeds_file_download($uri) {
1201
  $id = db_query("SELECT id FROM {feeds_source} WHERE source = :uri", array(':uri' => $uri))->fetchField();
1202
1203
  if (!$id) {
1204
    // File is not associated with a feed.
1205
    return;
1206
  }
1207
1208
   // Get the file record based on the URI. If not in the database just return.
1209
  $files = file_load_multiple(array(), array('uri' => $uri));
1210
  foreach ($files as $item) {
1211
    // Since some database servers sometimes use a case-insensitive comparison
1212
    // by default, double check that the filename is an exact match.
1213
    if ($item->uri === $uri) {
1214
      $file = $item;
1215
      break;
1216
    }
1217
  }
1218
  if (!isset($file)) {
1219
    return;
1220
  }
1221
1222
  // Check if this file belongs to Feeds.
1223
  $usage_list = file_usage_list($file);
1224
  if (!isset($usage_list['feeds'])) {
1225
    return;
1226
  }
1227
1228
  if (!feeds_access('import', $id)) {
1229
    // User does not have permission to import this feed.
1230
    return -1;
1231
  }
1232
1233
  // Return file headers.
1234
  return file_get_content_headers($file);
1235
}
1236
1237
/**
1238
 * Feeds API version.
1239
 */
1240
function feeds_api_version() {
1241
  $version = feeds_ctools_plugin_api('feeds', 'plugins');
1242
  return $version['version'];
1243
}