forked from biblibre/omeka-plugin-SolrSearch
-
Notifications
You must be signed in to change notification settings - Fork 0
/
SolrSearchPlugin.php
421 lines (344 loc) · 11.3 KB
/
SolrSearchPlugin.php
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
<?php
/**
* @package omeka
* @subpackage solr-search
* @copyright 2012 Rector and Board of Visitors, University of Virginia
* @license http://www.apache.org/licenses/LICENSE-2.0.html
*/
class SolrSearchPlugin extends Omeka_Plugin_AbstractPlugin
{
protected $_hooks = array(
'install',
'uninstall',
'upgrade',
'initialize',
'define_routes',
'after_save_record',
'after_save_item',
'after_save_element',
'before_delete_record',
'before_delete_item',
'before_delete_element'
);
protected $_filters = array(
'admin_navigation_main',
'search_form_default_action'
);
/**
* Create the database tables, install the starting facets, and set the
* default options.
*/
public function hookInstall()
{
self::_createSolrTables();
self::_installFacetMappings();
self::_setOptions();
}
/**
* Drop the database tables, flush the Solr index, and delete the options.
*/
public function hookUninstall()
{
$this->_db->query(<<<SQL
DROP TABLE IF EXISTS {$this->_db->prefix}solr_search_fields
SQL
);
$this->_db->query(<<<SQL
DROP TABLE IF EXISTS {$this->_db->prefix}solr_search_excludes
SQL
);
try {
$solr = SolrSearch_Helpers_Index::connect();
$solr->deleteByQuery('*:*');
$solr->commit();
$solr->optimize();
} catch (Exception $e) {}
self::_clearOptions();
}
/**
* If upgrading from 1.x, install the new schema.
*
* @param array $args Contains: `old_version` and `new_version`.
*/
public function hookUpgrade($args)
{
self::_createSolrTables();
if (version_compare($args['old_version'], '1.0.1', '<=')) {
self::_installFacetMappings();
self::_setOptions();
}
$fields = $this->_db->getTable('SolrSearchField');
$featured = $fields->findBy(array('slug' => 'featured'));
if (is_null($featured) || empty($featured)) {
$this->_installGenericFacet('featured', __('Featured'));
}
if (version_compare($args['old_version'], '2.2.1', '<=')) {
set_option('solr_search_hl_max_analyzed_chars', '51200');
}
if (version_compare($args['old_version'], '2.3.0', '<=')) {
$sth = $this->_db->query("
SELECT * FROM {$this->_db->SolrSearchField}
WHERE element_id IS NOT NULL
");
$fields = $sth->fetchAll();
$elementTable = $this->_db->getTable('Element');
foreach ($fields as $field) {
$element = $elementTable->find($field['element_id']);
if ($element) {
$element_set = $element->getElementSet();
$slug = Inflector::underscore($element->name . '_' . $element_set->name);
$this->_db->query("
UPDATE {$this->_db->SolrSearchField}
SET slug = ?
WHERE id = ?
", array($slug, $field['id']));
}
}
}
if (version_compare($args['old_version'], '2.4.0', '<=')) {
$sth = $this->_db->query("
ALTER TABLE {$this->_db->SolrSearchField}
ADD COLUMN is_sort tinyint unsigned DEFAULT 0 AFTER is_facet
");
}
}
/**
* Register the string translations.
*/
public function hookInitialize()
{
add_translation_source(dirname(__FILE__) . '/languages');
}
/**
* Register the application routes.
*
* @param array $args With `router`.
*/
public function hookDefineRoutes($args)
{
$args['router']->addConfig(new Zend_Config_Ini(
SOLR_DIR.'/routes.ini'
));
}
/**
* When a record is saved, try to extract and index a Solr document.
*
* @param array $args With `record`.
*/
public function hookAfterSaveRecord($args)
{
SolrSearch_Utils::ensureView();
$record = $args['record'];
$excludes = get_db()->getTable('SolrSearchExclude');
$collection = get_collection_for_item($record);
if (!is_null($collection) && $excludes->isExcluded($collection)) {
return;
}
// Try to extract a document for the record.
$mgr = new SolrSearch_Addon_Manager($this->_db);
$doc = $mgr->indexRecord($record);
// Does the record have an add-on profile?
if ($addon = $mgr->findAddonForRecord($record)) {
// Connect to Solr.
$solr = SolrSearch_Helpers_Index::connect();
// If the record yields a Solr document, index it.
if (!is_null($doc)) {
$solr->addDocuments(array($doc));
$solr->commit();
}
// If not, remove an existing document.
else {
try {
$solr->deleteById($mgr->getId($record));
$solr->commit();
} catch (Exception $e) {}
}
}
// Reindex related records.
$mgr->resaveRemoteParent($record);
$mgr->resaveChildren($record);
}
/**
* When an item is saved, index the record if the item is set public, and
* clear an existing record if it is set private.
*
* @param array $args With `record`.
*/
public function hookAfterSaveItem($args)
{
SolrSearch_Utils::ensureView();
$item = $args['record'];
$excludes = get_db()->getTable('SolrSearchExclude');
$collection = get_collection_for_item($item);
if (!is_null($collection) && $excludes->isExcluded($collection)) {
return;
}
$solr = SolrSearch_Helpers_Index::connect();
// Both public and private items will be indexed
$doc = SolrSearch_Helpers_Index::itemToDocument($item);
$solr->addDocuments(array($doc));
$solr->commit();
}
/**
* When a new element is added, register a facet mapping for it.
*
* @param array $args With `record` and `insert`.
*/
public function hookAfterSaveElement($args)
{
if ($args['insert']) {
$facet = new SolrSearchField(null, $args['record']);
$facet->save();
}
}
/**
* When a record is deleted, clear its Solr record.
*
* @param array $args With `record`.
*/
public function hookBeforeDeleteRecord($args)
{
$record = $args['record'];
$mgr = new SolrSearch_Addon_Manager($this->_db);
$id = $mgr->getId($record);
if (!is_null($id)) {
$solr = SolrSearch_Helpers_Index::connect();
try {
$solr->deleteById($id);
$solr->commit();
} catch (Exception $e) {}
}
}
/**
* When an item is deleted, clear its Solr record.
*
* @param array $args With `record`.
*/
public function hookBeforeDeleteItem($args)
{
$item = $args['record'];
$solr = SolrSearch_Helpers_Index::connect();
try {
$solr->deleteById('Item_' . $item['id']);
$solr->commit();
} catch (Exception $e) {}
}
/**
* When an element is deleted, remove its facet mapping.
*
* @param array $args With `record`.
*/
public function hookBeforeDeleteElement($args)
{
$table = $this->_db->getTable('SolrSearchField');
$facet = $table->findByElement($args['record']);
if(!empty($facet)) {
$facet->delete();
}
}
/**
* Add a link to the administrative navigation bar.
*
* @param string $nav The array of label/URI pairs.
* @return array
*/
public function filterAdminNavigationMain($nav)
{
$nav[] = array(
'label' => __('Solr Search'), 'uri' => url('solr-search/server')
);
return $nav;
}
/**
* Override the default simple-search URI to automagically integrate into
* the theme; leaves admin section alone for default search.
*
* @param string $uri URI for Simple Search.
* @return string
*/
public function filterSearchFormDefaultAction($uri)
{
if (!is_admin_theme()) $uri = url('solr-search/results/interceptor');
return $uri;
}
/**
* Install the facets table.
*/
protected function _createSolrTables()
{
$this->_db->query(<<<SQL
CREATE TABLE IF NOT EXISTS {$this->_db->prefix}solr_search_fields (
id int(10) unsigned NOT NULL auto_increment,
element_id int(10) unsigned,
slug tinytext collate utf8_unicode_ci NOT NULL,
label tinytext collate utf8_unicode_ci NOT NULL,
is_indexed tinyint unsigned DEFAULT 0,
is_facet tinyint unsigned DEFAULT 0,
is_sort tinyint unsigned DEFAULT 0,
PRIMARY KEY (id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
SQL
);
$this->_db->query(<<<SQL
CREATE TABLE IF NOT EXISTS {$this->_db->prefix}solr_search_excludes (
id int(10) unsigned NOT NULL auto_increment,
collection_id int(10) unsigned NOT NULL,
PRIMARY KEY (id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
SQL
);
}
/**
* Install the default facet mappings.
*/
protected function _installFacetMappings()
{
$this->_db
->getTable('SolrSearchField')
->installFacetMappings();
}
/**
* Install the default facet mappings.
*
* @param string $slug The facet `slug`.
* @param string $label The facet `label`.
*/
protected function _installGenericFacet($slug, $label)
{
$this->_db
->getTable('SolrSearchField')
->installGenericFacet($slug, $label);
}
/**
* Set the global options.
*/
protected function _setOptions()
{
set_option('solr_search_host', 'localhost');
set_option('solr_search_port', '8080');
set_option('solr_search_core', '/solr/omeka/');
set_option('solr_search_facet_limit', '25');
set_option('solr_search_facet_sort', 'count');
set_option('solr_search_hl', '1');
set_option('solr_search_hl_snippets', '1');
set_option('solr_search_hl_fragsize', '250');
set_option('solr_search_hl_max_analyzed_chars', '51200');
set_option('solr_search_display_private_items', '1');
}
/**
* Clear the global options.
*/
protected function _clearOptions()
{
delete_option('solr_search_host');
delete_option('solr_search_port');
delete_option('solr_search_core');
delete_option('solr_search_facet_limit');
delete_option('solr_search_facet_sort');
delete_option('solr_search_hl');
delete_option('solr_search_hl_snippets');
delete_option('solr_search_hl_fragsize');
delete_option('solr_search_hl_max_analyzed_chars');
delete_option('solr_search_display_private_items');
}
}