-
Notifications
You must be signed in to change notification settings - Fork 12
/
feeds.pages.inc
637 lines (574 loc) · 20.3 KB
/
feeds.pages.inc
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
<?php
/**
* @file
* Menu callbacks, form callbacks and helpers.
*/
/**
* Render a page of available importers.
*/
function feeds_page() {
$rows = array();
if ($importers = feeds_importer_load_all()) {
foreach ($importers as $importer) {
if ($importer->disabled) {
continue;
}
if (!(user_access('import ' . $importer->id . ' feeds') || user_access('administer feeds'))) {
continue;
}
if (empty($importer->config['content_type'])) {
$link = 'import/' . $importer->id;
$title = $importer->config['name'];
}
elseif (node_access('create', $importer->config['content_type'])) {
$link = 'node/add/' . str_replace('_', '-', $importer->config['content_type']);
$title = node_type_get_name($importer->config['content_type']);
}
else {
continue;
}
$rows[] = array(
l($title, $link),
check_plain($importer->config['description']),
);
}
}
if (empty($rows)) {
// The feeds_ui module is enabled.
if (module_exists('feeds_ui') && user_access('administer feeds')) {
backdrop_set_message(t('There are no importers, go to <a href="@importers">Feed importers</a> to create one or enable an existing one.', array('@importers' => url('admin/structure/feeds'))));
}
else {
// The feeds_ui module is not enabled but the current user has access to
// Modules to enable it.
if (user_access('administer modules')) {
backdrop_set_message(t('The Feeds UI Admin module is not enabled and there are no importers, go to <a href="@modules">Modules</a> and enable Feeds Admin UI. Then go to <a href="@importers">Feed importers</a> to create one or enable an existing one.', array('@modules' => url('admin/modules'), '@importers' => url('admin/structure/feeds'))));
}
else {
// The feeds_ui module is not enabled and the current user cannot
// enable it.
backdrop_set_message(t("The Feeds UI Admin module is not enabled. Please contact the Administrator for your site and ask them to enable it."));
}
}
}
$header = array(
t('Import'),
t('Description'),
);
return theme('table', array('header' => $header, 'rows' => $rows));
}
/**
* Render a feeds import form on import/[config] pages.
*/
function feeds_import_form(array $form, array &$form_state, FeedsImporter $importer) {
$source = feeds_source($importer->id);
$form['#importer_id'] = $importer->id;
// @todo Move this into fetcher?
$form['#attributes']['enctype'] = 'multipart/form-data';
$form['source_status'] = array(
'#type' => 'fieldset',
'#title' => t('Status'),
'#tree' => TRUE,
'#value' => feeds_source_status($source),
);
$source_form = $source->configForm($form_state);
if (!empty($source_form)) {
$form['feeds'] = array(
'#type' => 'fieldset',
'#title' => t('Import'),
'#tree' => TRUE,
) + $source_form;
}
// Set submit button label based on settings.
if ($source->importer->config['import_on_create']) {
$submit = t('Import');
if ($source->importer->config['process_in_background']) {
// When processing the import in background, the import job is put in the
// queue.
$submit = t('Schedule import');
}
}
elseif ($source->importer->config['import_period'] != FEEDS_SCHEDULE_NEVER) {
// The import would be scheduled according to the periodic import setting.
$submit = t('Schedule import');
}
else {
backdrop_set_message(t('For this importer both "@import_period" and "@import_on_create" are turned off. It is possible that Feeds will not import the provided source.', array(
'@import_period' => t('Periodic import'),
'@import_on_create' => t('Import on submission'),
)), 'warning', FALSE);
$submit = t('Save');
}
$form['actions'] = array('#type' => 'actions');
$form['actions']['submit'] = array(
'#type' => 'submit',
'#value' => $submit,
);
// Disable submit button if import is initiated.
$progress = $source->progressImporting();
if ($progress !== FEEDS_BATCH_COMPLETE) {
$form['actions']['submit']['#disabled'] = TRUE;
$form['actions']['submit']['#value'] = t('Importing (@progress %)', array(
'@progress' => number_format(100 * $progress, 0),
));
// Check if import task is queued.
if ($source->isQueued()) {
$form['source_status']['#value'] .= t('Run cron to continue the import.');
}
}
if (isset($form_state['feeds_parser_result'])) {
$form['feeds_preview'] = feeds_preview_show_result_on_form($form['#importer_id'], $form_state['feeds_parser_result']);
// Replace source form to support file fetcher.
$source = FeedsPreviewSource::instance($form['#importer_id'], 0);
$source_form = $source->configForm($form_state);
$form['feeds'] = array_merge($form['feeds'], $source_form);
}
$form['actions']['preview'] = array(
'#type' => 'submit',
'#value' => t('Preview'),
'#submit' => array('feeds_preview_form_submit'),
);
return $form;
}
/**
* Validation handler for node forms and feeds_import_form().
*/
function feeds_import_form_validate($form, &$form_state) {
// @todo This may be a problem here, as we don't have a feed_nid at this point.
feeds_source($form['#importer_id'])->configFormValidate($form_state['values']['feeds']);
}
/**
* Submit handler for feeds_import_form().
*/
function feeds_import_form_submit($form, &$form_state) {
// Save source and import.
$source = feeds_source($form['#importer_id']);
if (!empty($form_state['values']['feeds']) && is_array($form_state['values']['feeds'])) {
$source->addConfig($form_state['values']['feeds']);
$source->save();
}
// Refresh feed if import on create is selected.
if ($source->importer->config['import_on_create']) {
$source->startImport();
if ($source->importer->config['process_in_background']) {
backdrop_set_message(t('Import scheduled.'));
}
}
// Add to schedule, make sure importer is scheduled, too.
$source->ensureSchedule();
// If an import is only triggered by periodic import, check if it is about to
// be rescheduled so there is at least a message.
if (!$source->importer->config['import_on_create']) {
// Check if the importer is about to be rescheduled.
$importers = feeds_reschedule();
if (isset($importers[$form['#importer_id']])) {
backdrop_set_message(t('Rescheduling the import will happen on the next cron run.'), 'status');
}
}
}
/**
* Render a feeds import form on node/id/import pages.
*/
function feeds_import_tab_form($form, &$form_state, $node) {
$importer_id = feeds_get_importer_id($node->type);
$source = feeds_source($importer_id, $node->nid);
$form = array();
$form['#feed_nid'] = $node->nid;
$form['#importer_id'] = $importer_id;
$form['#redirect'] = 'node/' . $node->nid;
$form['source_status'] = array(
'#type' => 'fieldset',
'#title' => t('Status'),
'#tree' => TRUE,
'#value' => feeds_source_status($source),
);
$form = confirm_form($form, t('Import all content from source?'), 'node/' . $node->nid, '', t('Import'), t('Cancel'), 'confirm feeds update');
// Change submit button label if processing in background.
if ($source->importer->config['process_in_background']) {
$form['actions']['submit']['#value'] = t('Schedule import');
}
// Disable submit button if import is initiated.
$progress = $source->progressImporting();
if ($progress !== FEEDS_BATCH_COMPLETE) {
$form['actions']['submit']['#disabled'] = TRUE;
$form['actions']['submit']['#value'] = t('Importing (@progress %)', array(
'@progress' => number_format(100 * $progress, 0),
));
// Check if import task is queued.
if ($source->isQueued()) {
$form['source_status']['#value'] .= t('Run cron to continue the import.');
}
}
return $form;
}
/**
* Submit handler for feeds_import_tab_form().
*/
function feeds_import_tab_form_submit($form, &$form_state) {
$form_state['redirect'] = $form['#redirect'];
$source = feeds_source($form['#importer_id'], $form['#feed_nid']);
$source->startImport();
$source->ensureSchedule();
if ($source->importer->config['process_in_background']) {
backdrop_set_message(t('Import scheduled.'));
}
}
/**
* Render a feeds delete form.
*
* Used on both node pages and configuration pages.
* Therefore $node may be missing.
*/
function feeds_delete_tab_form(array $form, array &$form_state, FeedsImporter $importer = NULL, $node = NULL) {
if (empty($node)) {
$source = feeds_source($importer->id);
$form['#redirect'] = 'import/' . $source->id;
}
else {
$importer_id = feeds_get_importer_id($node->type);
$source = feeds_source($importer_id, $node->nid);
$form['#redirect'] = 'node/' . $source->feed_nid;
}
// Form cannot pass on source object.
$form['#importer_id'] = $source->id;
$form['#feed_nid'] = $source->feed_nid;
$form['source_status'] = array(
'#type' => 'fieldset',
'#title' => t('Status'),
'#tree' => TRUE,
'#value' => feeds_source_status($source),
);
$form = confirm_form($form, t('Delete all items from source?'), $form['#redirect'], '', t('Delete'), t('Cancel'), 'confirm feeds update');
// Change submit button label if processing in background.
if ($source->importer->config['process_in_background']) {
$form['actions']['submit']['#value'] = t('Schedule delete');
}
// Disable submit button if clearing is initiated.
$progress = $source->progressClearing();
if ($progress !== FEEDS_BATCH_COMPLETE) {
$form['actions']['submit']['#disabled'] = TRUE;
$form['actions']['submit']['#value'] = t('Deleting (@progress %)', array(
'@progress' => number_format(100 * $progress, 0),
));
$form['source_status']['#value'] .= t('Run cron to continue the deletion of items.');
}
return $form;
}
/**
* Submit handler for feeds_delete_tab_form().
*/
function feeds_delete_tab_form_submit($form, &$form_state) {
$form_state['redirect'] = $form['#redirect'];
$feed_nid = empty($form['#feed_nid']) ? 0 : $form['#feed_nid'];
$source = feeds_source($form['#importer_id'], $feed_nid);
$source->startClear();
$source->ensureSchedule();
if ($source->importer->config['process_in_background']) {
backdrop_set_message(t('Deletion of items scheduled.'));
}
}
/**
* Render a feeds unlock form.
*
* Used on both node pages and configuration pages.
* Therefore $node may be missing.
*/
function feeds_unlock_tab_form($form, &$form_state, FeedsImporter $importer = NULL, $node = NULL) {
if (empty($node)) {
$source = feeds_source($importer->id);
$form['#redirect'] = 'import/' . $source->id;
}
else {
$importer_id = feeds_get_importer_id($node->type);
$source = feeds_source($importer_id, $node->nid);
$form['#redirect'] = 'node/' . $source->feed_nid;
}
// Form cannot pass on source object.
$form['#importer_id'] = $source->id;
$form['#feed_nid'] = $source->feed_nid;
$form['source_status'] = array(
'#type' => 'fieldset',
'#title' => t('Status'),
'#tree' => TRUE,
'#value' => feeds_source_status($source),
);
$form = confirm_form($form, t('Unlock this importer?'), $form['#redirect'], '', t('Delete'), t('Cancel'), 'confirm feeds update');
if ($source->progressImporting() == FEEDS_BATCH_COMPLETE && $source->progressClearing() == FEEDS_BATCH_COMPLETE) {
$form['source_locked'] = array(
'#type' => 'markup',
'#title' => t('Not Locked'),
'#tree' => TRUE,
'#markup' => t('This importer is not locked, therefore it cannot be unlocked.'),
);
$form['actions']['submit']['#disabled'] = TRUE;
$form['actions']['submit']['#value'] = t('Unlock (disabled)');
}
else {
$form['actions']['submit']['#value'] = t('Unlock');
}
return $form;
}
/**
* Form submit handler. Resets all feeds state.
*/
function feeds_unlock_tab_form_submit($form, &$form_state) {
$form_state['redirect'] = $form['#redirect'];
$feed_nid = empty($form['#feed_nid']) ? 0 : $form['#feed_nid'];
$importer_id = $form['#importer_id'];
feeds_source($importer_id, $feed_nid)
->unlock();
backdrop_set_message(t('Importer unlocked.'));
}
/**
* Form constructor for the feeds import preview form.
*
* @see feeds_preview_form_validate()
* @see feeds_preview_form_submit()
*/
function feeds_preview_form($form, &$form_state, $importer_id) {
$source = FeedsPreviewSource::instance($importer_id, 0);
// Set flag that this form is viewed in the admin interface.
$form_state['admin_preview'] = TRUE;
$form = array();
$form['#importer_id'] = $importer_id;
$form['#attributes']['enctype'] = 'multipart/form-data';
$form['help'] = array(
'#markup' => '<p>' . t('This will give you a brief look of what the parsed result looks like (thus before the data is processed). Note that the data may be modified during processing. If your browser supports it, you can use the left and right arrow keys on your keyboard to navigate between the results.') . '</p>',
);
if (isset($form_state['feeds_parser_result'])) {
$form['feeds_preview_result'] = feeds_preview_table($source, $form_state['feeds_parser_result']);
}
$source_form = $source->configForm($form_state);
if (!empty($source_form)) {
$form['feeds'] = array(
'#type' => 'fieldset',
'#title' => t('Import settings'),
'#tree' => TRUE,
'#collapsed' => isset($form_state['feeds_parser_result']),
'#collapsible' => isset($form_state['feeds_parser_result']),
) + $source_form;
}
$form['submit'] = array(
'#type' => 'submit',
'#value' => t('Preview import'),
);
return $form;
}
/**
* Returns preview results array.
*
* @param string $importer_id
* The ID of the importer.
* @param FeedsParserResult $result
* The parser result.
*
* @return array
* Markup for previewing the results.
*/
function feeds_preview_show_result_on_form($importer_id, $result) {
$field = array(
'#type' => 'fieldset',
'#title' => t('Preview'),
'#weight' => -50,
);
$field['help'] = array(
'#markup' => '<p>' . t('This will give you a brief look of what the parsed result looks like (thus before the data is processed). Note that the data may be modified during processing. If your browser supports it, you can use the left and right arrow keys on your keyboard to navigate between the results.') . '</p>',
);
$source = FeedsPreviewSource::instance($importer_id, 0);
$field['result'] = feeds_preview_table($source, $result);
return $field;
}
/**
* Form validation handler for feeds_preview_form().
*
* @see feeds_preview_form()
* @see feeds_preview_form_submit()
*/
function feeds_preview_form_validate($form, &$form_state) {
FeedsPreviewSource::instance($form['#importer_id'], 0)->configFormValidate($form_state['values']['feeds']);
}
/**
* Form submission handler for feeds_preview_form().
*
* @see feeds_preview_form()
* @see feeds_preview_form_validate()
*/
function feeds_preview_form_submit($form, &$form_state) {
$source = FeedsPreviewSource::instance($form['#importer_id'], 0);
if (!empty($form_state['values']['feeds']) && is_array($form_state['values']['feeds'])) {
$source->addConfig($form_state['values']['feeds']);
if (!empty($form_state['admin_preview'])) {
// Save source only when using the preview form on the importer configuration.
$source->save();
}
}
try {
$form_state['feeds_parser_result'] = $source->preview();
}
catch (Exception $e) {
backdrop_set_message($e->getMessage(), 'error');
}
$form_state['rebuild'] = TRUE;
}
/**
* Builds a preview table.
*
* @param FeedsSource $feeds_source
* The import source.
* @param FeedsParserResult $result
* The parser result.
*
* @return array
* A build array in the format expected by backdrop_render().
*/
function feeds_preview_table(FeedsSource $feeds_source, FeedsParserResult $result) {
// Check the result's source.
if (empty($result->items)) {
backdrop_set_message(t('No result.'));
return;
}
$table_builder = new FeedsPreviewTable($feeds_source);
return $table_builder->build($result);
}
/**
* Handle a fetcher callback.
*/
function feeds_fetcher_callback($importer, $feed_nid = 0) {
if ($importer instanceof FeedsImporter) {
try {
return $importer->fetcher->request($feed_nid);
}
catch (Exception $e) {
// Do nothing.
}
}
backdrop_access_denied();
}
/**
* Template generation.
*/
function feeds_importer_template(FeedsImporter $importer) {
if ($importer->parser instanceof FeedsCSVParser) {
return $importer->parser->getTemplate();
}
return backdrop_not_found();
}
/**
* Renders a status display for a source.
*/
function feeds_source_status($source) {
$progress_importing = $source->progressImporting();
$v = array();
if ($progress_importing != FEEDS_BATCH_COMPLETE) {
$v['progress_importing'] = $progress_importing;
}
$progress_clearing = $source->progressClearing();
if ($progress_clearing != FEEDS_BATCH_COMPLETE) {
$v['progress_clearing'] = $progress_clearing;
}
$v['imported'] = $source->imported;
$v['count'] = $source->itemCount();
$v['next'] = $source->getNextImportTimeDetails();
if (!empty($v)) {
return theme('feeds_source_status', $v);
}
}
/**
* Themes a status display for a source.
*/
function theme_feeds_source_status($v) {
$output = '<div class="info-box feeds-source-status">';
$items = array();
if ($v['progress_importing']) {
$progress = number_format(100.0 * $v['progress_importing'], 0);
$items[] = t('Importing - @progress % complete.', array(
'@progress' => $progress,
));
}
if ($v['progress_clearing']) {
$progress = number_format(100.0 * $v['progress_clearing'], 0);
$items[] = t('Deleting items - @progress % complete.', array(
'@progress' => $progress,
));
}
if (!count($items)) {
if ($v['count']) {
if ($v['imported']) {
$items[] = t('Last import: @ago ago.', array(
'@ago' => format_interval(REQUEST_TIME - $v['imported'], 1),
));
}
$items[] = t('@count imported items total.', array(
'@count' => $v['count'],
));
}
else {
$items[] = t('No imported items.');
}
}
if ($v['next']) {
// Check if medium date format contains hours/minutes.
$date_format = config_get('system.date', 'formats.medium.pattern');
$use_custom_date_format = $date_format && !strpos($date_format, 'H:i');
if (!empty($v['next']['message'])) {
$items[] = t('Next import: @message.', array(
'@message' => $v['next']['message'],
));
}
elseif ($v['next']['time'] > REQUEST_TIME) {
$items[] = t('Next import: @date (via @method)', array(
'@date' => $use_custom_date_format ? format_date($v['next']['time'], 'custom', 'Y/m/d H:i:s') : format_date($v['next']['time']),
'@method' => $v['next']['method'],
));
}
else {
$items[] = t('Next import: on next cron run (via @method).', array(
'@method' => $v['next']['method'],
));
}
}
else {
$items[] = t('Next import: not scheduled.');
}
$output .= theme('item_list', array('items' => $items));
$output .= '</div>';
return $output;
}
/**
* Theme upload widget.
*/
function theme_feeds_upload($variables) {
$element = $variables['element'];
backdrop_add_css(backdrop_get_path('module', 'feeds') . '/feeds.css');
_form_set_class($element, array('form-file'));
$summary = '';
if (!empty($element['#file_info'])) {
$file = $element['#file_info'];
$wrapper = file_stream_wrapper_get_instance_by_uri($file->uri);
$summary .= '<div class="feeds-file-info">';
$summary .= '<div class="feeds-file-name">';
if ($wrapper) {
$summary .= l($file->filename, $wrapper->getExternalUrl());
}
else {
$summary .= t('URI scheme %scheme not available.', array(
'%scheme' => file_uri_scheme($file->uri),
));
}
$summary .= '</div>';
$summary .= '<div class="file-size">';
$summary .= format_size($file->filesize);
$summary .= '</div>';
$summary .= '<div class="feeds-file-mime">';
$summary .= check_plain($file->filemime);
$summary .= '</div>';
$summary .= '</div>';
}
// Prepend the summary to the form field.
$element['#children'] = '<div class="feeds-file">' . $summary . '<div class="feeds-file-upload">' . $element['#children'];
// Render file upload field using theme_form_element().
$output = theme('form_element', $element);
// Close "feeds-file" and "feeds-file-upload" divs.
$output .= '</div></div>';
return $output;
}