<?php
/**
+ * Implements Special:Upload
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License along
+ * with this program; if not, write to the Free Software Foundation, Inc.,
+ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ * http://www.gnu.org/copyleft/gpl.html
+ *
* @file
* @ingroup SpecialPage
* @ingroup Upload
- *
- * Form for handling uploads and special page.
*/
+/**
+ * Form for handling uploads and special page.
+ *
+ * @ingroup SpecialPage
+ * @ingroup Upload
+ */
class SpecialUpload extends SpecialPage {
/**
* Constructor : initialise object
* @param $request WebRequest : data posted.
*/
public function __construct( $request = null ) {
- global $wgRequest;
-
parent::__construct( 'Upload', 'upload' );
-
- $this->loadRequest( is_null( $request ) ? $wgRequest : $request );
}
/** Misc variables **/
- protected $mRequest; // The WebRequest or FauxRequest this form is supposed to handle
- protected $mSourceType;
- protected $mUpload;
- protected $mLocalFile;
- protected $mUploadClicked;
+ public $mRequest; // The WebRequest or FauxRequest this form is supposed to handle
+ public $mSourceType;
+
+ /**
+ * @var UploadBase
+ */
+ public $mUpload;
+
+ /**
+ * @var LocalFile
+ */
+ public $mLocalFile;
+ public $mUploadClicked;
/** User input variables from the "description" section **/
- public $mDesiredDestName; // The requested target file name
- protected $mComment;
- protected $mLicense;
+ public $mDesiredDestName; // The requested target file name
+ public $mComment;
+ public $mLicense;
/** User input variables from the root section **/
- protected $mIgnoreWarning;
- protected $mWatchThis;
- protected $mCopyrightStatus;
- protected $mCopyrightSource;
+ public $mIgnoreWarning;
+ public $mWatchThis;
+ public $mCopyrightStatus;
+ public $mCopyrightSource;
/** Hidden variables **/
- protected $mDestWarningAck;
- protected $mForReUpload; // The user followed an "overwrite this file" link
- protected $mCancelUpload; // The user clicked "Cancel and return to upload form" button
- protected $mTokenOk;
- protected $mUploadSuccessful = false; // Subclasses can use this to determine whether a file was uploaded
+ public $mDestWarningAck;
+ public $mForReUpload; // The user followed an "overwrite this file" link
+ public $mCancelUpload; // The user clicked "Cancel and return to upload form" button
+ public $mTokenOk;
+ public $mUploadSuccessful = false; // Subclasses can use this to determine whether a file was uploaded
/** Text injection points for hooks not using HTMLForm **/
public $uploadFormTextTop;
public $uploadFormTextAfterSummary;
+ public $mWatchthis;
+
/**
* Initialize instance variables from request and create an Upload handler
- *
- * @param $request WebRequest: the request to extract variables from
*/
- protected function loadRequest( $request ) {
- global $wgUser;
-
- $this->mRequest = $request;
+ protected function loadRequest() {
+ $this->mRequest = $request = $this->getRequest();
$this->mSourceType = $request->getVal( 'wpSourceType', 'file' );
$this->mUpload = UploadBase::createFromRequest( $request );
$this->mUploadClicked = $request->wasPosted()
// Guess the desired name from the filename if not provided
$this->mDesiredDestName = $request->getText( 'wpDestFile' );
- if( !$this->mDesiredDestName ) {
- $this->mDesiredDestName = $request->getText( 'wpUploadFile' );
+ if( !$this->mDesiredDestName && $request->getFileName( 'wpUploadFile' ) !== null ) {
+ $this->mDesiredDestName = $request->getFileName( 'wpUploadFile' );
}
$this->mComment = $request->getText( 'wpUploadDescription' );
$this->mLicense = $request->getText( 'wpLicense' );
$this->mDestWarningAck = $request->getText( 'wpDestFileWarningAck' );
$this->mIgnoreWarning = $request->getCheck( 'wpIgnoreWarning' )
|| $request->getCheck( 'wpUploadIgnoreWarning' );
- $this->mWatchthis = $request->getBool( 'wpWatchthis' ) && $wgUser->isLoggedIn();
+ $this->mWatchthis = $request->getBool( 'wpWatchthis' ) && $this->getUser()->isLoggedIn();
$this->mCopyrightStatus = $request->getText( 'wpUploadCopyStatus' );
$this->mCopyrightSource = $request->getText( 'wpUploadSource' );
$this->mForReUpload = $request->getBool( 'wpForReUpload' ); // updating a file
$this->mCancelUpload = $request->getCheck( 'wpCancelUpload' )
- || $request->getCheck( 'wpReUpload' ); // b/w compat
+ || $request->getCheck( 'wpReUpload' ); // b/w compat
// If it was posted check for the token (no remote POST'ing with user credentials)
$token = $request->getVal( 'wpEditToken' );
- if( $this->mSourceType == 'file' && $token == null ) {
- // Skip token check for file uploads as that can't be faked via JS...
- // Some client-side tools don't expect to need to send wpEditToken
- // with their submissions, as that's new in 1.16.
- $this->mTokenOk = true;
- } else {
- $this->mTokenOk = $wgUser->matchEditToken( $token );
- }
+ $this->mTokenOk = $this->getUser()->matchEditToken( $token );
$this->uploadFormTextTop = '';
$this->uploadFormTextAfterSummary = '';
* @param $user User object
* @return Boolean
*/
- public function userCanExecute( $user ) {
+ public function userCanExecute( User $user ) {
return UploadBase::isEnabled() && parent::userCanExecute( $user );
}
* Special page entry point
*/
public function execute( $par ) {
- global $wgUser, $wgOut, $wgRequest;
-
$this->setHeaders();
$this->outputHeader();
# Check uploading enabled
if( !UploadBase::isEnabled() ) {
- $wgOut->showErrorPage( 'uploaddisabled', 'uploaddisabledtext' );
- return;
+ throw new ErrorPageError( 'uploaddisabled', 'uploaddisabledtext' );
}
# Check permissions
- global $wgGroupPermissions;
- if( !$wgUser->isAllowed( 'upload' ) ) {
- if( !$wgUser->isLoggedIn() && ( $wgGroupPermissions['user']['upload']
- || $wgGroupPermissions['autoconfirmed']['upload'] ) ) {
- // Custom message if logged-in users without any special rights can upload
- $wgOut->showErrorPage( 'uploadnologin', 'uploadnologintext' );
- } else {
- $wgOut->permissionRequired( 'upload' );
- }
- return;
+ $user = $this->getUser();
+ $permissionRequired = UploadBase::isAllowed( $user );
+ if( $permissionRequired !== true ) {
+ throw new PermissionsError( $permissionRequired );
}
# Check blocks
- if( $wgUser->isBlocked() ) {
- $wgOut->blockedPage();
- return;
+ if( $user->isBlocked() ) {
+ throw new UserBlockedError( $user->getBlock() );
}
# Check whether we actually want to allow changing stuff
- if( wfReadOnly() ) {
- $wgOut->readOnlyPage();
- return;
- }
+ $this->checkReadOnly();
+
+ $this->loadRequest();
# Unsave the temporary file in case this was a cancelled upload
if ( $this->mCancelUpload ) {
if (
$this->mTokenOk && !$this->mCancelUpload &&
( $this->mUpload && $this->mUploadClicked )
- )
- {
+ ) {
$this->processUpload();
} else {
# Backwards compatibility hook
wfDebug( "Hook 'UploadForm:initial' broke output of the upload form" );
return;
}
-
$this->showUploadForm( $this->getUploadForm() );
}
*/
protected function showUploadForm( $form ) {
# Add links if file was previously deleted
- if ( !$this->mDesiredDestName ) {
+ if ( $this->mDesiredDestName ) {
$this->showViewDeletedLinks();
}
if ( $form instanceof HTMLForm ) {
$form->show();
} else {
- global $wgOut;
- $wgOut->addHTML( $form );
+ $this->getOutput()->addHTML( $form );
}
}
* @return UploadForm
*/
protected function getUploadForm( $message = '', $sessionKey = '', $hideIgnoreWarning = false ) {
- global $wgOut;
-
# Initialize form
$form = new UploadForm( array(
'watch' => $this->getWatchCheck(),
'hideignorewarning' => $hideIgnoreWarning,
'destwarningack' => (bool)$this->mDestWarningAck,
+ 'description' => $this->mComment,
'texttop' => $this->uploadFormTextTop,
'textaftersummary' => $this->uploadFormTextAfterSummary,
- ) );
+ 'destfile' => $this->mDesiredDestName,
+ ), $this->getContext() );
$form->setTitle( $this->getTitle() );
# Check the token, but only if necessary
if(
!$this->mTokenOk && !$this->mCancelUpload &&
( $this->mUpload && $this->mUploadClicked )
- )
- {
+ ) {
$form->addPreText( wfMsgExt( 'session_fail_preview', 'parseinline' ) );
}
# Give a notice if the user is uploading a file that has been deleted or moved
# Note that this is independent from the message 'filewasdeleted' that requires JS
- $desiredTitleObj = Title::newFromText( $this->mDesiredDestName, NS_FILE );
+ $desiredTitleObj = Title::makeTitleSafe( NS_FILE, $this->mDesiredDestName );
$delNotice = ''; // empty by default
if ( $desiredTitleObj instanceof Title && !$desiredTitleObj->exists() ) {
- LogEventsList::showLogExtract( &$delNotice, array( 'delete', 'move' ),
- $desiredTitleObj->getPrefixedText(),
+ LogEventsList::showLogExtract( $delNotice, array( 'delete', 'move' ),
+ $desiredTitleObj,
'', array( 'lim' => 10,
'conds' => array( "log_action != 'revision'" ),
'showIfEmpty' => false,
$form->addPreText( $delNotice );
# Add text to form
- $form->addPreText( '<div id="uploadtext">' .
- wfMsgExt( 'uploadtext', 'parse', array( $this->mDesiredDestName ) ) .
+ $form->addPreText( '<div id="uploadtext">' .
+ wfMsgExt( 'uploadtext', 'parse', array( $this->mDesiredDestName ) ) .
'</div>' );
# Add upload error message
$form->addPreText( $message );
# Add footer to form
- $uploadFooter = wfMsgNoTrans( 'uploadfooter' );
- if ( $uploadFooter != '-' && !wfEmptyMsg( 'uploadfooter', $uploadFooter ) ) {
+ $uploadFooter = wfMessage( 'uploadfooter' );
+ if ( !$uploadFooter->isDisabled() ) {
$form->addPostText( '<div id="mw-upload-footer-message">'
- . $wgOut->parse( $uploadFooter ) . "</div>\n" );
+ . $this->getOutput()->parse( $uploadFooter->plain() ) . "</div>\n" );
}
return $form;
-
}
/**
* Shows the "view X deleted revivions link""
*/
protected function showViewDeletedLinks() {
- global $wgOut, $wgUser;
-
$title = Title::makeTitleSafe( NS_FILE, $this->mDesiredDestName );
+ $user = $this->getUser();
// Show a subtitle link to deleted revisions (to sysops et al only)
if( $title instanceof Title ) {
$count = $title->isDeleted();
- if ( $count > 0 && $wgUser->isAllowed( 'deletedhistory' ) ) {
+ if ( $count > 0 && $user->isAllowed( 'deletedhistory' ) ) {
$link = wfMsgExt(
- $wgUser->isAllowed( 'delete' ) ? 'thisisdeleted' : 'viewdeleted',
+ $user->isAllowed( 'delete' ) ? 'thisisdeleted' : 'viewdeleted',
array( 'parse', 'replaceafter' ),
- $wgUser->getSkin()->linkKnown(
+ Linker::linkKnown(
SpecialPage::getTitleFor( 'Undelete', $title->getPrefixedText() ),
wfMsgExt( 'restorelink', array( 'parsemag', 'escape' ), $count )
)
);
- $wgOut->addHTML( "<div id=\"contentSub2\">{$link}</div>" );
+ $this->getOutput()->addHTML( "<div id=\"contentSub2\">{$link}</div>" );
}
}
-
- // Show the relevant lines from deletion log (for still deleted files only)
- if( $title instanceof Title && $title->isDeletedQuick() && !$title->exists() ) {
- $this->showDeletionLog( $wgOut, $title->getPrefixedText() );
- }
}
/**
*/
protected function showRecoverableUploadError( $message ) {
$sessionKey = $this->mUpload->stashSession();
- $message = '<h2>' . wfMsgHtml( 'uploadwarning' ) . "</h2>\n" .
+ $message = '<h2>' . wfMsgHtml( 'uploaderror' ) . "</h2>\n" .
'<div class="error">' . $message . "</div>\n";
$form = $this->getUploadForm( $message, $sessionKey );
# mDestWarningAck is set when some javascript has shown the warning
# to the user. mForReUpload is set when the user clicks the "upload a
# new version" link.
- if ( !$warnings || ( count( $warnings ) == 1 &&
- isset( $warnings['exists'] ) &&
+ if ( !$warnings || ( count( $warnings ) == 1 &&
+ isset( $warnings['exists'] ) &&
( $this->mDestWarningAck || $this->mForReUpload ) ) )
{
return false;
$warningHtml = '<h2>' . wfMsgHtml( 'uploadwarning' ) . "</h2>\n"
. '<ul class="warning">';
foreach( $warnings as $warning => $args ) {
- $msg = '';
if( $warning == 'exists' ) {
$msg = "\t<li>" . self::getExistsWarning( $args ) . "</li>\n";
} elseif( $warning == 'duplicate' ) {
/**
* Show the upload form with error message, but do not stash the file.
*
- * @param $message HTML string
+ * @param $message string HTML string
*/
protected function showUploadError( $message ) {
$message = '<h2>' . wfMsgHtml( 'uploadwarning' ) . "</h2>\n" .
* Checks are made in SpecialUpload::execute()
*/
protected function processUpload() {
- global $wgUser, $wgOut;
-
- // Verify permissions
- $permErrors = $this->mUpload->verifyPermissions( $wgUser );
- if( $permErrors !== true ) {
- $wgOut->showPermissionsErrorPage( $permErrors );
- return;
- }
-
- if( $this->mUpload instanceOf UploadFromUrl ) {
- return $this->showUploadError( wfMsg( 'uploadfromurl-queued' ) );
- }
-
// Fetch the file if required
$status = $this->mUpload->fetchFile();
if( !$status->isOK() ) {
- $this->showUploadError( $wgOut->parse( $status->getWikiText() ) );
+ $this->showUploadError( $this->getOutput()->parse( $status->getWikiText() ) );
return;
}
- // Deprecated backwards compatibility hook
if( !wfRunHooks( 'UploadForm:BeforeProcessing', array( &$this ) ) ) {
wfDebug( "Hook 'UploadForm:BeforeProcessing' broke processing the file.\n" );
- return array( 'status' => UploadBase::BEFORE_PROCESSING );
+ // This code path is deprecated. If you want to break upload processing
+ // do so by hooking into the appropriate hooks in UploadBase::verifyUpload
+ // and UploadBase::verifyFile.
+ // If you use this hook to break uploading, the user will be returned
+ // an empty form with no error message whatsoever.
+ return;
}
-
// Upload verification
$details = $this->mUpload->verifyUpload();
if ( $details['status'] != UploadBase::OK ) {
return;
}
+ // Verify permissions for this title
+ $permErrors = $this->mUpload->verifyTitlePermissions( $this->getUser() );
+ if( $permErrors !== true ) {
+ $code = array_shift( $permErrors[0] );
+ $this->showRecoverableUploadError( wfMsgExt( $code,
+ 'parseinline', $permErrors[0] ) );
+ return;
+ }
+
$this->mLocalFile = $this->mUpload->getLocalFile();
// Check warnings if necessary
} else {
$pageText = false;
}
- $status = $this->mUpload->performUpload( $this->mComment, $pageText, $this->mWatchthis, $wgUser );
+ $status = $this->mUpload->performUpload( $this->mComment, $pageText, $this->mWatchthis, $this->getUser() );
if ( !$status->isGood() ) {
- $this->showUploadError( $wgOut->parse( $status->getWikiText() ) );
+ $this->showUploadError( $this->getOutput()->parse( $status->getWikiText() ) );
return;
}
// Success, redirect to description page
$this->mUploadSuccessful = true;
wfRunHooks( 'SpecialUploadComplete', array( &$this ) );
- $wgOut->redirect( $this->mLocalFile->getTitle()->getFullURL() );
+ $this->getOutput()->redirect( $this->mLocalFile->getTitle()->getFullURL() );
}
/**
* Get the initial image page text based on a comment and optional file status information
+ * @param $comment string
+ * @param $license string
+ * @param $copyStatus string
+ * @param $source string
+ * @return string
*/
public static function getInitialPageText( $comment = '', $license = '', $copyStatus = '', $source = '' ) {
- global $wgUseCopyrightUpload;
+ global $wgUseCopyrightUpload, $wgForceUIMsgAsContentMsg;
+ $wgForceUIMsgAsContentMsg = (array) $wgForceUIMsgAsContentMsg;
+
+ $msg = array();
+ /* These messages are transcluded into the actual text of the description page.
+ * Thus, forcing them as content messages makes the upload to produce an int: template
+ * instead of hardcoding it there in the uploader language.
+ */
+ foreach( array( 'license-header', 'filedesc', 'filestatus', 'filesource' ) as $msgName ) {
+ if ( in_array( $msgName, $wgForceUIMsgAsContentMsg ) ) {
+ $msg[$msgName] = "{{int:$msgName}}";
+ } else {
+ $msg[$msgName] = wfMsgForContent( $msgName );
+ }
+ }
+
if ( $wgUseCopyrightUpload ) {
$licensetxt = '';
if ( $license != '' ) {
- $licensetxt = '== ' . wfMsgForContent( 'license-header' ) . " ==\n" . '{{' . $license . '}}' . "\n";
+ $licensetxt = '== ' . $msg[ 'license-header' ] . " ==\n" . '{{' . $license . '}}' . "\n";
}
- $pageText = '== ' . wfMsgForContent( 'filedesc' ) . " ==\n" . $comment . "\n" .
- '== ' . wfMsgForContent( 'filestatus' ) . " ==\n" . $copyStatus . "\n" .
+ $pageText = '== ' . $msg[ 'filedesc' ] . " ==\n" . $comment . "\n" .
+ '== ' . $msg[ 'filestatus' ] . " ==\n" . $copyStatus . "\n" .
"$licensetxt" .
- '== ' . wfMsgForContent( 'filesource' ) . " ==\n" . $source;
+ '== ' . $msg[ 'filesource' ] . " ==\n" . $source;
} else {
if ( $license != '' ) {
- $filedesc = $comment == '' ? '' : '== ' . wfMsgForContent( 'filedesc' ) . " ==\n" . $comment . "\n";
+ $filedesc = $comment == '' ? '' : '== ' . $msg[ 'filedesc' ] . " ==\n" . $comment . "\n";
$pageText = $filedesc .
- '== ' . wfMsgForContent( 'license-header' ) . " ==\n" . '{{' . $license . '}}' . "\n";
+ '== ' . $msg[ 'license-header' ] . " ==\n" . '{{' . $license . '}}' . "\n";
} else {
$pageText = $comment;
}
*
* Note that the page target can be changed *on the form*, so our check
* state can get out of sync.
+ * @return Bool|String
*/
protected function getWatchCheck() {
- global $wgUser;
- if( $wgUser->getOption( 'watchdefault' ) ) {
+ if( $this->getUser()->getOption( 'watchdefault' ) ) {
// Watch all edits!
return true;
}
return $local->getTitle()->userIsWatching();
} else {
// New page should get watched if that's our option.
- return $wgUser->getOption( 'watchcreations' );
+ return $this->getUser()->getOption( 'watchcreations' );
}
}
* @param $details Array: result of UploadBase::verifyUpload
*/
protected function processVerificationError( $details ) {
- global $wgFileExtensions, $wgLang;
+ global $wgFileExtensions;
switch( $details['status'] ) {
$this->showRecoverableUploadError( wfMsgExt( 'illegalfilename',
'parseinline', $details['filtered'] ) );
break;
- case UploadBase::OVERWRITE_EXISTING_FILE:
- $this->showRecoverableUploadError( wfMsgExt( $details['overwrite'],
- 'parseinline' ) );
+ case UploadBase::FILENAME_TOO_LONG:
+ $this->showRecoverableUploadError( wfMsgHtml( 'filename-toolong' ) );
break;
case UploadBase::FILETYPE_MISSING:
$this->showRecoverableUploadError( wfMsgExt( 'filetype-missing',
'parseinline' ) );
break;
+ case UploadBase::WINDOWS_NONASCII_FILENAME:
+ $this->showRecoverableUploadError( wfMsgExt( 'windows-nonascii-filename',
+ 'parseinline' ) );
+ break;
/** Statuses that require reuploading **/
case UploadBase::EMPTY_FILE:
$this->showUploadError( wfMsgHtml( 'largefileserver' ) );
break;
case UploadBase::FILETYPE_BADTYPE:
- $finalExt = $details['finalExt'];
- $this->showUploadError(
- wfMsgExt( 'filetype-banned-type',
- array( 'parseinline' ),
- htmlspecialchars( $finalExt ),
- implode(
- wfMsgExt( 'comma-separator', array( 'escapenoentities' ) ),
- $wgFileExtensions
- ),
- $wgLang->formatNum( count( $wgFileExtensions ) )
- )
- );
+ $msg = wfMessage( 'filetype-banned-type' );
+ if ( isset( $details['blacklistedExt'] ) ) {
+ $msg->params( $this->getLanguage()->commaList( $details['blacklistedExt'] ) );
+ } else {
+ $msg->params( $details['finalExt'] );
+ }
+ $msg->params( $this->getLanguage()->commaList( $wgFileExtensions ),
+ count( $wgFileExtensions ) );
+
+ // Add PLURAL support for the first parameter. This results
+ // in a bit unlogical parameter sequence, but does not break
+ // old translations
+ if ( isset( $details['blacklistedExt'] ) ) {
+ $msg->params( count( $details['blacklistedExt'] ) );
+ } else {
+ $msg->params( 1 );
+ }
+
+ $this->showUploadError( $msg->parse() );
break;
case UploadBase::VERIFICATION_ERROR:
unset( $details['status'] );
* @return Boolean: success
*/
protected function unsaveUploadedFile() {
- global $wgOut;
if ( !( $this->mUpload instanceof UploadFromStash ) ) {
return true;
}
$success = $this->mUpload->unsaveUploadedFile();
if ( !$success ) {
- $wgOut->showFileDeleteError( $this->mUpload->getTempPath() );
+ $this->getOutput()->showFileDeleteError( $this->mUpload->getTempPath() );
return false;
} else {
return true;
* @return String: empty string if there is no warning or an HTML fragment
*/
public static function getExistsWarning( $exists ) {
- global $wgUser, $wgContLang;
-
if ( !$exists ) {
return '';
}
$filename = $file->getTitle()->getPrefixedText();
$warning = '';
- $sk = $wgUser->getSkin();
-
if( $exists['warning'] == 'exists' ) {
// Exact match
$warning = wfMsgExt( 'fileexists', 'parseinline', $filename );
} elseif ( $exists['warning'] == 'was-deleted' ) {
# If the file existed before and was deleted, warn the user of this
$ltitle = SpecialPage::getTitleFor( 'Log' );
- $llink = $sk->linkKnown(
+ $llink = Linker::linkKnown(
$ltitle,
wfMsgHtml( 'deletionlog' ),
array(),
'page' => $filename
)
);
- $warning = wfMsgWikiHtml( 'filewasdeleted', $llink );
+ $warning = wfMsgExt( 'filewasdeleted', array( 'parse', 'replaceafter' ), $llink );
}
return $warning;
// if there isn't an exact match...
$file = wfLocalFile( $filename );
}
- $s = ' ';
+ $s = ' ';
if ( $file ) {
$exists = UploadBase::getExistsWarning( $file );
$warning = self::getExistsWarning( $exists );
/**
* Construct a warning and a gallery from an array of duplicate files.
+ * @param $dupes array
+ * @return string
*/
public static function getDupeWarning( $dupes ) {
+ global $wgOut;
if( $dupes ) {
- global $wgOut;
$msg = '<gallery>';
foreach( $dupes as $file ) {
$title = $file->getTitle();
protected $mSessionKey;
protected $mHideIgnoreWarning;
protected $mDestWarningAck;
+ protected $mDestFile;
+ protected $mComment;
protected $mTextTop;
protected $mTextAfterSummary;
protected $mSourceIds;
- public function __construct( $options = array() ) {
+ protected $mMaxFileSize = array();
+
+ protected $mMaxUploadSize = array();
+
+ public function __construct( array $options = array(), IContextSource $context = null ) {
$this->mWatch = !empty( $options['watch'] );
$this->mForReUpload = !empty( $options['forreupload'] );
$this->mSessionKey = isset( $options['sessionkey'] )
? $options['sessionkey'] : '';
$this->mHideIgnoreWarning = !empty( $options['hideignorewarning'] );
$this->mDestWarningAck = !empty( $options['destwarningack'] );
+ $this->mDestFile = isset( $options['destfile'] ) ? $options['destfile'] : '';
+
+ $this->mComment = isset( $options['description'] ) ?
+ $options['description'] : '';
+
+ $this->mTextTop = isset( $options['texttop'] )
+ ? $options['texttop'] : '';
- $this->mTextTop = $options['texttop'];
- $this->mTextAfterSummary = $options['textaftersummary'];
+ $this->mTextAfterSummary = isset( $options['textaftersummary'] )
+ ? $options['textaftersummary'] : '';
$sourceDescriptor = $this->getSourceSection();
$descriptor = $sourceDescriptor
+ $this->getOptionsSection();
wfRunHooks( 'UploadFormInitDescriptor', array( &$descriptor ) );
- parent::__construct( $descriptor, 'upload' );
+ parent::__construct( $descriptor, $context, 'upload' );
# Set some form properties
$this->setSubmitText( wfMsg( 'uploadbtn' ) );
$this->setSubmitName( 'wpUpload' );
+ # Used message keys: 'accesskey-upload', 'tooltip-upload'
$this->setSubmitTooltip( 'upload' );
$this->setId( 'mw-upload-form' );
# Build a list of IDs for javascript insertion
$this->mSourceIds = array();
- foreach ( $sourceDescriptor as $key => $field ) {
+ foreach ( $sourceDescriptor as $field ) {
if ( !empty( $field['id'] ) ) {
$this->mSourceIds[] = $field['id'];
}
* @return Array: descriptor array
*/
protected function getSourceSection() {
- global $wgLang, $wgUser, $wgRequest;
- global $wgMaxUploadSize;
-
if ( $this->mSessionKey ) {
return array(
- 'wpSessionKey' => array(
+ 'SessionKey' => array(
'type' => 'hidden',
'default' => $this->mSessionKey,
),
- 'wpSourceType' => array(
+ 'SourceType' => array(
'type' => 'hidden',
'default' => 'Stash',
),
);
}
- $canUploadByUrl = UploadFromUrl::isEnabled() && $wgUser->isAllowed( 'upload_by_url' );
+ $canUploadByUrl = UploadFromUrl::isEnabled() && UploadFromUrl::isAllowed( $this->getUser() );
$radio = $canUploadByUrl;
- $selectedSourceType = strtolower( $wgRequest->getText( 'wpSourceType', 'File' ) );
+ $selectedSourceType = strtolower( $this->getRequest()->getText( 'wpSourceType', 'File' ) );
$descriptor = array();
if ( $this->mTextTop ) {
);
}
+ $this->mMaxUploadSize['file'] = UploadBase::getMaxUploadSize( 'file' );
+ # Limit to upload_max_filesize unless we are running under HipHop and
+ # that setting doesn't exist
+ if ( !wfIsHipHop() ) {
+ $this->mMaxUploadSize['file'] = min( $this->mMaxUploadSize['file'],
+ wfShorthandToInteger( ini_get( 'upload_max_filesize' ) ),
+ wfShorthandToInteger( ini_get( 'post_max_size' ) )
+ );
+ }
+
$descriptor['UploadFile'] = array(
'class' => 'UploadSourceField',
'section' => 'source',
'radio' => &$radio,
'help' => wfMsgExt( 'upload-maxfilesize',
array( 'parseinline', 'escapenoentities' ),
- $wgLang->formatSize(
- wfShorthandToInteger( min(
- wfShorthandToInteger(
- ini_get( 'upload_max_filesize' )
- ), $wgMaxUploadSize
- ) )
- )
+ $this->getContext()->getLanguage()->formatSize( $this->mMaxUploadSize['file'] )
) . ' ' . wfMsgHtml( 'upload_source_file' ),
'checked' => $selectedSourceType == 'file',
);
if ( $canUploadByUrl ) {
+ $this->mMaxUploadSize['url'] = UploadBase::getMaxUploadSize( 'url' );
$descriptor['UploadFileURL'] = array(
'class' => 'UploadSourceField',
'section' => 'source',
'radio' => &$radio,
'help' => wfMsgExt( 'upload-maxfilesize',
array( 'parseinline', 'escapenoentities' ),
- $wgLang->formatSize( $wgMaxUploadSize )
+ $this->getContext()->getLanguage()->formatSize( $this->mMaxUploadSize['url'] )
) . ' ' . wfMsgHtml( 'upload_source_url' ),
'checked' => $selectedSourceType == 'url',
);
protected function getExtensionsMessage() {
# Print a list of allowed file extensions, if so configured. We ignore
# MIME type here, it's incomprehensible to most people and too long.
- global $wgLang, $wgCheckFileExtensions, $wgStrictFileExtensions,
+ global $wgCheckFileExtensions, $wgStrictFileExtensions,
$wgFileExtensions, $wgFileBlacklist;
- $allowedExtensions = '';
if( $wgCheckFileExtensions ) {
if( $wgStrictFileExtensions ) {
# Everything not permitted is banned
$extensionsList =
'<div id="mw-upload-permitted">' .
- wfMsgWikiHtml( 'upload-permitted', $wgLang->commaList( $wgFileExtensions ) ) .
+ wfMsgExt( 'upload-permitted', 'parse', $this->getContext()->getLanguage()->commaList( $wgFileExtensions ) ) .
"</div>\n";
} else {
# We have to list both preferred and prohibited
$extensionsList =
'<div id="mw-upload-preferred">' .
- wfMsgWikiHtml( 'upload-preferred', $wgLang->commaList( $wgFileExtensions ) ) .
+ wfMsgExt( 'upload-preferred', 'parse', $this->getContext()->getLanguage()->commaList( $wgFileExtensions ) ) .
"</div>\n" .
'<div id="mw-upload-prohibited">' .
- wfMsgWikiHtml( 'upload-prohibited', $wgLang->commaList( $wgFileBlacklist ) ) .
+ wfMsgExt( 'upload-prohibited', 'parse', $this->getContext()->getLanguage()->commaList( $wgFileBlacklist ) ) .
"</div>\n";
}
} else {
* @return Array: descriptor array
*/
protected function getDescriptionSection() {
- global $wgUser, $wgOut;
-
- $cols = intval( $wgUser->getOption( 'cols' ) );
- if( $wgUser->getOption( 'editwidth' ) ) {
- $wgOut->addInlineStyle( '#mw-htmlform-description { width: 100%; }' );
+ if ( $this->mSessionKey ) {
+ $stash = RepoGroup::singleton()->getLocalRepo()->getUploadStash();
+ try {
+ $file = $stash->getFile( $this->mSessionKey );
+ } catch ( MWException $e ) {
+ $file = null;
+ }
+ if ( $file ) {
+ global $wgContLang;
+
+ $mto = $file->transform( array( 'width' => 120 ) );
+ $this->addHeaderText(
+ '<div class="thumb t' . $wgContLang->alignEnd() . '">' .
+ Html::element( 'img', array(
+ 'src' => $mto->getUrl(),
+ 'class' => 'thumbimage',
+ ) ) . '</div>', 'description' );
+ }
}
$descriptor = array(
'id' => 'wpDestFile',
'label-message' => 'destfilename',
'size' => 60,
+ 'default' => $this->mDestFile,
+ # @todo FIXME: Hack to work around poor handling of the 'default' option in HTMLForm
+ 'nodata' => strval( $this->mDestFile ) !== '',
),
'UploadDescription' => array(
'type' => 'textarea',
'label-message' => $this->mForReUpload
? 'filereuploadsummary'
: 'fileuploadsummary',
- 'cols' => $cols,
+ 'default' => $this->mComment,
+ 'cols' => intval( $this->getUser()->getOption( 'cols' ) ),
'rows' => 8,
)
);
'EditTools' => array(
'type' => 'edittools',
'section' => 'description',
- ),
- 'License' => array(
+ 'message' => 'edittools-upload',
+ )
+ );
+
+ if ( $this->mForReUpload ) {
+ $descriptor['DestFile']['readonly'] = true;
+ } else {
+ $descriptor['License'] = array(
'type' => 'select',
'class' => 'Licenses',
'section' => 'description',
'id' => 'wpLicense',
'label-message' => 'license',
- ),
- );
- if ( $this->mForReUpload ) {
- $descriptor['DestFile']['readonly'] = true;
+ );
}
global $wgUseCopyrightUpload;
* @return Array: descriptor array
*/
protected function getOptionsSection() {
- global $wgUser;
-
- if ( $wgUser->isLoggedIn() ) {
+ $user = $this->getUser();
+ if ( $user->isLoggedIn() ) {
$descriptor = array(
'Watchthis' => array(
'type' => 'check',
'id' => 'wpWatchthis',
'label-message' => 'watchthisupload',
'section' => 'options',
- 'default' => $wgUser->getOption( 'watchcreations' ),
+ 'default' => $user->getOption( 'watchcreations' ),
)
);
}
);
}
- $descriptor['wpDestFileWarningAck'] = array(
+ $descriptor['DestFileWarningAck'] = array(
'type' => 'hidden',
'id' => 'wpDestFileWarningAck',
'default' => $this->mDestWarningAck ? '1' : '',
);
-
+
if ( $this->mForReUpload ) {
- $descriptor['wpForReUpload'] = array(
+ $descriptor['ForReUpload'] = array(
'type' => 'hidden',
'id' => 'wpForReUpload',
'default' => '1',
}
/**
- * Add upload JS to $wgOut
+ * Add upload JS to the OutputPage
*/
protected function addUploadJS() {
- global $wgUseAjax, $wgAjaxUploadDestCheck, $wgAjaxLicensePreview, $wgEnableAPI;
- global $wgOut;
+ global $wgUseAjax, $wgAjaxUploadDestCheck, $wgAjaxLicensePreview, $wgEnableAPI, $wgStrictFileExtensions;
$useAjaxDestCheck = $wgUseAjax && $wgAjaxUploadDestCheck;
$useAjaxLicensePreview = $wgUseAjax && $wgAjaxLicensePreview && $wgEnableAPI;
+ $this->mMaxUploadSize['*'] = UploadBase::getMaxUploadSize();
$scriptVars = array(
'wgAjaxUploadDestCheck' => $useAjaxDestCheck,
'wgAjaxLicensePreview' => $useAjaxLicensePreview,
- 'wgUploadAutoFill' => !$this->mForReUpload,
+ 'wgUploadAutoFill' => !$this->mForReUpload &&
+ // If we received mDestFile from the request, don't autofill
+ // the wpDestFile textbox
+ $this->mDestFile === '',
'wgUploadSourceIds' => $this->mSourceIds,
+ 'wgStrictFileExtensions' => $wgStrictFileExtensions,
+ 'wgCapitalizeUploads' => MWNamespace::isCapitalized( NS_FILE ),
+ 'wgMaxUploadSize' => $this->mMaxUploadSize,
);
- $wgOut->addScript( Skin::makeVariablesScript( $scriptVars ) );
+ $out = $this->getOutput();
+ $out->addJsConfigVars( $scriptVars );
- // For <charinsert> support
- $wgOut->addScriptFile( 'edit.js' );
- $wgOut->addScriptFile( 'upload.js' );
+
+ $out->addModules( array(
+ 'mediawiki.action.edit', // For <charinsert> support
+ 'mediawiki.legacy.upload', // Old form stuff...
+ 'mediawiki.special.upload', // Newer extras for thumbnail preview.
+ ) );
}
/**
* A form field that contains a radio box in the label
*/
class UploadSourceField extends HTMLTextField {
- function getLabelHtml() {
+
+ /**
+ * @param $cellAttributes array
+ * @return string
+ */
+ function getLabelHtml( $cellAttributes = array() ) {
$id = "wpSourceType{$this->mParams['upload-type']}";
$label = Html::rawElement( 'label', array( 'for' => $id ), $this->mLabel );
$label .= Html::element( 'input', $attribs );
}
- return Html::rawElement( 'td', array( 'class' => 'mw-label' ), $label );
+ return Html::rawElement( 'td', array( 'class' => 'mw-label' ) + $cellAttributes, $label );
}
+ /**
+ * @return int
+ */
function getSize() {
return isset( $this->mParams['size'] )
? $this->mParams['size']