forked from mathiasertl/AddResource
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ResourceForms.php
384 lines (330 loc) · 12.9 KB
/
ResourceForms.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
<?php
/**
* Baseclass for all forms in this extension.
*/
abstract class AddResourceForm extends HTMLForm {
protected $formAction;
public function __construct($title) {
$this->title = $title;
$descriptors = $this->getDescriptors();
parent::__construct($descriptors, 'addresource');
}
/**
* Get form fields common to all fields.
*/
protected function getCommonFields() {
return array(
# Remember what article we upload a resource for. This information
# is used by our upload backends in ResourceUploadBackends.php
ADD_RESOURCE_REFERER_NAME => array(
'type' => 'hidden',
'id' => ADD_RESOURCE_REFERER_FIELD,
'default' => $this->title->getPrefixedDBkey()
),
# Remember the action we performed.
ADD_RESOURCE_ACTION_NAME => array(
'type' => 'hidden',
'id' => ADD_RESOURCE_ACTION_FIELD,
'default' => $this->formAction,
),
);
}
/**
* Returns true if this particular form was clicked, which is decided
* via ADD_RESOURCE_ACTION_FIELD.
*/
function wasClicked() {
global $wgRequest;
$action = $wgRequest->getInt(ADD_RESOURCE_ACTION_FIELD);
return $wgRequest->wasPosted() && $action === $this->formAction;
}
/**
* Override the function in HTMLForm to only load the data when this form
* was actually clicked.
*
* We do nothing if the form wasn't clicked.
*/
function loadData() {
if ($this->wasClicked()) {
parent::loadData();
} else if ($this->mFieldData === null) {
$this->mFieldData = [];
}
}
/**
* Override function in baseclass to only try an authorized submit if this
* form was actually clicked.
*/
function tryAuthorizedSubmit() {
if ($this->wasClicked()) {
return parent::tryAuthorizedSubmit();
} else {
return false;
}
}
}
/**
* Superclass for creating links and subpages. Only contains common validator.
*/
abstract class PageCreationForm extends AddResourceForm {
/**
* Callback that validates that a page does *not* exist.
*/
public function validatePageNotExists($value, $alldata) {
$value = str_replace('/', '-', $value);
$title = Title::NewFromText(
$alldata[ADD_RESOURCE_REFERER_NAME] . '/' . $value);
if ($title->exists()) {
return wfMessage('page-exists')->text();
} else {
return true;
}
}
}
/**
*/
class UploadFileForm extends AddResourceForm {
protected $formAction = ADD_RESOURCE_ACTION_UPLOAD;
private $mDesiredDestName;
private $mForReUpload;
private $mComment;
public function __construct($title, $options = array()) {
parent::__construct($title);
# Set some form properties
$this->setSubmitText(wfMessage('uploadbtn')->text());
$this->setSubmitName('submit'); #TODO: maybe interesting to get type of submission?
# Used message keys: 'accesskey-upload', 'tooltip-upload'
$this->setSubmitTooltip('upload');
$this->setId('mw-upload-form');
$image_list_link = Linker::specialLink('Listfiles', 'upload_exp_linktext');
$this->addHeaderText(wfMessage('upload_exp', $image_list_link)->text()
);
$this->addPostText('<br />' . wfMessage('upload_footer',
Linker::makeExternalLink(
wfMessage('upload_footer_url')->text(),
wfMessage('upload_footer_linktext')->text()
))->text()
);
$this->mSubmitCallback = array($this, 'submit');
}
public function submit() {
}
protected function getDescriptors() {
global $wgUser, $wgLang, $wgMaxUploadSize;
$descriptor = $this->getCommonFields();
$size = $wgLang->formatSize(
wfShorthandToInteger(min(
wfShorthandToInteger(
ini_get('upload_max_filesize')
), $wgMaxUploadSize
))
);
$upload_source_file = wfMessage('upload_source_file')->escaped();
$descriptor['UploadFile'] = array(
'class' => 'UploadSourceField',
'type' => 'file',
'id' => 'wpUploadFile',
'label-message' => 'sourcefilename',
'upload-type' => 'File',
'radio' => &$radio,
'help' => wfMessage('upload-maxfilesize', $size)->text() . " $upload_source_file",
'checked' => true, #$selectedSourceType == 'file',
);
$descriptor['Extensions'] = array(
'type' => 'info',
'default' => $this->getExtensionsMessage(),
'raw' => true,
);
$descriptor['DestFile'] = array(
'type' => 'text',
'id' => 'wpDestFile',
'label-message' => 'destfilename',
'size' => 60,
'default' => $this->mDesiredDestName,
# FIXME: hack to work around poor handling of the 'default' option in HTMLForm
'nodata' => strval($this->mDesiredDestName) !== '',
);
$descriptor['UploadDescription'] = array(
'type' => 'textarea',
'id' => 'wpUploadDescription',
'label-message' => $this->mForReUpload
? 'filereuploadsummary'
: 'fileuploadsummary',
'default' => $this->mComment,
'cols' => intval($wgUser->getOption('cols')),
'rows' => 8,
);
$descriptor['IgnoreWarning'] = array(
'type' => 'hidden',
'id' => 'wpIgnoreWarning',
'label-message' => 'ignorewarnings',
'default' => '1',
);
$descriptor['DestFileWarningAck'] = array(
'type' => 'hidden',
'id' => 'wpDestFileWarningAck',
'default' => '1',
);
$descriptor['ForReUpload'] = array(
'type' => 'hidden',
'id' => 'wpForReUpload',
'default' => '1',
);
return $descriptor;
}
/**
* Get the messages indicating which extensions are preferred and prohibitted.
*
* Exact copy of UploadForm::getExtensionsMessage() in 1.21.1
*
* @return String: HTML string containing the message
*/
protected function getExtensionsMessage() {
# Print a list of allowed file extensions, if so configured. We ignore
# MIME type here, it's incomprehensible to most people and too long.
global $wgCheckFileExtensions, $wgStrictFileExtensions,
$wgFileExtensions, $wgFileBlacklist;
if ($wgCheckFileExtensions) {
if ($wgStrictFileExtensions) {
# Everything not permitted is banned
$extensionsList =
'<div id="mw-upload-permitted">' .
$this->msg('upload-permitted', $this->getContext()->getLanguage()->commaList($wgFileExtensions))->parseAsBlock() .
"</div>\n";
} else {
# We have to list both preferred and prohibited
$extensionsList =
'<div id="mw-upload-preferred">' .
$this->msg('upload-preferred', $this->getContext()->getLanguage()->commaList($wgFileExtensions))->parseAsBlock() .
"</div>\n" .
'<div id="mw-upload-prohibited">' .
$this->msg('upload-prohibited', $this->getContext()->getLanguage()->commaList($wgFileBlacklist))->parseAsBlock() .
"</div>\n";
}
} else {
# Everything is permitted.
$extensionsList = '';
}
return $extensionsList;
}
}
class SubpageForm extends PageCreationForm {
protected $formAction = ADD_RESOURCE_ACTION_SUBPAGE;
private $mDest;
public function __construct($action, $title, $options = array()) {
parent::__construct($title);
$this->mDest = isset($options['dest']) ? $options['dest'] : '';
$this->mSubmitCallback = array($this, 'submit');
# Set some form properties
$this->setSubmitText(wfMessage('subpage_button')->text());
$this->setSubmitName('submit');
# Used message keys: 'accesskey-upload', 'tooltip-upload'
$this->setSubmitTooltip('create');
$this->setId('mw-upload-form');
$this->addHeaderText(wfMessage('subpage_exp', wfMessage('subpage_button')->text())->text());
$this->addPostText('<br />' . wfMessage('subpage_after_exp')->text());
}
public function submit() {
global $wgOut;
$subpage = str_replace('/', '-', $this->mDest);
$wgOut->redirect($this->title->getFullURL() . '/' . $subpage . '?action=edit');
}
protected function getDescriptors() {
global $wgUser, $wgLang, $wgMaxUploadSize;
$descriptor = $this->getCommonFields();
$descriptor['SubpageDest'] = array(
'type' => 'text',
'id' => 'wpSubpageDest',
'label-message' => 'subpage_inputTitle',
'size' => 60,
'default' => $this->mDest,
'validation-callback' => array($this, 'validatePageNotExists'),
'required' => true,
);
return $descriptor;
}
}
class ExternalRedirectForm extends PageCreationForm {
protected $formAction = ADD_RESOURCE_ACTION_LINK;
private $mLinkUrl;
private $mLinkTitle;
private $mLinkDesc;
public function __construct($action, $title, $options = array()) {
parent::__construct($title);
$this->mLinkUrl = isset($options['desturl']) ? $options['desturl'] : '';
$this->mLinkTitle = isset($options['desttitle']) ? $options['desttitle'] : '';
$this->mLinkDesc = isset($options['destdesc']) ? $options['destdesc'] : '';
# Set some form properties
$this->setSubmitText(wfMessage('link_button')->text());
$this->setSubmitName('submit');
# Used message keys: 'accesskey-upload', 'tooltip-upload'
$this->setSubmitTooltip('create');
$this->setId('mw-upload-form');
$this->addHeaderText(wfMessage('link_exp', wfMessage('link_button')->text())->text());
$this->addPostText('<br />' . wfMessage('link_footer',
$title->getFullText(),
Linker::linkKnown(
SpecialPage::getTitleFor('Prefixindex', $title->getPrefixedText()),
wfMessage('link_footer_linktext')->text(),
array(),
array('namespace' => $title->getNamespace())
)
)->text());
$this->mSubmitCallback = array($this, 'submit');
}
public function submit() {
global $wgOut;
$subpage = str_replace('/', '-', $this->mLinkTitle);
$title = Title::NewFromText($this->title->getPrefixedText() . '/' . $subpage);
$article = new Article($title);
$text = '#REDIRECT [[' . $this->mLinkUrl;
if ($this->mLinkDesc != '')
$text .= '|' . $this->mLinkDesc;
$text .= ']]';
# add a category:
global $wgResourcesCategory;
if ($wgResourcesCategory != NULL && gettype($wgResourcesCategory) == "string") {
global $wgContLang;
$category_text = $wgContLang->getNSText(NS_CATEGORY);
$text .= "\n[[" . $category_text . ":" . $wgResourcesCategory . "]]";
}
$link = $title->getFullURL() . '?redirect=no';
$article->doEditContent(ContentHandler::makeContent($text, $title), wfMessage('commit_message', $link, $this->mLinkUrl)->text(), EDIT_NEW);
$redir = SpecialPage::getTitleFor('Resources', $this->title->getPrefixedText());
$wgOut->redirect($redir->getFullURL() . '?highlight=' . $subpage);
}
public function validateUrl($value, $alldata) {
#TODO: actually validate
return true;
}
protected function getDescriptors() {
$descriptor = $this->getCommonFields();
$descriptor['LinkUrl'] = array(
'type' => 'text',
'id' => 'wpLinkUrl',
'label-message' => 'link_url',
'size' => 60,
'default' => $this->mLinkUrl,
'validation-callback' => array($this, 'validateUrl'),
'required' => true,
);
$descriptor['LinkTitle'] = array(
'type' => 'text',
'id' => 'wpLinkTitle',
'label-message' => 'link_title',
'size' => 60,
'default' => $this->mLinkTitle,
'validation-callback' => array($this, 'validatePageNotExists'),
'required' => true,
);
$descriptor['LinkDesc'] = array(
'type' => 'text',
'id' => 'wpLinkDesc',
'label-message' => 'link_desc',
'size' => 60,
'default' => $this->mLinkDesc,
);
return $descriptor;
}
}
?>