-
Notifications
You must be signed in to change notification settings - Fork 2
/
SpecialAddResource.php
464 lines (405 loc) · 16.3 KB
/
SpecialAddResource.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
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
<?php
/**
* The AddResource special page
*/
class SpecialAddResource extends SpecialPage
{
private $mAction;
private $mRequest;
private $mSubpageDest;
private $mLinkUrl;
private $mLinkTitle;
private $mLinkDesc;
private $mUpload;
private $mTokenOk;
private $mCancelUpload;
private $mUploadClicked;
/**
* Statically set some variables that are set dynamically by
* Special:Upload. Some code is copied 1:1 from MediaWiki source code,
* and this code needs the variables to be present (we set sensible
* defaults here).
*/
private $mIgnoreWarning = true;
private $mWatchthis = false;
private $mLicense = '';
private $mCopyrightStatus = '';
private $mCopyrightSource = '';
function __construct($request = null) {
parent::__construct('AddResource');
global $wgRequest;
$this->loadRequest(is_null($request) ? $wgRequest : $request);
}
private function loadRequest($request) {
global $wgUser;
$this->mRequest = $request;
$this->mAction = $request->getInt(ADD_RESOURCE_ACTION_FIELD);
switch ($this->mAction) {
case ADD_RESOURCE_ACTION_UPLOAD;
$this->mUpload = UploadBase::createFromRequest($request);
# used by copied processUpload()
$this->mUploadClicked = true;
$this->mComment = $request->getVal('wpUploadDescription');
break;
case ADD_RESOURCE_ACTION_SUBPAGE;
$this->mSubpageDest = $request->getVal('wpSubpageDest');
break;
case ADD_RESOURCE_ACTION_LINK:
$this->mLinkUrl = $request->getVal('wpLinkUrl');
$this->mLinkTitle = $request->getVal('wpLinkTitle');
$this->mLinkDesc = $request->getVal('wpLinkDesc');
break;
default:
break;
};
$this->mTokenOk = $wgUser->matchEditToken(
$request->getVal('wpEditToken')
);
$this->mCancelUpload = false;
}
private function addSectionHeader($message, $class) {
global $wgOut;
$wgOut->wrapWikiMsg("<h2 class='mw-addresourcesection' id='mw-addresource-$class'>$1</h2>", $message);
}
/**
* this is the main worker function that calls all other functions,
* also depending on HTTP-variables (?foo=something). After this
* function you have a complete special page...
*/
function execute($par) {
global $wgOut, $wgRequest, $wgUser, $wgEnableUploads, $wgEnableExternalRedirects;
$this->setHeaders();
/* make a Title object from $par */
if ($par) {
$this->targetTitle = Title::newFromText($par);
$this->param = $par;
} else { /* if nothing was specified */
$wgOut->addWikiText(wfMessage('noParameterHelp')->text());
return;
}
/* header text, title */
$wgOut->setPagetitle(wfMessage('addResourcesPageTitle', $this->targetTitle->getPrefixedText())->text());
$wgOut->addWikiText(
wfMessage('explanation',
$this->targetTitle->getFullText(),
SpecialPage::getTitleFor('Resources'),
wfMessage('upload_header')->text(),
wfMessage('subpage_header')->text(),
wfMessage('link_header')->text()
)->text()
);
# If we are not allowed to do *anything*, we display a red warning message.
if (!($wgUser->isAllowed('edit') && $wgUser->isAllowed('createpage'))
&& ! $wgUser->isAllowed('upload')) {
if ($wgUser->isLoggedIn())
$wgOut->addHTML(getBanner(wfMessage('not_allowed')->text()));
else {
$loginPage = $this->getLoginLink(wfMessage('login_text')->text());
$wgOut->addHTML(getBanner(wfMessage('not_allowed_anon', $loginPage)->text()));
}
return;
}
/* add the various chapters */
if ($wgEnableUploads == True)
$this->uploadChapter();
if ($this->targetTitle->exists())
$this->subpageChapter();
if ($wgEnableExternalRedirects == True)
$this->linkChapter();
}
/**
* Display the upload chapter.
*
* Parts of this function are a 1:1 copy of SpecialUpload::execute() found
* in includes/specials/SpecialUpload.php, version 1.21.1. See
* inline-comments for exact details.
*/
private function uploadChapter() {
global $wgOut, $wgUser;
# Unsave the temporary file in case this was a cancelled upload
if ($this->mCancelUpload) {
if (!$this->unsaveUploadedFile()) {
# Something went wrong, so unsaveUploadedFile showed a warning
return;
}
}
# we need a header no matter what:
$this->addSectionHeader('upload_header', 'upload');
global $wgRequest;
/**
* Start copy of SpecialUpload::execute()
*/
# Process upload or show a form
if (
$this->mTokenOk && !$this->mCancelUpload &&
( $this->mUpload && $this->mUploadClicked )
) {
$this->processUpload();
} else {
# Backwards compatibility hook
if( !wfRunHooks( 'UploadForm:initial', array( &$this ) ) ) {
wfDebug( "Hook 'UploadForm:initial' broke output of the upload form" );
return;
}
$this->showUploadForm( $this->getUploadForm() );
}
# Cleanup
if ( $this->mUpload ) {
$this->mUpload->cleanupTempFile();
}
/**
* END copy of SpecialUpload::execute()
*/
}
/**
* Implementation of getUploadForm()
*/
protected function getUploadForm($message = '', $sessionKey = '', $hideIgnoreWarning = false) {
$form = new UploadFileForm($this->targetTitle);
$form->setTitle($this->getTitle($this->targetTitle));
# display any upload error
$form->addPreText($message);
return $form;
}
/**
* Implementation of showUploadForm()
*/
protected function showUploadForm($form) {
$form->show();
}
/**
* This functionis a 1:1 copy of class SpecialUpload found in
* includes/specials/SpecialUpload.php, version 1.21.1. The only
* difference is the different redirect at the end.
*/
private function processUpload() {
// Fetch the file if required
$status = $this->mUpload->fetchFile();
if (!$status->isOK()) {
$this->showUploadError($this->getOutput()->parse($status->getWikiText()));
return;
}
if (!wfRunHooks('UploadForm:BeforeProcessing', array(&$this))) {
wfDebug("Hook 'UploadForm:BeforeProcessing' broke processing the file.\n");
// This code path is deprecated. If you want to break upload processing
// do so by hooking into the appropriate hooks in UploadBase::verifyUpload
// and UploadBase::verifyFile.
// If you use this hook to break uploading, the user will be returned
// an empty form with no error message whatsoever.
return;
}
// Upload verification
$details = $this->mUpload->verifyUpload();
if ($details['status'] != UploadBase::OK) {
$this->processVerificationError($details);
return;
}
// Verify permissions for this title
$permErrors = $this->mUpload->verifyTitlePermissions($this->getUser());
if ($permErrors !== true) {
$code = array_shift($permErrors[0]);
$this->showRecoverableUploadError($this->msg($code, $permErrors[0])->parse());
return;
}
$this->mLocalFile = $this->mUpload->getLocalFile();
// Check warnings if necessary
if (!$this->mIgnoreWarning) {
$warnings = $this->mUpload->checkWarnings();
if ($this->showUploadWarning($warnings)) {
return;
}
}
// Get the page text if this is not a reupload
if (!$this->mForReUpload) {
$pageText = self::getInitialPageText($this->mComment, $this->mLicense,
$this->mCopyrightStatus, $this->mCopyrightSource);
} else {
$pageText = false;
}
$status = $this->mUpload->performUpload($this->mComment, $pageText, $this->mWatchthis, $this->getUser());
if (!$status->isGood()) {
$this->showUploadError($this->getOutput()->parse($status->getWikiText()));
return;
}
// Success, redirect to description page
$this->mUploadSuccessful = true;
//wfRunHooks('SpecialUploadComplete', array(&$this));
//$this->getOutput()->redirect($this->mLocalFile->getTitle()->getFullURL());
/**
* The previous two lines are in the original function. We don't need
* the hook and we need a different redirect.
*/
$redir = SpecialPage::getTitleFor('Resources', $this->targetTitle->getPrefixedText());
$this->getOutput()->redirect($redir->getFullURL());
}
/**
* This functionis a 1:1 copy of class SpecialUpload found in
* includes/specials/SpecialUpload.php, version 1.21.1.
*/
protected function processVerificationError($details) {
global $wgFileExtensions;
switch( $details['status'] ) {
/** Statuses that only require name changing **/
case UploadBase::MIN_LENGTH_PARTNAME:
$this->showRecoverableUploadError( $this->msg( 'minlength1' )->escaped() );
break;
case UploadBase::ILLEGAL_FILENAME:
$this->showRecoverableUploadError( $this->msg( 'illegalfilename',
$details['filtered'] )->parse() );
break;
case UploadBase::FILENAME_TOO_LONG:
$this->showRecoverableUploadError( $this->msg( 'filename-toolong' )->escaped() );
break;
case UploadBase::FILETYPE_MISSING:
$this->showRecoverableUploadError( $this->msg( 'filetype-missing' )->parse() );
break;
case UploadBase::WINDOWS_NONASCII_FILENAME:
$this->showRecoverableUploadError( $this->msg( 'windows-nonascii-filename' )->parse() );
break;
/** Statuses that require reuploading **/
case UploadBase::EMPTY_FILE:
$this->showUploadError( $this->msg( 'emptyfile' )->escaped() );
break;
case UploadBase::FILE_TOO_LARGE:
$this->showUploadError( $this->msg( 'largefileserver' )->escaped() );
break;
case UploadBase::FILETYPE_BADTYPE:
$msg = $this->msg( 'filetype-banned-type' );
if ( isset( $details['blacklistedExt'] ) ) {
$msg->params( $this->getLanguage()->commaList( $details['blacklistedExt'] ) );
} else {
$msg->params( $details['finalExt'] );
}
$msg->params( $this->getLanguage()->commaList( $wgFileExtensions ),
count( $wgFileExtensions ) );
// Add PLURAL support for the first parameter. This results
// in a bit unlogical parameter sequence, but does not break
// old translations
if ( isset( $details['blacklistedExt'] ) ) {
$msg->params( count( $details['blacklistedExt'] ) );
} else {
$msg->params( 1 );
}
$this->showUploadError( $msg->parse() );
break;
case UploadBase::VERIFICATION_ERROR:
unset( $details['status'] );
$code = array_shift( $details['details'] );
$this->showUploadError( $this->msg( $code, $details['details'] )->parse() );
break;
case UploadBase::HOOK_ABORTED:
if ( is_array( $details['error'] ) ) { # allow hooks to return error details in an array
$args = $details['error'];
$error = array_shift( $args );
} else {
$error = $details['error'];
$args = null;
}
$this->showUploadError( $this->msg( $error, $args )->parse() );
break;
default:
throw new MWException( __METHOD__ . ": Unknown value `{$details['status']}`" );
}
}
/**
* Display the subpage chapter
*/
private function subpageChapter() {
global $wgOut, $wgUser;
$this->addSectionHeader('subpage_header', 'subpage');
# check if we are allowed to create subpages:
if (!($wgUser->isAllowed('edit') && $wgUser->isAllowed('createpage'))) {
$link = $this->getLoginLink(wfMessage('login_text')->text());
$wgOut->addHTML(getBanner(wfMessage('createpage_not_allowed', wfMessage('subpages')->text(), $link)->text(),
'createpage_not_allowed', 'grey'));
return;
}
$form = new SubpageForm($this->mAction, $this->targetTitle, array(
'dest' => $this->mSubpageDest,
));
$form->setTitle($this->getTitle($this->targetTitle));
if ($this->mAction != 'subpage') {
# $form->setMethod('get');
}
$form->show();
}
/**
* Display the link chapter.
*/
private function linkChapter() {
global $wgOut, $wgUser;
$this->addSectionHeader('link_header', 'link');
# check if we are allowed to create subpages:
if (!($wgUser->isAllowed('edit') && $wgUser->isAllowed('createpage'))) {
$link = $this->getLoginLink(wfMessage('login_text')->text());
$wgOut->addHTML(getBanner(wfMessage('createpage_not_allowed', wfMessage('links')->text(), $link)->text(),
'createpage_not_allowed', 'grey'));
return;
}
$form = new ExternalRedirectForm($this->mAction, $this->targetTitle, array(
'desturl' => $this->mLinkUrl,
'desttitle' => $this->mLinkTitle,
'destdesc' => $this->mLinkDesc
));
$form->setTitle($this->getTitle($this->targetTitle));
if ($this->mAction != 'link') {
}
$form->show();
}
function getLoginLink($login_text) {
global $wgTitle;
$userLogin = SpecialPage::getTitleFor('Userlogin');
$query = array('returnto' => $wgTitle->getPrefixedText());
return Linker::link($userLogin, $login_text, array(), $query);
}
/**
* Wrapper-function of addWarning, used by copied code in uploadChapter()
*
*/
private function addWarning($msg, $id = 'warning') {
global $wgOut;
$wgOut->addHTML(getBanner($msg, $id, 'grey'));
}
/**
* Return HTML of an error-message
*/
protected function getError($msg) {
return getBanner($msg, 'error', 'red');
}
/**
* Return HTML of a warning-message
*/
protected function getWarning($msg) {
return getBanner($msg, 'error', 'grey');
}
/**
* Wrapper-functions for getError, used by various copied functions
*/
private function showUploadError($msg) {
$this->showUploadForm($this->getUploadForm($this->getError($msg)));
}
private function showUploadWarning($msg) {
$this->showUploadForm($this->getUploadForm($this->getWarning($msg)));
}
/**
* This function returns the text used in the description of a newly
* uploaded file.
*/
protected function getInitialPageText($comment, $license, $copyrightStatus, $copyrightSource) {
return $comment;
}
/**
* Show an upload error.
*
* Direct copy of includes/specials/SpecialUpload.php, MW version 1.21.1
*/
protected function showRecoverableUploadError( $message ) {
$sessionKey = $this->mUpload->stashSession();
$message = '<h2>' . $this->msg( 'uploaderror' )->escaped() . "</h2>\n" .
'<div class="error">' . $message . "</div>\n";
$form = $this->getUploadForm( $message, $sessionKey );
$form->setSubmitText( $this->msg( 'upload-tryagain' )->escaped() );
$this->showUploadForm( $form );
}
}
?>