forked from Islandora/islandora_solr_search
-
Notifications
You must be signed in to change notification settings - Fork 1
/
islandora_solr_search.module
488 lines (429 loc) · 16.9 KB
/
islandora_solr_search.module
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
<?php
//$islandora_query;
$queryClass;
// set current display
$current_display;
/**
* @file
* Implementation of Solr search for the Islandora fedora_search module.
*/
/**
* Implementation of hook_menu().
*/
function islandora_solr_search_menu() {
$items['islandora/solr/search'] = array(
'page callback' => 'islandora_solr_search',
'access arguments' => array('view fedora collection'), //depends of fedora_repository view
'type' => MENU_CALLBACK,
);
$items['islandora/solr/process'] = array(
'page callback' => 'update_solr_url_div',
'access arguments' => array('view fedora collection'),
'file' => 'islandora_solr_search.admin.inc',
'type' => MENU_CALLBACK,
);
$items['admin/settings/islandora_solr_search'] = array(
'title' => 'Islandora Solr Client',
'description' => 'Managing Islandora Solr Searching',
'page callback' => 'drupal_get_form',
'page arguments' => array('islandora_solr_admin_settings'),
'access arguments' => array('administer site configuration'),
'file' => 'islandora_solr_search.admin.inc',
'type' => MENU_NORMAL_ITEM,
);
return $items;
}
/**
* Implementation of hook_block().
*/
function islandora_solr_search_block($op = 'list', $delta = 0, $edit = array()) {
global $queryClass;
islandora_solr_search_init();
/*
* Here I need to call a hook which will return module/file/class/method/name/description
* for blocks which need a queryClass to render.
*/
$solr_blocks = module_invoke_all("islandora_solr_query_blocks");
// The $op parameter determines what piece of information is being requested.
switch ($op) {
case 'list':
// If $op is "list", we just need to return a list of block descriptions.
// This is used to provide a list of possible blocks to the administrator,
// end users will not see these descriptions.
foreach ($solr_blocks as $name => $block) {
$blocks[$name] = array(
'info' => $block['name'],
);
}
return $blocks;
case 'configure':
// If $op is "configure", we need to provide the administrator with a
// configuration form. The $delta parameter tells us which block is being
// configured. In this example, we'll allow the administrator to customize
// the text of the first block.
$form = array();
return $form;
case 'save':
// If $op is "save", we need to save settings from the configuration form.
// Since the first block is the only one that allows configuration, we
// need to check $delta to make sure we only save it.
case 'view': default:
// If $op is "view", then we need to generate the block for display
// purposes. The $delta parameter tells us which block is being requested.
if (!empty($solr_blocks[$delta])) {
// First we'll set the block title.
$block['subject'] = $solr_blocks[$delta]['name'];
// Include the file from which the block originates.
require_once( drupal_get_path('module', $solr_blocks[$delta]['module']) . '/' . $solr_blocks[$delta]['file'] );
// If a class is present, instantiate it and proceed from there.
// The variable $queryClass (the IslandoraSolrQueryProcessor, containing
// the Solr search result), is fed as an argument.
if (!empty($solr_blocks[$delta]['class'])) {
$displayClass = new IslandoraSolrResults();
$block_function = $solr_blocks[$delta]['function'];
if (method_exists($displayClass, $block_function)) {
$content = $displayClass->$block_function($queryClass);
$block['content'] = !empty($content) ? $content : NULL;
}
// Otherwise, simply load the form.
}
elseif (!empty($solr_blocks[$delta]['form'])) {
$block['content'] = drupal_get_form($solr_blocks[$delta]['form']);
}
}
return $block;
}
}
/**
* Implementation of hook_theme().
*/
function islandora_solr_search_theme() {
return array(
'islandora_solr_search_block_form' => array(
'arguments' => array(
'form' => NULL,
),
),
);
}
/**
* Implements hook_islandora_solr_primary_display()
*/
function islandora_solr_search_islandora_solr_primary_display() {
return array(
// 'machine-name' = array(
// 'name' => 'Human Readable Name',
// 'module' => 'module_name',
// 'file' => 'FileName.inc',
// 'class' => 'ClassName',
// 'function' => 'function_name',
// 'description' => 'A description of the display profile',
// );
//
// Note: this class should either be, or extend, the class IslandoraSolrResults.
//
'default' => array(
'name' => t('Fields (default)'),
'module' => 'islandora_solr_search',
'file' => 'IslandoraSolrResults.inc',
'class' => "IslandoraSolrResults",
'function' => "displayResults",
'description' => t("A simple output."),
),
);
}
/**
* Implements hook_islandora_solr_query_blocks()
*/
function islandora_solr_search_islandora_solr_query_blocks() {
return array(
// 'macine_name' => array(
// 'name' => 'Human Readable Name',
// 'module' => 'module_name',
// 'file' => 'FileName.inc',
// 'class' => 'ClassName',
// 'function' => 'method_name',
// 'form' => 'form_function_name',
// ),
//
// Note: As in the examples below, it is valid to specify
// *either* a class and method *or* a form. The latter implies no
// class needs to be instantiated.
//
'advanced' => array(
'name' => t('Advanced Search'),
'module' => 'islandora_solr_search',
'file' => 'islandora_solr_search.module',
'class' => NULL,
'function' => NULL,
'form' => 'islandora_solr_search_block_form',
),
'simple' => array(
'name' => t('Simple Search'),
'module' => 'islandora_solr_search',
'file' => 'islandora_solr_search.module',
'class' => NULL,
'function' => NULL,
'form' => 'islandora_solr_simple_search_form',
),
'basic_facets' => array(
'name' => t('Filters'),
'module' => 'islandora_solr_search',
'file' => 'IslandoraSolrResults.inc',
'class' => 'IslandoraSolrResults',
'function' => 'displayFacets',
'form' => NULL,
),
'current_query' => array(
'name' => t('Current Query'),
'module' => 'islandora_solr_search',
'file' => 'IslandoraSolrResults.inc',
'class' => 'IslandoraSolrResults',
'function' => 'currentQuery',
'form' => NULL,
),
);
}
/**
* islandora solr simple search form
* @return type
*/
function islandora_solr_simple_search_form() {
module_load_include('inc', 'islandora_solr_search', 'IslandoraSolrResults');
$resultsClass = new IslandoraSolrResults();
return $resultsClass->build_simple_solr_form();
}
/**
* islandora solr search block form validate
* @param type $form
* @param type $form_state
*/
function islandora_solr_search_block_form_validate($form, &$form_state) {
$repeat = variable_get('islandora_solr_search_block_repeat', '3');
$found = FALSE;
for ($fieldNum = 1; $fieldNum <= $repeat; $fieldNum++) {
if (isset($form_state['values']["fedora_terms$fieldNum"])) {
$found = TRUE;
}
}
if (!$found) {
form_set_error('edit_fedora_terms1', t('Please enter search term.'));
}
}
/**
* islandora solr simple search form submit
* @param type $form
* @param type $form_state
*/
function islandora_solr_simple_search_form_submit($form, &$form_state) {
$form_state['rebuild'] = TRUE;
$searchString = $form_state['values']['islandora_simple_search_query'];
$searchString = htmlspecialchars(drupal_urlencode($searchString), ENT_QUOTES, 'utf-8', FALSE);
$searchString = str_replace('/', '~slsh~', $searchString); //replace the slash so url doesn't break
$dismax = '';
if (variable_get('dismax_allowed', TRUE)) {
$dismax = 'dismax';
}
drupal_goto("islandora/solr/search/$searchString/-/$dismax");
}
/**
* islandora solr search block form
* @global type $queryClass
* @return type
*/
function islandora_solr_search_block_form() {
global $queryClass;
islandora_solr_search_init();
module_load_include('inc', 'islandora_solr_search', 'IslandoraSolrResults');
$resultsClass = new IslandoraSolrResults();
return $resultsClass->build_solr_search_form(NULL, NULL, $queryClass->solrQuery);
}
/**
* theme islandora solr search block form
* @global type $queryClass
* @param type $form
* @return type
*/
function theme_islandora_solr_search_block_form($form) {
global $queryClass;
islandora_solr_search_init();
module_load_include('inc', 'islandora_solr_search', 'IslandoraSolrResults');
$resultsClass = new IslandoraSolrResults();
return $resultsClass->theme_solr_search_form($form);
}
/**
* islandora solr search
* @global type $queryClass
* @param type $query
* @param type $fq
* @param type $dismax
* @return type
*/
function islandora_solr_search($query, $fq=NULL, $dismax=NULL) {
global $queryClass;
islandora_solr_search_init();
// Build and execute Apache Solr query
$queryResult = $queryClass->buildAndExecuteQuery("$query", $fq, $dismax);
// get profiles
$primary_profiles = module_invoke_all("islandora_solr_primary_display");
$secondary_profiles = module_invoke_all("islandora_solr_secondary_display");
// Get the preferred display profile
// Order: First choice is what's in the ?profile query var
// Second choice is the primary display profile
// Third choice is the default IslandoraSolrResults
$islandora_solr_primary_display = ((isset($_GET['display']) AND array_key_exists($_GET['display'], $primary_profiles)) ? $_GET['display'] : variable_get('islandora_solr_primary_display', 'default'));
$islandora_solr_selected_display = isset($_GET['solr_profile']) ? $_GET['solr_profile'] : NULL;
// set global variable
global $current_display;
$current_display = $islandora_solr_primary_display;
// TODO: Also filter secondary displays against those checked in the configuration options.
if (isset($secondary_profiles[$islandora_solr_selected_display])) {
$profile = $secondary_profiles[$islandora_solr_selected_display];
}
elseif (isset($primary_profiles[$islandora_solr_primary_display])) {
$profile = $primary_profiles[$islandora_solr_primary_display];
}
else {
drupal_set_message("There is an error in the solr search configuration: the display profile is not found.", 'error');
$profile = $primary_profiles['default'];
}
// Include the file for the display profile
require_once(drupal_get_path('module', $profile['module']) . '/' . $profile['file']);
// Set display class and function vars
$solrClass = $profile['class'];
$solrFunction = $profile['function'];
$nope = FALSE;
if (class_exists($solrClass)) {
$implementation = new $solrClass();
}
else {
$nope = TRUE;
}
if (!$nope && method_exists($implementation, $solrFunction)) {
$output = $implementation->$solrFunction($queryClass);
}
else {
$nope = TRUE;
}
if ($nope) {
module_load_include('inc', 'islandora_solr_search', 'IslandoraSolrResults');
$resultsClass = new IslandoraSolrResults();
$output = $resultsClass->displayResults($queryClass);
}
if (variable_get('islandora_solr_search_debug_mode', 0)) { // debug dump
drupal_set_message('Params: <br/><pre>' . print_r($queryClass->solrParams, TRUE) . '</pre>', 'status');
}
return $output;
// return $queryResult;
}
/**
* islandora solr search block form submit
* @global type $queryClass
* @param type $form
* @param type $form_state
*/
function islandora_solr_search_block_form_submit($form, &$form_state) {
$form_state['rebuild'] = TRUE;
global $queryClass;
islandora_solr_search_init();
$type_id = trim($form_state['values']['type']);
$repeat = variable_get('islandora_solr_search_block_repeat', '3');
$fedora_terms = array();
$types = array();
$booleans = array();
for ($fieldNum = 1; $fieldNum <= $repeat; $fieldNum++) {
if ($form_state['values']["fedora_terms$fieldNum"]) {
$types[] = trim($form_state['values']["type$fieldNum"]);
$fedora_terms[] = lesser_escape(trim($form_state['values']["fedora_terms$fieldNum"]));
}
$next = $fieldNum + 1;
if ($form_state['values']["fedora_terms$next"] && $form_state['values']["fedora_terms$fieldNum"]) {
$booleans[] = trim($form_state['values']["andor$fieldNum"]);
}
}
for ($iteration = 0; $iteration < count($fedora_terms); $iteration++) {
//FIXME (minor): t() should be changed to format_string() in this instance, in Drupal 7
// The string isn't really translatable, just templated.
$searchString .= t("!field:(!query) !bool ", array(
'!field' => $types[$iteration],
'!query' => $fedora_terms[$iteration],
'!bool' => $booleans[$iteration]
));
}
$searchString = trim($searchString);
$searchString = drupal_urlencode($searchString);
$searchString = str_replace('/', '~slsh~', $searchString); //replace the slash so url doesn't break
drupal_goto("islandora/solr/search/$searchString/-");
}
/**
* Implementation of hook_help().
*/
function islandora_solr_search_help($path, $arg) {
switch ($path) {
case 'admin/help#islandora_solr_search':
return t(
'<p>
The Islandora Solr Search extends the functionality of the Fedora_Repository module.
This module allows one or more of a series of blocks to be configured to search a solr index.
This module can co-exist with the original Fedora_Repositories search block, but Solr\'s
additional functionality will normally make the original block redundant.
</p>
<p>
The !guide contains additonal information.
</p>
<ul>
<li>Islandora Solr Search requires a working Solr instance. The !sWiki has full setup instructions</li>
<li>Once Solr is running and tested, configure <b>Gsearch</b> to update Solr. Consult the !GSearch for details.</li>
<li>Retreive the !client, unzip it, and copy the <b>Solr</b> directory from the archive to the islandora_solr_search module\'s folder.</li>
<li>Go to Administer > Site Configuration > Islandora Solr Client <em>(or click the link below)</em> to configure the module. Set which Solr request handler to use, set the port, host and context for the index to be queried, and select which fields are to be used for filtering. Solr\'s <b>schema.xml</b> and <b>solrconfig.xml</b> must be configured for the request handler as well as which fields to index and return.</li>
<li>The module allows custom code to be used to display search results. If custom PHP code is used, the paths to that codes\'s file and function must be entered here as well.</li>
<li>Three different blocks are now available under Administer > Site Building > Blocks: Islandora Solr Simple Search Block, Islandora Solr Facet Block, and Islandora Solr Search Block. The configuration setting for each of these blocks will allow you to control their position on the screen, and on which pages they will be displayed.</li>
<li>The Islandora Solr Simple Search Block will use will add defType=dismax to the configured request handler. The request handler tag in <b>solrconfig.xml</b> must have an attribute of <b>default="TRUE"</b>.</li>
</ul>
', array(
'!guide' => l('Islandora Guide', 'https://wiki.duraspace.org/display/ISLANDORA/Islandora+Guide'),
'!sWiki' => l("Solr Wiki", 'http://wiki.apache.org/solr/SolrTomcat'),
'!GSearch' => l('GSearch Documentation', 'https://wiki.duraspace.org/display/FCSVCS/Generic+Search+Service+2.2'),
'!client' => l('Apache Solr php client', 'http://code.google.com/p/solr-php-client'),
)
);
}
}
/**
* islandora solr search init
* @global IslandoraSolrQueryProcessor $queryClass
* @staticvar boolean $islandora_solr_search_init
*/
function islandora_solr_search_init() {
module_load_include('inc', 'islandora_solr_search', 'islandora_solr_search.admin');
module_load_include('inc', 'islandora_solr_search', 'includes/common');
module_load_include('inc', 'islandora_solr_search', 'IslandoraSolrQueryProcessor');
static $islandora_solr_search_init = false;
if (!$islandora_solr_search_init) {
drupal_add_css(drupal_get_path('module', 'islandora_solr_search') . '/css/islandora_solr_search.css');
global $queryClass;
if (empty($queryClass)) {
$queryClass = new IslandoraSolrQueryProcessor();
}
$islandora_solr_search_init = true;
}
}
/**
*
* @global type $conf
*/
function islandora_solr_search_boot() {
global $conf;
//Allow Internationalization, by using multilingual variables... if i18n is
// installed, that is.
if (module_exists('i18n')) {
$vars = array(
'islandora_solr_search_block_facets',
'islandora_solr_search_result_fields',
'islandora_solr_searchterms'
);
$conf['i18n_variables'] = (isset($conf['i18n_variables']) && is_array($conf['i18n_variables'])) ?
array_merge($vars, $conf['i18n_variables']):
$vars;
}
}