From c824e30197e40d473e36da488287ae05fa19e646 Mon Sep 17 00:00:00 2001 From: Bryan Tong Minh Date: Fri, 23 Oct 2009 20:05:31 +0000 Subject: [PATCH] * Updated upload hooks documentation * Allow users to submit a modified file description without ignoring warnings --- docs/hooks.txt | 24 ++++++++------ includes/specials/SpecialUpload.php | 51 ++++++++++++++++++++--------- includes/upload/UploadBase.php | 14 +++++--- includes/upload/UploadFromStash.php | 13 +++++--- includes/upload/UploadFromUrl.php | 2 +- languages/messages/MessagesEn.php | 2 ++ 6 files changed, 70 insertions(+), 36 deletions(-) diff --git a/docs/hooks.txt b/docs/hooks.txt index 3671b3c3c6..ca3cda4d2d 100644 --- a/docs/hooks.txt +++ b/docs/hooks.txt @@ -1448,7 +1448,7 @@ $term: string of search term 'SpecialUploadComplete': Called after successfully uploading a file from Special:Upload -$form: The UploadForm object +$form: The SpecialUpload object 'SpecialVersionExtensionTypes': called when generating the extensions credits, use this to change the tables headers @@ -1493,16 +1493,20 @@ $article: article object removed $user: user that watched $article: article object that was watched -'UploadForm:initial': before the upload form is generated -$form: UploadForm object -You might set the member-variables $uploadFormTextTop and -$uploadFormTextAfterSummary to inject text (HTML) either before -or after the editform. +'UploadCreateFromRequest': when UploadBase::createFromRequest has been called +string $type: the requested upload type +&$className: the class name of the Upload instance to be created -'UploadForm:BeforeProcessing': at the beginning of processUpload() -$form: UploadForm object -Lets you poke at member variables like $mUploadDescription before the -file is saved. +'UploadComplete': when Upload completes an upload +&$upload: an UploadBase child instance + +'UploadFormInitDescriptor': after the descriptor for the upload form as been +assembled +array $descriptor: the HTMLForm descriptor + +'UploadFormSourceDescriptors': after the standard source inputs have been +added to the descriptor +array $descriptor: the HTMLForm descriptor 'UploadVerification': additional chances to reject an uploaded file string $saveName: destination file name diff --git a/includes/specials/SpecialUpload.php b/includes/specials/SpecialUpload.php index 3317b5ed47..7da76af142 100644 --- a/includes/specials/SpecialUpload.php +++ b/includes/specials/SpecialUpload.php @@ -56,7 +56,9 @@ class SpecialUpload extends SpecialPage { $this->mRequest = $request; $this->mSourceType = $request->getVal( 'wpSourceType', 'file' ); $this->mUpload = UploadBase::createFromRequest( $request ); - $this->mUploadClicked = $request->getCheck( 'wpUpload' ) && $request->wasPosted(); + $this->mUploadClicked = $request->wasPosted() + && ( $request->getCheck( 'wpUpload' ) + || $request->getCheck( 'wpUploadIgnoreWarning' ) ); // Guess the desired name from the filename if not provided $this->mDesiredDestName = $request->getText( 'wpDestFile' ); @@ -66,7 +68,8 @@ class SpecialUpload extends SpecialPage { $this->mLicense = $request->getText( 'wpLicense' ); - $this->mIgnoreWarning = $request->getCheck( 'wpIgnoreWarning' ); + $this->mIgnoreWarning = $request->getCheck( 'wpIgnoreWarning' ) + || $request->getCheck( 'wpUploadIgnoreWarning' ); $this->mWatchthis = $request->getBool( 'wpWatchthis' ); $this->mCopyrightStatus = $request->getText( 'wpUploadCopyStatus' ); $this->mCopyrightSource = $request->getText( 'wpUploadSource' ); @@ -182,11 +185,16 @@ class SpecialUpload extends SpecialPage { * @param string $sessionKey Session key in case this is a stashed upload * @return UploadForm */ - protected function getUploadForm( $message = '', $sessionKey = '' ) { + protected function getUploadForm( $message = '', $sessionKey = '', $hideIgnoreWarning = false ) { global $wgOut; # Initialize form - $form = new UploadForm( $this->watchCheck(), $this->mForReUpload, $sessionKey ); + $form = new UploadForm( array( + 'watch' => $this->watchCheck(), + 'forreupload' => $this->mForReUpload, + 'sessionkey' => $sessionKey, + 'hideignorewarning' => $hideIgnoreWarning, + ) ); $form->setTitle( $this->getTitle() ); # Check the token, but only if necessary @@ -251,7 +259,10 @@ class SpecialUpload extends SpecialPage { $sessionKey = $this->mUpload->stashSession(); $message = '

' . wfMsgHtml( 'uploadwarning' ) . "

\n" . '
' . $message . "
\n"; - $this->showUploadForm( $this->getUploadForm( $message, $sessionKey ) ); + + $form = $this->getUploadForm( $message, $sessionKey ); + $form->setSubmitText( wfMsg( 'upload-tryagain' ) ); + $this->showUploadForm( $form ); } /** * Stashes the upload, shows the main form, but adds an "continue anyway button" @@ -286,10 +297,12 @@ class SpecialUpload extends SpecialPage { } $warningHtml .= $msg; } - $warningHtml .= ''; + $warningHtml .= "\n"; + $warningHtml .= wfMsgExt( 'uploadwarning-text', 'parse' ); - $form = $this->getUploadForm( $warningHtml, $sessionKey ); - $form->setSubmitText( wfMsg( 'ignorewarning' ) ); + $form = $this->getUploadForm( $warningHtml, $sessionKey, /* $hideIgnoreWarning */ true ); + $form->setSubmitText( wfMsg( 'upload-tryagain' ) ); + $form->addButton( 'wpUploadIgnoreWarning', wfMsg( 'ignorewarning' ) ); $form->addButton( 'wpCancelUpload', wfMsg( 'reuploaddesc' ) ); $this->showUploadForm( $form ); @@ -621,14 +634,18 @@ class UploadForm extends HTMLForm { protected $mWatch; protected $mForReUpload; protected $mSessionKey; + protected $mHideIgnoreWarning; + protected $mSourceIds; - public function __construct( $watch, $forReUpload = false, $sessionKey = '' ) { + public function __construct( $options = array() ) { global $wgLang; - $this->mWatch = $watch; - $this->mForReUpload = $forReUpload; - $this->mSessionKey = $sessionKey; + $this->mWatch = !empty( $options['watch'] ); + $this->mForReUpload = !empty( $options['forreupload'] ); + $this->mSessionKey = isset( $options['sessionkey'] ) + ? $options['sessionkey'] : ''; + $this->mHideIgnoreWarning = !empty( $options['hideignorewarning'] ); $sourceDescriptor = $this->getSourceSection(); $descriptor = $sourceDescriptor @@ -841,14 +858,16 @@ class UploadForm extends HTMLForm { 'id' => 'wpWatchthis', 'label-message' => 'watchthisupload', 'section' => 'options', - ), - 'IgnoreWarning' => array( + ) + ); + if ( !$this->mHideIgnoreWarning ) { + $descriptor['IgnoreWarning'] = array( 'type' => 'check', 'id' => 'wpIgnoreWarning', 'label-message' => 'ignorewarnings', 'section' => 'options', - ), - ); + ); + } return $descriptor; diff --git a/includes/upload/UploadBase.php b/includes/upload/UploadBase.php index 5c2929f2a1..147da3dd04 100644 --- a/includes/upload/UploadBase.php +++ b/includes/upload/UploadBase.php @@ -76,10 +76,16 @@ abstract class UploadBase { // Get the upload class $type = ucfirst( $type ); - $className = 'UploadFrom' . $type; - wfDebug( __METHOD__ . ": class name: $className\n" ); - if( !in_array( $type, self::$uploadHandlers ) ) - return null; + + // Give hooks the chance to handle this request + $className = null; + wfRunHooks( 'UploadCreateFromRequest', array( $type, &$className ) ); + if ( is_null( $className ) ) { + $className = 'UploadFrom' . $type; + wfDebug( __METHOD__ . ": class name: $className\n" ); + if( !in_array( $type, self::$uploadHandlers ) ) + return null; + } // Check whether this upload class is enabled if( !call_user_func( array( $className, 'isEnabled' ) ) ) diff --git a/includes/upload/UploadFromStash.php b/includes/upload/UploadFromStash.php index cb5926ff3e..38d99f7d78 100644 --- a/includes/upload/UploadFromStash.php +++ b/includes/upload/UploadFromStash.php @@ -46,13 +46,13 @@ class UploadFromStash extends UploadBase { } public function initializeFromRequest( &$request ) { - $sessionKey = $request->getInt( 'wpSessionKey' ); + $this->mSessionKey = $request->getInt( 'wpSessionKey' ); $sessionData = $request->getSessionData('wsUploadData'); $desiredDestName = $request->getText( 'wpDestFile' ); if( !$desiredDestName ) $desiredDestName = $request->getText( 'wpUploadFile' ); - return $this->initialize( $desiredDestName, $sessionData[$sessionKey], false ); + return $this->initialize( $desiredDestName, $sessionData[$this->mSessionKey], false ); } /** @@ -62,11 +62,14 @@ class UploadFromStash extends UploadBase { return true; } + /** - * We're here from "ignore warnings anyway" so return just OK + * There is no need to stash the image twice */ - public function checkWarnings() { - return array(); + public function stashSession() { + if ( !empty( $this->mSessionKey ) ) + return $this->mSessionKey; + return parent::stashSession(); } /** diff --git a/includes/upload/UploadFromUrl.php b/includes/upload/UploadFromUrl.php index c5458da824..d872bb1240 100644 --- a/includes/upload/UploadFromUrl.php +++ b/includes/upload/UploadFromUrl.php @@ -64,7 +64,7 @@ class UploadFromUrl extends UploadBase { public function initializeFromRequest( &$request ) { $desiredDestName = $request->getText( 'wpDestFile' ); if( !$desiredDestName ) - $desiredDestName = $request->getText( 'wpUploadFile' ); + $desiredDestName = $request->getText( 'wpUploadFileURL' ); return $this->initialize( $desiredDestName, $request->getVal( 'wpUploadFileURL' ), diff --git a/languages/messages/MessagesEn.php b/languages/messages/MessagesEn.php index d204778397..e03567e2a6 100644 --- a/languages/messages/MessagesEn.php +++ b/languages/messages/MessagesEn.php @@ -2040,6 +2040,7 @@ Pages on [[Special:Watchlist|your watchlist]] are '''bold'''.", 'upload' => 'Upload file', 'uploadbtn' => 'Upload file', 'reuploaddesc' => 'Cancel upload and return to the upload form', +'upload-tryagain' => 'Submit modified file description', 'uploadnologin' => 'Not logged in', 'uploadnologintext' => 'You must be [[Special:UserLogin|logged in]] to upload files.', 'upload_directory_missing' => 'The upload directory ($1) is missing and could not be created by the webserver.', @@ -2115,6 +2116,7 @@ If you still want to upload your file, please go back and use a new name. You should check that file's deletion history before proceeding to re-upload it.", 'successfulupload' => 'Successful upload', 'uploadwarning' => 'Upload warning', +'uploadwarning-text' => 'Please modify the file description below and try again.', 'savefile' => 'Save file', 'uploadedimage' => 'uploaded "[[$1]]"', 'overwroteimage' => 'uploaded a new version of "[[$1]]"', -- 2.20.1