You are here

function mostpopular_refresh in Drupal Most Popular 7

Same name and namespace in other branches
  1. 6 mostpopular.api.php \mostpopular_refresh()

Refreshes data from each service by invoking the refresh callback for each service.

1 call to mostpopular_refresh()
mostpopular_cron in ./mostpopular.module
Implements hook_cron().
1 string reference to 'mostpopular_refresh'
mostpopular_menu in ./mostpopular.module
Implements hook_menu().

File

./mostpopular.module, line 1004
The main file for the Most Popular module.

Code

function mostpopular_refresh($cron = FALSE) {
  $t = '';

  // Loop through all the local blocks
  $blocks = mostpopular_blocks_local();
  foreach ($blocks as $bid => $block) {
    $services = mostpopular_service_load_by_block($bid);
    $intervals = mostpopular_interval_load_by_block($bid);
    foreach ($services as $sid => $service) {
      $count = 0;
      $t .= '<div>';
      $t .= t("Refreshing %title", array(
        '%title' => $service->title,
      ));
      $status = array();
      foreach ($intervals as $iid => $interval) {

        // Get the number of seconds that this interval spans.
        $now = time();
        $span = $now - strtotime($interval->string, $now);

        // Get the next time this service should run.
        $row = db_select('mostpopular_last_run', 'r')
          ->fields('r', array(
          'last_run',
          'next_run',
        ))
          ->condition('sid', $sid)
          ->condition('iid', $iid)
          ->range(0, 1)
          ->execute()
          ->fetch();
        if ($row) {
          $last_run = $row->last_run;
          $next_run = $row->next_run;
        }
        else {
          $last_run = 0;
          $next_run = $now;
        }

        // When running as a cron job, ask the service how often it should refresh.
        // When running as a page request, refresh the service immediately.
        if (!$cron || $next_run <= $now) {

          // Invoke the module
          $out = _mostpopular_invoke('refresh', $service, $block, $span, $last_run);

          // If the module returned any results, save them to the database.
          if ($out !== FALSE) {
            if (count($out) > 0) {

              // Remove the previous results, if there are any
              db_delete('mostpopular_item')
                ->condition('sid', $sid)
                ->condition('iid', $iid)
                ->execute();

              // Write the new results to the cache table
              foreach ($out as $value) {
                $value->sid = $sid;
                $value->iid = $iid;

                // Fill in the entity properties if they are not already set.
                if (!empty($value->entity_type) && !empty($value->entity_id)) {
                  if (empty($value->path) || empty($value->title)) {
                    $entity = reset(entity_load($value->entity_type, array(
                      $value->entity_id,
                    )));
                    if ($entity && empty($value->path)) {
                      $uri = entity_uri($value->entity_type, $entity);
                      if (isset($uri['path'])) {
                        $value->path = $uri['path'];
                      }
                    }
                    if ($entity && empty($value->title)) {
                      $value->title = entity_label($value->entity_type, $entity);
                    }
                  }
                }

                // Save the URL as an absolute path, so we can reuse it on other sites.
                $value->url = url($value->path, array(
                  'absolute' => TRUE,
                ));
                drupal_write_record('mostpopular_item', $value);
              }

              // Since there were items returned, the service is OK.
              if ($service->status != MOSTPOPULAR_SERVICE_STATUS_OK) {
                $service->status = MOSTPOPULAR_SERVICE_STATUS_OK;
                mostpopular_service_save($service);
              }
            }

            // Ask the service when it should next run on this interval.
            $last_run = $now;
            $next_run = _mostpopular_invoke('next_run', $service, $span, $last_run);
            db_merge('mostpopular_last_run')
              ->fields(array(
              'last_run' => $last_run,
              'next_run' => $next_run,
            ))
              ->key(array(
              'sid' => $sid,
              'iid' => $iid,
            ))
              ->execute();

            // Clear the item cache
            cache_clear_all("mostpopular_items:{$bid}:{$sid}:{$iid}", 'cache_block');
            $status[] = t("%interval: Found %count items", array(
              '%interval' => $interval->title,
              '%count' => count($out),
            ));
          }
          else {
            $status[] = t("%interval: Error retrieving results", array(
              '%interval' => $interval->title,
            ));
            $service->status = MOSTPOPULAR_SERVICE_STATUS_ERROR;
            mostpopular_service_save($service);
          }
        }
        else {
          $status[] = t('%interval: No need to refresh yet', array(
            '%interval' => $interval->title,
          ));
        }
      }
      $t .= theme('item_list', array(
        'items' => $status,
      ));
      $t .= '</div><br/>';
    }
  }
  if (empty($t)) {
    $t .= t("You must first enable services.  Go to !link", array(
      '!link' => l(t('the services tab'), 'admin/settings/mostpopular/services'),
    ));
  }
  return $t;
}