1<?php
2/**
3 * @license    GPL 2 (http://www.gnu.org/licenses/gpl.html)
4 * @author     Esther Brunner <wikidesign@gmail.com>
5 */
6
7// must be run within Dokuwiki
8if (!defined('DOKU_INC')) die();
9
10class helper_plugin_editor extends DokuWiki_Plugin {
11
12    function getMethods() {
13        $result = array();
14        $result[] = array(
15                'name'   => 'getEditor',
16                'desc'   => 'returns pages recently edited by a given user',
17                'params' => array(
18                    'namespace (optional)' => 'string',
19                    'number (optional)' => 'integer',
20                    'user (required)' => 'string'),
21                'return' => array('pages' => 'array'),
22                );
23        return $result;
24    }
25
26    /**
27     * Get pages edited by user from a given namespace
28     */
29    function getEditor($ns = '', $num = NULL, $user = '') {
30        global $conf;
31
32        if (!$user) $user = $_REQUEST['user'];
33
34        $first  = $_REQUEST['first'];
35        if (!is_numeric($first)) $first = 0;
36
37        if ((!$num) || (!is_numeric($num))) $num = $conf['recent'];
38
39        if ($user == '@ALL') {                                                 // all users
40            $type = 'all';
41        } elseif ($user{0} == '@') {                                           // filter group
42            global $auth;
43
44            if (($auth) && ($auth->canDo('getUsers'))) {
45                $user = $auth->retrieveUsers(0, 0, array('grps' => substr($user, 1)));
46                $user = array_keys($user);
47                $type = 'group';
48            } else {
49                msg('Group filtering not supported by authentification class.', -1);
50                return array();
51            }
52        } elseif (preg_match("/\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}/", $user)) { // filter IP
53            $type = 'ip';
54        } else {                                                              // filter user
55            $type = 'user';
56        }
57
58        // read all recent changes. (kept short)
59        $lines = file($conf['changelog']);
60
61        // handle lines
62        $result = array();
63        $count  = 0;
64        for ($i = count($lines)-1; $i >= 0; $i--) {
65            $rec = $this->_handleRecent($lines[$i], $ns, $type, $user);
66            if ($rec !== false) {
67                if (--$first >= 0) continue; // skip first entries
68                $result[] = $rec;
69                $count++;
70                // break when we have enough entries
71                if ($count >= $num) break;
72            }
73        }
74
75        // clear static $seen in _handleRecent
76        $this->_handleRecent(array(), '', 'clear', '');
77
78        return $result;
79    }
80
81    /* ---------- Changelog function adapted for the Editor Plugin ---------- */
82
83    /**
84     * Internal function used by $this->getPages()
85     *
86     * don't call directly
87     *
88     * @see getRecents()
89     * @author Andreas Gohr <andi@splitbrain.org>
90     * @author Ben Coburn <btcoburn@silicodon.net>
91     * @author Esther Brunner <wikidesign@gmail.com>
92     */
93    function _handleRecent($line, $ns, $type, $user) {
94        global $auth;                          // authentification class
95        static $seen = array();                // caches seen pages and skip them
96
97        if ($type == 'clear') $seen = array(); // clear seen pages cache
98        if (empty($line)) return false;        // skip empty lines
99
100        // split the line into parts
101        $recent = parseChangelogLine($line);
102        if ($recent === false) return false;
103
104        // skip seen ones
105        if(isset($seen[$recent['id']])) return false;
106
107        // entry clauses for user and ip filtering
108        switch ($type) {
109            case 'all':
110                break;
111            case 'user':
112                if ($recent['user'] != $user) return false;
113                break;
114            case 'group':
115                if (!in_array($recent['user'], $user)) return false;
116                break;
117            case 'ip':
118                if ($recent['ip'] != $user) return false;
119                break;
120        }
121
122        // skip minors
123        if ($recent['type']==='e') return false;
124
125        // remember in seen to skip additional sights
126        $seen[$recent['id']] = 1;
127
128        // check if it's a hidden page
129        if (isHiddenPage($recent['id'])) return false;
130
131        // filter namespace
132        if (($ns) && (strpos($recent['id'], $ns.':') !== 0)) return false;
133
134        // check ACL
135        $recent['perm'] = auth_quickaclcheck($recent['id']);
136        if ($recent['perm'] < AUTH_READ) return false;
137
138        // check existance
139        $recent['file']   = wikiFN($recent['id']);
140        $recent['exists'] = @file_exists($recent['file']);
141        if (!$recent['exists']) return false;
142
143        $recent['desc']   = $recent['sum'];
144        if ($recent['user']) {
145            $userinfo = $auth->getUserData($recent['user']);
146            if ($userinfo) $recent['user'] = $userinfo['name'];
147        } else {
148            $recent['user'] = $recent['ip'];
149        }
150
151        return $recent;
152    }
153}
154// vim:ts=4:sw=4:et:enc=utf-8:
155