forked from symphonists/search_index
/
extension.driver.php
151 lines (129 loc) · 4.08 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
<?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 {
/**
* Extension meta data
*/
public function about() {
return array(
'name' => 'Search Index',
'version' => '0.1',
'release-date' => '2010-01-17',
'author' => array(
'name' => 'Nick Dunn'
),
'description' => 'Index text content of entries for efficient fulltext search.'
);
}
/**
* Set up configuration defaults and database tables
*/
public function install(){
// number of entries per page when rebuilding index
Symphony::Configuration()->set('re-index-per-page', 20, 'search_index');
// refresh frequency when rebuilding index
Symphony::Configuration()->set('re-index-refresh-rate', 0.5, 'search_index');
// append wildcard * to the end of search phrases (reduces performance, increases matches)
Symphony::Configuration()->set('append-wildcard', 'yes', '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');
Administration::instance()->saveConfig();
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`))");
Symphony::Database()->query(
"CREATE TABLE `tbl_search_index` (
`id` int(11) NOT NULL auto_increment,
`entry_id` int(11) NOT NULL,
`section_id` int(11) NOT NULL,
`language` varchar(2) NOT NULL,
`data` text,
PRIMARY KEY (`id`),
FULLTEXT KEY `data` (`data`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8"
);
}
catch (Exception $e){
return false;
}
return true;
}
/**
* Cleanup after yourself, remove configuration and database tables
*/
public function uninstall(){
Symphony::Configuration()->remove('search_index');
$this->_Parent->saveConfig();
try{
Symphony::Database()->query("DROP TABLE `tbl_search_index`");
Symphony::Database()->query("DROP TABLE `tbl_fields_search_index`");
}
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' => 'Delete',
'callback' => 'deleteEntryIndex'
),
);
}
/**
* Append navigation to Blueprints menu
*/
public function fetchNavigation() {
return array(
array(
'location' => 'Blueprints',
'name' => 'Search Indexes',
'link' => '/indexes/'
),
);
}
/**
* Index this entry for search
*
* @param object $context
*/
public function indexEntry($context) {
SearchIndex::indexEntry($context['entry']->get('id'), $context['section']->get('id'));
}
/**
* Delete this entry's search index
*
* @param object $context
*/
public function deleteEntryIndex($context) {
SearchIndex::deleteIndexByEntry($context['entry_id']);
}
}