You are here

对非节点的内容进行索引:hook_update_index()(3)

g089h515r806 的头像
Submitted by g089h515r806 on 星期四, 2009-08-20 15:04

老葛的Drupal培训班 Think in Drupal

继续前进,在你的文本编辑器中不要关闭legacysearch.module,我们将添加hook_update_index(),从而将遗留数据提供给HTML索引器。在创建了这些文件以后,现在我们就可以启用这个模块了。
 
/**
 * Implementation of hook_update_index().
 */
function legacysearch_update_index() {
    // We define these variables as global so our shutdown function can
    // access them.
    global $last_change, $last_id;
 
    // If PHP times out while indexing, run a function to save
    // information about how far we got so we can continue at next cron run.
    register_shutdown_function('legacysearch_update_shutdown');
 
    $last_id = variable_get('legacysearch_cron_last_id', 0);
    $last_change = variable_get('legacysearch_cron_last_change', 0);
 
    // Switch database connection to legacy database.
    db_set_active('legacy');
    $result = db_query("SELECT id, title, note, last_modified
                        FROM {technote}
                        WHERE (id > %d) OR (last_modified > %d)
                        ORDER BY last_modified ASC", $last_id, $last_change);
 
    // Switch database connection back to Drupal database.
    db_set_active('default');
 
    // Feed the external information to the search indexer.
    while ($data = db_fetch_object($result)) {
        $last_change = $data->last_modified;
        $last_id = $data->id;
 
        $text = '<h1>' . check_plain($data->title) . '</h1>' . $data->note;
 
        search_index($data->id, 'technote', $text);
    }
}
 
    每片内容都被传递给了search_index(),这里面包括一个标识符(在这里,就是遗留数据库表中的ID列的值),内容的类型(我们将类型设为technote;索引Drupal内容时它一般为节点或者用户),以及要被索引的文本。

Drupal版本: