xref: /plugin/statistics/action.php (revision 211caa5d3906ac05d560e9ce5a3171926bbb603c)
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
232257e39bSAndreas Gohr        $controller->register_hook('IO_WIKIPAGE_WRITE', 'BEFORE', $this, 'logedits', []);
242257e39bSAndreas Gohr        $controller->register_hook('SEARCH_QUERY_FULLPAGE', 'AFTER', $this, 'logsearch', []);
252257e39bSAndreas Gohr        $controller->register_hook('ACTION_ACT_PREPROCESS', 'BEFORE', $this, 'loglogins', []);
262257e39bSAndreas Gohr        $controller->register_hook('AUTH_USER_CHANGE', 'AFTER', $this, 'logregistration', []);
272257e39bSAndreas Gohr        $controller->register_hook('FETCH_MEDIA_STATUS', 'BEFORE', $this, 'logmedia', []);
282257e39bSAndreas Gohr        $controller->register_hook('INDEXER_TASKS_RUN', 'AFTER', $this, 'loghistory', []);
2914d99ec0SAndreas Gohr    }
3014d99ec0SAndreas Gohr
3114d99ec0SAndreas Gohr    /**
3214d99ec0SAndreas Gohr     * @fixme call this in the webbug call
3314d99ec0SAndreas Gohr     */
34a8acb244SAndreas Gohr    public function putpixel()
35a8acb244SAndreas Gohr    {
36ed6e7cc1SAndreas Gohr (aider)        global $ID, $INPUT;
3714d99ec0SAndreas Gohr        $url = DOKU_BASE . 'lib/plugins/statistics/log.php?p=' . rawurlencode($ID) .
38ed6e7cc1SAndreas Gohr (aider)            '&amp;r=' . rawurlencode($INPUT->server->str('HTTP_REFERER')) . '&rnd=' . time();
3914d99ec0SAndreas Gohr
402257e39bSAndreas Gohr        echo '<noscript><img alt="" src="' . $url . '" width="1" height="1" /></noscript>';
4114d99ec0SAndreas Gohr    }
4258511ae8SAndreas Gohr
4358511ae8SAndreas Gohr    /**
445bccfe87SAndreas Gohr     * Log page edits actions
4558511ae8SAndreas Gohr     */
46a8acb244SAndreas Gohr    public function logedits(Event $event, $param)
47a8acb244SAndreas Gohr    {
4858511ae8SAndreas Gohr        if ($event->data[3]) return; // no revision
4958511ae8SAndreas Gohr
5058511ae8SAndreas Gohr        if (file_exists($event->data[0][0])) {
5158511ae8SAndreas Gohr            if ($event->data[0][1] == '') {
5258511ae8SAndreas Gohr                $type = 'D';
5358511ae8SAndreas Gohr            } else {
5458511ae8SAndreas Gohr                $type = 'E';
5558511ae8SAndreas Gohr            }
5658511ae8SAndreas Gohr        } else {
5758511ae8SAndreas Gohr            $type = 'C';
5858511ae8SAndreas Gohr        }
591664ba1dSAndreas Gohr        /** @var helper_plugin_statistics $hlp */
6058511ae8SAndreas Gohr        $hlp = plugin_load('helper', 'statistics');
61*211caa5dSAndreas Gohr        $hlp->getLogger()->logEdit(cleanID($event->data[1] . ':' . $event->data[2]), $type);
6258511ae8SAndreas Gohr    }
635bccfe87SAndreas Gohr
645bccfe87SAndreas Gohr    /**
655bccfe87SAndreas Gohr     * Log internal search
665bccfe87SAndreas Gohr     */
67a8acb244SAndreas Gohr    public function logsearch(Event $event, $param)
68a8acb244SAndreas Gohr    {
691664ba1dSAndreas Gohr        /** @var helper_plugin_statistics $hlp */
705bccfe87SAndreas Gohr        $hlp = plugin_load('helper', 'statistics');
71*211caa5dSAndreas Gohr        $hlp->getLogger()->logSearch('', 'dokuwiki', $event->data['query'], $event->data['highlight']);
725bccfe87SAndreas Gohr    }
73b5e880bdSAndreas Gohr
74b5e880bdSAndreas Gohr    /**
75b5e880bdSAndreas Gohr     * Log login/logouts
76b5e880bdSAndreas Gohr     */
77a8acb244SAndreas Gohr    public function loglogins(Event $event, $param)
78a8acb244SAndreas Gohr    {
79ed6e7cc1SAndreas Gohr (aider)        global $INPUT;
80ed6e7cc1SAndreas Gohr (aider)
81b5e880bdSAndreas Gohr        $type = '';
82a5dadbc1SAndreas Gohr        $act  = $this->actClean($event->data);
83b5e880bdSAndreas Gohr        if ($act == 'logout') {
84b5e880bdSAndreas Gohr            $type = 'o';
85ed6e7cc1SAndreas Gohr (aider)        } elseif ($INPUT->server->str('REMOTE_USER') && $act == 'login') {
86ed6e7cc1SAndreas Gohr (aider)            if ($INPUT->str('r')) {
87b5e880bdSAndreas Gohr                $type = 'p';
88b5e880bdSAndreas Gohr            } else {
89b5e880bdSAndreas Gohr                $type = 'l';
90b5e880bdSAndreas Gohr            }
91ed6e7cc1SAndreas Gohr (aider)        } elseif ($INPUT->str('u') && !$INPUT->str('http_credentials') && !$INPUT->server->str('REMOTE_USER')) {
92b5e880bdSAndreas Gohr            $type = 'f';
93b5e880bdSAndreas Gohr        }
94b5e880bdSAndreas Gohr        if (!$type) return;
95b5e880bdSAndreas Gohr
961664ba1dSAndreas Gohr        /** @var helper_plugin_statistics $hlp */
97b5e880bdSAndreas Gohr        $hlp = plugin_load('helper', 'statistics');
98*211caa5dSAndreas Gohr        $hlp->getLogger()->logLogin($type);
9914d99ec0SAndreas Gohr    }
10014d99ec0SAndreas Gohr
101535aeea1SAndreas Gohr    /**
102535aeea1SAndreas Gohr     * Log user creations
103535aeea1SAndreas Gohr     */
104a8acb244SAndreas Gohr    public function logregistration(Event $event, $param)
105a8acb244SAndreas Gohr    {
106535aeea1SAndreas Gohr        if ($event->data['type'] == 'create') {
1071664ba1dSAndreas Gohr            /** @var helper_plugin_statistics $hlp */
108535aeea1SAndreas Gohr            $hlp = plugin_load('helper', 'statistics');
109*211caa5dSAndreas Gohr            $hlp->getLogger()->logLogin('C', $event->data['params'][0]);
110535aeea1SAndreas Gohr        }
111535aeea1SAndreas Gohr    }
112b5e880bdSAndreas Gohr
113b5e880bdSAndreas Gohr    /**
1141664ba1dSAndreas Gohr     * Log media access
1151664ba1dSAndreas Gohr     */
116a8acb244SAndreas Gohr    public function logmedia(Event $event, $param)
117a8acb244SAndreas Gohr    {
1181664ba1dSAndreas Gohr        if ($event->data['status'] < 200) return;
1191664ba1dSAndreas Gohr        if ($event->data['status'] >= 400) return;
1201664ba1dSAndreas Gohr        if (preg_match('/^\w+:\/\//', $event->data['media'])) return;
1211664ba1dSAndreas Gohr
1221664ba1dSAndreas Gohr        // no size for redirect/not modified
1231664ba1dSAndreas Gohr        if ($event->data['status'] >= 300) {
1241664ba1dSAndreas Gohr            $size = 0;
1251664ba1dSAndreas Gohr        } else {
12617978b38SAndreas Gohr            $size = @filesize($event->data['file']);
1271664ba1dSAndreas Gohr        }
1281664ba1dSAndreas Gohr
1291664ba1dSAndreas Gohr        /** @var helper_plugin_statistics $hlp */
1301664ba1dSAndreas Gohr        $hlp = plugin_load('helper', 'statistics');
131*211caa5dSAndreas Gohr        $hlp->getLogger()->logMedia(
1321664ba1dSAndreas Gohr            $event->data['media'],
1331664ba1dSAndreas Gohr            $event->data['mime'],
1341664ba1dSAndreas Gohr            !$event->data['download'],
1351664ba1dSAndreas Gohr            $size
1361664ba1dSAndreas Gohr        );
1371664ba1dSAndreas Gohr    }
1381664ba1dSAndreas Gohr
1391664ba1dSAndreas Gohr    /**
140cae4a1c5SAndreas Gohr     * Log the daily page and media counts for the history
141cae4a1c5SAndreas Gohr     */
142a8acb244SAndreas Gohr    public function loghistory(Event $event, $param)
143a8acb244SAndreas Gohr    {
144cae4a1c5SAndreas Gohr        echo 'Plugin Statistics: started' . DOKU_LF;
145cae4a1c5SAndreas Gohr
146cae4a1c5SAndreas Gohr        /** @var helper_plugin_statistics $hlp */
147cae4a1c5SAndreas Gohr        $hlp = plugin_load('helper', 'statistics');
148a9509e05SAndreas Gohr (aider)        $db = $hlp->getDB();
149cae4a1c5SAndreas Gohr
150cae4a1c5SAndreas Gohr        // check if a history was gathered already today
151a9509e05SAndreas Gohr (aider)        $result = $db->queryAll(
152a9509e05SAndreas Gohr (aider)            "SELECT info FROM history WHERE dt = date('now')"
153a9509e05SAndreas Gohr (aider)        );
154cae4a1c5SAndreas Gohr
155cae4a1c5SAndreas Gohr        $page_ran  = false;
156cae4a1c5SAndreas Gohr        $media_ran = false;
157cae4a1c5SAndreas Gohr        foreach ($result as $row) {
158cae4a1c5SAndreas Gohr            if ($row['info'] == 'page_count')  $page_ran  = true;
159cae4a1c5SAndreas Gohr            if ($row['info'] == 'media_count') $media_ran = true;
160cae4a1c5SAndreas Gohr        }
161cae4a1c5SAndreas Gohr
162cae4a1c5SAndreas Gohr        if ($page_ran && $media_ran) {
163cae4a1c5SAndreas Gohr            echo 'Plugin Statistics: nothing to do - finished' . DOKU_LF;
164cae4a1c5SAndreas Gohr            return;
165cae4a1c5SAndreas Gohr        }
166cae4a1c5SAndreas Gohr
167cae4a1c5SAndreas Gohr        $event->stopPropagation();
168cae4a1c5SAndreas Gohr        $event->preventDefault();
169cae4a1c5SAndreas Gohr
170cae4a1c5SAndreas Gohr        if ($page_ran) {
171cae4a1c5SAndreas Gohr            echo 'Plugin Statistics: logging media' . DOKU_LF;
172*211caa5dSAndreas Gohr            $hlp->getLogger()->logHistoryMedia();
173cae4a1c5SAndreas Gohr        } else {
174cae4a1c5SAndreas Gohr            echo 'Plugin Statistics: logging pages' . DOKU_LF;
175*211caa5dSAndreas Gohr            $hlp->getLogger()->logHistoryPages();
176cae4a1c5SAndreas Gohr        }
177cae4a1c5SAndreas Gohr        echo 'Plugin Statistics: finished' . DOKU_LF;
178cae4a1c5SAndreas Gohr    }
179cae4a1c5SAndreas Gohr
180cae4a1c5SAndreas Gohr    /**
181b5e880bdSAndreas Gohr     * Pre-Sanitize the action command
182b5e880bdSAndreas Gohr     *
183b5e880bdSAndreas Gohr     * Similar to act_clean in action.php but simplified and without
184b5e880bdSAndreas Gohr     * error messages
185b5e880bdSAndreas Gohr     */
186a5dadbc1SAndreas Gohr    protected function actClean($act)
187a8acb244SAndreas Gohr    {
188b5e880bdSAndreas Gohr        // check if the action was given as array key
189b5e880bdSAndreas Gohr        if (is_array($act)) {
190a8acb244SAndreas Gohr            [$act] = array_keys($act);
191b5e880bdSAndreas Gohr        }
192b5e880bdSAndreas Gohr
193b5e880bdSAndreas Gohr        //remove all bad chars
194b5e880bdSAndreas Gohr        $act = strtolower($act);
195b5e880bdSAndreas Gohr        $act = preg_replace('/[^a-z_]+/', '', $act);
196b5e880bdSAndreas Gohr
197b5e880bdSAndreas Gohr        return $act;
198b5e880bdSAndreas Gohr    }
199b5e880bdSAndreas Gohr}
200