2 // PukiWiki - Yet another WikiWikiWeb clone.
4 // Copyright 2017 PukiWiki Development Team
5 // License: GPL v2 or (at your option) any later version
7 // Search2 plugin - Show detail result using JavaScript
9 define('PLUGIN_SEARCH2_MAX_BASE', 16); // #search(1,2,3,...,15,16)
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);
16 // Show a search box on a page
17 function plugin_search2_convert()
19 $args = func_get_args();
20 return plugin_search_search_form('', '', $args);
23 function plugin_search2_action()
25 global $vars, $_title_search, $_title_result, $_msg_searching;
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;
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'] : '';
41 return array('msg' => $_title_search,
42 'body' => "<br>" . $_msg_searching . "\n" .
43 plugin_search2_search_form($q, $bases, $offset));
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));
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);
62 function plugin_search2_get_base_url($search_text)
66 if (!defined('PKWK_UTF8_ENABLE')) {
67 $params[] = 'encode_hint=' . rawurlencode($vars['encode_hint']);
69 $params[] = 'cmd=search2';
70 if (isset($vars['encode_hint']) && $vars['encode_hint']) {
71 $params[] = 'encode_hint=' . rawurlencode($vars['encode_hint']);
74 $params[] = 'q=' . plugin_search2_urlencode_searchtext($search_text);
76 if (isset($vars['base']) && $vars['base']) {
77 $params[] = 'base=' . rawurlencode($vars['base']);
79 $url = get_base_uri() . '?' . join('&', $params);
83 function plugin_search2_urlencode_searchtext($search_text)
85 $s2 = preg_replace('#^\s+|\s+$#', '', $search_text);
87 $sp = preg_split('#\s+#', $s2);
89 for ($i = 0; $i < count($sp); $i++) {
90 $list[] = rawurlencode($sp[$i]);
92 return join('+', $list);
95 function plugin_search2_do_search($query_text, $base, $start_index,
96 $search_start_time, $modified_since)
98 global $whatsnew, $non_list, $search_non_list;
99 global $_msg_andresult, $_msg_orresult;
100 global $search_auth, $auth_user;
102 $result_record_limit = $start_index === 0 ?
103 PLUGIN_SEARCH2_RESULT_RECORD_LIMIT_START : PLUGIN_SEARCH2_RESULT_RECORD_LIMIT;
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') {
111 unset($key_candidates[$i]);
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';
119 if ($modified_since > 0) {
121 $recent_files = get_recent_files();
122 $modified_loc = $modified_since - LOCALZONE;
124 foreach ($recent_files as $p => $time) {
125 if ($time >= $modified_loc) {
130 $pages = preg_grep('/^' . preg_quote($base, '/') . '/S', $pages);
132 $page_names = $pages;
135 $pages = get_existpages();
139 $pages = preg_grep('/^' . preg_quote($base, '/') . '/S', $pages);
141 if (! $search_non_list) {
142 $pages = array_diff($pages, preg_grep('/' . $non_list . '/S', $pages));
144 $pages = array_flip($pages);
145 unset($pages[$whatsnew]);
146 $page_names = array_keys($pages);
148 natsort($page_names);
150 if (is_null($search_start_time)) {
151 // Don't use client cache
152 $search_start_time = UTIME + LOCALZONE;
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) {
161 $pagename_only = false;
163 if (! is_page_readable($page)) {
165 // $search_auth - 1: User can know page names that contain search text if the page is readable
168 // $search_auth - 0: All users can know page names that conntain search text
169 $pagename_only = true;
171 $readable_page_index++;
172 if ($readable_page_index < $start_index) {
173 // Skip: It's not time to read
176 if ($saved_scan_start_index === -1) {
177 $saved_scan_start_index = $scan_page_index;
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
188 // No search target. get_source($page) is meaningless.
189 // $b_match is always false.
193 $author_info = get_author_info($body);
195 $updated_at = get_update_datetime_from_author($author_info);
196 $updated_time = strtotime($updated_at);
198 $updated_time = filemtime(get_filename($page));
199 $updated_at = get_date_atom($updated_time);
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);
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);
214 $last_read_page_name = $page;
215 if ($start_index + $result_record_limit <= $readable_page_index + 1) {
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));
224 'message' => $message,
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);
238 if (!defined('PKWK_UTF8_ENABLE')) {
239 if (SOURCE_ENCODING === 'EUC-JP') {
240 mb_convert_variables('UTF-8', 'CP51932', $obj);
242 mb_convert_variables('UTF-8', SOURCE_ENCODING, $obj);
245 print(json_encode($obj, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES));
248 function plugin_search2_search_form($search_text = '', $bases = array(),
249 $offset, $prev_offset_s = null)
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;
259 static $search2_form_total_count = 0;
260 $search2_form_total_count++;
261 $script = get_base_uri();
262 $h_search_text = htmlsc($search_text);
265 if (!empty($bases)) {
269 foreach($bases as $base) {
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);
278 <input type="radio" name="base" value="$s_base" $check> $base_label
285 <label><input type="radio" name="base" value=""> $_search_all</label>
287 $base_option = '<div class="small">' . $base_msg . '</div>';
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">
297 if ($h_search_text == '' || $search2_form_total_count > 1) {
298 $result_page_panel = '';
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);
304 <form action="$script" method="GET" class="_plugin_search2_form">
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">
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>
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">
344 if ($search2_form_total_count > 1) {
350 <p>$_msg_unsupported_webbrowser $alt_msg</p>
352 <p class="_plugin_search2_nosupport_message" style="display:none;">
353 $_msg_unsupported_webbrowser $alt_msg
357 <div class="_plugin_search2_search_status"></div>
358 <div class="_plugin_search2_message"></div>