Revisão | 7e5e27a58101507ecc3e43c2fb7bf76a0d8ae3d1 (tree) |
---|---|
Hora | 2017-09-30 15:34:42 |
Autor | umorigu <umorigu@gmai...> |
Commiter | umorigu |
BugTrack/2433 Search Progress and Cancel with search2 plugin
@@ -9,6 +9,10 @@ | ||
9 | 9 | define('PLUGIN_SEARCH2_MAX_LENGTH', 80); |
10 | 10 | define('PLUGIN_SEARCH2_MAX_BASE', 16); // #search(1,2,3,...,15,16) |
11 | 11 | |
12 | +define('PLUGIN_SEARCH2_RESULT_RECORD_LIMIT', 500); | |
13 | +define('PLUGIN_SEARCH2_RESULT_RECORD_LIMIT_START', 100); | |
14 | +define('PLUGIN_SEARCH2_SEARCH_WAIT_MILLISECONDS', 1000); | |
15 | + | |
12 | 16 | // Show a search box on a page |
13 | 17 | function plugin_search2_convert() |
14 | 18 | { |
@@ -22,6 +26,8 @@ function plugin_search2_action() | ||
22 | 26 | |
23 | 27 | $action = isset($vars['action']) ? $vars['action'] : ''; |
24 | 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; | |
25 | 31 | $bases = array(); |
26 | 32 | if ($base !== '') { |
27 | 33 | $bases[] = $base; |
@@ -39,17 +45,19 @@ function plugin_search2_action() | ||
39 | 45 | } else if ($action === 'query') { |
40 | 46 | $text = isset($vars['q']) ? $vars['q'] : ''; |
41 | 47 | header('Content-Type: application/json; charset=UTF-8'); |
42 | - plugin_search2_do_search($text, $base); | |
48 | + plugin_search2_do_search($text, $base, $start_index); | |
43 | 49 | exit; |
44 | 50 | } |
45 | 51 | } |
46 | 52 | |
47 | -function plugin_search2_do_search($query_text, $base) | |
53 | +function plugin_search2_do_search($query_text, $base, $start_index) | |
48 | 54 | { |
49 | 55 | global $whatsnew, $non_list, $search_non_list; |
50 | 56 | global $_msg_andresult, $_msg_orresult, $_msg_notfoundresult; |
51 | 57 | global $search_auth; |
52 | 58 | |
59 | + $result_record_limit = $start_index === 0 ? | |
60 | + PLUGIN_SEARCH2_RESULT_RECORD_LIMIT_START : PLUGIN_SEARCH2_RESULT_RECORD_LIMIT; | |
53 | 61 | $retval = array(); |
54 | 62 | |
55 | 63 | $b_type_and = true; // AND:TRUE OR:FALSE |
@@ -82,6 +90,7 @@ function plugin_search2_do_search($query_text, $base) | ||
82 | 90 | $found_pages = array(); |
83 | 91 | $readable_page_index = -1; |
84 | 92 | $scan_page_index = -1; |
93 | + $saved_scan_start_index = -1; | |
85 | 94 | $last_read_page_name = null; |
86 | 95 | foreach ($page_names as $page) { |
87 | 96 | $b_match = FALSE; |
@@ -96,6 +105,13 @@ function plugin_search2_do_search($query_text, $base) | ||
96 | 105 | $pagename_only = true; |
97 | 106 | } |
98 | 107 | $readable_page_index++; |
108 | + if ($readable_page_index < $start_index) { | |
109 | + // Skip: It's not time to read | |
110 | + continue; | |
111 | + } | |
112 | + if ($saved_scan_start_index === -1) { | |
113 | + $saved_scan_start_index = $scan_page_index; | |
114 | + } | |
99 | 115 | // Search for page name and contents |
100 | 116 | $body = get_source($page, TRUE, TRUE, TRUE); |
101 | 117 | $target = $page . "\n" . remove_author_header($body); |
@@ -122,6 +138,10 @@ function plugin_search2_do_search($query_text, $base) | ||
122 | 138 | } |
123 | 139 | } |
124 | 140 | $last_read_page_name = $page; |
141 | + if ($start_index + $result_record_limit <= $readable_page_index + 1) { | |
142 | + // Read page limit | |
143 | + break; | |
144 | + } | |
125 | 145 | } |
126 | 146 | $message = str_replace('$1', htmlsc($query_text), str_replace('$2', count($found_pages), |
127 | 147 | str_replace('$3', count($page_names), $b_type_and ? $_msg_andresult : $_msg_orresult))); |
@@ -129,10 +149,13 @@ function plugin_search2_do_search($query_text, $base) | ||
129 | 149 | $result_obj = array( |
130 | 150 | 'message' => $message, |
131 | 151 | 'q' => $query_text, |
132 | - 'read_page_count' => $readable_page_index + 1, | |
133 | - 'scan_page_count' => $scan_page_index + 1, | |
152 | + 'start_index' => $start_index, | |
153 | + 'limit' => $result_record_limit, | |
154 | + 'read_page_count' => $readable_page_index - $start_index + 1, | |
155 | + 'scan_page_count' => $scan_page_index - $saved_scan_start_index + 1, | |
134 | 156 | 'page_count' => count($page_names), |
135 | 157 | 'last_read_page_name' => $last_read_page_name, |
158 | + 'next_start_index' => $readable_page_index + 1, | |
136 | 159 | 'search_done' => $search_done, |
137 | 160 | 'results' => $found_pages); |
138 | 161 | $obj = $result_obj; |
@@ -186,8 +209,6 @@ EOD; | ||
186 | 209 | $_search2_result_found = htmlsc($_msg_andresult); |
187 | 210 | $_search2_search_wait_milliseconds = PLUGIN_SEARCH2_SEARCH_WAIT_MILLISECONDS; |
188 | 211 | $result_page_panel =<<<EOD |
189 | -<div id="_plugin_search2_search_status"></div> | |
190 | -<div id="_plugin_search2_message"></div> | |
191 | 212 | <input type="checkbox" id="_plugin_search2_detail" checked><label for="_plugin_search2_detail">$_search_detail</label> |
192 | 213 | <input type="hidden" id="_plugin_search2_msg_searching" value="$_search_searching"> |
193 | 214 | <input type="hidden" id="_plugin_search2_msg_result_notfound" value="$_search2_result_notfound"> |
@@ -200,13 +221,7 @@ EOD; | ||
200 | 221 | |
201 | 222 | $plain_search_link = '<a href="' . $script . '?cmd=search' . '">' . htmlsc($_btn_search) . '</a>'; |
202 | 223 | $alt_msg = str_replace('$1', $plain_search_link, $_msg_use_alternative_link); |
203 | - return <<<EOD | |
204 | -<noscript> | |
205 | - <p>$_msg_unsupported_webbrowser $alt_msg</p> | |
206 | -</noscript> | |
207 | -<p class="_plugin_search2_nosupport_message" style="display:none;"> | |
208 | - $_msg_unsupported_webbrowser $alt_msg | |
209 | -</p> | |
224 | + $form =<<<EOD | |
210 | 225 | <form action="$script" method="GET" class="_plugin_search2_form"> |
211 | 226 | <div> |
212 | 227 | <input type="hidden" name="cmd" value="search2"> |
@@ -215,8 +230,29 @@ EOD; | ||
215 | 230 | </div> |
216 | 231 | $base_option |
217 | 232 | </form> |
233 | +EOD; | |
234 | + $second_form =<<<EOD | |
235 | +<div class="_plugin_search2_second_form" style="display:none;"> | |
236 | +<div class="_plugin_search2_search_status"></div> | |
237 | +<div class="_plugin_search2_message"></div> | |
238 | +$form | |
239 | +</div> | |
240 | +EOD; | |
241 | + | |
242 | + | |
243 | + return <<<EOD | |
244 | +<noscript> | |
245 | + <p>$_msg_unsupported_webbrowser $alt_msg</p> | |
246 | +</noscript> | |
247 | +<p class="_plugin_search2_nosupport_message" style="display:none;"> | |
248 | + $_msg_unsupported_webbrowser $alt_msg | |
249 | +</p> | |
250 | +$form | |
251 | +<div class="_plugin_search2_search_status"></div> | |
252 | +<div class="_plugin_search2_message"></div> | |
218 | 253 | $result_page_panel |
219 | 254 | <ul id="result-list"> |
220 | 255 | </ul> |
256 | +$second_form | |
221 | 257 | EOD; |
222 | 258 | } |
@@ -93,15 +93,23 @@ window.addEventListener && window.addEventListener('DOMContentLoaded', function( | ||
93 | 93 | var searchRegex = textToRegex(removeSearchOperators(searchText)); |
94 | 94 | var ul = document.querySelector('#result-list'); |
95 | 95 | if (!ul) return; |
96 | - ul.innerHTML = ''; | |
96 | + if (obj.start_index === 0) { | |
97 | + ul.innerHTML = ''; | |
98 | + } | |
97 | 99 | if (! session.scan_page_count) session.scan_page_count = 0; |
98 | 100 | if (! session.read_page_count) session.read_page_count = 0; |
99 | 101 | if (! session.hit_page_count) session.hit_page_count = 0; |
102 | + var prevHitPageCount = session.hit_page_count; | |
100 | 103 | session.scan_page_count += obj.scan_page_count; |
101 | 104 | session.read_page_count += obj.read_page_count; |
102 | 105 | session.hit_page_count += obj.results.length; |
103 | 106 | session.page_count = obj.page_count; |
104 | - | |
107 | + if (prevHitPageCount === 0 && session.hit_page_count > 0) { | |
108 | + var div = document.querySelector('._plugin_search2_second_form'); | |
109 | + if (div) { | |
110 | + div.style.display = 'block'; | |
111 | + } | |
112 | + } | |
105 | 113 | var msg = obj.message; |
106 | 114 | var notFoundMessageTemplate = getMessageTemplate('_plugin_search2_msg_result_notfound', |
107 | 115 | 'No page which contains $1 has been found.'); |
@@ -110,7 +118,7 @@ window.addEventListener && window.addEventListener('DOMContentLoaded', function( | ||
110 | 118 | var searchTextDecorated = findAndDecorateText(searchText, searchRegex); |
111 | 119 | if (searchTextDecorated === null) searchTextDecorated = escapeHTML(searchText); |
112 | 120 | var messageTemplate = foundMessageTemplate; |
113 | - if (session.hit_page_count === 0) { | |
121 | + if (obj.search_done && session.hit_page_count === 0) { | |
114 | 122 | messageTemplate = notFoundMessageTemplate; |
115 | 123 | } |
116 | 124 | msg = messageTemplate.replace(/\$1|\$2|\$3/g, function(m){ |
@@ -120,9 +128,17 @@ window.addEventListener && window.addEventListener('DOMContentLoaded', function( | ||
120 | 128 | '$3': session.read_page_count |
121 | 129 | }[m]; |
122 | 130 | }); |
123 | - document.querySelector('#_plugin_search2_message').innerHTML = msg; | |
124 | - | |
125 | - setSearchStatus(''); | |
131 | + setSearchMessage(msg); | |
132 | + var progress = ' (read:' + session.read_page_count + ', scanned:' + | |
133 | + session.scan_page_count + ', all:' + session.page_count + ')'; | |
134 | + var e = document.querySelector('#_plugin_search2_msg_searching'); | |
135 | + var msg = e && e.value || 'Searching...'; | |
136 | + setSearchStatus(msg + progress); | |
137 | + if (obj.search_done) { | |
138 | + setTimeout(function(){ | |
139 | + setSearchStatus(''); | |
140 | + }, 5000); | |
141 | + } | |
126 | 142 | var results = obj.results; |
127 | 143 | var now = new Date(); |
128 | 144 | results.forEach(function(val, index) { |
@@ -167,6 +183,21 @@ window.addEventListener && window.addEventListener('DOMContentLoaded', function( | ||
167 | 183 | fragment.appendChild(div); |
168 | 184 | ul.appendChild(fragment); |
169 | 185 | }); |
186 | + if (!obj.search_done && obj.next_start_index) { | |
187 | + var waitE = document.querySelector('#_search2_search_wait_milliseconds'); | |
188 | + var interval = minSearchWaitMilliseconds; | |
189 | + try { | |
190 | + interval = parseInt(waitE.value); | |
191 | + } catch (e) { | |
192 | + interval = minSearchWaitMilliseconds; | |
193 | + } | |
194 | + if (interval < minSearchWaitMilliseconds) { | |
195 | + interval = minSearchWaitMilliseconds; | |
196 | + } | |
197 | + setTimeout(function(){ | |
198 | + doSearch(searchText, session, obj.next_start_index); | |
199 | + }, interval); | |
200 | + } | |
170 | 201 | } |
171 | 202 | function prepareKanaMap() { |
172 | 203 | if (kanaMap !== null) return; |
@@ -396,10 +427,16 @@ window.addEventListener && window.addEventListener('DOMContentLoaded', function( | ||
396 | 427 | } |
397 | 428 | } |
398 | 429 | function setSearchStatus(statusText) { |
399 | - var statusObj = document.querySelector('#_plugin_search2_search_status'); | |
400 | - if (statusObj) { | |
430 | + var statusList = document.querySelectorAll('._plugin_search2_search_status'); | |
431 | + forEach(statusList, function(statusObj){ | |
401 | 432 | statusObj.textContent = statusText; |
402 | - } | |
433 | + }); | |
434 | + } | |
435 | + function setSearchMessage(msgHTML) { | |
436 | + var objList = document.querySelectorAll('._plugin_search2_message'); | |
437 | + forEach(objList, function(obj){ | |
438 | + obj.innerHTML = msgHTML; | |
439 | + }); | |
403 | 440 | } |
404 | 441 | function forEach(nodeList, func) { |
405 | 442 | if (nodeList.forEach) { |