-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathaction.php
158 lines (139 loc) · 5.63 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
<?php
/**
* DokuWiki Plugin saveandedit (Action Component)
*
* @license GPL 2 http://www.gnu.org/licenses/gpl-2.0.html
* @author Michael Hamann <[email protected]>
*/
use dokuwiki\Extension\ActionPlugin;
use dokuwiki\Extension\EventHandler;
use dokuwiki\Extension\Event;
class action_plugin_saveandedit extends ActionPlugin
{
/** The action that has been handled before the current action */
private $previousAct;
public function register(EventHandler $controller)
{
// try to register our handler at a late position so e.g. the edittable plugin has a possibility to process its
// data
$controller->register_hook(
'ACTION_ACT_PREPROCESS',
'BEFORE',
$this,
'handleActionActPreprocess',
null,
1000
);
$controller->register_hook('FORM_EDIT_OUTPUT', 'BEFORE', $this, 'handleHtmlEditFormOutput');
}
/**
* Clean the environment after saving for the next edit.
*/
private function cleanAfterSave()
{
global $ID, $INFO, $REV, $RANGE, $TEXT, $PRE, $SUF;
$REV = ''; // now we are working on the current revision
// Handle section edits
if ($PRE || $SUF) {
// $from and $to are 1-based indexes of the actually edited content
$from = strlen($PRE) + 1;
$to = $from + strlen($TEXT);
$RANGE = $from . '-' . $to;
}
// Ensure the current text is loaded again from the file
unset($GLOBALS['TEXT'], $GLOBALS['PRE'], $GLOBALS['SUF']);
// Reset the date of the last modification to avoid conflict messages
unset($GLOBALS['DATE']);
// Reset the change check
unset($_REQUEST['changecheck']);
// Force rendering of the metadata in order to ensure metadata is correct
p_set_metadata($ID, [], true);
$INFO = pageinfo(); // reset pageinfo to new data (e.g. if the page exists)
}
public function handleActionActPreprocess(Event $event, $param)
{
global $INPUT;
if (!$INPUT->bool('saveandedit')) {
return;
}
// check if the action was given as array key
if (is_array($event->data)) {
[$act] = array_keys($event->data);
} else {
$act = $event->data;
}
// Greebo and above
if (class_exists('\\dokuwiki\\ActionRouter', false)) {
/*
The ACTION_ACT_PREPROCESS event is triggered several
times, once for every action. After the save has been
executed, the next event is 'draftdel'. We intercept
the 'draftdel' action and replace it by 'edit'. As this
is a logical place where other plugins may want to save
data (e.g. blogtng), we try to be handled relatively
late. To fix plugins that want to handle the 'edit'
action, we trigger a new event for the 'edit' action.
*/
if ($this->previousAct === 'save' && $act === 'draftdel') {
$this->cleanAfterSave();
$event->data = 'edit';
/*
The edittable plugin would restore $TEXT from the
edittable_data post data on each
ACTION_ACT_PREPROCESS call. This breaks the
automatic restore of the prefix and suffix
data. Stop it from doing this by unsetting its
data.
*/
$INPUT->post->remove('edittable_data');
/*
Stop propagation of the event. All subsequent event
handlers will be called anyway again by the event
triggered below.
*/
$event->stopPropagation();
/*
Trigger a new event for the edit action.
This ensures that all event handlers for the edit
action are called. However, we only advise the
before handlers and re-use the default action and
the after handling of the original event.
*/
$new_evt = new Event('ACTION_ACT_PREPROCESS', $event->data);
// prevent the default action of the original event
if (!$new_evt->advise_before()) {
$event->preventDefault();
}
}
$this->previousAct = $act;
// pre-Greebo compatibility
} elseif ($act === 'save' && actionOK($act) && act_permcheck($act) == 'save' && checkSecurityToken()) {
$event->data = act_save($act);
if ($event->data === 'show') {
$event->data = 'edit';
$this->cleanAfterSave();
} elseif ($event->data === 'conflict') {
// DokuWiki won't accept 'conflict' as action here.
// Just execute save again, the conflict will be detected again
$event->data = 'save';
}
}
}
public function handleHtmlEditFormOutput(Event $event, $param)
{
global $INPUT;
$form = $event->data;
$pos = $form->findPositionByAttribute('type', 'submit');
if (!$pos) {
// no submit button found, source view
return;
}
--$pos;
$form->addTagOpen('div', $pos++);
$attrs = $INPUT->bool('saveandedit') ? ['checked' => 'checked'] : [];
$cb = $form->addCheckBox('saveandedit', $this->getLang('btn_saveandedit'), $pos++);
$cb->attrs = $attrs;
$form->addtagClose('div', $pos);
}
}
// vim:ts=4:sw=4:et: