3 * Implements Special:Upload
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.
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.
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
21 * @ingroup SpecialPage
25 use MediaWiki\MediaWikiServices
;
28 * Form for handling uploads and special page.
30 * @ingroup SpecialPage
33 class SpecialUpload
extends SpecialPage
{
35 * Get data POSTed through the form and assign them to the object
36 * @param WebRequest|null $request Data posted.
38 public function __construct( $request = null ) {
39 parent
::__construct( 'Upload', 'upload' );
42 public function doesWrites() {
46 /** Misc variables **/
48 /** @var WebRequest|FauxRequest The request this form is supposed to handle */
52 /** @var UploadBase */
57 public $mUploadClicked;
59 /** User input variables from the "description" section **/
61 /** @var string The requested target file name */
62 public $mDesiredDestName;
66 /** User input variables from the root section **/
68 public $mIgnoreWarning;
70 public $mCopyrightStatus;
71 public $mCopyrightSource;
73 /** Hidden variables **/
75 public $mDestWarningAck;
77 /** @var bool The user followed an "overwrite this file" link */
80 /** @var bool The user clicked "Cancel and return to upload form" button */
81 public $mCancelUpload;
84 /** @var bool Subclasses can use this to determine whether a file was uploaded */
85 public $mUploadSuccessful = false;
87 /** Text injection points for hooks not using HTMLForm **/
88 public $uploadFormTextTop;
89 public $uploadFormTextAfterSummary;
92 * Initialize instance variables from request and create an Upload handler
94 protected function loadRequest() {
95 $this->mRequest
= $request = $this->getRequest();
96 $this->mSourceType
= $request->getVal( 'wpSourceType', 'file' );
97 $this->mUpload
= UploadBase
::createFromRequest( $request );
98 $this->mUploadClicked
= $request->wasPosted()
99 && ( $request->getCheck( 'wpUpload' )
100 ||
$request->getCheck( 'wpUploadIgnoreWarning' ) );
102 // Guess the desired name from the filename if not provided
103 $this->mDesiredDestName
= $request->getText( 'wpDestFile' );
104 if ( !$this->mDesiredDestName
&& $request->getFileName( 'wpUploadFile' ) !== null ) {
105 $this->mDesiredDestName
= $request->getFileName( 'wpUploadFile' );
107 $this->mLicense
= $request->getText( 'wpLicense' );
109 $this->mDestWarningAck
= $request->getText( 'wpDestFileWarningAck' );
110 $this->mIgnoreWarning
= $request->getCheck( 'wpIgnoreWarning' )
111 ||
$request->getCheck( 'wpUploadIgnoreWarning' );
112 $this->mWatchthis
= $request->getBool( 'wpWatchthis' ) && $this->getUser()->isLoggedIn();
113 $this->mCopyrightStatus
= $request->getText( 'wpUploadCopyStatus' );
114 $this->mCopyrightSource
= $request->getText( 'wpUploadSource' );
116 $this->mForReUpload
= $request->getBool( 'wpForReUpload' ); // updating a file
118 $commentDefault = '';
119 $commentMsg = wfMessage( 'upload-default-description' )->inContentLanguage();
120 if ( !$this->mForReUpload
&& !$commentMsg->isDisabled() ) {
121 $commentDefault = $commentMsg->plain();
123 $this->mComment
= $request->getText( 'wpUploadDescription', $commentDefault );
125 $this->mCancelUpload
= $request->getCheck( 'wpCancelUpload' )
126 ||
$request->getCheck( 'wpReUpload' ); // b/w compat
128 // If it was posted check for the token (no remote POST'ing with user credentials)
129 $token = $request->getVal( 'wpEditToken' );
130 $this->mTokenOk
= $this->getUser()->matchEditToken( $token );
132 $this->uploadFormTextTop
= '';
133 $this->uploadFormTextAfterSummary
= '';
137 * This page can be shown if uploading is enabled.
138 * Handle permission checking elsewhere in order to be able to show
139 * custom error messages.
144 public function userCanExecute( User
$user ) {
145 return UploadBase
::isEnabled() && parent
::userCanExecute( $user );
149 * Special page entry point
151 * @throws ErrorPageError
154 * @throws MWException
155 * @throws PermissionsError
156 * @throws ReadOnlyError
157 * @throws UserBlockedError
159 public function execute( $par ) {
160 $this->useTransactionalTimeLimit();
163 $this->outputHeader();
165 # Check uploading enabled
166 if ( !UploadBase
::isEnabled() ) {
167 throw new ErrorPageError( 'uploaddisabled', 'uploaddisabledtext' );
170 $this->addHelpLink( 'Help:Managing files' );
173 $user = $this->getUser();
174 $permissionRequired = UploadBase
::isAllowed( $user );
175 if ( $permissionRequired !== true ) {
176 throw new PermissionsError( $permissionRequired );
180 if ( $user->isBlocked() ) {
181 throw new UserBlockedError( $user->getBlock() );
185 if ( $user->isBlockedGlobally() ) {
186 throw new UserBlockedError( $user->getGlobalBlock() );
189 # Check whether we actually want to allow changing stuff
190 $this->checkReadOnly();
192 $this->loadRequest();
194 # Unsave the temporary file in case this was a cancelled upload
195 if ( $this->mCancelUpload
) {
196 if ( !$this->unsaveUploadedFile() ) {
197 # Something went wrong, so unsaveUploadedFile showed a warning
202 # Process upload or show a form
204 $this->mTokenOk
&& !$this->mCancelUpload
&&
205 ( $this->mUpload
&& $this->mUploadClicked
)
207 $this->processUpload();
209 # Backwards compatibility hook
210 // Avoid PHP 7.1 warning of passing $this by reference
212 if ( !Hooks
::run( 'UploadForm:initial', [ &$upload ] ) ) {
213 wfDebug( "Hook 'UploadForm:initial' broke output of the upload form\n" );
217 $this->showUploadForm( $this->getUploadForm() );
221 if ( $this->mUpload
) {
222 $this->mUpload
->cleanupTempFile();
227 * Show the main upload form
229 * @param HTMLForm|string $form An HTMLForm instance or HTML string to show
231 protected function showUploadForm( $form ) {
232 # Add links if file was previously deleted
233 if ( $this->mDesiredDestName
) {
234 $this->showViewDeletedLinks();
237 if ( $form instanceof HTMLForm
) {
240 $this->getOutput()->addHTML( $form );
245 * Get an UploadForm instance with title and text properly set.
247 * @param string $message HTML string to add to the form
248 * @param string $sessionKey Session key in case this is a stashed upload
249 * @param bool $hideIgnoreWarning Whether to hide "ignore warning" check box
252 protected function getUploadForm( $message = '', $sessionKey = '', $hideIgnoreWarning = false ) {
254 $context = new DerivativeContext( $this->getContext() );
255 $context->setTitle( $this->getPageTitle() ); // Remove subpage
256 $form = new UploadForm( [
257 'watch' => $this->getWatchCheck(),
258 'forreupload' => $this->mForReUpload
,
259 'sessionkey' => $sessionKey,
260 'hideignorewarning' => $hideIgnoreWarning,
261 'destwarningack' => (bool)$this->mDestWarningAck
,
263 'description' => $this->mComment
,
264 'texttop' => $this->uploadFormTextTop
,
265 'textaftersummary' => $this->uploadFormTextAfterSummary
,
266 'destfile' => $this->mDesiredDestName
,
267 ], $context, $this->getLinkRenderer() );
269 # Check the token, but only if necessary
271 !$this->mTokenOk
&& !$this->mCancelUpload
&&
272 ( $this->mUpload
&& $this->mUploadClicked
)
274 $form->addPreText( $this->msg( 'session_fail_preview' )->parse() );
277 # Give a notice if the user is uploading a file that has been deleted or moved
278 # Note that this is independent from the message 'filewasdeleted'
279 $desiredTitleObj = Title
::makeTitleSafe( NS_FILE
, $this->mDesiredDestName
);
280 $delNotice = ''; // empty by default
281 if ( $desiredTitleObj instanceof Title
&& !$desiredTitleObj->exists() ) {
282 $dbr = wfGetDB( DB_REPLICA
);
284 LogEventsList
::showLogExtract( $delNotice, [ 'delete', 'move' ],
287 'conds' => [ 'log_action != ' . $dbr->addQuotes( 'revision' ) ],
288 'showIfEmpty' => false,
289 'msgKey' => [ 'upload-recreate-warning' ] ]
292 $form->addPreText( $delNotice );
295 $form->addPreText( '<div id="uploadtext">' .
296 $this->msg( 'uploadtext', [ $this->mDesiredDestName
] )->parseAsBlock() .
298 # Add upload error message
299 $form->addPreText( $message );
302 $uploadFooter = $this->msg( 'uploadfooter' );
303 if ( !$uploadFooter->isDisabled() ) {
304 $form->addPostText( '<div id="mw-upload-footer-message">'
305 . $uploadFooter->parseAsBlock() . "</div>\n" );
312 * Shows the "view X deleted revivions link""
314 protected function showViewDeletedLinks() {
315 $title = Title
::makeTitleSafe( NS_FILE
, $this->mDesiredDestName
);
316 $user = $this->getUser();
317 // Show a subtitle link to deleted revisions (to sysops et al only)
318 if ( $title instanceof Title
) {
319 $count = $title->isDeleted();
320 if ( $count > 0 && $user->isAllowed( 'deletedhistory' ) ) {
321 $restorelink = $this->getLinkRenderer()->makeKnownLink(
322 SpecialPage
::getTitleFor( 'Undelete', $title->getPrefixedText() ),
323 $this->msg( 'restorelink' )->numParams( $count )->text()
325 $link = $this->msg( $user->isAllowed( 'delete' ) ?
'thisisdeleted' : 'viewdeleted' )
326 ->rawParams( $restorelink )->parseAsBlock();
327 $this->getOutput()->addHTML( "<div id=\"contentSub2\">{$link}</div>" );
333 * Stashes the upload and shows the main upload form.
335 * Note: only errors that can be handled by changing the name or
336 * description should be redirected here. It should be assumed that the
337 * file itself is sane and has passed UploadBase::verifyFile. This
338 * essentially means that UploadBase::VERIFICATION_ERROR and
339 * UploadBase::EMPTY_FILE should not be passed here.
341 * @param string $message HTML message to be passed to mainUploadForm
343 protected function showRecoverableUploadError( $message ) {
344 $stashStatus = $this->mUpload
->tryStashFile( $this->getUser() );
345 if ( $stashStatus->isGood() ) {
346 $sessionKey = $stashStatus->getValue()->getFileKey();
347 $uploadWarning = 'upload-tryagain';
350 $uploadWarning = 'upload-tryagain-nostash';
352 $message = '<h2>' . $this->msg( 'uploaderror' )->escaped() . "</h2>\n" .
353 '<div class="error">' . $message . "</div>\n";
355 $form = $this->getUploadForm( $message, $sessionKey );
356 $form->setSubmitText( $this->msg( $uploadWarning )->escaped() );
357 $this->showUploadForm( $form );
361 * Stashes the upload, shows the main form, but adds a "continue anyway button".
362 * Also checks whether there are actually warnings to display.
364 * @param array $warnings
365 * @return bool True if warnings were displayed, false if there are no
366 * warnings and it should continue processing
368 protected function showUploadWarning( $warnings ) {
369 # If there are no warnings, or warnings we can ignore, return early.
370 # mDestWarningAck is set when some javascript has shown the warning
371 # to the user. mForReUpload is set when the user clicks the "upload a
373 if ( !$warnings ||
( count( $warnings ) == 1
374 && isset( $warnings['exists'] )
375 && ( $this->mDestWarningAck ||
$this->mForReUpload
) )
380 $stashStatus = $this->mUpload
->tryStashFile( $this->getUser() );
381 if ( $stashStatus->isGood() ) {
382 $sessionKey = $stashStatus->getValue()->getFileKey();
383 $uploadWarning = 'uploadwarning-text';
386 $uploadWarning = 'uploadwarning-text-nostash';
389 // Add styles for the warning, reused from the live preview
390 $this->getOutput()->addModuleStyles( 'mediawiki.special' );
392 $linkRenderer = $this->getLinkRenderer();
393 $warningHtml = '<h2>' . $this->msg( 'uploadwarning' )->escaped() . "</h2>\n"
394 . '<div class="mw-destfile-warning"><ul>';
395 foreach ( $warnings as $warning => $args ) {
396 if ( $warning == 'badfilename' ) {
397 $this->mDesiredDestName
= Title
::makeTitle( NS_FILE
, $args )->getText();
399 if ( $warning == 'exists' ) {
400 $msg = "\t<li>" . self
::getExistsWarning( $args ) . "</li>\n";
401 } elseif ( $warning == 'no-change' ) {
403 $filename = $file->getTitle()->getPrefixedText();
404 $msg = "\t<li>" . wfMessage( 'fileexists-no-change', $filename )->parse() . "</li>\n";
405 } elseif ( $warning == 'duplicate-version' ) {
407 $count = count( $args );
408 $filename = $file->getTitle()->getPrefixedText();
409 $message = wfMessage( 'fileexists-duplicate-version' )
410 ->params( $filename )
411 ->numParams( $count );
412 $msg = "\t<li>" . $message->parse() . "</li>\n";
413 } elseif ( $warning == 'was-deleted' ) {
414 # If the file existed before and was deleted, warn the user of this
415 $ltitle = SpecialPage
::getTitleFor( 'Log' );
416 $llink = $linkRenderer->makeKnownLink(
418 wfMessage( 'deletionlog' )->text(),
422 'page' => Title
::makeTitle( NS_FILE
, $args )->getPrefixedText(),
425 $msg = "\t<li>" . wfMessage( 'filewasdeleted' )->rawParams( $llink )->parse() . "</li>\n";
426 } elseif ( $warning == 'duplicate' ) {
427 $msg = $this->getDupeWarning( $args );
428 } elseif ( $warning == 'duplicate-archive' ) {
429 if ( $args === '' ) {
430 $msg = "\t<li>" . $this->msg( 'file-deleted-duplicate-notitle' )->parse()
433 $msg = "\t<li>" . $this->msg( 'file-deleted-duplicate',
434 Title
::makeTitle( NS_FILE
, $args )->getPrefixedText() )->parse()
438 if ( $args === true ) {
440 } elseif ( !is_array( $args ) ) {
443 $msg = "\t<li>" . $this->msg( $warning, $args )->parse() . "</li>\n";
445 $warningHtml .= $msg;
447 $warningHtml .= "</ul></div>\n";
448 $warningHtml .= $this->msg( $uploadWarning )->parseAsBlock();
450 $form = $this->getUploadForm( $warningHtml, $sessionKey, /* $hideIgnoreWarning */ true );
451 $form->setSubmitText( $this->msg( 'upload-tryagain' )->text() );
453 'name' => 'wpUploadIgnoreWarning',
454 'value' => $this->msg( 'ignorewarning' )->text()
457 'name' => 'wpCancelUpload',
458 'value' => $this->msg( 'reuploaddesc' )->text()
461 $this->showUploadForm( $form );
463 # Indicate that we showed a form
468 * Show the upload form with error message, but do not stash the file.
470 * @param string $message HTML string
472 protected function showUploadError( $message ) {
473 $message = '<h2>' . $this->msg( 'uploadwarning' )->escaped() . "</h2>\n" .
474 '<div class="error">' . $message . "</div>\n";
475 $this->showUploadForm( $this->getUploadForm( $message ) );
480 * Checks are made in SpecialUpload::execute()
482 protected function processUpload() {
483 // Fetch the file if required
484 $status = $this->mUpload
->fetchFile();
485 if ( !$status->isOK() ) {
486 $this->showUploadError( $this->getOutput()->parse( $status->getWikiText() ) );
490 // Avoid PHP 7.1 warning of passing $this by reference
492 if ( !Hooks
::run( 'UploadForm:BeforeProcessing', [ &$upload ] ) ) {
493 wfDebug( "Hook 'UploadForm:BeforeProcessing' broke processing the file.\n" );
494 // This code path is deprecated. If you want to break upload processing
495 // do so by hooking into the appropriate hooks in UploadBase::verifyUpload
496 // and UploadBase::verifyFile.
497 // If you use this hook to break uploading, the user will be returned
498 // an empty form with no error message whatsoever.
502 // Upload verification
503 $details = $this->mUpload
->verifyUpload();
504 if ( $details['status'] != UploadBase
::OK
) {
505 $this->processVerificationError( $details );
510 // Verify permissions for this title
511 $permErrors = $this->mUpload
->verifyTitlePermissions( $this->getUser() );
512 if ( $permErrors !== true ) {
513 $code = array_shift( $permErrors[0] );
514 $this->showRecoverableUploadError( $this->msg( $code, $permErrors[0] )->parse() );
519 $this->mLocalFile
= $this->mUpload
->getLocalFile();
521 // Check warnings if necessary
522 if ( !$this->mIgnoreWarning
) {
523 $warnings = $this->mUpload
->checkWarnings();
524 if ( $this->showUploadWarning( $warnings ) ) {
529 // This is as late as we can throttle, after expected issues have been handled
530 if ( UploadBase
::isThrottled( $this->getUser() ) ) {
531 $this->showRecoverableUploadError(
532 $this->msg( 'actionthrottledtext' )->escaped()
537 // Get the page text if this is not a reupload
538 if ( !$this->mForReUpload
) {
539 $pageText = self
::getInitialPageText( $this->mComment
, $this->mLicense
,
540 $this->mCopyrightStatus
, $this->mCopyrightSource
, $this->getConfig() );
545 $changeTags = $this->getRequest()->getVal( 'wpChangeTags' );
546 if ( is_null( $changeTags ) ||
$changeTags === '' ) {
549 $changeTags = array_filter( array_map( 'trim', explode( ',', $changeTags ) ) );
553 $changeTagsStatus = ChangeTags
::canAddTagsAccompanyingChange(
554 $changeTags, $this->getUser() );
555 if ( !$changeTagsStatus->isOK() ) {
556 $this->showUploadError( $this->getOutput()->parse( $changeTagsStatus->getWikiText() ) );
562 $status = $this->mUpload
->performUpload(
570 if ( !$status->isGood() ) {
571 $this->showRecoverableUploadError( $this->getOutput()->parse( $status->getWikiText() ) );
576 // Success, redirect to description page
577 $this->mUploadSuccessful
= true;
578 // Avoid PHP 7.1 warning of passing $this by reference
580 Hooks
::run( 'SpecialUploadComplete', [ &$upload ] );
581 $this->getOutput()->redirect( $this->mLocalFile
->getTitle()->getFullURL() );
585 * Get the initial image page text based on a comment and optional file status information
586 * @param string $comment
587 * @param string $license
588 * @param string $copyStatus
589 * @param string $source
590 * @param Config|null $config Configuration object to load data from
593 public static function getInitialPageText( $comment = '', $license = '',
594 $copyStatus = '', $source = '', Config
$config = null
596 if ( $config === null ) {
597 wfDebug( __METHOD__
. ' called without a Config instance passed to it' );
598 $config = MediaWikiServices
::getInstance()->getMainConfig();
602 $forceUIMsgAsContentMsg = (array)$config->get( 'ForceUIMsgAsContentMsg' );
603 /* These messages are transcluded into the actual text of the description page.
604 * Thus, forcing them as content messages makes the upload to produce an int: template
605 * instead of hardcoding it there in the uploader language.
607 foreach ( [ 'license-header', 'filedesc', 'filestatus', 'filesource' ] as $msgName ) {
608 if ( in_array( $msgName, $forceUIMsgAsContentMsg ) ) {
609 $msg[$msgName] = "{{int:$msgName}}";
611 $msg[$msgName] = wfMessage( $msgName )->inContentLanguage()->text();
616 if ( $license !== '' ) {
617 $licenseText = '== ' . $msg['license-header'] . " ==\n{{" . $license . "}}\n";
620 $pageText = $comment . "\n";
621 $headerText = '== ' . $msg['filedesc'] . ' ==';
622 if ( $comment !== '' && strpos( $comment, $headerText ) === false ) {
623 // prepend header to page text unless it's already there (or there is no content)
624 $pageText = $headerText . "\n" . $pageText;
627 if ( $config->get( 'UseCopyrightUpload' ) ) {
628 $pageText .= '== ' . $msg['filestatus'] . " ==\n" . $copyStatus . "\n";
629 $pageText .= $licenseText;
630 $pageText .= '== ' . $msg['filesource'] . " ==\n" . $source;
632 $pageText .= $licenseText;
635 // allow extensions to modify the content
636 Hooks
::run( 'UploadForm:getInitialPageText', [ &$pageText, $msg, $config ] );
642 * See if we should check the 'watch this page' checkbox on the form
643 * based on the user's preferences and whether we're being asked
644 * to create a new file or update an existing one.
646 * In the case where 'watch edits' is off but 'watch creations' is on,
647 * we'll leave the box unchecked.
649 * Note that the page target can be changed *on the form*, so our check
650 * state can get out of sync.
651 * @return bool|string
653 protected function getWatchCheck() {
654 if ( $this->getUser()->getOption( 'watchdefault' ) ) {
659 $desiredTitleObj = Title
::makeTitleSafe( NS_FILE
, $this->mDesiredDestName
);
660 if ( $desiredTitleObj instanceof Title
&& $this->getUser()->isWatched( $desiredTitleObj ) ) {
661 // Already watched, don't change that
665 $local = wfLocalFile( $this->mDesiredDestName
);
666 if ( $local && $local->exists() ) {
667 // We're uploading a new version of an existing file.
668 // No creation, so don't watch it if we're not already.
671 // New page should get watched if that's our option.
672 return $this->getUser()->getOption( 'watchcreations' ) ||
673 $this->getUser()->getOption( 'watchuploads' );
678 * Provides output to the user for a result of UploadBase::verifyUpload
680 * @param array $details Result of UploadBase::verifyUpload
681 * @throws MWException
683 protected function processVerificationError( $details ) {
684 switch ( $details['status'] ) {
685 /** Statuses that only require name changing **/
686 case UploadBase
::MIN_LENGTH_PARTNAME
:
687 $this->showRecoverableUploadError( $this->msg( 'minlength1' )->escaped() );
689 case UploadBase
::ILLEGAL_FILENAME
:
690 $this->showRecoverableUploadError( $this->msg( 'illegalfilename',
691 $details['filtered'] )->parse() );
693 case UploadBase
::FILENAME_TOO_LONG
:
694 $this->showRecoverableUploadError( $this->msg( 'filename-toolong' )->escaped() );
696 case UploadBase
::FILETYPE_MISSING
:
697 $this->showRecoverableUploadError( $this->msg( 'filetype-missing' )->parse() );
699 case UploadBase
::WINDOWS_NONASCII_FILENAME
:
700 $this->showRecoverableUploadError( $this->msg( 'windows-nonascii-filename' )->parse() );
703 /** Statuses that require reuploading **/
704 case UploadBase
::EMPTY_FILE
:
705 $this->showUploadError( $this->msg( 'emptyfile' )->escaped() );
707 case UploadBase
::FILE_TOO_LARGE
:
708 $this->showUploadError( $this->msg( 'largefileserver' )->escaped() );
710 case UploadBase
::FILETYPE_BADTYPE
:
711 $msg = $this->msg( 'filetype-banned-type' );
712 if ( isset( $details['blacklistedExt'] ) ) {
713 $msg->params( $this->getLanguage()->commaList( $details['blacklistedExt'] ) );
715 $msg->params( $details['finalExt'] );
717 $extensions = array_unique( $this->getConfig()->get( 'FileExtensions' ) );
718 $msg->params( $this->getLanguage()->commaList( $extensions ),
719 count( $extensions ) );
721 // Add PLURAL support for the first parameter. This results
722 // in a bit unlogical parameter sequence, but does not break
724 if ( isset( $details['blacklistedExt'] ) ) {
725 $msg->params( count( $details['blacklistedExt'] ) );
730 $this->showUploadError( $msg->parse() );
732 case UploadBase
::VERIFICATION_ERROR
:
733 unset( $details['status'] );
734 $code = array_shift( $details['details'] );
735 $this->showUploadError( $this->msg( $code, $details['details'] )->parse() );
737 case UploadBase
::HOOK_ABORTED
:
738 if ( is_array( $details['error'] ) ) { # allow hooks to return error details in an array
739 $args = $details['error'];
740 $error = array_shift( $args );
742 $error = $details['error'];
746 $this->showUploadError( $this->msg( $error, $args )->parse() );
749 throw new MWException( __METHOD__
. ": Unknown value `{$details['status']}`" );
754 * Remove a temporarily kept file stashed by saveTempUploadedFile().
756 * @return bool Success
758 protected function unsaveUploadedFile() {
759 if ( !( $this->mUpload
instanceof UploadFromStash
) ) {
762 $success = $this->mUpload
->unsaveUploadedFile();
764 $this->getOutput()->showFatalError(
765 $this->msg( 'filedeleteerror' )
766 ->params( $this->mUpload
->getTempPath() )
776 /*** Functions for formatting warnings ***/
779 * Formats a result of UploadBase::getExistsWarning as HTML
780 * This check is static and can be done pre-upload via AJAX
782 * @param array $exists The result of UploadBase::getExistsWarning
783 * @return string Empty string if there is no warning or an HTML fragment
785 public static function getExistsWarning( $exists ) {
790 $file = $exists['file'];
791 $filename = $file->getTitle()->getPrefixedText();
794 if ( $exists['warning'] == 'exists' ) {
796 $warnMsg = wfMessage( 'fileexists', $filename );
797 } elseif ( $exists['warning'] == 'page-exists' ) {
798 // Page exists but file does not
799 $warnMsg = wfMessage( 'filepageexists', $filename );
800 } elseif ( $exists['warning'] == 'exists-normalized' ) {
801 $warnMsg = wfMessage( 'fileexists-extension', $filename,
802 $exists['normalizedFile']->getTitle()->getPrefixedText() );
803 } elseif ( $exists['warning'] == 'thumb' ) {
804 // Swapped argument order compared with other messages for backwards compatibility
805 $warnMsg = wfMessage( 'fileexists-thumbnail-yes',
806 $exists['thumbFile']->getTitle()->getPrefixedText(), $filename );
807 } elseif ( $exists['warning'] == 'thumb-name' ) {
808 // Image w/o '180px-' does not exists, but we do not like these filenames
809 $name = $file->getName();
810 $badPart = substr( $name, 0, strpos( $name, '-' ) +
1 );
811 $warnMsg = wfMessage( 'file-thumbnail-no', $badPart );
812 } elseif ( $exists['warning'] == 'bad-prefix' ) {
813 $warnMsg = wfMessage( 'filename-bad-prefix', $exists['prefix'] );
816 return $warnMsg ?
$warnMsg->title( $file->getTitle() )->parse() : '';
820 * Construct a warning and a gallery from an array of duplicate files.
821 * @param array $dupes
824 public function getDupeWarning( $dupes ) {
829 $gallery = ImageGalleryBase
::factory( false, $this->getContext() );
830 $gallery->setShowBytes( false );
831 $gallery->setShowDimensions( false );
832 foreach ( $dupes as $file ) {
833 $gallery->add( $file->getTitle() );
837 $this->msg( 'file-exists-duplicate' )->numParams( count( $dupes ) )->parse() .
838 $gallery->toHTML() . "</li>\n";
841 protected function getGroupName() {
846 * Should we rotate images in the preview on Special:Upload.
848 * This controls js: mw.config.get( 'wgFileCanRotate' )
850 * @todo What about non-BitmapHandler handled files?
853 public static function rotationEnabled() {
854 $bitmapHandler = new BitmapHandler();
855 return $bitmapHandler->autoRotateEnabled();