| 1 |
<?php |
<?php |
| 2 |
// $Id: luceneapi_node.module,v 1.41 2009/11/15 19:22:14 cpliakas Exp $ |
// $Id: luceneapi_node.module,v 1.42 2009/11/16 03:14:34 cpliakas Exp $ |
| 3 |
|
|
| 4 |
/** |
/** |
| 5 |
* @file |
* @file |
| 14 |
function luceneapi_node_menu() { |
function luceneapi_node_menu() { |
| 15 |
$items = array(); |
$items = array(); |
| 16 |
|
|
| 17 |
$items['admin/settings/search/luceneapi_node/bias'] = array( |
$items['admin/settings/luceneapi_node/bias'] = array( |
| 18 |
'title' => 'Content bias', |
'title' => 'Content bias', |
| 19 |
'page callback' => 'drupal_get_form', |
'page callback' => 'drupal_get_form', |
| 20 |
'page arguments' => array('luceneapi_node_admin_settings'), |
'page arguments' => array('luceneapi_node_admin_settings'), |
| 413 |
} |
} |
| 414 |
|
|
| 415 |
/** |
/** |
| 416 |
* Implementation of hook_search(). |
* Implement hook_search_info(). |
| 417 |
*/ |
*/ |
| 418 |
function luceneapi_node_search($op, $keys = NULL) { |
function luceneapi_node_search_info() { |
| 419 |
switch ($op) { |
return array( |
| 420 |
case 'name': |
'title' => luceneapi_node_variable_get('search_name'), |
| 421 |
return luceneapi_node_variable_get('search_name'); |
'path' => 'luceneapi_node', |
| 422 |
|
); |
| 423 |
case 'reset': |
} |
|
variable_del('luceneapi_node:last_change'); |
|
|
variable_del('luceneapi_node:last_nid'); |
|
|
return; |
|
| 424 |
|
|
| 425 |
case 'search': |
/** |
| 426 |
$results = array(); |
* Implement hook_search_reset(). |
| 427 |
try { |
*/ |
| 428 |
if (!$index = luceneapi_index_open('luceneapi_node', $errstr)) { |
function luceneapi_node_search_reset() { |
| 429 |
throw new LuceneAPI_Exception($errstr); |
variable_del('luceneapi_node:last_change'); |
| 430 |
} |
variable_del('luceneapi_node:last_nid'); |
| 431 |
|
} |
| 432 |
|
|
| 433 |
// executes search, builds results array |
/** |
| 434 |
$hits = luceneapi_do_search($index, $keys, $positive_keys, TRUE); |
* Implement hook_search_execute(). |
| 435 |
foreach ($hits as $hit) { |
*/ |
| 436 |
try { |
function luceneapi_node_search_execute($keys = NULL) { |
| 437 |
|
$results = array(); |
| 438 |
// loads the body of the node, thorws exception on errors |
try { |
| 439 |
// NOTE: this is more efficient than $hit->nid |
if (!$index = luceneapi_index_open('luceneapi_node', $errstr)) { |
| 440 |
$nid = $index->getDocument($hit->id)->getFieldValue('nid'); |
throw new LuceneAPI_Exception($errstr); |
| 441 |
if (!$node = node_load($nid)) { |
} |
|
throw new LuceneAPI_Exception(t('Error loading node.')); |
|
|
} |
|
| 442 |
|
|
| 443 |
// gets the contents of the node from the database |
// executes search, builds results array |
| 444 |
$node->build_mode = NODE_BUILD_SEARCH_RESULT; |
$hits = luceneapi_do_search($index, $keys, $positive_keys, TRUE); |
| 445 |
$node = node_build_content($node, FALSE, FALSE); |
foreach ($hits as $hit) { |
| 446 |
$node->body = drupal_render($node->content); |
try { |
|
|
|
|
// fetch comments, taxonomy terms for snippet |
|
|
$node->body .= module_invoke('comment', 'nodeapi', $node, 'update index'); |
|
|
$node->body .= module_invoke('taxonomy', 'nodeapi', $node, 'update index'); |
|
|
|
|
|
// returns standard result item array |
|
|
$result = array( |
|
|
'link' => url('node/'. $node->nid, array('absolute' => TRUE)), |
|
|
'type' => node_get_types('name', $node), |
|
|
'title' => $node->title, |
|
|
'user' => theme('username', $node), |
|
|
'date' => $node->changed, |
|
|
'node' => $node, |
|
|
'extra' => node_invoke_nodeapi($node, 'search result'), |
|
|
'score' => $hit->score, |
|
|
'snippet' => luceneapi_excerpt($positive_keys, $node->body), |
|
|
); |
|
|
} |
|
|
catch (Exception $e) { |
|
|
$result = array( |
|
|
'type' => '', |
|
|
'title' => t('Error loading page'), |
|
|
'score' => $hit->score, |
|
|
'snippet' => t('The contents of the page could not be retrieved.'), |
|
|
); |
|
|
} |
|
| 447 |
|
|
| 448 |
// allows modules to alter the item's result array, adds to results |
// loads the body of the node, thorws exception on errors |
| 449 |
drupal_alter('luceneapi_result', $result, 'luceneapi_node', 'node'); |
// NOTE: this is more efficient than $hit->nid |
| 450 |
$results[] = $result; |
$nid = $index->getDocument($hit->id)->getFieldValue('nid'); |
| 451 |
|
if (!$node = node_load($nid)) { |
| 452 |
|
throw new LuceneAPI_Exception(t('Error loading node.')); |
| 453 |
} |
} |
| 454 |
|
|
| 455 |
|
// gets the contents of the node from the database |
| 456 |
|
$node->build_mode = NODE_BUILD_SEARCH_RESULT; |
| 457 |
|
$node = node_build_content($node, FALSE, FALSE); |
| 458 |
|
$node->body = drupal_render($node->content); |
| 459 |
|
|
| 460 |
|
// fetch comments, taxonomy terms for snippet |
| 461 |
|
$node->body .= module_invoke('comment', 'nodeapi', $node, 'update index'); |
| 462 |
|
$node->body .= module_invoke('taxonomy', 'nodeapi', $node, 'update index'); |
| 463 |
|
|
| 464 |
|
// returns standard result item array |
| 465 |
|
$result = array( |
| 466 |
|
'link' => url('node/'. $node->nid, array('absolute' => TRUE)), |
| 467 |
|
'type' => node_get_types('name', $node), |
| 468 |
|
'title' => $node->title, |
| 469 |
|
'user' => theme('username', $node), |
| 470 |
|
'date' => $node->changed, |
| 471 |
|
'node' => $node, |
| 472 |
|
'extra' => node_invoke_nodeapi($node, 'search result'), |
| 473 |
|
'score' => $hit->score, |
| 474 |
|
'snippet' => luceneapi_excerpt($positive_keys, $node->body), |
| 475 |
|
); |
| 476 |
} |
} |
| 477 |
catch (Exception $e) { |
catch (Exception $e) { |
| 478 |
luceneapi_throw_error($e, WATCHDOG_ERROR, 'luceneapi_node'); |
$result = array( |
| 479 |
|
'type' => '', |
| 480 |
|
'title' => t('Error loading page'), |
| 481 |
|
'score' => $hit->score, |
| 482 |
|
'snippet' => t('The contents of the page could not be retrieved.'), |
| 483 |
|
); |
| 484 |
} |
} |
|
return $results; |
|
| 485 |
|
|
| 486 |
case 'status': |
// allows modules to alter the item's result array, adds to results |
| 487 |
$total = db_result(db_query(luceneapi_node_type_condition_add( |
drupal_alter('luceneapi_result', $result, 'luceneapi_node', 'node'); |
| 488 |
'SELECT COUNT(*) FROM {node} n WHERE n.status = 1' |
$results[] = $result; |
| 489 |
))); |
} |
|
|
|
|
$last_nid = variable_get('luceneapi_node:last_nid', 0); |
|
|
$last = variable_get('luceneapi_node:last_change', 0); |
|
|
$params = array($last, $last_nid, $last, $last, $last); |
|
| 490 |
|
|
| 491 |
// SQL that gets nodes that have changed since the last Lucene index update |
} |
| 492 |
$sql = 'SELECT COUNT(*)' |
catch (Exception $e) { |
| 493 |
.' FROM {node} n' |
luceneapi_throw_error($e, WATCHDOG_ERROR, 'luceneapi_node'); |
| 494 |
.' LEFT JOIN {node_comment_statistics} c ON n.nid = c.nid' |
} |
| 495 |
.' WHERE n.status = 1' |
return $results; |
| 496 |
.' AND (' |
} |
|
.' (GREATEST(n.changed, c.last_comment_timestamp) = %d AND n.nid > %d) OR' |
|
|
.' (n.changed > %d OR c.last_comment_timestamp > %d)' |
|
|
.' )'; |
|
| 497 |
|
|
| 498 |
// adds statement to ignore excluded content types, gets num remaining |
/** |
| 499 |
$sql = luceneapi_node_type_condition_add($sql); |
* Implementation of hook_search(). |
| 500 |
$remaining = db_result(db_query($sql, $params)); |
*/ |
| 501 |
|
function luceneapi_node_search($op, $keys = NULL) { |
| 502 |
|
switch ($op) { |
| 503 |
|
|
| 504 |
|
|
| 505 |
|
case 'search': |
| 506 |
|
|
| 507 |
|
|
| 508 |
|
case 'status': |
| 509 |
|
|
|
return array( |
|
|
'remaining' => $remaining, |
|
|
'total' => $total |
|
|
); |
|
| 510 |
} |
} |
| 511 |
} |
} |
| 512 |
|
|
| 513 |
/** |
/** |
| 514 |
|
* Implement hook_search_status(). |
| 515 |
|
*/ |
| 516 |
|
function luceneapi_node_search_status() { |
| 517 |
|
$total = db_result(db_query(luceneapi_node_type_condition_add( |
| 518 |
|
'SELECT COUNT(*) FROM {node} n WHERE n.status = 1' |
| 519 |
|
))); |
| 520 |
|
|
| 521 |
|
$last_nid = variable_get('luceneapi_node:last_nid', 0); |
| 522 |
|
$last = variable_get('luceneapi_node:last_change', 0); |
| 523 |
|
$params = array($last, $last_nid, $last, $last, $last); |
| 524 |
|
|
| 525 |
|
// SQL that gets nodes that have changed since the last Lucene index update |
| 526 |
|
$sql = 'SELECT COUNT(*)' |
| 527 |
|
.' FROM {node} n' |
| 528 |
|
.' LEFT JOIN {node_comment_statistics} c ON n.nid = c.nid' |
| 529 |
|
.' WHERE n.status = 1' |
| 530 |
|
.' AND (' |
| 531 |
|
.' (GREATEST(n.changed, c.last_comment_timestamp) = %d AND n.nid > %d) OR' |
| 532 |
|
.' (n.changed > %d OR c.last_comment_timestamp > %d)' |
| 533 |
|
.' )'; |
| 534 |
|
|
| 535 |
|
// adds statement to ignore excluded content types, gets num remaining |
| 536 |
|
$sql = luceneapi_node_type_condition_add($sql); |
| 537 |
|
$remaining = db_result(db_query($sql, $params)); |
| 538 |
|
|
| 539 |
|
return array( |
| 540 |
|
'remaining' => $remaining, |
| 541 |
|
'total' => $total |
| 542 |
|
); |
| 543 |
|
} |
| 544 |
|
|
| 545 |
|
/** |
| 546 |
* Implementation of hook_luceneapi_query_rebuild(). |
* Implementation of hook_luceneapi_query_rebuild(). |
| 547 |
*/ |
*/ |
| 548 |
function luceneapi_node_luceneapi_query_rebuild($query, $module, $type) { |
function luceneapi_node_luceneapi_query_rebuild($query, $module, $type) { |