Skip to content
Snippets Groups Projects
Commit ba3f0094 authored by twistor's avatar twistor Committed by Chris Leppanen
Browse files

Issue #2531828 by twistor: Simplify db queries in FeedsProcessor

parent 3383fd11
No related branches found
No related tags found
No related merge requests found
...@@ -362,8 +362,10 @@ abstract class FeedsProcessor extends FeedsPlugin { ...@@ -362,8 +362,10 @@ abstract class FeedsProcessor extends FeedsPlugin {
} }
/** /**
* Initialize the array of entities to remove with all existing entities * Initializes the list of entities to remove.
* previously imported from the source. *
* This populates $state->removeList with all existing entities previously
* imported from the source.
* *
* @param FeedsSource $source * @param FeedsSource $source
* Source information about this import. * Source information about this import.
...@@ -372,33 +374,27 @@ abstract class FeedsProcessor extends FeedsPlugin { ...@@ -372,33 +374,27 @@ abstract class FeedsProcessor extends FeedsPlugin {
*/ */
protected function initEntitiesToBeRemoved(FeedsSource $source, FeedsState $state) { protected function initEntitiesToBeRemoved(FeedsSource $source, FeedsState $state) {
$state->removeList = array(); $state->removeList = array();
// We fill it only if needed. // We fill it only if needed.
if (!isset($this->config['update_non_existent']) || $this->config['update_non_existent'] == FEEDS_SKIP_NON_EXISTENT) { if ($this->config['update_non_existent'] == FEEDS_SKIP_NON_EXISTENT) {
return; return;
} }
// Build base select statement.
$info = $this->entityInfo();
$id_key = db_escape_field($info['entity keys']['id']);
$select = db_select($info['base table'], 'e');
$select->addField('e', $info['entity keys']['id'], 'entity_id');
$select->join(
'feeds_item',
'fi',
'e.' . $id_key . ' = fi.entity_id AND fi.entity_type = :entity_type', array(
':entity_type' => $this->entityType(),
));
$select->condition('fi.id', $this->id); // Get the full list of entities for this source.
$select->condition('fi.feed_nid', $source->feed_nid); $entity_ids = db_select('feeds_item')
// No need to remove item again if same method of removal was already used. ->fields('feeds_item', array('entity_id'))
$select->condition('fi.hash', $this->config['update_non_existent'], '<>'); ->condition('entity_type', $this->entityType())
$entities = $select->execute(); ->condition('id', $this->id)
// If not found on process, existing entities will be deleted. ->condition('feed_nid', $source->feed_nid)
foreach ($entities as $entity) { ->condition('hash', $this->config['update_non_existent'], '<>')
// Obviously, items which are still included in the source feed will be ->execute()
// removed from this array when processed. ->fetchCol();
$state->removeList[$entity->entity_id] = $entity->entity_id;
if (!$entity_ids) {
return;
} }
$state->removeList = array_combine($entity_ids, $entity_ids);
} }
/** /**
...@@ -411,12 +407,11 @@ abstract class FeedsProcessor extends FeedsPlugin { ...@@ -411,12 +407,11 @@ abstract class FeedsProcessor extends FeedsPlugin {
*/ */
protected function clean(FeedsState $state) { protected function clean(FeedsState $state) {
// We clean only if needed. // We clean only if needed.
if (!isset($this->config['update_non_existent']) || $this->config['update_non_existent'] == FEEDS_SKIP_NON_EXISTENT) { if ($this->config['update_non_existent'] == FEEDS_SKIP_NON_EXISTENT) {
return; return;
} }
$total = count($state->removeList); if ($total = count($state->removeList)) {
if ($total) {
$this->entityDeleteMultiple($state->removeList); $this->entityDeleteMultiple($state->removeList);
$state->deleted += $total; $state->deleted += $total;
} }
...@@ -437,35 +432,25 @@ abstract class FeedsProcessor extends FeedsPlugin { ...@@ -437,35 +432,25 @@ abstract class FeedsProcessor extends FeedsPlugin {
$state = $source->state(FEEDS_PROCESS_CLEAR); $state = $source->state(FEEDS_PROCESS_CLEAR);
// Build base select statement. // Build base select statement.
$info = $this->entityInfo(); $select = db_select('feeds_item')
$select = db_select($info['base table'], 'e'); ->fields('feeds_item', array('entity_id'))
$select->addField('e', $info['entity keys']['id'], 'entity_id'); ->condition('entity_type', $this->entityType())
$select->join( ->condition('id', $this->id)
'feeds_item', ->condition('feed_nid', $source->feed_nid);
'fi',
"e.{$info['entity keys']['id']} = fi.entity_id AND fi.entity_type = '{$this->entityType()}'");
$select->condition('fi.id', $this->id);
$select->condition('fi.feed_nid', $source->feed_nid);
// If there is no total, query it. // If there is no total, query it.
if (!$state->total) { if (!$state->total) {
$state->total = $select->countQuery() $state->total = $select->countQuery()->execute()->fetchField();
->execute()
->fetchField();
} }
// Delete a batch of entities. // Delete a batch of entities.
$entities = $select->range(0, $this->getLimit())->execute(); $entity_ids = $select->range(0, $this->getLimit())->execute()->fetchCol();
$entity_ids = array();
foreach ($entities as $entity) {
$entity_ids[$entity->entity_id] = $entity->entity_id;
}
$this->entityDeleteMultiple($entity_ids); $this->entityDeleteMultiple($entity_ids);
// Report progress, take into account that we may not have deleted as // Report progress, take into account that we may not have deleted as many
// many items as we have counted at first. // items as we have counted at first.
if (count($entity_ids)) { if ($deleted_count = count($entity_ids)) {
$state->deleted += count($entity_ids); $state->deleted += $deleted_count;
$state->progress($state->total, $state->deleted); $state->progress($state->total, $state->deleted);
} }
else { else {
...@@ -474,6 +459,8 @@ abstract class FeedsProcessor extends FeedsPlugin { ...@@ -474,6 +459,8 @@ abstract class FeedsProcessor extends FeedsPlugin {
// Report results when done. // Report results when done.
if ($source->progressClearing() == FEEDS_BATCH_COMPLETE) { if ($source->progressClearing() == FEEDS_BATCH_COMPLETE) {
$info = $this->entityInfo();
if ($state->deleted) { if ($state->deleted) {
$message = format_plural( $message = format_plural(
$state->deleted, $state->deleted,
...@@ -577,16 +564,12 @@ abstract class FeedsProcessor extends FeedsPlugin { ...@@ -577,16 +564,12 @@ abstract class FeedsProcessor extends FeedsPlugin {
protected function expiryQuery(FeedsSource $source, $time) { protected function expiryQuery(FeedsSource $source, $time) {
// Build base select statement. // Build base select statement.
$info = $this->entityInfo(); $info = $this->entityInfo();
$id_key = db_escape_field($info['entity keys']['id']); $id_key = $info['entity keys']['id'];
$select = db_select($info['base table'], 'e'); $select = db_select($info['base table'], 'e');
$select->addField('e', $info['entity keys']['id'], 'entity_id'); $select->addField('e', $id_key);
$select->join( $select->join('feeds_item', 'fi', "e.$id_key = fi.entity_id");
'feeds_item', $select->condition('fi.entity_type', $this->entityType());
'fi',
"e.$id_key = fi.entity_id AND fi.entity_type = :entity_type", array(
':entity_type' => $this->entityType(),
));
$select->condition('fi.id', $this->id); $select->condition('fi.id', $this->id);
$select->condition('fi.feed_nid', $source->feed_nid); $select->condition('fi.feed_nid', $source->feed_nid);
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment