-
Notifications
You must be signed in to change notification settings - Fork 12
/
feeds.module
1740 lines (1583 loc) · 49.6 KB
/
feeds.module
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
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
/**
* @file
* Feeds - basic API functions and hook implementations.
*/
/**
* Common request time, use as point of reference and to avoid calls to time().
*
* @var int
*/
define('FEEDS_REQUEST_TIME', time());
/**
* Do not schedule a feed for refresh.
*
* @var int
*/
define('FEEDS_SCHEDULE_NEVER', -1);
/**
* Never expire feed items.
*
* @var int
*/
define('FEEDS_EXPIRE_NEVER', -1);
/**
* Status of batched operations: complete.
*
* @var float
*/
define('FEEDS_BATCH_COMPLETE', 1.0);
/**
* Status of batched operations: active
*
* @var float
*/
define('FEEDS_BATCH_ACTIVE', 0.0);
/**
* Implements hook_boot().
*/
function feeds_boot() {
// Set the default class implementation for the Feeds HTTP Cache if it's
// not set in settings.php. Need to assign it to the global $settings here so
// it gets picked up whenever a cache function is called for this bin.
if (empty(settings_get('cache_class_cache_feeds_http'))) {
global $settings;
$settings['cache_class_cache_feeds_http'] = 'FeedsHTTPCache';
}
}
/**
* @defgroup hooks Hook and callback implementations
* @{
*/
function feeds_config_info() {
$prefixes['feeds.feeds_importer'] = array(
'name_key' => 'id',
'label_key' => 'id',
'group' => t('Feeds importers'),
);
$prefixes['feeds.settings'] = array(
'label' => t('Feeds settings'),
'group' => t('Configuration'),
);
return $prefixes;
}
/**
* Implements hook_hook_info().
*/
function feeds_hook_info() {
$hooks = array(
'feeds_plugins',
'feeds_after_parse',
'feeds_before_import',
'feeds_before_update',
'feeds_prevalidate',
'feeds_presave',
'feeds_after_save',
'feeds_after_import',
'feeds_after_clear',
'feeds_processor_targets',
'feeds_processor_targets_alter',
'feeds_parser_sources_alter',
'feeds_config_defaults',
'feeds_fetcher_config_defaults',
'feeds_parser_config_defaults',
'feeds_processor_config_defaults',
);
return array_fill_keys($hooks, array('group' => 'feeds'));
}
/**
* Implements hook_cron().
*/
function feeds_cron() {
// Expire old log entries.
db_delete('feeds_log')
->condition('request_time', REQUEST_TIME - 604800, '<')
->execute();
// Find importers that need to be rescheduled.
$importers = feeds_reschedule();
if ($importers) {
// @todo Maybe we should queue this somehow as well. This could be potentially
// very long.
$sources = db_query("SELECT feed_nid, id FROM {feeds_source} WHERE id IN (:ids)", array(':ids' => $importers));
foreach ($sources as $source) {
feeds_source($source->id, $source->feed_nid)->schedule();
}
feeds_reschedule(FALSE);
}
// Sync the files in the cache directory with entries in the cache every now
// and then. By default: every six hours.
$last_check = state_get('feeds_sync_cache_feeds_http_last_check');
$interval = config_get('feeds.settings', 'feeds_sync_cache_feeds_http_interval');
if ($last_check < (REQUEST_TIME - $interval)) {
// Check first if the task isn't already queued.
$queue = BackdropQueue::get('feeds_sync_cache_feeds_http');
if ($queue->numberOfItems() < 1) {
// Queue sync task.
FeedsHTTPCache::getInstance('cache_feeds_http')->startSync();
}
state_set('feeds_sync_cache_feeds_http_last_check', REQUEST_TIME);
}
}
/**
* Implements hook_cron_job_scheduler_info().
*
* Compare queue names with key names in feeds_cron_queue_info().
*/
function feeds_cron_job_scheduler_info() {
$info = array();
$info['feeds_source_import'] = array(
'queue name' => 'feeds_source_import',
);
// feeds_source_clear never gets called, since we now use the queue directly.
// This is left in case any background jobs are still running after an
// upgrade.
$info['feeds_source_clear'] = array(
'queue name' => 'feeds_source_clear',
);
$info['feeds_source_expire'] = array(
'queue name' => 'feeds_source_expire',
);
$info['feeds_push_unsubscribe'] = array(
'queue name' => 'feeds_push_unsubscribe',
);
return $info;
}
/**
* Implements hook_cron_queue_info().
*/
function feeds_cron_queue_info() {
$queues = array();
$queues['feeds_source_import'] = array(
'worker callback' => 'feeds_source_import',
'time' => 60,
);
$queues['feeds_source_clear'] = array(
'worker callback' => 'feeds_source_clear',
);
$queues['feeds_source_expire'] = array(
'worker callback' => 'feeds_source_expire',
);
$queues['feeds_push_unsubscribe'] = array(
'worker callback' => 'feeds_push_unsubscribe',
);
$queues['feeds_sync_cache_feeds_http'] = array(
'worker callback' => 'feeds_sync_cache_feeds_http',
);
return $queues;
}
/**
* Scheduler callback for importing from a source.
*
* @param array $job
* A job definition, which consists of at least the following elements:
* - type (string)
* The importer ID.
* - id (int)
* The Feed node ID if the importer is attached to a content type. Otherwise
* 0.
*/
function feeds_source_import(array $job) {
$source = _feeds_queue_worker_helper($job, 'import');
if ($source->doesExist()) {
$source->scheduleImport();
}
}
/**
* Scheduler callback for deleting all items from a source.
*
* @param array $job
* A job definition, which consists of at least the following elements:
* - type (string)
* The importer ID.
* - id (int)
* The Feed node ID if the importer is attached to a content type. Otherwise
* 0.
*/
function feeds_source_clear(array $job) {
$source = _feeds_queue_worker_helper($job, 'clear');
if ($source->doesExist()) {
$source->scheduleClear();
}
}
/**
* Scheduler callback for expiring content.
*
* @param array $job
* A job definition, which consists of at least the following elements:
* - type (string)
* The importer ID.
* - id (int)
* The Feed node ID if the importer is attached to a content type. Otherwise
* 0.
*/
function feeds_source_expire(array $job) {
$source = _feeds_queue_worker_helper($job, 'expire');
if ($source->doesExist()) {
$source->scheduleExpire();
}
}
/**
* Executes a method on a feed source.
*
* @param array $job
* A job definition, which consists of at least the following elements:
* - type (string)
* The importer ID.
* - id (int)
* The Feed node ID if the importer is attached to a content type. Otherwise
* 0.
* @param string $method
* The method to execute.
*/
function _feeds_queue_worker_helper(array $job, $method) {
$source = feeds_source($job['type'], $job['id']);
try {
$source->existing()->$method();
}
catch (FeedsNotExistingException $e) {
// Eventually remove the job from job scheduler.
JobScheduler::get('feeds_source_import')->remove($job);
}
catch (Exception $e) {
$source->log($method, $e->getMessage(), array(), WATCHDOG_ERROR);
}
return $source;
}
/**
* Scheduler callback for keeping the cache dir and cache entries in sync.
*
* This makes sure that files that appear in the Feeds cache directory that are
* no longer referenced in the cache_feeds_http bin, are cleaned up.
* The entries saved in the cache_feeds_http bin and the actual cached files
* saved on the file system can get out of sync when:
* - Truncating the cache_feeds_http table manually.
* - When using an alternative cache class for the cache_feeds_http bin
* (other than 'FeedsHTTPCache').
*/
function feeds_sync_cache_feeds_http(array $job) {
FeedsHTTPCache::getInstance('cache_feeds_http')->sync($job['files']);
}
/**
* Scheduler callback for unsubscribing from PuSH hubs.
*
* @param array $job
* A job definition, which consists of at least the following elements:
* - type (string)
* The importer ID.
* - id (int)
* The Feed node ID if the importer is attached to a content type. Otherwise
* 0.
*/
function feeds_push_unsubscribe($job) {
$source = feeds_source($job['type'], $job['id']);
/** @var FeedsFetcher $fetcher */
$fetcher = feeds_plugin('FeedsHTTPFetcher', $source->importer->id);
$fetcher->unsubscribe($source);
}
/**
* Batch API worker callback. Used by FeedsSource::startBatchAPIJob().
*
* @todo Harmonize Job Scheduler API callbacks with Batch API callbacks?
*
* @param string $method
* Method to execute on importer; one of 'import' or 'clear'.
* @param string $importer_id
* Identifier of a FeedsImporter object.
* @param int $feed_nid
* If importer is attached to content type, feed node id identifying the
* source to be imported. Set to zero if not attached to a node.
* @param array $context
* Batch context.
*
* @see FeedsSource::startBatchAPIJob()
*/
function feeds_batch($method, $importer_id, $feed_nid, &$context) {
if (!isset($feed_nid)) {
$feed_nid = 0;
}
$context['finished'] = FEEDS_BATCH_COMPLETE;
try {
$context['finished'] = feeds_source($importer_id, $feed_nid)->$method();
}
catch (Exception $e) {
backdrop_set_message($e->getMessage(), 'error');
}
}
/**
* Reschedule one or all importers.
*
* @param string|bool|null $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 string[]
* A list of importer ids that need rescheduling.
*/
function feeds_reschedule($importer_id = NULL) {
$reschedule = state_get('feeds_reschedule');
if ($importer_id === TRUE || $importer_id === FALSE) {
$reschedule = $importer_id;
}
elseif (is_string($importer_id) && $reschedule !== TRUE) {
$reschedule = array_filter((array) $reschedule);
$reschedule[$importer_id] = $importer_id;
}
if (isset($importer_id)) {
state_set('feeds_reschedule', $reschedule);
}
if ($reschedule === TRUE) {
return feeds_enabled_importers();
}
elseif ($reschedule === FALSE) {
return array();
}
return $reschedule;
}
/**
* Implements hook_permission().
*/
function feeds_permission() {
$perms = array(
'administer feeds' => array(
'title' => t('Administer Feeds'),
'description' => t('Create, update, delete importers, execute import and delete tasks on any importer.'),
),
);
foreach (feeds_importer_load_all() as $importer) {
$perms["import $importer->id feeds"] = array(
'title' => t('Import @name feeds', array('@name' => $importer->config['name'])),
);
$perms["clear $importer->id feeds"] = array(
'title' => t('Delete items from @name feeds', array('@name' => $importer->config['name'])),
);
$perms["unlock $importer->id feeds"] = array(
'title' => t('Unlock imports from @name feeds', array('@name' => $importer->config['name'])),
'description' => t('If a feed importation breaks for some reason, users with this permission can unlock them.'),
);
}
return $perms;
}
/**
* Implements hook_forms().
*
* Declare form callbacks for all known classes derived from FeedsConfigurable.
*/
function feeds_forms($form_id, $args) {
// Check if the requested form is a Feeds form.
if (!stripos($form_id, '_feeds_form')) {
return;
}
$forms = array();
$forms['FeedsImporter_feeds_form']['callback'] = 'feeds_form';
$plugins = FeedsPlugin::all();
foreach ($plugins as $plugin) {
$forms[$plugin['handler']['class'] . '_feeds_form']['callback'] = 'feeds_form';
}
return $forms;
}
/**
* Implements hook_menu().
*/
function feeds_menu() {
$items = array();
$items['import'] = array(
'title' => 'Feeds Import',
'page callback' => 'feeds_page',
'access callback' => 'feeds_page_access',
'file' => 'feeds.pages.inc',
);
$items['admin/content/import'] = array(
'title' => 'Feeds Import',
'page callback' => 'feeds_page',
'access callback' => 'feeds_page_access',
'file' => 'feeds.pages.inc',
'menu_name' => 'management',
);
$items['import/%feeds_importer'] = array(
'title callback' => 'feeds_importer_title',
'title arguments' => array(1),
'page callback' => 'backdrop_get_form',
'page arguments' => array('feeds_import_form', 1),
'access callback' => 'feeds_access',
'access arguments' => array('import', 1),
'file' => 'feeds.pages.inc',
);
$items['import/%feeds_importer/import'] = array(
'title' => 'Import',
'type' => MENU_DEFAULT_LOCAL_TASK,
'weight' => -10,
);
$items['import/%feeds_importer/delete-items'] = array(
'title' => 'Delete items',
'page callback' => 'backdrop_get_form',
'page arguments' => array('feeds_delete_tab_form', 1),
'access callback' => 'feeds_access',
'access arguments' => array('clear', 1),
'file' => 'feeds.pages.inc',
'type' => MENU_LOCAL_TASK,
);
$items['import/%feeds_importer/unlock'] = array(
'title' => 'Unlock',
'page callback' => 'backdrop_get_form',
'page arguments' => array('feeds_unlock_tab_form', 1),
'access callback' => 'feeds_access',
'access arguments' => array('unlock', 1),
'file' => 'feeds.pages.inc',
'type' => MENU_LOCAL_TASK,
);
$items['import/%feeds_importer/template'] = array(
'page callback' => 'feeds_importer_template',
'page arguments' => array(1),
'access callback' => 'feeds_access',
'access arguments' => array('import', 1),
'file' => 'feeds.pages.inc',
'type' => MENU_CALLBACK,
);
$items['node/%node/import'] = array(
'title' => 'Import',
'page callback' => 'backdrop_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' => 'backdrop_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['node/%node/unlock'] = array(
'title' => 'Unlock',
'page callback' => 'backdrop_get_form',
'page arguments' => array('feeds_unlock_tab_form', NULL, 1),
'access callback' => 'feeds_access',
'access arguments' => array('unlock', 1),
'file' => 'feeds.pages.inc',
'type' => MENU_LOCAL_TASK,
'weight' => 11,
);
// @todo Eliminate this step and thus eliminate clearing menu cache when
// manipulating importers.
foreach (feeds_importer_load_all() as $importer) {
$items += $importer->fetcher->menuItem();
}
return $items;
}
/**
* Implements hook_admin_paths().
*/
function feeds_admin_paths() {
$paths = array(
'import' => TRUE,
'import/*' => TRUE,
'node/*/import' => TRUE,
'node/*/delete-items' => TRUE,
'node/*/log' => TRUE,
);
return $paths;
}
/**
* Menu loader callback.
*
* @param string $id
* The ID of the importer to load.
*
* @return FeedsImporter|false
* A FeedsImporter instance if found. False otherwise.
*/
function feeds_importer_load($id) {
try {
$importer = feeds_importer($id);
if ($importer->doesExist()) {
return $importer;
}
}
catch (InvalidArgumentException $e) {
}
return FALSE;
}
/**
* Title callback.
*
* @param FeedsImporter $importer
* The importer to return the page title for.
*
* @return string
* A page title.
*/
function feeds_importer_title(FeedsImporter $importer) {
return $importer->config['name'];
}
/**
* Implements hook_theme().
*/
function feeds_theme() {
return array(
'feeds_upload' => array(
'file' => 'feeds.pages.inc',
'render element' => 'element',
),
'feeds_source_status' => array(
'file' => 'feeds.pages.inc',
'variables' => array(
'progress_importing' => NULL,
'progress_clearing' => NULL,
'imported' => NULL,
'count' => NULL,
),
),
);
}
/**
* Menu access callback.
*
* @param string $action
* The action to be performed. Possible values are:
* - import;
* - clear;
* - unlock.
* @param object|string $param
* Node object or FeedsImporter id.
*
* @return bool
* True if access is granted. False otherwise.
*/
function feeds_access($action, $param) {
if (!in_array($action, array('import', 'clear', 'unlock'))) {
// If $action is not one of the supported actions, we return access denied.
return FALSE;
}
$importer_id = FALSE;
if (is_string($param)) {
$importer_id = $param;
}
elseif ($param instanceof FeedsImporter) {
$importer_id = $param->id;
}
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;
}
/**
* Access callback to determine if the user can import Feeds importers.
*
* Feeds imports require an additional access check because they are
* configuration.
*
* @return bool
* True if access is granted. False otherwise.
*/
function feeds_importer_import_access() {
return user_access('administer feeds') && user_access('synchronize configuration');
}
/**
* Menu access callback.
*
* @return bool
* True if access is granted. False otherwise.
*/
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;
}
/**
* Implements hook_exit().
*/
function feeds_exit() {
// Process any pending PuSH subscriptions.
$jobs = feeds_get_subscription_jobs();
foreach ($jobs as $job) {
if (!isset($job['fetcher']) || !isset($job['source'])) {
continue;
}
$job['fetcher']->subscribe($job['source']);
}
if (backdrop_static('feeds_log_error', FALSE)) {
watchdog('feeds', 'Feeds reported errors, visit the Feeds log for details.', array(), WATCHDOG_ERROR, l(t('view'), 'admin/reports/feeds'));
}
}
/**
* Implements hook_views_api().
*/
function feeds_views_api() {
return array(
'api' => 3,
'path' => backdrop_get_path('module', 'feeds') . '/views',
);
}
/**
* Implements hook_feeds_plugins().
*/
function feeds_feeds_plugins() {
module_load_include('inc', 'feeds', 'feeds.plugins');
return _feeds_feeds_plugins();
}
/**
* Gets the feed_nid for a single entity.
*
* @param int $entity_id
* The entity id.
* @param string $entity_type
* The type of entity.
*
* @return int|bool
* The feed_nid of the entity, or FALSE if the entity doesn't belong to a
* feed.
*/
function feeds_get_feed_nid($entity_id, $entity_type) {
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();
}
/**
* Implements hook_entity_insert().
*/
function feeds_entity_insert($entity, $type) {
list($id) = entity_extract_ids($type, $entity);
feeds_item_info_insert($entity, $id);
}
/**
* Implements hook_entity_update().
*/
function feeds_entity_update($entity, $type) {
list($id) = entity_extract_ids($type, $entity);
feeds_item_info_save($entity, $id);
}
/**
* Implements hook_entity_delete().
*/
function feeds_entity_delete($entity, $type) {
list($id) = entity_extract_ids($type, $entity);
// Delete any imported items produced by the source.
db_delete('feeds_item')
->condition('entity_type', $type)
->condition('entity_id', $id)
->execute();
}
/**
* Implements hook_node_validate().
*/
function feeds_node_validate($node, $form, &$form_state) {
if (!$importer_id = feeds_get_importer_id($node->type)) {
return;
}
// Keep a copy of the title for subsequent node creation stages.
// @todo: revisit whether $node still looses all of its properties
// between validate and insert stage.
$last_title = &backdrop_static('feeds_node_last_title');
$last_feeds = &backdrop_static('feeds_node_last_feeds');
// 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.
// configFormValidate may modify $last_feed, smuggle it to update/insert stage
// through a static variable.
$last_feeds = $node->feeds;
$source->configFormValidate($last_feeds);
// Check if title form element is hidden.
$title_hidden = (isset($form['title']['#access']) && !$form['title']['#access']);
// If the node title is empty and the title form element wasn't hidden, try to
// retrieve the title from the feed.
if (isset($node->title) && trim($node->title) == '' && !$title_hidden) {
try {
$source->addConfig($last_feeds);
if (!$last_title = $source->preview()->title) {
throw new Exception();
}
}
catch (Exception $e) {
backdrop_set_message($e->getMessage(), 'error');
form_set_error('title', t('Could not retrieve title from feed.'));
}
}
}
/**
* Implements hook_node_presave().
*/
function feeds_node_presave($node) {
// Populate $node->title and $node->feed from result of validation phase.
$last_title = &backdrop_static('feeds_node_last_title');
$last_feeds = &backdrop_static('feeds_node_last_feeds');
if (empty($node->title) && !empty($last_title)) {
$node->title = $last_title;
}
if (!empty($last_feeds)) {
$node->feeds = $last_feeds;
}
$last_title = NULL;
$last_feeds = NULL;
// Update "changed" value if there was mapped to that.
if (isset($node->feeds_item->node_changed)) {
$node->changed = $node->feeds_item->node_changed;
}
}
/**
* Implements hook_node_insert().
*/
function feeds_node_insert($node) {
// Source attached to node.
if (isset($node->feeds) && $importer_id = feeds_get_importer_id($node->type)) {
$source = feeds_source($importer_id, $node->nid);
$source->addConfig($node->feeds);
$source->save();
// Start import if requested.
if (feeds_importer($importer_id)->config['import_on_create'] && !isset($node->feeds['suppress_import'])) {
$source->startImport();
}
// Schedule the source.
$source->schedule();
}
}
/**
* Implements hook_node_update().
*/
function feeds_node_update($node) {
// Source attached to node.
if (isset($node->feeds) && $importer_id = feeds_get_importer_id($node->type)) {
$source = feeds_source($importer_id, $node->nid);
$source->addConfig($node->feeds);
$source->save();
$source->ensureSchedule();
}
}
/**
* Implements hook_node_delete().
*/
function feeds_node_delete($node) {
// Source attached to node.
// Make sure we don't leave any orphans behind: Do not use
// feeds_get_importer_id() to determine importer id as the importer may have
// been deleted.
if ($importer_id = db_query("SELECT id FROM {feeds_source} WHERE feed_nid = :nid", array(':nid' => $node->nid))->fetchField()) {
feeds_source($importer_id, $node->nid)->delete();
}
}
/**
* Implements hook_form_BASE_FORM_ID_alter().
*/
function feeds_form_node_form_alter(&$form, $form_state) {
if ($importer_id = feeds_get_importer_id($form['#node']->type)) {
// Enable uploads.
$form['#attributes']['enctype'] = 'multipart/form-data';
// Build form.
$source = feeds_source($importer_id, empty($form['#node']->nid) ? NULL : $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;
// If the parser has support for delivering a source title, set node title
// to not required and try to retrieve it from the source if the node title
// is left empty by the user.
// @see feeds_node_validate()
if (isset($form['title']) && $source->importer()->parser->providesSourceTitle()) {
$form['title']['#required'] = FALSE;
}
}
}
/**
* Implements hook_field_extra_fields().
*/
function feeds_field_extra_fields() {
$extras = array();
foreach (node_type_get_names() as $type => $name) {
if (feeds_get_importer_id($type)) {
$extras['node'][$type]['form']['feeds'] = array(
'label' => t('Feed'),
'description' => t('Feeds module form elements'),
'weight' => 0,
);
}
}
return $extras;
}
/**
* Implements hook_features_pipe_COMPONENT_alter() for 'feeds_importer'.
*
* Automatically adds dependencies when a Feed importer is selected in Features.
*/
function feeds_features_pipe_feeds_importer_alter(&$pipe, $data, &$export) {
foreach ($data as $importer_id) {
if ($importer = feeds_importer_load($importer_id)) {
$export['dependencies'] = array_merge($export['dependencies'], $importer->dependencies());
}
}
}
/**
* Implements hook_system_info_alter().
*
* Goes through a list of all modules that provide Feeds plugins and makes them
* required if there are any importers using those plugins.
*/
function feeds_system_info_alter(array &$info, $file, $type) {
if ($type !== 'module' || !module_exists($file->name) || !module_hook($file->name, 'feeds_plugins')) {
return;
}
// Don't make Feeds require itself, otherwise you can't disable Feeds until
// all importers are deleted.
if ($file->name === 'feeds') {
return;
}
// Get the plugins that belong to the current module.
$module_plugins = array();
foreach (feeds_get_plugins() as $plugin_id => $plugin) {
if ($file->name === $plugin['module']) {
$module_plugins[$plugin_id] = TRUE;
}
}
// Check if any importers are using any plugins from the current module.
foreach (feeds_importer_load_all(TRUE) as $importer) {
$configuration = $importer->getConfig();
foreach (array('fetcher', 'parser', 'processor') as $plugin_type) {
$plugin_key = $configuration[$plugin_type]['plugin_key'];
if (isset($module_plugins[$plugin_key])) {
$info['disabled'] = TRUE;
break 2;
}
}
}
if (empty($info['disabled'])) {
return;
}
if (module_exists('feeds_ui') && user_access('administer feeds')) {
$info['explanation'] = t('Feeds is currently using this module for one or more <a href="@link">importers</a>', array('@link' => url('admin/structure/feeds')));
}
else {
$info['explanation'] = t('Feeds is currently using this module for one or more importers');
}
}
/**
* Implements hook_module_implements_alter().
*/
function feeds_module_implements_alter(array &$implementations, $hook) {
if ($hook === 'feeds_processor_targets_alter') {
// We need two implementations of this hook, so we add one that gets
// called first, and move the normal one to last.
$implementations = array('_feeds' => FALSE) + $implementations;
// Move normal implementation to last.
$group = $implementations['feeds'];
unset($implementations['feeds']);
$implementations['feeds'] = $group;
}
}
/**
* Implements hook_feeds_processor_targets_alter().
*
* @see feeds_feeds_processor_targets()
* @see feeds_feeds_processor_targets_alter()
*/
function _feeds_feeds_processor_targets_alter(array &$targets, $entity_type, $bundle) {
// If hook_feeds_processor_targets() hasn't been called, for instance, by
// older processors, invoke it ourself.
if (!backdrop_static('feeds_feeds_processor_targets', FALSE)) {
$targets += module_invoke_all('feeds_processor_targets', $entity_type, $bundle);
}
}
/**
* Implements hook_flush_caches().
*/
function feeds_flush_caches() {
// The update to add the table needs to have run. Taken from
// https://www.drupal.org/node/2511858
include_once BACKDROP_ROOT . '/core/includes/install.inc';
if (backdrop_get_installed_schema_version('feeds') >= 7212) {
return array('cache_feeds_http');
}
return array();
}
/**
* Implements hook_admin_bar_output_build().
*