You are here

function flickrcachewarmer_run in Flickr 7

Virtually visits all nodes of selected content types to ensure the cache of these pages is rebuild to avoid long page loads for a real visitor. Note that with the HEAD method the server MUST NOT return a message-body in the response. It turns out Drupal will rebuild the full page's cache anyway.

2 calls to flickrcachewarmer_run()
flickrcachewarmer_admin_settings_submit in cachewarmer/flickrcachewarmer.admin.inc
Submit form data.
flickrcachewarmer_cron in cachewarmer/flickrcachewarmer.module
Implements hook_cron().
2 string references to 'flickrcachewarmer_run'
flickrcachewarmer_form_flickr_admin_settings_alter in cachewarmer/flickrcachewarmer.admin.inc
Implements hook_form_FORM_ID_alter().
flickrcachewarmer_uninstall in cachewarmer/flickrcachewarmer.install
Implements hook_uninstall().

File

cachewarmer/flickrcachewarmer.module, line 80
The Flickr Cache Warmer module.

Code

function flickrcachewarmer_run($nids) {

  // Visit each node.
  foreach ($nids as $nid) {
    if ((variable_get('flickr_curl2', 0) || !function_exists('stream_socket_client')) && function_exists('curl_version')) {
      $result = flickr_curl_http_request($GLOBALS['base_url'] . '/node/' . $nid, array(
        'method' => 'HEAD',
      ));
      $cmethod = 'cURL';
    }
    elseif (function_exists('stream_socket_client')) {
      $result = drupal_http_request($GLOBALS['base_url'] . '/node/' . $nid, array(
        'method' => 'HEAD',
      ));
      $cmethod = 'stream_socket_client';
    }
    if (isset($result)) {
      if ($result->code != 200 && ($cmethod == 'stream_socket_client' || $cmethod == 'none') && function_exists('curl_version')) {

        // Try to use cURL when drupal_http_request returns a different code
        // than 200 (valid request, no errors). Most likely are 403 (forbidden)
        // or 408 (Request Timeout).
        $result = flickr_curl_http_request($GLOBALS['base_url'] . '/node/' . $nid, array(
          'method' => 'HEAD',
        ));
        $cmethod = 'cURL';
        $message = t('Automatic fallback to the cURL connection method kicked in on nid %nid to handle the request. Result code from the failing request', array(
          '%nid' => $nid,
        )) . ': ' . $result->code;
        drupal_set_message($message, 'warning', FALSE);
        watchdog('flickr', $message, array(), WATCHDOG_WARNING);

        // Even the cURL method returns an error.
        if ($result->code != 200) {

          // Debug info.
          if (variable_get('flickr_debug', 0) == 2 && function_exists('dpm')) {
            dpm(t("Value of 'result' on nid %nid with error in 'function flickr_request()' with connection method '%cmethod' in 'flickr.inc'", array(
              '%nid' => $nid,
              '%cmethod' => $cmethod,
            )) . ':');
            dpm($result);
          }
          flickr_set_error(t("Could not connect to Flickr, Error: %error", array(
            '%error' => $result->error,
          )));
        }
      }
    }
    else {
      $message = t("There seems to be no connection method available on your server. Neither 'stream_socket_client' nor 'cURL'.");
      drupal_set_message($message, 'error', FALSE);
      watchdog('flickr', $message, array(), WATCHDOG_ERROR);
    }
  }
}