-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathi18n_search.php
237 lines (196 loc) · 8.59 KB
/
i18n_search.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
<?php
/*
Plugin Name: I18N Search
Description: Search (I18N enabled!)
Version: 2.13.1
Author: Martin Vlcek
Author URI: http://mvlcek.bplaced.net
Public functions:
delete_i18n_search_index()
return_i18n_search_results($tags=null, $words=null, $first=0, $max=10, $order=null, $lang=null)
returns an array of search results, each with the attributes url, parent, title,
date (UNIX timestamp), content, score and custom fields and
a member function getExcerpt($content, $excerptLength)
return_i18n_tags()
returns an ordered array, where the key is the tag and the value is an array of
urls (slugs) using that tag. Tags starting with "_" are ignored.
Display functions:
get_i18n_search_results($params)
outputs the search results. params is an associative array and includes keys like 'tags'
(to search for), 'words' (to search for), 'max' (number of results to display), ...
If no tags or words are given they are taken from the request parameters 'tags' and 'words'
A param 'numWords' < 0 will output the whole content of the page.
Will use Pagify plugin for pagination, if installed.
get_i18n_tags($params)
outputs the ordered tags in spans with class "tag" and a font-size proportional to the
percentage of pages using it (within the percentage range specified by the $params['minTagSize']
and $params['maxTagSize']).
Tags starting with "_" are ignored.
get_i18n_search_form($params)
outputs a search form. $params['slug'] is the URL of the result page.
get_i18n_search_rsslink($params)
outputs a link to an RSS document, needs parameters 'title', 'name' and parameters specifying
a search, see get_i18n_search_results.
*/
# get correct id for plugin
$thisfile = basename(__FILE__, ".php");
define('I18N_WORD_INDEX', 'i18n_word_index.txt');
define('I18N_TAG_INDEX', 'i18n_tag_index.txt');
define('I18N_DATE_INDEX', 'i18n_date_index.txt');
define('I18N_SEARCH_SETTINGS_FILE', 'i18n_search_settings.xml');
# for sorting results - these can be changed in the search administration
define('I18N_CONTENT_WEIGHT', 1);
define('I18N_TITLE_WEIGHT', 5);
define('I18N_TAG_WEIGHT', 10);
define('I18N_MAX_RESULTS', 10);
define('I18N_NUM_WORDS', 30);
define('I18N_MIN_TAG_SIZE', 100);
define('I18N_MAX_TAG_SIZE', 250);
define('I18N_TAGS_ALWAYS_DEFLANG', 0);
define('I18N_TAGS_ALWAYS_LANG', 1);
define('I18N_TAGS_LANG_OR_DEFLANG', 2);
# action to add non-page items to index
# - call i18n_search_index_item($id, $language, $creDate, $pubDate, $tags, $title, $content) to add an item
define('I18N_ACTION_INDEX', 'search-index');
# filter to add custom fields of a page to the index
# - parameters: $item (of type I18nSearchPageItem)
# - call methods addTags, addTitle, addContent to add fields to index
# - return value is ignored
define('I18N_FILTER_INDEX_PAGE', 'search-index-page');
# filter to return an object of type I18nSearchResultItem for an indexed non-page item
# - parameters: $id, $language, $creationDate, $publicationDate, $score
# (dates are UNIX timestamps, score is an integer)
# - should return an object of a class extending I18nSearchResultItem
define('I18N_FILTER_SEARCH_ITEM', 'search-item');
# filter search results (vetoed items are removed from results)
# - parameters: $item (of type I18nSearchResultItem or I18nSearchResultPage)
# - must return true, if item should not be included in search results
define('I18N_FILTER_VETO_SEARCH_ITEM', 'search-veto');
# filter to display a search item
# - parameters: $item, $showLanguage, $showDate, $dateFormat, $numWords
# (item is of type I18nSearchResultItem, dateFormat for strftime)
# - if the function handles the item, it must output the HTML
# - must return true, if item was handled, false otherwise
define('I18N_FILTER_DISPLAY_ITEM', 'search-display');
# register plugin
register_plugin(
$thisfile,
'I18N Search',
'2.13.1',
'Martin Vlcek',
'http://mvlcek.bplaced.net',
'Search (I18N enabled!)',
'plugins',
'i18n_search_configure'
);
# load i18n texts
if (basename($_SERVER['PHP_SELF']) != 'index.php') { // back end only
i18n_merge('i18n_search', substr($LANG,0,2));
i18n_merge('i18n_search', 'en');
}
# ===== BACKEND =====
add_action('changedata-save', 'delete_i18n_search_index');
add_action('page-delete', 'delete_i18n_search_index'); // GetSimple 3.0+
add_action('plugins-sidebar', 'createSideMenu', array($thisfile, i18n_r('i18n_search/CONFIGURE')));
# ===== FRONTEND =====
add_action('index-pretemplate','i18n_search_pretemplate_for_rss');
add_action('index-pretemplate','i18n_search_pretemplate_for_mark');
add_action('theme-header','i18n_search_header_for_rss');
add_filter('content','i18n_search_content');
add_filter('search-index-page', 'i18n_search_index_page');
# ===== BACKEND HOOKS =====
# workaround for page-delete in GetSimple 2.03:
if (basename($_SERVER['PHP_SELF']) == 'deletefile.php') {
delete_i18n_search_index();
}
# can also be called directly
function delete_i18n_search_index() {
require_once(GSPLUGINPATH.'i18n_search/indexer.class.php');
I18nSearchIndexer::deleteIndex();
}
# ===== INDEXING =====
function create_i18n_search_index() {
require_once(GSPLUGINPATH.'i18n_search/indexer.class.php');
I18nSearchIndexer::index();
}
function i18n_search_index_page($item) {
// virtual parent tag
$parent = @$item->parent;
if ($parent) $item->addTags('parent', array('_parent_'.$parent));
// virtual date tags
$pubDate = @$item->pubDate;
$item->addTags('pubDate', array('_pub_'.date('Ym',$pubDate), '_pub_'.date('Y',$pubDate)));
$creDate = @$item->creDate;
if ($creDate) {
$item->addTags('creDate', array('_cre_'.date('Ym',$creDate), '_cre_'.date('Y',$creDate)));
}
$menu = @$item->menuStatus;
if ($menu == 'Y') $item->addTags('menuStatus', array('_menu'));
}
# ===== FRONTEND HOOKS =====
function i18n_search_content($content) {
require_once(GSPLUGINPATH.'i18n_search/viewer.class.php');
return I18nSearchViewer::processContent($content);
}
function i18n_search_pretemplate_for_rss() {
require_once(GSPLUGINPATH.'i18n_search/viewer.class.php');
return I18nSearchViewer::processPreTemplateForRSS();
}
function i18n_search_header_for_rss() {
require_once(GSPLUGINPATH.'i18n_search/viewer.class.php');
return I18nSearchViewer::processHeaderForRSS();
}
function i18n_search_pretemplate_for_mark() {
if (file_exists(GSDATAOTHERPATH.I18N_SEARCH_SETTINGS_FILE)) {
$data = getXML(GSDATAOTHERPATH.I18N_SEARCH_SETTINGS_FILE);
if (!$data || !((string) $data->mark)) return;
require_once(GSPLUGINPATH.'i18n_search/marker.class.php');
$words = I18nSearchMarker::getWords();
if ($words && count($words) > 0) {
add_filter('content','i18n_search_mark');
}
}
}
function i18n_search_mark($content) {
require_once(GSPLUGINPATH.'i18n_search/marker.class.php');
return I18nSearchMarker::mark($content, I18nSearchMarker::getWords());
}
# ===== FRONTEND FUNCTIONS =====
function return_i18n_number_of_results($tags=null, $words=null) {
require_once(GSPLUGINPATH.'i18n_search/searcher.class.php');
$results = I18nSearcher::search($tags, $words);
return $results ? count($results) : 0;
}
function &return_i18n_tags() {
require_once(GSPLUGINPATH.'i18n_search/searcher.class.php');
return I18nSearcher::tags();
}
function return_i18n_search_results($tags=null, $words=null, $first=0, $max=10, $order=null, $lang=null) {
require_once(GSPLUGINPATH.'i18n_search/searcher.class.php');
$results = I18nSearcher::search($tags, $words, $order, $lang);
$count = count($results);
if ($max > 0) $results = array_slice($results, $first, $max); else if ($first > 0) $results = array_slice($results, $first);
return array('totalCount' => $count, 'first' => $first, 'results' => $results);
}
function get_i18n_search_rsslink($params) {
require_once(GSPLUGINPATH.'i18n_search/viewer.class.php');
return I18nSearchViewer::displayRSSLink($params);
}
function get_i18n_search_form($params=null) {
require_once(GSPLUGINPATH.'i18n_search/viewer.class.php');
return I18nSearchViewer::displaySearchForm($params);
}
function get_i18n_search_results($params=null) {
# switch off paging by default as multiple paged search results are not supported
if (!isset($params['showPaging'])) $params['showPaging'] = false;
require_once(GSPLUGINPATH.'i18n_search/viewer.class.php');
return I18nSearchViewer::displaySearchResults($params);
}
function get_i18n_tags($params=null) {
require_once(GSPLUGINPATH.'i18n_search/viewer.class.php');
return I18nSearchViewer::displayTags($params);
}
# ===== BACKEND PAGES =====
function i18n_search_configure() {
include(GSPLUGINPATH.'i18n_search/configure.php');
}