xref: /plugin/statistics/action.php (revision 02aa9b73ad7fe7ccc600c393ae1cf861c37c9024)
114d99ec0SAndreas Gohr<?php
2a8acb244SAndreas Gohr
3a8acb244SAndreas Gohruse dokuwiki\Extension\ActionPlugin;
4a8acb244SAndreas Gohruse dokuwiki\Extension\EventHandler;
5a8acb244SAndreas Gohruse dokuwiki\Extension\Event;
6a8acb244SAndreas Gohr
714d99ec0SAndreas Gohr/**
814d99ec0SAndreas Gohr *
914d99ec0SAndreas Gohr * @license    GPL 2 (http://www.gnu.org/licenses/gpl.html)
1014d99ec0SAndreas Gohr * @author     Andreas Gohr <gohr@cosmocode.de>
1114d99ec0SAndreas Gohr */
12a8acb244SAndreas Gohrclass action_plugin_statistics extends ActionPlugin
13a8acb244SAndreas Gohr{
1414d99ec0SAndreas Gohr    /**
1514d99ec0SAndreas Gohr     * register the eventhandlers and initialize some options
1614d99ec0SAndreas Gohr     */
17a8acb244SAndreas Gohr    public function register(EventHandler $controller)
18a8acb244SAndreas Gohr    {
19eabe0d07SAndreas Gohr        global $JSINFO;
20eabe0d07SAndreas Gohr        global $ACT;
21eabe0d07SAndreas Gohr        $JSINFO['act'] = $ACT;
2214d99ec0SAndreas Gohr
23*02aa9b73SAndreas Gohr        // FIXME new save event might be better:
242257e39bSAndreas Gohr        $controller->register_hook('IO_WIKIPAGE_WRITE', 'BEFORE', $this, 'logedits', []);
252257e39bSAndreas Gohr        $controller->register_hook('SEARCH_QUERY_FULLPAGE', 'AFTER', $this, 'logsearch', []);
262257e39bSAndreas Gohr        $controller->register_hook('ACTION_ACT_PREPROCESS', 'BEFORE', $this, 'loglogins', []);
272257e39bSAndreas Gohr        $controller->register_hook('AUTH_USER_CHANGE', 'AFTER', $this, 'logregistration', []);
282257e39bSAndreas Gohr        $controller->register_hook('FETCH_MEDIA_STATUS', 'BEFORE', $this, 'logmedia', []);
292257e39bSAndreas Gohr        $controller->register_hook('INDEXER_TASKS_RUN', 'AFTER', $this, 'loghistory', []);
3014d99ec0SAndreas Gohr    }
3114d99ec0SAndreas Gohr
3214d99ec0SAndreas Gohr    /**
3314d99ec0SAndreas Gohr     * @fixme call this in the webbug call
3414d99ec0SAndreas Gohr     */
35a8acb244SAndreas Gohr    public function putpixel()
36a8acb244SAndreas Gohr    {
37ed6e7cc1SAndreas Gohr (aider)        global $ID, $INPUT;
3814d99ec0SAndreas Gohr        $url = DOKU_BASE . 'lib/plugins/statistics/log.php?p=' . rawurlencode($ID) .
39ed6e7cc1SAndreas Gohr (aider)            '&amp;r=' . rawurlencode($INPUT->server->str('HTTP_REFERER')) . '&rnd=' . time();
4014d99ec0SAndreas Gohr
412257e39bSAndreas Gohr        echo '<noscript><img alt="" src="' . $url . '" width="1" height="1" /></noscript>';
4214d99ec0SAndreas Gohr    }
4358511ae8SAndreas Gohr
4458511ae8SAndreas Gohr    /**
455bccfe87SAndreas Gohr     * Log page edits actions
4658511ae8SAndreas Gohr     */
47a8acb244SAndreas Gohr    public function logedits(Event $event, $param)
48a8acb244SAndreas Gohr    {
4958511ae8SAndreas Gohr        if ($event->data[3]) return; // no revision
5058511ae8SAndreas Gohr
5158511ae8SAndreas Gohr        if (file_exists($event->data[0][0])) {
5258511ae8SAndreas Gohr            if ($event->data[0][1] == '') {
5358511ae8SAndreas Gohr                $type = 'D';
5458511ae8SAndreas Gohr            } else {
5558511ae8SAndreas Gohr                $type = 'E';
5658511ae8SAndreas Gohr            }
5758511ae8SAndreas Gohr        } else {
5858511ae8SAndreas Gohr            $type = 'C';
5958511ae8SAndreas Gohr        }
601664ba1dSAndreas Gohr        /** @var helper_plugin_statistics $hlp */
6158511ae8SAndreas Gohr        $hlp = plugin_load('helper', 'statistics');
62*02aa9b73SAndreas Gohr        $hlp->getLogger()->logEdit($event->data[1] . ':' . $event->data[2], $type);
6358511ae8SAndreas Gohr    }
645bccfe87SAndreas Gohr
655bccfe87SAndreas Gohr    /**
665bccfe87SAndreas Gohr     * Log internal search
675bccfe87SAndreas Gohr     */
68a8acb244SAndreas Gohr    public function logsearch(Event $event, $param)
69a8acb244SAndreas Gohr    {
701664ba1dSAndreas Gohr        /** @var helper_plugin_statistics $hlp */
715bccfe87SAndreas Gohr        $hlp = plugin_load('helper', 'statistics');
72*02aa9b73SAndreas Gohr        $hlp->getLogger()->logSearch($event->data['query'], $event->data['highlight']);
735bccfe87SAndreas Gohr    }
74b5e880bdSAndreas Gohr
75b5e880bdSAndreas Gohr    /**
76b5e880bdSAndreas Gohr     * Log login/logouts
77b5e880bdSAndreas Gohr     */
78a8acb244SAndreas Gohr    public function loglogins(Event $event, $param)
79a8acb244SAndreas Gohr    {
80ed6e7cc1SAndreas Gohr (aider)        global $INPUT;
81ed6e7cc1SAndreas Gohr (aider)
82b5e880bdSAndreas Gohr        $type = '';
83a5dadbc1SAndreas Gohr        $act  = $this->actClean($event->data);
84b5e880bdSAndreas Gohr        if ($act == 'logout') {
85b5e880bdSAndreas Gohr            $type = 'o';
86ed6e7cc1SAndreas Gohr (aider)        } elseif ($INPUT->server->str('REMOTE_USER') && $act == 'login') {
87ed6e7cc1SAndreas Gohr (aider)            if ($INPUT->str('r')) {
88b5e880bdSAndreas Gohr                $type = 'p';
89b5e880bdSAndreas Gohr            } else {
90b5e880bdSAndreas Gohr                $type = 'l';
91b5e880bdSAndreas Gohr            }
92ed6e7cc1SAndreas Gohr (aider)        } elseif ($INPUT->str('u') && !$INPUT->str('http_credentials') && !$INPUT->server->str('REMOTE_USER')) {
93b5e880bdSAndreas Gohr            $type = 'f';
94b5e880bdSAndreas Gohr        }
95b5e880bdSAndreas Gohr        if (!$type) return;
96b5e880bdSAndreas Gohr
971664ba1dSAndreas Gohr        /** @var helper_plugin_statistics $hlp */
98b5e880bdSAndreas Gohr        $hlp = plugin_load('helper', 'statistics');
99211caa5dSAndreas Gohr        $hlp->getLogger()->logLogin($type);
10014d99ec0SAndreas Gohr    }
10114d99ec0SAndreas Gohr
102535aeea1SAndreas Gohr    /**
103535aeea1SAndreas Gohr     * Log user creations
104535aeea1SAndreas Gohr     */
105a8acb244SAndreas Gohr    public function logregistration(Event $event, $param)
106a8acb244SAndreas Gohr    {
107535aeea1SAndreas Gohr        if ($event->data['type'] == 'create') {
1081664ba1dSAndreas Gohr            /** @var helper_plugin_statistics $hlp */
109535aeea1SAndreas Gohr            $hlp = plugin_load('helper', 'statistics');
110211caa5dSAndreas Gohr            $hlp->getLogger()->logLogin('C', $event->data['params'][0]);
111535aeea1SAndreas Gohr        }
112535aeea1SAndreas Gohr    }
113b5e880bdSAndreas Gohr
114b5e880bdSAndreas Gohr    /**
1151664ba1dSAndreas Gohr     * Log media access
1161664ba1dSAndreas Gohr     */
117a8acb244SAndreas Gohr    public function logmedia(Event $event, $param)
118a8acb244SAndreas Gohr    {
1191664ba1dSAndreas Gohr        if ($event->data['status'] < 200) return;
1201664ba1dSAndreas Gohr        if ($event->data['status'] >= 400) return;
1211664ba1dSAndreas Gohr        if (preg_match('/^\w+:\/\//', $event->data['media'])) return;
1221664ba1dSAndreas Gohr
1231664ba1dSAndreas Gohr        // no size for redirect/not modified
1241664ba1dSAndreas Gohr        if ($event->data['status'] >= 300) {
1251664ba1dSAndreas Gohr            $size = 0;
1261664ba1dSAndreas Gohr        } else {
12717978b38SAndreas Gohr            $size = @filesize($event->data['file']);
1281664ba1dSAndreas Gohr        }
1291664ba1dSAndreas Gohr
1301664ba1dSAndreas Gohr        /** @var helper_plugin_statistics $hlp */
1311664ba1dSAndreas Gohr        $hlp = plugin_load('helper', 'statistics');
132211caa5dSAndreas Gohr        $hlp->getLogger()->logMedia(
1331664ba1dSAndreas Gohr            $event->data['media'],
1341664ba1dSAndreas Gohr            $event->data['mime'],
1351664ba1dSAndreas Gohr            !$event->data['download'],
1361664ba1dSAndreas Gohr            $size
1371664ba1dSAndreas Gohr        );
1381664ba1dSAndreas Gohr    }
1391664ba1dSAndreas Gohr
1401664ba1dSAndreas Gohr    /**
141cae4a1c5SAndreas Gohr     * Log the daily page and media counts for the history
142cae4a1c5SAndreas Gohr     */
143a8acb244SAndreas Gohr    public function loghistory(Event $event, $param)
144a8acb244SAndreas Gohr    {
145cae4a1c5SAndreas Gohr        echo 'Plugin Statistics: started' . DOKU_LF;
146cae4a1c5SAndreas Gohr
147cae4a1c5SAndreas Gohr        /** @var helper_plugin_statistics $hlp */
148cae4a1c5SAndreas Gohr        $hlp = plugin_load('helper', 'statistics');
149a9509e05SAndreas Gohr (aider)        $db = $hlp->getDB();
150cae4a1c5SAndreas Gohr
151cae4a1c5SAndreas Gohr        // check if a history was gathered already today
152a9509e05SAndreas Gohr (aider)        $result = $db->queryAll(
153*02aa9b73SAndreas Gohr            "SELECT info FROM history WHERE date(dt) = date('now')"
154a9509e05SAndreas Gohr (aider)        );
155cae4a1c5SAndreas Gohr
156cae4a1c5SAndreas Gohr        $page_ran  = false;
157cae4a1c5SAndreas Gohr        $media_ran = false;
158cae4a1c5SAndreas Gohr        foreach ($result as $row) {
159cae4a1c5SAndreas Gohr            if ($row['info'] == 'page_count')  $page_ran  = true;
160cae4a1c5SAndreas Gohr            if ($row['info'] == 'media_count') $media_ran = true;
161cae4a1c5SAndreas Gohr        }
162cae4a1c5SAndreas Gohr
163cae4a1c5SAndreas Gohr        if ($page_ran && $media_ran) {
164cae4a1c5SAndreas Gohr            echo 'Plugin Statistics: nothing to do - finished' . DOKU_LF;
165cae4a1c5SAndreas Gohr            return;
166cae4a1c5SAndreas Gohr        }
167cae4a1c5SAndreas Gohr
168cae4a1c5SAndreas Gohr        $event->stopPropagation();
169cae4a1c5SAndreas Gohr        $event->preventDefault();
170cae4a1c5SAndreas Gohr
171cae4a1c5SAndreas Gohr        if ($page_ran) {
172cae4a1c5SAndreas Gohr            echo 'Plugin Statistics: logging media' . DOKU_LF;
173211caa5dSAndreas Gohr            $hlp->getLogger()->logHistoryMedia();
174cae4a1c5SAndreas Gohr        } else {
175cae4a1c5SAndreas Gohr            echo 'Plugin Statistics: logging pages' . DOKU_LF;
176211caa5dSAndreas Gohr            $hlp->getLogger()->logHistoryPages();
177cae4a1c5SAndreas Gohr        }
178cae4a1c5SAndreas Gohr        echo 'Plugin Statistics: finished' . DOKU_LF;
179cae4a1c5SAndreas Gohr    }
180cae4a1c5SAndreas Gohr
181cae4a1c5SAndreas Gohr    /**
182b5e880bdSAndreas Gohr     * Pre-Sanitize the action command
183b5e880bdSAndreas Gohr     *
184b5e880bdSAndreas Gohr     * Similar to act_clean in action.php but simplified and without
185b5e880bdSAndreas Gohr     * error messages
186b5e880bdSAndreas Gohr     */
187a5dadbc1SAndreas Gohr    protected function actClean($act)
188a8acb244SAndreas Gohr    {
189b5e880bdSAndreas Gohr        // check if the action was given as array key
190b5e880bdSAndreas Gohr        if (is_array($act)) {
191a8acb244SAndreas Gohr            [$act] = array_keys($act);
192b5e880bdSAndreas Gohr        }
193b5e880bdSAndreas Gohr
194b5e880bdSAndreas Gohr        //remove all bad chars
195b5e880bdSAndreas Gohr        $act = strtolower($act);
196b5e880bdSAndreas Gohr        $act = preg_replace('/[^a-z_]+/', '', $act);
197b5e880bdSAndreas Gohr
198b5e880bdSAndreas Gohr        return $act;
199b5e880bdSAndreas Gohr    }
200b5e880bdSAndreas Gohr}
201