-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathrequest.php
More file actions
294 lines (239 loc) · 7.07 KB
/
request.php
File metadata and controls
294 lines (239 loc) · 7.07 KB
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
<?php
/**
* Plugin BatchEdit: User request
*
* @license GPL 2 (http://www.gnu.org/licenses/gpl.html)
* @author Mykola Ostrovskyy <dwpforge@gmail.com>
*/
require_once(DOKU_PLUGIN . 'batchedit/engine.php');
/**
* As of DW 2023-04-04 the \dokuwiki\File\Resolver class is abstact with only
* two concrete implementations (PageResolver, MediaResolver). This child class
* is used to access basic namespace resolution from the abstract base.
*/
class BatcheditNamespaceResolver extends \dokuwiki\File\Resolver {
}
class BatcheditRequest {
const COMMAND_WELCOME = 'welcome';
const COMMAND_PREVIEW = 'preview';
const COMMAND_APPLY = 'apply';
private $command;
private $sessionId;
private $namespace;
private $regexp;
private $replacement;
private $summary;
private $minorEdit;
private $appliedMatches;
/**
*
*/
public function __construct($config) {
$this->command = $this->parseCommand();
if ($this->command == self::COMMAND_WELCOME) {
return;
}
$config->update($this->parseOptions());
$this->sessionId = $this->parseSessionId();
$this->namespace = $this->parseNamespace();
$this->regexp = $this->parseRegexp($config);
$this->replacement = $this->parseReplacement();
$this->summary = $this->parseSummary();
$this->minorEdit = isset($_REQUEST['minor']);
if ($this->command == self::COMMAND_APPLY || $config->getConf('keepmarks')) {
$this->appliedMatches = $this->parseAppliedMatches();
}
}
/**
*
*/
public function getCommand() {
return $this->command;
}
/**
*
*/
public function getSessionId() {
return $this->sessionId;
}
/**
*
*/
public function getNamespace() {
return $this->namespace;
}
/**
*
*/
public function getRegexp() {
return $this->regexp;
}
/**
*
*/
public function getReplacement() {
return $this->replacement;
}
/**
*
*/
public function getSummary() {
return $this->summary;
}
/**
*
*/
public function getMinorEdit() {
return $this->minorEdit;
}
/**
*
*/
public function getAppliedMatches() {
return $this->appliedMatches;
}
/**
*
*/
private function parseCommand() {
if (!isset($_REQUEST['cmd'])) {
return self::COMMAND_WELCOME;
}
if (!is_array($_REQUEST['cmd'])) {
throw new BatcheditException('err_invreq');
}
$command = key($_REQUEST['cmd']);
if (($command != 'preview') && ($command != 'apply')) {
throw new BatcheditException('err_invreq');
}
return $command;
}
/**
*
*/
private function parseOptions() {
if (!isset($_REQUEST['searchmode'])) {
throw new BatcheditException('err_invreq');
}
$options = array();
$options['searchmode'] = $_REQUEST['searchmode'];
$options['matchcase'] = isset($_REQUEST['matchcase']);
$options['multiline'] = isset($_REQUEST['multiline']);
$options['advregexp'] = isset($_REQUEST['advregexp']);
$options['matchctx'] = isset($_REQUEST['matchctx']);
$options['ctxchars'] = isset($_REQUEST['ctxchars']) ? $_REQUEST['ctxchars'] : '';
$options['ctxlines'] = isset($_REQUEST['ctxlines']) ? $_REQUEST['ctxlines'] : '';
$options['searchlimit'] = isset($_REQUEST['searchlimit']);
$options['searchmax'] = isset($_REQUEST['searchmax']) ? $_REQUEST['searchmax'] : '';
$options['keepmarks'] = isset($_REQUEST['keepmarks']);
$options['markpolicy'] = isset($_REQUEST['markpolicy']) ? $_REQUEST['markpolicy'] : '';
$options['tplpatterns'] = isset($_REQUEST['tplpatterns']);
$options['checksummary'] = isset($_REQUEST['checksummary']);
return $options;
}
/**
*
*/
private function parseSessionId() {
if (!isset($_REQUEST['session'])) {
return '';
}
return $_REQUEST['session'];
}
/**
*
*/
private function parseNamespace() {
if (!isset($_REQUEST['namespace'])) {
throw new BatcheditException('err_invreq');
}
$namespace = trim($_REQUEST['namespace']);
if ($namespace != '') {
global $ID;
$resolver = new BatcheditNamespaceResolver($ID);
$resolved = array();
foreach (explode(',', $namespace) as $ns) {
$ns = trim($ns) . ':';
if ($ns[0] == '-') {
$resolved[] = '-' . $resolver->resolveId(substr($ns, 1));
}
else {
$resolved[] = $resolver->resolveId($ns);
}
}
$namespace = implode(',', $resolved);
}
return $namespace;
}
/**
*
*/
private function parseRegexp($config) {
if (!isset($_REQUEST['search'])) {
throw new BatcheditException('err_invreq');
}
$regexp = trim($_REQUEST['search']);
if ($regexp == '') {
throw new BatcheditException('err_nosearch');
}
if ($config->getConf('searchmode') == 'regexp') {
if ($config->getConf('advregexp')) {
if (preg_match('/^([^\w\\\\]|_).+?\1[imsxADSUXJu]*$/s', $regexp) != 1) {
throw new BatcheditException('err_invregexp');
}
}
else {
$regexp = "\033" . $regexp . "\033um";
}
}
else {
$regexp = "\033" . preg_quote($regexp) . "\033";
}
$regexp = str_replace("\r\n", "\n", $regexp);
if (!$config->getConf('matchcase')) {
$regexp .= 'i';
}
return $regexp;
}
/**
*
*/
private function parseReplacement() {
if (!isset($_REQUEST['replace'])) {
throw new BatcheditException('err_invreq');
}
$replace = str_replace("\r\n", "\n", $_REQUEST['replace']);
$unescape = function($matches) {
static $unescaped = array('n' => "\n", 'r' => "\r", 't' => "\t");
if (strlen($matches[1]) % 2) {
return substr($matches[1], 1) . $unescaped[$matches[2]];
}
else {
return $matches[0];
}
};
return preg_replace_callback('/(\\\\+)([nrt])/', $unescape, $replace);
}
/**
*
*/
private function parseSummary() {
if (!isset($_REQUEST['summary'])) {
throw new BatcheditException('err_invreq');
}
return $_REQUEST['summary'];
}
/**
*
*/
private function parseAppliedMatches() {
if (!isset($_REQUEST['apply'])) {
return array();
}
$matchIds = json_decode($_REQUEST['apply']);
if (!is_array($matchIds)) {
throw new BatcheditException('err_invreq');
}
return $matchIds;
}
}