xref: /plugin/publish/helper.php (revision 81e0fa5156f68a5ef26ac13201fcb3ea35d50fcd)
1<?php
2/**
3 * DokuWiki Plugin publish (Helper Component)
4 *
5 * @license GPL 2 http://www.gnu.org/licenses/gpl-2.0.html
6 * @author  Jarrod Lowe <dokuwiki@rrod.net>
7 * @author  Andreas Gohr <gohr@cosmocode.de>
8 */
9
10// must be run within Dokuwiki
11if (!defined('DOKU_INC')) die();
12
13class helper_plugin_publish extends DokuWiki_Plugin {
14
15    private $sortedApprovedRevisions = null;
16
17    // FIXME find out what this is supposed to do and how it can be done better
18    function in_namespace($valid, $check) {
19        // PHP apparantly does not have closures -
20        // so we will parse $valid ourselves. Wasteful.
21        $valid = preg_split('/\s+/', $valid);
22        //if(count($valid) == 0) { return true; }//whole wiki matches
23        if((count($valid)==1) and ($valid[0]=="")) { return true; }//whole wiki matches
24        $check = trim($check, ':');
25        $check = explode(':', $check);
26
27        // Check against all possible namespaces
28        foreach($valid as $v) {
29            $v = explode(':', $v);
30            $n = 0;
31            $c = count($v);
32            $matching = 1;
33
34            // Check each element, untill all elements of $v satisfied
35            while($n < $c) {
36                if($v[$n] != $check[$n]) {
37                    // not a match
38                    $matching = 0;
39                    break;
40                }
41                $n += 1;
42            }
43            if($matching == 1) { return true; } // a match
44        }
45        return false;
46    }
47
48    // FIXME find out what this is supposed to do and how it can be done better
49    function in_sub_namespace($valid, $check) {
50        // is check a dir which contains any valid?
51        // PHP apparantly does not have closures -
52        // so we will parse $valid ourselves. Wasteful.
53        $valid = preg_split('/\s+/', $valid);
54        //if(count($valid) == 0) { return true; }//whole wiki matches
55        if((count($valid)==1) and ($valid[0]=="")) { return true; }//whole wiki matches
56        $check = trim($check, ':');
57        $check = explode(':', $check);
58
59        // Check against all possible namespaces
60        foreach($valid as $v) {
61            $v = explode(':', $v);
62            $n = 0;
63            $c = count($check); //this is what is different from above!
64            $matching = 1;
65
66            // Check each element, untill all elements of $v satisfied
67            while($n < $c) {
68                if($v[$n] != $check[$n]) {
69                    // not a match
70                    $matching = 0;
71                    break;
72                }
73                $n += 1;
74            }
75            if($matching == 1) { return true; } // a match
76        }
77        return false;
78    }
79
80    function canApprove() {
81        global $INFO;
82        global $ID;
83
84        if (!$this->in_namespace($this->getConf('apr_namespaces'), $ID)) {
85            return false;
86        }
87
88        return ($INFO['perm'] >= AUTH_DELETE);
89    }
90
91    function getRevision($id = null) {
92        global $REV;
93        if (isset($REV) && !empty($REV)) {
94            return $REV;
95        }
96        $meta = $this->getMeta($id);
97        if (isset($meta['last_change']['date'])) {
98            return $meta['last_change']['date'];
99        }
100        return $meta['date']['modified'];
101    }
102
103    function getApprovals($id = null) {
104        $meta = $this->getMeta($id);
105        if (!isset($meta['approval'])) {
106            return array();
107        }
108        $approvals = $meta['approval'];
109        return $approvals;
110    }
111
112    function getMeta($id = null) {
113        global $ID;
114        if ($id === null || $ID === $id) {
115            global $INFO;
116            return $INFO['meta'];
117        } else {
118            return p_get_metadata($id);
119        }
120    }
121
122    function getApprovalsOnRevision($revision) {
123        $approvals = $this->getApprovals();
124
125        if (isset($approvals[$revision])) {
126            return $approvals[$revision];
127        }
128        return array();
129    }
130
131    function getSortedApprovedRevisions() {
132        if ($this->sortedApprovedRevisions === null) {
133            $approvals = $this->getApprovals();
134            krsort($approvals);
135            $this->sortedApprovedRevisions = $approvals;
136        }
137        return $this->sortedApprovedRevisions;
138    }
139
140    function isRevisionApproved($revision, $id = null) {
141        $approvals = $this->getApprovals($id);
142        if (!isset($approvals[$revision])) {
143            return false;
144        }
145        return (count($approvals[$revision]) >= $this->getConf('number_of_approved'));
146    }
147
148    function isCurrentRevisionApproved($id = null) {
149        return $this->isRevisionApproved($this->getRevision($id), $id);
150    }
151
152    function getLatestApprovedRevision() {
153        $approvals = $this->getSortedApprovedRevisions();
154        foreach ($approvals as $revision => $ignored) {
155            if ($this->isRevisionApproved($revision)) {
156                return $revision;
157            }
158        }
159        return 0;
160    }
161
162    function getLastestRevision() {
163        global $INFO;
164        return $INFO['meta']['date']['modified'];
165    }
166
167    function getApprovalDate() {
168        if (!$this->isCurrentRevisionApproved()) {
169            return -1;
170        }
171
172        $approvals = $this->getApprovalsOnRevision($this->getRevision());
173        uasort($approvals, array(&$this, 'cmpApprovals'));
174        $keys = array_keys($approvals);
175        return $approvals[$keys[$this->getConf('number_of_approved') -1]][3];
176
177    }
178
179    function cmpApprovals($left, $right) {
180        if ($left[3] == $right[3]) {
181            return 0;
182        }
183        return ($left[3] < $right[3]) ? -1 : 1;
184    }
185
186    function getApprovers() {
187        $approvers = $this->getApprovalsOnRevision($this->getRevision());
188        $result = array();
189        foreach ($approvers as $approver) {
190            $result[] = editorinfo($this->getApproverName($approver));
191        }
192        return $result;
193    }
194
195    function getApproverName($approver) {
196        if ($approver[1]) {
197            return $approver[1];
198        }
199        if ($approver[2]) {
200            return $approver[2];
201        }
202        return $approver[0];
203    }
204
205    function getPreviousApprovedRevision() {
206        $currentRevision = $this->getRevision();
207        $approvals = $this->getSortedApprovedRevisions();
208        foreach ($approvals as $revision => $ignored) {
209            if ($revision >= $currentRevision) {
210                continue;
211            }
212            if ($this->isRevisionApproved($revision)) {
213                return $revision;
214            }
215        }
216        return 0;
217    }
218
219}
220