* Updated upload hooks documentation
authorBryan Tong Minh <btongminh@users.mediawiki.org>
Fri, 23 Oct 2009 20:05:31 +0000 (20:05 +0000)
committerBryan Tong Minh <btongminh@users.mediawiki.org>
Fri, 23 Oct 2009 20:05:31 +0000 (20:05 +0000)
* Allow users to submit a modified file description without ignoring warnings

docs/hooks.txt
includes/specials/SpecialUpload.php
includes/upload/UploadBase.php
includes/upload/UploadFromStash.php
includes/upload/UploadFromUrl.php
languages/messages/MessagesEn.php

index 3671b3c..ca3cda4 100644 (file)
@@ -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
index 3317b5e..7da76af 100644 (file)
@@ -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 = '<h2>' . wfMsgHtml( 'uploadwarning' ) . "</h2>\n" .
                        '<div class="error">' . $message . "</div>\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 .= '</ul>';
+               $warningHtml .= "</ul>\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;
 
index 5c2929f..147da3d 100644 (file)
@@ -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' ) ) )
index cb5926f..38d99f7 100644 (file)
@@ -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();
        }
 
        /**
index c5458da..d872bb1 100644 (file)
@@ -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' ),
index d204778..e03567e 100644 (file)
@@ -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]]"',