Permalink
Browse files

core changes for UploadWizard (merged from r73549 to HEAD in branches…

…/uploadwizard/phase3)

git-svn-id: file:///home/git-svn-mirror/pushmi-svn-mirror/trunk@75906 dd0e9695-b195-4be7-bd10-2dea1a65a6b6
  • Loading branch information...
neilk
neilk committed Nov 3, 2010
1 parent 493bcc3 commit ef889f719b1337a17ec99c7ee429e62c4e0bae83
@@ -634,6 +634,7 @@
'SpecialRecentChanges' => 'includes/specials/SpecialRecentchanges.php',
'SpecialRecentchangeslinked' => 'includes/specials/SpecialRecentchangeslinked.php',
'SpecialSearch' => 'includes/specials/SpecialSearch.php',
+ 'SpecialUploadStash' => 'includes/specials/SpecialUploadStash.php',
'SpecialSpecialpages' => 'includes/specials/SpecialSpecialpages.php',
'SpecialStatistics' => 'includes/specials/SpecialStatistics.php',
'SpecialTags' => 'includes/specials/SpecialTags.php',
@@ -669,6 +670,7 @@
'UserloginTemplate' => 'includes/templates/Userlogin.php',
# includes/upload
+ 'UploadStash' => 'includes/upload/UploadStash.php',
'UploadBase' => 'includes/upload/UploadBase.php',
'UploadFromStash' => 'includes/upload/UploadFromStash.php',
'UploadFromFile' => 'includes/upload/UploadFromFile.php',
@@ -149,6 +149,7 @@ class SpecialPage {
'MIMEsearch' => array( 'SpecialPage', 'MIMEsearch' ),
'FileDuplicateSearch' => array( 'SpecialPage', 'FileDuplicateSearch' ),
'Upload' => 'SpecialUpload',
+ 'UploadStash' => 'SpecialUploadStash',
# Wiki data and tools
'Statistics' => 'SpecialStatistics',
@@ -36,26 +36,21 @@
*/
class ApiQueryImageInfo extends ApiQueryBase {
- public function __construct( $query, $moduleName ) {
- parent::__construct( $query, $moduleName, 'ii' );
+ public function __construct( $query, $moduleName, $prefix = 'ii' ) {
+ // We allow a subclass to override the prefix, to create a related API module.
+ // Some other parts of MediaWiki construct this with a null $prefix, which used to be ignored when this only took two arguments
+ if ( is_null( $prefix ) ) {
+ $prefix = 'ii';
+ }
+ parent::__construct( $query, $moduleName, $prefix );
}
public function execute() {
$params = $this->extractRequestParams();
$prop = array_flip( $params['prop'] );
- if ( $params['urlheight'] != - 1 && $params['urlwidth'] == - 1 ) {
- $this->dieUsage( 'iiurlheight cannot be used without iiurlwidth', 'iiurlwidth' );
- }
-
- if ( $params['urlwidth'] != - 1 ) {
- $scale = array();
- $scale['width'] = $params['urlwidth'];
- $scale['height'] = $params['urlheight'];
- } else {
- $scale = null;
- }
+ $scale = $this->getScale( $params );
$pageIds = $this->getPageSet()->getAllTitlesByNamespace();
if ( !empty( $pageIds[NS_FILE] ) ) {
@@ -183,6 +178,28 @@ public function execute() {
}
}
+ /**
+ * From parameters, construct a 'scale' array
+ * @param {Array} $params
+ * @return {null|Array} key-val array of 'width' and 'height', or null
+ */
+ public function getScale( $params ) {
+ $p = $this->getModulePrefix();
+ if ( $params['urlheight'] != -1 && $params['urlwidth'] == -1 ) {
+ $this->dieUsage( "${p}urlheight cannot be used without {$p}urlwidth", "{$p}urlwidth" );
+ }
+
+ if ( $params['urlwidth'] != -1 ) {
+ $scale = array();
+ $scale['width'] = $params['urlwidth'];
+ $scale['height'] = $params['urlheight'];
+ } else {
+ $scale = null;
+ }
+ return $scale;
+ }
+
+
/**
* Get result information for an image revision
*
@@ -324,11 +341,11 @@ public function getAllowedParams() {
),
'urlwidth' => array(
ApiBase::PARAM_TYPE => 'integer',
- ApiBase::PARAM_DFLT => - 1
+ ApiBase::PARAM_DFLT => -1
),
'urlheight' => array(
ApiBase::PARAM_TYPE => 'integer',
- ApiBase::PARAM_DFLT => - 1
+ ApiBase::PARAM_DFLT => -1
),
'continue' => null,
);
@@ -356,6 +373,11 @@ public static function getPropertyNames() {
);
}
+
+ /**
+ * Return the API documentation for the parameters.
+ * @return {Array} parameter documentation.
+ */
public function getParamDescription() {
$p = $this->getModulePrefix();
return array(
@@ -375,14 +397,14 @@ public function getParamDescription() {
' metadata - Lists EXIF metadata for the version of the image',
' archivename - Adds the file name of the archive version for non-latest versions',
' bitdepth - Adds the bit depth of the version',
- ),
- 'limit' => 'How many image revisions to return',
- 'start' => 'Timestamp to start listing from',
- 'end' => 'Timestamp to stop listing at',
+ ),
'urlwidth' => array( "If {$p}prop=url is set, a URL to an image scaled to this width will be returned.",
'Only the current version of the image can be scaled' ),
'urlheight' => "Similar to {$p}urlwidth. Cannot be used without {$p}urlwidth",
- 'continue' => 'When more results are available, use this to continue',
+ 'limit' => 'How many image revisions to return',
+ 'start' => 'Timestamp to start listing from',
+ 'end' => 'Timestamp to stop listing at',
+ 'continue' => 'If the query response includes a continue value, use it here to get another page of results'
);
}
@@ -80,24 +80,65 @@ public function execute() {
// Check permission to upload this file
$permErrors = $this->mUpload->verifyPermissions( $wgUser );
if ( $permErrors !== true ) {
- // Todo: stash the upload and allow choosing a new name
+ // TODO: stash the upload and allow choosing a new name
$this->dieUsageMsg( array( 'badaccess-groups' ) );
}
- // Check warnings if necessary
- $warnings = $this->checkForWarnings();
- if ( $warnings ) {
- $this->getResult()->addValue( null, $this->getModuleName(), $warnings );
+ // Prepare the API result
+ $result = array();
+
+ $warnings = $this->getApiWarnings();
+ if ( $warnings ) {
+ $result['result'] = 'Warning';
+ $result['warnings'] = $warnings;
+ // in case the warnings can be fixed with some further user action, let's stash this upload
+ // and return a key they can use to restart it
+ try {
+ $result['sessionkey'] = $this->performStash();
+ } catch ( MWException $e ) {
+ $result['warnings']['stashfailed'] = $e->getMessage();
+ }
+ } elseif ( $this->mParams['stash'] ) {
+ // Some uploads can request they be stashed, so as not to publish them immediately.
+ // In this case, a failure to stash ought to be fatal
+ try {
+ $result['result'] = 'Success';
+ $result['sessionkey'] = $this->performStash();
+ } catch ( MWException $e ) {
+ $this->dieUsage( $e->getMessage(), 'stashfailed' );
+ }
} else {
- // Perform the upload
+ // This is the most common case -- a normal upload with no warnings
+ // $result will be formatted properly for the API already, with a status
$result = $this->performUpload();
- $this->getResult()->addValue( null, $this->getModuleName(), $result );
}
+ if ( $result['result'] === 'Success' ) {
+ $result['imageinfo'] = $this->mUpload->getImageInfo( $this->getResult() );
+ }
+
+ $this->getResult()->addValue( null, $this->getModuleName(), $result );
+
// Cleanup any temporary mess
$this->mUpload->cleanupTempFile();
}
+ /**
+ * Stash the file and return the session key
+ * Also re-raises exceptions with slightly more informative message strings (useful for API)
+ * @throws MWException
+ * @return {String} session key
+ */
+ function performStash() {
+ try {
+ $sessionKey = $this->mUpload->stashSessionFile()->getSessionKey();
+ } catch ( MWException $e ) {
+ throw new MWException( 'Stashing temporary file failed: ' . get_class($e) . ' ' . $e->getMessage() );
+ }
+ return $sessionKey;
+ }
+
+
/**
* 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;
@@ -106,13 +147,14 @@ public function execute() {
* @return bool
*/
protected function selectUploadModule() {
+ global $wgAllowAsyncCopyUploads;
$request = $this->getMain()->getRequest();
// One and only one of the following parameters is needed
$this->requireOnlyOneParameter( $this->mParams,
'sessionkey', 'file', 'url', 'statuskey' );
- if ( isset( $this->mParams['statuskey'] ) ) {
+ if ( $wgAllowAsyncCopyUploads && $this->mParams['statuskey'] ) {
// Status request for an async upload
$sessionData = UploadFromUrlJob::getSessionData( $this->mParams['statuskey'] );
if ( !isset( $sessionData['result'] ) ) {
@@ -126,12 +168,14 @@ protected function selectUploadModule() {
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() );
@@ -249,56 +293,41 @@ protected function verifyUpload( ) {
}
}
+
/**
* Check warnings if ignorewarnings is not set.
- * Returns a suitable result array if there were warnings
+ * Returns a suitable array for inclusion into API results if there were warnings
+ * Returns the empty array if there were no warnings
+ *
+ * @return array
*/
- protected function checkForWarnings() {
- $result = array();
+ protected function getApiWarnings() {
+ $warnings = array();
if ( !$this->mParams['ignorewarnings'] ) {
$warnings = $this->mUpload->checkWarnings();
if ( $warnings ) {
- $result['result'] = 'Warning';
- $result['warnings'] = $this->transformWarnings( $warnings );
-
- $sessionKey = $this->mUpload->stashSession();
- if ( !$sessionKey ) {
- $this->dieUsage( 'Stashing temporary file failed', 'stashfailed' );
+ // Add indices
+ $this->getResult()->setIndexedTagName( $warnings, 'warning' );
+
+ if ( isset( $warnings['duplicate'] ) ) {
+ $dupes = array();
+ foreach ( $warnings['duplicate'] as $dupe ) {
+ $dupes[] = $dupe->getName();
+ }
+ $this->getResult()->setIndexedTagName( $dupes, 'duplicate' );
+ $warnings['duplicate'] = $dupes;
}
- $result['sessionkey'] = $sessionKey;
-
- return $result;
- }
- }
- return;
- }
-
- /**
- * Transforms a warnings array returned by mUpload->checkWarnings() into
- * something that can be directly used as API result
- */
- protected function transformWarnings( $warnings ) {
- // Add indices
- $this->getResult()->setIndexedTagName( $warnings, 'warning' );
-
- if ( isset( $warnings['duplicate'] ) ) {
- $dupes = array();
- foreach ( $warnings['duplicate'] as $dupe ) {
- $dupes[] = $dupe->getName();
+ if ( isset( $warnings['exists'] ) ) {
+ $warning = $warnings['exists'];
+ unset( $warnings['exists'] );
+ $warnings[$warning['warning']] = $warning['file']->getName();
+ }
}
- $this->getResult()->setIndexedTagName( $dupes, 'duplicate' );
- $warnings['duplicate'] = $dupes;
}
- if ( isset( $warnings['exists'] ) ) {
- $warning = $warnings['exists'];
- unset( $warnings['exists'] );
- $warnings[$warning['warning']] = $warning['file']->getName();
- }
-
- return $warnings;
+ return $warnings;
}
/**
@@ -346,7 +375,7 @@ protected function performUpload() {
$result['result'] = 'Success';
$result['filename'] = $file->getName();
- $result['imageinfo'] = $this->mUpload->getImageInfo( $this->getResult() );
+
return $result;
}
@@ -384,8 +413,8 @@ public function getAllowedParams() {
'ignorewarnings' => false,
'file' => null,
'url' => null,
-
'sessionkey' => null,
+ 'stash' => false,
);
global $wgAllowAsyncCopyUploads;
@@ -410,7 +439,8 @@ public function getParamDescription() {
'ignorewarnings' => 'Ignore any warnings',
'file' => 'File contents',
'url' => 'Url to fetch the file from',
- 'sessionkey' => 'Session key returned by a previous upload that failed due to warnings',
+ 'sessionkey' => 'Session key that identifies a previous upload that was stashed temporarily.',
+ 'stash' => 'If set, the server will not add the file to the repository and stash it temporarily.'
);
global $wgAllowAsyncCopyUploads;
@@ -541,7 +541,7 @@ public function getThumbnail( $width, $height=-1, $render = true ) {
* @param $params Array: an associative array of handler-specific parameters.
* Typical keys are width, height and page.
* @param $flags Integer: a bitfield, may contain self::RENDER_NOW to force rendering
- * @return MediaTransformOutput
+ * @return MediaTransformOutput | false
*/
function transform( $params, $flags = 0 ) {
global $wgUseSquid, $wgIgnoreImageErrors, $wgThumbnailEpoch, $wgServer;
@@ -575,7 +575,7 @@ function transform( $params, $flags = 0 ) {
$thumbPath = $this->getThumbPath( $thumbName );
$thumbUrl = $this->getThumbUrl( $thumbName );
- if ( $this->repo->canTransformVia404() && !($flags & self::RENDER_NOW ) ) {
+ if ( $this->repo && $this->repo->canTransformVia404() && !($flags & self::RENDER_NOW ) ) {
$thumb = $this->handler->getTransform( $this, $thumbPath, $thumbUrl, $params );
break;
}
Oops, something went wrong.

0 comments on commit ef889f7

Please sign in to comment.