42 private $userOptionsLookup;
48 private $watchlistManager;
62 parent::__construct(
'Upload',
'upload' );
64 $services = MediaWikiServices::getInstance();
65 $repoGroup = $repoGroup ?? $services->getRepoGroup();
66 $this->localRepo = $repoGroup->getLocalRepo();
67 $this->userOptionsLookup = $userOptionsLookup ?? $services->getUserOptionsLookup();
68 $this->nsInfo = $nsInfo ?? $services->getNamespaceInfo();
69 $this->watchlistManager = $watchlistManager ?? $services->getWatchlistManager();
126 $this->mRequest = $request = $this->
getRequest();
127 $this->mSourceType = $request->getVal(
'wpSourceType',
'file' );
128 $this->mUpload = UploadBase::createFromRequest( $request );
129 $this->mUploadClicked = $request->wasPosted()
130 && ( $request->getCheck(
'wpUpload' )
131 || $request->getCheck(
'wpUploadIgnoreWarning' ) );
134 $this->mDesiredDestName = $request->getText(
'wpDestFile' );
135 if ( !$this->mDesiredDestName && $request->getFileName(
'wpUploadFile' ) !== null ) {
136 $this->mDesiredDestName = $request->getFileName(
'wpUploadFile' );
138 $this->mLicense = $request->getText(
'wpLicense' );
140 $this->mDestWarningAck = $request->getText(
'wpDestFileWarningAck' );
141 $this->mIgnoreWarning = $request->getCheck(
'wpIgnoreWarning' )
142 || $request->getCheck(
'wpUploadIgnoreWarning' );
143 $this->mWatchthis = $request->getBool(
'wpWatchthis' ) && $this->
getUser()->isRegistered();
144 $this->mCopyrightStatus = $request->getText(
'wpUploadCopyStatus' );
145 $this->mCopyrightSource = $request->getText(
'wpUploadSource' );
147 $this->mForReUpload = $request->getBool(
'wpForReUpload' );
149 $commentDefault =
'';
150 $commentMsg = $this->
msg(
'upload-default-description' )->inContentLanguage();
151 if ( !$this->mForReUpload && !$commentMsg->isDisabled() ) {
152 $commentDefault = $commentMsg->plain();
154 $this->mComment = $request->getText(
'wpUploadDescription', $commentDefault );
156 $this->mCancelUpload = $request->getCheck(
'wpCancelUpload' )
157 || $request->getCheck(
'wpReUpload' );
160 $token = $request->getVal(
'wpEditToken' );
161 $this->mTokenOk = $this->
getUser()->matchEditToken( $token );
163 $this->uploadFormTextTop =
'';
164 $this->uploadFormTextAfterSummary =
'';
176 return UploadBase::isEnabled() && parent::userCanExecute( $user );
195 # Check uploading enabled
196 if ( !UploadBase::isEnabled() ) {
197 throw new ErrorPageError(
'uploaddisabled',
'uploaddisabledtext' );
204 $permissionRequired = UploadBase::isAllowed( $user );
205 if ( $permissionRequired !==
true ) {
210 if ( $user->isBlockedFromUpload() ) {
215 $this->getLanguage(),
216 $this->getRequest()->getIP()
221 if ( $user->isBlockedGlobally() ) {
223 $user->getGlobalBlock(),
225 $this->getLanguage(),
226 $this->getRequest()->getIP()
230 # Check whether we actually want to allow changing stuff
235 # Unsave the temporary file in case this was a cancelled upload
237 # Something went wrong, so unsaveUploadedFile showed a warning
241 # Process upload or show a form
243 $this->mTokenOk && !$this->mCancelUpload &&
244 ( $this->mUpload && $this->mUploadClicked )
248 # Backwards compatibility hook
249 if ( !$this->
getHookRunner()->onUploadForm_initial( $this ) ) {
250 wfDebug(
"Hook 'UploadForm:initial' broke output of the upload form" );
258 if ( $this->mUpload ) {
259 $this->mUpload->cleanupTempFile();
269 # Add links if file was previously deleted
270 if ( $this->mDesiredDestName ) {
289 protected function getUploadForm( $message =
'', $sessionKey =
'', $hideIgnoreWarning =
false ) {
294 'forreupload' => $this->mForReUpload,
295 'sessionkey' => $sessionKey,
296 'hideignorewarning' => $hideIgnoreWarning,
297 'destwarningack' => (
bool)$this->mDestWarningAck,
299 'description' => $this->mComment,
300 'texttop' => $this->uploadFormTextTop,
301 'textaftersummary' => $this->uploadFormTextAfterSummary,
302 'destfile' => $this->mDesiredDestName,
312 # Check the token, but only if necessary
314 !$this->mTokenOk && !$this->mCancelUpload &&
315 ( $this->mUpload && $this->mUploadClicked )
317 $form->addPreText( $this->
msg(
'session_fail_preview' )->parse() );
320 # Give a notice if the user is uploading a file that has been deleted or moved
321 # Note that this is independent from the message 'filewasdeleted'
322 $desiredTitleObj = Title::makeTitleSafe(
NS_FILE, $this->mDesiredDestName );
324 if ( $desiredTitleObj instanceof
Title && !$desiredTitleObj->
exists() ) {
325 LogEventsList::showLogExtract( $delNotice, [
'delete',
'move' ],
328 'conds' => [
'log_action != ' . $this->localRepo->getReplicaDB()->addQuotes(
'revision' ) ],
329 'showIfEmpty' =>
false,
330 'msgKey' => [
'upload-recreate-warning' ] ]
333 $form->addPreText( $delNotice );
336 $form->addPreText(
'<div id="uploadtext">' .
337 $this->
msg(
'uploadtext', [ $this->mDesiredDestName ] )->parseAsBlock() .
339 # Add upload error message
340 $form->addPreText( $message );
343 $uploadFooter = $this->
msg(
'uploadfooter' );
344 if ( !$uploadFooter->isDisabled() ) {
345 $form->addPostText(
'<div id="mw-upload-footer-message">'
346 . $uploadFooter->parseAsBlock() .
"</div>\n" );
356 $title = Title::makeTitleSafe(
NS_FILE, $this->mDesiredDestName );
360 $count =
$title->getDeletedEditsCount();
361 if ( $count > 0 && $this->
getAuthority()->isAllowed(
'deletedhistory' ) ) {
364 $this->msg(
'restorelink' )->numParams( $count )->text()
367 $this->
getAuthority()->isAllowed(
'delete' ) ?
'thisisdeleted' :
'viewdeleted'
368 )->rawParams( $restorelink )->parseAsBlock();
372 [
'id' =>
'contentSub2' ],
392 $stashStatus = $this->mUpload->tryStashFile( $this->
getUser() );
393 if ( $stashStatus->isGood() ) {
394 $sessionKey = $stashStatus->getValue()->getFileKey();
395 $uploadWarning =
'upload-tryagain';
398 $uploadWarning =
'upload-tryagain-nostash';
400 $message =
'<h2>' . $this->
msg(
'uploaderror' )->escaped() .
'</h2>' .
401 Html::errorBox( $message );
404 $form->setSubmitText( $this->
msg( $uploadWarning )->escaped() );
417 # If there are no warnings, or warnings we can ignore, return early.
418 # mDestWarningAck is set when some javascript has shown the warning
419 # to the user. mForReUpload is set when the user clicks the "upload a
421 if ( !$warnings || ( count( $warnings ) == 1
422 && isset( $warnings[
'exists'] )
423 && ( $this->mDestWarningAck || $this->mForReUpload ) )
428 $stashStatus = $this->mUpload->tryStashFile( $this->
getUser() );
429 if ( $stashStatus->isGood() ) {
430 $sessionKey = $stashStatus->getValue()->getFileKey();
431 $uploadWarning =
'uploadwarning-text';
434 $uploadWarning =
'uploadwarning-text-nostash';
438 $this->
getOutput()->addModuleStyles(
'mediawiki.special' );
441 $warningHtml =
'<h2>' . $this->
msg(
'uploadwarning' )->escaped() .
"</h2>\n"
442 .
'<div class="mw-destfile-warning"><ul>';
443 foreach ( $warnings as $warning =>
$args ) {
444 if ( $warning ==
'badfilename' ) {
445 $this->mDesiredDestName = Title::makeTitle(
NS_FILE,
$args )->getText();
447 if ( $warning ==
'exists' ) {
448 $msg =
"\t<li>" . self::getExistsWarning(
$args ) .
"</li>\n";
449 } elseif ( $warning ==
'no-change' ) {
451 $filename =
$file->getTitle()->getPrefixedText();
452 $msg =
"\t<li>" . $this->
msg(
'fileexists-no-change', $filename )->parse() .
"</li>\n";
453 } elseif ( $warning ==
'duplicate-version' ) {
455 $count = count(
$args );
456 $filename =
$file->getTitle()->getPrefixedText();
457 $message = $this->
msg(
'fileexists-duplicate-version' )
458 ->params( $filename )
459 ->numParams( $count );
460 $msg =
"\t<li>" . $message->parse() .
"</li>\n";
461 } elseif ( $warning ==
'was-deleted' ) {
462 # If the file existed before and was deleted, warn the user of this
464 $llink = $linkRenderer->makeKnownLink(
466 $this->
msg(
'deletionlog' )->text(),
470 'page' => Title::makeTitle(
NS_FILE,
$args )->getPrefixedText(),
473 $msg =
"\t<li>" . $this->
msg(
'filewasdeleted' )->rawParams( $llink )->parse() .
"</li>\n";
474 } elseif ( $warning ==
'duplicate' ) {
476 } elseif ( $warning ==
'duplicate-archive' ) {
477 if (
$args ===
'' ) {
478 $msg =
"\t<li>" . $this->
msg(
'file-deleted-duplicate-notitle' )->parse()
481 $msg =
"\t<li>" . $this->
msg(
'file-deleted-duplicate',
482 Title::makeTitle(
NS_FILE,
$args )->getPrefixedText() )->parse()
486 if (
$args ===
true ) {
488 } elseif ( !is_array(
$args ) ) {
491 $msg =
"\t<li>" . $this->
msg( $warning,
$args )->parse() .
"</li>\n";
493 $warningHtml .= $msg;
495 $warningHtml .=
"</ul></div>\n";
496 $warningHtml .= $this->
msg( $uploadWarning )->parseAsBlock();
498 $form = $this->
getUploadForm( $warningHtml, $sessionKey,
true );
499 $form->setSubmitTextMsg(
'upload-tryagain' );
501 'name' =>
'wpUploadIgnoreWarning',
502 'value' => $this->
msg(
'ignorewarning' )->text()
505 'name' =>
'wpCancelUpload',
506 'value' => $this->
msg(
'reuploaddesc' )->text()
511 # Indicate that we showed a form
521 $message =
'<h2>' . $this->
msg(
'uploadwarning' )->escaped() .
'</h2>' .
522 Html::errorBox( $message );
532 $status = $this->mUpload->fetchFile();
533 if ( !$status->isOK() ) {
535 $status->getWikiText(
false,
false, $this->getLanguage() )
540 if ( !$this->
getHookRunner()->onUploadForm_BeforeProcessing( $this ) ) {
541 wfDebug(
"Hook 'UploadForm:BeforeProcessing' broke processing the file." );
551 $details = $this->mUpload->verifyUpload();
552 if ( $details[
'status'] != UploadBase::OK ) {
560 $permErrors = $this->mUpload->verifyTitlePermissions( $user );
561 if ( $permErrors !==
true ) {
562 $code = array_shift( $permErrors[0] );
568 $this->mLocalFile = $this->mUpload->getLocalFile();
571 if ( !$this->mIgnoreWarning ) {
572 $warnings = $this->mUpload->checkWarnings( $user );
579 if ( UploadBase::isThrottled( $user ) ) {
581 $this->
msg(
'actionthrottledtext' )->escaped()
587 if ( !$this->mForReUpload ) {
588 $pageText = self::getInitialPageText( $this->mComment, $this->mLicense,
589 $this->mCopyrightStatus, $this->mCopyrightSource, $this->
getConfig() );
594 $changeTags = $this->
getRequest()->getVal(
'wpChangeTags' );
595 if ( $changeTags ===
null || $changeTags ===
'' ) {
598 $changeTags = array_filter( array_map(
'trim', explode(
',', $changeTags ) ) );
603 $changeTags, $user );
604 if ( !$changeTagsStatus->isOK() ) {
606 $changeTagsStatus->getWikiText(
false,
false, $this->getLanguage() )
613 $status = $this->mUpload->performUpload(
621 if ( !$status->isGood() ) {
624 $status->getWikiText(
false,
false, $this->getLanguage() )
632 $this->mUploadSuccessful =
true;
634 $this->
getOutput()->redirect( $this->mLocalFile->getTitle()->getFullURL() );
649 if ( $config ===
null ) {
650 wfDebug( __METHOD__ .
' called without a Config instance passed to it' );
651 $config = MediaWikiServices::getInstance()->getMainConfig();
655 $forceUIMsgAsContentMsg = (array)$config->get( MainConfigNames::ForceUIMsgAsContentMsg );
660 foreach ( [
'license-header',
'filedesc',
'filestatus',
'filesource' ] as $msgName ) {
661 if ( in_array( $msgName, $forceUIMsgAsContentMsg ) ) {
662 $msg[$msgName] =
"{{int:$msgName}}";
664 $msg[$msgName] =
wfMessage( $msgName )->inContentLanguage()->text();
669 if ( $license !==
'' ) {
670 $licenseText =
'== ' . $msg[
'license-header'] .
" ==\n{{" . $license .
"}}\n";
673 $pageText = $comment .
"\n";
674 $headerText =
'== ' . $msg[
'filedesc'] .
' ==';
675 if ( $comment !==
'' && strpos( $comment, $headerText ) ===
false ) {
677 $pageText = $headerText .
"\n" . $pageText;
680 if ( $config->get( MainConfigNames::UseCopyrightUpload ) ) {
681 $pageText .=
'== ' . $msg[
'filestatus'] .
" ==\n" . $copyStatus .
"\n";
682 $pageText .= $licenseText;
683 $pageText .=
'== ' . $msg[
'filesource'] .
" ==\n" .
$source;
685 $pageText .= $licenseText;
689 Hooks::runner()->onUploadForm_getInitialPageText( $pageText, $msg, $config );
708 if ( $this->userOptionsLookup->getBoolOption( $user,
'watchdefault' ) ) {
713 $desiredTitleObj = Title::makeTitleSafe(
NS_FILE, $this->mDesiredDestName );
714 if ( $desiredTitleObj instanceof
Title &&
715 $this->watchlistManager->isWatched( $user, $desiredTitleObj ) ) {
720 $local = $this->localRepo->newFile( $this->mDesiredDestName );
721 if ( $local && $local->exists() ) {
727 return $this->userOptionsLookup->getBoolOption( $user,
'watchcreations' ) ||
728 $this->userOptionsLookup->getBoolOption( $user,
'watchuploads' );
739 switch ( $details[
'status'] ) {
741 case UploadBase::MIN_LENGTH_PARTNAME:
744 case UploadBase::ILLEGAL_FILENAME:
746 $details[
'filtered'] )->parse() );
748 case UploadBase::FILENAME_TOO_LONG:
751 case UploadBase::FILETYPE_MISSING:
754 case UploadBase::WINDOWS_NONASCII_FILENAME:
759 case UploadBase::EMPTY_FILE:
762 case UploadBase::FILE_TOO_LARGE:
765 case UploadBase::FILETYPE_BADTYPE:
766 $msg = $this->
msg(
'filetype-banned-type' );
767 if ( isset( $details[
'blacklistedExt'] ) ) {
768 $msg->params( $this->
getLanguage()->commaList( $details[
'blacklistedExt'] ) );
770 $msg->params( $details[
'finalExt'] );
773 array_unique( $this->
getConfig()->
get( MainConfigNames::FileExtensions ) );
774 $msg->params( $this->
getLanguage()->commaList( $extensions ),
775 count( $extensions ) );
780 if ( isset( $details[
'blacklistedExt'] ) ) {
781 $msg->params( count( $details[
'blacklistedExt'] ) );
788 case UploadBase::VERIFICATION_ERROR:
789 unset( $details[
'status'] );
790 $code = array_shift( $details[
'details'] );
793 case UploadBase::HOOK_ABORTED:
794 if ( is_array( $details[
'error'] ) ) { # allow hooks to
return error details in an array
795 $args = $details[
'error'];
796 $error = array_shift(
$args );
798 $error = $details[
'error'];
805 throw new MWException( __METHOD__ .
": Unknown value `{$details['status']}`" );
818 $success = $this->mUpload->unsaveUploadedFile();
821 $this->
msg(
'filedeleteerror' )
822 ->params( $this->mUpload->getTempPath() )
846 $file = $exists[
'file'];
847 $filename =
$file->getTitle()->getPrefixedText();
850 if ( $exists[
'warning'] ==
'exists' ) {
852 $warnMsg =
wfMessage(
'fileexists', $filename );
853 } elseif ( $exists[
'warning'] ==
'page-exists' ) {
855 $warnMsg =
wfMessage(
'filepageexists', $filename );
856 } elseif ( $exists[
'warning'] ==
'exists-normalized' ) {
857 $warnMsg =
wfMessage(
'fileexists-extension', $filename,
858 $exists[
'normalizedFile']->
getTitle()->getPrefixedText() );
859 } elseif ( $exists[
'warning'] ==
'thumb' ) {
861 $warnMsg =
wfMessage(
'fileexists-thumbnail-yes',
862 $exists[
'thumbFile']->
getTitle()->getPrefixedText(), $filename );
863 } elseif ( $exists[
'warning'] ==
'thumb-name' ) {
865 $name =
$file->getName();
866 $badPart = substr( $name, 0, strpos( $name,
'-' ) + 1 );
867 $warnMsg =
wfMessage(
'file-thumbnail-no', $badPart );
868 } elseif ( $exists[
'warning'] ==
'bad-prefix' ) {
869 $warnMsg =
wfMessage(
'filename-bad-prefix', $exists[
'prefix'] );
872 return $warnMsg ? $warnMsg->page(
$file->getTitle() )->parse() :
'';
885 $gallery = ImageGalleryBase::factory(
false, $this->
getContext() );
886 $gallery->setShowBytes(
false );
887 $gallery->setShowDimensions(
false );
888 foreach ( $dupes as
$file ) {
889 $gallery->add(
$file->getTitle() );
893 $this->
msg(
'file-exists-duplicate' )->numParams( count( $dupes ) )->parse() .
894 $gallery->toHTML() .
"</li>\n";
911 return $bitmapHandler->autoRotateEnabled();
wfDebug( $text, $dest='all', array $context=[])
Sends a line to the debug log if enabled or, optionally, to a comment in output.
wfMessage( $key,... $params)
This is the function for getting translated interface messages.
Generic handler for bitmap images.
An error page which can definitely be safely rendered using the OutputPage.
WebRequest clone which takes values from a provided array.
Local file in the wiki's own database.
Local repository that stores files in the local filesystem and registers them in the wiki's own datab...
A class containing constants representing the names of configuration variables.
This is a utility class for dealing with namespaces that encodes all the "magic" behaviors of them ba...
Show an error when a user tries to do something they do not have the necessary permissions for.
Prioritized list of file repositories.
Parent class for all special pages.
outputHeader( $summaryMessageKey='')
Outputs a summary message on top of special pages Per default the message key is the canonical name o...
setHeaders()
Sets headers - this should be called from the execute() method of all derived classes!
getOutput()
Get the OutputPage being used for this instance.
static getTitleFor( $name, $subpage=false, $fragment='')
Get a localised Title object for a specified special page name If you don't need a full Title object,...
msg( $key,... $params)
Wrapper around wfMessage that sets the current context.
getConfig()
Shortcut to get main config object.
getRequest()
Get the WebRequest being used for this instance.
checkReadOnly()
If the wiki is currently in readonly mode, throws a ReadOnlyError.
getPageTitle( $subpage=false)
Get a self-referential title object.
useTransactionalTimeLimit()
Call wfTransactionalTimeLimit() if this request was POSTed.
getLanguage()
Shortcut to get user's language.
addHelpLink( $to, $overrideBaseUrl=false)
Adds help link with an icon via page indicators.
getContentLanguage()
Shortcut to get content language.
Form for handling uploads and special page.
processVerificationError( $details)
Provides output to the user for a result of UploadBase::verifyUpload.
showViewDeletedLinks()
Shows the "view X deleted revisions link"".
getWatchCheck()
See if we should check the 'watch this page' checkbox on the form based on the user's preferences and...
loadRequest()
Initialize instance variables from request and create an Upload handler.
static getInitialPageText( $comment='', $license='', $copyStatus='', $source='', Config $config=null)
Get the initial image page text based on a comment and optional file status information.
showUploadWarning( $warnings)
Stashes the upload, shows the main form, but adds a "continue anyway button".
bool $mCancelUpload
The user clicked "Cancel and return to upload form" button.
unsaveUploadedFile()
Remove a temporarily kept file stashed by saveTempUploadedFile().
userCanExecute(User $user)
This page can be shown if uploading is enabled.
static getExistsWarning( $exists)
Functions for formatting warnings.
string $mDesiredDestName
User input variables from the "description" section.
showRecoverableUploadError( $message)
Stashes the upload and shows the main upload form.
string $uploadFormTextAfterSummary
Raw html injection point for hooks not using HTMLForm.
$mDestWarningAck
Hidden variables.
getDupeWarning( $dupes)
Construct a warning and a gallery from an array of duplicate files.
showUploadForm( $form)
Show the main upload form.
WebRequest FauxRequest $mRequest
Misc variables.
processUpload()
Do the upload.
bool $mUploadSuccessful
Subclasses can use this to determine whether a file was uploaded.
getGroupName()
Under which header this special page is listed in Special:SpecialPages See messages 'specialpages-gro...
getUploadForm( $message='', $sessionKey='', $hideIgnoreWarning=false)
Get an UploadForm instance with title and text properly set.
doesWrites()
Indicates whether this special page may perform database writes.
string $uploadFormTextTop
Raw html injection point for hooks not using HTMLForm.
bool $mForReUpload
The user followed an "overwrite this file" link.
$mIgnoreWarning
User input variables from the root section.
showUploadError( $message)
Show the upload form with error message, but do not stash the file.
static rotationEnabled()
Should we rotate images in the preview on Special:Upload.
__construct(RepoGroup $repoGroup=null, UserOptionsLookup $userOptionsLookup=null, NamespaceInfo $nsInfo=null, WatchlistManager $watchlistManager=null)
Represents a title within MediaWiki.
exists( $flags=0)
Check if page exists.
UploadBase and subclasses are the backend of MediaWiki's file uploads.
Implements uploading from previously stored file.
Show an error when the user tries to do something whilst blocked.
The WebRequest class encapsulates getting at data passed in the URL or via a POSTed form stripping il...
Interface for configuration instances.
if(PHP_SAPI !='cli-server') if(!isset( $_SERVER['SCRIPT_FILENAME'])) $file
Item class for a filearchive table row.