| 1 |
<?php
|
| 2 |
|
| 3 |
/*
|
| 4 |
* hook_menu
|
| 5 |
*/
|
| 6 |
function solr_menu($may_cache) {
|
| 7 |
$items = array();
|
| 8 |
if ($may_cache){
|
| 9 |
$items[] = array(
|
| 10 |
'path' => 'admin/settings/solr',
|
| 11 |
'title' => t('Solr'),
|
| 12 |
'description' => t('Administer Solr.'),
|
| 13 |
'callback' => 'drupal_get_form',
|
| 14 |
'callback arguments' => 'solr_settings',
|
| 15 |
'access' => user_access('administer site configuration'),
|
| 16 |
);
|
| 17 |
}
|
| 18 |
return $items;
|
| 19 |
}
|
| 20 |
|
| 21 |
/*
|
| 22 |
* module settings page
|
| 23 |
*/
|
| 24 |
function solr_settings() {
|
| 25 |
$form = array();
|
| 26 |
|
| 27 |
// WARNING: could be used to GET/POST to/from URLs restricted to access from localhost
|
| 28 |
$form['solr_path'] = array(
|
| 29 |
'#type' => 'textfield',
|
| 30 |
'#title' => t('Solr location'),
|
| 31 |
'#default_value' => variable_get('solr_path', 'http://localhost:8080'),
|
| 32 |
'#description' => t('Base URL for your Solr instances (without the trailing slash). The actual URL called will be this plus the name of the node type.'),
|
| 33 |
'#validate' => array('solr_validate_location' => array())
|
| 34 |
);
|
| 35 |
|
| 36 |
return system_settings_form($form);
|
| 37 |
}
|
| 38 |
|
| 39 |
/*
|
| 40 |
* Make sure the Solr location is valid
|
| 41 |
*/
|
| 42 |
function solr_validate_location($edit) {
|
| 43 |
// TODO: check for a response from Solr at this address
|
| 44 |
if ($edit['#value'] !== check_url($edit['#value'])) {
|
| 45 |
form_set_error('solr_path', t('This location is not valid'));
|
| 46 |
}
|
| 47 |
}
|
| 48 |
|
| 49 |
/*
|
| 50 |
* hook_cron
|
| 51 |
*/
|
| 52 |
function solr_cron() {
|
| 53 |
if (! module_exists('search')){
|
| 54 |
module_invoke_all('update_index');
|
| 55 |
}
|
| 56 |
}
|
| 57 |
|
| 58 |
/*
|
| 59 |
* select all nodes created since the last indexed node
|
| 60 |
*/
|
| 61 |
function solr_update_solr($type){
|
| 62 |
// select nodes created since last update
|
| 63 |
$last = ($_GET['refresh'] == 1) ? 0 : variable_get($type . '_solr_last', 0);
|
| 64 |
$last = 0;
|
| 65 |
if ($result = db_query("SELECT n.nid FROM {node} n WHERE n.type = '%s' AND n.created > %d ORDER BY n.created ASC", $type, $last)){
|
| 66 |
|
| 67 |
while ($node = db_fetch_object($result)) {
|
| 68 |
$node = node_load($node->nid);
|
| 69 |
$node = node_prepare($node);
|
| 70 |
$data = array_shift((module_invoke_all('solr', 'data', $type, $node)));
|
| 71 |
|
| 72 |
$response = solr_post($type, $data);
|
| 73 |
if ($response !== '<result status="0"></result>'){
|
| 74 |
drupal_set_message('Error posting to Solr: ' . check_plain($response));
|
| 75 |
break;
|
| 76 |
}
|
| 77 |
|
| 78 |
variable_set($type . '_solr_last', $node->created);
|
| 79 |
print "$node->nid\n";
|
| 80 |
}
|
| 81 |
solr_post($type, '<commit/>');
|
| 82 |
// TODO: post '<optimize/>'
|
| 83 |
}
|
| 84 |
}
|
| 85 |
|
| 86 |
/*
|
| 87 |
* choose an appropriate posting method
|
| 88 |
*/
|
| 89 |
function solr_post($type, $data){
|
| 90 |
// TODO: add support for other methods of posting
|
| 91 |
return solr_post_curl($type, $data);
|
| 92 |
}
|
| 93 |
|
| 94 |
/*
|
| 95 |
* post data to Solr using curl
|
| 96 |
*/
|
| 97 |
function solr_post_curl($type, $data){
|
| 98 |
$path = variable_get('solr_path', 'http://localhost:8080');
|
| 99 |
|
| 100 |
$ch = curl_init();
|
| 101 |
curl_setopt($ch, CURLOPT_URL, "$path/$type/update");
|
| 102 |
curl_setopt($ch, CURLOPT_POST, TRUE);
|
| 103 |
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-type:text/xml; charset=utf-8'));
|
| 104 |
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
|
| 105 |
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
|
| 106 |
$result = curl_exec($ch);
|
| 107 |
curl_close($ch);
|
| 108 |
|
| 109 |
return $result;
|
| 110 |
}
|
| 111 |
|
| 112 |
/*
|
| 113 |
* search Solr
|
| 114 |
*/
|
| 115 |
function solr_search_solr($type, $query, $page = 0, $rows = 20){
|
| 116 |
$path = variable_get('solr_path', 'http://localhost:8080');
|
| 117 |
$qt = array_shift(module_invoke_all('solr', 'qt', $type));
|
| 118 |
|
| 119 |
$start = $page * $rows;
|
| 120 |
$url = "$path/$type/select?version=2.2&start=$start&rows=$rows&fl=*+score&qt=$qt&q=" . trim(urlencode($query));
|
| 121 |
$xml = simplexml_load_file($url);
|
| 122 |
|
| 123 |
global $pager_total;
|
| 124 |
$pager_total[0] = $xml->result['numFound'];
|
| 125 |
|
| 126 |
$results = array();
|
| 127 |
foreach ($xml->result->doc as $item) {
|
| 128 |
$thus = array();
|
| 129 |
foreach ($item as $field) {
|
| 130 |
$name = $field->attributes()->name;
|
| 131 |
$thus["$name"] = "$field";
|
| 132 |
}
|
| 133 |
$results[] = $thus;
|
| 134 |
}
|
| 135 |
return $results;
|
| 136 |
}
|