Last active
December 17, 2015 21:59
-
-
Save benclark/5678811 to your computer and use it in GitHub Desktop.
Return all existing node IDs from a given FeedsImportBatch object. Written to be called from an implementation of hook_feeds_after_parse(), but could be called at any point in the feed importer operation, provided that the batch still exists (i.e. can't be called from hook_feeds_after_import() because the batch is explicitly set to NULL before c…
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Return all existing node IDs from a given FeedsImportBatch object. | |
* | |
* @param $processor | |
* Instance of FeedsNodeProcessor. | |
* @param $source | |
* Instance of FeedsSource. | |
* | |
* @return | |
* Array of node IDs that match on the unique targets of each parsed item in | |
* the FeedsImportBatch. Empty array if none found. | |
*/ | |
function MYMODULE_feeds_after_parse_get_existing_nids(FeedsNodeProcessor $processor, FeedsSource $source) { | |
$nids = array(); | |
// Clone the batch so shiftItem() doesn't occur on the actual batch. | |
$batch = clone($source->batch); | |
if (!($batch instanceOf FeedsImportBatch)) { | |
// Ensure that the cloned batch is valid. | |
return $nids; | |
} | |
while ($item = $batch->shiftItem()) { | |
// Copied from FeedsProcessor::uniqueTargets(), which is a protected | |
// method and cannot be called from this context. | |
$parser = feeds_importer($processor->id)->parser; | |
$targets = array(); | |
foreach ($processor->config['mappings'] as $mapping) { | |
if ($mapping['unique']) { | |
// Invoke the parser's getSourceElement to retrieve the value for this | |
// mapping's source. | |
$targets[$mapping['target']] = $parser->getSourceElement($batch, $mapping['source']); | |
} | |
} | |
// Copied from FeedsNodeProcessor::existingItemId(), which is a | |
// protected method and cannot be called from this context. | |
foreach ($targets as $target => $value) { | |
switch ($target) { | |
case 'nid': | |
$nid = db_result(db_query("SELECT nid FROM {node} WHERE nid = %d", $value)); | |
break; | |
case 'url': | |
$nid = db_result(db_query("SELECT nid FROM {feeds_node_item} WHERE feed_nid = %d AND id = '%s' AND url = '%s'", $source->feed_nid, $source->id, $value)); | |
break; | |
case 'guid': | |
$nid = db_result(db_query("SELECT nid FROM {feeds_node_item} WHERE feed_nid = %d AND id = '%s' AND guid = '%s'", $source->feed_nid, $source->id, $value)); | |
break; | |
default: | |
$nid = 0; | |
} | |
if ($nid) { | |
$nids[$nid] = $nid; | |
} | |
} | |
} | |
return $nids; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment