45 private $userOptionsLookup;
51 private $watchlistManager;
65 parent::__construct(
'Upload',
'upload' );
67 $services = MediaWikiServices::getInstance();
68 $repoGroup ??= $services->getRepoGroup();
69 $this->localRepo = $repoGroup->getLocalRepo();
70 $this->userOptionsLookup = $userOptionsLookup ?? $services->getUserOptionsLookup();
71 $this->nsInfo = $nsInfo ?? $services->getNamespaceInfo();
72 $this->watchlistManager = $watchlistManager ?? $services->getWatchlistManager();
129 $this->mRequest = $request = $this->
getRequest();
130 $this->mSourceType = $request->getVal(
'wpSourceType',
'file' );
131 $this->mUpload = UploadBase::createFromRequest( $request );
132 $this->mUploadClicked = $request->wasPosted()
133 && ( $request->getCheck(
'wpUpload' )
134 || $request->getCheck(
'wpUploadIgnoreWarning' ) );
137 $this->mDesiredDestName = $request->getText(
'wpDestFile' );
138 if ( !$this->mDesiredDestName && $request->getFileName(
'wpUploadFile' ) !== null ) {
139 $this->mDesiredDestName = $request->getFileName(
'wpUploadFile' );
141 $this->mLicense = $request->getText(
'wpLicense' );
143 $this->mDestWarningAck = $request->getText(
'wpDestFileWarningAck' );
144 $this->mIgnoreWarning = $request->getCheck(
'wpIgnoreWarning' )
145 || $request->getCheck(
'wpUploadIgnoreWarning' );
146 $this->mWatchthis = $request->getBool(
'wpWatchthis' ) && $this->
getUser()->isRegistered();
147 $this->mCopyrightStatus = $request->getText(
'wpUploadCopyStatus' );
148 $this->mCopyrightSource = $request->getText(
'wpUploadSource' );
150 $this->mForReUpload = $request->getBool(
'wpForReUpload' );
152 $commentDefault =
'';
153 $commentMsg = $this->
msg(
'upload-default-description' )->inContentLanguage();
154 if ( !$this->mForReUpload && !$commentMsg->isDisabled() ) {
155 $commentDefault = $commentMsg->plain();
157 $this->mComment = $request->getText(
'wpUploadDescription', $commentDefault );
159 $this->mCancelUpload = $request->getCheck(
'wpCancelUpload' )
160 || $request->getCheck(
'wpReUpload' );
163 $token = $request->getVal(
'wpEditToken' );
164 $this->mTokenOk = $this->
getUser()->matchEditToken( $token );
166 $this->uploadFormTextTop =
'';
167 $this->uploadFormTextAfterSummary =
'';
179 return UploadBase::isEnabled() && parent::userCanExecute( $user );
191 # Check uploading enabled
192 if ( !UploadBase::isEnabled() ) {
193 throw new ErrorPageError(
'uploaddisabled',
'uploaddisabledtext' );
200 $permissionRequired = UploadBase::isAllowed( $user );
201 if ( $permissionRequired !==
true ) {
206 if ( $user->isBlockedFromUpload() ) {
211 $this->getLanguage(),
212 $this->getRequest()->getIP()
216 # Check whether we actually want to allow changing stuff
221 # Unsave the temporary file in case this was a cancelled upload
223 # Something went wrong, so unsaveUploadedFile showed a warning
227 # Process upload or show a form
229 $this->mTokenOk && !$this->mCancelUpload &&
230 ( $this->mUpload && $this->mUploadClicked )
234 # Backwards compatibility hook
235 if ( !$this->
getHookRunner()->onUploadForm_initial( $this ) ) {
236 wfDebug(
"Hook 'UploadForm:initial' broke output of the upload form" );
244 if ( $this->mUpload ) {
245 $this->mUpload->cleanupTempFile();
270 protected function getUploadForm( $message =
'', $sessionKey =
'', $hideIgnoreWarning =
false ) {
275 'forreupload' => $this->mForReUpload,
276 'sessionkey' => $sessionKey,
277 'hideignorewarning' => $hideIgnoreWarning,
278 'destwarningack' => (
bool)$this->mDestWarningAck,
280 'description' => $this->mComment,
281 'texttop' => $this->uploadFormTextTop,
282 'textaftersummary' => $this->uploadFormTextAfterSummary,
283 'destfile' => $this->mDesiredDestName,
294 # Check the token, but only if necessary
296 !$this->mTokenOk && !$this->mCancelUpload &&
297 ( $this->mUpload && $this->mUploadClicked )
299 $form->addPreText( $this->
msg(
'session_fail_preview' )->parse() );
302 # Give a notice if the user is uploading a file that has been deleted or moved
303 # Note that this is independent from the message 'filewasdeleted'
304 $desiredTitleObj = Title::makeTitleSafe(
NS_FILE, $this->mDesiredDestName );
306 if ( $desiredTitleObj instanceof
Title && !$desiredTitleObj->
exists() ) {
307 LogEventsList::showLogExtract( $delNotice, [
'delete',
'move' ],
310 'conds' => [
'log_action != ' . $this->localRepo->getReplicaDB()->addQuotes(
'revision' ) ],
311 'showIfEmpty' =>
false,
312 'msgKey' => [
'upload-recreate-warning' ] ]
315 $form->addPreText( $delNotice );
318 $form->addPreText(
'<div id="uploadtext">' .
319 $this->
msg(
'uploadtext', [ $this->mDesiredDestName ] )->parseAsBlock() .
321 # Add upload error message
322 $form->addPreText( $message );
325 $uploadFooter = $this->
msg(
'uploadfooter' );
326 if ( !$uploadFooter->isDisabled() ) {
327 $form->addPostText(
'<div id="mw-upload-footer-message">'
328 . $uploadFooter->parseAsBlock() .
"</div>\n" );
346 $stashStatus = $this->mUpload->tryStashFile( $this->
getUser() );
347 if ( $stashStatus->isGood() ) {
348 $sessionKey = $stashStatus->getValue()->getFileKey();
349 $uploadWarning =
'upload-tryagain';
352 $uploadWarning =
'upload-tryagain-nostash';
354 $message =
'<h2>' . $this->
msg(
'uploaderror' )->escaped() .
'</h2>' .
355 Html::errorBox( $message );
358 $form->setSubmitText( $this->
msg( $uploadWarning )->escaped() );
371 # If there are no warnings, or warnings we can ignore, return early.
372 # mDestWarningAck is set when some javascript has shown the warning
373 # to the user. mForReUpload is set when the user clicks the "upload a
375 if ( !$warnings || ( count( $warnings ) == 1
376 && isset( $warnings[
'exists'] )
377 && ( $this->mDestWarningAck || $this->mForReUpload ) )
382 $stashStatus = $this->mUpload->tryStashFile( $this->
getUser() );
383 if ( $stashStatus->isGood() ) {
384 $sessionKey = $stashStatus->getValue()->getFileKey();
385 $uploadWarning =
'uploadwarning-text';
388 $uploadWarning =
'uploadwarning-text-nostash';
392 $this->
getOutput()->addModuleStyles(
'mediawiki.special' );
395 $warningHtml =
'<h2>' . $this->
msg(
'uploadwarning' )->escaped() .
"</h2>\n"
396 .
'<div class="mw-destfile-warning"><ul>';
397 foreach ( $warnings as $warning => $args ) {
398 if ( $warning ==
'badfilename' ) {
399 $this->mDesiredDestName = Title::makeTitle(
NS_FILE, $args )->getText();
401 if ( $warning ==
'exists' ) {
402 $msg =
"\t<li>" . self::getExistsWarning( $args ) .
"</li>\n";
403 } elseif ( $warning ==
'no-change' ) {
405 $filename =
$file->getTitle()->getPrefixedText();
406 $msg =
"\t<li>" . $this->
msg(
'fileexists-no-change', $filename )->parse() .
"</li>\n";
407 } elseif ( $warning ==
'duplicate-version' ) {
409 $count = count( $args );
410 $filename =
$file->getTitle()->getPrefixedText();
411 $message = $this->
msg(
'fileexists-duplicate-version' )
412 ->params( $filename )
413 ->numParams( $count );
414 $msg =
"\t<li>" . $message->parse() .
"</li>\n";
415 } elseif ( $warning ==
'was-deleted' ) {
416 # If the file existed before and was deleted, warn the user of this
418 $llink = $linkRenderer->makeKnownLink(
420 $this->
msg(
'deletionlog' )->text(),
424 'page' => Title::makeTitle(
NS_FILE, $args )->getPrefixedText(),
427 $msg =
"\t<li>" . $this->
msg(
'filewasdeleted' )->rawParams( $llink )->parse() .
"</li>\n";
428 } elseif ( $warning ==
'duplicate' ) {
430 } elseif ( $warning ==
'duplicate-archive' ) {
431 if ( $args ===
'' ) {
432 $msg =
"\t<li>" . $this->
msg(
'file-deleted-duplicate-notitle' )->parse()
435 $msg =
"\t<li>" . $this->
msg(
'file-deleted-duplicate',
436 Title::makeTitle(
NS_FILE, $args )->getPrefixedText() )->parse()
440 if ( $args ===
true ) {
442 } elseif ( !is_array( $args ) ) {
445 $msg =
"\t<li>" . $this->
msg( $warning, $args )->parse() .
"</li>\n";
447 $warningHtml .= $msg;
449 $warningHtml .=
"</ul></div>\n";
450 $warningHtml .= $this->
msg( $uploadWarning )->parseAsBlock();
452 $form = $this->
getUploadForm( $warningHtml, $sessionKey,
true );
453 $form->setSubmitTextMsg(
'upload-tryagain' );
455 'name' =>
'wpUploadIgnoreWarning',
456 'value' => $this->
msg(
'ignorewarning' )->text()
459 'name' =>
'wpCancelUpload',
460 'value' => $this->
msg(
'reuploaddesc' )->text()
465 # Indicate that we showed a form
475 $message =
'<h2>' . $this->
msg(
'uploadwarning' )->escaped() .
'</h2>' .
476 Html::errorBox( $message );
486 $status = $this->mUpload->fetchFile();
487 if ( !$status->isOK() ) {
489 $status->getWikiText(
false,
false, $this->getLanguage() )
494 if ( !$this->
getHookRunner()->onUploadForm_BeforeProcessing( $this ) ) {
495 wfDebug(
"Hook 'UploadForm:BeforeProcessing' broke processing the file." );
505 $details = $this->mUpload->verifyUpload();
506 if ( $details[
'status'] != UploadBase::OK ) {
514 $permErrors = $this->mUpload->verifyTitlePermissions( $user );
515 if ( $permErrors !==
true ) {
516 $code = array_shift( $permErrors[0] );
522 $this->mLocalFile = $this->mUpload->getLocalFile();
525 if ( !$this->mIgnoreWarning ) {
526 $warnings = $this->mUpload->checkWarnings( $user );
533 if ( UploadBase::isThrottled( $user ) ) {
535 $this->
msg(
'actionthrottledtext' )->escaped()
541 if ( !$this->mForReUpload ) {
542 $pageText = self::getInitialPageText( $this->mComment, $this->mLicense,
543 $this->mCopyrightStatus, $this->mCopyrightSource, $this->
getConfig() );
548 $changeTags = $this->
getRequest()->getVal(
'wpChangeTags' );
549 if ( $changeTags ===
null || $changeTags ===
'' ) {
552 $changeTags = array_filter( array_map(
'trim', explode(
',', $changeTags ) ) );
557 $changeTags, $user );
558 if ( !$changeTagsStatus->isOK() ) {
560 $changeTagsStatus->getWikiText(
false,
false, $this->getLanguage() )
567 $status = $this->mUpload->performUpload(
575 if ( !$status->isGood() ) {
578 $status->getWikiText(
false,
false, $this->getLanguage() )
586 $this->mUploadSuccessful =
true;
588 $this->
getOutput()->redirect( $this->mLocalFile->getTitle()->getFullURL() );
603 if ( $config ===
null ) {
604 wfDebug( __METHOD__ .
' called without a Config instance passed to it' );
605 $config = MediaWikiServices::getInstance()->getMainConfig();
609 $forceUIMsgAsContentMsg = (array)$config->get( MainConfigNames::ForceUIMsgAsContentMsg );
614 foreach ( [
'license-header',
'filedesc',
'filestatus',
'filesource' ] as $msgName ) {
615 if ( in_array( $msgName, $forceUIMsgAsContentMsg ) ) {
616 $msg[$msgName] =
"{{int:$msgName}}";
618 $msg[$msgName] =
wfMessage( $msgName )->inContentLanguage()->text();
623 if ( $license !==
'' ) {
624 $licenseText =
'== ' . $msg[
'license-header'] .
" ==\n{{" . $license .
"}}\n";
627 $pageText = $comment .
"\n";
628 $headerText =
'== ' . $msg[
'filedesc'] .
' ==';
629 if ( $comment !==
'' && !str_contains( $comment, $headerText ) ) {
631 $pageText = $headerText .
"\n" . $pageText;
634 if ( $config->get( MainConfigNames::UseCopyrightUpload ) ) {
635 $pageText .=
'== ' . $msg[
'filestatus'] .
" ==\n" . $copyStatus .
"\n";
636 $pageText .= $licenseText;
637 $pageText .=
'== ' . $msg[
'filesource'] .
" ==\n" .
$source;
639 $pageText .= $licenseText;
643 Hooks::runner()->onUploadForm_getInitialPageText( $pageText, $msg, $config );
662 if ( $this->userOptionsLookup->getBoolOption( $user,
'watchdefault' ) ) {
667 $desiredTitleObj = Title::makeTitleSafe(
NS_FILE, $this->mDesiredDestName );
668 if ( $desiredTitleObj instanceof
Title &&
669 $this->watchlistManager->isWatched( $user, $desiredTitleObj ) ) {
674 $local = $this->localRepo->newFile( $this->mDesiredDestName );
675 if ( $local && $local->exists() ) {
681 return $this->userOptionsLookup->getBoolOption( $user,
'watchcreations' ) ||
682 $this->userOptionsLookup->getBoolOption( $user,
'watchuploads' );
692 switch ( $details[
'status'] ) {
694 case UploadBase::MIN_LENGTH_PARTNAME:
697 case UploadBase::ILLEGAL_FILENAME:
699 $details[
'filtered'] )->parse() );
701 case UploadBase::FILENAME_TOO_LONG:
704 case UploadBase::FILETYPE_MISSING:
707 case UploadBase::WINDOWS_NONASCII_FILENAME:
712 case UploadBase::EMPTY_FILE:
715 case UploadBase::FILE_TOO_LARGE:
718 case UploadBase::FILETYPE_BADTYPE:
719 $msg = $this->
msg(
'filetype-banned-type' );
720 if ( isset( $details[
'blacklistedExt'] ) ) {
721 $msg->params( $this->
getLanguage()->commaList( $details[
'blacklistedExt'] ) );
723 $msg->params( $details[
'finalExt'] );
726 array_unique( $this->
getConfig()->
get( MainConfigNames::FileExtensions ) );
727 $msg->params( $this->
getLanguage()->commaList( $extensions ),
728 count( $extensions ) );
733 if ( isset( $details[
'blacklistedExt'] ) ) {
734 $msg->params( count( $details[
'blacklistedExt'] ) );
741 case UploadBase::VERIFICATION_ERROR:
742 unset( $details[
'status'] );
743 $code = array_shift( $details[
'details'] );
746 case UploadBase::HOOK_ABORTED:
747 if ( is_array( $details[
'error'] ) ) { # allow hooks to
return error details in an array
748 $args = $details[
'error'];
749 $error = array_shift( $args );
751 $error = $details[
'error'];
758 throw new UnexpectedValueException( __METHOD__ .
": Unknown value `{$details['status']}`" );
771 $success = $this->mUpload->unsaveUploadedFile();
774 $this->
msg(
'filedeleteerror' )
775 ->params( $this->mUpload->getTempPath() )
799 $file = $exists[
'file'];
800 $filename =
$file->getTitle()->getPrefixedText();
803 if ( $exists[
'warning'] ==
'exists' ) {
805 $warnMsg =
wfMessage(
'fileexists', $filename );
806 } elseif ( $exists[
'warning'] ==
'page-exists' ) {
808 $warnMsg =
wfMessage(
'filepageexists', $filename );
809 } elseif ( $exists[
'warning'] ==
'exists-normalized' ) {
810 $warnMsg =
wfMessage(
'fileexists-extension', $filename,
811 $exists[
'normalizedFile']->
getTitle()->getPrefixedText() );
812 } elseif ( $exists[
'warning'] ==
'thumb' ) {
814 $warnMsg =
wfMessage(
'fileexists-thumbnail-yes',
815 $exists[
'thumbFile']->
getTitle()->getPrefixedText(), $filename );
816 } elseif ( $exists[
'warning'] ==
'thumb-name' ) {
818 $name =
$file->getName();
819 $badPart = substr( $name, 0, strpos( $name,
'-' ) + 1 );
820 $warnMsg =
wfMessage(
'file-thumbnail-no', $badPart );
821 } elseif ( $exists[
'warning'] ==
'bad-prefix' ) {
822 $warnMsg =
wfMessage(
'filename-bad-prefix', $exists[
'prefix'] );
825 return $warnMsg ? $warnMsg->page(
$file->getTitle() )->parse() :
'';
838 $gallery = ImageGalleryBase::factory(
false, $this->
getContext() );
839 $gallery->setShowBytes(
false );
840 $gallery->setShowDimensions(
false );
841 foreach ( $dupes as
$file ) {
842 $gallery->add(
$file->getTitle() );
846 $this->
msg(
'file-exists-duplicate' )->numParams( count( $dupes ) )->parse() .
847 $gallery->toHTML() .
"</li>\n";
864 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.
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.
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)
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.