| 1 |
<?php |
<?php |
| 2 |
/* $Id: z3950.module,v 1.5 2007/09/01 19:57:47 douggreen Exp $ */ |
/* $Id: z3950.module,v 1.5.2.1 2007/09/01 20:03:08 douggreen Exp $ */ |
| 3 |
|
|
| 4 |
/** |
/** |
| 5 |
* @file |
* @file |
| 161 |
'au' => '1=1', // author |
'au' => '1=1', // author |
| 162 |
); |
); |
| 163 |
|
|
| 164 |
|
// replace the Drupal keys types with the equivalent CCL command |
| 165 |
|
$keys = preg_replace('/(au|author):/', 'au=', $keys); |
| 166 |
|
$keys = preg_replace('/(ti|title):/', 'ti=', $keys); |
| 167 |
|
|
| 168 |
// setup the YAZ search on each server |
// setup the YAZ search on each server |
| 169 |
$ids = array(); |
$ids = array(); |
| 170 |
foreach ($servers as $server_index => $server) { |
foreach ($servers as $server_index => $server) { |
| 208 |
// check for error and get number of 'hits' (results) from this server |
// check for error and get number of 'hits' (results) from this server |
| 209 |
$error = yaz_error($id); |
$error = yaz_error($id); |
| 210 |
if (empty($error)) { |
if (empty($error)) { |
| 211 |
$server_hits[$server_index] = yaz_hits($id); |
$hits = yaz_hits($id); |
| 212 |
|
|
| 213 |
// if this server has any results |
// if this server has any results |
| 214 |
if (!empty($server_hits[$server_index])) { |
if (!empty($hits)) { |
| 215 |
// keep track of total number of hits, for use in the pager |
// keep track of total number of hits, for use in the pager |
| 216 |
$total_hits += $server_hits[$server_index]; |
$server_hits[$server_index] = $hits; |
| 217 |
|
$total_hits += $hits; |
| 218 |
|
|
| 219 |
// set the starting position |
// set the starting position |
| 220 |
$server_pos[$server_index] = 1; |
$server_pos[$server_index] = 1; |
| 242 |
$total_pos = pager_init(10, 0, $total_hits); |
$total_pos = pager_init(10, 0, $total_hits); |
| 243 |
$total_per_server = $total_pos / count($ids); |
$total_per_server = $total_pos / count($ids); |
| 244 |
|
|
| 245 |
|
// display total matches when there are more than fit on one page |
| 246 |
|
if ($total_hits > 10) { |
| 247 |
|
drupal_set_message(t('%hits matches found', array('%hits' => $total_hits))); |
| 248 |
|
} |
| 249 |
|
|
| 250 |
// initialize the current position in each server |
// initialize the current position in each server |
| 251 |
// @TODO: there's a faster way to do this, this is just the easy way! |
// @TODO: there's a faster way to do this, this is just the easy way! |
| 252 |
// @TODO: test this around the edge cases, is it losing any results? |
// @TODO: test this around the edge cases, is it losing any results? |
| 253 |
while ($total_pos > 1) { |
while ($total_pos > 0) { |
| 254 |
foreach ($ids as $server_index => $id) { |
foreach ($ids as $server_index => $id) { |
| 255 |
if ($server_pos[$server_index] < $server_hits[$server_index]) { |
if ($server_pos[$server_index] < $server_hits[$server_index]) { |
| 256 |
$server_pos[$server_index] ++; |
$server_pos[$server_index] ++; |
| 262 |
// process results from search server, evenly mixing results from each server |
// process results from search server, evenly mixing results from each server |
| 263 |
$limit = min($total_hits, 10); |
$limit = min($total_hits, 10); |
| 264 |
$find = array(); |
$find = array(); |
| 265 |
while (count($find) <= $limit) { |
while (count($find) < $limit && count($server_hits) > 0) { |
| 266 |
foreach ($ids as $server_index => $id) { |
foreach ($ids as $server_index => $id) { |
| 267 |
// loop through the results from this server |
// loop through the results from this server |
| 268 |
if ($server_pos[$server_index] < $server_hits[$server_index]) { |
if (!empty($server_hits[$server_index])) { |
| 269 |
// store the position in a scalar variable to make code easier to read |
// store the position in a scalar variable to make code easier to read |
| 270 |
$pos = $server_pos[$server_index]; |
$pos = $server_pos[$server_index] ++; |
| 271 |
|
|
| 272 |
|
// when all records from this server are retrieved, remove it from the array |
| 273 |
|
if ($pos >= $server_hits[$server_index]) { |
| 274 |
|
unset($server_hits[$server_index]); |
| 275 |
|
} |
| 276 |
|
|
| 277 |
// store the database name for use in the source string below |
// store the database name for use in the source string below |
| 278 |
$rec_database = yaz_record($id, $pos, 'database'); |
$rec_database = yaz_record($id, $pos, 'database'); |
| 294 |
|
|
| 295 |
// save this summary, and don't process it again |
// save this summary, and don't process it again |
| 296 |
$find[] = $summary; |
$find[] = $summary; |
|
$server_pos[$server_index] ++; |
|
| 297 |
} |
} |
| 298 |
} |
} |
| 299 |
} |
} |
| 305 |
} |
} |
| 306 |
} |
} |
| 307 |
|
|
|
|
|
| 308 |
/** |
/** |
| 309 |
* Implementation of hook_form_alter(). |
* Implementation of hook_form_alter(). |
| 310 |
*/ |
*/ |
| 316 |
$default_values = array_keys($options); |
$default_values = array_keys($options); |
| 317 |
|
|
| 318 |
// if search keys have already been used, parse them into form elements |
// if search keys have already been used, parse them into form elements |
| 319 |
$keys = $form['basic']['inline']['keys']['#default_value']; |
$keys = isset($form_state['post']['keys']) ? $form_state['post']['keys'] : $form['basic']['inline']['keys']['#default_value']; |
| 320 |
if (preg_match('/(.*)server:(.*)/', $keys, $match)) { |
if (isset($keys) && preg_match('/(.*)server:(.*)/', $keys, $match)) { |
| 321 |
// replace the keys (which was keys and server) with just the keys |
// replace the keys (which was keys and server) with just the keys |
| 322 |
$form['basic']['inline']['keys']['#default_value'] = $match[1]; |
$form['basic']['inline']['keys']['#default_value'] = $match[1]; |
| 323 |
|
|
| 340 |
} |
} |
| 341 |
|
|
| 342 |
// add help text to the basic form |
// add help text to the basic form |
| 343 |
$form['basic']['#description'] = t('enter the z39.50 search query (<A HREF="@url">see also</A>) and select the servers to search', array('@url' => 'http://www.indexdata.dk/yaz/doc/tools.tkl#CCL')); |
$form['basic']['#description'] = t('enter the z39.50 search query. You may use the CCL qualifiers au (or author) and ti (or title). For example, <i>author:buber, martin</i> or <i>au:buber and ti:thou</i> are valid searches (<A HREF="@url">see also</A>).', array('@url' => 'http://www.indexdata.dk/yaz/doc/tools.tkl#CCL')); |
| 344 |
|
|
| 345 |
// add advanced form |
// add advanced form |
| 346 |
// @TODO |
// @TODO |