You are here

function search_by_page_rebuild_paths in Search by Page 6

Same name and namespace in other branches
  1. 8 search_by_page.module \search_by_page_rebuild_paths()
  2. 7 search_by_page.module \search_by_page_rebuild_paths()

Rebuilds the paths table for a particular module.

Calls that module's hook_sbp_paths() implementation, which should return an array of Drupal paths to be indexed. Removes any obsolete paths, adds new ones, and updates existing ones' information, so that the correct paths will be indexed.

Parameters

$module: Module to rebuild.

$environment: Environment ID to rebuild.

$reset_items: TRUE if the last_index_time for items whose max_time has been exceeded should be reset. FALSE to leave it alone.

1 call to search_by_page_rebuild_paths()
_search_by_page_rebuild_all_paths in ./search_by_page.module
Internal function: rebuilds the paths table for all modules.

File

./search_by_page.module, line 97
Main module file for Drupal module Search by Page.

Code

function search_by_page_rebuild_paths($module, $environment, $reset_items) {

  // Find out what paths this module wants now.
  $function = $module . '_sbp_paths';
  $paths = call_user_func($function, $environment);
  if (!is_array($paths)) {
    $paths = array();
  }

  // Make this into an array that matches db structure, with a
  // unique ID as key.
  $pathsnow = array();
  $defaults = array(
    'min_time' => 1,
    'max_time' => 0,
  );
  foreach ($paths as $path => $item) {
    $item += $defaults;
    foreach ($item['languages'] as $lang) {
      $pathsnow[$lang . "_._" . $path] = array(
        'page_path' => $path,
        'language' => $lang,
        'modid' => $item['id'],
        'role' => $item['role'],
        'min_time' => $item['min_time'],
        'max_time' => $item['max_time'],
      );
    }
  }
  $newpaths = array_keys($pathsnow);

  // Now see what's in the database already.
  // Figure out what they wanted last time we did this
  $dbr = db_query("SELECT p.page_path, p.language, p.pid FROM {sbp_path} p WHERE p.from_module='%s' AND p.environment = %d", $module, $environment);
  $pathsindb = array();
  while ($item = db_fetch_object($dbr)) {
    $pathsindb[$item->language . "_._" . $item->page_path] = $item->pid;
  }
  $oldpaths = array_keys($pathsindb);

  // Resolve differences by deleting items they no longer want,
  // adding items they want now but didn't before, and modifying any
  // items whose information might potentially have changed. Do it this way
  // to preserve the information we've recorded on old items.
  $new = array_diff($newpaths, $oldpaths);
  $del = array_diff($oldpaths, $newpaths);
  $chg = array_diff($newpaths, $new);
  if (count($del) > 0) {
    foreach ($del as $key) {
      _search_by_page_remove_path($pathsindb[$key]);
    }
  }
  if (count($new) > 0) {
    foreach ($new as $key) {
      $item = $pathsnow[$key];
      db_query("INSERT INTO {sbp_path} (page_path, from_module, modid, language, role, environment, min_time, max_time) VALUES ('%s', '%s', %d, '%s', %d, %d, %d, %d)", $item['page_path'], $module, $item['modid'], $item['language'], $item['role'], $environment, $item['min_time'], $item['max_time']);
    }
  }
  if (count($chg) > 0) {
    foreach ($chg as $key) {
      $item = $pathsnow[$key];
      db_query("UPDATE {sbp_path} SET modid=%d, role=%d, min_time=%d, max_time= %d WHERE pid=%d", $item['modid'], $item['role'], $item['min_time'], $item['max_time'], $pathsindb[$key]);
    }
  }

  // Finally, reset the last index time for any item whose max time to reindex
  // has passed.
  if ($reset_items) {
    db_query("UPDATE {sbp_path} SET last_index_time=0 WHERE (max_time > 0 AND %d > (last_index_time + max_time))", time());
  }
}