1<?php
2/**
3 * Access Counter and Popularity Plugin -- Popularity Lists
4 *
5 * Original source of this plugin is PukiWiki.
6 *
7 * @license GPL 2 (http://www.gnu.org/licenses/gpl.html)
8 * @author  HokkaidoPerson <dosankomali@yahoo.co.jp>
9 */
10
11// Original Licenses of this plugin:
12//
13// PukiWiki - Yet another WikiWikiWeb clone
14// $Id: popular.inc.php,v 1.20 2011/01/25 15:01:01 henoheno Exp $
15// Copyright (C)
16//   2003-2005, 2007 PukiWiki Developers Team
17//   2002 Kazunori Mizushima <kazunori@uc.netyou.jp>
18// License: WHERE IS THE RECORD?
19//
20// Popular pages plugin: Show an access ranking of this wiki
21// -- like recent plugin, using counter plugin's count --
22
23// must be run within Dokuwiki
24if(!defined('DOKU_INC')) die();
25
26class syntax_plugin_accscounter_popularity extends DokuWiki_Syntax_Plugin {
27
28    function getType(){
29        return 'substition';
30    }
31
32    function getSort(){
33        return 300; // after accscounter_counter
34    }
35
36    //Syntax: {{POPULAR (divide by a space) the number of items (10 in default) (divide by a space) allperiod(default), today, or yesterday (divide by a space) blacklist 1|blacklist 2|(divide by "|")...}}
37    //If entered just {{POPULAR}} , this will return the list with 10 items, considering whole period, without any blacklists.
38
39    function connectTo($mode) {
40      $this->Lexer->addSpecialPattern('\{\{POPULAR[^}]*\}\}',$mode,'plugin_accscounter_popularity');
41    }
42
43
44    function handle($match, $state, $pos, Doku_Handler $handler){
45
46        return explode(' ', substr($match, strlen('{{POPULAR '), -2));
47
48    }
49
50    function render($mode, Doku_Renderer $renderer, $data) {
51        define('PLUGIN_POPULAR_DEFAULT', 10);
52
53        // Get the time zone from conf (if null, it will use the default setting on your server)
54        if ($this->getConf('timezone') != '') date_default_timezone_set($this->getConf('timezone'));
55
56        // Get current time (local)
57        define('CURRENT', time());
58
59        $achelper = plugin_load('helper','accscounter');
60
61        global $INFO;
62        global $conf;
63
64        $max    = PLUGIN_POPULAR_DEFAULT;
65        $except = '';
66
67        if ($data[0] != null) $max = $data[0];
68
69        switch ($data[1]) {
70        case ''         : /*FALLTHROUGH*/
71        case 'allperiod': $period = 'allperiod';
72            break;
73        case 'today'    : $period = 'today';
74            $today = date('Y/m/d');
75            break;
76        case 'yesterday': $period = 'yesterday';
77            $yesterday = date('Y/m/d', CURRENT - 24 * 60 * 60);
78            $thisday = date('Y/m/d');
79            break;
80        default:
81            $renderer->doc .= htmlspecialchars($this->getLang('err3'));
82            return;
83        }
84
85        $except = '|' . $data[2] . '|';
86
87        $counters = array();
88
89        // Get the list of all pages
90        require_once(DOKU_INC.'inc/search.php');
91        $dir = $conf['datadir'];
92        $items = array();
93        search($items, $dir, 'search_allpages', array());
94
95        foreach ($items as $item) {
96            $page = $item['id'];
97            if ((strpos($except, '|' . $page . '|') !== FALSE) ||
98                ! page_exists($page) || ! auth_quickaclcheck($page))
99                continue;
100
101            $array = @file($achelper->counterFN($page, '.number'));
102            if ($array === FALSE) continue;
103            $count = rtrim($array[0]);
104            $date  = rtrim($array[1]);
105            $today_count = rtrim($array[2]);
106            $yesterday_count = rtrim($array[3]);
107
108            if ($today) {
109                if (($today == $date) and ($today_count != 0)) $counters[$page] = $today_count;
110            } else if ($yesterday) {
111                if (($yesterday == $date) and ($today_count != 0)) $counters[$page] = $today_count;
112                if (($thisday == $date) and ($yesterday_count != 0)) $counters[$page] = $yesterday_count;
113            } else {
114                $counters[$page] = $count;
115            }
116        }
117
118        asort($counters, SORT_NUMERIC);
119
120        // BugTrack2/106: Only variables can be passed by reference from PHP 5.0.5
121        $counters = array_reverse($counters, TRUE); // with array_splice()
122        $counters = array_splice($counters, 0, $max);
123
124
125        if (! empty($counters)) {
126            $renderer->listu_open();
127
128            foreach ($counters as $page=>$count) {
129                $renderer->listitem_open(1);
130                $renderer->listcontent_open();
131
132                $renderer->internallink(':' . $page);
133                $renderer->cdata('(' . $count . ')');
134
135                $renderer->listcontent_close();
136                $renderer->listitem_close();
137            }
138            $renderer->listu_close();
139        } else $renderer->doc .= $this->getLang('noitems');
140
141    }
142
143
144}
145