http://www.mediawiki.org/wiki/Special:Code/MediaWiki/73843

Revision: 73843
Author:   neilk
Date:     2010-09-27 23:59:21 +0000 (Mon, 27 Sep 2010)

Log Message:
-----------
giving up on implementing most of this in extension-land; implementing the 
stash as a standard UploadBase-inheriting upload method; picked during 
selectUploadModule()

Modified Paths:
--------------
    branches/uploadwizard/extensions/UploadWizard/SessionStash.php
    branches/uploadwizard/extensions/UploadWizard/UploadWizard.php
    branches/uploadwizard/phase3/includes/api/ApiUpload.php
    branches/uploadwizard/phase3/includes/upload/UploadBase.php

Added Paths:
-----------
    branches/uploadwizard/extensions/UploadWizard/UploadFromFileToStash.php

Modified: branches/uploadwizard/extensions/UploadWizard/SessionStash.php
===================================================================
--- branches/uploadwizard/extensions/UploadWizard/SessionStash.php      
2010-09-27 23:58:00 UTC (rev 73842)
+++ branches/uploadwizard/extensions/UploadWizard/SessionStash.php      
2010-09-27 23:59:21 UTC (rev 73843)
@@ -84,7 +84,7 @@
         * @param {String} name - this is used for directory hashing when 
storing. Otherwise not important
         * @param {String} path - path to file you want stashed
         * @param {Array} data - other data you want added to the session. Do 
not use 'mTempPath', 'mFileProps', 'mFileSize', or version as keys here
-        * @return {SessionStashItem} item
+        * @return {SessionStashFile} file
         */
        public function stashFile( $key, $path, $data=array() ) {
                if ( !$key ) {

Added: branches/uploadwizard/extensions/UploadWizard/UploadFromFileToStash.php
===================================================================
--- branches/uploadwizard/extensions/UploadWizard/UploadFromFileToStash.php     
                        (rev 0)
+++ branches/uploadwizard/extensions/UploadWizard/UploadFromFileToStash.php     
2010-09-27 23:59:21 UTC (rev 73843)
@@ -0,0 +1,97 @@
+<?php
+/**
+ * Implements regular file uploads, except: 
+ *     - stores in local temp area, adds record to session (SessionStash)
+ *     - returns lots of mime info and metadata
+ *     - creates thumbnail
+ *     - returns URLs for file and thumbnail accessible only to uploading user
+ *
+ * @file
+ * @ingroup upload
+ * @author Neil Kandalgaonkar
+ */
+
+class UploadFromFileToStash extends UploadFromFile {
+
+       /**
+        * Return the primary uploaded file (when in stash)
+        * @return {File}
+       public function getLocalFile() {
+               if ( ! is_a( $this->mLocalFile, 'File' ) ) {
+                       wfDebug( __METHOD__ );
+                       wfDebug( print_r( debug_backtrace(), 1 ) );
+                       throw new MWException( 'file not in stash yet' );
+               }
+               return $this->mLocalFile;
+       }
+        */
+
+
+       /**
+        * Overrides performUpload, which normally adds the file to the 
database and makes it publicly available.
+        * Instead, we store it in the SessionStash, and return metadata about 
the file
+        * We also create a thumbnail, which is visible only to the uploading 
user.
+        *
+        * @return {Status} result
+        */
+       public function performUpload( $comment, $pageText, $watch, $user ) { 
+               $status = new Status();
+               try { 
+                       $stash = new SessionStash();
+                       $data = array( 
+                               'comment' => $comment,
+                               'pageText' => $pageText,
+                               'watch' => $watch,
+                               'mFileProps' => $this->mFileProps 
+                       );
+
+                       // we now change the value of the local file
+                       $this->mLocalFile = $stash->stashFile( false, 
$this->mTempPath, $data );
+                       $status->setResult( true, $this->mLocalFile );
+
+               } catch ( Exception $e ) { 
+                       $status->setResult( false );
+                       $status->error( $e->getMessage );       
+               }
+
+               return $status;
+       }
+
+       /**
+        * Get image info, but also add a thumbnail.  Relies a lot on 
SessionStashFile...
+        * This method also has to pass in an API result, which is a rather bad 
idea and breaks
+        * separation of concerns, but that's how the rest of the code works :(
+        *
+        * @param {ApiResult} result
+        * @return {Array} key-val pair of image info, including thumbnail
+        */
+       public function getImageInfo( $result ) {
+
+               $imageInfo = parent::getImageInfo( $result );
+
+               // XXX get default thumbnail width. 
+               // perhaps when initializing... Isn't this a global? Can't find 
it anywhere in docs.
+               $thumbWidth = 120;
+
+               if ( isset( $this->mParams['thumbWidth'] ) ) {
+                       $thumbWidthParam = ( int )( 
$this->mParams['thumbWidth'] );
+                       if ( $thumbWidthParam > 0 and $thumbWidthParam <= 
$file->getWidth() ) {
+                               $thumbWidth = $thumbWidthParam;
+                       }
+               }
+
+               $thumbnailImage = null;
+
+               // because the file is a SessionStashFile, this thumbnail will 
also be stashed,
+               // and a thumbnailFile will be created
+               if ( ! $thumbnailImage = $this->getLocalFile()->getThumbnail( 
$thumbWidth ) ) { 
+                       $this->dieUsageMsg( 'Could not obtain thumbnail', 
'nothumb' );
+               }
+               
+               $thumbFile = $thumbnailImage->thumbnailFile;
+
+               $imageInfo[ 'thumbnail' ] = $this->getImageInfoForFile( 
$thumbFile, $result );
+
+               return $imageInfo;
+       }
+};


Property changes on: 
branches/uploadwizard/extensions/UploadWizard/UploadFromFileToStash.php
___________________________________________________________________
Added: svn:eol-style
   + native

Modified: branches/uploadwizard/extensions/UploadWizard/UploadWizard.php
===================================================================
--- branches/uploadwizard/extensions/UploadWizard/UploadWizard.php      
2010-09-27 23:58:00 UTC (rev 73842)
+++ branches/uploadwizard/extensions/UploadWizard/UploadWizard.php      
2010-09-27 23:59:21 UTC (rev 73843)
@@ -35,10 +35,15 @@
 $wgExtensionMessagesFiles['UploadWizard'] = $dir . 'UploadWizard.i18n.php';
 $wgExtensionAliasesFiles['UploadWizard'] = $dir . 'UploadWizard.alias.php';
 
+# hooks!
+$wgAutoloadClasses['UploadWizardHooks'] = dirname( __FILE__ ) . 
'/UploadWizard.hooks.php';
+$wgHooks['selectUploadModule'][] = 'UploadWizardHooks::selectUploadModule';
+
 # Require modules, includeing the special page
 $wgAutoloadLocalClasses[ 'SpecialUploadWizard' ] = $dir . 
'SpecialUploadWizard.php';
 $wgAutoloadLocalClasses[ 'SessionStash' ] = $dir . 'SessionStash.php';
 $wgAutoloadLocalClasses[ 'UploadWizardMessages' ] = $dir . 
'UploadWizardMessages.php';
+$wgAutoloadLocalClasses[ 'UploadFromFileToStash' ] = $dir . 
'UploadFromFileToStash.php';
 
 # Let the special page be a special center of unique specialness
 $wgSpecialPages['UploadWizard'] = 'SpecialUploadWizard';

Modified: branches/uploadwizard/phase3/includes/api/ApiUpload.php
===================================================================
--- branches/uploadwizard/phase3/includes/api/ApiUpload.php     2010-09-27 
23:58:00 UTC (rev 73842)
+++ branches/uploadwizard/phase3/includes/api/ApiUpload.php     2010-09-27 
23:59:21 UTC (rev 73843)
@@ -84,104 +84,34 @@
                        $this->dieUsageMsg( array( 'badaccess-groups' ) );
                }
 
+               // Check warnings if necessary
+               $warnings = $this->getApiWarnings();
 
-               //xdebug_start_trace();
-               // die("right before xdebug_break");
-               // xdebug_break();
-
-               if ( $this->mParams['stash'] ) {
-                       wfDebug( "in stash\n" );
-                       $result = array();
-
-                       $warnings = $this->getApiWarnings();
-                       if ( $warnings ) {
-                               // is it really necessary to say 
'result=warning' ?
-                               $result['result'] = 'Warning';
-                               $result['warnings'] = $warnings;
-                       }
-
-                       // this saves the file to a temp area and 
-                       // stores the path in the session
-                       $key = $this->mUpload->stashSession();
-                       if ( !$key ) {
+               if ( $warnings ) {
+                       $sessionKey = $this->mUpload->stashSession();
+                       if ( !$sessionKey ) {
                                $this->dieUsage( 'Stashing temporary file 
failed', 'stashfailed' );
-                       } 
-                       $result['sessionkey'] = $key;
-
-                       try { 
-                               $stash = new SessionStash();
-                               $file = $stash->getFile( $key );
-                       } catch (Exception $e) { 
-                               $this->dieUsage( 'Obtaining stash file failed: 
' . $e->getText(), 'stashfailed' );
                        }
-                       
-                       // return data to the client similar to an imageinfo 
call
-                       $props = array( 
-                               'url' => true, 
-                               'size' => true, 
-                               'sha1' => true, 
-                               'mime' => true, 
-                               'bitdepth' => true,
-                               'metadata' => true 
-                       );
 
-                       $result[ 'imageinfo' ] = ApiQueryImageInfo::getInfo( 
$file, $props, $this->getResult(), null );
-                       // XXX get the rest of the image info including exif 
data and so on
- 
-       
-                       // move all this to the session, so it can look up the 
thumbnail itself, and return opaque url 
-                       // with proper extension.
+                       $result['sessionkey'] = $sessionKey;
 
-                       // thumbnail
-                       // XXX get default thumbnail width. Isn't this a 
global? Can't find in docs.
-                       $thumbWidth = 120;
-                       if ( array_key_exists( 'thumbWidth', $this->mParams ) ) 
{ 
-                               $thumbWidthParam = ( int )( 
$this->mParams['thumbWidth'] );
-                               if ( $thumbWidthParam > 0 and $thumbWidthParam 
<= $file->getWidth() ) {
-                                       $thumbWidth = $thumbWidthParam;
-                               }
-                       }
-                       $thumbnailImage = null;
-                       if ( ! $thumbnailImage = $file->getThumbnail( 
$thumbWidth ) ) { 
-                               $this->dieUsageMsg( 'Could not obtain 
thumbnail', 'nothumb' );
-                       }
-                       
-                       $thumbFile = $thumbnailImage->thumbnailFile;
-                       $result[ 'thumbnailimageinfo' ] = 
ApiQueryImageInfo::getInfo( $thumbFile, $props, $this->getResult(), null );
-
-                       
+                       // is it really necessary to say 'result=warning'
+                       $result['result'] = 'Warning';
+                       $result['warnings'] = $warnings;
                        $this->getResult()->addValue( null, 
$this->getModuleName(), $result );
 
                } else {
-
-                       // Check warnings if necessary
-                       $warnings = $this->getApiWarnings();
-
-                       if ( $warnings ) {
-                               $sessionKey = $this->mUpload->stashSession();
-                               if ( !$sessionKey ) {
-                                       $this->dieUsage( 'Stashing temporary 
file failed', 'stashfailed' );
-                               }
-
-                               $result['sessionkey'] = $sessionKey;
-
-                               // is it really necessary to say 
'result=warning'
-                               $result['result'] = 'Warning';
-                               $result['warnings'] = $warnings;
-                               $this->getResult()->addValue( null, 
$this->getModuleName(), $result );
-
-                       } else {
-                               // Perform the upload
-                               $result = $this->performUpload();
-                               $this->getResult()->addValue( null, 
$this->getModuleName(), $result );
-                       }
-
+                       // Perform the upload
+                       $result = $this->performUpload();
                }
 
+               $this->getResult()->addValue( null, $this->getModuleName(), 
$result );
+               
                // Cleanup any temporary mess
                $this->mUpload->cleanupTempFile();
        }
 
+
        /**
         * Select an upload module and set it to mUpload. Dies on failure. If 
the
         * request was a status request and not a true upload, returns false; 
@@ -211,12 +141,14 @@
                        return false;
                        
                } 
-               
+
+
                // The following modules all require the filename parameter to 
be set
                if ( is_null( $this->mParams['filename'] ) ) {
                        $this->dieUsageMsg( array( 'missingparam', 'filename' ) 
);
                }
-               
+                       
+
                if ( $this->mParams['sessionkey'] ) {
                        // Upload stashed in a previous request
                        $sessionData = $request->getSessionData( 
UploadBase::getSessionKeyName() );
@@ -231,7 +163,11 @@
 
 
                } elseif ( isset( $this->mParams['file'] ) ) {
-                       $this->mUpload = new UploadFromFile();
+                       if ( isset( $this->mParams['stash'] ) and 
$this->mParams['stash'] ) {
+                               $this->mUpload = new UploadFromFileToStash();
+                       } else {
+                               $this->mUpload = new UploadFromFile();
+                       }
                        $this->mUpload->initialize(
                                $this->mParams['filename'],
                                $request->getUpload( 'file' )
@@ -455,7 +391,10 @@
 
                $result['result'] = 'Success';
                $result['filename'] = $file->getName();
+
+               wfDebug( __METHOD__ . " result so far " . print_r( $result, 1 ) 
);
                $result['imageinfo'] = $this->mUpload->getImageInfo( 
$this->getResult() );
+               wfDebug( __METHOD__ . " result with imageinfo " . print_r( 
$result, 1 ) );
 
                return $result;
        }
@@ -495,6 +434,10 @@
                        'url' => null,
 
                        'sessionkey' => null,
+
+                       /* This is for UploadFromFileToStash.php in 
extensions/UploadWizard
+                          We can't add this dynamically?
+                          suggests that we should either define our own method 
or make this all core */
                        'stash' => array(
                                ApiBase::PARAM_DFLT => false,
                        )

Modified: branches/uploadwizard/phase3/includes/upload/UploadBase.php
===================================================================
--- branches/uploadwizard/phase3/includes/upload/UploadBase.php 2010-09-27 
23:58:00 UTC (rev 73842)
+++ branches/uploadwizard/phase3/includes/upload/UploadBase.php 2010-09-27 
23:59:21 UTC (rev 73843)
@@ -1197,8 +1197,29 @@
                return $blacklist;
        }
 
+       /**
+        * Gets image info about the file just uploaded. 
+        *
+        * @param {ApiResult} 
+        * @return {Array} image info
+        */
        public function getImageInfo( $result ) {
-               $file = $this->getLocalFile();
+               return $this->getImageInfoForFile( $this->getLocalFile(), 
$result );
+       }
+
+       /**
+        * Gets image info about any file object (useful for some API functions 
which return information
+        * about several files (such as the original and a thumbnail).
+        *
+        * Also has the effect of setting metadata to be an 'indexed tag name' 
in returned API result if 
+        * 'metadata' was requested. Oddly, we have to pass the "result" object 
down just so it can do that
+        * with the appropriate format, presumably. 
+        *
+        * @param {File} file object
+        * @param {ApiResult} 
+        * @return {Array} image info
+        */
+       protected function getImageInfoForFile( $file, $result ) {
                $imParam = ApiQueryImageInfo::getPropertyNames();
                return ApiQueryImageInfo::getInfo( $file, array_flip( $imParam 
), $result );
        }



_______________________________________________
MediaWiki-CVS mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-cvs

Reply via email to