7 * Form for handling uploads and special page.
11 class SpecialUpload extends SpecialPage {
13 * Constructor : initialise object
14 * Get data POSTed through the form and assign them to the object
15 * @param WebRequest $request Data posted.
17 public function __construct( $request = null ) {
20 parent::__construct( 'Upload', 'upload' );
22 $this->loadRequest( is_null( $request ) ? $wgRequest : $request );
25 /** Misc variables **/
26 protected $mRequest; // The WebRequest or FauxRequest this form is supposed to handle
27 protected $mSourceType;
29 protected $mLocalFile;
30 protected $mUploadClicked;
32 /** User input variables from the "description" section **/
33 public $mDesiredDestName; // The requested target file name
37 /** User input variables from the root section **/
38 protected $mIgnoreWarning;
39 protected $mWatchThis;
40 protected $mCopyrightStatus;
41 protected $mCopyrightSource;
43 /** Hidden variables **/
44 protected $mDestWarningAck;
45 protected $mForReUpload; // The user followed an "overwrite this file" link
46 protected $mCancelUpload; // The user clicked "Cancel and return to upload form" button
48 protected $mUploadSuccessful = false; // Subclasses can use this to determine whether a file was uploaded
50 /** Text injection points for hooks not using HTMLForm **/
51 public $uploadFormTextTop;
52 public $uploadFormTextAfterSummary;
56 * Initialize instance variables from request and create an Upload handler
58 * @param WebRequest $request The request to extract variables from
60 protected function loadRequest( $request ) {
63 $this->mRequest = $request;
64 $this->mSourceType = $request->getVal( 'wpSourceType', 'file' );
65 $this->mUpload = UploadBase::createFromRequest( $request );
66 $this->mUploadClicked = $request->wasPosted()
67 && ( $request->getCheck( 'wpUpload' )
68 || $request->getCheck( 'wpUploadIgnoreWarning' ) );
70 // Guess the desired name from the filename if not provided
71 $this->mDesiredDestName = $request->getText( 'wpDestFile' );
72 if( !$this->mDesiredDestName && $request->getFileName( 'wpUploadFile' ) !== null )
73 $this->mDesiredDestName = $request->getFileName( 'wpUploadFile' );
74 $this->mComment = $request->getText( 'wpUploadDescription' );
75 $this->mLicense = $request->getText( 'wpLicense' );
78 $this->mDestWarningAck = $request->getText( 'wpDestFileWarningAck' );
79 $this->mIgnoreWarning = $request->getCheck( 'wpIgnoreWarning' )
80 || $request->getCheck( 'wpUploadIgnoreWarning' );
81 $this->mWatchthis = $request->getBool( 'wpWatchthis' ) && $wgUser->isLoggedIn();
82 $this->mCopyrightStatus = $request->getText( 'wpUploadCopyStatus' );
83 $this->mCopyrightSource = $request->getText( 'wpUploadSource' );
86 $this->mForReUpload = $request->getBool( 'wpForReUpload' ); // updating a file
87 $this->mCancelUpload = $request->getCheck( 'wpCancelUpload' )
88 || $request->getCheck( 'wpReUpload' ); // b/w compat
90 // If it was posted check for the token (no remote POST'ing with user credentials)
91 $token = $request->getVal( 'wpEditToken' );
92 if( $this->mSourceType == 'file' && $token == null ) {
93 // Skip token check for file uploads as that can't be faked via JS...
94 // Some client-side tools don't expect to need to send wpEditToken
95 // with their submissions, as that's new in 1.16.
96 $this->mTokenOk = true;
98 $this->mTokenOk = $wgUser->matchEditToken( $token );
101 $this->uploadFormTextTop = '';
102 $this->uploadFormTextAfterSummary = '';
106 * This page can be shown if uploading is enabled.
107 * Handle permission checking elsewhere in order to be able to show
108 * custom error messages.
113 public function userCanExecute( $user ) {
114 return UploadBase::isEnabled() && parent::userCanExecute( $user );
118 * Special page entry point
120 public function execute( $par ) {
121 global $wgUser, $wgOut, $wgRequest;
124 $this->outputHeader();
126 # Check uploading enabled
127 if( !UploadBase::isEnabled() ) {
128 $wgOut->showErrorPage( 'uploaddisabled', 'uploaddisabledtext' );
133 global $wgGroupPermissions;
134 if( !$wgUser->isAllowed( 'upload' ) ) {
135 if( !$wgUser->isLoggedIn() && ( $wgGroupPermissions['user']['upload']
136 || $wgGroupPermissions['autoconfirmed']['upload'] ) ) {
137 // Custom message if logged-in users without any special rights can upload
138 $wgOut->showErrorPage( 'uploadnologin', 'uploadnologintext' );
140 $wgOut->permissionRequired( 'upload' );
146 if( $wgUser->isBlocked() ) {
147 $wgOut->blockedPage();
151 # Check whether we actually want to allow changing stuff
153 $wgOut->readOnlyPage();
157 # Unsave the temporary file in case this was a cancelled upload
158 if ( $this->mCancelUpload ) {
159 if ( !$this->unsaveUploadedFile() )
160 # Something went wrong, so unsaveUploadedFile showed a warning
164 # Process upload or show a form
165 if ( $this->mTokenOk && !$this->mCancelUpload
166 && ( $this->mUpload && $this->mUploadClicked ) ) {
167 $this->processUpload();
169 # Backwards compatibility hook
170 if( !wfRunHooks( 'UploadForm:initial', array( &$this ) ) )
172 wfDebug( "Hook 'UploadForm:initial' broke output of the upload form" );
176 $this->showUploadForm( $this->getUploadForm() );
180 if ( $this->mUpload )
181 $this->mUpload->cleanupTempFile();
185 * Show the main upload form
187 * @param mixed $form An HTMLForm instance or HTML string to show
189 protected function showUploadForm( $form ) {
190 # Add links if file was previously deleted
191 if ( !$this->mDesiredDestName ) {
192 $this->showViewDeletedLinks();
195 if ( $form instanceof HTMLForm ) {
199 $wgOut->addHTML( $form );
205 * Get an UploadForm instance with title and text properly set.
207 * @param string $message HTML string to add to the form
208 * @param string $sessionKey Session key in case this is a stashed upload
211 protected function getUploadForm( $message = '', $sessionKey = '', $hideIgnoreWarning = false ) {
215 $form = new UploadForm( array(
216 'watch' => $this->getWatchCheck(),
217 'forreupload' => $this->mForReUpload,
218 'sessionkey' => $sessionKey,
219 'hideignorewarning' => $hideIgnoreWarning,
220 'destwarningack' => (bool)$this->mDestWarningAck,
222 'texttop' => $this->uploadFormTextTop,
223 'textaftersummary' => $this->uploadFormTextAfterSummary,
224 'destfile' => $this->mDesiredDestName,
226 $form->setTitle( $this->getTitle() );
228 # Check the token, but only if necessary
229 if( !$this->mTokenOk && !$this->mCancelUpload
230 && ( $this->mUpload && $this->mUploadClicked ) ) {
231 $form->addPreText( wfMsgExt( 'session_fail_preview', 'parseinline' ) );
235 $form->addPreText( '<div id="uploadtext">' .
236 wfMsgExt( 'uploadtext', 'parse', array( $this->mDesiredDestName ) ) .
238 # Add upload error message
239 $form->addPreText( $message );
242 $uploadFooter = wfMsgNoTrans( 'uploadfooter' );
243 if ( $uploadFooter != '-' && !wfEmptyMsg( 'uploadfooter', $uploadFooter ) ) {
244 $form->addPostText( '<div id="mw-upload-footer-message">'
245 . $wgOut->parse( $uploadFooter ) . "</div>\n" );
253 * Shows the "view X deleted revivions link""
255 protected function showViewDeletedLinks() {
256 global $wgOut, $wgUser;
258 $title = Title::makeTitleSafe( NS_FILE, $this->mDesiredDestName );
259 // Show a subtitle link to deleted revisions (to sysops et al only)
260 if( $title instanceof Title ) {
261 $count = $title->isDeleted();
262 if ( $count > 0 && $wgUser->isAllowed( 'deletedhistory' ) ) {
264 $wgUser->isAllowed( 'delete' ) ? 'thisisdeleted' : 'viewdeleted',
265 array( 'parse', 'replaceafter' ),
266 $wgUser->getSkin()->linkKnown(
267 SpecialPage::getTitleFor( 'Undelete', $title->getPrefixedText() ),
268 wfMsgExt( 'restorelink', array( 'parsemag', 'escape' ), $count )
271 $wgOut->addHTML( "<div id=\"contentSub2\">{$link}</div>" );
275 // Show the relevant lines from deletion log (for still deleted files only)
276 if( $title instanceof Title && $title->isDeletedQuick() && !$title->exists() ) {
277 $this->showDeletionLog( $wgOut, $title->getPrefixedText() );
282 * Stashes the upload and shows the main upload form.
284 * Note: only errors that can be handled by changing the name or
285 * description should be redirected here. It should be assumed that the
286 * file itself is sane and has passed UploadBase::verifyFile. This
287 * essentially means that UploadBase::VERIFICATION_ERROR and
288 * UploadBase::EMPTY_FILE should not be passed here.
290 * @param string $message HTML message to be passed to mainUploadForm
292 protected function showRecoverableUploadError( $message ) {
293 $sessionKey = $this->mUpload->stashSession();
294 $message = '<h2>' . wfMsgHtml( 'uploadwarning' ) . "</h2>\n" .
295 '<div class="error">' . $message . "</div>\n";
297 $form = $this->getUploadForm( $message, $sessionKey );
298 $form->setSubmitText( wfMsg( 'upload-tryagain' ) );
299 $this->showUploadForm( $form );
302 * Stashes the upload, shows the main form, but adds an "continue anyway button".
303 * Also checks whether there are actually warnings to display.
305 * @param array $warnings
306 * @return boolean true if warnings were displayed, false if there are no
307 * warnings and the should continue processing like there was no warning
309 protected function showUploadWarning( $warnings ) {
312 # If there are no warnings, or warnings we can ignore, return early.
313 # mDestWarningAck is set when some javascript has shown the warning
314 # to the user. mForReUpload is set when the user clicks the "upload a
316 if ( !$warnings || ( count( $warnings ) == 1 &&
317 isset( $warnings['exists'] ) &&
318 ( $this->mDestWarningAck || $this->mForReUpload ) ) )
323 $sessionKey = $this->mUpload->stashSession();
325 $sk = $wgUser->getSkin();
327 $warningHtml = '<h2>' . wfMsgHtml( 'uploadwarning' ) . "</h2>\n"
328 . '<ul class="warning">';
329 foreach( $warnings as $warning => $args ) {
331 if( $warning == 'exists' ) {
332 $msg = "\t<li>" . self::getExistsWarning( $args ) . "</li>\n";
333 } elseif( $warning == 'duplicate' ) {
334 $msg = self::getDupeWarning( $args );
335 } elseif( $warning == 'duplicate-archive' ) {
336 $msg = "\t<li>" . wfMsgExt( 'file-deleted-duplicate', 'parseinline',
337 array( Title::makeTitle( NS_FILE, $args )->getPrefixedText() ) )
340 if ( $args === true )
342 elseif ( !is_array( $args ) )
343 $args = array( $args );
344 $msg = "\t<li>" . wfMsgExt( $warning, 'parseinline', $args ) . "</li>\n";
346 $warningHtml .= $msg;
348 $warningHtml .= "</ul>\n";
349 $warningHtml .= wfMsgExt( 'uploadwarning-text', 'parse' );
351 $form = $this->getUploadForm( $warningHtml, $sessionKey, /* $hideIgnoreWarning */ true );
352 $form->setSubmitText( wfMsg( 'upload-tryagain' ) );
353 $form->addButton( 'wpUploadIgnoreWarning', wfMsg( 'ignorewarning' ) );
354 $form->addButton( 'wpCancelUpload', wfMsg( 'reuploaddesc' ) );
356 $this->showUploadForm( $form );
358 # Indicate that we showed a form
363 * Show the upload form with error message, but do not stash the file.
365 * @param string $message
367 protected function showUploadError( $message ) {
368 $message = '<h2>' . wfMsgHtml( 'uploadwarning' ) . "</h2>\n" .
369 '<div class="error">' . $message . "</div>\n";
370 $this->showUploadForm( $this->getUploadForm( $message ) );
375 * Checks are made in SpecialUpload::execute()
377 protected function processUpload() {
378 global $wgUser, $wgOut;
380 // Verify permissions
381 $permErrors = $this->mUpload->verifyPermissions( $wgUser );
382 if( $permErrors !== true ) {
383 $wgOut->showPermissionsErrorPage( $permErrors );
387 // Fetch the file if required
388 $status = $this->mUpload->fetchFile();
389 if( !$status->isOK() ) {
390 $this->showUploadForm( $this->getUploadForm( $wgOut->parse( $status->getWikiText() ) ) );
394 // Deprecated backwards compatibility hook
395 if( !wfRunHooks( 'UploadForm:BeforeProcessing', array( &$this ) ) )
397 wfDebug( "Hook 'UploadForm:BeforeProcessing' broke processing the file.\n" );
398 return array( 'status' => UploadBase::BEFORE_PROCESSING );
402 // Upload verification
403 $details = $this->mUpload->verifyUpload();
404 if ( $details['status'] != UploadBase::OK ) {
405 $this->processVerificationError( $details );
409 $this->mLocalFile = $this->mUpload->getLocalFile();
411 // Check warnings if necessary
412 if( !$this->mIgnoreWarning ) {
413 $warnings = $this->mUpload->checkWarnings();
414 if( $this->showUploadWarning( $warnings ) ) {
419 // Get the page text if this is not a reupload
420 if( !$this->mForReUpload ) {
421 $pageText = self::getInitialPageText( $this->mComment, $this->mLicense,
422 $this->mCopyrightStatus, $this->mCopyrightSource );
426 $status = $this->mUpload->performUpload( $this->mComment, $pageText, $this->mWatchthis, $wgUser );
427 if ( !$status->isGood() ) {
428 $this->showUploadError( $wgOut->parse( $status->getWikiText() ) );
432 // Success, redirect to description page
433 $this->mUploadSuccessful = true;
434 wfRunHooks( 'SpecialUploadComplete', array( &$this ) );
435 $wgOut->redirect( $this->mLocalFile->getTitle()->getFullURL() );
440 * Get the initial image page text based on a comment and optional file status information
442 public static function getInitialPageText( $comment = '', $license = '', $copyStatus = '', $source = '' ) {
443 global $wgUseCopyrightUpload;
444 if ( $wgUseCopyrightUpload ) {
446 if ( $license != '' ) {
447 $licensetxt = '== ' . wfMsgForContent( 'license-header' ) . " ==\n" . '{{' . $license . '}}' . "\n";
449 $pageText = '== ' . wfMsgForContent ( 'filedesc' ) . " ==\n" . $comment . "\n" .
450 '== ' . wfMsgForContent ( 'filestatus' ) . " ==\n" . $copyStatus . "\n" .
452 '== ' . wfMsgForContent ( 'filesource' ) . " ==\n" . $source ;
454 if ( $license != '' ) {
455 $filedesc = $comment == '' ? '' : '== ' . wfMsgForContent ( 'filedesc' ) . " ==\n" . $comment . "\n";
456 $pageText = $filedesc .
457 '== ' . wfMsgForContent ( 'license-header' ) . " ==\n" . '{{' . $license . '}}' . "\n";
459 $pageText = $comment;
466 * See if we should check the 'watch this page' checkbox on the form
467 * based on the user's preferences and whether we're being asked
468 * to create a new file or update an existing one.
470 * In the case where 'watch edits' is off but 'watch creations' is on,
471 * we'll leave the box unchecked.
473 * Note that the page target can be changed *on the form*, so our check
474 * state can get out of sync.
476 protected function getWatchCheck() {
478 if( $wgUser->getOption( 'watchdefault' ) ) {
483 $local = wfLocalFile( $this->mDesiredDestName );
484 if( $local && $local->exists() ) {
485 // We're uploading a new version of an existing file.
486 // No creation, so don't watch it if we're not already.
487 return $local->getTitle()->userIsWatching();
489 // New page should get watched if that's our option.
490 return $wgUser->getOption( 'watchcreations' );
496 * Provides output to the user for a result of UploadBase::verifyUpload
498 * @param array $details Result of UploadBase::verifyUpload
500 protected function processVerificationError( $details ) {
501 global $wgFileExtensions, $wgLang;
503 switch( $details['status'] ) {
505 /** Statuses that only require name changing **/
506 case UploadBase::MIN_LENGTH_PARTNAME:
507 $this->showRecoverableUploadError( wfMsgHtml( 'minlength1' ) );
509 case UploadBase::ILLEGAL_FILENAME:
510 $this->showRecoverableUploadError( wfMsgExt( 'illegalfilename',
511 'parseinline', $details['filtered'] ) );
513 case UploadBase::OVERWRITE_EXISTING_FILE:
514 $this->showRecoverableUploadError( wfMsgExt( $details['overwrite'],
517 case UploadBase::FILETYPE_MISSING:
518 $this->showRecoverableUploadError( wfMsgExt( 'filetype-missing',
522 /** Statuses that require reuploading **/
523 case UploadBase::EMPTY_FILE:
524 $this->showUploadForm( $this->getUploadForm( wfMsgHtml( 'emptyfile' ) ) );
526 case UploadBase::FILETYPE_BADTYPE:
527 $finalExt = $details['finalExt'];
528 $this->showUploadError(
529 wfMsgExt( 'filetype-banned-type',
530 array( 'parseinline' ),
531 htmlspecialchars( $finalExt ),
533 wfMsgExt( 'comma-separator', array( 'escapenoentities' ) ),
536 $wgLang->formatNum( count( $wgFileExtensions ) )
540 case UploadBase::VERIFICATION_ERROR:
541 unset( $details['status'] );
542 $code = array_shift( $details['details'] );
543 $this->showUploadError( wfMsgExt( $code, 'parseinline', $details['details'] ) );
545 case UploadBase::HOOK_ABORTED:
546 if ( is_array( $details['error'] ) ) { # allow hooks to return error details in an array
547 $args = $details['error'];
548 $error = array_shift( $args );
550 $error = $details['error'];
554 $this->showUploadError( wfMsgExt( $error, 'parseinline', $args ) );
557 throw new MWException( __METHOD__ . ": Unknown value `{$details['status']}`" );
562 * Remove a temporarily kept file stashed by saveTempUploadedFile().
566 protected function unsaveUploadedFile() {
568 if ( !( $this->mUpload instanceof UploadFromStash ) )
570 $success = $this->mUpload->unsaveUploadedFile();
572 $wgOut->showFileDeleteError( $this->mUpload->getTempPath() );
579 /*** Functions for formatting warnings ***/
582 * Formats a result of UploadBase::getExistsWarning as HTML
583 * This check is static and can be done pre-upload via AJAX
585 * @param array $exists The result of UploadBase::getExistsWarning
586 * @return string Empty string if there is no warning or an HTML fragment
588 public static function getExistsWarning( $exists ) {
589 global $wgUser, $wgContLang;
594 $file = $exists['file'];
595 $filename = $file->getTitle()->getPrefixedText();
598 $sk = $wgUser->getSkin();
600 if( $exists['warning'] == 'exists' ) {
602 $warning = wfMsgExt( 'fileexists', 'parseinline', $filename );
603 } elseif( $exists['warning'] == 'page-exists' ) {
604 // Page exists but file does not
605 $warning = wfMsgExt( 'filepageexists', 'parseinline', $filename );
606 } elseif ( $exists['warning'] == 'exists-normalized' ) {
607 $warning = wfMsgExt( 'fileexists-extension', 'parseinline', $filename,
608 $exists['normalizedFile']->getTitle()->getPrefixedText() );
609 } elseif ( $exists['warning'] == 'thumb' ) {
610 // Swapped argument order compared with other messages for backwards compatibility
611 $warning = wfMsgExt( 'fileexists-thumbnail-yes', 'parseinline',
612 $exists['thumbFile']->getTitle()->getPrefixedText(), $filename );
613 } elseif ( $exists['warning'] == 'thumb-name' ) {
614 // Image w/o '180px-' does not exists, but we do not like these filenames
615 $name = $file->getName();
616 $badPart = substr( $name, 0, strpos( $name, '-' ) + 1 );
617 $warning = wfMsgExt( 'file-thumbnail-no', 'parseinline', $badPart );
618 } elseif ( $exists['warning'] == 'bad-prefix' ) {
619 $warning = wfMsgExt( 'filename-bad-prefix', 'parseinline', $exists['prefix'] );
620 } elseif ( $exists['warning'] == 'was-deleted' ) {
621 # If the file existed before and was deleted, warn the user of this
622 $ltitle = SpecialPage::getTitleFor( 'Log' );
623 $llink = $sk->linkKnown(
625 wfMsgHtml( 'deletionlog' ),
632 $warning = wfMsgWikiHtml( 'filewasdeleted', $llink );
639 * Get a list of warnings
641 * @param string local filename, e.g. 'file exists', 'non-descriptive filename'
642 * @return array list of warning messages
644 public static function ajaxGetExistsWarning( $filename ) {
645 $file = wfFindFile( $filename );
647 // Force local file so we have an object to do further checks against
648 // if there isn't an exact match...
649 $file = wfLocalFile( $filename );
653 $exists = UploadBase::getExistsWarning( $file );
654 $warning = self::getExistsWarning( $exists );
655 if ( $warning !== '' ) {
656 $s = "<div>$warning</div>";
663 * Construct a warning and a gallery from an array of duplicate files.
665 public static function getDupeWarning( $dupes ) {
669 foreach( $dupes as $file ) {
670 $title = $file->getTitle();
671 $msg .= $title->getPrefixedText() .
672 "|" . $title->getText() . "\n";
674 $msg .= "</gallery>";
676 wfMsgExt( "file-exists-duplicate", array( "parse" ), count( $dupes ) ) .
677 $wgOut->parse( $msg ) .
687 * Sub class of HTMLForm that provides the form section of SpecialUpload
689 class UploadForm extends HTMLForm {
691 protected $mForReUpload;
692 protected $mSessionKey;
693 protected $mHideIgnoreWarning;
694 protected $mDestWarningAck;
695 protected $mDestFile;
698 protected $mTextAfterSummary;
700 protected $mSourceIds;
702 public function __construct( $options = array() ) {
705 $this->mWatch = !empty( $options['watch'] );
706 $this->mForReUpload = !empty( $options['forreupload'] );
707 $this->mSessionKey = isset( $options['sessionkey'] )
708 ? $options['sessionkey'] : '';
709 $this->mHideIgnoreWarning = !empty( $options['hideignorewarning'] );
710 $this->mDestWarningAck = !empty( $options['destwarningack'] );
712 $this->mTextTop = $options['texttop'];
713 $this->mTextAfterSummary = $options['textaftersummary'];
714 $this->mDestFile = isset( $options['destfile'] ) ? $options['destfile'] : '';
716 $sourceDescriptor = $this->getSourceSection();
717 $descriptor = $sourceDescriptor
718 + $this->getDescriptionSection()
719 + $this->getOptionsSection();
721 wfRunHooks( 'UploadFormInitDescriptor', array( &$descriptor ) );
722 parent::__construct( $descriptor, 'upload' );
724 # Set some form properties
725 $this->setSubmitText( wfMsg( 'uploadbtn' ) );
726 $this->setSubmitName( 'wpUpload' );
727 $this->setSubmitTooltip( 'upload' );
728 $this->setId( 'mw-upload-form' );
730 # Build a list of IDs for javascript insertion
731 $this->mSourceIds = array();
732 foreach ( $sourceDescriptor as $key => $field ) {
733 if ( !empty( $field['id'] ) )
734 $this->mSourceIds[] = $field['id'];
740 * Get the descriptor of the fieldset that contains the file source
741 * selection. The section is 'source'
743 * @return array Descriptor array
745 protected function getSourceSection() {
746 global $wgLang, $wgUser, $wgRequest;
748 if ( $this->mSessionKey ) {
750 'wpSessionKey' => array(
752 'default' => $this->mSessionKey,
754 'wpSourceType' => array(
756 'default' => 'Stash',
761 $canUploadByUrl = UploadFromUrl::isEnabled() && $wgUser->isAllowed( 'upload_by_url' );
762 $radio = $canUploadByUrl;
763 $selectedSourceType = strtolower( $wgRequest->getText( 'wpSourceType', 'File' ) );
765 $descriptor = array();
766 if ( $this->mTextTop ) {
767 $descriptor['UploadFormTextTop'] = array(
769 'section' => 'source',
770 'default' => $this->mTextTop,
775 $descriptor['UploadFile'] = array(
776 'class' => 'UploadSourceField',
777 'section' => 'source',
779 'id' => 'wpUploadFile',
780 'label-message' => 'sourcefilename',
781 'upload-type' => 'File',
783 'help' => wfMsgExt( 'upload-maxfilesize',
784 array( 'parseinline', 'escapenoentities' ),
786 wfShorthandToInteger( ini_get( 'upload_max_filesize' ) )
788 ) . ' ' . wfMsgHtml( 'upload_source_file' ),
789 'checked' => $selectedSourceType == 'file',
791 if ( $canUploadByUrl ) {
792 global $wgMaxUploadSize;
793 $descriptor['UploadFileURL'] = array(
794 'class' => 'UploadSourceField',
795 'section' => 'source',
796 'id' => 'wpUploadFileURL',
797 'label-message' => 'sourceurl',
798 'upload-type' => 'url',
800 'help' => wfMsgExt( 'upload-maxfilesize',
801 array( 'parseinline', 'escapenoentities' ),
802 $wgLang->formatSize( $wgMaxUploadSize )
803 ) . ' ' . wfMsgHtml( 'upload_source_url' ),
804 'checked' => $selectedSourceType == 'url',
807 wfRunHooks( 'UploadFormSourceDescriptors', array( &$descriptor, &$radio, $selectedSourceType ) );
809 $descriptor['Extensions'] = array(
811 'section' => 'source',
812 'default' => $this->getExtensionsMessage(),
820 * Get the messages indicating which extensions are preferred and prohibitted.
822 * @return string HTML string containing the message
824 protected function getExtensionsMessage() {
825 # Print a list of allowed file extensions, if so configured. We ignore
826 # MIME type here, it's incomprehensible to most people and too long.
827 global $wgLang, $wgCheckFileExtensions, $wgStrictFileExtensions,
828 $wgFileExtensions, $wgFileBlacklist;
830 $allowedExtensions = '';
831 if( $wgCheckFileExtensions ) {
832 if( $wgStrictFileExtensions ) {
833 # Everything not permitted is banned
835 '<div id="mw-upload-permitted">' .
836 wfMsgWikiHtml( 'upload-permitted', $wgLang->commaList( $wgFileExtensions ) ) .
839 # We have to list both preferred and prohibited
841 '<div id="mw-upload-preferred">' .
842 wfMsgWikiHtml( 'upload-preferred', $wgLang->commaList( $wgFileExtensions ) ) .
844 '<div id="mw-upload-prohibited">' .
845 wfMsgWikiHtml( 'upload-prohibited', $wgLang->commaList( $wgFileBlacklist ) ) .
849 # Everything is permitted.
850 $extensionsList = '';
852 return $extensionsList;
856 * Get the descriptor of the fieldset that contains the file description
857 * input. The section is 'description'
859 * @return array Descriptor array
861 protected function getDescriptionSection() {
862 global $wgUser, $wgOut;
864 $cols = intval( $wgUser->getOption( 'cols' ) );
865 if( $wgUser->getOption( 'editwidth' ) ) {
866 $wgOut->addInlineStyle( '#mw-htmlform-description { width: 100%; }' );
872 'section' => 'description',
873 'id' => 'wpDestFile',
874 'label-message' => 'destfilename',
876 'default' => $this->mDestFile,
877 # FIXME: hack to work around poor handling of the 'default' option in HTMLForm
878 'nodata' => strval( $this->mDestFile ) !== '',
880 'UploadDescription' => array(
881 'type' => 'textarea',
882 'section' => 'description',
883 'id' => 'wpUploadDescription',
884 'label-message' => $this->mForReUpload
885 ? 'filereuploadsummary'
886 : 'fileuploadsummary',
891 if ( $this->mTextAfterSummary ) {
892 $descriptor['UploadFormTextAfterSummary'] = array(
894 'section' => 'description',
895 'default' => $this->mTextAfterSummary,
900 $descriptor += array(
901 'EditTools' => array(
902 'type' => 'edittools',
903 'section' => 'description',
907 'class' => 'Licenses',
908 'section' => 'description',
910 'label-message' => 'license',
913 if ( $this->mForReUpload )
914 $descriptor['DestFile']['readonly'] = true;
916 global $wgUseCopyrightUpload;
917 if ( $wgUseCopyrightUpload ) {
918 $descriptor['UploadCopyStatus'] = array(
920 'section' => 'description',
921 'id' => 'wpUploadCopyStatus',
922 'label-message' => 'filestatus',
924 $descriptor['UploadSource'] = array(
926 'section' => 'description',
927 'id' => 'wpUploadSource',
928 'label-message' => 'filesource',
936 * Get the descriptor of the fieldset that contains the upload options,
937 * such as "watch this file". The section is 'options'
939 * @return array Descriptor array
941 protected function getOptionsSection() {
942 global $wgUser, $wgOut;
944 if( $wgUser->isLoggedIn() ) {
946 'Watchthis' => array(
948 'id' => 'wpWatchthis',
949 'label-message' => 'watchthisupload',
950 'section' => 'options',
951 'default' => $wgUser->getOption( 'watchcreations' ),
955 if( !$this->mHideIgnoreWarning ) {
956 $descriptor['IgnoreWarning'] = array(
958 'id' => 'wpIgnoreWarning',
959 'label-message' => 'ignorewarnings',
960 'section' => 'options',
964 $descriptor['wpDestFileWarningAck'] = array(
966 'id' => 'wpDestFileWarningAck',
967 'default' => $this->mDestWarningAck ? '1' : '',
970 if ( $this->mForReUpload ) {
971 $descriptor['wpForReUpload'] = array(
973 'id' => 'wpForReUpload',
983 * Add the upload JS and show the form.
985 public function show() {
986 $this->addUploadJS();
991 * Add upload JS to $wgOut
993 * @param bool $autofill Whether or not to autofill the destination
996 protected function addUploadJS( ) {
997 global $wgUseAjax, $wgAjaxUploadDestCheck, $wgAjaxLicensePreview, $wgEnableAPI;
1000 $useAjaxDestCheck = $wgUseAjax && $wgAjaxUploadDestCheck;
1001 $useAjaxLicensePreview = $wgUseAjax && $wgAjaxLicensePreview && $wgEnableAPI;
1003 $scriptVars = array(
1004 'wgAjaxUploadDestCheck' => $useAjaxDestCheck,
1005 'wgAjaxLicensePreview' => $useAjaxLicensePreview,
1006 'wgUploadAutoFill' => !$this->mForReUpload &&
1007 // If we received mDestFile from the request, don't autofill
1008 // the wpDestFile textbox
1009 $this->mDestFile === '',
1010 'wgUploadSourceIds' => $this->mSourceIds,
1013 $wgOut->addScript( Skin::makeVariablesScript( $scriptVars ) );
1015 // For <charinsert> support
1016 $wgOut->addScriptFile( 'edit.js' );
1017 $wgOut->addScriptFile( 'upload.js' );
1021 * Empty function; submission is handled elsewhere.
1023 * @return bool false
1025 function trySubmit() {
1032 * A form field that contains a radio box in the label
1034 class UploadSourceField extends HTMLTextField {
1035 function getLabelHtml() {
1036 $id = "wpSourceType{$this->mParams['upload-type']}";
1037 $label = Html::rawElement( 'label', array( 'for' => $id ), $this->mLabel );
1039 if ( !empty( $this->mParams['radio'] ) ) {
1041 'name' => 'wpSourceType',
1044 'value' => $this->mParams['upload-type'],
1046 if ( !empty( $this->mParams['checked'] ) )
1047 $attribs['checked'] = 'checked';
1048 $label .= Html::element( 'input', $attribs );
1051 return Html::rawElement( 'td', array( 'class' => 'mw-label' ), $label );
1053 function getSize() {
1054 return isset( $this->mParams['size'] )
1055 ? $this->mParams['size']