OSDN Git Service

BugTrack/2436 ESLint - Linting utility for JavaScript
[pukiwiki/pukiwiki.git] / plugin / search2.inc.php
1 <?php
2 // PukiWiki - Yet another WikiWikiWeb clone.
3 // search2.inc.php
4 // Copyright 2017 PukiWiki Development Team
5 // License: GPL v2 or (at your option) any later version
6 //
7 // Search2 plugin - Show detail result using JavaScript
8
9 define('PLUGIN_SEARCH2_MAX_BASE', 16); // #search(1,2,3,...,15,16)
10
11 define('PLUGIN_SEARCH2_RESULT_RECORD_LIMIT', 500);
12 define('PLUGIN_SEARCH2_RESULT_RECORD_LIMIT_START', 100);
13 define('PLUGIN_SEARCH2_SEARCH_WAIT_MILLISECONDS', 1000);
14 define('PLUGIN_SEARCH2_SEARCH_MAX_RESULTS', 1000);
15
16 // Show a search box on a page
17 function plugin_search2_convert()
18 {
19         $args = func_get_args();
20         return plugin_search_search_form('', '', $args);
21 }
22
23 function plugin_search2_action()
24 {
25         global $vars, $_title_search, $_title_result, $_msg_searching;
26
27         $action = isset($vars['action']) ? $vars['action'] : '';
28         $base = isset($vars['base']) ? $vars['base'] : '';
29         $start_s = isset($vars['start']) ? $vars['start'] : '';
30         $start_index = pkwk_ctype_digit($start_s) ? intval($start_s) : 0;
31         $bases = array();
32         if ($base !== '') {
33                 $bases[] = $base;
34         }
35         if ($action === '') {
36                 $q = trim(isset($vars['q']) ? $vars['q'] : '');
37                 $offset_s = isset($vars['offset']) ? $vars['offset'] : '';
38                 $offset = pkwk_ctype_digit($offset_s) ? intval($offset_s) : 0;
39                 $prev_offset_s = isset($vars['prev_offset']) ? $vars['prev_offset'] : '';
40                 if ($q === '') {
41                         return array('msg' => $_title_search,
42                                 'body' => "<br>" . $_msg_searching . "\n" .
43                                 plugin_search2_search_form($q, $bases, $offset));
44                 } else {
45                         $msg  = str_replace('$1', htmlsc($q), $_title_result);
46                         return array('msg' => $msg,
47                                 'body' => plugin_search2_search_form($q, $bases, $offset, $prev_offset_s));
48                 }
49         } else if ($action === 'query') {
50                 $q = isset($vars['q']) ? $vars['q'] : '';
51                 $search_start_time = isset($vars['search_start_time']) ?
52                         $vars['search_start_time'] : null;
53                 $modified_since = (int)(isset($vars['modified_since']) ?
54                         $vars['modified_since'] : '0');
55                 header('Content-Type: application/json; charset=UTF-8');
56                 plugin_search2_do_search($q, $base, $start_index,
57                         $search_start_time, $modified_since);
58                 exit;
59         }
60 }
61
62 function plugin_search2_get_base_url($search_text)
63 {
64         global $vars;
65         $params = array();
66         if (!defined('PKWK_UTF8_ENABLE')) {
67                 $params[] = 'encode_hint=' . rawurlencode($vars['encode_hint']);
68         }
69         $params[] = 'cmd=search2';
70         if (isset($vars['encode_hint']) && $vars['encode_hint']) {
71                 $params[] = 'encode_hint=' . rawurlencode($vars['encode_hint']);
72         }
73         if ($search_text) {
74                 $params[] = 'q=' . plugin_search2_urlencode_searchtext($search_text);
75         }
76         if (isset($vars['base']) && $vars['base']) {
77                 $params[] = 'base=' . rawurlencode($vars['base']);
78         }
79         $url = get_base_uri() . '?' . join('&', $params);
80         return $url;
81 }
82
83 function plugin_search2_urlencode_searchtext($search_text)
84 {
85         $s2 = preg_replace('#^\s+|\s+$#', '', $search_text);
86         if (!$s2) return '';
87         $sp = preg_split('#\s+#', $s2);
88         $list = array();
89         for ($i = 0; $i < count($sp); $i++) {
90                 $list[] = rawurlencode($sp[$i]);
91         }
92         return join('+', $list);
93 }
94
95 function plugin_search2_do_search($query_text, $base, $start_index,
96         $search_start_time, $modified_since)
97 {
98         global $whatsnew, $non_list, $search_non_list;
99         global $_msg_andresult, $_msg_orresult;
100         global $search_auth, $auth_user;
101
102         $result_record_limit = $start_index === 0 ?
103                 PLUGIN_SEARCH2_RESULT_RECORD_LIMIT_START : PLUGIN_SEARCH2_RESULT_RECORD_LIMIT;
104         $retval = array();
105
106         $b_type_and = true; // AND:TRUE OR:FALSE
107         $key_candidates = preg_split('/\s+/', $query_text, -1, PREG_SPLIT_NO_EMPTY);
108         for ($i = count($key_candidates) - 2; $i >= 1; $i--) {
109                 if ($key_candidates[$i] === 'OR') {
110                         $b_type_and = false;
111                         unset($key_candidates[$i]);
112                 }
113         }
114         $key_candidates = array_merge($key_candidates);
115         $keys = get_search_words($key_candidates);
116         foreach ($keys as $key=>$value)
117                 $keys[$key] = '/' . $value . '/S';
118
119         if ($modified_since > 0) {
120                 // Recent search
121                 $recent_files = get_recent_files();
122                 $modified_loc = $modified_since - LOCALZONE;
123                 $pages = array();
124                 foreach ($recent_files as $p => $time) {
125                         if ($time >= $modified_loc) {
126                                 $pages[] = $p;
127                         }
128                 }
129                 if ($base != '') {
130                         $pages = preg_grep('/^' . preg_quote($base, '/') . '/S', $pages);
131                 }
132                 $page_names = $pages;
133         } else {
134                 // Normal search
135                 $pages = get_existpages();
136
137                 // Avoid
138                 if ($base != '') {
139                         $pages = preg_grep('/^' . preg_quote($base, '/') . '/S', $pages);
140                 }
141                 if (! $search_non_list) {
142                         $pages = array_diff($pages, preg_grep('/' . $non_list . '/S', $pages));
143                 }
144                 $pages = array_flip($pages);
145                 unset($pages[$whatsnew]);
146                 $page_names = array_keys($pages);
147         }
148         natsort($page_names);
149         // Cache collabolate
150         if (is_null($search_start_time)) {
151                 // Don't use client cache
152                 $search_start_time = UTIME + LOCALZONE;
153         }
154         $found_pages = array();
155         $readable_page_index = -1;
156         $scan_page_index = -1;
157         $saved_scan_start_index = -1;
158         $last_read_page_name = null;
159         foreach ($page_names as $page) {
160                 $b_match = FALSE;
161                 $pagename_only = false;
162                 $scan_page_index++;
163                 if (! is_page_readable($page)) {
164                         if ($search_auth) {
165                                 // $search_auth - 1: User can know page names that contain search text if the page is readable
166                                 continue;
167                         }
168                         // $search_auth - 0: All users can know page names that conntain search text
169                         $pagename_only = true;
170                 }
171                 $readable_page_index++;
172                 if ($readable_page_index < $start_index) {
173                         // Skip: It's not time to read
174                         continue;
175                 }
176                 if ($saved_scan_start_index === -1) {
177                         $saved_scan_start_index = $scan_page_index;
178                 }
179                 if (count($keys) > 0) {
180                         // Search for page name and contents
181                         $body = get_source($page, TRUE, TRUE, TRUE);
182                         $target = $page . "\n" . remove_author_header($body);
183                         foreach ($keys as $key) {
184                                 $b_match = preg_match($key, $target);
185                                 if ($b_type_and xor $b_match) break; // OR
186                         }
187                 } else {
188                         // No search target. get_source($page) is meaningless.
189                         // $b_match is always false.
190                 }
191                 if ($b_match) {
192                         // Found!
193                         $author_info = get_author_info($body);
194                         if ($author_info) {
195                                 $updated_at = get_update_datetime_from_author($author_info);
196                                 $updated_time = strtotime($updated_at);
197                         } else {
198                                 $updated_time = filemtime(get_filename($page));
199                                 $updated_at = get_date_atom($updated_time);
200                         }
201                         if ($pagename_only) {
202                                 // The user cannot read this page body
203                                 $found_pages[] = array('name' => (string)$page,
204                                         'url' => get_page_uri($page), 'updated_at' => $updated_at,
205                                         'updated_time' => $updated_time,
206                                         'body' => '', 'pagename_only' => 1);
207                         } else {
208                                 $found_pages[] = array('name' => (string)$page,
209                                         'url' => get_page_uri($page), 'updated_at' => $updated_at,
210                                         'updated_time' => $updated_time,
211                                         'body' => (string)$body);
212                         }
213                 }
214                 $last_read_page_name = $page;
215                 if ($start_index + $result_record_limit <= $readable_page_index + 1) {
216                         // Read page limit
217                         break;
218                 }
219         }
220         $message = str_replace('$1', htmlsc($query_text), str_replace('$2', count($found_pages),
221                 str_replace('$3', count($page_names), $b_type_and ? $_msg_andresult : $_msg_orresult)));
222         $search_done = (boolean)($scan_page_index + 1 === count($page_names));
223         $result_obj = array(
224                 'message' => $message,
225                 'q' => $query_text,
226                 'start_index' => $start_index,
227                 'limit' => $result_record_limit,
228                 'read_page_count' => $readable_page_index - $start_index + 1,
229                 'scan_page_count' => $scan_page_index - $saved_scan_start_index + 1,
230                 'page_count' => count($page_names),
231                 'last_read_page_name' => $last_read_page_name,
232                 'next_start_index' => $readable_page_index + 1,
233                 'search_done' => $search_done,
234                 'search_start_time' => $search_start_time,
235                 'auth_user' => $auth_user,
236                 'results' => $found_pages);
237         $obj = $result_obj;
238         if (!defined('PKWK_UTF8_ENABLE')) {
239                 if (SOURCE_ENCODING === 'EUC-JP') {
240                         mb_convert_variables('UTF-8', 'CP51932', $obj);
241                 } else {
242                         mb_convert_variables('UTF-8', SOURCE_ENCODING, $obj);
243                 }
244         }
245         print(json_encode($obj, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES));
246 }
247
248 function plugin_search2_search_form($search_text = '', $bases = array(),
249         $offset, $prev_offset_s = null)
250 {
251         global $_btn_search;
252         global $_search_pages, $_search_all;
253         global $_msg_andresult, $_msg_orresult, $_msg_notfoundresult;
254         global $_search_detail, $_search_searching, $_search_showing_result;
255         global $_msg_unsupported_webbrowser, $_msg_use_alternative_link;
256         global $_msg_more_results, $_msg_prev_results, $_msg_general_error;
257         global $auth_user;
258
259         static $search2_form_total_count = 0;
260         $search2_form_total_count++;
261         $script = get_base_uri();
262         $h_search_text = htmlsc($search_text);
263
264         $base_option = '';
265         if (!empty($bases)) {
266                 $base_msg = '';
267                 $_num = 0;
268                 $check = ' checked';
269                 foreach($bases as $base) {
270                         ++$_num;
271                         if (PLUGIN_SEARCH2_MAX_BASE < $_num) break;
272                         $s_base   = htmlsc($base);
273                         $base_str = '<strong>' . $s_base . '</strong>';
274                         $base_label = str_replace('$1', $base_str, $_search_pages);
275                         $base_msg  .=<<<EOD
276  <div>
277   <label>
278    <input type="radio" name="base" value="$s_base" $check> $base_label
279   </label>
280  </div>
281 EOD;
282                         $check = '';
283                 }
284                 $base_msg .=<<<EOD
285 <label><input type="radio" name="base" value=""> $_search_all</label>
286 EOD;
287                 $base_option = '<div class="small">' . $base_msg . '</div>';
288         }
289         $_search2_result_notfound = htmlsc($_msg_notfoundresult);
290         $_search2_result_found = htmlsc($_msg_andresult);
291         $_search2_search_wait_milliseconds = PLUGIN_SEARCH2_SEARCH_WAIT_MILLISECONDS;
292         $result_page_panel =<<<EOD
293 <input type="checkbox" id="_plugin_search2_detail" checked><label for="_plugin_search2_detail">$_search_detail</label>
294 <ul id="_plugin_search2_result-list">
295 </ul>
296 EOD;
297         if ($h_search_text == '' || $search2_form_total_count > 1) {
298                 $result_page_panel = '';
299         }
300
301         $plain_search_link = '<a href="' . $script . '?cmd=search' . '">' . htmlsc($_btn_search) . '</a>';
302         $alt_msg = str_replace('$1', $plain_search_link, $_msg_use_alternative_link);
303         $form =<<<EOD
304 <form action="$script" method="GET" class="_plugin_search2_form">
305  <div>
306   <input type="hidden" name="cmd" value="search2">
307   <input type="search" name="q" value="$h_search_text" data-original-q="$h_search_text" size="40">
308   <input type="submit" value="$_btn_search">
309  </div>
310 $base_option
311 </form>
312 EOD;
313         $second_form =<<<EOD
314 <div class="_plugin_search2_second_form" style="display:none;">
315 <div class="_plugin_search2_search_status"></div>
316 <div class="_plugin_search2_message"></div>
317 $form
318 </div>
319 EOD;
320
321         $h_auth_user = htmlsc($auth_user);
322         $h_base_url = htmlsc(plugin_search2_get_base_url($search_text));
323         $h_msg_more_results = htmlsc($_msg_more_results);
324         $h_msg_prev_results = htmlsc($_msg_prev_results);
325         $max_results = PLUGIN_SEARCH2_SEARCH_MAX_RESULTS;
326         $prev_offset = pkwk_ctype_digit($prev_offset_s) ? $prev_offset_s : '';
327         $search_props =<<<EOD
328 <div style="display:none;">
329   <input type="hidden" id="_plugin_search2_auth_user" value="$h_auth_user">
330   <input type="hidden" id="_plugin_search2_base_url" value="$h_base_url">
331   <input type="hidden" id="_plugin_search2_msg_searching" value="$_search_searching">
332   <input type="hidden" id="_plugin_search2_msg_showing_result" value="$_search_showing_result">
333   <input type="hidden" id="_plugin_search2_msg_result_notfound" value="$_search2_result_notfound">
334   <input type="hidden" id="_plugin_search2_msg_result_found" value="$_search2_result_found">
335   <input type="hidden" id="_plugin_search2_msg_more_results" value="$h_msg_more_results">
336   <input type="hidden" id="_plugin_search2_msg_prev_results" value="$h_msg_prev_results">
337   <input type="hidden" id="_plugin_search2_search_wait_milliseconds" value="$_search2_search_wait_milliseconds">
338   <input type="hidden" id="_plugin_search2_max_results" value="$max_results">
339   <input type="hidden" id="_plugin_search2_offset" value="$offset">
340   <input type="hidden" id="_plugin_search2_prev_offset" value="$prev_offset">
341   <input type="hidden" id="_plugin_search2_msg_error" value="$_msg_general_error">
342 </div>
343 EOD;
344         if ($search2_form_total_count > 1) {
345                 $search_props = '';
346         }
347
348         return <<<EOD
349 <noscript>
350  <p>$_msg_unsupported_webbrowser $alt_msg</p>
351 </noscript>
352 <p class="_plugin_search2_nosupport_message" style="display:none;">
353   $_msg_unsupported_webbrowser $alt_msg
354 </p>
355 $search_props
356 $form
357 <div class="_plugin_search2_search_status"></div>
358 <div class="_plugin_search2_message"></div>
359 $result_page_panel
360 $second_form
361 EOD;
362 }