public function InstagramFeedsPluginsPager::fetch in Instagram Feeds 7
Implements parent::fetch().
File
- modules/
instagram_feeds_plugins/ plugins/ feeds/ InstagramFeedsPluginsPager.inc, line 46 - Home of the InstagramFeedsPluginsPager.
Class
- InstagramFeedsPluginsPager
- Fetches data via Instagram API with the ability to page all results.
Code
public function fetch(FeedsSource $source) {
// Taken from parent::fetch().
$source_config = $source
->getConfigFor($this);
if ($this->config['use_pubsubhubbub'] && ($raw = $this
->subscriber($source->feed_nid)
->receive())) {
return new InstagramFeedsPluginsPagerResult($raw);
}
// Initiate the fetch state.
$state = $source
->state(FEEDS_FETCH);
// Populate the state.
if (empty($state->inited)) {
$state->inited = TRUE;
$state->total = $this->config['max_pages'];
$state->total_img = $this->config['number_of_images'];
$state->item_count = array();
$state->count = 0;
$state->next_url = $source_config['source'];
$state->page_until = $this
->pageUntilID($source->feed_nid);
}
// Increment the operation count.
$state->count++;
// If there is no maximum amount of pages, then continue to
// increment the total count so the batch doesn't end until
// we finish paging.
if (!$this->config['max_pages'] && (!$this->config['number_of_images'] || !isset($state->item_count[$source_config['source']]) || isset($state->item_count[$source_config['source']]) && $state->item_count[$source_config['source']] < $this->config['number_of_images'])) {
$state->total = $state->total + 2;
}
elseif (isset($state->item_count[$source_config['source']]) && $state->item_count[$source_config['source']] >= $this->config['number_of_images']) {
$state->total = $state->count - 1;
}
// Set the progress.
$state
->progress($state->total, $state->count);
// Fetch the next URL, if there is one.
if (isset($state->next_url) && $state->next_url) {
$result = new InstagramFeedsPluginsPagerResult($state->next_url);
}
// Unset the next URL so we can determine if there is a new one.
$state->next_url = NULL;
// Extract the feed result data.
if (isset($result) && ($data = $result
->getRaw())) {
// Decode the JSON.
if ($data = drupal_json_decode($data)) {
// Determine the next URL, if there is one.
if (isset($data['pagination']['next_url'])) {
$state->next_url = $data['pagination']['next_url'];
}
// See if we've reached the item we need to page until.
if (isset($state->page_until) && $state->page_until) {
if (isset($data['data']) && is_array($data['data'])) {
// Iterate all of the results.
if (!$state->item_count || !isset($state->item_count[$source_config['source']])) {
if (!is_array($state->item_count)) {
$state->item_count = array();
}
$state->item_count[$source_config['source']] = 0;
}
foreach ($data['data'] as $item) {
if (isset($item['id'])) {
// Stop if we've reached the ID we're looking for.
if ($item['id'] == $state->page_until || $state->total_img && $state->total_img < $state->item_count[$source_config['source']]) {
// Remove the next URL so the operation stops.
$state->next_url = NULL;
break;
}
}
}
}
}
}
}
// If there is not a next URL, then stop the operation.
if (!$state->next_url) {
$state
->progress($state->total, $state->total);
}
// @todo: What's best to return if we have no $result.
return isset($result) && $result ? $result : new InstagramFeedsPluginsPagerResult('');
}