function search_by_page_update_index in Search by Page 7
Same name and namespace in other branches
- 8 search_by_page.module \search_by_page_update_index()
- 6 search_by_page.module \search_by_page_update_index()
Implements hook_update_index().
Indexes the site's pages, or at least some of them (up to cron limit on searches), each cron run.
See also
_search_by_page_rebuild_all_paths()
File
- ./
search_by_page.module, line 760 - Main module file for Drupal module Search by Page.
Code
function search_by_page_update_index() {
global $user;
global $language;
$save_user = $user;
$save_language = $language;
$save_sessions = drupal_save_session();
// Rebuild the list of paths to index, resetting ones whose time has come.
_search_by_page_rebuild_all_paths(TRUE);
$users = _search_by_page_indexing_users();
// Figure out which pages to index this run - the ones that haven't been
// indexed ever or re-indexed recently, up to limits of cron.
$core_limit = (int) variable_get('search_cron_limit', 100);
$limit = (int) variable_get('sbp_cron_limit', $core_limit);
$result = db_query_range('SELECT * FROM {sbp_path} p WHERE (p.last_index_time = 0) OR (p.min_time > 0 AND :currtime > (p.last_index_time + p.min_time)) ORDER BY p.last_index_time', 0, $limit, array(
':currtime' => time(),
))
->fetchAll();
// Index each page, but don't save sessions for the indexing users.
drupal_save_session(FALSE);
// Make sure output from rendering pages does not screw up the cron run.
ob_start();
$reasons = array(
MENU_ACCESS_DENIED => t('access denied'),
MENU_NOT_FOUND => t('not found'),
MENU_SITE_OFFLINE => t('site offline'),
);
$allroles = user_roles();
$languages = language_list();
foreach ($result as $item) {
// Set up language and user.
$role = $item->role;
$rolename = $allroles[$role];
$user = $users[$rolename];
// No matter what, we want to update the time so it's marked as indexed.
// That way, if there is an error it will at least not hold up other pages
// in the next cron run.
_search_by_page_update_last_time($item->pid, time());
if (!$user) {
watchdog('search_by_page', 'Role %rid (%rname) could not be used to index PID (%pid), path (%path)', array(
'%rid' => $role,
'%rname' => $rolename,
'%pid' => $item->pid,
'%path' => $item->page_path,
), WATCHDOG_NOTICE);
// Remove content from the search index.
_search_by_page_remove_searchinfo($item->pid);
continue;
}
$language = $languages[$item->language];
// Get page title from module and optional content
$info = module_invoke($item->from_module, 'sbp_details', $item->modid, $item->environment);
if (!$info) {
// Module indicated not to index this page after all
watchdog('search_by_page', 'Content was skipped - PID (%pid), path (%path)', array(
'%pid' => $item->pid,
'%path' => $item->page_path,
), WATCHDOG_NOTICE);
// Remove content from the search index.
_search_by_page_remove_searchinfo($item->pid);
continue;
}
// Check for module override of content.
$content = '';
if (!isset($info['content'])) {
// No module override - render page to get the content.
$content = search_by_page_page_content($item->page_path);
}
else {
$content = $info['content'];
}
if (!isset($content)) {
$content = 0;
}
// $content will be either a string if found/allowed, or an error code.
// Note that an empty string is acceptable for content -- it could just
// be a page with nothing on it except the title.
if (!is_int($content)) {
if ($info['title']) {
$content = '<h1>' . $info['title'] . '</h1> ' . $content;
}
$content = search_by_page_strip_tags($content, $item->environment);
// Add to search index (already marked as updated).
search_index($item->pid, 'search_by_page', $content);
// Add to SBP table.
db_update('sbp_path')
->condition('pid', $item->pid)
->fields(array(
'page_data' => $content,
))
->execute();
}
else {
$con = $content;
if (isset($reasons[$con])) {
$con = $reasons[$con];
}
// If the site was off-line, abort the cron run
if ($content == MENU_SITE_OFFLINE) {
// In this one case, we want to reset this item so it gets indexed
// next time for sure. But there is no point in continuing the cron
// run if the site is off-line.
_search_by_page_update_last_time($item->pid, 0);
watchdog('search_by_page', 'Site is off-line, cannot index content', array(), WATCHDOG_NOTICE);
break;
}
// Remove content from the search index.
_search_by_page_remove_searchinfo($item->pid);
watchdog('search_by_page', 'Content not rendered (%con) - PID (%pid), path (%path), language (%lang)', array(
'%pid' => $item->pid,
'%path' => $item->page_path,
'%lang' => $item->language,
'%con' => $con,
), WATCHDOG_ERROR);
}
}
// Switch user back and rebuild permissions cache.
$user = $save_user;
drupal_static_reset('user_access');
$language = $save_language;
drupal_save_session($save_sessions);
_search_by_page_setup_theme();
ob_end_clean();
}