forked from cosmocode/dokuwiki-plugin-publish
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaction.php
317 lines (269 loc) · 11.8 KB
/
action.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
<?php
/**
* DokuWiki Plugin publish (Action Component)
*
* @license GPL 2 http://www.gnu.org/licenses/gpl-2.0.html
* @author Jarrod Lowe <[email protected]>
* @author Andreas Gohr <[email protected]>
*/
// TODO:
// Old Revisions Display X
// Recent Changes Display X
// Redirection X
// List of Unapproved Documents user has permission to approve X
// Namespace restrictions + admin X
// Diff Links in banner on Prev Approved X
// List of Recent Approvals X
// Subscriptions should show approvals - hard (MAIL_MESSAGE_SEND is the only appropriate hook)
// Allow submits of docs with no changes for approval, with autocomment X
// RSS Info -- hard (no hooks in feed.php)
// Internationalisation (or not) X
// must be run within Dokuwiki
if(!defined('DOKU_INC')) die();
class action_plugin_publish extends DokuWiki_Action_Plugin {
private $hlp;
function action_plugin_publish(){
$this->hlp = plugin_load('helper','publish'); // load helper plugin
}
function register(&$controller) {
$controller->register_hook('HTML_EDITFORM_OUTPUT', 'BEFORE', $this, handle_html_editform_output, array());
#$controller->register_hook('TPL_ACT_RENDER', 'AFTER', $this, debug, array());
$controller->register_hook('TPL_ACT_RENDER', 'BEFORE', $this, handle_display_banner, array());
$controller->register_hook('IO_WIKIPAGE_WRITE', 'BEFORE', $this, handle_io_write, array());
$controller->register_hook('HTML_REVISIONSFORM_OUTPUT', 'BEFORE', $this, handle_revisions, array());
$controller->register_hook('HTML_RECENTFORM_OUTPUT', 'BEFORE', $this, handle_recent, array());
$controller->register_hook('DOKUWIKI_STARTED', 'BEFORE', $this, handle_start, array());
}
function handle_html_editform_output(&$event, $param) {
global $ID;
global $INFO;
if(!$this->hlp->in_namespace($this->getConf('apr_namespaces'), $ID)) {
return;
}
if($INFO['perm'] < AUTH_DELETE) {
return;
}
$html = '<label class="nowrap" for="approved"><input type="checkbox" id="approved" name="approved" value="1" tabindex=3 onclick="{ return approval_checkbox(\'' . $this->getConf('apr_approved_text') . '\'); }"/> <span>' . $this->getLang('apr_do_approve') . '</span></label>';
$event->data->insertElement(12,$html); //FIXME hardcoded element position
}
function debug(&$event, $param) {
global $ID;
ptln('<pre>');
ptln(print_r(p_get_metadata($ID), true));
ptln(print_r(pageinfo(), true));
ptln('</pre>');
}
function handle_io_write(&$event, $param) {
# This is the only hook I could find which runs on save,
# but late enough to have lastmod set (ACTION_ACT_PREPROCESS
# is too early)
global $_POST;
global $ID;
global $ACT;
global $USERINFO;
global $INFO;
if(!$this->hlp->in_namespace($this->getConf('apr_namespaces'), $ID)) { return; }
if($INFO['perm'] < AUTH_DELETE) { return true; }
if($ACT != 'save') { return true; }
if(!$event->data[3]) { return true; } # don't approve the doc being moved to archive
if($_POST['approved']) {
$data = pageinfo();
#$newdata = p_get_metadata($ID, 'approval');
$newdata = $data['meta']['approval'];
$newdata[$data['lastmod']] = array($data['client'], $_SERVER['REMOTE_USER'], $USERINFO['mail']);
p_set_metadata($ID, array('approval' => $newdata), true, true);
}
return true;
}
function handle_display_banner(&$event, $param) {
global $ID;
global $REV;
global $INFO;
if(!$this->hlp->in_namespace($this->getConf('apr_namespaces'), $ID)) return;
if($event->data != 'show') return true;
if(!$INFO['exists']) return true;
$strings = array();
$strings_flag = false;
$meta = p_get_metadata($ID);
$rev = $REV;
if(!$rev) { $rev = $meta['last_change']['date']; }
if(!$meta['approval']) { $meta['approval'] = array(); }
$allapproved = array_keys($meta['approval']);
sort($allapproved);
$latest_rev = $meta['last_change']['date'];
#$strings[] = '<!-- ' . print_r($meta, true) . '-->';
$longdate = dformat($rev);
# Is this document approved?
$approver = null;
if($meta['approval'][$rev]) {
# Approved
if(is_array($meta['approval'][$rev])) {
$approver = $meta['approval'][$rev][1];
if(!$approver) { $approver = $meta['approval'][$rev][2]; }
if(!$approver) { $approver = $meta['approval'][$rev][0]; }
}else{
$approver = $meta['approval'][$rev];
}
}
# What is the most recent approved version?
$most_recent_approved = null;
$id = count($allapproved)-1;
if($id >= 0) {
if($allapproved[$id] > $rev) {
$most_recent_approved = $allapproved[$id];
}
}
# Latest, if draft
$most_recent_draft = null;
#$strings[] = '<!-- lr='.$latest_rev.', r='.$rev.', mra='.$most_recently_approved.', d='.($latest_rev != $rev).','.($latest_rev != $most_recently_approved).' -->';
if($latest_rev != $rev && $latest_rev != $most_recent_approved) {
$most_recent_draft = $latest_rev;
}
# Approved *before* this one
$previous_approved = null;
foreach($allapproved as $arev) {
if($arev >= $rev) { break; }
$previous_approved = $arev;
}
$strings[] = '<div class="approval approved_';
if($approver && !$most_recent_approved) { $strings[] = 'yes'; } else { $strings[] = 'no'; }
$strings[] = '">';
if($most_recent_draft && $this->getConf('apr_recent_draft')) {
$strings_flag = true;
$strings[] = '<span class="approval_latest_draft">';
$strings[] = sprintf($this->getLang('apr_recent_draft'), wl($ID, 'force_rev=1'));
$strings[] = $this->difflink($ID, null, $REV) . '</span>';
}
if($most_recent_approved && $this->getConf('apr_outdated')) {
$strings_flag = true;
# Approved, but there is a more recent version
$userrev = $most_recent_approved;
if($userrev == $latest_rev) { $userrev = ''; }
$strings[] = '<span class="approval_outdated">';
$strings[] = sprintf($this->getLang('apr_outdated'), wl($ID, 'rev=' . $userrev));
$strings[] = $this->difflink($ID, $userrev, $REV) . '</span>';
}
if(!$approver && $this->getConf('apr_draft')) {
$strings_flag = true;
# Draft
$strings[] = '<span class="approval_draft">';
$strings[] = sprintf($this->getLang('apr_draft'),
'<span class="approval_date">' . $longdate . '</span>');
$strings[] = '</span>';
}
if($approver && $this->getConf('apr_approved')) {
$strings_flag = true;
# Approved
$strings[] = '<span class="approval_approved">';
$strings[] = sprintf($this->getLang('apr_approved'),
'<span class="approval_date">' . $longdate . '</span>',
editorinfo($approver));
$strings[] = '</span>';
}
if($previous_approved && $this->getConf('apr_previous')) {
$strings_flag = true;
$strings[] = '<span class="approval_previous">';
$strings[] = sprintf($this->getLang('apr_previous'),
wl($ID, 'rev=' . $previous_approved),
dformat($previous_approved));
$strings[] = $this->difflink($ID, $previous_approved, $REV) . '</span>';
}
$strings[] = '</div>';
# Don't print the banner if no visible banner is to be displayed
if($strings_flag)
ptln(implode($strings));
return true;
}
function handle_revisions(&$event, $param) {
global $ID;
global $REV;
if(!$this->hlp->in_namespace($this->getConf('apr_namespaces'), $ID)) { return; }
$meta = p_get_metadata($ID);
$latest_rev = $meta['last_change']['date'];
$member = null;
foreach($event->data->_content as $key => $ref) {
if($ref['_elem'] == 'opentag' && $ref['_tag'] == 'div' && $ref['class'] == 'li') {
$member = $key;
}
if($member && $ref['_elem'] == 'tag' &&
$ref['_tag'] == 'input' && $ref['name'] == 'rev2[]'){
if($meta['approval'][$ref['value']] ||
($ref['value'] == 'current' && $meta['approval'][$latest_rev])) {
$event->data->_content[$member]['class'] = 'li approved_revision';
}else{
$event->data->_content[$member]['class'] = 'li unapproved_revision';
}
$member = null;
}
}
return true;
}
function handle_recent(&$event, $param) {
#$meta = p_get_metadata($ID);
#$latest_rev = $meta['last_change']['date'];
$member = null;
foreach($event->data->_content as $key => $ref) {
if($ref['_elem'] == 'opentag' && $ref['_tag'] == 'div' && $ref['class'] == 'li') {
$member = $key;
}
if($member && $ref['_elem'] == 'opentag' &&
$ref['_tag'] == 'a' && $ref['class'] == 'diff_link'){
$name = $ref['href'];
$name = explode('?', $name);
$name = explode('&', $name[1]);
$usename = null;
foreach($name as $n) {
$fields = explode('=', $n);
if($fields[0] == 'id') {
$usename = $fields[1];
break;
}
}
if($usename) {
if($this->hlp->in_namespace($this->getConf('apr_namespaces'), $usename)) {
$meta = p_get_metadata($usename);
if($meta['approval'][$meta['last_change']['date']]) {
$event->data->_content[$member]['class'] = 'li approved_revision';
}else{
$event->data->_content[$member]['class'] = 'li unapproved_revision';
}
}
}
$member = null;
}
}
return true;
}
function difflink($id, $rev1, $rev2) {
if($rev1 == $rev2) { return ''; }
return '<a href="' . wl($id, 'rev2[]=' . $rev1 . '&rev2[]=' . $rev2 . '&do[diff]=1') .
'" class="approved_diff_link">' .
'<img src="'.DOKU_BASE.'lib/images/diff.png" class="approved_diff_link" alt="Diff" />' .
'</a>';
}
function handle_start(&$event, $param) {
# show only
global $ACT;
if($ACT != 'show') { return; }
# only apply to latest rev
global $REV;
if($REV != '') { return; }
# apply to readers only
global $INFO;
if($INFO['perm'] != AUTH_READ) { return; }
# Check for override token
global $_GET;
if($_GET['force_rev']) { return; }
# Only apply to appropriate namespaces
global $ID;
if(!$this->hlp->in_namespace($this->getConf('apr_namespaces'), $ID)) { return; }
# Find latest rev
$meta = p_get_metadata($ID);
if($meta['approval'][$meta['last_change']['date']]) { return; } //REV=0 *is* approved
if(!$meta['approval']) { return; } //no approvals
# Get list of approvals
$all = array_keys($meta['approval']);
if(count($all) == 0) { return; } //no approvals
$REV = $all[count($all)-1];
}
}