summaryrefslogtreecommitdiffstats
path: root/feeds.module
blob: a73e0615f485c3b0e341851fd68822db6da3d5ff (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
<?php

/**
 * @file
 * Feeds - basic API functions and hook implementations.
 */

// Common request time, use as point of reference and to avoid calls to time().
define('FEEDS_REQUEST_TIME', time());
// Do not schedule a feed for refresh.
define('FEEDS_SCHEDULE_NEVER', -1);
// Never expire feed items.
define('FEEDS_EXPIRE_NEVER', -1);
// An object that is not persistent. Compare EXPORT_IN_DATABASE, EXPORT_IN_CODE.
define('FEEDS_EXPORT_NONE', 0x0);
// Status of batched operations.
define('FEEDS_BATCH_COMPLETE', 1);
define('FEEDS_BATCH_ACTIVE', 0);

/**
 * @defgroup hooks Hook and callback implementations
 * @{
 */

/**
 * Implements hook_cron().
 */
function feeds_cron() {
  if ($importers = feeds_reschedule()) {
    foreach ($importers as $id) {
      feeds_importer($id)->schedule();
      $result = db_query("SELECT feed_nid FROM {feeds_source} WHERE id = '%s'", $id);
      while ($row = db_fetch_object($result)) {
        feeds_source($id, $row->feed_nid)->schedule();
      }
    }
    feeds_reschedule(FALSE);
    return;
  }
}

/**
 * Implementation of hook_cron_queue_info().
 *
 * Invoked by drupal_queue module if present.
 */
function feeds_cron_queue_info() {
  $queues = array();
  $queues['feeds_source_import'] = array(
    'worker callback' => 'feeds_source_import',
    'time' => variable_get('feeds_worker_time', 15),
  );
  $queues['feeds_importer_expire'] = array(
    'worker callback' => 'feeds_importer_expire',
    'time' => variable_get('feeds_worker_time', 15),
  );
  return $queues;
}

/**
 * Scheduler callback for importing from a source.
 */
function feeds_source_import($job) {
  $source = feeds_source($job['type'], $job['id']);
  try {
    $source->existing()->import();
  }
  catch (FeedsNotExistingException $e) {
    // Do nothing.
  }
  catch (Exception $e) {
    watchdog('feeds_source_import()', $e->getMessage(), array(), WATCHDOG_ERROR);
  }
  $source->schedule();
}

/**
 * Scheduler callback for expiring content.
 */
function feeds_importer_expire($job) {
  $importer = feeds_importer($job['type']);
  try {
    $importer->existing()->expire();
  }
  catch (FeedsNotExistingException $e) {
    // Do nothing.
  }
  catch (Exception $e) {
    watchdog('feeds_importer_expire()', $e->getMessage(), array(), WATCHDOG_ERROR);
  }
  $importer->schedule();
}

/**
 * Reschedule one or all importers.
 *
 * Note: variable_set('feeds_reschedule', TRUE) is used in update hook
 * feeds_update_6013() and as such must be maintained as part of the upgrade
 * path from pre 6.x 1.0 beta 6 versions of Feeds.
 *
 * @param $importer_id
 *   If TRUE, all importers will be rescheduled, if FALSE, no importers will
 *   be rescheduled, if an importer id, only importer of that id will be
 *   rescheduled.
 *
 * @return
 *   TRUE if all importers need rescheduling. FALSE if no rescheduling is
 *   required. An array of importers that need rescheduling.
 */
function feeds_reschedule($importer_id = NULL) {
  $reschedule = variable_get('feeds_reschedule', FALSE);
  if ($importer_id === TRUE || $importer_id === FALSE) {
    $reschedule = $importer_id;
  }
  elseif (is_string($importer_id) && $reschedule !== TRUE) {
    $reschedule = is_array($reschedule) ? $reschedule : array();
    $reschedule[$importer_id] = $importer_id;
  }
  variable_set('feeds_reschedule', $reschedule);
  if ($reschedule === TRUE) {
    return feeds_enabled_importers();
  }
  return $reschedule;
}

/**
 * Implementation of hook_perm().
 */
function feeds_perm() {
  $perms = array('administer feeds');
  foreach (feeds_importer_load_all() as $importer) {
    $perms[] = 'import '. $importer->id .' feeds';
    $perms[] = 'clear '. $importer->id .' feeds';
  }
  return $perms;
}

/**
 * Implementation of hook_forms().
 *
 * Declare form callbacks for all known classes derived from FeedsConfigurable.
 */
function feeds_forms() {
  $forms = array();
  $forms['FeedsImporter_feeds_form']['callback'] = 'feeds_form';
  $plugins = feeds_get_plugins();
  foreach ($plugins as $plugin) {
    $forms[$plugin['handler']['class'] .'_feeds_form']['callback'] = 'feeds_form';
  }
  return $forms;
}

/**
 * Implementation of hook_menu().
 */
function feeds_menu() {
  // Register a callback for all feed configurations that are not attached to a content type.
  $items = array();
  foreach (feeds_importer_load_all() as $importer) {
    if (empty($importer->config['content_type'])) {
      $items['import/'. $importer->id] = array(
        'title' => $importer->config['name'],
        'page callback' => 'drupal_get_form',
        'page arguments' => array('feeds_import_form', 1),
        'access callback' => 'feeds_access',
        'access arguments' => array('import', $importer->id),
        'file' => 'feeds.pages.inc',
      );
      $items['import/'. $importer->id .'/import'] = array(
        'title' => 'Import',
        'type' => MENU_DEFAULT_LOCAL_TASK,
        'weight' => -10,
      );
      $items['import/'. $importer->id .'/delete-items'] = array(
        'title' => 'Delete items',
        'page callback' => 'drupal_get_form',
        'page arguments' => array('feeds_delete_tab_form', 1),
        'access callback' => 'feeds_access',
        'access arguments' => array('clear', $importer->id),
        'file' => 'feeds.pages.inc',
        'type' => MENU_LOCAL_TASK,
      );
    }
    else {
      $items['node/%node/import'] = array(
        'title' => 'Import',
        'page callback' => 'drupal_get_form',
        'page arguments' => array('feeds_import_tab_form', 1),
        'access callback' => 'feeds_access',
        'access arguments' => array('import', 1),
        'file' => 'feeds.pages.inc',
        'type' => MENU_LOCAL_TASK,
        'weight' => 10,
      );
      $items['node/%node/delete-items'] = array(
        'title' => 'Delete items',
        'page callback' => 'drupal_get_form',
        'page arguments' => array('feeds_delete_tab_form', NULL, 1),
        'access callback' => 'feeds_access',
        'access arguments' => array('clear', 1),
        'file' => 'feeds.pages.inc',
        'type' => MENU_LOCAL_TASK,
        'weight' => 11,
      );
    }
    $items += $importer->fetcher->menuItem();
  }
  if (count($items)) {
    $items['import'] = array(
      'title' => 'Import',
      'page callback' => 'feeds_page',
      'access callback' => 'feeds_page_access',
      'file' => 'feeds.pages.inc',
    );
  }
  return $items;
}

/**
 * Menu loader callback.
 */
function feeds_importer_load($id) {
  return feeds_importer($id);
}

/**
 * Implementation of hook_theme().
 */
function feeds_theme() {
  return array(
    'feeds_upload' => array(
      'file' => 'feeds.pages.inc',
    ),
  );
}

/**
 * Menu access callback.
 *
 * @param $action
 *   The action to be performed. Possible values are:
 *   - import
 *   - clear
 * @param $param
 *   Node object or FeedsImporter id.
 */
function feeds_access($action, $param) {
  if (!in_array($action, array('import', 'clear'))) {
    // If $action is not one of the supported actions, we return access denied.
    return FALSE;
  }

  if (is_string($param)) {
    $importer_id = $param;
  }
  elseif ($param->type) {
    $importer_id = feeds_get_importer_id($param->type);
  }

  // Check for permissions if feed id is present, otherwise return FALSE.
  if ($importer_id) {
    if (user_access('administer feeds') || user_access($action .' '. $importer_id .' feeds')) {
      return TRUE;
    }
  }
  return FALSE;
}

/**
 * Menu access callback.
 */
function feeds_page_access() {
  if (user_access('administer feeds')) {
    return TRUE;
  }
  foreach (feeds_enabled_importers() as $id) {
    if (user_access("import $id feeds")) {
      return TRUE;
    }
  }
  return FALSE;
}

/**
 * Implementation of hook_views_api().
 */
function feeds_views_api() {
  return array(
    'api' => '2.0',
    'path' => drupal_get_path('module', 'feeds') .'/views',
  );
}

/**
 * Implementation of hook_ctools_plugin_api().
 */
function feeds_ctools_plugin_api($owner, $api) {
  if ($owner == 'feeds' && $api == 'plugins') {
    return array('version' => 1);
  }
}

/**
 * Implementation of hook_ctools_plugin_plugins().
 *
 * Psuedo hook defintion plugin system options and defaults.
 */
function feeds_ctools_plugin_plugins() {
  return array(
    'cache' => TRUE,
    'use hooks' => TRUE,
  );
}

/**
 * Implementation of hook_feeds_plugins().
 */
function feeds_feeds_plugins() {
  module_load_include('inc', 'feeds', 'feeds.plugins');
  return _feeds_feeds_plugins();
}

/**
 * Implementation of hook_nodeapi().
 *
 * @todo For Drupal 7, revisit static cache based shuttling of values between
 *   'validate' and 'update'/'insert'.
 */
function feeds_nodeapi(&$node, $op, $form) {

  // $node looses any changes after 'validate' stage (see node_form_validate()).
  // Keep a copy of title and feeds array between 'validate' and subsequent
  // stages. This allows for automatically populating the title of the node form
  // and modifying the $form['feeds'] array on node validation just like on the
  // standalone form.
  static $last_title;
  static $node_feeds;

  // Break out node processor related nodeapi functionality.
  _feeds_nodeapi_node_processor($node, $op);

  if ($importer_id = feeds_get_importer_id($node->type)) {
    switch ($op) {
      case 'validate':
        // On validation stage we are working with a FeedsSource object that is
        // not tied to a nid - when creating a new node there is no
        // $node->nid at this stage.
        $source = feeds_source($importer_id);

        // Node module magically moved $form['feeds'] to $node->feeds :P
        $node_feeds = $node->feeds;
        $source->configFormValidate($node_feeds);

        // If node title is empty, try to retrieve title from feed.
        if (trim($node->title) == '') {
          try {
            $source->addConfig($node_feeds);
            if (!$last_title = $source->preview()->getTitle()) {
              throw new Exception();
            }
          }
          catch (Exception $e) {
            drupal_set_message($e->getMessage(), 'error');
            form_set_error('title', t('Could not retrieve title from feed.'));
          }
        }
        break;
      case 'presave':
        if (!empty($last_title)) {
          $node->title = $last_title;
        }
        $last_title = NULL;
        break;
      case 'insert':
      case 'update':
        // A node may not have been validated, make sure $node_feeds is present.
        if (empty($node_feeds)) {
          // If $node->feeds is empty here, nodes are being programatically
          // created in some fashion without Feeds stuff being added.
          if (empty($node->feeds)) {
            return;
          }
          $node_feeds = $node->feeds;
        }
        // Add configuration to feed source and save.
        $source = feeds_source($importer_id, $node->nid);
        $source->addConfig($node_feeds);
        $source->save();

        // Refresh feed if import on create is selected and suppress_import is
        // not set.
        if ($op == 'insert' && feeds_importer($importer_id)->config['import_on_create'] && !isset($node_feeds['suppress_import'])) {
          feeds_batch_set(t('Importing'), 'import', $importer_id, $node->nid);
        }
        // Add source to schedule, make sure importer is scheduled, too.
        if ($op == 'insert') {
          $source->schedule();
          $source->importer->schedule();
        }
        $node_feeds = NULL;
        break;
      case 'delete':
        $source = feeds_source($importer_id, $node->nid);
        if (!empty($source->importer->processor->config['delete_with_source'])) {
          feeds_batch_set(t('Deleting'), 'clear', $importer_id, $node->nid);
        }
        // Remove attached source.
        $source->delete();
        break;
    }
  }
}

/**
 * Handles FeedsNodeProcessor specific nodeapi operations.
 */
function _feeds_nodeapi_node_processor($node, $op) {
  switch ($op) {
    case 'load':
      if ($result = db_fetch_object(db_query("SELECT imported, guid, url, feed_nid FROM {feeds_node_item} WHERE nid = %d", $node->nid))) {
        $node->feeds_node_item = $result;
      }
      break;
    case 'insert':
      if (isset($node->feeds_node_item)) {
        $node->feeds_node_item->nid = $node->nid;
        drupal_write_record('feeds_node_item', $node->feeds_node_item);
      }
      break;
    case 'update':
      if (isset($node->feeds_node_item)) {
        $node->feeds_node_item->nid = $node->nid;
        drupal_write_record('feeds_node_item', $node->feeds_node_item, 'nid');
      }
      break;
    case 'delete':
      db_query("DELETE FROM {feeds_node_item} WHERE nid = %d", $node->nid);
      break;
  }
}

/**
 * Implementation of hook_taxonomy().
 */
function feeds_taxonomy($op = NULL, $type = NULL, $term = NULL) {
  if ($type == 'term' && !empty($term['tid'])) {
    switch ($op) {
      case 'delete':
        db_query("DELETE FROM {feeds_term_item} WHERE tid = %d", $term['tid']);
        break;
      case 'update':
        if (isset($term['feeds_term_item'])) {
          db_query("DELETE FROM {feeds_term_item} WHERE tid = %d", $term['tid']);
        }
      case 'insert':
        if (isset($term['feeds_term_item'])) {
          $term['feeds_term_item']['tid'] = $term['tid'];
          drupal_write_record('feeds_term_item', $term['feeds_term_item']);
        }
        break;
    }
  }
}

/**
 * Implements hook_form_alter().
 */
function feeds_form_alter(&$form, $form_state, $form_id) {
  if (isset($form['#node']->type) && $form['#node']->type . '_node_form' == $form_id) {
    if ($importer_id = feeds_get_importer_id($form['#node']->type)) {
      // Set title to not required, try to retrieve it from feed.
      if (isset($form['title'])) {
        $form['title']['#required'] = FALSE;
      }

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

      // Build form.
      $source = feeds_source($importer_id, empty($form['#node']->nid) ? 0 : $form['#node']->nid);
      $form['feeds'] = array(
        '#type' => 'fieldset',
        '#title' => t('Feed'),
        '#tree' => TRUE,
        '#weight' => 0,
      );
      $form['feeds'] += $source->configForm($form_state);
      $form['#feed_id'] = $importer_id;
    }
  }
}

/**
 * Implements hook_content_extra_fields().
 */
function feeds_content_extra_fields($type) {
  $extras = array();
  if (feeds_get_importer_id($type)) {
    $extras['feeds'] = array(
      'label' => t('Feed'),
      'description' => t('Feeds module form elements'),
      'weight' => 0,
    );
  }
  return $extras;
}

/**
 * @}
 */

/**
 * @defgroup batch Batch functions
 */

/**
 * Batch helper.
 *
 * @param $title
 *   Title to show to user when executing batch.
 * @param $method
 *   Method to execute on importer; one of 'import', 'clear' or 'expire'.
 * @param $importer_id
 *   Identifier of a FeedsImporter object.
 * @param $feed_nid
 *   If importer is attached to content type, feed node id identifying the
 *   source to be imported.
 */
function feeds_batch_set($title, $method, $importer_id, $feed_nid = 0) {
  $batch = array(
    'title' => $title,
    'operations' => array(
      array('feeds_batch', array($method, $importer_id, $feed_nid)),
    ),
    'progress_message' => '',
  );
  batch_set($batch);
}

/**
 * Batch callback.
 *
 * @param $method
 *   Method to execute on importer; one of 'import' or 'clear'.
 * @param $importer_id
 *   Identifier of a FeedsImporter object.
 * @param $feed_nid
 *   If importer is attached to content type, feed node id identifying the
 *   source to be imported.
 * @param $context
 *   Batch context.
 */
function feeds_batch($method, $importer_id, $feed_nid = 0, &$context) {
  $context['finished'] = 1;
  try {
    $context['finished'] = feeds_source($importer_id, $feed_nid)->$method();
  }
  catch (Exception $e) {
    drupal_set_message($e->getMessage(), 'error');
  }
}

/**
 * @}
 */

/**
 * @defgroup utility Utility functions
 * @{
 */

/**
 * Loads all importers.
 *
 * @param $load_disabled
 *   Pass TRUE to load all importers, enabled or disabled, pass FALSE to only
 *   retrieve enabled importers.
 *
 * @return
 *   An array of all feed configurations available.
 */
function feeds_importer_load_all($load_disabled = FALSE) {
  $feeds = array();
  // This function can get called very early in install process through
  // menu_router_rebuild(). Do not try to include CTools if not available.
  if (function_exists('ctools_include')) {
    ctools_include('export');
    $configs = ctools_export_load_object('feeds_importer', 'all');
    foreach ($configs as $config) {
      if (!empty($config->id) && ($load_disabled || empty($config->disabled))) {
        $feeds[$config->id] = feeds_importer($config->id);
      }
    }
  }
  return $feeds;
}

/**
 * Gets an array of enabled importer ids.
 *
 * @return
 *   An array where the values contain ids of enabled importers.
 */
function feeds_enabled_importers() {
  return array_keys(_feeds_importer_digest());
}

/**
 * Gets an enabled importer configuration by content type.
 *
 * @param $content_type
 *   A node type string.
 *
 * @return
 *   A FeedsImporter id if there is an importer for the given content type,
 *   FALSE otherwise.
 */
function feeds_get_importer_id($content_type) {
  $importers = array_flip(_feeds_importer_digest());
  return isset($importers[$content_type]) ? $importers[$content_type] : FALSE;
}

/**
 * Helper function for feeds_get_importer_id() and feeds_enabled_importers().
 */
function _feeds_importer_digest() {
  $importers = &ctools_static(__FUNCTION__);
  if ($importers === NULL) {
    if ($cache = cache_get(__FUNCTION__)) {
      $importers = $cache->data;
    }
    else {
      $importers = array();
      foreach (feeds_importer_load_all() as $importer) {
        $importers[$importer->id] = isset($importer->config['content_type']) ? $importer->config['content_type'] : '';
      }
      cache_set(__FUNCTION__, $importers);
    }
  }
  return $importers;
}

/**
 * Resets importer caches. Call when enabling/disabling importers.
 */
function feeds_cache_clear($rebuild_menu = TRUE) {
  cache_clear_all('_feeds_importer_digest', 'cache');
  ctools_static_reset('_feeds_importer_digest');
  ctools_include('export');
  ctools_export_load_object_reset('feeds_importer');
  node_get_types('types', NULL, TRUE);
  if ($rebuild_menu) {
    menu_rebuild();
  }
}

/**
 * Exports a FeedsImporter configuration to code.
 */
function feeds_export($importer_id, $indent = '') {
  ctools_include('export');
  $result = ctools_export_load_object('feeds_importer', 'names', array('id' => $importer_id));
  if (isset($result[$importer_id])) {
    return ctools_export_object('feeds_importer', $result[$importer_id], $indent);
  }
}

/**
 * Logs to a file like /mytmp/feeds_my_domain_org.log in temporary directory.
 */
function feeds_dbg($msg) {
  if (variable_get('feeds_debug', FALSE)) {
    if (!is_string($msg)) {
      $msg = var_export($msg, TRUE);
    }
    $filename = trim(str_replace('/', '_', $_SERVER['HTTP_HOST'] . base_path()), '_');
    $handle = fopen(file_directory_temp() ."/feeds_$filename.log", 'a');
    fwrite($handle, date('c') ."\t$msg\n");
    fclose($handle);
  }
}

/**
 * @}
 */

/**
 * @defgroup instantiators Instantiators
 * @{
 */

/**
 * Gets an importer instance.
 *
 * @param $id
 *   The unique id of the importer object.
 *
 * @return
 *   A FeedsImporter object or an object of a class defined by the Drupal
 *   variable 'feeds_importer_class'. There is only one importer object
 *   per $id system-wide.
 */
function feeds_importer($id) {
  feeds_include('FeedsImporter');
  return FeedsConfigurable::instance(variable_get('feeds_importer_class', 'FeedsImporter'), $id);
}

/**
 * Gets an instance of a source object.
 *
 * @param $importer_id
 *   A FeedsImporter id.
 * @param $feed_nid
 *   The node id of a feed node if the source is attached to a feed node.
 *
 * @return
 *   A FeedsSource object or an object of a class defiend by the Drupal
 *   variable 'source_class'.
 */
function feeds_source($importer_id, $feed_nid = 0) {
  feeds_include('FeedsImporter');
  return FeedsSource::instance($importer_id, $feed_nid);
}

/**
 * @}
 */

/**
 * @defgroup plugins Plugin functions
 * @{
 *
 * @todo Encapsulate this in a FeedsPluginHandler class, move it to includes/
 * and only load it if we're manipulating plugins.
 */

/**
 * Gets all available plugins. Does not list hidden plugins.
 *
 * @return
 *   An array where the keys are the plugin keys and the values
 *   are the plugin info arrays as defined in hook_feeds_plugins().
 */
function feeds_get_plugins() {
  ctools_include('plugins');
  $plugins = ctools_get_plugins('feeds', 'plugins');

  $result = array();
  foreach ($plugins as $key => $info) {
    if (!empty($info['hidden'])) {
      continue;
    }
    $result[$key] = $info;
  }

  // Sort plugins by name and return.
  uasort($result, 'feeds_plugin_compare');
  return $result;
}

/**
 * Sort callback for feeds_get_plugins().
 */
function feeds_plugin_compare($a, $b) {
  return strcasecmp($a['name'], $b['name']);
}

/**
 * Gets all available plugins of a particular type.
 *
 * @param $type
 *   'fetcher', 'parser' or 'processor'
 */
function feeds_get_plugins_by_type($type) {
  $plugins = feeds_get_plugins();

  $result = array();
  foreach ($plugins as $key => $info) {
    if ($type == feeds_plugin_type($key)) {
      $result[$key] = $info;
    }
  }
  return $result;
}

/**
 * Gets an instance of a class for a given plugin and id.
 *
 * @param $plugin
 *   A string that is the key of the plugin to load.
 * @param $id
 *   A string that is the id of the object.
 *
 * @return
 *   A FeedsPlugin object.
 *
 * @throws Exception
 *   If plugin can't be instantiated.
 */
function feeds_plugin_instance($plugin, $id) {
  feeds_include('FeedsImporter');
  ctools_include('plugins');
  if ($class = ctools_plugin_load_class('feeds', 'plugins', $plugin, 'handler')) {
    return FeedsConfigurable::instance($class, $id);
  }
  $args = array( '%plugin' => $plugin, '@id' => $id);
  if (user_access('administer feeds')) {
    $args['@link'] = url('admin/build/feeds/edit/' . $id);
    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);
  }
  else {
    drupal_set_message(t('Missing Feeds plugin %plugin. Please contact your site administrator.', $args), 'warning', FALSE);
  }
  $class = ctools_plugin_load_class('feeds', 'plugins', 'FeedsMissingPlugin', 'handler');
  return FeedsConfigurable::instance($class, $id);
}

/**
 * Determines whether given plugin is derived from given base plugin.
 *
 * @param $plugin_key
 *   String that identifies a Feeds plugin key.
 * @param $parent_plugin
 *   String that identifies a Feeds plugin key to be tested against.
 *
 * @return
 *   TRUE if $parent_plugin is directly *or indirectly* a parent of $plugin,
 *   FALSE otherwise.
 */
function feeds_plugin_child($plugin_key, $parent_plugin) {
  ctools_include('plugins');
  $plugins = ctools_get_plugins('feeds', 'plugins');
  $info = $plugins[$plugin_key];

  if (empty($info['handler']['parent'])) {
    return FALSE;
  }
  elseif ($info['handler']['parent'] == $parent_plugin) {
    return TRUE;
  }
  else {
    return feeds_plugin_child($info['handler']['parent'], $parent_plugin);
  }
}

/**
 * Determines the type of a plugin.
 *
 * @param $plugin_key
 *   String that identifies a Feeds plugin key.
 *
 * @return
 *   One of the following values:
 *   'fetcher' if the plugin is a fetcher
 *   'parser' if the plugin is a parser
 *   'processor' if the plugin is a processor
 *   FALSE otherwise.
 */
function feeds_plugin_type($plugin_key) {
  if (feeds_plugin_child($plugin_key, 'FeedsFetcher')) {
    return 'fetcher';
  }
  elseif (feeds_plugin_child($plugin_key, 'FeedsParser')) {
    return 'parser';
  }
  elseif (feeds_plugin_child($plugin_key, 'FeedsProcessor')) {
    return 'processor';
  }
  return FALSE;
}

/**
 * @}
 */

/**
 * @defgroup include Funtions for loading libraries
 * @{
 */

/**
 * Includes a feeds module include file.
 *
 * @param $file
 *   The filename without the .inc extension.
 * @param $directory
 *   The directory to include the file from. Do not include files from libraries
 *   directory. Use feeds_include_library() instead
 */
function feeds_include($file, $directory = 'includes') {
  static $included = array();
  if (!isset($included[$file])) {
    require './'. drupal_get_path('module', 'feeds') ."/$directory/$file.inc";
  }
  $included[$file] = TRUE;
}

/**
 * Includes a library file.
 *
 * @param $file
 *   The filename to load from.
 * @param $library
 *   The name of the library. If libraries module is installed,
 *   feeds_include_library() will look for libraries with this name managed by
 *   libraries module.
 */
function feeds_include_library($file, $library) {
  static $included = array();
  if (!isset($included[$file])) {
    // Try first whether libraries module is present and load the file from
    // there. If this fails, require the library from the local path.
    if (module_exists('libraries') && file_exists(libraries_get_path($library) ."/$file")) {
      require libraries_get_path($library) ."/$file";
    }
    else {
      require './' . drupal_get_path('module', 'feeds') . "/libraries/$file";
    }
  }
  $included[$file] = TRUE;
}

/**
 * Checks whether a library is present.
 *
 * @param $file
 *   The filename to load from.
 * @param $library
 *   The name of the library. If libraries module is installed,
 *   feeds_library_exists() will look for libraries with this name managed by
 *   libraries module.
 */
function feeds_library_exists($file, $library) {
  if (module_exists('libraries') && file_exists(libraries_get_path($library) ."/$file")) {
    return TRUE;
  }
  elseif (file_exists('./' . drupal_get_path('module', 'feeds') . "/libraries/$file")) {
    return TRUE;
  }
  return FALSE;
}

/**
 * @}
 */

/**
 * Copy of valid_url() that supports the webcal scheme.
 *
 * @see valid_url().
 *
 * @todo Replace with valid_url() when http://drupal.org/node/295021 is fixed.
 */
function feeds_valid_url($url, $absolute = FALSE) {
  if ($absolute) {
    return (bool) preg_match("
      /^                                                      # Start at the beginning of the text
      (?:ftp|https?|feed|webcal):\/\/                         # Look for ftp, http, https, feed or webcal schemes
      (?:                                                     # Userinfo (optional) which is typically
        (?:(?:[\w\.\-\+!$&'\(\)*\+,;=]|%[0-9a-f]{2})+:)*      # a username or a username and password
        (?:[\w\.\-\+%!$&'\(\)*\+,;=]|%[0-9a-f]{2})+@          # combination
      )?
      (?:
        (?:[a-z0-9\-\.]|%[0-9a-f]{2})+                        # A domain name or a IPv4 address
        |(?:\[(?:[0-9a-f]{0,4}:)*(?:[0-9a-f]{0,4})\])         # or a well formed IPv6 address
      )
      (?::[0-9]+)?                                            # Server port number (optional)
      (?:[\/|\?]
        (?:[|\w#!:\.\?\+=&@$'~*,;\/\(\)\[\]\-]|%[0-9a-f]{2})   # The path and query (optional)
      *)?
    $/xi", $url);
  }
  else {
    return (bool) preg_match("/^(?:[\w#!:\.\?\+=&@$'~*,;\/\(\)\[\]\-]|%[0-9a-f]{2})+$/i", $url);
  }
}