-
Notifications
You must be signed in to change notification settings - Fork 64
/
islandora_solr_search.module
318 lines (292 loc) · 15.7 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
<?php
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
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['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'),
'type' => MENU_NORMAL_ITEM,
);
return $items;
}
function islandora_solr_admin_settings() {
$handler_file_options = array();
$include_dir_handler = opendir(drupal_get_path('module', 'islandora_solr_search'));
while ($file = readdir($include_dir_handler)) {
$ext = pathinfo($file, PATHINFO_EXTENSION);
if ($ext == "inc") {
$handler_file_options[$file] = t($file);
}
}
$handler_class_options = array();
if (file_exists(drupal_get_path('module', 'islandora_solr_search') . "/" . variable_get('islandora_solr_search_block_handler_file', t('IslandoraSolrResults.inc')))) {
$handler_class_methods_before = get_declared_classes();
include_once( drupal_get_path('module', 'islandora_solr_search') . "/" . variable_get('islandora_solr_search_block_handler_file', t('IslandoraSolrResults.inc')) );
$handler_class_methods_after = get_declared_classes();
$handler_class_diff = array_diff($handler_class_methods_after, $handler_class_methods_before);
}
foreach ($handler_class_diff as $key => $class) {
$handler_class_options[$class] = $class;
}
$handler_method_options = array();
if (class_exists(variable_get('islandora_solr_search_block_handler_class', t('IslandoraSolrResults')))) {
$method_list = get_class_methods(variable_get('islandora_solr_search_block_handler_class', t('IslandoraSolrResults')));
foreach ($method_list as $key => $method) {
$handler_method_options[$method] = $method;
}
}
$form = array();
$form['islandora_solr_search_block_repeat'] = array(
'#type' => 'textfield',
'#title' => t('Number of times to repeat search fields'),
'#size' => 5,
'#description' => t('The number of times you would like the search blocks to be repeated'),
'#default_value' => variable_get('islandora_solr_search_block_repeat', t('3')),
);
$form['islandora_solr_search_block_request_handler'] = array(
'#type' => 'textfield',
'#title' => t('The name of the solr request handler to use for this search'),
'#size' => 25,
'#description' => t('The name of the solr request handler to use for this search. This must be configured in your solr config file. If this is not set we will use the default handler.'),
'#default_value' => variable_get('islandora_solr_search_block_request_handler', t('standard')),
);
$form['islandora_solr_search_block_facets'] = array(
'#type' => 'textarea',
'#title' => t('The fields to use for facets'),
// '#size' => 75,
'#description' => t('a space separated list of fields to use as facets'),
'#default_value' => variable_get('islandora_solr_search_block_facets', t('dc.subject dc.type')),
);
$form['islandora_solr_search_block_host'] = array(
'#type' => 'textfield',
'#title' => t('The domain name or ip of the solr server'),
'#size' => 25,
'#description' => t('The domain name or ip of the solr server. Do not include http://. The default is localhost.'),
'#default_value' => variable_get('islandora_solr_search_block_host', t('localhost')),
);
$form['islandora_solr_search_block_port'] = array(
'#type' => 'textfield',
'#title' => t('The port that the solr server is listening on'),
'#size' => 25,
'#description' => t('The port that the solr server is listening on. The default value is 8080'),
'#default_value' => variable_get('islandora_solr_search_block_port', t('8080')),
);
$form['islandora_solr_search_block_app_path'] = array(
'#type' => 'textfield',
'#title' => t('The path to the solr webapp'),
'#size' => 25,
'#description' => t('The path to the solr webapp. The default value is solr'),
'#default_value' => variable_get('islandora_solr_search_block_app_path', t('solr')),
);
$form['islandora_solr_search_block_handler_file'] = array(
'#type' => 'select',
'#title' => t('The path to the php file that will be called to display solr search results'),
// '#size' => 25,
'#multiple' => false,
'#options' => $handler_file_options,
'#description' => t('The path to the php file that will be called to display solr search results. This file must have a function that can display the solr search results. The default is IslandoraSolrResults.inc '),
'#default_value' => variable_get('islandora_solr_search_block_handler_file', t('IslandoraSolrResults.inc')),
);
$form['islandora_solr_search_block_handler_class'] = array(
'#type' => 'select',
'#title' => t('The class included in the file above'),
// '#size' => 25,
'#multiple' => false,
'#options' => $handler_class_options,
'#description' => t('The class included in the file above. The default value is IslandoraSolrResults'),
'#default_value' => variable_get('islandora_solr_search_block_handler_class', t('IslandoraSolrResults')),
);
$form['islandora_solr_search_block_handler_function'] = array(
'#type' => 'select',
'#title' => t('The function to call to display the search results'),
// '#size' => 25,
'#multiple' => false,
'#options' => $handler_method_options,
'#description' => t('The function to call to display the search results. The default value is displayResults'),
'#default_value' => variable_get('islandora_solr_search_block_handler_function', t('displayResults')),
);
$form['islandora_solr_search_block_facet_min_count'] = array(
'#type' => 'textfield',
'#title' => t('This param indicates the minimum counts for facet fields should be included in the response'),
'#size' => 25,
'#description' => t('This param indicates the minimum counts for facet fields should be included in the response. '),
'#default_value' => variable_get('islandora_solr_search_block_facet_min_count', '2'),
);
$form['islandora_solr_search_block_facet_limit'] = array(
'#type' => 'textfield',
'#title' => t('This param indicates the maximum number of constraint counts that should be returned for the facet fields. A negative value means unlimited'),
'#size' => 25,
'#description' => t('This param indicates the maximum number of constraint counts that should be returned for the facet fields. A negative value means unlimited. '),
'#default_value' => variable_get('islandora_solr_search_block_facet_limit', '10'),
);
$form['islandora_solr_search_num_of_results'] = array(
'#type' => 'textfield',
'#title' => t('This param indicates the number of results to show per page'),
'#size' => 25,
'#description' => t('This param indicates the number of results to show per page. '),
'#default_value' => variable_get('islandora_solr_search_num_of_results', '20'),
);
return system_settings_form($form);
}
function islandora_solr_search_block($op = 'list', $delta = 0, $edit = array()) {
// 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.
$blocks[0] = array(
'info' => t('Islandora Solr Search Block'),
);
$blocks[1] = array(
'info' => t('Islandora Solr Facet Block'),
);
$blocks[2] = array(
'info' => t('Islandora Solr Simple Search Block'),
);
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.
switch ($delta) {
case 0:
$block['subject'] = t('Islandora Solr Search');
// The content of the block is typically generated by calling a custom
// function.
$block['content'] = drupal_get_form('islandora_solr_search_block_form');
break;
case 1:
// The subject is displayed at the top of the block. Note that it
// should be passed through t() for translation.
$block['subject'] = t('Islandora Facet Block');
// The content of the block is typically generated by calling a custom
// function.
require_once(drupal_get_path('module', 'islandora_solr_search') . '/IslandoraSolrResults.inc');
$solrResults = new IslandoraSolrResults();
$block['content'] = $solrResults->displayFacets();
break;
case 2:
$block['subject'] = t('Islandora Solr Simple Search');
$block['content'] = drupal_get_form('islandora_solr_simple_search_form');
break;
}
return $block;
}
}
function islandora_solr_search_theme() {
return array(
'islandora_solr_search_block_form' => array(
'arguments' => array(
'form' => NULL,
),
),
);
}
function islandora_solr_simple_search_form() {
module_load_include('inc', 'islandora_solr_search', 'IslandoraSolrResults');
$searchClass = new IslandoraSolrResults();
return $searchClass->build_simple_solr_form();
}
function islandora_solr_simple_search_form_validate($form, &$form_state) {
if ($form_state['values']['islandora_simple_search_query'] == '') {
form_set_error('search_query', t('The Search field cannot be empty.'));
}
}
function islandora_solr_simple_search_form_submit($form, &$form_state) {
$searchString = $form_state['values']['islandora_simple_search_query'];
//$searchString = drupal_urlencode($searchString);
$searchString = htmlspecialchars(drupal_urlencode($searchString), ENT_QUOTES, 'utf-8', false);
$searchString = str_replace('/', '~slsh~', $searchString); //replace the slash so url doesn't break
drupal_goto("islandora/solr/search/$searchString/1/-/dismax");
}
function islandora_solr_search_block_form() {
module_load_include('inc', 'islandora_solr_search', 'IslandoraSolrResults');
$searchClass = new IslandoraSolrResults();
return $searchClass->build_solr_search_form();
}
function theme_islandora_solr_search_block_form($form) {
drupal_add_css(drupal_get_path('module', 'islandora_solr_search') . '/islandora_solr_search.css');
module_load_include('inc', 'islandora_solr_search', 'IslandoraSolrResults');
$advanced_search_form = new IslandoraSolrResults();
return $advanced_search_form->theme_solr_search_form($form);
}
function islandora_solr_search($query, $startPage = 1, $fq=null, $dismax=null) {
module_load_include('inc', 'islandora_solr_search', 'IslandoraSolrResults');
$searchClass = new IslandoraSolrResults();
return $searchClass->searchAndDisplay($query, $startPage, $fq, $dismax);
}
function islandora_solr_search_block_form_submit($form, &$form_state) {
module_load_include('php', 'islandora_solr_search', 'Solr/Service');
$type_id = trim($form_state['values']['type']);
$repeat = variable_get('islandora_solr_search_block_repeat', t('3'));
$searchString = trim($form_state['values']['type1']) . ':' . Apache_Solr_Service::escapePhrase(trim($form_state['values']['fedora_terms1']));
if ($form_state['values']['fedora_terms2'] != '') {
$searchString .=' ' . trim($form_state['values']['andor1']) . ' ' . trim($form_state['values']['type2']) . ':' . Apache_Solr_Service::escapePhrase(trim($form_state['values']['fedora_terms2']));
}
if ($repeat > 2 && $repeat < 9) {
for ($i = 3; $i < $repeat + 1; $i++) {
$t = $i - 1;
if ($form_state['values']["fedora_terms$i"] != '') {
$searchString .= ' ' . trim($form_state['values']["andor$t"]) . ' ' . trim($form_state['values']["type$i"]) . ':' . Apache_Solr_Service::escapePhrase(trim($form_state['values']["fedora_terms$i"]));
}
}
}
$searchString = htmlspecialchars(drupal_urlencode($searchString), ENT_QUOTES, 'utf-8', false);
$searchString = str_replace('/', '~slsh~', $searchString); //replace the slash so url doesn't break
drupal_goto("islandora/solr/search/$searchString/1");
}
/**
* 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 <a href="https://wiki.duraspace.org/display/ISLANDORA/Islandora+Guide">Islandora Guide</a> 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(
'!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'),
)
);
}
}