You are here

function boost_cache_set_node_relationships in Boost 6

Creates a parent child relationship for pages like views.

Parameters

$relationships: Array of $router_item array "objects" Required ['child_page_callback'] ['child_page_type'] ['child_page_id'] Optional ['base_dir'] ['page_callback'] ['page_type'] ['page_id']

If Optional is not set it will use values in $GLOBALS['_boost_router_item']

1 call to boost_cache_set_node_relationships()
boost_cache_set in ./boost.module
Replaces/Sets the cached contents of the specified page, if stale.
1 string reference to 'boost_cache_set_node_relationships'
boost.module in ./boost.module
Provides static file caching for Drupal text output. Pages, Feeds, ect...

File

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

Code

function boost_cache_set_node_relationships($relationships) {
  global $base_root, $_boost;
  $url = $base_root . request_uri();
  if (variable_get('boost_store_url_percent_enc', FALSE)) {
    $url = urldecode(rawurlencode($url));
  }
  $hash_url = md5($url);
  $router_item = _boost_get_menu_router();
  if (!is_array($relationships) || empty($relationships)) {
    return FALSE;
  }

  // Grab all entries related to this url
  $old_data = array();
  $results = db_query("SELECT hash, timestamp FROM {boost_cache_relationships} WHERE hash_url = '%s'", $hash_url);
  while ($row = db_fetch_array($results)) {
    $old_data[$row['hash']] = $row['timestamp'];
  }
  $counter = 0;
  $new_data = array();
  foreach ($relationships as $data) {

    // If one of the required items is not set, skip this entry
    if (!isset($data['child_page_callback']) || !isset($data['child_page_type']) || !isset($data['child_page_id'])) {
      if (BOOST_VERBOSE >= 5) {
        watchdog('boost', 'boost_cache_set_node_relationships() <br /> child_page_* was not set.<br /> !data<br /> !backtrace', array(
          '!data' => boost_print_r($data, TRUE, TRUE),
          '!backtrace' => boost_backtrace(FALSE),
        ));
      }
      continue;
    }

    // Set the optional parameters
    $data['base_dir'] = isset($data['base_dir']) ? $data['base_dir'] : BOOST_FILE_PATH;
    $data['page_callback'] = isset($data['page_callback']) ? $data['page_callback'] : $router_item['page_callback'];
    $data['page_type'] = isset($data['page_type']) ? $data['page_type'] : $router_item['page_type'];
    $data['page_id'] = isset($data['page_id']) ? $data['page_id'] : $router_item['page_id'];

    // Sanity Checks
    foreach ($data as $key => $value) {
      $loop = 0;
      while (is_array($data[$key]) && $loop < 25) {
        $data[$key] = array_pop($value);
        $loop++;
      }
    }
    foreach ($data as $key => $value) {
      if (is_object($value) || is_array($value)) {
        if (BOOST_VERBOSE >= 5) {
          watchdog('boost', 'boost_cache_set_node_relationships() <br />"!key" has a value of "!value"; should be a string or int.', array(
            '!key' => $key,
            '!value' => $value,
          ));
        }
        continue;
      }
    }

    // Skip if this is referencing its self.
    if ($data['page_callback'] == $data['child_page_callback'] && $data['page_type'] == $data['child_page_type'] && $data['page_id'] == $data['child_page_id']) {
      continue;
    }

    // Create the primary key
    $data['hash'] = md5($data['base_dir'] . $data['page_callback'] . $data['page_type'] . $data['page_id'] . $data['child_page_callback'] . $data['child_page_type'] . $data['child_page_id']);
    $new_data[] = $data;
  }

  // Count number of entries
  $update_required = FALSE;
  if (count($new_data) !== count($old_data)) {
    $update_required = TRUE;
  }

  // See if new and old data is not the same
  if (!$update_required) {
    foreach ($new_data as $data) {
      if (empty($old_data[$data['hash']])) {
        $update_required = TRUE;
        break;
      }
    }
  }
  if ($update_required) {
    foreach ($new_data as $data) {

      // Insert data into database
      db_query("UPDATE {boost_cache_relationships}\n      SET base_dir = '%s',\n      page_callback = '%s',\n      page_type = '%s',\n      page_id = '%s',\n      child_page_callback = '%s',\n      child_page_type = '%s',\n      child_page_id = '%s',\n      hash_url = '%s',\n      timestamp = '%d'\n      WHERE hash = '%s'", $data['base_dir'], $data['page_callback'], $data['page_type'], $data['page_id'], $data['child_page_callback'], $data['child_page_type'], $data['child_page_id'], $hash_url, BOOST_TIME, $data['hash']);
      if (!db_affected_rows()) {
        @db_query("INSERT INTO {boost_cache_relationships} (hash, base_dir, page_callback, page_type, page_id, child_page_callback, child_page_type, child_page_id, hash_url, timestamp) VALUES ('%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', %d)", $data['hash'], $data['base_dir'], $data['page_callback'], $data['page_type'], $data['page_id'], $data['child_page_callback'], $data['child_page_type'], $data['child_page_id'], $hash_url, BOOST_TIME);
      }
      $counter++;
    }
    $removed = boost_cache_prune_node_relationship($hash_url);
    if (BOOST_VERBOSE >= 7 && isset($_boost['verbose_option_selected']['boost_cache_set_node_relationships'])) {
      watchdog('boost', 'Debug: boost_cache_set_node_relationships() <br />!num of !total given entries to the boost_cache_relationships table added or updated; !removed entries removed due to them being outdated. !dump', array(
        '!num' => $counter,
        '!total' => count($GLOBALS['_boost_relationships']),
        '!removed' => $removed,
        '!dump' => str_replace('    ', '&nbsp;&nbsp;&nbsp;&nbsp;', nl2br(htmlentities(print_r($relationships, TRUE)))),
      ));
    }
  }
  unset($GLOBALS['_boost_relationships']);
  return TRUE;
}