You are here

function _boost_views_runit in Boost 6

3 calls to _boost_views_runit()
boost_block in ./boost.module
Implementation of hook_block().
boost_views_async in ./boost.module
Run views looking for new nodes.
_boost_view_insert in ./boost.module
Shutdown function, gets called at the very end of node creation.

File

./boost.module, line 1406
Provides static file caching for Drupal text output. Pages, Feeds, ect...

Code

function _boost_views_runit($debug = FALSE, $nid = FALSE) {
  global $_boost, $base_url, $base_path;

  // Ensure we're in the correct working directory, since some web servers (e.g. Apache) mess this up here.
  chdir(dirname($_SERVER['SCRIPT_FILENAME']));

  // Get all views that are not expired in database
  if (BOOST_FLUSH_ALL_MULTISITE) {
    $result = db_query("SELECT page_id, page_type FROM {boost_cache} WHERE page_callback = 'view' AND expire > 0 AND expire <> 434966400 GROUP BY page_id, page_type");
  }
  else {
    $result = db_query("SELECT page_id, page_type FROM {boost_cache} WHERE base_dir = '%s' AND page_callback = 'view' AND expire > 0 AND expire <> 434966400 GROUP BY page_id, page_type", BOOST_FILE_PATH);
  }

  // Setup data arrays and presets
  $data = array();
  $number_views = 0;
  $number_hits = 0;
  $views = array();
  while ($boost = db_fetch_array($result)) {
    $key = $boost['page_type'] . ' - ' . $boost['page_id'];
    $views[$key]['page_type'] = $boost['page_type'];
    $views[$key]['page_id'] = $boost['page_id'];
  }

  // Get list of views that might contain the new content.
  $views += boost_views_get_valid_array();
  $base_urls = array();
  if (module_exists('domain')) {

    // Get all domains into the base_urls array
    foreach (domain_domains() as $key => $value) {
      $domains[$key] = $key;
    }
    $fake_node = new stdClass();
    $fake_node->domains = $domains;
    $base_urls = boost_get_base_urls($fake_node);
    unset($fake_node);
  }
  else {
    $base_urls[0][] = $base_url . '/';
  }

  // get list of nodes
  $nodes = array();
  if (!empty($nid)) {
    $nodes[] = $nid;
  }
  elseif (!empty($_boost['new_nodes'])) {
    $nodes = $_boost['new_nodes'];
  }
  if (empty($nodes)) {
    return;
  }

  // Loop through each node
  foreach ($nodes as $nid) {
    $node = boost_node_get_basics($nid);
    if (!$node || !is_numeric($node->nid)) {
      continue;
    }

    // Get terms for future usage (/taxonomy/term/% view)
    $tids = boost_taxonomy_node_get_tids($node->nid);
    if (isset($_boost['nid-' . $node->nid]['tids'])) {
      $tids += $_boost['nid-' . $node->nid]['tids'];
    }
    $options = array(
      'operator' => '=',
      'value' => array(
        'value' => $node->nid,
      ),
      'group' => '0',
      'exposed' => FALSE,
      'expose' => array(
        'operator' => FALSE,
        'label' => '',
      ),
      'relationship' => 'none',
    );

    // Loop through each view in the boost cache
    foreach ($views as $boost) {
      unset($view);
      $view = views_get_view($boost['page_type'], TRUE);
      if (!is_object($view)) {
        continue;
      }

      // Only work with node views
      if ($view->base_table != 'node') {
        continue;
      }

      // Make sure the view is a valid display
      if (empty($view->display[$boost['page_id']])) {
        continue;
      }

      // Set view display
      $view
        ->set_display($boost['page_id']);
      $domains = array();
      $domain_filter = FALSE;
      if (module_exists('domain')) {

        // Check for domain access filters
        $filters = $view
          ->get_items('filter', $boost['page_id']);
        if (isset($filters['current_all']) || isset($filters['gid']) || isset($filters['domain_id'])) {

          // Only need to check the view for domains this node is published to
          $domains = $node->domains;
          foreach ($domains as $key => $value) {
            if ($key != $value) {
              unset($domains[$key]);
            }
          }
          $domain_filter = TRUE;
        }
      }
      if (!$domain_filter) {

        // boost_get_base_urls uses 0 if domain access is not installed
        $domains = array(
          0 => 0,
        );
      }
      $first = TRUE;
      $view_checked = FALSE;
      foreach ($domains as $key => $domain_id) {

        // View has to be reloaded inorder for hook_views_query_substitutions
        // to work correctly because this is in a loop
        if (!$first) {
          unset($view);
          $view = views_get_view($boost['page_type'], TRUE);
          if (!is_object($view)) {
            continue;
          }

          // Make sure the view is a valid display
          if (empty($view->display[$boost['page_id']])) {
            continue;
          }
        }
        $first = FALSE;
        $view
          ->set_display($boost['page_id']);

        // Make sure view is valid
        if (!$view_checked) {
          $h = $view->display[$boost['page_id']]->handler->default_display->options;
          $broken = FALSE;
          if (!empty($h)) {
            foreach ($h as $type => $rows) {
              if (empty($rows)) {
                continue;
              }
              if ($type == 'sorts' || $type == 'fields' || $type == 'arguments' || $type == 'filters' || $type == 'relationships') {
                $type = rtrim($type, 's');
                foreach ($rows as $id => $row) {
                  $table = $row['table'];
                  $field = $row['field'];
                  $views_data = views_fetch_data($table);
                  if (empty($views_data)) {
                    $broken = TRUE;
                    break 2;
                  }
                  if (!isset($views_data[$field][$type])) {
                    $broken = TRUE;
                    break 2;
                  }
                }
              }
            }
          }
          unset($h);
          if ($broken) {
            break;
          }
          $view_checked = TRUE;
        }

        // Filter to just this nid
        $view
          ->add_item($boost['page_id'], 'filter', 'node', 'nid', $options);

        // Set ***CURRENT_DOMAIN*** variable
        // See domain_views_query_substitutions()
        if ($domain_filter) {
          domain_set_domain($domain_id, TRUE);
        }
        $view
          ->pre_execute();
        $view
          ->execute();

        // Increment Counter
        $number_views++;
        foreach ($view->result as $item) {
          if ($item->nid == $nid) {
            foreach ($base_urls as $key => $value) {
              $save = TRUE;
              if ($domain_filter && $key != $domain_id) {
                $save = FALSE;
              }
              if ($save) {
                foreach ($value as $url) {
                  $parts = parse_url($url);
                  $file_path = boost_cache_directory($parts['host'], FALSE);
                  $hash = $file_path . 'view' . $boost['page_type'] . $boost['page_id'];
                  $data[$hash] = array(
                    'base_dir' => $file_path,
                    'page_callback' => 'view',
                    'page_type' => $boost['page_type'],
                    'page_id' => $boost['page_id'],
                  );
                  $number_hits++;
                }
              }
            }
          }
        }

        // Free memory
        if (isset($view) && is_object($view) && method_exists($view, 'destroy')) {
          $view
            ->destroy();

          // Fix views bug http://drupal.org/node/988680
          unset($view->old_view);
        }
        unset($view);
      }

      // Free memory
      if (isset($view) && is_object($view) && method_exists($view, 'destroy')) {
        $view
          ->destroy();

        // Fix views bug http://drupal.org/node/988680
        unset($view->old_view);
      }
      unset($view);
      if ($domain_filter) {
        domain_reset_domain(TRUE);
      }
    }
  }
  if (!$debug) {
    if ($data) {
      $flushed = boost_cache_expire_router($data);
    }
    if (BOOST_VERBOSE >= 7 && isset($_boost['verbose_option_selected']['boost_view_insert'])) {
      watchdog('boost', 'Debug: _boost_view_insert() <br />%count Views Searched (%viewnames) %times times; %hits of them contain the new nodes (%nids) and where thus flushed. As a result of this %flushed pages where expired from the boost cache.<br /> !domains', array(
        '%nids' => implode(', ', $nodes),
        '%count' => count($views),
        '%times' => $number_views,
        '%hits' => $number_hits,
        '%flushed' => $flushed,
        '!domains' => str_replace('    ', '&nbsp;&nbsp;&nbsp;&nbsp;', nl2br(htmlentities(print_r($base_urls, TRUE)))),
        '%viewnames' => implode(', ', array_keys($views)),
      ));
    }
    $processed = TRUE;
  }
  else {
    return array(
      'in-view' => $data,
      'in-cache' => boost_cache_expire_router($data, FALSE, FALSE, TRUE),
    );
  }
}