-
Notifications
You must be signed in to change notification settings - Fork 21
/
extension.driver.php
314 lines (264 loc) · 9.12 KB
/
extension.driver.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
<?php
require_once(TOOLKIT . '/class.datasource.php');
require_once(EXTENSIONS . '/search_index/lib/class.search_index.php');
require_once(EXTENSIONS . '/search_index/lib/class.entry_xml_datasource.php');
require_once(EXTENSIONS . '/search_index/lib/class.reindex_datasource.php');
class Extension_Search_Index extends Extension {
private function createTables() {
try {
Symphony::Database()->query(
"CREATE TABLE IF NOT EXISTS `tbl_fields_search_index` (
`id` int(11) unsigned NOT NULL auto_increment,
`field_id` int(11) unsigned NOT NULL,
PRIMARY KEY (`id`),
KEY `field_id` (`field_id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;"
);
Symphony::Database()->query(
"CREATE TABLE IF NOT EXISTS `tbl_search_index` (
`id` int(11) NOT NULL auto_increment,
`entry_id` int(11) NOT NULL,
`section_id` int(11) NOT NULL,
`data` text,
PRIMARY KEY (`id`),
KEY `entry_id` (`entry_id`),
FULLTEXT KEY `data` (`data`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;"
);
Symphony::Database()->query(
"CREATE TABLE IF NOT EXISTS `tbl_search_index_logs` (
`id` int(11) NOT NULL auto_increment,
`date` datetime NOT NULL,
`keywords` varchar(255) default NULL,
`keywords_manipulated` varchar(255) default NULL,
`sections` varchar(255) default NULL,
`page` int(11) NOT NULL,
`results` int(11) default NULL,
`session_id` varchar(255) default NULL,
PRIMARY KEY (`id`),
FULLTEXT KEY `keywords` (`keywords`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;"
);
Symphony::Database()->query(
"CREATE TABLE IF NOT EXISTS `tbl_search_index_keywords` (
`id` int(11) NOT NULL auto_increment,
`keyword` varchar(255) default NULL,
PRIMARY KEY (`id`),
FULLTEXT KEY `keyword` (`keyword`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;"
);
Symphony::Database()->query(
"CREATE TABLE IF NOT EXISTS `tbl_search_index_entry_keywords` (
`entry_id` int(11) default NULL,
`keyword_id` int(11) default NULL,
`frequency` int(11) default NULL,
KEY `entry_id` (`entry_id`),
KEY `keyword_id` (`keyword_id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;"
);
}
catch (Exception $e){
#var_dump($e);die;
return FALSE;
}
return TRUE;
}
private function setInitialConfig() {
Symphony::Configuration()->set('re-index-per-page', 20, 'search_index');
Symphony::Configuration()->set('re-index-refresh-rate', 0.5, 'search_index');
// names of GET parameters used for custom search DS
Symphony::Configuration()->set('get-param-prefix', '', 'search_index');
Symphony::Configuration()->set('get-param-keywords', 'keywords', 'search_index');
Symphony::Configuration()->set('get-param-per-page', 'per-page', 'search_index');
Symphony::Configuration()->set('get-param-sort', 'sort', 'search_index');
Symphony::Configuration()->set('get-param-direction', 'direction', 'search_index');
Symphony::Configuration()->set('get-param-sections', 'sections', 'search_index');
Symphony::Configuration()->set('get-param-page', 'page', 'search_index');
// default search params, used if not specifed in GET
Symphony::Configuration()->set('default-sections', '', 'search_index');
Symphony::Configuration()->set('default-per-page', 20, 'search_index');
Symphony::Configuration()->set('default-sort', 'score', 'search_index');
Symphony::Configuration()->set('default-direction', 'desc', 'search_index');
Symphony::Configuration()->set('excerpt-length', 250, 'search_index');
Symphony::Configuration()->set('min-word-length', 3, 'search_index');
Symphony::Configuration()->set('max-word-length', 30, 'search_index');
Symphony::Configuration()->set('stem-words', 'yes', 'search_index');
Symphony::Configuration()->set('build-entries', 'no', 'search_index');
Symphony::Configuration()->set('mode', 'like', 'search_index');
Symphony::Configuration()->set('log-keywords', 'yes', 'search_index');
Symphony::Configuration()->write();
}
/**
* Set up configuration defaults and database tables
*/
public function install(){
$this->createTables();
$this->setInitialConfig();
return TRUE;
}
public function update($previousVersion = false){
if(version_compare($previousVersion, '0.6', '<')){
Symphony::Database()->query("ALTER TABLE `tbl_search_index_logs` ADD `keywords_manipulated` varchar(255) default NULL");
}
// lower versions get the full upgrade treatment, new tables and config
// should retain "indexes" and "synonyms" in config though.
if(version_compare($previousVersion, '0.7.1', '<')){
$this->install();
}
return TRUE;
}
/**
* Cleanup after yourself, remove configuration and database tables
*/
public function uninstall(){
Symphony::Configuration()->remove('search_index');
Symphony::Configuration()->write();
try{
Symphony::Database()->query("DROP TABLE `tbl_search_index`");
Symphony::Database()->query("DROP TABLE `tbl_fields_search_index`");
Symphony::Database()->query("DROP TABLE `tbl_search_index_logs`");
Symphony::Database()->query("DROP TABLE `tbl_search_index_keywords`");
Symphony::Database()->query("DROP TABLE `tbl_search_index_entry_keywords`");
}
catch(Exception $e){
return false;
}
return true;
}
/**
* Callback functions for backend delegates
*/
public function getSubscribedDelegates() {
return array(
array(
'page' => '/publish/new/',
'delegate' => 'EntryPostCreate',
'callback' => 'indexEntry'
),
array(
'page' => '/publish/edit/',
'delegate' => 'EntryPostEdit',
'callback' => 'indexEntry'
),
array(
'page' => '/publish/',
'delegate' => 'EntryPostDelete',
'callback' => 'deleteEntryIndex'
),
array(
'page' => '/frontend/',
'delegate' => 'EventPostSaveFilter',
'callback' => 'indexEntry'
),
// Dashboard
array(
'page' => '/backend/',
'delegate' => 'DashboardPanelRender',
'callback' => 'renderPanel'
),
array(
'page' => '/backend/',
'delegate' => 'DashboardPanelTypes',
'callback' => 'dashboardPanelTypes'
),
);
}
/**
* Append navigation to Blueprints menu
*/
public function fetchNavigation() {
return array(
array(
'name' => __('Search Index'),
'type' => 'structure',
'children' => array(
array(
'name' => __('Indexes'),
'link' => '/indexes/',
'limit' => 'developer'
),
array(
'name' => __('Synonyms'),
'link' => '/synonyms/',
'limit' => 'developer'
),
array(
'name' => __('Logs'),
'link' => '/logs/',
'limit' => 'developer'
),
)
)
);
}
/**
* Index this entry for search
*
* @param object $context
*/
public function indexEntry($context) {
SearchIndex::indexEntry($context['entry']->get('id'), $context['entry']->get('section_id'));
}
/**
* Delete this entry's search index
*
* @param object $context
*/
public function deleteEntryIndex($context) {
if (is_array($context['entry_id'])) {
foreach($context['entry_id'] as $entry_id) {
SearchIndex::deleteIndexByEntry($entry_id);
}
} else {
SearchIndex::deleteIndexByEntry($context['entry_id']);
}
}
/*-------------------------------------------------------------------------
Dashboard
-------------------------------------------------------------------------*/
public function dashboardPanelTypes($context) {
$context['types']['search_index'] = "Search Index";
}
public function renderPanel($context) {
$config = $context['config'];
switch($context['type']) {
case 'search_index':
$logs = SearchIndex::getLogs('date', 'desc', 1);
$thead = array(
array(__('Date'), 'col'),
array(__('Keywords'), 'col'),
array(__('Results'), 'col')
);
$tbody = array();
if (!is_array($logs) or empty($logs)) {
$tbody = array(Widget::TableRow(array(
Widget::TableData(
__('No data available.'),
'inactive',
null,
count($thead)
)))
);
}
else {
foreach ($logs as $log) {
$tbody[] = Widget::TableRow(
array(
Widget::TableData(DateTimeObj::get(__SYM_DATETIME_FORMAT__, strtotime($log['date']))),
Widget::TableData($log['keywords']),
Widget::TableData($log['results'])
)
);
}
}
$table = Widget::Table(
Widget::TableHead($thead), null,
Widget::TableBody($tbody), null
);
$table->setAttribute('class', 'skinny');
$context['panel']->appendChild($table);
$context['panel']->appendChild(new XMLElement('p', '<a href="'.(SYMPHONY_URL . '/extension/search_index/logs/').'">' . __('View full search logs') . ' →</a>', array('style' => 'margin:0.7em;text-align:right;')));
break;
}
}
}