老葛的Drupal培训班 Think in Drupal
在这个模块中,我们需要实现的最后一个函数是钩子hook_search(),它允许我们使用内置的用户界面来搜索我们的遗留信息。
/**
* Implementation of hook_search().
*/
function legacysearch_search($op = 'search', $keys = NULL) {
switch ($op) {
case 'name':
return t('Tech Notes'); // Used on search tab.
case 'reset':
variable_del('legacysearch_cron_last');
variable_del('legacysearch_cron_last_id');
return;
case 'search':
// Search the index for the keywords that were entered.
$hits = do_search($keys, 'technote');
$results = array();
// Prepend URL of legacy system to each result. Assume a legacy URL
// for a given tech note is http://technotes.example.com/note.pl?3
$legacy_url = 'http://technotes.example.com/';
// We now have the IDs of the results. Pull each result
// from the legacy database.
foreach ($hits as $item) {
db_set_active('legacy');
$note = db_fetch_object(db_query("SELECT * FROM {technote} WHERE
id = %d", $item->sid));
db_set_active('default');
$results[] = array(
'link' => url($legacy_url . 'note.pl', array('query' => $item->sid, 'absolute' => TRUE)),
'type' => t('Note'),
'title' => $note->title,
'date' => $note->last_modified,
'score' => $item->score,
'snippet' => search_excerpt($keys, $note->note));
}
return $results;
}
}
在运行cron并且索引信息以后,就可以搜索技术笔记了,如图12-8所示。索引是在Drupal内部进行的,但是legacysearch_search()返回的搜索结果则来源于(并指向)遗留系统。
图 12-8.搜索一个外部的遗留数据库
评论
官方勘误:
variable_del('legacy_cron_last');
variable_del('legacy_cron_last_change');