23 |
23 |
24 // $plugins->attachHook('session_started', 'SpecialSearch_paths_init();'); |
24 // $plugins->attachHook('session_started', 'SpecialSearch_paths_init();'); |
25 |
25 |
26 function SpecialSearch_paths_init() |
26 function SpecialSearch_paths_init() |
27 { |
27 { |
28 register_special_page('SearchRebuild', 'specialpage_search_rebuild'); |
28 register_special_page('SearchRebuild', 'specialpage_search_rebuild'); |
29 register_special_page('Search', 'specialpage_search'); |
29 register_special_page('Search', 'specialpage_search'); |
30 } |
30 } |
31 |
31 |
32 function page_Special_SearchRebuild() |
32 function page_Special_SearchRebuild() |
33 { |
33 { |
34 global $db, $session, $paths, $template, $plugins; // Common objects |
34 global $db, $session, $paths, $template, $plugins; // Common objects |
35 if ( !$session->get_permissions('mod_misc') ) |
35 if ( !$session->get_permissions('mod_misc') ) |
36 { |
36 { |
37 die_friendly('Unauthorized', '<p>You need to be an administrator to rebuild the search index</p>'); |
37 die_friendly('Unauthorized', '<p>You need to be an administrator to rebuild the search index</p>'); |
38 } |
38 } |
39 $template->header(); |
39 $template->header(); |
40 @set_time_limit(0); |
40 @set_time_limit(0); |
41 echo '<p>'; |
41 echo '<p>'; |
42 if($paths->rebuild_search_index(true)) |
42 if($paths->rebuild_search_index(true)) |
43 echo '</p><p>Index rebuilt!</p>'; |
43 echo '</p><p>Index rebuilt!</p>'; |
44 else |
44 else |
45 echo '<p>Index was not rebuilt due to an error.'; |
45 echo '<p>Index was not rebuilt due to an error.'; |
46 $template->footer(); |
46 $template->footer(); |
47 } |
47 } |
48 |
48 |
49 function page_Special_Search() |
49 function page_Special_Search() |
50 { |
50 { |
51 global $db, $session, $paths, $template, $plugins; // Common objects |
51 global $db, $session, $paths, $template, $plugins; // Common objects |
52 global $aggressive_optimize_html; |
52 global $aggressive_optimize_html; |
53 global $lang; |
53 global $lang; |
54 |
54 |
55 require_once(ENANO_ROOT.'/includes/search.php'); |
55 require_once(ENANO_ROOT.'/includes/search.php'); |
56 |
56 |
57 $aggressive_optimize_html = false; |
57 $aggressive_optimize_html = false; |
58 |
58 |
59 if ( !$q = $paths->getParam(0) ) |
59 if ( !$q = $paths->getParam(0) ) |
60 $q = ( isset($_GET['q']) ) ? $_GET['q'] : ''; |
60 $q = ( isset($_GET['q']) ) ? $_GET['q'] : ''; |
61 |
61 |
62 if(isset($_GET['words_any'])) |
62 if(isset($_GET['words_any'])) |
63 { |
63 { |
64 $q = ''; |
64 $q = ''; |
65 if(!empty($_GET['words_any'])) |
65 if(!empty($_GET['words_any'])) |
66 { |
66 { |
67 $q .= $_GET['words_any'] . ' '; |
67 $q .= $_GET['words_any'] . ' '; |
68 } |
68 } |
69 if(!empty($_GET['exact_phrase'])) |
69 if(!empty($_GET['exact_phrase'])) |
70 { |
70 { |
71 $q .= '"' . $_GET['exact_phrase'] . '" '; |
71 $q .= '"' . $_GET['exact_phrase'] . '" '; |
72 } |
72 } |
73 if(!empty($_GET['exclude_words'])) |
73 if(!empty($_GET['exclude_words'])) |
74 { |
74 { |
75 $not = explode(' ', $_GET['exclude_words']); |
75 $not = explode(' ', $_GET['exclude_words']); |
76 foreach ( $not as $i => $foo ) |
76 foreach ( $not as $i => $foo ) |
77 { |
77 { |
78 $not[$i] = '-' . $not[$i]; |
78 $not[$i] = '-' . $not[$i]; |
79 } |
79 } |
80 $q .= implode(' ', $not) . ' '; |
80 $q .= implode(' ', $not) . ' '; |
81 } |
81 } |
82 if(!empty($_GET['require_words'])) |
82 if(!empty($_GET['require_words'])) |
83 { |
83 { |
84 $req = explode(' ', $_GET['require_words']); |
84 $req = explode(' ', $_GET['require_words']); |
85 foreach ( $req as $i => $foo ) |
85 foreach ( $req as $i => $foo ) |
86 { |
86 { |
87 $req[$i] = '+' . $req[$i]; |
87 $req[$i] = '+' . $req[$i]; |
88 } |
88 } |
89 $q .= implode(' ', $req) . ' '; |
89 $q .= implode(' ', $req) . ' '; |
90 } |
90 } |
91 } |
91 } |
92 $q = trim($q); |
92 $q = trim($q); |
93 |
93 |
94 $template->header(); |
94 $template->header(); |
95 |
95 |
96 $qin = ( isset($q) ) ? str_replace('"', '\"', htmlspecialchars($q)) : ''; |
96 $qin = ( isset($q) ) ? str_replace('"', '\"', htmlspecialchars($q)) : ''; |
97 $search_form = '<form action="' . makeUrlNS('Special', 'Search') . '"> |
97 $search_form = '<form action="' . makeUrlNS('Special', 'Search') . '"> |
98 <input type="text" tabindex="1" name="q" size="50" value="' . $qin . '" /> <input tabindex="2" type="submit" value="' . $lang->get('search_btn_search') . '" /> <a href="' . makeUrlNS('Special', 'Search') . '">' . $lang->get('search_btn_advanced_search') . '</a> |
98 <input type="text" tabindex="1" name="q" size="50" value="' . $qin . '" /> <input tabindex="2" type="submit" value="' . $lang->get('search_btn_search') . '" /> <a href="' . makeUrlNS('Special', 'Search') . '">' . $lang->get('search_btn_advanced_search') . '</a> |
99 ' . ( $session->auth_level > USER_LEVEL_MEMBER ? '<input type="hidden" name="auth" value="' . $session->sid_super . '" />' : '' ) . ' |
99 ' . ( $session->auth_level > USER_LEVEL_MEMBER ? '<input type="hidden" name="auth" value="' . $session->sid_super . '" />' : '' ) . ' |
100 </form>'; |
100 </form>'; |
101 |
101 |
102 if ( !empty($q) ) |
102 if ( !empty($q) ) |
103 { |
103 { |
104 $search_start = microtime_float(); |
104 $search_start = microtime_float(); |
105 |
105 |
106 $results = perform_search($q, $warn, ( isset($_GET['match_case']) ), $word_list); |
106 $results = perform_search($q, $warn, ( isset($_GET['match_case']) ), $word_list); |
107 $warn = array_unique($warn); |
107 $warn = array_unique($warn); |
108 |
108 |
109 if ( file_exists( ENANO_ROOT . '/themes/' . $template->theme . '/search-result.tpl' ) ) |
109 if ( file_exists( ENANO_ROOT . '/themes/' . $template->theme . '/search-result.tpl' ) ) |
110 { |
110 { |
111 $parser = $template->makeParser('search-result.tpl'); |
111 $parser = $template->makeParser('search-result.tpl'); |
112 } |
112 } |
113 else |
113 else |
114 { |
114 { |
115 $tpl_code = <<<LONGSTRING |
115 $tpl_code = <<<LONGSTRING |
116 |
116 |
117 <!-- Start search result --> |
117 <!-- Start search result --> |
118 |
118 |
119 <div class="search-result"> |
119 <div class="search-result"> |
120 <p> |
120 <p> |
121 <h3><a href="{RESULT_URL}"><span class="search-result-annotation">{PAGE_NOTE}</span>{PAGE_TITLE}</a></h3> |
121 <h3><a href="{RESULT_URL}"><span class="search-result-annotation">{PAGE_NOTE}</span>{PAGE_TITLE}</a></h3> |
122 {PAGE_TEXT} |
122 {PAGE_TEXT} |
123 <span class="search-result-url">{PAGE_URL}</span> - |
123 <span class="search-result-url">{PAGE_URL}</span> - |
124 <!-- BEGINNOT special_page --><span class="search-result-info">{PAGE_LENGTH} {PAGE_LENGTH_UNIT}</span> -<!-- END special_page --> |
124 <!-- BEGINNOT special_page --><span class="search-result-info">{PAGE_LENGTH} {PAGE_LENGTH_UNIT}</span> -<!-- END special_page --> |
125 <span class="search-result-info">{lang:search_lbl_relevance} {RELEVANCE_SCORE}%</span> |
125 <span class="search-result-info">{lang:search_lbl_relevance} {RELEVANCE_SCORE}%</span> |
126 </p> |
126 </p> |
127 </div> |
127 </div> |
128 |
128 |
129 <!-- Finish search result --> |
129 <!-- Finish search result --> |
130 |
130 |
131 LONGSTRING; |
131 LONGSTRING; |
132 $parser = $template->makeParserText($tpl_code); |
132 $parser = $template->makeParserText($tpl_code); |
133 } |
133 } |
134 foreach ( $results as $i => $_ ) |
134 foreach ( $results as $i => $_ ) |
135 { |
135 { |
136 $result =& $results[$i]; |
136 $result =& $results[$i]; |
137 $result['page_text'] = str_replace(array('<highlight>', '</highlight>'), array('<span class="search-term">', '</span>'), $result['page_text']); |
137 $result['page_text'] = str_replace(array('<highlight>', '</highlight>'), array('<span class="search-term">', '</span>'), $result['page_text']); |
138 if ( !empty($result['page_text']) ) |
138 if ( !empty($result['page_text']) ) |
139 $result['page_text'] .= '<br />'; |
139 $result['page_text'] .= '<br />'; |
140 $result['page_name'] = str_replace(array('<highlight>', '</highlight>'), array('<span class="title-search-term">', '</span>'), $result['page_name']); |
140 $result['page_name'] = str_replace(array('<highlight>', '</highlight>'), array('<span class="title-search-term">', '</span>'), $result['page_name']); |
141 $result['url_highlight'] = str_replace(array('<highlight>', '</highlight>'), array('<span class="url-search-term">', '</span>'), $result['url_highlight']); |
141 $result['url_highlight'] = str_replace(array('<highlight>', '</highlight>'), array('<span class="url-search-term">', '</span>'), $result['url_highlight']); |
142 if ( $result['page_length'] >= 1048576 ) |
142 if ( $result['page_length'] >= 1048576 ) |
143 { |
143 { |
144 $result['page_length'] = round($result['page_length'] / 1048576, 1); |
144 $result['page_length'] = round($result['page_length'] / 1048576, 1); |
145 $length_unit = $lang->get('etc_unit_megabytes_short'); |
145 $length_unit = $lang->get('etc_unit_megabytes_short'); |
146 } |
146 } |
147 else if ( $result['page_length'] >= 1024 ) |
147 else if ( $result['page_length'] >= 1024 ) |
148 { |
148 { |
149 $result['page_length'] = round($result['page_length'] / 1024, 1); |
149 $result['page_length'] = round($result['page_length'] / 1024, 1); |
150 $length_unit = $lang->get('etc_unit_kilobytes_short'); |
150 $length_unit = $lang->get('etc_unit_kilobytes_short'); |
151 } |
151 } |
152 else |
152 else |
153 { |
153 { |
154 $length_unit = $lang->get('etc_unit_bytes'); |
154 $length_unit = $lang->get('etc_unit_bytes'); |
155 } |
155 } |
156 //$url = makeUrlComplete($result['namespace'], $result['page_id']); |
156 //$url = makeUrlComplete($result['namespace'], $result['page_id']); |
157 //$url = preg_replace('/\?.+$/', '', $url); |
157 //$url = preg_replace('/\?.+$/', '', $url); |
158 $url = $result['url_highlight']; |
158 $url = $result['url_highlight']; |
159 |
159 |
160 $parser->assign_vars(array( |
160 $parser->assign_vars(array( |
161 'PAGE_TITLE' => $result['page_name'], |
161 'PAGE_TITLE' => $result['page_name'], |
162 'PAGE_TEXT' => $result['page_text'], |
162 'PAGE_TEXT' => $result['page_text'], |
163 'PAGE_LENGTH' => $result['page_length'], |
163 'PAGE_LENGTH' => $result['page_length'], |
164 'RELEVANCE_SCORE' => $result['score'], |
164 'RELEVANCE_SCORE' => $result['score'], |
165 'RESULT_URL' => makeUrlNS($result['namespace'], $result['page_id'], false, true) . ( isset($result['url_append']) ? $result['url_append'] : '' ), |
165 'RESULT_URL' => makeUrlNS($result['namespace'], $result['page_id'], false, true) . ( isset($result['url_append']) ? $result['url_append'] : '' ), |
166 'PAGE_LENGTH_UNIT' => $length_unit, |
166 'PAGE_LENGTH_UNIT' => $length_unit, |
167 'PAGE_URL' => $url, |
167 'PAGE_URL' => $url, |
168 'PAGE_NOTE' => ( isset($result['page_note']) ? $result['page_note'] . ' ' : '' ) |
168 'PAGE_NOTE' => ( isset($result['page_note']) ? $result['page_note'] . ' ' : '' ) |
169 )); |
169 )); |
170 $has_content = ( $result['namespace'] == 'Special' || !empty($result['zero_length']) ); |
170 $has_content = ( $result['namespace'] == 'Special' || !empty($result['zero_length']) ); |
171 |
171 |
172 $code = $plugins->setHook('search_global_results'); |
172 $code = $plugins->setHook('search_global_results'); |
173 foreach ( $code as $cmd ) |
173 foreach ( $code as $cmd ) |
174 { |
174 { |
175 eval($cmd); |
175 eval($cmd); |
176 } |
176 } |
177 |
177 |
178 $parser->assign_bool(array( |
178 $parser->assign_bool(array( |
179 'special_page' => $has_content |
179 'special_page' => $has_content |
180 )); |
180 )); |
181 $result = $parser->run(); |
181 $result = $parser->run(); |
182 } |
182 } |
183 unset($result); |
183 unset($result); |
184 |
184 |
185 $per_page = 10; |
185 $per_page = 10; |
186 $start = ( isset($_GET['start']) ? intval($_GET['start']) : 0 ); |
186 $start = ( isset($_GET['start']) ? intval($_GET['start']) : 0 ); |
187 // for plugin compatibility: |
187 // for plugin compatibility: |
188 $offset =& $start; |
188 $offset =& $start; |
189 $start_string = $start + 1; |
189 $start_string = $start + 1; |
190 $per_string = $start_string + $per_page - 1; |
190 $per_string = $start_string + $per_page - 1; |
191 $num_results = count($results); |
191 $num_results = count($results); |
192 if ( $per_string > $num_results ) |
192 if ( $per_string > $num_results ) |
193 $per_string = $num_results; |
193 $per_string = $num_results; |
194 |
194 |
195 $search_time = microtime_float() - $search_start; |
195 $search_time = microtime_float() - $search_start; |
196 $search_time = round($search_time, 3); |
196 $search_time = round($search_time, 3); |
197 |
197 |
198 $q_trim = ( strlen($q) > 30 ) ? substr($q, 0, 27) . '...' : $q; |
198 $q_trim = ( strlen($q) > 30 ) ? substr($q, 0, 27) . '...' : $q; |
199 $q_trim = htmlspecialchars($q_trim); |
199 $q_trim = htmlspecialchars($q_trim); |
200 |
200 |
201 $result_detail = $lang->get('search_msg_result_detail', array( |
201 $result_detail = $lang->get('search_msg_result_detail', array( |
202 'start_string' => $start_string, |
202 'start_string' => $start_string, |
203 'per_string' => $per_string, |
203 'per_string' => $per_string, |
204 'q_trim' => $q_trim, |
204 'q_trim' => $q_trim, |
205 'num_results' => $num_results, |
205 'num_results' => $num_results, |
206 'search_time' => $search_time |
206 'search_time' => $search_time |
207 )); |
207 )); |
208 $result_string = ( count($results) > 0 ) ? $result_detail : $lang->get('search_msg_no_results'); |
208 $result_string = ( count($results) > 0 ) ? $result_detail : $lang->get('search_msg_no_results'); |
209 |
209 |
210 echo '<div class="search-hibar"> |
210 echo '<div class="search-hibar"> |
211 <div style="float: right;"> |
211 <div style="float: right;"> |
212 ' . $result_string . ' |
212 ' . $result_string . ' |
213 </div> |
213 </div> |
214 <b>' . $lang->get('search_lbl_site_search') . '</b> |
214 <b>' . $lang->get('search_lbl_site_search') . '</b> |
215 </div> |
215 </div> |
216 <div class="search-lobar"> |
216 <div class="search-lobar"> |
217 ' . $search_form . ' |
217 ' . $search_form . ' |
218 </div>'; |
218 </div>'; |
219 |
219 |
220 if ( count($warn) > 0 ) |
220 if ( count($warn) > 0 ) |
221 { |
221 { |
222 echo '<div class="warning-box" style="margin: 10px 0 0 0;">'; |
222 echo '<div class="warning-box" style="margin: 10px 0 0 0;">'; |
223 echo '<b>' . $lang->get('search_err_query_title') . '</b><br /> |
223 echo '<b>' . $lang->get('search_err_query_title') . '</b><br /> |
224 ' . $lang->get('search_err_query_body'); |
224 ' . $lang->get('search_err_query_body'); |
225 echo '<ul><li>' . implode('</li><li>', $warn) . '</li></ul>'; |
225 echo '<ul><li>' . implode('</li><li>', $warn) . '</li></ul>'; |
226 echo '</div>'; |
226 echo '</div>'; |
227 } |
227 } |
228 |
228 |
229 if ( count($results) > 0 ) |
229 if ( count($results) > 0 ) |
230 { |
230 { |
231 $html = paginate_array( |
231 $html = paginate_array( |
232 $results, |
232 $results, |
233 count($results), |
233 count($results), |
234 makeUrlNS('Special', 'Search', 'q=' . str_replace('%', '%%', htmlspecialchars(urlencode($q))) . '&start=%s'), |
234 makeUrlNS('Special', 'Search', 'q=' . str_replace('%', '%%', htmlspecialchars(urlencode($q))) . '&start=%s'), |
235 $start, |
235 $start, |
236 $per_page |
236 $per_page |
237 ); |
237 ); |
238 echo $html; |
238 echo $html; |
239 } |
239 } |
240 else |
240 else |
241 { |
241 { |
242 // No results for the search |
242 // No results for the search |
243 echo '<h3 style="font-weight: normal;">' . $lang->get('search_body_no_results_title', array('query' => htmlspecialchars($q))) . '</h3>'; |
243 echo '<h3 style="font-weight: normal;">' . $lang->get('search_body_no_results_title', array('query' => htmlspecialchars($q))) . '</h3>'; |
244 echo $lang->get('search_body_no_results_body', array( |
244 echo $lang->get('search_body_no_results_body', array( |
245 'query' => htmlspecialchars($q), |
245 'query' => htmlspecialchars($q), |
246 'create_url' => makeUrl($q), |
246 'create_url' => makeUrl($q), |
247 'special_url' => makeUrlNS('Special', 'SpecialPages'), |
247 'special_url' => makeUrlNS('Special', 'SpecialPages'), |
248 )); |
248 )); |
249 } |
249 } |
250 $code = $plugins->setHook('search_results'); |
250 $code = $plugins->setHook('search_results'); |
251 foreach ( $code as $cmd ) |
251 foreach ( $code as $cmd ) |
252 { |
252 { |
253 eval($cmd); |
253 eval($cmd); |
254 } |
254 } |
255 } |
255 } |
256 else |
256 else |
257 { |
257 { |
258 ?> |
258 ?> |
259 <form action="<?php echo makeUrl($paths->page); ?>" method="get"> |
259 <form action="<?php echo makeUrl($paths->page); ?>" method="get"> |
260 <?php if ( urlSeparator == '&' ): ?> |
260 <?php if ( urlSeparator == '&' ): ?> |
261 <input type="hidden" name="title" value="<?php echo $paths->nslist['Special'] . 'Search'; ?>" /> |
261 <input type="hidden" name="title" value="<?php echo $paths->nslist['Special'] . 'Search'; ?>" /> |
262 <?php |
262 <?php |
263 echo ( $session->auth_level > USER_LEVEL_MEMBER ? '<input type="hidden" name="auth" value="' . $session->sid_super . '" />' : '' ); |
263 echo ( $session->auth_level > USER_LEVEL_MEMBER ? '<input type="hidden" name="auth" value="' . $session->sid_super . '" />' : '' ); |
264 endif; ?> |
264 endif; ?> |
265 <div class="tblholder"> |
265 <div class="tblholder"> |
266 <table border="0" style="width: 100%;" cellspacing="1" cellpadding="4"> |
266 <table border="0" style="width: 100%;" cellspacing="1" cellpadding="4"> |
267 <tr><th colspan="2"><?php echo $lang->get('search_th_advanced_search'); ?></th></tr> |
267 <tr><th colspan="2"><?php echo $lang->get('search_th_advanced_search'); ?></th></tr> |
268 <tr> |
268 <tr> |
269 <td class="row1"><?php echo $lang->get('search_lbl_field_any'); ?></td> |
269 <td class="row1"><?php echo $lang->get('search_lbl_field_any'); ?></td> |
270 <td class="row1"><input type="text" name="words_any" size="40" /></td> |
270 <td class="row1"><input type="text" name="words_any" size="40" /></td> |
271 </tr> |
271 </tr> |
272 <tr> |
272 <tr> |
273 <td class="row2"><?php echo $lang->get('search_lbl_field_exact'); ?></td> |
273 <td class="row2"><?php echo $lang->get('search_lbl_field_exact'); ?></td> |
274 <td class="row2"><input type="text" name="exact_phrase" size="40" /></td> |
274 <td class="row2"><input type="text" name="exact_phrase" size="40" /></td> |
275 </tr> |
275 </tr> |
276 <tr> |
276 <tr> |
277 <td class="row1"><?php echo $lang->get('search_lbl_field_none'); ?></td> |
277 <td class="row1"><?php echo $lang->get('search_lbl_field_none'); ?></td> |
278 <td class="row1"><input type="text" name="exclude_words" size="40" /></td> |
278 <td class="row1"><input type="text" name="exclude_words" size="40" /></td> |
279 </tr> |
279 </tr> |
280 <tr> |
280 <tr> |
281 <td class="row2"><?php echo $lang->get('search_lbl_field_all'); ?></td> |
281 <td class="row2"><?php echo $lang->get('search_lbl_field_all'); ?></td> |
282 <td class="row2"><input type="text" name="require_words" size="40" /></td> |
282 <td class="row2"><input type="text" name="require_words" size="40" /></td> |
283 </tr> |
283 </tr> |
284 <tr> |
284 <tr> |
285 <td class="row1"> |
285 <td class="row1"> |
286 <label for="chk_case"><?php echo $lang->get('search_lbl_field_casesensitive'); ?></label> |
286 <label for="chk_case"><?php echo $lang->get('search_lbl_field_casesensitive'); ?></label> |
287 </td> |
287 </td> |
288 <td class="row1"> |
288 <td class="row1"> |
289 <input type="checkbox" name="match_case" id="chk_case" /> |
289 <input type="checkbox" name="match_case" id="chk_case" /> |
290 </td> |
290 </td> |
291 </tr> |
291 </tr> |
292 <tr> |
292 <tr> |
293 <th colspan="2" class="subhead"> |
293 <th colspan="2" class="subhead"> |
294 <input type="submit" name="do_search" value="<?php echo $lang->get('search_btn_search'); ?>" /> |
294 <input type="submit" name="do_search" value="<?php echo $lang->get('search_btn_search'); ?>" /> |
295 </td> |
295 </td> |
296 </tr> |
296 </tr> |
297 </table> |
297 </table> |
298 </div> |
298 </div> |
299 </form> |
299 </form> |
300 <?php |
300 <?php |
301 } |
301 } |
302 |
302 |
303 $template->footer(); |
303 $template->footer(); |
304 } |
304 } |
305 |
305 |
306 ?> |
306 ?> |