Revisão | 34a4ca0b0d69995f903e808936abceeb49ec1711 (tree) |
---|---|
Hora | 2017-06-11 21:47:12 |
Autor | umorigu <umorigu@gmai...> |
Commiter | umorigu |
BugTrack/2422 popular plugin supports Dababase counter
@@ -203,6 +203,108 @@ function plugin_counter_get_count($page) | ||
203 | 203 | return $c; |
204 | 204 | } |
205 | 205 | |
206 | +function plugin_counter_get_popular_list($today, $except, $max) { | |
207 | + if (PLUGIN_COUNTER_USE_DB === 0) { | |
208 | + return plugin_counter_get_popular_list_file($today, $except, $max); | |
209 | + } else { | |
210 | + return plugin_counter_get_popular_list_db($today, $except, $max); | |
211 | + } | |
212 | +} | |
213 | + | |
214 | +function plugin_counter_get_popular_list_file($today, $except, $max) { | |
215 | + global $whatsnew; | |
216 | + $counters = array(); | |
217 | + $except_quote = str_replace('#', '\#', $except); | |
218 | + foreach (get_existpages(COUNTER_DIR, '.count') as $file=>$page) { | |
219 | + if (($except != '' && preg_match("#$except_quote#", $page)) || | |
220 | + $page == $whatsnew || check_non_list($page) || | |
221 | + ! is_page($page)) | |
222 | + continue; | |
223 | + | |
224 | + $array = file(COUNTER_DIR . $file); | |
225 | + $count = rtrim($array[0]); | |
226 | + $date = rtrim($array[1]); | |
227 | + $today_count = rtrim($array[2]); | |
228 | + | |
229 | + if ($today) { | |
230 | + // $pageが数値に見える(たとえばencode('BBS')=424253)とき、 | |
231 | + // array_splice()によってキー値が変更されてしまうのを防ぐ | |
232 | + // ため、キーに '_' を連結する | |
233 | + if ($today == $date) $counters['_' . $page] = $today_count; | |
234 | + } else { | |
235 | + $counters['_' . $page] = $count; | |
236 | + } | |
237 | + } | |
238 | + | |
239 | + asort($counters, SORT_NUMERIC); | |
240 | + | |
241 | + // BugTrack2/106: Only variables can be passed by reference from PHP 5.0.5 | |
242 | + $counters = array_reverse($counters, TRUE); // with array_splice() | |
243 | + $counters = array_splice($counters, 0, $max); | |
244 | + return $counters; | |
245 | +} | |
246 | + | |
247 | +function plugin_counter_get_popular_list_db($today, $except, $max) { | |
248 | + global $whatsnew; | |
249 | + $page_counter_t = PLUGIN_COUNTER_DB_TABLE_NAME_PREFIX . 'page_counter'; | |
250 | + if ($today) { | |
251 | + $order_by_c = 'today_viewcount'; | |
252 | + } else { | |
253 | + $order_by_c = 'total'; | |
254 | + } | |
255 | + $counters = array(); | |
256 | + try { | |
257 | + $pdo = new PDO(PLUGIN_COUNTER_DB_CONNECT_STRING, | |
258 | + PLUGIN_COUNTER_DB_USERNAME, PLUGIN_COUNTER_DB_PASSWORD, | |
259 | + $plugin_counter_db_options); | |
260 | + $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); | |
261 | + $pdo->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_ASSOC); | |
262 | + $pdo->setAttribute(PDO::ATTR_TIMEOUT, 5); | |
263 | + if ($today) { | |
264 | + $stmt = $pdo->prepare( | |
265 | +"SELECT page_name, total, update_date, | |
266 | + today_viewcount, yesterday_viewcount | |
267 | + FROM $page_counter_t | |
268 | + WHERE update_date = ? | |
269 | + ORDER BY $order_by_c DESC | |
270 | + LIMIT ?" | |
271 | + ); | |
272 | + } else { | |
273 | + $stmt = $pdo->prepare( | |
274 | +"SELECT page_name, total, update_date, | |
275 | + today_viewcount, yesterday_viewcount | |
276 | + FROM $page_counter_t | |
277 | + ORDER BY $order_by_c DESC | |
278 | + LIMIT ?" | |
279 | + ); | |
280 | + } | |
281 | + $except_quote = str_replace('#', '\#', $except); | |
282 | + $limit = $max + 100; | |
283 | + if ($today) { | |
284 | + $stmt->execute(array($today, $limit)); | |
285 | + } else { | |
286 | + $stmt->execute(array($limit)); | |
287 | + } | |
288 | + foreach ($stmt as $r) { | |
289 | + $page = $r['page_name']; | |
290 | + if (($except != '' && preg_match("#$except_quote#", $page)) || | |
291 | + $page == $whatsnew || check_non_list($page) || | |
292 | + ! is_page($page)) { | |
293 | + continue; | |
294 | + } | |
295 | + if ($today) { | |
296 | + $counters['_' . $page] = $r['today_viewcount']; | |
297 | + } else { | |
298 | + $counters['_' . $page] = $r['total']; | |
299 | + } | |
300 | + } | |
301 | + $stmt->closeCursor(); | |
302 | + return array_splice($counters, 0, $max); | |
303 | + } catch (Exception $e) { | |
304 | + die('counter.inc.php: Error occurred on getting pupular pages'); | |
305 | + } | |
306 | +} | |
307 | + | |
206 | 308 | /** |
207 | 309 | * php -r "include 'plugin/counter.inc.php'; plugin_counter_tool_setup_table();" |
208 | 310 | */ |
@@ -1,17 +1,15 @@ | ||
1 | 1 | <?php |
2 | 2 | // PukiWiki - Yet another WikiWikiWeb clone |
3 | -// $Id: popular.inc.php,v 1.20 2011/01/25 15:01:01 henoheno Exp $ | |
4 | -// Copyright (C) | |
5 | -// 2003-2005, 2007 PukiWiki Developers Team | |
3 | +// popular.inc.php | |
4 | +// Copyright | |
5 | +// 2003-2017 PukiWiki Development Team | |
6 | 6 | // 2002 Kazunori Mizushima <kazunori@uc.netyou.jp> |
7 | 7 | // License: WHERE IS THE RECORD? |
8 | 8 | // |
9 | 9 | // Popular pages plugin: Show an access ranking of this wiki |
10 | 10 | // -- like recent plugin, using counter plugin's count -- |
11 | 11 | |
12 | -/* | |
13 | - * (C) 2003-2005 PukiWiki Developers Team | |
14 | - * (C) 2002 Kazunori Mizushima <kazunori@uc.netyou.jp> | |
12 | +/** | |
15 | 13 | * |
16 | 14 | * 通算および今日に別けて一覧を作ることができます。 |
17 | 15 | * |
@@ -29,9 +27,11 @@ | ||
29 | 27 | |
30 | 28 | define('PLUGIN_POPULAR_DEFAULT', 10); |
31 | 29 | |
30 | +require_once(PLUGIN_DIR . 'counter.inc.php'); | |
31 | + | |
32 | 32 | function plugin_popular_convert() |
33 | 33 | { |
34 | - global $vars, $whatsnew; | |
34 | + global $vars; | |
35 | 35 | global $_popular_plugin_frame, $_popular_plugin_today_frame; |
36 | 36 | |
37 | 37 | $max = PLUGIN_POPULAR_DEFAULT; |
@@ -42,37 +42,9 @@ function plugin_popular_convert() | ||
42 | 42 | switch (func_num_args()) { |
43 | 43 | case 3: if ($array[2]) $today = get_date('Y/m/d'); |
44 | 44 | case 2: $except = $array[1]; |
45 | - case 1: $max = $array[0]; | |
45 | + case 1: $max = (int)$array[0]; | |
46 | 46 | } |
47 | - | |
48 | - $counters = array(); | |
49 | - $except_quote = str_replace('#', '\#', $except); | |
50 | - foreach (get_existpages(COUNTER_DIR, '.count') as $file=>$page) { | |
51 | - if (($except != '' && preg_match("#$except_quote#", $page)) || | |
52 | - $page == $whatsnew || check_non_list($page) || | |
53 | - ! is_page($page)) | |
54 | - continue; | |
55 | - | |
56 | - $array = file(COUNTER_DIR . $file); | |
57 | - $count = rtrim($array[0]); | |
58 | - $date = rtrim($array[1]); | |
59 | - $today_count = rtrim($array[2]); | |
60 | - | |
61 | - if ($today) { | |
62 | - // $pageが数値に見える(たとえばencode('BBS')=424253)とき、 | |
63 | - // array_splice()によってキー値が変更されてしまうのを防ぐ | |
64 | - // ため、キーに '_' を連結する | |
65 | - if ($today == $date) $counters['_' . $page] = $today_count; | |
66 | - } else { | |
67 | - $counters['_' . $page] = $count; | |
68 | - } | |
69 | - } | |
70 | - | |
71 | - asort($counters, SORT_NUMERIC); | |
72 | - | |
73 | - // BugTrack2/106: Only variables can be passed by reference from PHP 5.0.5 | |
74 | - $counters = array_reverse($counters, TRUE); // with array_splice() | |
75 | - $counters = array_splice($counters, 0, $max); | |
47 | + $counters = plugin_counter_get_popular_list($today, $except, $max); | |
76 | 48 | |
77 | 49 | $items = ''; |
78 | 50 | if (! empty($counters)) { |
@@ -99,4 +71,3 @@ function plugin_popular_convert() | ||
99 | 71 | |
100 | 72 | return sprintf($today ? $_popular_plugin_today_frame : $_popular_plugin_frame, count($counters), $items); |
101 | 73 | } |
102 | - |