diff --git a/feeds.pages.inc b/feeds.pages.inc index 1ee6f9c..93604ff 100644 --- a/feeds.pages.inc +++ b/feeds.pages.inc @@ -89,12 +89,19 @@ function feeds_import_form(array $form, array &$form_state, FeedsImporter $impor '#type' => 'submit', '#value' => t('Import'), ); + // Change submit button label if processing in background. + if ($source->importer->config['process_in_background']) { + $form['submit']['#value'] = t('Schedule import'); + } + + // Disable submit button if import is initiated. $progress = $source->progressImporting(); if ($progress !== FEEDS_BATCH_COMPLETE) { $form['submit']['#disabled'] = TRUE; $form['submit']['#value'] = t('Importing (@progress %)', array('@progress' => number_format(100 * $progress, 0))); } + return $form; } @@ -121,6 +128,9 @@ function feeds_import_form_submit($form, &$form_state) { // Refresh feed if import on create is selected. if ($source->importer->config['import_on_create']) { $source->startImport(); + if ($source->importer->config['process_in_background']) { + drupal_set_message(t('Import scheduled.')); + } } // Add to schedule, make sure importer is scheduled, too. @@ -145,12 +155,20 @@ function feeds_import_tab_form($form, &$form_state, $node) { '#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))); } + return $form; } @@ -162,6 +180,10 @@ function feeds_import_tab_form_submit($form, &$form_state) { $source = feeds_source($form['#importer_id'], $form['#feed_nid']); $source->startImport(); $source->ensureSchedule(); + + if ($source->importer->config['process_in_background']) { + drupal_set_message(t('Import scheduled.')); + } } /** @@ -190,12 +212,20 @@ function feeds_delete_tab_form(array $form, array &$form_state, FeedsImporter $i '#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))); } + return $form; } @@ -208,6 +238,10 @@ function feeds_delete_tab_form_submit($form, &$form_state) { $source = feeds_source($form['#importer_id'], $feed_nid); $source->startClear(); $source->ensureSchedule(); + + if ($source->importer->config['process_in_background']) { + drupal_set_message(t('Deletion of items scheduled.')); + } } /** diff --git a/includes/FeedsSource.inc b/includes/FeedsSource.inc index 690bfe8..3195def 100644 --- a/includes/FeedsSource.inc +++ b/includes/FeedsSource.inc @@ -254,7 +254,7 @@ class FeedsSource extends FeedsConfigurable { public function startImport() { $config = $this->importer->getConfig(); if ($config['process_in_background']) { - $this->import(); + $this->startBackgroundJob('import'); } else { $this->startBatchAPIJob(t('Importing'), 'import'); @@ -276,8 +276,7 @@ class FeedsSource extends FeedsConfigurable { public function startClear() { $config = $this->importer->getConfig(); if ($config['process_in_background']) { - $this->clear(); - $this->scheduleClear(); + $this->startBackgroundJob('clear'); } else { $this->startBatchAPIJob(t('Deleting'), 'clear'); @@ -905,6 +904,43 @@ class FeedsSource extends FeedsConfigurable { } /** + * Background job helper. Starts a background job using the Drupal queue. + * + * @see FeedsSource::startImport(). + * @see FeedsSource::startClear(). + * + * @param string $method + * Method to execute on importer; one of 'import' or 'clear'. + */ + protected function startBackgroundJob($method) { + $job = array( + 'type' => $this->id, + 'id' => $this->feed_nid, + ); + $queue = DrupalQueue::get('feeds_source_' . $method); + $queue->createItem($job); + + switch ($method) { + case 'import': + $state = $this->state(FEEDS_FETCH); + break; + + case 'clear': + $state = $this->state(FEEDS_PROCESS_CLEAR); + break; + + case 'expire': + $state = $this->state(FEEDS_PROCESS_EXPIRE); + break; + } + + if (isset($state)) { + $state->progress = 0; + $this->save(); + } + } + + /** * Acquires a lock for this source. * * @throws FeedsLockException diff --git a/tests/feeds.test b/tests/feeds.test index ff8f256..7f0755e 100644 --- a/tests/feeds.test +++ b/tests/feeds.test @@ -359,14 +359,24 @@ class FeedsWebTestCase extends DrupalWebTestCase { /** * Import a file through the import form. Assumes FeedsFileFetcher in place. + * + * @param string $id + * The ID of the importer. + * @param string $file + * The absolute path to the file. + * @param string $submit + * (optional) The button to press. + * Defaults to the button "Import". */ - public function importFile($id, $file) { - + public function importFile($id, $file, $submit = NULL) { $this->assertTrue(file_exists($file), 'Source file exists'); $edit = array( 'files[feeds]' => $file, ); - $this->drupalPost('import/' . $id, $edit, 'Import'); + if (empty($submit)) { + $submit = 'Import'; + } + $this->drupalPost('import/' . $id, $edit, $submit); } /** diff --git a/tests/feeds_fetcher_file.test b/tests/feeds_fetcher_file.test index 089c7c0..484f41e 100644 --- a/tests/feeds_fetcher_file.test +++ b/tests/feeds_fetcher_file.test @@ -149,7 +149,7 @@ class FeedsFileFetcherTestCase extends FeedsWebTestCase { variable_set('feeds_tests_feeds_after_save_sleep', 1); // Import a file with 9 nodes. - $this->importFile('node', $this->absolutePath() . '/tests/feeds/nodes.csv'); + $this->importFile('node', $this->absolutePath() . '/tests/feeds/nodes.csv', 'Schedule import'); // Assert that the file has been created. $this->assertTrue(file_exists('private://feeds/nodes.csv'), 'The imported file is created.'); diff --git a/tests/feeds_processor_node.test b/tests/feeds_processor_node.test index 25ec79a..6d64336 100644 --- a/tests/feeds_processor_node.test +++ b/tests/feeds_processor_node.test @@ -572,8 +572,8 @@ class FeedsRSStoNodesTest extends FeedsWebTestCase { ), )); - $this->importFile('syndication', $this->absolutePath() . '/tests/feeds/many_nodes_ordered.csv'); - $this->assertEqual(5, db_query("SELECT COUNT(*) FROM {node}")->fetchField()); + $this->importFile('syndication', $this->absolutePath() . '/tests/feeds/many_nodes_ordered.csv', 'Schedule import'); + $this->assertEqual(0, db_query("SELECT COUNT(*) FROM {node}")->fetchField()); // The feed should still be scheduled because it is being processed. // @see https://drupal.org/node/2275893