SpecialUpload: Separate style only module
[lhc/web/wiklou.git] / includes / specials / SpecialUpload.php
1 <?php
2 /**
3 * Implements Special:Upload
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
19 *
20 * @file
21 * @ingroup SpecialPage
22 * @ingroup Upload
23 */
24
25 /**
26 * Form for handling uploads and special page.
27 *
28 * @ingroup SpecialPage
29 * @ingroup Upload
30 */
31 class SpecialUpload extends SpecialPage {
32 /**
33 * Constructor : initialise object
34 * Get data POSTed through the form and assign them to the object
35 * @param WebRequest $request Data posted.
36 */
37 public function __construct( $request = null ) {
38 parent::__construct( 'Upload', 'upload' );
39 }
40
41 public function doesWrites() {
42 return true;
43 }
44
45 /** Misc variables **/
46
47 /** @var WebRequest|FauxRequest The request this form is supposed to handle */
48 public $mRequest;
49 public $mSourceType;
50
51 /** @var UploadBase */
52 public $mUpload;
53
54 /** @var LocalFile */
55 public $mLocalFile;
56 public $mUploadClicked;
57
58 /** User input variables from the "description" section **/
59
60 /** @var string The requested target file name */
61 public $mDesiredDestName;
62 public $mComment;
63 public $mLicense;
64
65 /** User input variables from the root section **/
66
67 public $mIgnoreWarning;
68 public $mWatchthis;
69 public $mCopyrightStatus;
70 public $mCopyrightSource;
71
72 /** Hidden variables **/
73
74 public $mDestWarningAck;
75
76 /** @var bool The user followed an "overwrite this file" link */
77 public $mForReUpload;
78
79 /** @var bool The user clicked "Cancel and return to upload form" button */
80 public $mCancelUpload;
81 public $mTokenOk;
82
83 /** @var bool Subclasses can use this to determine whether a file was uploaded */
84 public $mUploadSuccessful = false;
85
86 /** Text injection points for hooks not using HTMLForm **/
87 public $uploadFormTextTop;
88 public $uploadFormTextAfterSummary;
89
90 /**
91 * Initialize instance variables from request and create an Upload handler
92 */
93 protected function loadRequest() {
94 $this->mRequest = $request = $this->getRequest();
95 $this->mSourceType = $request->getVal( 'wpSourceType', 'file' );
96 $this->mUpload = UploadBase::createFromRequest( $request );
97 $this->mUploadClicked = $request->wasPosted()
98 && ( $request->getCheck( 'wpUpload' )
99 || $request->getCheck( 'wpUploadIgnoreWarning' ) );
100
101 // Guess the desired name from the filename if not provided
102 $this->mDesiredDestName = $request->getText( 'wpDestFile' );
103 if ( !$this->mDesiredDestName && $request->getFileName( 'wpUploadFile' ) !== null ) {
104 $this->mDesiredDestName = $request->getFileName( 'wpUploadFile' );
105 }
106 $this->mLicense = $request->getText( 'wpLicense' );
107
108 $this->mDestWarningAck = $request->getText( 'wpDestFileWarningAck' );
109 $this->mIgnoreWarning = $request->getCheck( 'wpIgnoreWarning' )
110 || $request->getCheck( 'wpUploadIgnoreWarning' );
111 $this->mWatchthis = $request->getBool( 'wpWatchthis' ) && $this->getUser()->isLoggedIn();
112 $this->mCopyrightStatus = $request->getText( 'wpUploadCopyStatus' );
113 $this->mCopyrightSource = $request->getText( 'wpUploadSource' );
114
115 $this->mForReUpload = $request->getBool( 'wpForReUpload' ); // updating a file
116
117 $commentDefault = '';
118 $commentMsg = wfMessage( 'upload-default-description' )->inContentLanguage();
119 if ( !$this->mForReUpload && !$commentMsg->isDisabled() ) {
120 $commentDefault = $commentMsg->plain();
121 }
122 $this->mComment = $request->getText( 'wpUploadDescription', $commentDefault );
123
124 $this->mCancelUpload = $request->getCheck( 'wpCancelUpload' )
125 || $request->getCheck( 'wpReUpload' ); // b/w compat
126
127 // If it was posted check for the token (no remote POST'ing with user credentials)
128 $token = $request->getVal( 'wpEditToken' );
129 $this->mTokenOk = $this->getUser()->matchEditToken( $token );
130
131 $this->uploadFormTextTop = '';
132 $this->uploadFormTextAfterSummary = '';
133 }
134
135 /**
136 * This page can be shown if uploading is enabled.
137 * Handle permission checking elsewhere in order to be able to show
138 * custom error messages.
139 *
140 * @param User $user
141 * @return bool
142 */
143 public function userCanExecute( User $user ) {
144 return UploadBase::isEnabled() && parent::userCanExecute( $user );
145 }
146
147 /**
148 * Special page entry point
149 * @param string $par
150 * @throws ErrorPageError
151 * @throws Exception
152 * @throws FatalError
153 * @throws MWException
154 * @throws PermissionsError
155 * @throws ReadOnlyError
156 * @throws UserBlockedError
157 */
158 public function execute( $par ) {
159 $this->useTransactionalTimeLimit();
160
161 $this->setHeaders();
162 $this->outputHeader();
163
164 # Check uploading enabled
165 if ( !UploadBase::isEnabled() ) {
166 throw new ErrorPageError( 'uploaddisabled', 'uploaddisabledtext' );
167 }
168
169 $this->addHelpLink( 'Help:Managing files' );
170
171 # Check permissions
172 $user = $this->getUser();
173 $permissionRequired = UploadBase::isAllowed( $user );
174 if ( $permissionRequired !== true ) {
175 throw new PermissionsError( $permissionRequired );
176 }
177
178 # Check blocks
179 if ( $user->isBlocked() ) {
180 throw new UserBlockedError( $user->getBlock() );
181 }
182
183 // Global blocks
184 if ( $user->isBlockedGlobally() ) {
185 throw new UserBlockedError( $user->getGlobalBlock() );
186 }
187
188 # Check whether we actually want to allow changing stuff
189 $this->checkReadOnly();
190
191 $this->loadRequest();
192
193 # Unsave the temporary file in case this was a cancelled upload
194 if ( $this->mCancelUpload ) {
195 if ( !$this->unsaveUploadedFile() ) {
196 # Something went wrong, so unsaveUploadedFile showed a warning
197 return;
198 }
199 }
200
201 # Process upload or show a form
202 if (
203 $this->mTokenOk && !$this->mCancelUpload &&
204 ( $this->mUpload && $this->mUploadClicked )
205 ) {
206 $this->processUpload();
207 } else {
208 # Backwards compatibility hook
209 if ( !Hooks::run( 'UploadForm:initial', [ &$this ] ) ) {
210 wfDebug( "Hook 'UploadForm:initial' broke output of the upload form\n" );
211
212 return;
213 }
214 $this->showUploadForm( $this->getUploadForm() );
215 }
216
217 # Cleanup
218 if ( $this->mUpload ) {
219 $this->mUpload->cleanupTempFile();
220 }
221 }
222
223 /**
224 * Show the main upload form
225 *
226 * @param HTMLForm|string $form An HTMLForm instance or HTML string to show
227 */
228 protected function showUploadForm( $form ) {
229 # Add links if file was previously deleted
230 if ( $this->mDesiredDestName ) {
231 $this->showViewDeletedLinks();
232 }
233
234 if ( $form instanceof HTMLForm ) {
235 $form->show();
236 } else {
237 $this->getOutput()->addHTML( $form );
238 }
239 }
240
241 /**
242 * Get an UploadForm instance with title and text properly set.
243 *
244 * @param string $message HTML string to add to the form
245 * @param string $sessionKey Session key in case this is a stashed upload
246 * @param bool $hideIgnoreWarning Whether to hide "ignore warning" check box
247 * @return UploadForm
248 */
249 protected function getUploadForm( $message = '', $sessionKey = '', $hideIgnoreWarning = false ) {
250 # Initialize form
251 $context = new DerivativeContext( $this->getContext() );
252 $context->setTitle( $this->getPageTitle() ); // Remove subpage
253 $form = new UploadForm( [
254 'watch' => $this->getWatchCheck(),
255 'forreupload' => $this->mForReUpload,
256 'sessionkey' => $sessionKey,
257 'hideignorewarning' => $hideIgnoreWarning,
258 'destwarningack' => (bool)$this->mDestWarningAck,
259
260 'description' => $this->mComment,
261 'texttop' => $this->uploadFormTextTop,
262 'textaftersummary' => $this->uploadFormTextAfterSummary,
263 'destfile' => $this->mDesiredDestName,
264 ], $context );
265
266 # Check the token, but only if necessary
267 if (
268 !$this->mTokenOk && !$this->mCancelUpload &&
269 ( $this->mUpload && $this->mUploadClicked )
270 ) {
271 $form->addPreText( $this->msg( 'session_fail_preview' )->parse() );
272 }
273
274 # Give a notice if the user is uploading a file that has been deleted or moved
275 # Note that this is independent from the message 'filewasdeleted'
276 $desiredTitleObj = Title::makeTitleSafe( NS_FILE, $this->mDesiredDestName );
277 $delNotice = ''; // empty by default
278 if ( $desiredTitleObj instanceof Title && !$desiredTitleObj->exists() ) {
279 LogEventsList::showLogExtract( $delNotice, [ 'delete', 'move' ],
280 $desiredTitleObj,
281 '', [ 'lim' => 10,
282 'conds' => [ "log_action != 'revision'" ],
283 'showIfEmpty' => false,
284 'msgKey' => [ 'upload-recreate-warning' ] ]
285 );
286 }
287 $form->addPreText( $delNotice );
288
289 # Add text to form
290 $form->addPreText( '<div id="uploadtext">' .
291 $this->msg( 'uploadtext', [ $this->mDesiredDestName ] )->parseAsBlock() .
292 '</div>' );
293 # Add upload error message
294 $form->addPreText( $message );
295
296 # Add footer to form
297 $uploadFooter = $this->msg( 'uploadfooter' );
298 if ( !$uploadFooter->isDisabled() ) {
299 $form->addPostText( '<div id="mw-upload-footer-message">'
300 . $uploadFooter->parseAsBlock() . "</div>\n" );
301 }
302
303 return $form;
304 }
305
306 /**
307 * Shows the "view X deleted revivions link""
308 */
309 protected function showViewDeletedLinks() {
310 $title = Title::makeTitleSafe( NS_FILE, $this->mDesiredDestName );
311 $user = $this->getUser();
312 // Show a subtitle link to deleted revisions (to sysops et al only)
313 if ( $title instanceof Title ) {
314 $count = $title->isDeleted();
315 if ( $count > 0 && $user->isAllowed( 'deletedhistory' ) ) {
316 $restorelink = Linker::linkKnown(
317 SpecialPage::getTitleFor( 'Undelete', $title->getPrefixedText() ),
318 $this->msg( 'restorelink' )->numParams( $count )->escaped()
319 );
320 $link = $this->msg( $user->isAllowed( 'delete' ) ? 'thisisdeleted' : 'viewdeleted' )
321 ->rawParams( $restorelink )->parseAsBlock();
322 $this->getOutput()->addHTML( "<div id=\"contentSub2\">{$link}</div>" );
323 }
324 }
325 }
326
327 /**
328 * Stashes the upload and shows the main upload form.
329 *
330 * Note: only errors that can be handled by changing the name or
331 * description should be redirected here. It should be assumed that the
332 * file itself is sane and has passed UploadBase::verifyFile. This
333 * essentially means that UploadBase::VERIFICATION_ERROR and
334 * UploadBase::EMPTY_FILE should not be passed here.
335 *
336 * @param string $message HTML message to be passed to mainUploadForm
337 */
338 protected function showRecoverableUploadError( $message ) {
339 $sessionKey = $this->mUpload->stashSession();
340 $message = '<h2>' . $this->msg( 'uploaderror' )->escaped() . "</h2>\n" .
341 '<div class="error">' . $message . "</div>\n";
342
343 $form = $this->getUploadForm( $message, $sessionKey );
344 $form->setSubmitText( $this->msg( 'upload-tryagain' )->escaped() );
345 $this->showUploadForm( $form );
346 }
347
348 /**
349 * Stashes the upload, shows the main form, but adds a "continue anyway button".
350 * Also checks whether there are actually warnings to display.
351 *
352 * @param array $warnings
353 * @return bool True if warnings were displayed, false if there are no
354 * warnings and it should continue processing
355 */
356 protected function showUploadWarning( $warnings ) {
357 # If there are no warnings, or warnings we can ignore, return early.
358 # mDestWarningAck is set when some javascript has shown the warning
359 # to the user. mForReUpload is set when the user clicks the "upload a
360 # new version" link.
361 if ( !$warnings || ( count( $warnings ) == 1
362 && isset( $warnings['exists'] )
363 && ( $this->mDestWarningAck || $this->mForReUpload ) )
364 ) {
365 return false;
366 }
367
368 $sessionKey = $this->mUpload->stashSession();
369
370 // Add styles for the warning, reused from the live preview
371 $this->getOutput()->addModuleStyles( 'mediawiki.special.upload.styles' );
372
373 $warningHtml = '<h2>' . $this->msg( 'uploadwarning' )->escaped() . "</h2>\n"
374 . '<div class="mw-destfile-warning"><ul>';
375 foreach ( $warnings as $warning => $args ) {
376 if ( $warning == 'badfilename' ) {
377 $this->mDesiredDestName = Title::makeTitle( NS_FILE, $args )->getText();
378 }
379 if ( $warning == 'exists' ) {
380 $msg = "\t<li>" . self::getExistsWarning( $args ) . "</li>\n";
381 } elseif ( $warning == 'was-deleted' ) {
382 # If the file existed before and was deleted, warn the user of this
383 $ltitle = SpecialPage::getTitleFor( 'Log' );
384 $llink = Linker::linkKnown(
385 $ltitle,
386 wfMessage( 'deletionlog' )->escaped(),
387 [],
388 [
389 'type' => 'delete',
390 'page' => Title::makeTitle( NS_FILE, $args )->getPrefixedText(),
391 ]
392 );
393 $msg = "\t<li>" . wfMessage( 'filewasdeleted' )->rawParams( $llink )->parse() . "</li>\n";
394 } elseif ( $warning == 'duplicate' ) {
395 $msg = $this->getDupeWarning( $args );
396 } elseif ( $warning == 'duplicate-archive' ) {
397 if ( $args === '' ) {
398 $msg = "\t<li>" . $this->msg( 'file-deleted-duplicate-notitle' )->parse()
399 . "</li>\n";
400 } else {
401 $msg = "\t<li>" . $this->msg( 'file-deleted-duplicate',
402 Title::makeTitle( NS_FILE, $args )->getPrefixedText() )->parse()
403 . "</li>\n";
404 }
405 } else {
406 if ( $args === true ) {
407 $args = [];
408 } elseif ( !is_array( $args ) ) {
409 $args = [ $args ];
410 }
411 $msg = "\t<li>" . $this->msg( $warning, $args )->parse() . "</li>\n";
412 }
413 $warningHtml .= $msg;
414 }
415 $warningHtml .= "</ul></div>\n";
416 $warningHtml .= $this->msg( 'uploadwarning-text' )->parseAsBlock();
417
418 $form = $this->getUploadForm( $warningHtml, $sessionKey, /* $hideIgnoreWarning */ true );
419 $form->setSubmitText( $this->msg( 'upload-tryagain' )->text() );
420 $form->addButton( [
421 'name' => 'wpUploadIgnoreWarning',
422 'value' => $this->msg( 'ignorewarning' )->text()
423 ] );
424 $form->addButton( [
425 'name' => 'wpCancelUpload',
426 'value' => $this->msg( 'reuploaddesc' )->text()
427 ] );
428
429 $this->showUploadForm( $form );
430
431 # Indicate that we showed a form
432 return true;
433 }
434
435 /**
436 * Show the upload form with error message, but do not stash the file.
437 *
438 * @param string $message HTML string
439 */
440 protected function showUploadError( $message ) {
441 $message = '<h2>' . $this->msg( 'uploadwarning' )->escaped() . "</h2>\n" .
442 '<div class="error">' . $message . "</div>\n";
443 $this->showUploadForm( $this->getUploadForm( $message ) );
444 }
445
446 /**
447 * Do the upload.
448 * Checks are made in SpecialUpload::execute()
449 */
450 protected function processUpload() {
451 // Fetch the file if required
452 $status = $this->mUpload->fetchFile();
453 if ( !$status->isOK() ) {
454 $this->showUploadError( $this->getOutput()->parse( $status->getWikiText() ) );
455
456 return;
457 }
458
459 if ( !Hooks::run( 'UploadForm:BeforeProcessing', [ &$this ] ) ) {
460 wfDebug( "Hook 'UploadForm:BeforeProcessing' broke processing the file.\n" );
461 // This code path is deprecated. If you want to break upload processing
462 // do so by hooking into the appropriate hooks in UploadBase::verifyUpload
463 // and UploadBase::verifyFile.
464 // If you use this hook to break uploading, the user will be returned
465 // an empty form with no error message whatsoever.
466 return;
467 }
468
469 // Upload verification
470 $details = $this->mUpload->verifyUpload();
471 if ( $details['status'] != UploadBase::OK ) {
472 $this->processVerificationError( $details );
473
474 return;
475 }
476
477 // Verify permissions for this title
478 $permErrors = $this->mUpload->verifyTitlePermissions( $this->getUser() );
479 if ( $permErrors !== true ) {
480 $code = array_shift( $permErrors[0] );
481 $this->showRecoverableUploadError( $this->msg( $code, $permErrors[0] )->parse() );
482
483 return;
484 }
485
486 $this->mLocalFile = $this->mUpload->getLocalFile();
487
488 // Check warnings if necessary
489 if ( !$this->mIgnoreWarning ) {
490 $warnings = $this->mUpload->checkWarnings();
491 if ( $this->showUploadWarning( $warnings ) ) {
492 return;
493 }
494 }
495
496 // This is as late as we can throttle, after expected issues have been handled
497 if ( UploadBase::isThrottled( $this->getUser() ) ) {
498 $this->showRecoverableUploadError(
499 $this->msg( 'actionthrottledtext' )->escaped()
500 );
501 return;
502 }
503
504 // Get the page text if this is not a reupload
505 if ( !$this->mForReUpload ) {
506 $pageText = self::getInitialPageText( $this->mComment, $this->mLicense,
507 $this->mCopyrightStatus, $this->mCopyrightSource, $this->getConfig() );
508 } else {
509 $pageText = false;
510 }
511
512 $changeTags = $this->getRequest()->getVal( 'wpChangeTags' );
513 if ( is_null( $changeTags ) || $changeTags === '' ) {
514 $changeTags = [];
515 } else {
516 $changeTags = array_filter( array_map( 'trim', explode( ',', $changeTags ) ) );
517 }
518
519 if ( $changeTags ) {
520 $changeTagsStatus = ChangeTags::canAddTagsAccompanyingChange(
521 $changeTags, $this->getUser() );
522 if ( !$changeTagsStatus->isOK() ) {
523 $this->showUploadError( $this->getOutput()->parse( $changeTagsStatus->getWikiText() ) );
524
525 return;
526 }
527 }
528
529 $status = $this->mUpload->performUpload(
530 $this->mComment,
531 $pageText,
532 $this->mWatchthis,
533 $this->getUser(),
534 $changeTags
535 );
536
537 if ( !$status->isGood() ) {
538 $this->showUploadError( $this->getOutput()->parse( $status->getWikiText() ) );
539
540 return;
541 }
542
543 // Success, redirect to description page
544 $this->mUploadSuccessful = true;
545 Hooks::run( 'SpecialUploadComplete', [ &$this ] );
546 $this->getOutput()->redirect( $this->mLocalFile->getTitle()->getFullURL() );
547 }
548
549 /**
550 * Get the initial image page text based on a comment and optional file status information
551 * @param string $comment
552 * @param string $license
553 * @param string $copyStatus
554 * @param string $source
555 * @param Config $config Configuration object to load data from
556 * @return string
557 */
558 public static function getInitialPageText( $comment = '', $license = '',
559 $copyStatus = '', $source = '', Config $config = null
560 ) {
561 if ( $config === null ) {
562 wfDebug( __METHOD__ . ' called without a Config instance passed to it' );
563 $config = ConfigFactory::getDefaultInstance()->makeConfig( 'main' );
564 }
565
566 $msg = [];
567 $forceUIMsgAsContentMsg = (array)$config->get( 'ForceUIMsgAsContentMsg' );
568 /* These messages are transcluded into the actual text of the description page.
569 * Thus, forcing them as content messages makes the upload to produce an int: template
570 * instead of hardcoding it there in the uploader language.
571 */
572 foreach ( [ 'license-header', 'filedesc', 'filestatus', 'filesource' ] as $msgName ) {
573 if ( in_array( $msgName, $forceUIMsgAsContentMsg ) ) {
574 $msg[$msgName] = "{{int:$msgName}}";
575 } else {
576 $msg[$msgName] = wfMessage( $msgName )->inContentLanguage()->text();
577 }
578 }
579
580 if ( $config->get( 'UseCopyrightUpload' ) ) {
581 $licensetxt = '';
582 if ( $license != '' ) {
583 $licensetxt = '== ' . $msg['license-header'] . " ==\n" . '{{' . $license . '}}' . "\n";
584 }
585 $pageText = '== ' . $msg['filedesc'] . " ==\n" . $comment . "\n" .
586 '== ' . $msg['filestatus'] . " ==\n" . $copyStatus . "\n" .
587 "$licensetxt" .
588 '== ' . $msg['filesource'] . " ==\n" . $source;
589 } else {
590 if ( $license != '' ) {
591 $filedesc = $comment == '' ? '' : '== ' . $msg['filedesc'] . " ==\n" . $comment . "\n";
592 $pageText = $filedesc .
593 '== ' . $msg['license-header'] . " ==\n" . '{{' . $license . '}}' . "\n";
594 } else {
595 $pageText = $comment;
596 }
597 }
598
599 return $pageText;
600 }
601
602 /**
603 * See if we should check the 'watch this page' checkbox on the form
604 * based on the user's preferences and whether we're being asked
605 * to create a new file or update an existing one.
606 *
607 * In the case where 'watch edits' is off but 'watch creations' is on,
608 * we'll leave the box unchecked.
609 *
610 * Note that the page target can be changed *on the form*, so our check
611 * state can get out of sync.
612 * @return bool|string
613 */
614 protected function getWatchCheck() {
615 if ( $this->getUser()->getOption( 'watchdefault' ) ) {
616 // Watch all edits!
617 return true;
618 }
619
620 $desiredTitleObj = Title::makeTitleSafe( NS_FILE, $this->mDesiredDestName );
621 if ( $desiredTitleObj instanceof Title && $this->getUser()->isWatched( $desiredTitleObj ) ) {
622 // Already watched, don't change that
623 return true;
624 }
625
626 $local = wfLocalFile( $this->mDesiredDestName );
627 if ( $local && $local->exists() ) {
628 // We're uploading a new version of an existing file.
629 // No creation, so don't watch it if we're not already.
630 return false;
631 } else {
632 // New page should get watched if that's our option.
633 return $this->getUser()->getOption( 'watchcreations' ) ||
634 $this->getUser()->getOption( 'watchuploads' );
635 }
636 }
637
638 /**
639 * Provides output to the user for a result of UploadBase::verifyUpload
640 *
641 * @param array $details Result of UploadBase::verifyUpload
642 * @throws MWException
643 */
644 protected function processVerificationError( $details ) {
645 switch ( $details['status'] ) {
646
647 /** Statuses that only require name changing **/
648 case UploadBase::MIN_LENGTH_PARTNAME:
649 $this->showRecoverableUploadError( $this->msg( 'minlength1' )->escaped() );
650 break;
651 case UploadBase::ILLEGAL_FILENAME:
652 $this->showRecoverableUploadError( $this->msg( 'illegalfilename',
653 $details['filtered'] )->parse() );
654 break;
655 case UploadBase::FILENAME_TOO_LONG:
656 $this->showRecoverableUploadError( $this->msg( 'filename-toolong' )->escaped() );
657 break;
658 case UploadBase::FILETYPE_MISSING:
659 $this->showRecoverableUploadError( $this->msg( 'filetype-missing' )->parse() );
660 break;
661 case UploadBase::WINDOWS_NONASCII_FILENAME:
662 $this->showRecoverableUploadError( $this->msg( 'windows-nonascii-filename' )->parse() );
663 break;
664
665 /** Statuses that require reuploading **/
666 case UploadBase::EMPTY_FILE:
667 $this->showUploadError( $this->msg( 'emptyfile' )->escaped() );
668 break;
669 case UploadBase::FILE_TOO_LARGE:
670 $this->showUploadError( $this->msg( 'largefileserver' )->escaped() );
671 break;
672 case UploadBase::FILETYPE_BADTYPE:
673 $msg = $this->msg( 'filetype-banned-type' );
674 if ( isset( $details['blacklistedExt'] ) ) {
675 $msg->params( $this->getLanguage()->commaList( $details['blacklistedExt'] ) );
676 } else {
677 $msg->params( $details['finalExt'] );
678 }
679 $extensions = array_unique( $this->getConfig()->get( 'FileExtensions' ) );
680 $msg->params( $this->getLanguage()->commaList( $extensions ),
681 count( $extensions ) );
682
683 // Add PLURAL support for the first parameter. This results
684 // in a bit unlogical parameter sequence, but does not break
685 // old translations
686 if ( isset( $details['blacklistedExt'] ) ) {
687 $msg->params( count( $details['blacklistedExt'] ) );
688 } else {
689 $msg->params( 1 );
690 }
691
692 $this->showUploadError( $msg->parse() );
693 break;
694 case UploadBase::VERIFICATION_ERROR:
695 unset( $details['status'] );
696 $code = array_shift( $details['details'] );
697 $this->showUploadError( $this->msg( $code, $details['details'] )->parse() );
698 break;
699 case UploadBase::HOOK_ABORTED:
700 if ( is_array( $details['error'] ) ) { # allow hooks to return error details in an array
701 $args = $details['error'];
702 $error = array_shift( $args );
703 } else {
704 $error = $details['error'];
705 $args = null;
706 }
707
708 $this->showUploadError( $this->msg( $error, $args )->parse() );
709 break;
710 default:
711 throw new MWException( __METHOD__ . ": Unknown value `{$details['status']}`" );
712 }
713 }
714
715 /**
716 * Remove a temporarily kept file stashed by saveTempUploadedFile().
717 *
718 * @return bool Success
719 */
720 protected function unsaveUploadedFile() {
721 if ( !( $this->mUpload instanceof UploadFromStash ) ) {
722 return true;
723 }
724 $success = $this->mUpload->unsaveUploadedFile();
725 if ( !$success ) {
726 $this->getOutput()->showFileDeleteError( $this->mUpload->getTempPath() );
727
728 return false;
729 } else {
730 return true;
731 }
732 }
733
734 /*** Functions for formatting warnings ***/
735
736 /**
737 * Formats a result of UploadBase::getExistsWarning as HTML
738 * This check is static and can be done pre-upload via AJAX
739 *
740 * @param array $exists The result of UploadBase::getExistsWarning
741 * @return string Empty string if there is no warning or an HTML fragment
742 */
743 public static function getExistsWarning( $exists ) {
744 if ( !$exists ) {
745 return '';
746 }
747
748 $file = $exists['file'];
749 $filename = $file->getTitle()->getPrefixedText();
750 $warning = '';
751
752 if ( $exists['warning'] == 'exists' ) {
753 // Exact match
754 $warning = wfMessage( 'fileexists', $filename )->parse();
755 } elseif ( $exists['warning'] == 'page-exists' ) {
756 // Page exists but file does not
757 $warning = wfMessage( 'filepageexists', $filename )->parse();
758 } elseif ( $exists['warning'] == 'exists-normalized' ) {
759 $warning = wfMessage( 'fileexists-extension', $filename,
760 $exists['normalizedFile']->getTitle()->getPrefixedText() )->parse();
761 } elseif ( $exists['warning'] == 'thumb' ) {
762 // Swapped argument order compared with other messages for backwards compatibility
763 $warning = wfMessage( 'fileexists-thumbnail-yes',
764 $exists['thumbFile']->getTitle()->getPrefixedText(), $filename )->parse();
765 } elseif ( $exists['warning'] == 'thumb-name' ) {
766 // Image w/o '180px-' does not exists, but we do not like these filenames
767 $name = $file->getName();
768 $badPart = substr( $name, 0, strpos( $name, '-' ) + 1 );
769 $warning = wfMessage( 'file-thumbnail-no', $badPart )->parse();
770 } elseif ( $exists['warning'] == 'bad-prefix' ) {
771 $warning = wfMessage( 'filename-bad-prefix', $exists['prefix'] )->parse();
772 }
773
774 return $warning;
775 }
776
777 /**
778 * Construct a warning and a gallery from an array of duplicate files.
779 * @param array $dupes
780 * @return string
781 */
782 public function getDupeWarning( $dupes ) {
783 if ( !$dupes ) {
784 return '';
785 }
786
787 $gallery = ImageGalleryBase::factory( false, $this->getContext() );
788 $gallery->setShowBytes( false );
789 foreach ( $dupes as $file ) {
790 $gallery->add( $file->getTitle() );
791 }
792
793 return '<li>' .
794 $this->msg( 'file-exists-duplicate' )->numParams( count( $dupes ) )->parse() .
795 $gallery->toHTML() . "</li>\n";
796 }
797
798 protected function getGroupName() {
799 return 'media';
800 }
801
802 /**
803 * Should we rotate images in the preview on Special:Upload.
804 *
805 * This controls js: mw.config.get( 'wgFileCanRotate' )
806 *
807 * @todo What about non-BitmapHandler handled files?
808 */
809 public static function rotationEnabled() {
810 $bitmapHandler = new BitmapHandler();
811 return $bitmapHandler->autoRotateEnabled();
812 }
813 }
814
815 /**
816 * Sub class of HTMLForm that provides the form section of SpecialUpload
817 */
818 class UploadForm extends HTMLForm {
819 protected $mWatch;
820 protected $mForReUpload;
821 protected $mSessionKey;
822 protected $mHideIgnoreWarning;
823 protected $mDestWarningAck;
824 protected $mDestFile;
825
826 protected $mComment;
827 protected $mTextTop;
828 protected $mTextAfterSummary;
829
830 protected $mSourceIds;
831
832 protected $mMaxFileSize = [];
833
834 protected $mMaxUploadSize = [];
835
836 public function __construct( array $options = [], IContextSource $context = null ) {
837 if ( $context instanceof IContextSource ) {
838 $this->setContext( $context );
839 }
840
841 $this->mWatch = !empty( $options['watch'] );
842 $this->mForReUpload = !empty( $options['forreupload'] );
843 $this->mSessionKey = isset( $options['sessionkey'] ) ? $options['sessionkey'] : '';
844 $this->mHideIgnoreWarning = !empty( $options['hideignorewarning'] );
845 $this->mDestWarningAck = !empty( $options['destwarningack'] );
846 $this->mDestFile = isset( $options['destfile'] ) ? $options['destfile'] : '';
847
848 $this->mComment = isset( $options['description'] ) ?
849 $options['description'] : '';
850
851 $this->mTextTop = isset( $options['texttop'] )
852 ? $options['texttop'] : '';
853
854 $this->mTextAfterSummary = isset( $options['textaftersummary'] )
855 ? $options['textaftersummary'] : '';
856
857 $sourceDescriptor = $this->getSourceSection();
858 $descriptor = $sourceDescriptor
859 + $this->getDescriptionSection()
860 + $this->getOptionsSection();
861
862 Hooks::run( 'UploadFormInitDescriptor', [ &$descriptor ] );
863 parent::__construct( $descriptor, $context, 'upload' );
864
865 # Add a link to edit MediaWik:Licenses
866 if ( $this->getUser()->isAllowed( 'editinterface' ) ) {
867 $this->getOutput()->addModuleStyles( 'mediawiki.special' );
868 $licensesLink = Linker::linkKnown(
869 $this->msg( 'licenses' )->inContentLanguage()->getTitle(),
870 $this->msg( 'licenses-edit' )->escaped(),
871 [],
872 [ 'action' => 'edit' ]
873 );
874 $editLicenses = '<p class="mw-upload-editlicenses">' . $licensesLink . '</p>';
875 $this->addFooterText( $editLicenses, 'description' );
876 }
877
878 # Set some form properties
879 $this->setSubmitText( $this->msg( 'uploadbtn' )->text() );
880 $this->setSubmitName( 'wpUpload' );
881 # Used message keys: 'accesskey-upload', 'tooltip-upload'
882 $this->setSubmitTooltip( 'upload' );
883 $this->setId( 'mw-upload-form' );
884
885 # Build a list of IDs for javascript insertion
886 $this->mSourceIds = [];
887 foreach ( $sourceDescriptor as $field ) {
888 if ( !empty( $field['id'] ) ) {
889 $this->mSourceIds[] = $field['id'];
890 }
891 }
892 }
893
894 /**
895 * Get the descriptor of the fieldset that contains the file source
896 * selection. The section is 'source'
897 *
898 * @return array Descriptor array
899 */
900 protected function getSourceSection() {
901 if ( $this->mSessionKey ) {
902 return [
903 'SessionKey' => [
904 'type' => 'hidden',
905 'default' => $this->mSessionKey,
906 ],
907 'SourceType' => [
908 'type' => 'hidden',
909 'default' => 'Stash',
910 ],
911 ];
912 }
913
914 $canUploadByUrl = UploadFromUrl::isEnabled()
915 && ( UploadFromUrl::isAllowed( $this->getUser() ) === true )
916 && $this->getConfig()->get( 'CopyUploadsFromSpecialUpload' );
917 $radio = $canUploadByUrl;
918 $selectedSourceType = strtolower( $this->getRequest()->getText( 'wpSourceType', 'File' ) );
919
920 $descriptor = [];
921 if ( $this->mTextTop ) {
922 $descriptor['UploadFormTextTop'] = [
923 'type' => 'info',
924 'section' => 'source',
925 'default' => $this->mTextTop,
926 'raw' => true,
927 ];
928 }
929
930 $this->mMaxUploadSize['file'] = min(
931 UploadBase::getMaxUploadSize( 'file' ),
932 UploadBase::getMaxPhpUploadSize()
933 );
934
935 $help = $this->msg( 'upload-maxfilesize',
936 $this->getContext()->getLanguage()->formatSize( $this->mMaxUploadSize['file'] )
937 )->parse();
938
939 // If the user can also upload by URL, there are 2 different file size limits.
940 // This extra message helps stress which limit corresponds to what.
941 if ( $canUploadByUrl ) {
942 $help .= $this->msg( 'word-separator' )->escaped();
943 $help .= $this->msg( 'upload_source_file' )->parse();
944 }
945
946 $descriptor['UploadFile'] = [
947 'class' => 'UploadSourceField',
948 'section' => 'source',
949 'type' => 'file',
950 'id' => 'wpUploadFile',
951 'radio-id' => 'wpSourceTypeFile',
952 'label-message' => 'sourcefilename',
953 'upload-type' => 'File',
954 'radio' => &$radio,
955 'help' => $help,
956 'checked' => $selectedSourceType == 'file',
957 ];
958
959 if ( $canUploadByUrl ) {
960 $this->mMaxUploadSize['url'] = UploadBase::getMaxUploadSize( 'url' );
961 $descriptor['UploadFileURL'] = [
962 'class' => 'UploadSourceField',
963 'section' => 'source',
964 'id' => 'wpUploadFileURL',
965 'radio-id' => 'wpSourceTypeurl',
966 'label-message' => 'sourceurl',
967 'upload-type' => 'url',
968 'radio' => &$radio,
969 'help' => $this->msg( 'upload-maxfilesize',
970 $this->getContext()->getLanguage()->formatSize( $this->mMaxUploadSize['url'] )
971 )->parse() .
972 $this->msg( 'word-separator' )->escaped() .
973 $this->msg( 'upload_source_url' )->parse(),
974 'checked' => $selectedSourceType == 'url',
975 ];
976 }
977 Hooks::run( 'UploadFormSourceDescriptors', [ &$descriptor, &$radio, $selectedSourceType ] );
978
979 $descriptor['Extensions'] = [
980 'type' => 'info',
981 'section' => 'source',
982 'default' => $this->getExtensionsMessage(),
983 'raw' => true,
984 ];
985
986 return $descriptor;
987 }
988
989 /**
990 * Get the messages indicating which extensions are preferred and prohibitted.
991 *
992 * @return string HTML string containing the message
993 */
994 protected function getExtensionsMessage() {
995 # Print a list of allowed file extensions, if so configured. We ignore
996 # MIME type here, it's incomprehensible to most people and too long.
997 $config = $this->getConfig();
998
999 if ( $config->get( 'CheckFileExtensions' ) ) {
1000 $fileExtensions = array_unique( $config->get( 'FileExtensions' ) );
1001 if ( $config->get( 'StrictFileExtensions' ) ) {
1002 # Everything not permitted is banned
1003 $extensionsList =
1004 '<div id="mw-upload-permitted">' .
1005 $this->msg( 'upload-permitted' )
1006 ->params( $this->getLanguage()->commaList( $fileExtensions ) )
1007 ->numParams( count( $fileExtensions ) )
1008 ->parseAsBlock() .
1009 "</div>\n";
1010 } else {
1011 # We have to list both preferred and prohibited
1012 $fileBlacklist = array_unique( $config->get( 'FileBlacklist' ) );
1013 $extensionsList =
1014 '<div id="mw-upload-preferred">' .
1015 $this->msg( 'upload-preferred' )
1016 ->params( $this->getLanguage()->commaList( $fileExtensions ) )
1017 ->numParams( count( $fileExtensions ) )
1018 ->parseAsBlock() .
1019 "</div>\n" .
1020 '<div id="mw-upload-prohibited">' .
1021 $this->msg( 'upload-prohibited' )
1022 ->params( $this->getLanguage()->commaList( $fileBlacklist ) )
1023 ->numParams( count( $fileBlacklist ) )
1024 ->parseAsBlock() .
1025 "</div>\n";
1026 }
1027 } else {
1028 # Everything is permitted.
1029 $extensionsList = '';
1030 }
1031
1032 return $extensionsList;
1033 }
1034
1035 /**
1036 * Get the descriptor of the fieldset that contains the file description
1037 * input. The section is 'description'
1038 *
1039 * @return array Descriptor array
1040 */
1041 protected function getDescriptionSection() {
1042 $config = $this->getConfig();
1043 if ( $this->mSessionKey ) {
1044 $stash = RepoGroup::singleton()->getLocalRepo()->getUploadStash( $this->getUser() );
1045 try {
1046 $file = $stash->getFile( $this->mSessionKey );
1047 } catch ( Exception $e ) {
1048 $file = null;
1049 }
1050 if ( $file ) {
1051 global $wgContLang;
1052
1053 $mto = $file->transform( [ 'width' => 120 ] );
1054 $this->addHeaderText(
1055 '<div class="thumb t' . $wgContLang->alignEnd() . '">' .
1056 Html::element( 'img', [
1057 'src' => $mto->getUrl(),
1058 'class' => 'thumbimage',
1059 ] ) . '</div>', 'description' );
1060 }
1061 }
1062
1063 $descriptor = [
1064 'DestFile' => [
1065 'type' => 'text',
1066 'section' => 'description',
1067 'id' => 'wpDestFile',
1068 'label-message' => 'destfilename',
1069 'size' => 60,
1070 'default' => $this->mDestFile,
1071 # @todo FIXME: Hack to work around poor handling of the 'default' option in HTMLForm
1072 'nodata' => strval( $this->mDestFile ) !== '',
1073 ],
1074 'UploadDescription' => [
1075 'type' => 'textarea',
1076 'section' => 'description',
1077 'id' => 'wpUploadDescription',
1078 'label-message' => $this->mForReUpload
1079 ? 'filereuploadsummary'
1080 : 'fileuploadsummary',
1081 'default' => $this->mComment,
1082 'cols' => $this->getUser()->getIntOption( 'cols' ),
1083 'rows' => 8,
1084 ]
1085 ];
1086 if ( $this->mTextAfterSummary ) {
1087 $descriptor['UploadFormTextAfterSummary'] = [
1088 'type' => 'info',
1089 'section' => 'description',
1090 'default' => $this->mTextAfterSummary,
1091 'raw' => true,
1092 ];
1093 }
1094
1095 $descriptor += [
1096 'EditTools' => [
1097 'type' => 'edittools',
1098 'section' => 'description',
1099 'message' => 'edittools-upload',
1100 ]
1101 ];
1102
1103 if ( $this->mForReUpload ) {
1104 $descriptor['DestFile']['readonly'] = true;
1105 } else {
1106 $descriptor['License'] = [
1107 'type' => 'select',
1108 'class' => 'Licenses',
1109 'section' => 'description',
1110 'id' => 'wpLicense',
1111 'label-message' => 'license',
1112 ];
1113 }
1114
1115 if ( $config->get( 'UseCopyrightUpload' ) ) {
1116 $descriptor['UploadCopyStatus'] = [
1117 'type' => 'text',
1118 'section' => 'description',
1119 'id' => 'wpUploadCopyStatus',
1120 'label-message' => 'filestatus',
1121 ];
1122 $descriptor['UploadSource'] = [
1123 'type' => 'text',
1124 'section' => 'description',
1125 'id' => 'wpUploadSource',
1126 'label-message' => 'filesource',
1127 ];
1128 }
1129
1130 return $descriptor;
1131 }
1132
1133 /**
1134 * Get the descriptor of the fieldset that contains the upload options,
1135 * such as "watch this file". The section is 'options'
1136 *
1137 * @return array Descriptor array
1138 */
1139 protected function getOptionsSection() {
1140 $user = $this->getUser();
1141 if ( $user->isLoggedIn() ) {
1142 $descriptor = [
1143 'Watchthis' => [
1144 'type' => 'check',
1145 'id' => 'wpWatchthis',
1146 'label-message' => 'watchthisupload',
1147 'section' => 'options',
1148 'default' => $this->mWatch,
1149 ]
1150 ];
1151 }
1152 if ( !$this->mHideIgnoreWarning ) {
1153 $descriptor['IgnoreWarning'] = [
1154 'type' => 'check',
1155 'id' => 'wpIgnoreWarning',
1156 'label-message' => 'ignorewarnings',
1157 'section' => 'options',
1158 ];
1159 }
1160
1161 $descriptor['DestFileWarningAck'] = [
1162 'type' => 'hidden',
1163 'id' => 'wpDestFileWarningAck',
1164 'default' => $this->mDestWarningAck ? '1' : '',
1165 ];
1166
1167 if ( $this->mForReUpload ) {
1168 $descriptor['ForReUpload'] = [
1169 'type' => 'hidden',
1170 'id' => 'wpForReUpload',
1171 'default' => '1',
1172 ];
1173 }
1174
1175 return $descriptor;
1176 }
1177
1178 /**
1179 * Add the upload JS and show the form.
1180 */
1181 public function show() {
1182 $this->addUploadJS();
1183 parent::show();
1184 }
1185
1186 /**
1187 * Add upload JS to the OutputPage
1188 */
1189 protected function addUploadJS() {
1190 $config = $this->getConfig();
1191
1192 $useAjaxDestCheck = $config->get( 'UseAjax' ) && $config->get( 'AjaxUploadDestCheck' );
1193 $useAjaxLicensePreview = $config->get( 'UseAjax' ) &&
1194 $config->get( 'AjaxLicensePreview' ) && $config->get( 'EnableAPI' );
1195 $this->mMaxUploadSize['*'] = UploadBase::getMaxUploadSize();
1196
1197 $scriptVars = [
1198 'wgAjaxUploadDestCheck' => $useAjaxDestCheck,
1199 'wgAjaxLicensePreview' => $useAjaxLicensePreview,
1200 'wgUploadAutoFill' => !$this->mForReUpload &&
1201 // If we received mDestFile from the request, don't autofill
1202 // the wpDestFile textbox
1203 $this->mDestFile === '',
1204 'wgUploadSourceIds' => $this->mSourceIds,
1205 'wgCheckFileExtensions' => $config->get( 'CheckFileExtensions' ),
1206 'wgStrictFileExtensions' => $config->get( 'StrictFileExtensions' ),
1207 'wgFileExtensions' => array_values( array_unique( $config->get( 'FileExtensions' ) ) ),
1208 'wgCapitalizeUploads' => MWNamespace::isCapitalized( NS_FILE ),
1209 'wgMaxUploadSize' => $this->mMaxUploadSize,
1210 'wgFileCanRotate' => SpecialUpload::rotationEnabled(),
1211 ];
1212
1213 $out = $this->getOutput();
1214 $out->addJsConfigVars( $scriptVars );
1215
1216 $out->addModules( [
1217 'mediawiki.action.edit', // For <charinsert> support
1218 'mediawiki.special.upload', // Extras for thumbnail and license preview.
1219 ] );
1220 }
1221
1222 /**
1223 * Empty function; submission is handled elsewhere.
1224 *
1225 * @return bool False
1226 */
1227 function trySubmit() {
1228 return false;
1229 }
1230 }
1231
1232 /**
1233 * A form field that contains a radio box in the label
1234 */
1235 class UploadSourceField extends HTMLTextField {
1236
1237 /**
1238 * @param array $cellAttributes
1239 * @return string
1240 */
1241 function getLabelHtml( $cellAttributes = [] ) {
1242 $id = $this->mParams['id'];
1243 $label = Html::rawElement( 'label', [ 'for' => $id ], $this->mLabel );
1244
1245 if ( !empty( $this->mParams['radio'] ) ) {
1246 if ( isset( $this->mParams['radio-id'] ) ) {
1247 $radioId = $this->mParams['radio-id'];
1248 } else {
1249 // Old way. For the benefit of extensions that do not define
1250 // the 'radio-id' key.
1251 $radioId = 'wpSourceType' . $this->mParams['upload-type'];
1252 }
1253
1254 $attribs = [
1255 'name' => 'wpSourceType',
1256 'type' => 'radio',
1257 'id' => $radioId,
1258 'value' => $this->mParams['upload-type'],
1259 ];
1260
1261 if ( !empty( $this->mParams['checked'] ) ) {
1262 $attribs['checked'] = 'checked';
1263 }
1264
1265 $label .= Html::element( 'input', $attribs );
1266 }
1267
1268 return Html::rawElement( 'td', [ 'class' => 'mw-label' ] + $cellAttributes, $label );
1269 }
1270
1271 /**
1272 * @return int
1273 */
1274 function getSize() {
1275 return isset( $this->mParams['size'] )
1276 ? $this->mParams['size']
1277 : 60;
1278 }
1279 }