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 protected $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
49 /** Text injection points for hooks not using HTMLForm **/
50 public $uploadFormTextTop;
51 public $uploadFormTextAfterSummary;
55 * Initialize instance variables from request and create an Upload handler
57 * @param WebRequest $request The request to extract variables from
59 protected function loadRequest( $request ) {
62 $this->mRequest
= $request;
63 $this->mSourceType
= $request->getVal( 'wpSourceType', 'file' );
64 $this->mUpload
= UploadBase
::createFromRequest( $request );
65 $this->mUploadClicked
= $request->wasPosted()
66 && ( $request->getCheck( 'wpUpload' )
67 ||
$request->getCheck( 'wpUploadIgnoreWarning' ) );
69 // Guess the desired name from the filename if not provided
70 $this->mDesiredDestName
= $request->getText( 'wpDestFile' );
71 if( !$this->mDesiredDestName
)
72 $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
163 # Process upload or show a form
164 if ( $this->mTokenOk
&& !$this->mCancelUpload
165 && ( $this->mUpload
&& $this->mUploadClicked
) ) {
166 $this->processUpload();
168 # Backwards compatibility hook
169 if( !wfRunHooks( 'UploadForm:initial', array( &$this ) ) )
171 wfDebug( "Hook 'UploadForm:initial' broke output of the upload form" );
175 $this->showUploadForm( $this->getUploadForm() );
179 if ( $this->mUpload
)
180 $this->mUpload
->cleanupTempFile();
184 * Show the main upload form and optionally add the session key to the
185 * output. This hides the source selection.
187 * @param string $message HTML message to be shown at top of form
188 * @param string $sessionKey Session key of the stashed upload
190 protected function showUploadForm( $form ) {
191 # Add links if file was previously deleted
192 if ( !$this->mDesiredDestName
)
193 $this->showViewDeletedLinks();
199 * Get an UploadForm instance with title and text properly set.
201 * @param string $message HTML string to add to the form
202 * @param string $sessionKey Session key in case this is a stashed upload
205 protected function getUploadForm( $message = '', $sessionKey = '', $hideIgnoreWarning = false ) {
209 $form = new UploadForm( array(
210 'watch' => $this->getWatchCheck(),
211 'forreupload' => $this->mForReUpload
,
212 'sessionkey' => $sessionKey,
213 'hideignorewarning' => $hideIgnoreWarning,
214 'destwarningack' => (bool)$this->mDestWarningAck
,
216 'texttop' => $this->uploadFormTextTop
,
217 'textaftersummary' => $this->uploadFormTextAfterSummary
,
219 $form->setTitle( $this->getTitle() );
221 # Check the token, but only if necessary
222 if( !$this->mTokenOk
&& !$this->mCancelUpload
223 && ( $this->mUpload
&& $this->mUploadClicked
) ) {
224 $form->addPreText( wfMsgExt( 'session_fail_preview', 'parseinline' ) );
228 $form->addPreText( '<div id="uploadtext">' . wfMsgExt( 'uploadtext', 'parse' ) . '</div>');
229 # Add upload error message
230 $form->addPreText( $message );
233 $uploadFooter = wfMsgNoTrans( 'uploadfooter' );
234 if ( $uploadFooter != '-' && !wfEmptyMsg( 'uploadfooter', $uploadFooter ) ) {
235 $form->addPostText( '<div id="mw-upload-footer-message">'
236 . $wgOut->parse( $uploadFooter ) . "</div>\n" );
244 * Shows the "view X deleted revivions link""
246 protected function showViewDeletedLinks() {
247 global $wgOut, $wgUser;
249 $title = Title
::makeTitleSafe( NS_FILE
, $this->mDesiredDestName
);
250 // Show a subtitle link to deleted revisions (to sysops et al only)
251 if( $title instanceof Title
) {
252 $count = $title->isDeleted();
253 if ( $count > 0 && $wgUser->isAllowed( 'deletedhistory' ) ) {
255 $wgUser->isAllowed( 'delete' ) ?
'thisisdeleted' : 'viewdeleted',
256 array( 'parse', 'replaceafter' ),
257 $wgUser->getSkin()->linkKnown(
258 SpecialPage
::getTitleFor( 'Undelete', $title->getPrefixedText() ),
259 wfMsgExt( 'restorelink', array( 'parsemag', 'escape' ), $count )
262 $wgOut->addHTML( "<div id=\"contentSub2\">{$link}</div>" );
266 // Show the relevant lines from deletion log (for still deleted files only)
267 if( $title instanceof Title
&& $title->isDeletedQuick() && !$title->exists() ) {
268 $this->showDeletionLog( $wgOut, $title->getPrefixedText() );
273 * Stashes the upload and shows the main upload form.
275 * Note: only errors that can be handled by changing the name or
276 * description should be redirected here. It should be assumed that the
277 * file itself is sane and has passed UploadBase::verifyFile. This
278 * essentially means that UploadBase::VERIFICATION_ERROR and
279 * UploadBase::EMPTY_FILE should not be passed here.
281 * @param string $message HTML message to be passed to mainUploadForm
283 protected function showRecoverableUploadError( $message ) {
284 $sessionKey = $this->mUpload
->stashSession();
285 $message = '<h2>' . wfMsgHtml( 'uploadwarning' ) . "</h2>\n" .
286 '<div class="error">' . $message . "</div>\n";
288 $form = $this->getUploadForm( $message, $sessionKey );
289 $form->setSubmitText( wfMsg( 'upload-tryagain' ) );
290 $this->showUploadForm( $form );
293 * Stashes the upload, shows the main form, but adds an "continue anyway button".
294 * Also checks whether there are actually warnings to display.
296 * @param array $warnings
297 * @return boolean true if warnings were displayed, false if there are no
298 * warnings and the should continue processing like there was no warning
300 protected function showUploadWarning( $warnings ) {
303 # If there are no warnings, or warnings we can ignore, return early
304 if ( !$warnings ||
( count( $warnings ) == 1 &&
305 isset( $warnings['exists']) && $this->mDestWarningAck
) ) {
309 $sessionKey = $this->mUpload
->stashSession();
311 $sk = $wgUser->getSkin();
313 $warningHtml = '<h2>' . wfMsgHtml( 'uploadwarning' ) . "</h2>\n"
314 . '<ul class="warning">';
315 foreach( $warnings as $warning => $args ) {
317 if( $warning == 'exists' ) {
318 $msg = "\t<li>" . self
::getExistsWarning( $args ) . "</li>\n";
319 } elseif( $warning == 'duplicate' ) {
320 $msg = self
::getDupeWarning( $args );
321 } elseif( $warning == 'duplicate-archive' ) {
322 $msg = "\t<li>" . wfMsgExt( 'file-deleted-duplicate', 'parseinline',
323 array( Title
::makeTitle( NS_FILE
, $args )->getPrefixedText() ) )
326 if ( $args === true )
328 elseif ( !is_array( $args ) )
329 $args = array( $args );
330 $msg = "\t<li>" . wfMsgExt( $warning, 'parseinline', $args ) . "</li>\n";
332 $warningHtml .= $msg;
334 $warningHtml .= "</ul>\n";
335 $warningHtml .= wfMsgExt( 'uploadwarning-text', 'parse' );
337 $form = $this->getUploadForm( $warningHtml, $sessionKey, /* $hideIgnoreWarning */ true );
338 $form->setSubmitText( wfMsg( 'upload-tryagain' ) );
339 $form->addButton( 'wpUploadIgnoreWarning', wfMsg( 'ignorewarning' ) );
340 $form->addButton( 'wpCancelUpload', wfMsg( 'reuploaddesc' ) );
342 $this->showUploadForm( $form );
344 # Indicate that we showed a form
349 * Show the upload form with error message, but do not stash the file.
351 * @param string $message
353 protected function showUploadError( $message ) {
354 $message = '<h2>' . wfMsgHtml( 'uploadwarning' ) . "</h2>\n" .
355 '<div class="error">' . $message . "</div>\n";
356 $this->showUploadForm( $this->getUploadForm( $message ) );
361 * Checks are made in SpecialUpload::execute()
363 protected function processUpload() {
364 global $wgUser, $wgOut;
366 // Verify permissions
367 $permErrors = $this->mUpload
->verifyPermissions( $wgUser );
368 if( $permErrors !== true ) {
369 $wgOut->showPermissionsErrorPage( $permErrors );
373 // Fetch the file if required
374 $status = $this->mUpload
->fetchFile();
375 if( !$status->isOK() ) {
376 $this->showUploadForm( $this->getUploadForm( $wgOut->parse( $status->getWikiText() ) ) );
380 // Deprecated backwards compatibility hook
381 if( !wfRunHooks( 'UploadForm:BeforeProcessing', array( &$this ) ) )
383 wfDebug( "Hook 'UploadForm:BeforeProcessing' broke processing the file.\n" );
384 return array( 'status' => UploadBase
::BEFORE_PROCESSING
);
388 // Upload verification
389 $details = $this->mUpload
->verifyUpload();
390 if ( $details['status'] != UploadBase
::OK
) {
391 $this->processVerificationError( $details );
395 $this->mLocalFile
= $this->mUpload
->getLocalFile();
397 // Check warnings if necessary
398 if( !$this->mIgnoreWarning
) {
399 $warnings = $this->mUpload
->checkWarnings();
400 if( $this->showUploadWarning( $warnings ) ) {
405 // Get the page text if this is not a reupload
406 if( !$this->mForReUpload
) {
407 $pageText = self
::getInitialPageText( $this->mComment
, $this->mLicense
,
408 $this->mCopyrightStatus
, $this->mCopyrightSource
);
412 $status = $this->mUpload
->performUpload( $this->mComment
, $pageText, $this->mWatchthis
, $wgUser );
413 if ( !$status->isGood() ) {
414 $this->showUploadError( $wgOut->parse( $status->getWikiText() ) );
418 // Success, redirect to description page
419 wfRunHooks( 'SpecialUploadComplete', array( &$this ) );
420 $wgOut->redirect( $this->mLocalFile
->getTitle()->getFullURL() );
425 * Get the initial image page text based on a comment and optional file status information
427 public static function getInitialPageText( $comment = '', $license = '', $copyStatus = '', $source = '' ) {
428 global $wgUseCopyrightUpload;
429 if ( $wgUseCopyrightUpload ) {
431 if ( $license != '' ) {
432 $licensetxt = '== ' . wfMsgForContent( 'license-header' ) . " ==\n" . '{{' . $license . '}}' . "\n";
434 $pageText = '== ' . wfMsgForContent ( 'filedesc' ) . " ==\n" . $comment . "\n" .
435 '== ' . wfMsgForContent ( 'filestatus' ) . " ==\n" . $copyStatus . "\n" .
437 '== ' . wfMsgForContent ( 'filesource' ) . " ==\n" . $source ;
439 if ( $license != '' ) {
440 $filedesc = $comment == '' ?
'' : '== ' . wfMsgForContent ( 'filedesc' ) . " ==\n" . $comment . "\n";
441 $pageText = $filedesc .
442 '== ' . wfMsgForContent ( 'license-header' ) . " ==\n" . '{{' . $license . '}}' . "\n";
444 $pageText = $comment;
451 * See if we should check the 'watch this page' checkbox on the form
452 * based on the user's preferences and whether we're being asked
453 * to create a new file or update an existing one.
455 * In the case where 'watch edits' is off but 'watch creations' is on,
456 * we'll leave the box unchecked.
458 * Note that the page target can be changed *on the form*, so our check
459 * state can get out of sync.
461 protected function getWatchCheck() {
463 if( $wgUser->getOption( 'watchdefault' ) ) {
468 $local = wfLocalFile( $this->mDesiredDestName
);
469 if( $local && $local->exists() ) {
470 // We're uploading a new version of an existing file.
471 // No creation, so don't watch it if we're not already.
472 return $local->getTitle()->userIsWatching();
474 // New page should get watched if that's our option.
475 return $wgUser->getOption( 'watchcreations' );
481 * Provides output to the user for a result of UploadBase::verifyUpload
483 * @param array $details Result of UploadBase::verifyUpload
485 protected function processVerificationError( $details ) {
486 global $wgFileExtensions, $wgLang;
488 switch( $details['status'] ) {
490 /** Statuses that only require name changing **/
491 case UploadBase
::MIN_LENGTH_PARTNAME
:
492 $this->showRecoverableUploadError( wfMsgHtml( 'minlength1' ) );
494 case UploadBase
::ILLEGAL_FILENAME
:
495 $this->showRecoverableUploadError( wfMsgExt( 'illegalfilename',
496 'parseinline', $details['filtered'] ) );
498 case UploadBase
::OVERWRITE_EXISTING_FILE
:
499 $this->showRecoverableUploadError( wfMsgExt( $details['overwrite'],
502 case UploadBase
::FILETYPE_MISSING
:
503 $this->showRecoverableUploadError( wfMsgExt( 'filetype-missing',
507 /** Statuses that require reuploading **/
508 case UploadBase
::EMPTY_FILE
:
509 $this->showUploadForm( $this->getUploadForm( wfMsgHtml( 'emptyfile' ) ) );
511 case UploadBase
::FILETYPE_BADTYPE
:
512 $finalExt = $details['finalExt'];
513 $this->showUploadError(
514 wfMsgExt( 'filetype-banned-type',
515 array( 'parseinline' ),
516 htmlspecialchars( $finalExt ),
518 wfMsgExt( 'comma-separator', array( 'escapenoentities' ) ),
521 $wgLang->formatNum( count( $wgFileExtensions ) )
525 case UploadBase
::VERIFICATION_ERROR
:
526 unset( $details['status'] );
527 $code = array_shift( $details['details'] );
528 $this->showUploadError( wfMsgExt( $code, 'parseinline', $details['details'] ) );
530 case UploadBase
::HOOK_ABORTED
:
531 if ( is_array( $details['error'] ) ) { # allow hooks to return error details in an array
532 $args = $details['error'];
533 $error = array_shift( $args );
535 $error = $details['error'];
539 $this->showUploadError( wfMsgExt( $error, 'parseinline', $args ) );
542 throw new MWException( __METHOD__
. ": Unknown value `{$details['status']}`" );
547 * Remove a temporarily kept file stashed by saveTempUploadedFile().
551 protected function unsaveUploadedFile() {
553 if ( !( $this->mUpload
instanceof UploadFromStash
) )
555 $success = $this->mUpload
->unsaveUploadedFile();
557 $wgOut->showFileDeleteError( $this->mUpload
->getTempPath() );
564 /*** Functions for formatting warnings ***/
567 * Formats a result of UploadBase::getExistsWarning as HTML
568 * This check is static and can be done pre-upload via AJAX
570 * @param array $exists The result of UploadBase::getExistsWarning
571 * @return string Empty string if there is no warning or an HTML fragment
573 public static function getExistsWarning( $exists ) {
574 global $wgUser, $wgContLang;
579 $file = $exists['file'];
580 $filename = $file->getTitle()->getPrefixedText();
583 $sk = $wgUser->getSkin();
585 if( $exists['warning'] == 'exists' ) {
587 $warning = wfMsgExt( 'fileexists', 'parseinline', $filename );
588 } elseif( $exists['warning'] == 'page-exists' ) {
589 // Page exists but file does not
590 $warning = wfMsgExt( 'filepageexists', 'parseinline', $filename );
591 } elseif ( $exists['warning'] == 'exists-normalized' ) {
592 $warning = wfMsgExt( 'fileexists-extension', 'parseinline', $filename,
593 $exists['normalizedFile']->getTitle()->getPrefixedText() );
594 } elseif ( $exists['warning'] == 'thumb' ) {
595 // Swapped argument order compared with other messages for backwards compatibility
596 $warning = wfMsgExt( 'fileexists-thumbnail-yes', 'parseinline',
597 $exists['thumbFile']->getTitle()->getPrefixedText(), $filename );
598 } elseif ( $exists['warning'] == 'thumb-name' ) {
599 // Image w/o '180px-' does not exists, but we do not like these filenames
600 $name = $file->getName();
601 $badPart = substr( $name, 0, strpos( $name, '-' ) +
1 );
602 $warning = wfMsgExt( 'file-thumbnail-no', 'parseinline', $badPart );
603 } elseif ( $exists['warning'] == 'bad-prefix' ) {
604 $warning = wfMsgExt( 'filename-bad-prefix', 'parseinline', $exists['prefix'] );
605 } elseif ( $exists['warning'] == 'was-deleted' ) {
606 # If the file existed before and was deleted, warn the user of this
607 $ltitle = SpecialPage
::getTitleFor( 'Log' );
608 $llink = $sk->linkKnown(
610 wfMsgHtml( 'deletionlog' ),
617 $warning = wfMsgWikiHtml( 'filewasdeleted', $llink );
624 * Get a list of warnings
626 * @param string local filename, e.g. 'file exists', 'non-descriptive filename'
627 * @return array list of warning messages
629 public static function ajaxGetExistsWarning( $filename ) {
630 $file = wfFindFile( $filename );
632 // Force local file so we have an object to do further checks against
633 // if there isn't an exact match...
634 $file = wfLocalFile( $filename );
638 $exists = UploadBase
::getExistsWarning( $file );
639 $warning = self
::getExistsWarning( $exists );
640 if ( $warning !== '' ) {
641 $s = "<div>$warning</div>";
648 * Construct a warning and a gallery from an array of duplicate files.
650 public static function getDupeWarning( $dupes ) {
654 foreach( $dupes as $file ) {
655 $title = $file->getTitle();
656 $msg .= $title->getPrefixedText() .
657 "|" . $title->getText() . "\n";
659 $msg .= "</gallery>";
661 wfMsgExt( "file-exists-duplicate", array( "parse" ), count( $dupes ) ) .
662 $wgOut->parse( $msg ) .
672 * Sub class of HTMLForm that provides the form section of SpecialUpload
674 class UploadForm
extends HTMLForm
{
676 protected $mForReUpload;
677 protected $mSessionKey;
678 protected $mHideIgnoreWarning;
679 protected $mDestWarningAck;
682 protected $mTextAfterSummary;
684 protected $mSourceIds;
686 public function __construct( $options = array() ) {
689 $this->mWatch
= !empty( $options['watch'] );
690 $this->mForReUpload
= !empty( $options['forreupload'] );
691 $this->mSessionKey
= isset( $options['sessionkey'] )
692 ?
$options['sessionkey'] : '';
693 $this->mHideIgnoreWarning
= !empty( $options['hideignorewarning'] );
694 $this->mDestWarningAck
= !empty( $options['destwarningack'] );
696 $this->mTextTop
= $options['texttop'];
697 $this->mTextAfterSummary
= $options['textaftersummary'];
699 $sourceDescriptor = $this->getSourceSection();
700 $descriptor = $sourceDescriptor
701 +
$this->getDescriptionSection()
702 +
$this->getOptionsSection();
704 wfRunHooks( 'UploadFormInitDescriptor', array( $descriptor ) );
705 parent
::__construct( $descriptor, 'upload' );
707 # Set some form properties
708 $this->setSubmitText( wfMsg( 'uploadbtn' ) );
709 $this->setSubmitName( 'wpUpload' );
710 $this->setSubmitTooltip( 'upload' );
711 $this->setId( 'mw-upload-form' );
713 # Build a list of IDs for javascript insertion
714 $this->mSourceIds
= array();
715 foreach ( $sourceDescriptor as $key => $field ) {
716 if ( !empty( $field['id'] ) )
717 $this->mSourceIds
[] = $field['id'];
723 * Get the descriptor of the fieldset that contains the file source
724 * selection. The section is 'source'
726 * @return array Descriptor array
728 protected function getSourceSection() {
729 global $wgLang, $wgUser, $wgRequest;
731 if ( $this->mSessionKey
) {
733 'wpSessionKey' => array(
735 'default' => $this->mSessionKey
,
737 'wpSourceType' => array(
739 'default' => 'Stash',
744 $canUploadByUrl = UploadFromUrl
::isEnabled() && $wgUser->isAllowed( 'upload_by_url' );
745 $radio = $canUploadByUrl;
746 $selectedSourceType = strtolower( $wgRequest->getText( 'wpSourceType', 'File' ) );
748 $descriptor = array();
749 if ( $this->mTextTop
) {
750 $descriptor['UploadFormTextTop'] = array(
752 'section' => 'source',
753 'default' => $this->mTextTop
,
758 $descriptor['UploadFile'] = array(
759 'class' => 'UploadSourceField',
760 'section' => 'source',
762 'id' => 'wpUploadFile',
763 'label-message' => 'sourcefilename',
764 'upload-type' => 'File',
766 'help' => wfMsgExt( 'upload-maxfilesize',
767 array( 'parseinline', 'escapenoentities' ),
769 wfShorthandToInteger( ini_get( 'upload_max_filesize' ) )
771 ) . ' ' . wfMsgHtml( 'upload_source_file' ),
772 'checked' => $selectedSourceType == 'file',
774 if ( $canUploadByUrl ) {
775 global $wgMaxUploadSize;
776 $descriptor['UploadFileURL'] = array(
777 'class' => 'UploadSourceField',
778 'section' => 'source',
779 'id' => 'wpUploadFileURL',
780 'label-message' => 'sourceurl',
781 'upload-type' => 'url',
783 'help' => wfMsgExt( 'upload-maxfilesize',
784 array( 'parseinline', 'escapenoentities' ),
785 $wgLang->formatSize( $wgMaxUploadSize )
786 ) . ' ' . wfMsgHtml( 'upload_source_url' ),
787 'checked' => $selectedSourceType == 'url',
790 wfRunHooks( 'UploadFormSourceDescriptors', array( &$descriptor, &$radio, $selectedSourceType ) );
792 $descriptor['Extensions'] = array(
794 'section' => 'source',
795 'default' => $this->getExtensionsMessage(),
803 * Get the messages indicating which extensions are preferred and prohibitted.
805 * @return string HTML string containing the message
807 protected function getExtensionsMessage() {
808 # Print a list of allowed file extensions, if so configured. We ignore
809 # MIME type here, it's incomprehensible to most people and too long.
810 global $wgLang, $wgCheckFileExtensions, $wgStrictFileExtensions,
811 $wgFileExtensions, $wgFileBlacklist;
813 $allowedExtensions = '';
814 if( $wgCheckFileExtensions ) {
815 if( $wgStrictFileExtensions ) {
816 # Everything not permitted is banned
818 '<div id="mw-upload-permitted">' .
819 wfMsgWikiHtml( 'upload-permitted', $wgLang->commaList( $wgFileExtensions ) ) .
822 # We have to list both preferred and prohibited
824 '<div id="mw-upload-preferred">' .
825 wfMsgWikiHtml( 'upload-preferred', $wgLang->commaList( $wgFileExtensions ) ) .
827 '<div id="mw-upload-prohibited">' .
828 wfMsgWikiHtml( 'upload-prohibited', $wgLang->commaList( $wgFileBlacklist ) ) .
832 # Everything is permitted.
833 $extensionsList = '';
835 return $extensionsList;
839 * Get the descriptor of the fieldset that contains the file description
840 * input. The section is 'description'
842 * @return array Descriptor array
844 protected function getDescriptionSection() {
845 global $wgUser, $wgOut;
847 $cols = intval( $wgUser->getOption( 'cols' ) );
848 if( $wgUser->getOption( 'editwidth' ) ) {
849 $wgOut->addInlineStyle( '#mw-htmlform-description { width: 100%; }' );
855 'section' => 'description',
856 'id' => 'wpDestFile',
857 'label-message' => 'destfilename',
860 'UploadDescription' => array(
861 'type' => 'textarea',
862 'section' => 'description',
863 'id' => 'wpUploadDescription',
864 'label-message' => $this->mForReUpload
865 ?
'filereuploadsummary'
866 : 'fileuploadsummary',
871 if ( $this->mTextAfterSummary
) {
872 $descriptor['UploadFormTextAfterSummary'] = array(
874 'section' => 'description',
875 'default' => $this->mTextAfterSummary
,
880 $descriptor +
= array(
881 'EditTools' => array(
882 'type' => 'edittools',
883 'section' => 'description',
887 'class' => 'Licenses',
888 'section' => 'description',
890 'label-message' => 'license',
893 if ( $this->mForReUpload
)
894 $descriptor['DestFile']['readonly'] = true;
896 global $wgUseCopyrightUpload;
897 if ( $wgUseCopyrightUpload ) {
898 $descriptor['UploadCopyStatus'] = array(
900 'section' => 'description',
901 'id' => 'wpUploadCopyStatus',
902 'label-message' => 'filestatus',
904 $descriptor['UploadSource'] = array(
906 'section' => 'description',
907 'id' => 'wpUploadSource',
908 'label-message' => 'filesource',
916 * Get the descriptor of the fieldset that contains the upload options,
917 * such as "watch this file". The section is 'options'
919 * @return array Descriptor array
921 protected function getOptionsSection() {
922 global $wgUser, $wgOut;
924 if( $wgUser->isLoggedIn() ) {
926 'Watchthis' => array(
928 'id' => 'wpWatchthis',
929 'label-message' => 'watchthisupload',
930 'section' => 'options',
934 if( !$this->mHideIgnoreWarning
) {
935 $descriptor['IgnoreWarning'] = array(
937 'id' => 'wpIgnoreWarning',
938 'label-message' => 'ignorewarnings',
939 'section' => 'options',
943 $descriptor['wpDestFileWarningAck'] = array(
945 'id' => 'wpDestFileWarningAck',
946 'default' => $this->mDestWarningAck ?
'1' : '',
954 * Add the upload JS and show the form.
956 public function show() {
957 $this->addUploadJS();
962 * Add upload JS to $wgOut
964 * @param bool $autofill Whether or not to autofill the destination
967 protected function addUploadJS( ) {
968 global $wgUseAjax, $wgAjaxUploadDestCheck, $wgAjaxLicensePreview, $wgEnableAPI;
971 $useAjaxDestCheck = $wgUseAjax && $wgAjaxUploadDestCheck;
972 $useAjaxLicensePreview = $wgUseAjax && $wgAjaxLicensePreview && $wgEnableAPI;
975 'wgAjaxUploadDestCheck' => $useAjaxDestCheck,
976 'wgAjaxLicensePreview' => $useAjaxLicensePreview,
977 'wgUploadAutoFill' => !$this->mForReUpload
,
978 'wgUploadSourceIds' => $this->mSourceIds
,
981 $wgOut->addScript( Skin
::makeVariablesScript( $scriptVars ) );
983 // For <charinsert> support
984 $wgOut->addScriptFile( 'edit.js' );
985 $wgOut->addScriptFile( 'upload.js' );
989 * Empty function; submission is handled elsewhere.
993 function trySubmit() {
1000 * A form field that contains a radio box in the label
1002 class UploadSourceField
extends HTMLTextField
{
1003 function getLabelHtml() {
1004 $id = "wpSourceType{$this->mParams['upload-type']}";
1005 $label = Html
::rawElement( 'label', array( 'for' => $id ), $this->mLabel
);
1007 if ( !empty( $this->mParams
['radio'] ) ) {
1009 'name' => 'wpSourceType',
1012 'value' => $this->mParams
['upload-type'],
1014 if ( !empty( $this->mParams
['checked'] ) )
1015 $attribs['checked'] = 'checked';
1016 $label .= Html
::element( 'input', $attribs );
1019 return Html
::rawElement( 'td', array( 'class' => 'mw-label' ), $label );
1021 function getSize() {
1022 return isset( $this->mParams
['size'] )
1023 ?
$this->mParams
['size']