You are here

function expire_node in Cache Expiration 6

Same name and namespace in other branches
  1. 7 expire.module \expire_node()

Expires a node from the cache; including related pages.

Expires front page if promoted, taxonomy terms,

Parameters

$node: node object

5 calls to expire_node()
drush_expire_nid in ./expire.drush.inc
Callback for expire-node drush command.
expire_comment in ./expire.module
Implementation of hook_comment().
expire_nodeapi in ./expire.module
Implementation of hook_nodeapi().
expire_rules_action_flush_url in ./expire.rules.inc
Expire a URL from the page cache.
_expire_votingapi in ./expire.module
Common expiry logic for votingapi.

File

./expire.module, line 180
Provides logic for page cache expiration

Code

function expire_node(&$node) {
  $paths = array();

  // Check node object
  if (empty($node->nid)) {
    return FALSE;
  }

  // Expire this node
  $paths['node'] = 'node/' . $node->nid;

  // If promoted to front page, expire front page
  if (variable_get('expire_flush_front', EXPIRE_FLUSH_FRONT) && $node->promote == 1) {
    $paths['front'] = '<front>';
  }

  // Get taxonomy terms and flush
  if (module_exists('taxonomy') && variable_get('expire_flush_node_terms', EXPIRE_FLUSH_NODE_TERMS)) {

    // Get old terms from DB
    $tids = expire_taxonomy_node_get_tids($node->nid);

    // Get old terms from static variable
    $terms = taxonomy_node_get_terms($node);
    if (!empty($terms)) {
      foreach ($terms as $term) {
        $tids[$term->tid] = $term->tid;
      }
    }

    // Get new terms from node object
    if (!empty($node->taxonomy)) {
      foreach ($node->taxonomy as $vocab) {
        if (is_array($vocab)) {
          foreach ($vocab as $term) {
            $tids[$term] = $term;
          }
        }
      }
    }
    $filenames = array();
    foreach ($tids as $tid) {
      if (is_numeric($tid)) {
        $term = taxonomy_get_term($tid);
        $paths['term' . $tid] = taxonomy_term_path($term);
      }
    }
  }

  // Get menu and flush related items in the menu.
  if (variable_get('expire_flush_menu_items', EXPIRE_FLUSH_MENU_ITEMS) != 0) {
    if (!isset($node->menu['menu_name'])) {
      menu_nodeapi($node, 'prepare');
    }
    $menu = menu_tree_all_data($node->menu['menu_name']);
    $tempa = NULL;
    $tempb = NULL;
    if (variable_get('expire_flush_menu_items', EXPIRE_FLUSH_MENU_ITEMS) == 1) {
      $links = expire_get_menu_structure($menu, FALSE, 'node/' . $node->nid, NULL, $tempa, $tempb);
    }
    elseif (variable_get('expire_flush_menu_items', EXPIRE_FLUSH_MENU_ITEMS) == 2) {
      $links = expire_get_menu_structure($menu, NULL, NULL, NULL, $tempa, $tempb);
    }
    unset($tempa);
    unset($tempb);
    $paths = array_merge($links, $paths);
  }

  // Get CCK References and flush.
  if (variable_get('expire_flush_cck_references', EXPIRE_FLUSH_CCK_REFERENCES) && module_exists('nodereference')) {
    $nids = array();
    $type = content_types($node->type);
    if ($type) {
      foreach ($type['fields'] as $field) {

        // Add referenced nodes to nids. This will clean up nodereferrer fields
        // when the referencing node is updated.
        if ($field['type'] == 'nodereference') {
          $node_field = isset($node->{$field}['field_name']) ? $node->{$field}['field_name'] : array();
          foreach ($node_field as $delta => $item) {
            if (is_numeric($item['nid'])) {
              $paths['reference' . $item['nid']] = 'node/' . $item['nid'];
            }
          }

          // Look for node referers without using nodereferrer
          $info = content_database_info($field);
          $table = $info['table'];
          $column = $info['columns']['nid']['column'];
          $results = db_query("SELECT n.nid\n            FROM {%s} nr\n            INNER JOIN {node} n USING (vid)\n            WHERE nr.%s = %d", $table, $column, $node->nid);
          while ($nid = db_result($results)) {
            if (is_numeric($nid)) {
              $paths['referenceparent' . $nid] = 'node/' . $nid;
            }
          }
        }
      }
    }

    // Get CCK references pointing to this node and flush.
    if (module_exists('nodereferrer')) {
      $nids = nodereferrer_referrers($node->nid);
      foreach ($nids as $nid) {
        if (is_numeric($nid['nid'])) {
          $paths['referrer' . $nid['nid']] = 'node/' . $nid['nid'];
        }
      }
    }
  }

  // Flush array of paths
  if (!empty($paths)) {
    $flushed = expire_cache_derivative($paths, $node);
    watchdog('expire', 'Node !nid was flushed resulting in !flushed pages being expired from the cache', array(
      '!nid' => $node->nid,
      '!flushed' => $flushed,
    ));
  }
}