/
MysqlIndexPlugin.inc.php
243 lines (210 loc) · 6.84 KB
/
MysqlIndexPlugin.inc.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
<?php
/**
* @file plugins/generic/mysqlIndex/MysqlIndexPlugin.inc.php
*
* Copyright (c) 2005-2012 Alec Smecher and John Willinsky
* Distributed under the GNU GPL v2. For full terms see the file docs/COPYING.
*
* @class MysqlIndexPlugin
* @ingroup plugins_generic_mysqlIndex
*
* @brief MySQL search index implementation for the Harvester
*/
// $Id$
import('lib.pkp.classes.plugins.GenericPlugin');
class MysqlIndexPlugin extends GenericPlugin {
/** @var $index object */
var $index;
/**
* Register the plugin, if enabled
* @param $category string
* @param $path string
* @return boolean
*/
function register($category, $path) {
if (parent::register($category, $path)) {
// HookRegistry::register('Installer::postInstall',array(&$this, 'postInstallCallback'));
if ($this->getEnabled()) {
$this->addHelpData();
// Add DAOs
$this->import('CrosswalkDAO');
$this->import('Crosswalk');
$this->import('Search');
$this->import('SearchIndex');
$this->import('SearchDAO');
$crosswalkDao = new CrosswalkDAO();
DAORegistry::registerDAO('CrosswalkDAO', $crosswalkDao);
$searchDao = new SearchDAO();
DAORegistry::registerDAO('SearchDAO', $searchDao);
/**
* Set hooks
*/
// Record handling & harvesting
HookRegistry::register('Harvester::insertRecord', array(&$this, 'insertRecordCallback'));
HookRegistry::register('Harvester::indexRecord', array(&$this, 'insertRecordCallback'));
HookRegistry::register('Harvester::updateRecord', array(&$this, 'updateRecordCallback'));
HookRegistry::register('Harvester::deleteRecord', array(&$this, 'deleteRecordCallback'));
// User interface
HookRegistry::register('Templates::Common::Header::Navbar', array(&$this, 'navBarCallback'));
HookRegistry::register('Template::Admin::Index::SiteManagement', array(&$this, 'siteManagementCallback'));
HookRegistry::register('LoadHandler', array(&$this, 'loadHandlerCallback'));
HookRegistry::register('PluginRegistry::loadCategory', array(&$this, 'callbackLoadCategory'));
// Rebuild index
HookRegistry::register('rebuildSearchIndex::flush', array(&$this, 'callbackFlush'));
}
return true;
}
return false;
}
/**
* Get the filename of the ADODB schema for this plugin.
*/
function getInstallSchemaFile() {
return $this->getPluginPath() . '/' . 'schema.xml';
}
/**
* Get the display name of this plugin
* @return string
*/
function getDisplayName() {
return __('plugins.generic.mysqlIndex.name');
}
/**
* Get the description of this plugin
* @return string
*/
function getDescription() {
return __('plugins.generic.mysqlIndex.description');
}
/**
* Get a list of available management verbs for this plugin
* @return array
*/
function getManagementVerbs() {
$verbs = array();
if ($this->getEnabled()) {
$verbs[] = array('adminCrosswalks', __('plugins.generic.mysqlIndex.crosswalks'));
}
return parent::getManagementVerbs($verbs);
}
/*
* Execute a management verb on this plugin
* @param $verb string
* @param $args array
* @param $message string Location for the plugin to put a result msg
* @return boolean
*/
function manage($verb, $args, &$message) {
if (!parent::manage($verb, $args, $message)) return false;
switch ($verb) {
case 'adminCrosswalks':
Request::redirect('mysqlIndexAdmin', 'adminCrosswalks');
return false;
default:
// Unknown management verb
assert(false);
return false;
}
}
/**
* Add the search link to the header.
*/
function navBarCallback($hookName, $args) {
$output =& $args[2];
$output .= '<li><a href="' . Request::url('misearch', 'index') . '">' . __('navigation.search') . '</a></li>';
return false;
}
/**
* Add the site management links
*/
function siteManagementCallback($hookName, $args) {
$output =& $args[2];
$output .= '<li>» <a href="' . Request::url('admin', 'plugin', array('generic', $this->getName(), 'adminCrosswalks')) . '">' . __('plugins.generic.mysqlIndex.crosswalks') . '</a></li>';
return false;
}
/**
* Register as a block plugin, even though this is a generic plugin.
* This will allow the plugin to behave as a block plugin, i.e. to
* have layout tasks performed on it.
* @param $hookName string
* @param $args array
*/
function callbackLoadCategory($hookName, $args) {
$category =& $args[0];
$plugins =& $args[1];
switch ($category) {
case 'blocks':
$this->import('MysqlIndexBlockPlugin');
$blockPlugin = new MysqlIndexBlockPlugin($this->getName());
$plugins[$blockPlugin->getSeq()][$blockPlugin->getPluginPath()] =& $blockPlugin;
break;
}
return false;
}
function loadHandlerCallback($hookName, $args) {
$page =& $args[0];
$op =& $args[1];
switch ($page) {
case 'misearch':
$this->import('MysqlIndexSearchHandler');
$methods = array_map('strtolower', get_class_methods('MysqlIndexSearchHandler'));
if (in_array(strtolower($op), $methods)) {
define('HANDLER_CLASS', 'MysqlIndexSearchHandler');
define('MYSQL_PLUGIN_NAME', $this->getName()); // Kludge
return true;
}
break;
case 'mysqlIndexAdmin':
$this->import('MysqlIndexAdminHandler');
$methods = array_map('strtolower', get_class_methods('MysqlIndexAdminHandler'));
if (in_array(strtolower($op), $methods)) {
define('HANDLER_CLASS', 'MysqlIndexAdminHandler');
define('MYSQL_PLUGIN_NAME', $this->getName()); // Kludge
return true;
}
break;
}
return false;
}
function insertRecordCallback($hookName, $args) {
// Handle the record.
$record =& $args[0];
$schemaPlugin =& $record->getSchemaPlugin();
$schemaPluginName = $schemaPlugin->getName();
$fieldDao =& DAORegistry::getDAO('FieldDAO');
foreach ($schemaPlugin->getFieldList() as $fieldName) {
$field =& $fieldDao->buildField($fieldName, $schemaPluginName);
$fieldValue = $schemaPlugin->getFieldValue($record, $fieldName, SORT_ORDER_TYPE_STRING);
SearchIndex::updateTextIndex($record->getRecordId(), $field->getFieldId(), $fieldValue);
}
return false;
}
function updateRecordCallback($hookName, $args) {
// First delete the old indexing
$this->deleteRecordCallback($hookName, $args);
// Then recreate it with the new record
return $this->insertRecordCallback($hookName, $args);
}
function deleteRecordCallback($hookName, $args) {
$record =& $args[0];
$searchDao =& DAORegistry::getDAO('SearchDAO');
$searchDao->deleteRecordObjects($record->getRecordId());
return false;
}
/**
* Flush the entire index prior to rebuilding it.
*/
function callbackFlush($hookName, $args) {
$searchDao =& DAORegistry::getDAO('SearchDAO');
$searchDao->flushIndex();
}
/**
* Get the name of the settings file to be installed site-wide when
* Harvester is installed.
* @return string
*/
function getInstallSitePluginSettingsFile() {
return $this->getPluginPath() . '/settings.xml';
}
}
?>