3 * Implements Special:Upload
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
21 * @ingroup SpecialPage
26 * Form for handling uploads and special page.
28 * @ingroup SpecialPage
31 class SpecialUpload
extends SpecialPage
{
33 * Constructor : initialise object
34 * Get data POSTed through the form and assign them to the object
35 * @param $request WebRequest : data posted.
37 public function __construct( $request = null ) {
40 parent
::__construct( 'Upload', 'upload' );
42 $this->loadRequest( is_null( $request ) ?
$wgRequest : $request );
45 /** Misc variables **/
46 public $mRequest; // The WebRequest or FauxRequest this form is supposed to handle
50 public $mUploadClicked;
52 /** User input variables from the "description" section **/
53 public $mDesiredDestName; // The requested target file name
57 /** User input variables from the root section **/
58 public $mIgnoreWarning;
60 public $mCopyrightStatus;
61 public $mCopyrightSource;
63 /** Hidden variables **/
64 public $mDestWarningAck;
65 public $mForReUpload; // The user followed an "overwrite this file" link
66 public $mCancelUpload; // The user clicked "Cancel and return to upload form" button
68 public $mUploadSuccessful = false; // Subclasses can use this to determine whether a file was uploaded
70 /** Text injection points for hooks not using HTMLForm **/
71 public $uploadFormTextTop;
72 public $uploadFormTextAfterSummary;
75 * Initialize instance variables from request and create an Upload handler
77 * @param $request WebRequest: the request to extract variables from
79 protected function loadRequest( $request ) {
82 $this->mRequest
= $request;
83 $this->mSourceType
= $request->getVal( 'wpSourceType', 'file' );
84 $this->mUpload
= UploadBase
::createFromRequest( $request );
85 $this->mUploadClicked
= $request->wasPosted()
86 && ( $request->getCheck( 'wpUpload' )
87 ||
$request->getCheck( 'wpUploadIgnoreWarning' ) );
89 // Guess the desired name from the filename if not provided
90 $this->mDesiredDestName
= $request->getText( 'wpDestFile' );
91 if( !$this->mDesiredDestName
&& $request->getFileName( 'wpUploadFile' ) !== null ) {
92 $this->mDesiredDestName
= $request->getFileName( 'wpUploadFile' );
94 $this->mComment
= $request->getText( 'wpUploadDescription' );
95 $this->mLicense
= $request->getText( 'wpLicense' );
98 $this->mDestWarningAck
= $request->getText( 'wpDestFileWarningAck' );
99 $this->mIgnoreWarning
= $request->getCheck( 'wpIgnoreWarning' )
100 ||
$request->getCheck( 'wpUploadIgnoreWarning' );
101 $this->mWatchthis
= $request->getBool( 'wpWatchthis' ) && $wgUser->isLoggedIn();
102 $this->mCopyrightStatus
= $request->getText( 'wpUploadCopyStatus' );
103 $this->mCopyrightSource
= $request->getText( 'wpUploadSource' );
106 $this->mForReUpload
= $request->getBool( 'wpForReUpload' ); // updating a file
107 $this->mCancelUpload
= $request->getCheck( 'wpCancelUpload' )
108 ||
$request->getCheck( 'wpReUpload' ); // b/w compat
110 // If it was posted check for the token (no remote POST'ing with user credentials)
111 $token = $request->getVal( 'wpEditToken' );
112 if( $this->mSourceType
== 'file' && $token == null ) {
113 // Skip token check for file uploads as that can't be faked via JS...
114 // Some client-side tools don't expect to need to send wpEditToken
115 // with their submissions, as that's new in 1.16.
116 $this->mTokenOk
= true;
118 $this->mTokenOk
= $wgUser->matchEditToken( $token );
121 $this->uploadFormTextTop
= '';
122 $this->uploadFormTextAfterSummary
= '';
126 * This page can be shown if uploading is enabled.
127 * Handle permission checking elsewhere in order to be able to show
128 * custom error messages.
130 * @param $user User object
133 public function userCanExecute( $user ) {
134 return UploadBase
::isEnabled() && parent
::userCanExecute( $user );
138 * Special page entry point
140 public function execute( $par ) {
141 global $wgUser, $wgOut;
144 $this->outputHeader();
146 # Check uploading enabled
147 if( !UploadBase
::isEnabled() ) {
148 $wgOut->showErrorPage( 'uploaddisabled', 'uploaddisabledtext' );
153 global $wgGroupPermissions;
154 $permissionRequired = UploadBase
::isAllowed( $wgUser );
155 if( $permissionRequired !== true ) {
156 if( !$wgUser->isLoggedIn() && ( $wgGroupPermissions['user']['upload']
157 ||
$wgGroupPermissions['autoconfirmed']['upload'] ) ) {
158 // Custom message if logged-in users without any special rights can upload
159 $wgOut->showErrorPage( 'uploadnologin', 'uploadnologintext' );
161 $wgOut->permissionRequired( $permissionRequired );
167 if( $wgUser->isBlocked() ) {
168 $wgOut->blockedPage();
172 # Check whether we actually want to allow changing stuff
174 $wgOut->readOnlyPage();
178 # Unsave the temporary file in case this was a cancelled upload
179 if ( $this->mCancelUpload
) {
180 if ( !$this->unsaveUploadedFile() ) {
181 # Something went wrong, so unsaveUploadedFile showed a warning
186 # Process upload or show a form
188 $this->mTokenOk
&& !$this->mCancelUpload
&&
189 ( $this->mUpload
&& $this->mUploadClicked
)
192 $this->processUpload();
194 # Backwards compatibility hook
195 if( !wfRunHooks( 'UploadForm:initial', array( &$this ) ) ) {
196 wfDebug( "Hook 'UploadForm:initial' broke output of the upload form" );
201 $this->showUploadForm( $this->getUploadForm() );
205 if ( $this->mUpload
) {
206 $this->mUpload
->cleanupTempFile();
211 * Show the main upload form
213 * @param $form Mixed: an HTMLForm instance or HTML string to show
215 protected function showUploadForm( $form ) {
216 # Add links if file was previously deleted
217 if ( !$this->mDesiredDestName
) {
218 $this->showViewDeletedLinks();
221 if ( $form instanceof HTMLForm
) {
225 $wgOut->addHTML( $form );
231 * Get an UploadForm instance with title and text properly set.
233 * @param $message String: HTML string to add to the form
234 * @param $sessionKey String: session key in case this is a stashed upload
235 * @param $hideIgnoreWarning Boolean: whether to hide "ignore warning" check box
238 protected function getUploadForm( $message = '', $sessionKey = '', $hideIgnoreWarning = false ) {
242 $form = new UploadForm( array(
243 'watch' => $this->getWatchCheck(),
244 'forreupload' => $this->mForReUpload
,
245 'sessionkey' => $sessionKey,
246 'hideignorewarning' => $hideIgnoreWarning,
247 'destwarningack' => (bool)$this->mDestWarningAck
,
249 'texttop' => $this->uploadFormTextTop
,
250 'textaftersummary' => $this->uploadFormTextAfterSummary
,
251 'destfile' => $this->mDesiredDestName
,
253 $form->setTitle( $this->getTitle() );
255 # Check the token, but only if necessary
257 !$this->mTokenOk
&& !$this->mCancelUpload
&&
258 ( $this->mUpload
&& $this->mUploadClicked
)
261 $form->addPreText( wfMsgExt( 'session_fail_preview', 'parseinline' ) );
264 # Give a notice if the user is uploading a file that has been deleted or moved
265 # Note that this is independent from the message 'filewasdeleted' that requires JS
266 $desiredTitleObj = Title
::newFromText( $this->mDesiredDestName
, NS_FILE
);
267 $delNotice = ''; // empty by default
268 if ( $desiredTitleObj instanceof Title
&& !$desiredTitleObj->exists() ) {
269 LogEventsList
::showLogExtract( $delNotice, array( 'delete', 'move' ),
270 $desiredTitleObj->getPrefixedText(),
271 '', array( 'lim' => 10,
272 'conds' => array( "log_action != 'revision'" ),
273 'showIfEmpty' => false,
274 'msgKey' => array( 'upload-recreate-warning' ) )
277 $form->addPreText( $delNotice );
280 $form->addPreText( '<div id="uploadtext">' .
281 wfMsgExt( 'uploadtext', 'parse', array( $this->mDesiredDestName
) ) .
283 # Add upload error message
284 $form->addPreText( $message );
287 $uploadFooter = wfMsgNoTrans( 'uploadfooter' );
288 if ( $uploadFooter != '-' && !wfEmptyMsg( 'uploadfooter', $uploadFooter ) ) {
289 $form->addPostText( '<div id="mw-upload-footer-message">'
290 . $wgOut->parse( $uploadFooter ) . "</div>\n" );
298 * Shows the "view X deleted revivions link""
300 protected function showViewDeletedLinks() {
301 global $wgOut, $wgUser;
303 $title = Title
::makeTitleSafe( NS_FILE
, $this->mDesiredDestName
);
304 // Show a subtitle link to deleted revisions (to sysops et al only)
305 if( $title instanceof Title
) {
306 $count = $title->isDeleted();
307 if ( $count > 0 && $wgUser->isAllowed( 'deletedhistory' ) ) {
309 $wgUser->isAllowed( 'delete' ) ?
'thisisdeleted' : 'viewdeleted',
310 array( 'parse', 'replaceafter' ),
311 $wgUser->getSkin()->linkKnown(
312 SpecialPage
::getTitleFor( 'Undelete', $title->getPrefixedText() ),
313 wfMsgExt( 'restorelink', array( 'parsemag', 'escape' ), $count )
316 $wgOut->addHTML( "<div id=\"contentSub2\">{$link}</div>" );
320 // Show the relevant lines from deletion log (for still deleted files only)
321 if( $title instanceof Title
&& $title->isDeletedQuick() && !$title->exists() ) {
322 $this->showDeletionLog( $wgOut, $title->getPrefixedText() );
327 * Stashes the upload and shows the main upload form.
329 * Note: only errors that can be handled by changing the name or
330 * description should be redirected here. It should be assumed that the
331 * file itself is sane and has passed UploadBase::verifyFile. This
332 * essentially means that UploadBase::VERIFICATION_ERROR and
333 * UploadBase::EMPTY_FILE should not be passed here.
335 * @param $message String: HTML message to be passed to mainUploadForm
337 protected function showRecoverableUploadError( $message ) {
338 $sessionKey = $this->mUpload
->stashSession();
339 $message = '<h2>' . wfMsgHtml( 'uploadwarning' ) . "</h2>\n" .
340 '<div class="error">' . $message . "</div>\n";
342 $form = $this->getUploadForm( $message, $sessionKey );
343 $form->setSubmitText( wfMsg( 'upload-tryagain' ) );
344 $this->showUploadForm( $form );
347 * Stashes the upload, shows the main form, but adds an "continue anyway button".
348 * Also checks whether there are actually warnings to display.
350 * @param $warnings Array
351 * @return boolean true if warnings were displayed, false if there are no
352 * warnings and the should continue processing like there was no warning
354 protected function showUploadWarning( $warnings ) {
355 # If there are no warnings, or warnings we can ignore, return early.
356 # mDestWarningAck is set when some javascript has shown the warning
357 # to the user. mForReUpload is set when the user clicks the "upload a
359 if ( !$warnings ||
( count( $warnings ) == 1 &&
360 isset( $warnings['exists'] ) &&
361 ( $this->mDestWarningAck ||
$this->mForReUpload
) ) )
366 $sessionKey = $this->mUpload
->stashSession();
368 $warningHtml = '<h2>' . wfMsgHtml( 'uploadwarning' ) . "</h2>\n"
369 . '<ul class="warning">';
370 foreach( $warnings as $warning => $args ) {
372 if( $warning == 'exists' ) {
373 $msg = "\t<li>" . self
::getExistsWarning( $args ) . "</li>\n";
374 } elseif( $warning == 'duplicate' ) {
375 $msg = self
::getDupeWarning( $args );
376 } elseif( $warning == 'duplicate-archive' ) {
377 $msg = "\t<li>" . wfMsgExt( 'file-deleted-duplicate', 'parseinline',
378 array( Title
::makeTitle( NS_FILE
, $args )->getPrefixedText() ) )
381 if ( $args === true ) {
383 } elseif ( !is_array( $args ) ) {
384 $args = array( $args );
386 $msg = "\t<li>" . wfMsgExt( $warning, 'parseinline', $args ) . "</li>\n";
388 $warningHtml .= $msg;
390 $warningHtml .= "</ul>\n";
391 $warningHtml .= wfMsgExt( 'uploadwarning-text', 'parse' );
393 $form = $this->getUploadForm( $warningHtml, $sessionKey, /* $hideIgnoreWarning */ true );
394 $form->setSubmitText( wfMsg( 'upload-tryagain' ) );
395 $form->addButton( 'wpUploadIgnoreWarning', wfMsg( 'ignorewarning' ) );
396 $form->addButton( 'wpCancelUpload', wfMsg( 'reuploaddesc' ) );
398 $this->showUploadForm( $form );
400 # Indicate that we showed a form
405 * Show the upload form with error message, but do not stash the file.
407 * @param $message HTML string
409 protected function showUploadError( $message ) {
410 $message = '<h2>' . wfMsgHtml( 'uploadwarning' ) . "</h2>\n" .
411 '<div class="error">' . $message . "</div>\n";
412 $this->showUploadForm( $this->getUploadForm( $message ) );
417 * Checks are made in SpecialUpload::execute()
419 protected function processUpload() {
420 global $wgUser, $wgOut;
422 // Fetch the file if required
423 $status = $this->mUpload
->fetchFile();
424 if( !$status->isOK() ) {
425 $this->showUploadError( $wgOut->parse( $status->getWikiText() ) );
429 // Deprecated backwards compatibility hook
430 if( !wfRunHooks( 'UploadForm:BeforeProcessing', array( &$this ) ) ) {
431 wfDebug( "Hook 'UploadForm:BeforeProcessing' broke processing the file.\n" );
432 // Return without notifying the user of an error. This sucks, but
433 // this was the previous behaviour as well, and as this hook is
434 // deprecated we're not going to do anything about it.
439 // Upload verification
440 $details = $this->mUpload
->verifyUpload();
441 if ( $details['status'] != UploadBase
::OK
) {
442 $this->processVerificationError( $details );
446 // Verify permissions for this title
447 $permErrors = $this->mUpload
->verifyPermissions( $wgUser );
448 if( $permErrors !== true ) {
449 $code = array_shift( $permErrors[0] );
450 $this->showRecoverableUploadError( wfMsgExt( $code,
451 'parseinline', $permErrors[0] ) );
455 $this->mLocalFile
= $this->mUpload
->getLocalFile();
457 // Check warnings if necessary
458 if( !$this->mIgnoreWarning
) {
459 $warnings = $this->mUpload
->checkWarnings();
460 if( $this->showUploadWarning( $warnings ) ) {
465 // Get the page text if this is not a reupload
466 if( !$this->mForReUpload
) {
467 $pageText = self
::getInitialPageText( $this->mComment
, $this->mLicense
,
468 $this->mCopyrightStatus
, $this->mCopyrightSource
);
472 $status = $this->mUpload
->performUpload( $this->mComment
, $pageText, $this->mWatchthis
, $wgUser );
473 if ( !$status->isGood() ) {
474 $this->showUploadError( $wgOut->parse( $status->getWikiText() ) );
478 // Success, redirect to description page
479 $this->mUploadSuccessful
= true;
480 wfRunHooks( 'SpecialUploadComplete', array( &$this ) );
481 $wgOut->redirect( $this->mLocalFile
->getTitle()->getFullURL() );
485 * Get the initial image page text based on a comment and optional file status information
487 public static function getInitialPageText( $comment = '', $license = '', $copyStatus = '', $source = '' ) {
488 global $wgUseCopyrightUpload;
489 if ( $wgUseCopyrightUpload ) {
491 if ( $license != '' ) {
492 $licensetxt = '== ' . wfMsgForContent( 'license-header' ) . " ==\n" . '{{' . $license . '}}' . "\n";
494 $pageText = '== ' . wfMsgForContent( 'filedesc' ) . " ==\n" . $comment . "\n" .
495 '== ' . wfMsgForContent( 'filestatus' ) . " ==\n" . $copyStatus . "\n" .
497 '== ' . wfMsgForContent( 'filesource' ) . " ==\n" . $source;
499 if ( $license != '' ) {
500 $filedesc = $comment == '' ?
'' : '== ' . wfMsgForContent( 'filedesc' ) . " ==\n" . $comment . "\n";
501 $pageText = $filedesc .
502 '== ' . wfMsgForContent( 'license-header' ) . " ==\n" . '{{' . $license . '}}' . "\n";
504 $pageText = $comment;
511 * See if we should check the 'watch this page' checkbox on the form
512 * based on the user's preferences and whether we're being asked
513 * to create a new file or update an existing one.
515 * In the case where 'watch edits' is off but 'watch creations' is on,
516 * we'll leave the box unchecked.
518 * Note that the page target can be changed *on the form*, so our check
519 * state can get out of sync.
521 protected function getWatchCheck() {
523 if( $wgUser->getOption( 'watchdefault' ) ) {
528 $local = wfLocalFile( $this->mDesiredDestName
);
529 if( $local && $local->exists() ) {
530 // We're uploading a new version of an existing file.
531 // No creation, so don't watch it if we're not already.
532 return $local->getTitle()->userIsWatching();
534 // New page should get watched if that's our option.
535 return $wgUser->getOption( 'watchcreations' );
541 * Provides output to the user for a result of UploadBase::verifyUpload
543 * @param $details Array: result of UploadBase::verifyUpload
545 protected function processVerificationError( $details ) {
546 global $wgFileExtensions, $wgLang;
548 switch( $details['status'] ) {
550 /** Statuses that only require name changing **/
551 case UploadBase
::MIN_LENGTH_PARTNAME
:
552 $this->showRecoverableUploadError( wfMsgHtml( 'minlength1' ) );
554 case UploadBase
::ILLEGAL_FILENAME
:
555 $this->showRecoverableUploadError( wfMsgExt( 'illegalfilename',
556 'parseinline', $details['filtered'] ) );
558 case UploadBase
::FILETYPE_MISSING
:
559 $this->showRecoverableUploadError( wfMsgExt( 'filetype-missing',
563 /** Statuses that require reuploading **/
564 case UploadBase
::EMPTY_FILE
:
565 $this->showUploadError( wfMsgHtml( 'emptyfile' ) );
567 case UploadBase
::FILE_TOO_LARGE
:
568 $this->showUploadError( wfMsgHtml( 'largefileserver' ) );
570 case UploadBase
::FILETYPE_BADTYPE
:
571 $finalExt = $details['finalExt'];
572 $this->showUploadError(
573 wfMsgExt( 'filetype-banned-type',
574 array( 'parseinline' ),
575 htmlspecialchars( $finalExt ),
577 wfMsgExt( 'comma-separator', array( 'escapenoentities' ) ),
580 $wgLang->formatNum( count( $wgFileExtensions ) )
584 case UploadBase
::VERIFICATION_ERROR
:
585 unset( $details['status'] );
586 $code = array_shift( $details['details'] );
587 $this->showUploadError( wfMsgExt( $code, 'parseinline', $details['details'] ) );
589 case UploadBase
::HOOK_ABORTED
:
590 if ( is_array( $details['error'] ) ) { # allow hooks to return error details in an array
591 $args = $details['error'];
592 $error = array_shift( $args );
594 $error = $details['error'];
598 $this->showUploadError( wfMsgExt( $error, 'parseinline', $args ) );
601 throw new MWException( __METHOD__
. ": Unknown value `{$details['status']}`" );
606 * Remove a temporarily kept file stashed by saveTempUploadedFile().
608 * @return Boolean: success
610 protected function unsaveUploadedFile() {
612 if ( !( $this->mUpload
instanceof UploadFromStash
) ) {
615 $success = $this->mUpload
->unsaveUploadedFile();
617 $wgOut->showFileDeleteError( $this->mUpload
->getTempPath() );
624 /*** Functions for formatting warnings ***/
627 * Formats a result of UploadBase::getExistsWarning as HTML
628 * This check is static and can be done pre-upload via AJAX
630 * @param $exists Array: the result of UploadBase::getExistsWarning
631 * @return String: empty string if there is no warning or an HTML fragment
633 public static function getExistsWarning( $exists ) {
640 $file = $exists['file'];
641 $filename = $file->getTitle()->getPrefixedText();
644 $sk = $wgUser->getSkin();
646 if( $exists['warning'] == 'exists' ) {
648 $warning = wfMsgExt( 'fileexists', 'parseinline', $filename );
649 } elseif( $exists['warning'] == 'page-exists' ) {
650 // Page exists but file does not
651 $warning = wfMsgExt( 'filepageexists', 'parseinline', $filename );
652 } elseif ( $exists['warning'] == 'exists-normalized' ) {
653 $warning = wfMsgExt( 'fileexists-extension', 'parseinline', $filename,
654 $exists['normalizedFile']->getTitle()->getPrefixedText() );
655 } elseif ( $exists['warning'] == 'thumb' ) {
656 // Swapped argument order compared with other messages for backwards compatibility
657 $warning = wfMsgExt( 'fileexists-thumbnail-yes', 'parseinline',
658 $exists['thumbFile']->getTitle()->getPrefixedText(), $filename );
659 } elseif ( $exists['warning'] == 'thumb-name' ) {
660 // Image w/o '180px-' does not exists, but we do not like these filenames
661 $name = $file->getName();
662 $badPart = substr( $name, 0, strpos( $name, '-' ) +
1 );
663 $warning = wfMsgExt( 'file-thumbnail-no', 'parseinline', $badPart );
664 } elseif ( $exists['warning'] == 'bad-prefix' ) {
665 $warning = wfMsgExt( 'filename-bad-prefix', 'parseinline', $exists['prefix'] );
666 } elseif ( $exists['warning'] == 'was-deleted' ) {
667 # If the file existed before and was deleted, warn the user of this
668 $ltitle = SpecialPage
::getTitleFor( 'Log' );
669 $llink = $sk->linkKnown(
671 wfMsgHtml( 'deletionlog' ),
678 $warning = wfMsgWikiHtml( 'filewasdeleted', $llink );
685 * Get a list of warnings
687 * @param $filename String: local filename, e.g. 'file exists', 'non-descriptive filename'
688 * @return Array: list of warning messages
690 public static function ajaxGetExistsWarning( $filename ) {
691 $file = wfFindFile( $filename );
693 // Force local file so we have an object to do further checks against
694 // if there isn't an exact match...
695 $file = wfLocalFile( $filename );
699 $exists = UploadBase
::getExistsWarning( $file );
700 $warning = self
::getExistsWarning( $exists );
701 if ( $warning !== '' ) {
702 $s = "<div>$warning</div>";
709 * Construct a warning and a gallery from an array of duplicate files.
711 public static function getDupeWarning( $dupes ) {
715 foreach( $dupes as $file ) {
716 $title = $file->getTitle();
717 $msg .= $title->getPrefixedText() .
718 '|' . $title->getText() . "\n";
720 $msg .= '</gallery>';
722 wfMsgExt( 'file-exists-duplicate', array( 'parse' ), count( $dupes ) ) .
723 $wgOut->parse( $msg ) .
733 * Sub class of HTMLForm that provides the form section of SpecialUpload
735 class UploadForm
extends HTMLForm
{
737 protected $mForReUpload;
738 protected $mSessionKey;
739 protected $mHideIgnoreWarning;
740 protected $mDestWarningAck;
741 protected $mDestFile;
744 protected $mTextAfterSummary;
746 protected $mSourceIds;
748 public function __construct( $options = array() ) {
749 $this->mWatch
= !empty( $options['watch'] );
750 $this->mForReUpload
= !empty( $options['forreupload'] );
751 $this->mSessionKey
= isset( $options['sessionkey'] )
752 ?
$options['sessionkey'] : '';
753 $this->mHideIgnoreWarning
= !empty( $options['hideignorewarning'] );
754 $this->mDestWarningAck
= !empty( $options['destwarningack'] );
755 $this->mDestFile
= isset( $options['destfile'] ) ?
$options['destfile'] : '';
757 $this->mTextTop
= isset( $options['texttop'] )
758 ?
$options['texttop'] : '';
760 $this->mTextAfterSummary
= isset( $options['textaftersummary'] )
761 ?
$options['textaftersummary'] : '';
763 $sourceDescriptor = $this->getSourceSection();
764 $descriptor = $sourceDescriptor
765 +
$this->getDescriptionSection()
766 +
$this->getOptionsSection();
768 wfRunHooks( 'UploadFormInitDescriptor', array( &$descriptor ) );
769 parent
::__construct( $descriptor, 'upload' );
771 # Set some form properties
772 $this->setSubmitText( wfMsg( 'uploadbtn' ) );
773 $this->setSubmitName( 'wpUpload' );
774 # Used message keys: 'accesskey-upload', 'tooltip-upload'
775 $this->setSubmitTooltip( 'upload' );
776 $this->setId( 'mw-upload-form' );
778 # Build a list of IDs for javascript insertion
779 $this->mSourceIds
= array();
780 foreach ( $sourceDescriptor as $key => $field ) {
781 if ( !empty( $field['id'] ) ) {
782 $this->mSourceIds
[] = $field['id'];
789 * Get the descriptor of the fieldset that contains the file source
790 * selection. The section is 'source'
792 * @return Array: descriptor array
794 protected function getSourceSection() {
795 global $wgLang, $wgUser, $wgRequest;
796 global $wgMaxUploadSize;
798 if ( $this->mSessionKey
) {
800 'wpSessionKey' => array(
802 'default' => $this->mSessionKey
,
804 'wpSourceType' => array(
806 'default' => 'Stash',
811 $canUploadByUrl = UploadFromUrl
::isEnabled() && $wgUser->isAllowed( 'upload_by_url' );
812 $radio = $canUploadByUrl;
813 $selectedSourceType = strtolower( $wgRequest->getText( 'wpSourceType', 'File' ) );
815 $descriptor = array();
816 if ( $this->mTextTop
) {
817 $descriptor['UploadFormTextTop'] = array(
819 'section' => 'source',
820 'default' => $this->mTextTop
,
825 $descriptor['UploadFile'] = array(
826 'class' => 'UploadSourceField',
827 'section' => 'source',
829 'id' => 'wpUploadFile',
830 'label-message' => 'sourcefilename',
831 'upload-type' => 'File',
833 'help' => wfMsgExt( 'upload-maxfilesize',
834 array( 'parseinline', 'escapenoentities' ),
836 wfShorthandToInteger( min(
837 wfShorthandToInteger(
838 ini_get( 'upload_max_filesize' )
842 ) . ' ' . wfMsgHtml( 'upload_source_file' ),
843 'checked' => $selectedSourceType == 'file',
845 if ( $canUploadByUrl ) {
846 $descriptor['UploadFileURL'] = array(
847 'class' => 'UploadSourceField',
848 'section' => 'source',
849 'id' => 'wpUploadFileURL',
850 'label-message' => 'sourceurl',
851 'upload-type' => 'url',
853 'help' => wfMsgExt( 'upload-maxfilesize',
854 array( 'parseinline', 'escapenoentities' ),
855 $wgLang->formatSize( $wgMaxUploadSize )
856 ) . ' ' . wfMsgHtml( 'upload_source_url' ),
857 'checked' => $selectedSourceType == 'url',
860 wfRunHooks( 'UploadFormSourceDescriptors', array( &$descriptor, &$radio, $selectedSourceType ) );
862 $descriptor['Extensions'] = array(
864 'section' => 'source',
865 'default' => $this->getExtensionsMessage(),
872 * Get the messages indicating which extensions are preferred and prohibitted.
874 * @return String: HTML string containing the message
876 protected function getExtensionsMessage() {
877 # Print a list of allowed file extensions, if so configured. We ignore
878 # MIME type here, it's incomprehensible to most people and too long.
879 global $wgLang, $wgCheckFileExtensions, $wgStrictFileExtensions,
880 $wgFileExtensions, $wgFileBlacklist;
882 if( $wgCheckFileExtensions ) {
883 if( $wgStrictFileExtensions ) {
884 # Everything not permitted is banned
886 '<div id="mw-upload-permitted">' .
887 wfMsgWikiHtml( 'upload-permitted', $wgLang->commaList( $wgFileExtensions ) ) .
890 # We have to list both preferred and prohibited
892 '<div id="mw-upload-preferred">' .
893 wfMsgWikiHtml( 'upload-preferred', $wgLang->commaList( $wgFileExtensions ) ) .
895 '<div id="mw-upload-prohibited">' .
896 wfMsgWikiHtml( 'upload-prohibited', $wgLang->commaList( $wgFileBlacklist ) ) .
900 # Everything is permitted.
901 $extensionsList = '';
903 return $extensionsList;
907 * Get the descriptor of the fieldset that contains the file description
908 * input. The section is 'description'
910 * @return Array: descriptor array
912 protected function getDescriptionSection() {
913 global $wgUser, $wgOut;
915 $cols = intval( $wgUser->getOption( 'cols' ) );
916 if( $wgUser->getOption( 'editwidth' ) ) {
917 $wgOut->addInlineStyle( '#mw-htmlform-description { width: 100%; }' );
923 'section' => 'description',
924 'id' => 'wpDestFile',
925 'label-message' => 'destfilename',
927 'default' => $this->mDestFile
,
928 # FIXME: hack to work around poor handling of the 'default' option in HTMLForm
929 'nodata' => strval( $this->mDestFile
) !== '',
931 'UploadDescription' => array(
932 'type' => 'textarea',
933 'section' => 'description',
934 'id' => 'wpUploadDescription',
935 'label-message' => $this->mForReUpload
936 ?
'filereuploadsummary'
937 : 'fileuploadsummary',
942 if ( $this->mTextAfterSummary
) {
943 $descriptor['UploadFormTextAfterSummary'] = array(
945 'section' => 'description',
946 'default' => $this->mTextAfterSummary
,
951 $descriptor +
= array(
952 'EditTools' => array(
953 'type' => 'edittools',
954 'section' => 'description',
958 if ( $this->mForReUpload
) {
959 $descriptor['DestFile']['readonly'] = true;
961 $descriptor['License'] = array(
963 'class' => 'Licenses',
964 'section' => 'description',
966 'label-message' => 'license',
970 global $wgUseCopyrightUpload;
971 if ( $wgUseCopyrightUpload ) {
972 $descriptor['UploadCopyStatus'] = array(
974 'section' => 'description',
975 'id' => 'wpUploadCopyStatus',
976 'label-message' => 'filestatus',
978 $descriptor['UploadSource'] = array(
980 'section' => 'description',
981 'id' => 'wpUploadSource',
982 'label-message' => 'filesource',
990 * Get the descriptor of the fieldset that contains the upload options,
991 * such as "watch this file". The section is 'options'
993 * @return Array: descriptor array
995 protected function getOptionsSection() {
998 if ( $wgUser->isLoggedIn() ) {
1000 'Watchthis' => array(
1002 'id' => 'wpWatchthis',
1003 'label-message' => 'watchthisupload',
1004 'section' => 'options',
1005 'default' => $wgUser->getOption( 'watchcreations' ),
1009 if ( !$this->mHideIgnoreWarning
) {
1010 $descriptor['IgnoreWarning'] = array(
1012 'id' => 'wpIgnoreWarning',
1013 'label-message' => 'ignorewarnings',
1014 'section' => 'options',
1018 $descriptor['wpDestFileWarningAck'] = array(
1020 'id' => 'wpDestFileWarningAck',
1021 'default' => $this->mDestWarningAck ?
'1' : '',
1024 if ( $this->mForReUpload
) {
1025 $descriptor['wpForReUpload'] = array(
1027 'id' => 'wpForReUpload',
1036 * Add the upload JS and show the form.
1038 public function show() {
1039 $this->addUploadJS();
1044 * Add upload JS to $wgOut
1046 protected function addUploadJS() {
1047 global $wgUseAjax, $wgAjaxUploadDestCheck, $wgAjaxLicensePreview, $wgEnableAPI, $wgStrictFileExtensions;
1050 $useAjaxDestCheck = $wgUseAjax && $wgAjaxUploadDestCheck;
1051 $useAjaxLicensePreview = $wgUseAjax && $wgAjaxLicensePreview && $wgEnableAPI;
1053 $scriptVars = array(
1054 'wgAjaxUploadDestCheck' => $useAjaxDestCheck,
1055 'wgAjaxLicensePreview' => $useAjaxLicensePreview,
1056 'wgUploadAutoFill' => !$this->mForReUpload
&&
1057 // If we received mDestFile from the request, don't autofill
1058 // the wpDestFile textbox
1059 $this->mDestFile
=== '',
1060 'wgUploadSourceIds' => $this->mSourceIds
,
1061 'wgStrictFileExtensions' => $wgStrictFileExtensions,
1062 'wgCapitalizeUploads' => MWNamespace
::isCapitalized( NS_FILE
),
1065 $wgOut->addScript( Skin
::makeVariablesScript( $scriptVars ) );
1067 // For <charinsert> support
1068 $wgOut->addScriptFile( 'edit.js' );
1069 $wgOut->addScriptFile( 'upload.js' );
1073 * Empty function; submission is handled elsewhere.
1075 * @return bool false
1077 function trySubmit() {
1084 * A form field that contains a radio box in the label
1086 class UploadSourceField
extends HTMLTextField
{
1087 function getLabelHtml( $cellAttributes = array() ) {
1088 $id = "wpSourceType{$this->mParams['upload-type']}";
1089 $label = Html
::rawElement( 'label', array( 'for' => $id ), $this->mLabel
);
1091 if ( !empty( $this->mParams
['radio'] ) ) {
1093 'name' => 'wpSourceType',
1096 'value' => $this->mParams
['upload-type'],
1098 if ( !empty( $this->mParams
['checked'] ) ) {
1099 $attribs['checked'] = 'checked';
1101 $label .= Html
::element( 'input', $attribs );
1104 return Html
::rawElement( 'td', array( 'class' => 'mw-label' ) +
$cellAttributes, $label );
1107 function getSize() {
1108 return isset( $this->mParams
['size'] )
1109 ?
$this->mParams
['size']