7 * Form for handling uploads and special page.
10 class SpecialUpload
extends SpecialPage
{
12 * Constructor : initialise object
13 * Get data POSTed through the form and assign them to the object
14 * @param WebRequest $request Data posted.
16 public function __construct( $request = null ) {
19 parent
::__construct( 'Upload', 'upload' );
21 $this->loadRequest( is_null( $request ) ?
$wgRequest : $request );
24 /** Misc variables **/
25 protected $mRequest; // The WebRequest or FauxRequest this form is supposed to handle
26 protected $mSourceType;
28 protected $mLocalFile;
29 protected $mUploadClicked;
31 /** User input variables from the "description" section **/
32 public $mDesiredDestName; // The requested target file name
36 /** User input variables from the root section **/
37 protected $mIgnoreWarning;
38 protected $mWatchThis;
39 protected $mCopyrightStatus;
40 protected $mCopyrightSource;
42 /** Hidden variables **/
43 protected $mDestWarningAck;
44 protected $mForReUpload; // The user followed an "overwrite this file" link
45 protected $mCancelUpload; // The user clicked "Cancel and return to upload form" button
47 protected $mUploadSuccessful = false; // Subclasses can use this to determine whether a file was uploaded
49 /** Text injection points for hooks not using HTMLForm **/
50 public $uploadFormTextTop;
51 public $uploadFormTextAfterSummary;
54 * Initialize instance variables from request and create an Upload handler
56 * @param WebRequest $request The request to extract variables from
58 protected function loadRequest( $request ) {
61 $this->mRequest
= $request;
62 $this->mSourceType
= $request->getVal( 'wpSourceType', 'file' );
63 $this->mUpload
= UploadBase
::createFromRequest( $request );
64 $this->mUploadClicked
= $request->wasPosted()
65 && ( $request->getCheck( 'wpUpload' )
66 ||
$request->getCheck( 'wpUploadIgnoreWarning' ) );
68 // Guess the desired name from the filename if not provided
69 $this->mDesiredDestName
= $request->getText( 'wpDestFile' );
70 if( !$this->mDesiredDestName
) {
71 $this->mDesiredDestName
= $request->getText( 'wpUploadFile' );
73 $this->mComment
= $request->getText( 'wpUploadDescription' );
74 $this->mLicense
= $request->getText( 'wpLicense' );
77 $this->mDestWarningAck
= $request->getText( 'wpDestFileWarningAck' );
78 $this->mIgnoreWarning
= $request->getCheck( 'wpIgnoreWarning' )
79 ||
$request->getCheck( 'wpUploadIgnoreWarning' );
80 $this->mWatchthis
= $request->getBool( 'wpWatchthis' ) && $wgUser->isLoggedIn();
81 $this->mCopyrightStatus
= $request->getText( 'wpUploadCopyStatus' );
82 $this->mCopyrightSource
= $request->getText( 'wpUploadSource' );
85 $this->mForReUpload
= $request->getBool( 'wpForReUpload' ); // updating a file
86 $this->mCancelUpload
= $request->getCheck( 'wpCancelUpload' )
87 ||
$request->getCheck( 'wpReUpload' ); // b/w compat
89 // If it was posted check for the token (no remote POST'ing with user credentials)
90 $token = $request->getVal( 'wpEditToken' );
91 if( $this->mSourceType
== 'file' && $token == null ) {
92 // Skip token check for file uploads as that can't be faked via JS...
93 // Some client-side tools don't expect to need to send wpEditToken
94 // with their submissions, as that's new in 1.16.
95 $this->mTokenOk
= true;
97 $this->mTokenOk
= $wgUser->matchEditToken( $token );
100 $this->uploadFormTextTop
= '';
101 $this->uploadFormTextAfterSummary
= '';
105 * This page can be shown if uploading is enabled.
106 * Handle permission checking elsewhere in order to be able to show
107 * custom error messages.
112 public function userCanExecute( $user ) {
113 return UploadBase
::isEnabled() && parent
::userCanExecute( $user );
117 * Special page entry point
119 public function execute( $par ) {
120 global $wgUser, $wgOut, $wgRequest;
123 $this->outputHeader();
125 # Check uploading enabled
126 if( !UploadBase
::isEnabled() ) {
127 $wgOut->showErrorPage( 'uploaddisabled', 'uploaddisabledtext' );
132 global $wgGroupPermissions;
133 if( !$wgUser->isAllowed( 'upload' ) ) {
134 if( !$wgUser->isLoggedIn() && ( $wgGroupPermissions['user']['upload']
135 ||
$wgGroupPermissions['autoconfirmed']['upload'] ) ) {
136 // Custom message if logged-in users without any special rights can upload
137 $wgOut->showErrorPage( 'uploadnologin', 'uploadnologintext' );
139 $wgOut->permissionRequired( 'upload' );
145 if( $wgUser->isBlocked() ) {
146 $wgOut->blockedPage();
150 # Check whether we actually want to allow changing stuff
152 $wgOut->readOnlyPage();
156 # Unsave the temporary file in case this was a cancelled upload
157 if ( $this->mCancelUpload
) {
158 if ( !$this->unsaveUploadedFile() ) {
159 # Something went wrong, so unsaveUploadedFile showed a warning
164 # Process upload or show a form
166 $this->mTokenOk
&& !$this->mCancelUpload
&&
167 ( $this->mUpload
&& $this->mUploadClicked
)
170 $this->processUpload();
172 # Backwards compatibility hook
173 if( !wfRunHooks( 'UploadForm:initial', array( &$this ) ) ) {
174 wfDebug( "Hook 'UploadForm:initial' broke output of the upload form" );
178 $this->showUploadForm( $this->getUploadForm() );
182 if ( $this->mUpload
) {
183 $this->mUpload
->cleanupTempFile();
188 * Show the main upload form
190 * @param mixed $form An HTMLForm instance or HTML string to show
192 protected function showUploadForm( $form ) {
193 # Add links if file was previously deleted
194 if ( !$this->mDesiredDestName
) {
195 $this->showViewDeletedLinks();
198 if ( $form instanceof HTMLForm
) {
202 $wgOut->addHTML( $form );
208 * Get an UploadForm instance with title and text properly set.
210 * @param string $message HTML string to add to the form
211 * @param string $sessionKey Session key in case this is a stashed upload
214 protected function getUploadForm( $message = '', $sessionKey = '', $hideIgnoreWarning = false ) {
218 $form = new UploadForm( array(
219 'watch' => $this->getWatchCheck(),
220 'forreupload' => $this->mForReUpload
,
221 'sessionkey' => $sessionKey,
222 'hideignorewarning' => $hideIgnoreWarning,
223 'destwarningack' => (bool)$this->mDestWarningAck
,
225 'texttop' => $this->uploadFormTextTop
,
226 'textaftersummary' => $this->uploadFormTextAfterSummary
,
228 $form->setTitle( $this->getTitle() );
230 # Check the token, but only if necessary
232 !$this->mTokenOk
&& !$this->mCancelUpload
&&
233 ( $this->mUpload
&& $this->mUploadClicked
)
236 $form->addPreText( wfMsgExt( 'session_fail_preview', 'parseinline' ) );
240 $form->addPreText( '<div id="uploadtext">' . wfMsgExt( 'uploadtext', 'parse' ) . '</div>');
241 # Add upload error message
242 $form->addPreText( $message );
245 $uploadFooter = wfMsgNoTrans( 'uploadfooter' );
246 if ( $uploadFooter != '-' && !wfEmptyMsg( 'uploadfooter', $uploadFooter ) ) {
247 $form->addPostText( '<div id="mw-upload-footer-message">'
248 . $wgOut->parse( $uploadFooter ) . "</div>\n" );
256 * Shows the "view X deleted revivions link""
258 protected function showViewDeletedLinks() {
259 global $wgOut, $wgUser;
261 $title = Title
::makeTitleSafe( NS_FILE
, $this->mDesiredDestName
);
262 // Show a subtitle link to deleted revisions (to sysops et al only)
263 if( $title instanceof Title
) {
264 $count = $title->isDeleted();
265 if ( $count > 0 && $wgUser->isAllowed( 'deletedhistory' ) ) {
267 $wgUser->isAllowed( 'delete' ) ?
'thisisdeleted' : 'viewdeleted',
268 array( 'parse', 'replaceafter' ),
269 $wgUser->getSkin()->linkKnown(
270 SpecialPage
::getTitleFor( 'Undelete', $title->getPrefixedText() ),
271 wfMsgExt( 'restorelink', array( 'parsemag', 'escape' ), $count )
274 $wgOut->addHTML( "<div id=\"contentSub2\">{$link}</div>" );
278 // Show the relevant lines from deletion log (for still deleted files only)
279 if( $title instanceof Title
&& $title->isDeletedQuick() && !$title->exists() ) {
280 $this->showDeletionLog( $wgOut, $title->getPrefixedText() );
285 * Stashes the upload and shows the main upload form.
287 * Note: only errors that can be handled by changing the name or
288 * description should be redirected here. It should be assumed that the
289 * file itself is sane and has passed UploadBase::verifyFile. This
290 * essentially means that UploadBase::VERIFICATION_ERROR and
291 * UploadBase::EMPTY_FILE should not be passed here.
293 * @param string $message HTML message to be passed to mainUploadForm
295 protected function showRecoverableUploadError( $message ) {
296 $sessionKey = $this->mUpload
->stashSession();
297 $message = '<h2>' . wfMsgHtml( 'uploadwarning' ) . "</h2>\n" .
298 '<div class="error">' . $message . "</div>\n";
300 $form = $this->getUploadForm( $message, $sessionKey );
301 $form->setSubmitText( wfMsg( 'upload-tryagain' ) );
302 $this->showUploadForm( $form );
305 * Stashes the upload, shows the main form, but adds an "continue anyway button".
306 * Also checks whether there are actually warnings to display.
308 * @param array $warnings
309 * @return boolean true if warnings were displayed, false if there are no
310 * warnings and the should continue processing like there was no warning
312 protected function showUploadWarning( $warnings ) {
313 # If there are no warnings, or warnings we can ignore, return early
315 !$warnings ||
( count( $warnings ) == 1 &&
316 isset( $warnings['exists']) && $this->mDestWarningAck
)
322 $sessionKey = $this->mUpload
->stashSession();
324 $warningHtml = '<h2>' . wfMsgHtml( 'uploadwarning' ) . "</h2>\n"
325 . '<ul class="warning">';
326 foreach( $warnings as $warning => $args ) {
328 if( $warning == 'exists' ) {
329 $msg = "\t<li>" . self
::getExistsWarning( $args ) . "</li>\n";
330 } elseif( $warning == 'duplicate' ) {
331 $msg = self
::getDupeWarning( $args );
332 } elseif( $warning == 'duplicate-archive' ) {
333 $msg = "\t<li>" . wfMsgExt( 'file-deleted-duplicate', 'parseinline',
334 array( Title
::makeTitle( NS_FILE
, $args )->getPrefixedText() ) )
337 if ( $args === true ) {
339 } elseif ( !is_array( $args ) ) {
340 $args = array( $args );
342 $msg = "\t<li>" . wfMsgExt( $warning, 'parseinline', $args ) . "</li>\n";
344 $warningHtml .= $msg;
346 $warningHtml .= "</ul>\n";
347 $warningHtml .= wfMsgExt( 'uploadwarning-text', 'parse' );
349 $form = $this->getUploadForm( $warningHtml, $sessionKey, /* $hideIgnoreWarning */ true );
350 $form->setSubmitText( wfMsg( 'upload-tryagain' ) );
351 $form->addButton( 'wpUploadIgnoreWarning', wfMsg( 'ignorewarning' ) );
352 $form->addButton( 'wpCancelUpload', wfMsg( 'reuploaddesc' ) );
354 $this->showUploadForm( $form );
356 # Indicate that we showed a form
361 * Show the upload form with error message, but do not stash the file.
363 * @param string $message
365 protected function showUploadError( $message ) {
366 $message = '<h2>' . wfMsgHtml( 'uploadwarning' ) . "</h2>\n" .
367 '<div class="error">' . $message . "</div>\n";
368 $this->showUploadForm( $this->getUploadForm( $message ) );
373 * Checks are made in SpecialUpload::execute()
375 protected function processUpload() {
376 global $wgUser, $wgOut;
378 // Verify permissions
379 $permErrors = $this->mUpload
->verifyPermissions( $wgUser );
380 if( $permErrors !== true ) {
381 $wgOut->showPermissionsErrorPage( $permErrors );
385 // Fetch the file if required
386 $status = $this->mUpload
->fetchFile();
387 if( !$status->isOK() ) {
388 $this->showUploadForm( $this->getUploadForm( $wgOut->parse( $status->getWikiText() ) ) );
392 // Deprecated backwards compatibility hook
393 if( !wfRunHooks( 'UploadForm:BeforeProcessing', array( &$this ) ) ) {
394 wfDebug( "Hook 'UploadForm:BeforeProcessing' broke processing the file.\n" );
395 return array( 'status' => UploadBase
::BEFORE_PROCESSING
);
399 // Upload verification
400 $details = $this->mUpload
->verifyUpload();
401 if ( $details['status'] != UploadBase
::OK
) {
402 $this->processVerificationError( $details );
406 $this->mLocalFile
= $this->mUpload
->getLocalFile();
408 // Check warnings if necessary
409 if( !$this->mIgnoreWarning
) {
410 $warnings = $this->mUpload
->checkWarnings();
411 if( $this->showUploadWarning( $warnings ) ) {
416 // Get the page text if this is not a reupload
417 if( !$this->mForReUpload
) {
418 $pageText = self
::getInitialPageText( $this->mComment
, $this->mLicense
,
419 $this->mCopyrightStatus
, $this->mCopyrightSource
);
423 $status = $this->mUpload
->performUpload( $this->mComment
, $pageText, $this->mWatchthis
, $wgUser );
424 if ( !$status->isGood() ) {
425 $this->showUploadError( $wgOut->parse( $status->getWikiText() ) );
429 // Success, redirect to description page
430 $this->mUploadSuccessful
= true;
431 wfRunHooks( 'SpecialUploadComplete', array( &$this ) );
432 $wgOut->redirect( $this->mLocalFile
->getTitle()->getFullURL() );
436 * Get the initial image page text based on a comment and optional file status information
438 public static function getInitialPageText( $comment = '', $license = '', $copyStatus = '', $source = '' ) {
439 global $wgUseCopyrightUpload;
440 if ( $wgUseCopyrightUpload ) {
442 if ( $license != '' ) {
443 $licensetxt = '== ' . wfMsgForContent( 'license-header' ) . " ==\n" . '{{' . $license . '}}' . "\n";
445 $pageText = '== ' . wfMsgForContent( 'filedesc' ) . " ==\n" . $comment . "\n" .
446 '== ' . wfMsgForContent( 'filestatus' ) . " ==\n" . $copyStatus . "\n" .
448 '== ' . wfMsgForContent( 'filesource' ) . " ==\n" . $source;
450 if ( $license != '' ) {
451 $filedesc = $comment == '' ?
'' : '== ' . wfMsgForContent( 'filedesc' ) . " ==\n" . $comment . "\n";
452 $pageText = $filedesc .
453 '== ' . wfMsgForContent( 'license-header' ) . " ==\n" . '{{' . $license . '}}' . "\n";
455 $pageText = $comment;
462 * See if we should check the 'watch this page' checkbox on the form
463 * based on the user's preferences and whether we're being asked
464 * to create a new file or update an existing one.
466 * In the case where 'watch edits' is off but 'watch creations' is on,
467 * we'll leave the box unchecked.
469 * Note that the page target can be changed *on the form*, so our check
470 * state can get out of sync.
472 protected function getWatchCheck() {
474 if( $wgUser->getOption( 'watchdefault' ) ) {
479 $local = wfLocalFile( $this->mDesiredDestName
);
480 if( $local && $local->exists() ) {
481 // We're uploading a new version of an existing file.
482 // No creation, so don't watch it if we're not already.
483 return $local->getTitle()->userIsWatching();
485 // New page should get watched if that's our option.
486 return $wgUser->getOption( 'watchcreations' );
492 * Provides output to the user for a result of UploadBase::verifyUpload
494 * @param array $details Result of UploadBase::verifyUpload
496 protected function processVerificationError( $details ) {
497 global $wgFileExtensions, $wgLang;
499 switch( $details['status'] ) {
501 /** Statuses that only require name changing **/
502 case UploadBase
::MIN_LENGTH_PARTNAME
:
503 $this->showRecoverableUploadError( wfMsgHtml( 'minlength1' ) );
505 case UploadBase
::ILLEGAL_FILENAME
:
506 $this->showRecoverableUploadError( wfMsgExt( 'illegalfilename',
507 'parseinline', $details['filtered'] ) );
509 case UploadBase
::OVERWRITE_EXISTING_FILE
:
510 $this->showRecoverableUploadError( wfMsgExt( $details['overwrite'],
513 case UploadBase
::FILETYPE_MISSING
:
514 $this->showRecoverableUploadError( wfMsgExt( 'filetype-missing',
518 /** Statuses that require reuploading **/
519 case UploadBase
::EMPTY_FILE
:
520 $this->showUploadForm( $this->getUploadForm( wfMsgHtml( 'emptyfile' ) ) );
522 case UploadBase
::FILETYPE_BADTYPE
:
523 $finalExt = $details['finalExt'];
524 $this->showUploadError(
525 wfMsgExt( 'filetype-banned-type',
526 array( 'parseinline' ),
527 htmlspecialchars( $finalExt ),
529 wfMsgExt( 'comma-separator', array( 'escapenoentities' ) ),
532 $wgLang->formatNum( count( $wgFileExtensions ) )
536 case UploadBase
::VERIFICATION_ERROR
:
537 unset( $details['status'] );
538 $code = array_shift( $details['details'] );
539 $this->showUploadError( wfMsgExt( $code, 'parseinline', $details['details'] ) );
541 case UploadBase
::HOOK_ABORTED
:
542 if ( is_array( $details['error'] ) ) { # allow hooks to return error details in an array
543 $args = $details['error'];
544 $error = array_shift( $args );
546 $error = $details['error'];
550 $this->showUploadError( wfMsgExt( $error, 'parseinline', $args ) );
553 throw new MWException( __METHOD__
. ": Unknown value `{$details['status']}`" );
558 * Remove a temporarily kept file stashed by saveTempUploadedFile().
561 protected function unsaveUploadedFile() {
563 if ( !( $this->mUpload
instanceof UploadFromStash
) ) {
566 $success = $this->mUpload
->unsaveUploadedFile();
568 $wgOut->showFileDeleteError( $this->mUpload
->getTempPath() );
575 /*** Functions for formatting warnings ***/
578 * Formats a result of UploadBase::getExistsWarning as HTML
579 * This check is static and can be done pre-upload via AJAX
581 * @param array $exists The result of UploadBase::getExistsWarning
582 * @return string Empty string if there is no warning or an HTML fragment
584 public static function getExistsWarning( $exists ) {
585 global $wgUser, $wgContLang;
591 $file = $exists['file'];
592 $filename = $file->getTitle()->getPrefixedText();
595 $sk = $wgUser->getSkin();
597 if( $exists['warning'] == 'exists' ) {
599 $warning = wfMsgExt( 'fileexists', 'parseinline', $filename );
600 } elseif( $exists['warning'] == 'page-exists' ) {
601 // Page exists but file does not
602 $warning = wfMsgExt( 'filepageexists', 'parseinline', $filename );
603 } elseif ( $exists['warning'] == 'exists-normalized' ) {
604 $warning = wfMsgExt( 'fileexists-extension', 'parseinline', $filename,
605 $exists['normalizedFile']->getTitle()->getPrefixedText() );
606 } elseif ( $exists['warning'] == 'thumb' ) {
607 // Swapped argument order compared with other messages for backwards compatibility
608 $warning = wfMsgExt( 'fileexists-thumbnail-yes', 'parseinline',
609 $exists['thumbFile']->getTitle()->getPrefixedText(), $filename );
610 } elseif ( $exists['warning'] == 'thumb-name' ) {
611 // Image w/o '180px-' does not exists, but we do not like these filenames
612 $name = $file->getName();
613 $badPart = substr( $name, 0, strpos( $name, '-' ) +
1 );
614 $warning = wfMsgExt( 'file-thumbnail-no', 'parseinline', $badPart );
615 } elseif ( $exists['warning'] == 'bad-prefix' ) {
616 $warning = wfMsgExt( 'filename-bad-prefix', 'parseinline', $exists['prefix'] );
617 } elseif ( $exists['warning'] == 'was-deleted' ) {
618 # If the file existed before and was deleted, warn the user of this
619 $ltitle = SpecialPage
::getTitleFor( 'Log' );
620 $llink = $sk->linkKnown(
622 wfMsgHtml( 'deletionlog' ),
629 $warning = wfMsgWikiHtml( 'filewasdeleted', $llink );
636 * Get a list of warnings
638 * @param string local filename, e.g. 'file exists', 'non-descriptive filename'
639 * @return array list of warning messages
641 public static function ajaxGetExistsWarning( $filename ) {
642 $file = wfFindFile( $filename );
644 // Force local file so we have an object to do further checks against
645 // if there isn't an exact match...
646 $file = wfLocalFile( $filename );
650 $exists = UploadBase
::getExistsWarning( $file );
651 $warning = self
::getExistsWarning( $exists );
652 if ( $warning !== '' ) {
653 $s = "<div>$warning</div>";
660 * Construct a warning and a gallery from an array of duplicate files.
662 public static function getDupeWarning( $dupes ) {
666 foreach( $dupes as $file ) {
667 $title = $file->getTitle();
668 $msg .= $title->getPrefixedText() .
669 '|' . $title->getText() . "\n";
671 $msg .= '</gallery>';
673 wfMsgExt( 'file-exists-duplicate', array( 'parse' ), count( $dupes ) ) .
674 $wgOut->parse( $msg ) .
684 * Sub class of HTMLForm that provides the form section of SpecialUpload
686 class UploadForm
extends HTMLForm
{
688 protected $mForReUpload;
689 protected $mSessionKey;
690 protected $mHideIgnoreWarning;
691 protected $mDestWarningAck;
694 protected $mTextAfterSummary;
696 protected $mSourceIds;
698 public function __construct( $options = array() ) {
699 $this->mWatch
= !empty( $options['watch'] );
700 $this->mForReUpload
= !empty( $options['forreupload'] );
701 $this->mSessionKey
= isset( $options['sessionkey'] )
702 ?
$options['sessionkey'] : '';
703 $this->mHideIgnoreWarning
= !empty( $options['hideignorewarning'] );
704 $this->mDestWarningAck
= !empty( $options['destwarningack'] );
706 $this->mTextTop
= $options['texttop'];
707 $this->mTextAfterSummary
= $options['textaftersummary'];
709 $sourceDescriptor = $this->getSourceSection();
710 $descriptor = $sourceDescriptor
711 +
$this->getDescriptionSection()
712 +
$this->getOptionsSection();
714 wfRunHooks( 'UploadFormInitDescriptor', array( &$descriptor ) );
715 parent
::__construct( $descriptor, 'upload' );
717 # Set some form properties
718 $this->setSubmitText( wfMsg( 'uploadbtn' ) );
719 $this->setSubmitName( 'wpUpload' );
720 $this->setSubmitTooltip( 'upload' );
721 $this->setId( 'mw-upload-form' );
723 # Build a list of IDs for javascript insertion
724 $this->mSourceIds
= array();
725 foreach ( $sourceDescriptor as $key => $field ) {
726 if ( !empty( $field['id'] ) ) {
727 $this->mSourceIds
[] = $field['id'];
734 * Get the descriptor of the fieldset that contains the file source
735 * selection. The section is 'source'
737 * @return array Descriptor array
739 protected function getSourceSection() {
740 global $wgLang, $wgUser, $wgRequest;
742 if ( $this->mSessionKey
) {
744 'wpSessionKey' => array(
746 'default' => $this->mSessionKey
,
748 'wpSourceType' => array(
750 'default' => 'Stash',
755 $canUploadByUrl = UploadFromUrl
::isEnabled() && $wgUser->isAllowed( 'upload_by_url' );
756 $radio = $canUploadByUrl;
757 $selectedSourceType = strtolower( $wgRequest->getText( 'wpSourceType', 'File' ) );
759 $descriptor = array();
760 if ( $this->mTextTop
) {
761 $descriptor['UploadFormTextTop'] = array(
763 'section' => 'source',
764 'default' => $this->mTextTop
,
769 $descriptor['UploadFile'] = array(
770 'class' => 'UploadSourceField',
771 'section' => 'source',
773 'id' => 'wpUploadFile',
774 'label-message' => 'sourcefilename',
775 'upload-type' => 'File',
777 'help' => wfMsgExt( 'upload-maxfilesize',
778 array( 'parseinline', 'escapenoentities' ),
780 wfShorthandToInteger( ini_get( 'upload_max_filesize' ) )
782 ) . ' ' . wfMsgHtml( 'upload_source_file' ),
783 'checked' => $selectedSourceType == 'file',
785 if ( $canUploadByUrl ) {
786 global $wgMaxUploadSize;
787 $descriptor['UploadFileURL'] = array(
788 'class' => 'UploadSourceField',
789 'section' => 'source',
790 'id' => 'wpUploadFileURL',
791 'label-message' => 'sourceurl',
792 'upload-type' => 'url',
794 'help' => wfMsgExt( 'upload-maxfilesize',
795 array( 'parseinline', 'escapenoentities' ),
796 $wgLang->formatSize( $wgMaxUploadSize )
797 ) . ' ' . wfMsgHtml( 'upload_source_url' ),
798 'checked' => $selectedSourceType == 'url',
801 wfRunHooks( 'UploadFormSourceDescriptors', array( &$descriptor, &$radio, $selectedSourceType ) );
803 $descriptor['Extensions'] = array(
805 'section' => 'source',
806 'default' => $this->getExtensionsMessage(),
813 * Get the messages indicating which extensions are preferred and prohibitted.
815 * @return string HTML string containing the message
817 protected function getExtensionsMessage() {
818 # Print a list of allowed file extensions, if so configured. We ignore
819 # MIME type here, it's incomprehensible to most people and too long.
820 global $wgLang, $wgCheckFileExtensions, $wgStrictFileExtensions,
821 $wgFileExtensions, $wgFileBlacklist;
823 $allowedExtensions = '';
824 if( $wgCheckFileExtensions ) {
825 if( $wgStrictFileExtensions ) {
826 # Everything not permitted is banned
828 '<div id="mw-upload-permitted">' .
829 wfMsgWikiHtml( 'upload-permitted', $wgLang->commaList( $wgFileExtensions ) ) .
832 # We have to list both preferred and prohibited
834 '<div id="mw-upload-preferred">' .
835 wfMsgWikiHtml( 'upload-preferred', $wgLang->commaList( $wgFileExtensions ) ) .
837 '<div id="mw-upload-prohibited">' .
838 wfMsgWikiHtml( 'upload-prohibited', $wgLang->commaList( $wgFileBlacklist ) ) .
842 # Everything is permitted.
843 $extensionsList = '';
845 return $extensionsList;
849 * Get the descriptor of the fieldset that contains the file description
850 * input. The section is 'description'
852 * @return array Descriptor array
854 protected function getDescriptionSection() {
855 global $wgUser, $wgOut;
857 $cols = intval( $wgUser->getOption( 'cols' ) );
858 if( $wgUser->getOption( 'editwidth' ) ) {
859 $wgOut->addInlineStyle( '#mw-htmlform-description { width: 100%; }' );
865 'section' => 'description',
866 'id' => 'wpDestFile',
867 'label-message' => 'destfilename',
870 'UploadDescription' => array(
871 'type' => 'textarea',
872 'section' => 'description',
873 'id' => 'wpUploadDescription',
874 'label-message' => $this->mForReUpload
875 ?
'filereuploadsummary'
876 : 'fileuploadsummary',
881 if ( $this->mTextAfterSummary
) {
882 $descriptor['UploadFormTextAfterSummary'] = array(
884 'section' => 'description',
885 'default' => $this->mTextAfterSummary
,
890 $descriptor +
= array(
891 'EditTools' => array(
892 'type' => 'edittools',
893 'section' => 'description',
897 'class' => 'Licenses',
898 'section' => 'description',
900 'label-message' => 'license',
903 if ( $this->mForReUpload
) {
904 $descriptor['DestFile']['readonly'] = true;
907 global $wgUseCopyrightUpload;
908 if ( $wgUseCopyrightUpload ) {
909 $descriptor['UploadCopyStatus'] = array(
911 'section' => 'description',
912 'id' => 'wpUploadCopyStatus',
913 'label-message' => 'filestatus',
915 $descriptor['UploadSource'] = array(
917 'section' => 'description',
918 'id' => 'wpUploadSource',
919 'label-message' => 'filesource',
927 * Get the descriptor of the fieldset that contains the upload options,
928 * such as "watch this file". The section is 'options'
930 * @return array Descriptor array
932 protected function getOptionsSection() {
935 if( $wgUser->isLoggedIn() ) {
937 'Watchthis' => array(
939 'id' => 'wpWatchthis',
940 'label-message' => 'watchthisupload',
941 'section' => 'options',
945 if( !$this->mHideIgnoreWarning
) {
946 $descriptor['IgnoreWarning'] = array(
948 'id' => 'wpIgnoreWarning',
949 'label-message' => 'ignorewarnings',
950 'section' => 'options',
954 $descriptor['wpDestFileWarningAck'] = array(
956 'id' => 'wpDestFileWarningAck',
957 'default' => $this->mDestWarningAck ?
'1' : '',
964 * Add the upload JS and show the form.
966 public function show() {
967 $this->addUploadJS();
972 * Add upload JS to $wgOut
974 * @param $autofill Boolean: Whether or not to autofill the destination
977 protected function addUploadJS() {
978 global $wgUseAjax, $wgAjaxUploadDestCheck, $wgAjaxLicensePreview, $wgEnableAPI;
981 $useAjaxDestCheck = $wgUseAjax && $wgAjaxUploadDestCheck;
982 $useAjaxLicensePreview = $wgUseAjax && $wgAjaxLicensePreview && $wgEnableAPI;
985 'wgAjaxUploadDestCheck' => $useAjaxDestCheck,
986 'wgAjaxLicensePreview' => $useAjaxLicensePreview,
987 'wgUploadAutoFill' => !$this->mForReUpload
,
988 'wgUploadSourceIds' => $this->mSourceIds
,
991 $wgOut->addScript( Skin
::makeVariablesScript( $scriptVars ) );
993 // For <charinsert> support
994 $wgOut->addScriptFile( 'edit.js' );
995 $wgOut->addScriptFile( 'upload.js' );
999 * Empty function; submission is handled elsewhere.
1001 * @return bool false
1003 function trySubmit() {
1010 * A form field that contains a radio box in the label
1012 class UploadSourceField
extends HTMLTextField
{
1013 function getLabelHtml() {
1014 $id = "wpSourceType{$this->mParams['upload-type']}";
1015 $label = Html
::rawElement( 'label', array( 'for' => $id ), $this->mLabel
);
1017 if ( !empty( $this->mParams
['radio'] ) ) {
1019 'name' => 'wpSourceType',
1022 'value' => $this->mParams
['upload-type'],
1024 if ( !empty( $this->mParams
['checked'] ) ) {
1025 $attribs['checked'] = 'checked';
1027 $label .= Html
::element( 'input', $attribs );
1030 return Html
::rawElement( 'td', array( 'class' => 'mw-label' ), $label );
1033 function getSize() {
1034 return isset( $this->mParams
['size'] )
1035 ?
$this->mParams
['size']