function search_by_page_rebuild_paths in Search by Page 7
Same name and namespace in other branches
- 8 search_by_page.module \search_by_page_rebuild_paths()
- 6 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= :modname AND p.environment = :envid", array(
':modname' => $module,
':envid' => $environment,
))
->fetchAll();
$pathsindb = array();
foreach ($dbr as $item) {
$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_insert('sbp_path')
->fields(array(
'page_path' => $item['page_path'],
'from_module' => $module,
'modid' => $item['modid'],
'language' => $item['language'],
'role' => $item['role'],
'environment' => $environment,
'min_time' => $item['min_time'],
'max_time' => $item['max_time'],
))
->execute();
}
}
if (count($chg) > 0) {
foreach ($chg as $key) {
$item = $pathsnow[$key];
db_update('sbp_path')
->fields(array(
'modid' => $item['modid'],
'role' => $item['role'],
'min_time' => $item['min_time'],
'max_time' => $item['max_time'],
))
->condition('pid', $pathsindb[$key])
->execute();
}
}
// 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 :currtime > (last_index_time + max_time))", array(
':currtime' => time(),
));
}
}