forked from cosmocode/dokuwiki-plugin-publish
-
Notifications
You must be signed in to change notification settings - Fork 0
/
helper.php
434 lines (368 loc) · 12.4 KB
/
helper.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
<?php
/**
* DokuWiki Plugin publish (Helper Component)
*
* @license GPL 2 http://www.gnu.org/licenses/gpl-2.0.html
* @author Jarrod Lowe <[email protected]>
* @author Andreas Gohr <[email protected]>
*/
// must be run within Dokuwiki
if (!defined('DOKU_INC')) die();
class helper_plugin_publish extends DokuWiki_Plugin {
private $sortedApprovedRevisions = null;
/**
* checks if an id is within one of the namespaces in $namespace_list
*
* @param string $namespace_list
* @param string $id
*
* @return bool
*/
function in_namespace($namespace_list, $id) {
// PHP apparantly does not have closures -
// so we will parse $valid ourselves. Wasteful.
$namespace_list = preg_split('/\s+/', $namespace_list);
//if(count($valid) == 0) { return true; }//whole wiki matches
if((count($namespace_list)==1) and ($namespace_list[0]=="")) { return true; }//whole wiki matches
$id = trim($id, ':');
$id = explode(':', $id);
// Check against all possible namespaces
foreach($namespace_list as $namespace) {
$namespace = explode(':', $namespace);
$current_ns_depth = 0;
$total_ns_depth = count($namespace);
$matching = true;
// Check each element, untill all elements of $v satisfied
while($current_ns_depth < $total_ns_depth) {
if($namespace[$current_ns_depth] != $id[$current_ns_depth]) {
// not a match
$matching = false;
break;
}
$current_ns_depth += 1;
}
if($matching) { return true; } // a match
}
return false;
}
/**
* check if given $dir contains a valid namespace or is contained in a valid namespace
*
* @param $valid_namespaces_list
* @param $dir
*
* @return bool
*/
function is_dir_valid($valid_namespaces_list, $dir) {
$valid_namespaces_list = preg_split('/\s+/', $valid_namespaces_list);
//if(count($valid) == 0) { return true; }//whole wiki matches
if((count($valid_namespaces_list)==1) && ($valid_namespaces_list[0]=="")) { return true; }//whole wiki matches
$dir = trim($dir, ':');
$dir = explode(':', $dir);
// Check against all possible namespaces
foreach($valid_namespaces_list as $valid_namespace) {
$valid_namespace = explode(':', $valid_namespace);
$current_depth = 0;
$dir_depth = count($dir); //this is what is different from above!
$matching = true;
// Check each element, untill all elements of $v satisfied
while($current_depth < $dir_depth) {
if (empty($valid_namespace[$current_depth])) {
break;
}
if($valid_namespace[$current_depth] != $dir[$current_depth]) {
// not a match
$matching = false;
break;
}
$current_depth += 1;
}
if($matching) { return true; } // a match
}
return false;
}
function canApprove() {
global $INFO;
global $ID;
if (!$this->in_namespace($this->getConf('apr_namespaces'), $ID)) {
return false;
}
return ($INFO['perm'] >= AUTH_DELETE);
}
function getRevision($id = null) {
global $REV;
if (isset($REV) && !empty($REV)) {
return $REV;
}
$meta = $this->getMeta($id);
if (isset($meta['last_change']['date'])) {
return $meta['last_change']['date'];
}
return $meta['date']['modified'];
}
function getApprovals($id = null) {
$meta = $this->getMeta($id);
if (!isset($meta['approval'])) {
return array();
}
$approvals = $meta['approval'];
if (!is_array($approvals)) {
return array();
}
return $approvals;
}
function getMeta($id = null) {
global $ID;
global $INFO;
if ($id === null) $id = $ID;
if($ID === $id && $INFO['meta']) {
$meta = $INFO['meta'];
} else {
$meta = p_get_metadata($id);
}
$this->checkApprovalFormat($meta, $id);
return $meta;
}
function checkApprovalFormat($meta, $id) {
if (isset($meta['approval_version']) && $meta['approval_version'] >= 2) {
return;
}
if (!$this->hasApprovals($meta)) {
return;
}
$approvals = $meta['approval'];
foreach (array_keys($approvals) as $approvedId) {
$keys = array_keys($approvals[$approvedId]);
if (is_array($approvals[$approvedId][$keys[0]])) {
continue; // current format
}
$newEntry = $approvals[$approvedId];
if (count($newEntry) !== 3) {
//continue; // some messed up format...
}
$newEntry[] = intval($approvedId); // revision is the time of page edit
$approvals[$approvedId] = array();
$approvals[$approvedId][$newEntry[0]] = $newEntry;
}
p_set_metadata($id, array('approval' => $approvals), true, true);
p_set_metadata($id, array('approval_version' => 2), true, true);
}
function hasApprovals($meta) {
return isset($meta['approval']) && !empty($meta['approval']);
}
function getApprovalsOnRevision($revision) {
$approvals = $this->getApprovals();
if (isset($approvals[$revision])) {
return $approvals[$revision];
}
return array();
}
function getSortedApprovedRevisions($id = null) {
if ($id === null) {
global $ID;
$id = $ID;
}
static $sortedApprovedRevisions = array();
if (!isset($sortedApprovedRevisions[$id])) {
$approvals = $this->getApprovals($id);
krsort($approvals);
$sortedApprovedRevisions[$id] = $approvals;
}
return $sortedApprovedRevisions[$id];
}
function isRevisionApproved($revision, $id = null) {
$approvals = $this->getApprovals($id);
if (!isset($approvals[$revision])) {
return false;
}
return (count($approvals[$revision]) >= $this->getConf('number_of_approved'));
}
function isCurrentRevisionApproved($id = null) {
return $this->isRevisionApproved($this->getRevision($id), $id);
}
function getLatestApprovedRevision($id = null) {
$approvals = $this->getSortedApprovedRevisions($id);
foreach ($approvals as $revision => $ignored) {
if ($this->isRevisionApproved($revision, $id)) {
return $revision;
}
}
return 0;
}
function getLastestRevision() {
global $INFO;
return $INFO['meta']['date']['modified'];
}
function getApprovalDate() {
if (!$this->isCurrentRevisionApproved()) {
return -1;
}
$approvals = $this->getApprovalsOnRevision($this->getRevision());
uasort($approvals, array(&$this, 'cmpApprovals'));
$keys = array_keys($approvals);
return $approvals[$keys[$this->getConf('number_of_approved') -1]][3];
}
function cmpApprovals($left, $right) {
if ($left[3] == $right[3]) {
return 0;
}
return ($left[3] < $right[3]) ? -1 : 1;
}
function getApprovers() {
$approvers = $this->getApprovalsOnRevision($this->getRevision());
if (count($approvers) === 0) {
return;
}
$result = array();
foreach ($approvers as $approver) {
$result[] = editorinfo($this->getApproverName($approver));
}
return $result;
}
function getApproverName($approver) {
if ($approver[1]) {
return $approver[1];
}
if ($approver[2]) {
return $approver[2];
}
return $approver[0];
}
function getPreviousApprovedRevision() {
$currentRevision = $this->getRevision();
$approvals = $this->getSortedApprovedRevisions();
foreach ($approvals as $revision => $ignored) {
if ($revision >= $currentRevision) {
continue;
}
if ($this->isRevisionApproved($revision)) {
return $revision;
}
}
return 0;
}
function isHidden($id = null) {
if (!$this->getConf('hide drafts')) {
return false;
}
// needs to check if the actual namespace belongs to the apr_namespaces
if ($id == null) {
global $ID;
$id = $ID;
}
if (!$this->isActive($id)) {
return false;
}
if ($this->getLatestApprovedRevision($id)) {
return false;
}
return true;
}
function isHiddenForUser($id = null) {
if (!$this->isHidden($id)) {
return false;
}
if ($id == null) {
global $ID;
$id = $ID;
}
$allowedGroups = array_filter(explode(' ', trim($this->getConf('author groups'))));
if (empty($allowedGroups)) {
return auth_quickaclcheck($id) < AUTH_EDIT;
}
if (!$_SERVER['REMOTE_USER']) {
return true;
}
global $USERINFO;
foreach ($allowedGroups as $allowedGroup) {
$allowedGroup = trim($allowedGroup);
if (in_array($allowedGroup, $USERINFO['grps'])) {
return false;
}
}
return true;
}
function isActive($id = null) {
if ($id == null) {
global $ID;
$id = $ID;
}
if (!$this->in_namespace($this->getConf('apr_namespaces'), $id)) {
return false;
}
$no_apr_namespaces = $this->getConf('no_apr_namespaces');
if (!empty($no_apr_namespaces)) {
if ($this->in_namespace($no_apr_namespaces, $id)) {
return false;
}
}
return true;
}
/**
* Create absolute diff-link between the two given revisions
*
* @param string $id
* @param int $rev1
* @param int $rev2
* @return string Diff-Link or empty string if $rev1 == $rev2
*/
public function getDifflink($id, $rev1, $rev2) {
if($rev1 == $rev2) {
return '';
}
$params = 'do=diff,rev2[0]=' . $rev1 . ',rev2[1]=' . $rev2 . ',difftype=sidebyside';
$difflink = wl($id, $params,true,'&');
return $difflink;
}
function getPagesFromNamespace($namespace) {
global $conf;
$dir = $conf['datadir'] . '/' . str_replace(':', '/', $namespace);
$pages = array();
search($pages, $dir, array($this,'_search_helper'), array($namespace, $this->getConf('apr_namespaces'),
$this->getConf('no_apr_namespaces')));
return $pages;
}
/**
* search callback function
*
* filter out pages which can't be approved by the current user
* then check if they need approving
*/
function _search_helper(&$data, $base, $file, $type, $lvl, $opts) {
$ns = $opts[0];
$valid_ns = $opts[1];
$invalid_ns = $opts[2];
if ($type == 'd') {
return $this->is_dir_valid($valid_ns, $ns . ':' . str_replace('/', ':', $file));
}
if (!preg_match('#\.txt$#', $file)) {
return false;
}
$id = pathID($ns . $file);
if (!empty($valid_ns) && !$this->in_namespace($valid_ns, $id)) {
return false;
}
if (!empty($invalid_ns) && $this->in_namespace($invalid_ns, $id)) {
return false;
}
if (auth_quickaclcheck($id) < AUTH_DELETE) {
return false;
}
$meta = $this->getMeta($id);
if ($this->isCurrentRevisionApproved($id)) {
// Already approved
return false;
}
$data[] = array($id, $meta['approval'], $meta['last_change']['date']);
return false;
}
public function removeSubnamespacePages ($pages, $namespace) {
$cleanpages = array();
foreach ($pages as $page) {
if (getNS($page[0]) == $namespace) {
$cleanpages[] = $page;
}
}
return $cleanpages;
}
}