r76782 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r76781‎ | r76782 | r76783 >
Date:06:57, 16 November 2010
Author:neilk
Status:ok
Tags:
Comment:
Fixed bug#25784 (thumbnails of stashed files had wrong description URLs).

This fixes the more general problem that the imageinfo returned with stashed uploads was inaccurate, since it was relying on
code that only worked with non-stashed files.

So, I had to:
- move the ApiQueryStashImageInfo module into core. Which others had asked for anyway, and was anticipated sometime later.
- add lines to AutoLoader and ApiQuery to accomodate the new module

- add an ugly if/then to UploadBase -- based on the type of uploaded file, it will use a different API module to simulate a getImageInfo call.
I left a TODO that this situation wasn't ideal, but the way things are now, imageInfo is constructed by the API modules, when it should probably
really be the File modules. Then the API can wrap that info into various formats.

- add a few new lines to the tests to check imageinfo information in both regular and stashed upload files
Modified paths:
  • /trunk/extensions/UploadWizard/ApiQueryStashImageInfo.php (deleted) (history)
  • /trunk/extensions/UploadWizard/UploadWizard.php (modified) (history)
  • /trunk/phase3/includes/AutoLoader.php (modified) (history)
  • /trunk/phase3/includes/api/ApiQuery.php (modified) (history)
  • /trunk/phase3/includes/api/ApiQueryStashImageInfo.php (added) (history)
  • /trunk/phase3/includes/upload/UploadBase.php (modified) (history)
  • /trunk/phase3/maintenance/tests/phpunit/includes/api/ApiUploadTest.php (modified) (history)

Diff [purge]

Index: trunk/phase3/maintenance/tests/phpunit/includes/api/ApiUploadTest.php
@@ -261,6 +261,7 @@
262262
263263 $filePaths = $randomImageGenerator->writeImages( 1, $extension, dirname( wfTempDir() ) );
264264 $filePath = $filePaths[0];
 265+ $fileSize = filesize( $filePath );
265266 $fileName = basename( $filePath );
266267
267268 $this->deleteFileByFileName( $fileName );
@@ -286,6 +287,8 @@
287288 }
288289 $this->assertTrue( isset( $result['upload'] ) );
289290 $this->assertEquals( 'Success', $result['upload']['result'] );
 291+ $this->assertEquals( $fileSize, ( int )$result['upload']['imageinfo']['size'] );
 292+ $this->assertEquals( $mimeType, $result['upload']['imageinfo']['mime'] );
290293 $this->assertFalse( $exception );
291294
292295 // clean up
@@ -511,6 +514,7 @@
512515
513516 $filePaths = $randomImageGenerator->writeImages( 1, $extension, dirname( wfTempDir() ) );
514517 $filePath = $filePaths[0];
 518+ $fileSize = filesize( $filePath );
515519 $fileName = basename( $filePath );
516520
517521 $this->deleteFileByFileName( $fileName );
@@ -538,6 +542,8 @@
539543 $this->assertFalse( $exception );
540544 $this->assertTrue( isset( $result['upload'] ) );
541545 $this->assertEquals( 'Success', $result['upload']['result'] );
 546+ $this->assertEquals( $fileSize, ( int )$result['upload']['imageinfo']['size'] );
 547+ $this->assertEquals( $mimeType, $result['upload']['imageinfo']['mime'] );
542548 $this->assertTrue( isset( $result['upload']['sessionkey'] ) );
543549 $sessionkey = $result['upload']['sessionkey'];
544550
Index: trunk/phase3/includes/upload/UploadBase.php
@@ -505,10 +505,15 @@
506506 * @return mixed Status indicating the whether the upload succeeded.
507507 */
508508 public function performUpload( $comment, $pageText, $watch, $user ) {
509 - wfDebug( "\n\n\performUpload: sum: " . $comment . ' c: ' . $pageText .
510 - ' w: ' . $watch );
511 - $status = $this->getLocalFile()->upload( $this->mTempPath, $comment, $pageText,
512 - File::DELETE_SOURCE, $this->mFileProps, false, $user );
 509+ $status = $this->getLocalFile()->upload(
 510+ $this->mTempPath,
 511+ $comment,
 512+ $pageText,
 513+ File::DELETE_SOURCE, i
 514+ $this->mFileProps,
 515+ false,
 516+ $user
 517+ );
513518
514519 if( $status->isGood() ) {
515520 if ( $watch ) {
@@ -636,8 +641,7 @@
637642 'mFileProps' => $this->mFileProps
638643 );
639644 $file = $stash->stashFile( $this->mTempPath, $data, $key );
640 - // TODO should we change the "local file" here?
641 - // $this->mLocalFile = $file;
 645+ $this->mLocalFile = $file;
642646 return $file;
643647 }
644648
@@ -1207,8 +1211,16 @@
12081212 */
12091213 public function getImageInfo( $result ) {
12101214 $file = $this->getLocalFile();
1211 - $imParam = ApiQueryImageInfo::getPropertyNames();
1212 - return ApiQueryImageInfo::getInfo( $file, array_flip( $imParam ), $result );
 1215+ // TODO This cries out for refactoring. We really want to say $file->getAllInfo(); here.
 1216+ // Perhaps "info" methods should be moved into files, and the API should just wrap them in queries.
 1217+ if ( is_a( $file, 'UploadStashFile' ) ) {
 1218+ $imParam = ApiQueryStashImageInfo::getPropertyNames();
 1219+ $info = ApiQueryStashImageInfo::getInfo( $file, array_flip( $imParam ), $result );
 1220+ } else {
 1221+ $imParam = ApiQueryImageInfo::getPropertyNames();
 1222+ $info = ApiQueryImageInfo::getInfo( $file, array_flip( $imParam ), $result );
 1223+ }
 1224+ return $info;
12131225 }
12141226
12151227
Index: trunk/phase3/includes/api/ApiQuery.php
@@ -54,6 +54,7 @@
5555 'langlinks' => 'ApiQueryLangLinks',
5656 'images' => 'ApiQueryImages',
5757 'imageinfo' => 'ApiQueryImageInfo',
 58+ 'stashimageinfo' => 'ApiQueryStashImageInfo',
5859 'templates' => 'ApiQueryLinks',
5960 'categories' => 'ApiQueryCategories',
6061 'extlinks' => 'ApiQueryExternalLinks',
Index: trunk/phase3/includes/api/ApiQueryStashImageInfo.php
@@ -0,0 +1,152 @@
 2+<?php
 3+/**
 4+ * API for MediaWiki 1.16+
 5+ *
 6+ * This program is free software; you can redistribute it and/or modify
 7+ * it under the terms of the GNU General Public License as published by
 8+ * the Free Software Foundation; either version 2 of the License, or
 9+ * (at your option) any later version.
 10+ *
 11+ * This program is distributed in the hope that it will be useful,
 12+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
 13+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 14+ * GNU General Public License for more details.
 15+ *
 16+ * You should have received a copy of the GNU General Public License along
 17+ * with this program; if not, write to the Free Software Foundation, Inc.,
 18+ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 19+ * http://www.gnu.org/copyleft/gpl.html
 20+ *
 21+ * @file
 22+ */
 23+
 24+/**
 25+ * A query action to get image information from temporarily stashed files.
 26+ *
 27+ * @ingroup API
 28+ */
 29+class ApiQueryStashImageInfo extends ApiQueryImageInfo {
 30+
 31+ public function __construct( $query, $moduleName ) {
 32+ parent::__construct( $query, $moduleName, 'sii' );
 33+ }
 34+
 35+ public function execute() {
 36+ $params = $this->extractRequestParams();
 37+ $modulePrefix = $this->getModulePrefix();
 38+
 39+ $prop = array_flip( $params['prop'] );
 40+
 41+ $scale = $this->getScale( $params );
 42+
 43+ $result = $this->getResult();
 44+
 45+ try {
 46+ $stash = new UploadStash();
 47+
 48+ foreach ( $params['sessionkey'] as $sessionkey ) {
 49+ $file = $stash->getFile( $sessionkey );
 50+ $imageInfo = self::getInfo( $file, $prop, $result, $scale );
 51+ $result->addValue( array( 'query', $this->getModuleName() ), null, $imageInfo );
 52+ $result->setIndexedTagName_internal( array( 'query', $this->getModuleName() ), $modulePrefix );
 53+ }
 54+
 55+ } catch ( UploadStashNotAvailableException $e ) {
 56+ $this->dieUsage( "Session not available: " . $e->getMessage(), "nosession" );
 57+ } catch ( UploadStashFileNotFoundException $e ) {
 58+ $this->dieUsage( "File not found: " . $e->getMessage(), "invalidsessiondata" );
 59+ } catch ( UploadStashBadPathException $e ) {
 60+ $this->dieUsage( "Bad path: " . $e->getMessage(), "invalidsessiondata" );
 61+ }
 62+
 63+ }
 64+
 65+ /**
 66+ * Returns all valid parameters to siiprop
 67+ */
 68+ public static function getPropertyNames() {
 69+ return array(
 70+ 'timestamp',
 71+ 'url',
 72+ 'size',
 73+ 'dimensions', // For backwards compatibility with Allimages
 74+ 'sha1',
 75+ 'mime',
 76+ 'thumbmime',
 77+ 'metadata',
 78+ 'bitdepth',
 79+ );
 80+ }
 81+
 82+
 83+ public function getAllowedParams() {
 84+ return array(
 85+ 'sessionkey' => array(
 86+ ApiBase::PARAM_ISMULTI => true,
 87+ ApiBase::PARAM_REQUIRED => true,
 88+ ApiBase::PARAM_DFLT => null
 89+ ),
 90+ 'prop' => array(
 91+ ApiBase::PARAM_ISMULTI => true,
 92+ ApiBase::PARAM_DFLT => 'timestamp|url',
 93+ ApiBase::PARAM_TYPE => self::getPropertyNames()
 94+ ),
 95+ 'urlwidth' => array(
 96+ ApiBase::PARAM_TYPE => 'integer',
 97+ ApiBase::PARAM_DFLT => -1
 98+ ),
 99+ 'urlheight' => array(
 100+ ApiBase::PARAM_TYPE => 'integer',
 101+ ApiBase::PARAM_DFLT => -1
 102+ )
 103+ );
 104+ }
 105+
 106+ /**
 107+ * Return the API documentation for the parameters.
 108+ * @return {Array} parameter documentation.
 109+ */
 110+ public function getParamDescription() {
 111+ $p = $this->getModulePrefix();
 112+ return array(
 113+ 'prop' => array(
 114+ 'What image information to get:',
 115+ ' timestamp - Adds timestamp for the uploaded version',
 116+ ' url - Gives URL to the image and the description page',
 117+ ' size - Adds the size of the image in bytes and the height and width',
 118+ ' dimensions - Alias for size',
 119+ ' sha1 - Adds sha1 hash for the image',
 120+ ' mime - Adds MIME of the image',
 121+ ' thumbmime - Adss MIME of the image thumbnail (requires url)',
 122+ ' metadata - Lists EXIF metadata for the version of the image',
 123+ ' bitdepth - Adds the bit depth of the version',
 124+ ),
 125+ 'sessionkey' => 'Session key that identifies a previous upload that was stashed temporarily.',
 126+ 'urlwidth' => "If {$p}prop=url is set, a URL to an image scaled to this width will be returned.",
 127+ 'urlheight' => "Similar to {$p}urlwidth. Cannot be used without {$p}urlwidth"
 128+ );
 129+ }
 130+
 131+ public function getDescription() {
 132+ return 'Returns image information for stashed images';
 133+ }
 134+
 135+ public function getPossibleErrors() {
 136+ return array_merge( parent::getPossibleErrors(), array(
 137+ array( 'code' => 'siiurlwidth', 'info' => 'siiurlheight cannot be used without iiurlwidth' ),
 138+ ) );
 139+ }
 140+
 141+ protected function getExamples() {
 142+ return array(
 143+ 'api.php?action=query&prop=stashimageinfo&siisessionkey=124sd34rsdf567',
 144+ 'api.php?action=query&prop=stashimageinfo&siisessionkey=b34edoe3|bceffd4&siiurlwidth=120&siiprop=url',
 145+ );
 146+ }
 147+
 148+ public function getVersion() {
 149+ return __CLASS__ . ': $Id$';
 150+ }
 151+
 152+}
 153+
Property changes on: trunk/phase3/includes/api/ApiQueryStashImageInfo.php
___________________________________________________________________
Added: svn:eol-style
1154 + native
Added: svn:keywords
2155 + Id
Index: trunk/phase3/includes/AutoLoader.php
@@ -336,6 +336,7 @@
337337 'ApiQueryRevisions' => 'includes/api/ApiQueryRevisions.php',
338338 'ApiQuerySearch' => 'includes/api/ApiQuerySearch.php',
339339 'ApiQuerySiteinfo' => 'includes/api/ApiQuerySiteinfo.php',
 340+ 'ApiQueryStashImageInfo' => 'includes/api/ApiQueryStashImageInfo.php',
340341 'ApiQueryTags' => 'includes/api/ApiQueryTags.php',
341342 'ApiQueryUserInfo' => 'includes/api/ApiQueryUserInfo.php',
342343 'ApiQueryUsers' => 'includes/api/ApiQueryUsers.php',
Index: trunk/extensions/UploadWizard/ApiQueryStashImageInfo.php
@@ -1,152 +0,0 @@
2 -<?php
3 -/**
4 - * API for MediaWiki 1.16+
5 - *
6 - * This program is free software; you can redistribute it and/or modify
7 - * it under the terms of the GNU General Public License as published by
8 - * the Free Software Foundation; either version 2 of the License, or
9 - * (at your option) any later version.
10 - *
11 - * This program is distributed in the hope that it will be useful,
12 - * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 - * GNU General Public License for more details.
15 - *
16 - * You should have received a copy of the GNU General Public License along
17 - * with this program; if not, write to the Free Software Foundation, Inc.,
18 - * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19 - * http://www.gnu.org/copyleft/gpl.html
20 - *
21 - * @file
22 - */
23 -
24 -/**
25 - * A query action to get image information from temporarily stashed files.
26 - *
27 - * @ingroup API
28 - */
29 -class ApiQueryStashImageInfo extends ApiQueryImageInfo {
30 -
31 - public function __construct( $query, $moduleName ) {
32 - parent::__construct( $query, $moduleName, 'sii' );
33 - }
34 -
35 - public function execute() {
36 - $params = $this->extractRequestParams();
37 - $modulePrefix = $this->getModulePrefix();
38 -
39 - $prop = array_flip( $params['prop'] );
40 -
41 - $scale = $this->getScale( $params );
42 -
43 - $result = $this->getResult();
44 -
45 - try {
46 - $stash = new UploadStash();
47 -
48 - foreach ( $params['sessionkey'] as $sessionkey ) {
49 - $file = $stash->getFile( $sessionkey );
50 - $imageInfo = self::getInfo( $file, $prop, $result, $scale );
51 - $result->addValue( array( 'query', $this->getModuleName() ), null, $imageInfo );
52 - $result->setIndexedTagName_internal( array( 'query', $this->getModuleName() ), $modulePrefix );
53 - }
54 -
55 - } catch ( UploadStashNotAvailableException $e ) {
56 - $this->dieUsage( "Session not available: " . $e->getMessage(), "nosession" );
57 - } catch ( UploadStashFileNotFoundException $e ) {
58 - $this->dieUsage( "File not found: " . $e->getMessage(), "invalidsessiondata" );
59 - } catch ( UploadStashBadPathException $e ) {
60 - $this->dieUsage( "Bad path: " . $e->getMessage(), "invalidsessiondata" );
61 - }
62 -
63 - }
64 -
65 - /**
66 - * Returns all valid parameters to siiprop
67 - */
68 - public static function getPropertyNames() {
69 - return array(
70 - 'timestamp',
71 - 'url',
72 - 'size',
73 - 'dimensions', // For backwards compatibility with Allimages
74 - 'sha1',
75 - 'mime',
76 - 'thumbmime',
77 - 'metadata',
78 - 'bitdepth',
79 - );
80 - }
81 -
82 -
83 - public function getAllowedParams() {
84 - return array(
85 - 'sessionkey' => array(
86 - ApiBase::PARAM_ISMULTI => true,
87 - ApiBase::PARAM_REQUIRED => true,
88 - ApiBase::PARAM_DFLT => null
89 - ),
90 - 'prop' => array(
91 - ApiBase::PARAM_ISMULTI => true,
92 - ApiBase::PARAM_DFLT => 'timestamp|url',
93 - ApiBase::PARAM_TYPE => self::getPropertyNames()
94 - ),
95 - 'urlwidth' => array(
96 - ApiBase::PARAM_TYPE => 'integer',
97 - ApiBase::PARAM_DFLT => -1
98 - ),
99 - 'urlheight' => array(
100 - ApiBase::PARAM_TYPE => 'integer',
101 - ApiBase::PARAM_DFLT => -1
102 - )
103 - );
104 - }
105 -
106 - /**
107 - * Return the API documentation for the parameters.
108 - * @return {Array} parameter documentation.
109 - */
110 - public function getParamDescription() {
111 - $p = $this->getModulePrefix();
112 - return array(
113 - 'prop' => array(
114 - 'What image information to get:',
115 - ' timestamp - Adds timestamp for the uploaded version',
116 - ' url - Gives URL to the image and the description page',
117 - ' size - Adds the size of the image in bytes and the height and width',
118 - ' dimensions - Alias for size',
119 - ' sha1 - Adds sha1 hash for the image',
120 - ' mime - Adds MIME of the image',
121 - ' thumbmime - Adss MIME of the image thumbnail (requires url)',
122 - ' metadata - Lists EXIF metadata for the version of the image',
123 - ' bitdepth - Adds the bit depth of the version',
124 - ),
125 - 'sessionkey' => 'Session key that identifies a previous upload that was stashed temporarily.',
126 - 'urlwidth' => "If {$p}prop=url is set, a URL to an image scaled to this width will be returned.",
127 - 'urlheight' => "Similar to {$p}urlwidth. Cannot be used without {$p}urlwidth"
128 - );
129 - }
130 -
131 - public function getDescription() {
132 - return 'Returns image information for stashed images';
133 - }
134 -
135 - public function getPossibleErrors() {
136 - return array_merge( parent::getPossibleErrors(), array(
137 - array( 'code' => 'siiurlwidth', 'info' => 'siiurlheight cannot be used without iiurlwidth' ),
138 - ) );
139 - }
140 -
141 - protected function getExamples() {
142 - return array(
143 - 'api.php?action=query&prop=stashimageinfo&siisessionkey=124sd34rsdf567',
144 - 'api.php?action=query&prop=stashimageinfo&siisessionkey=b34edoe3|bceffd4&siiurlwidth=120&siiprop=url',
145 - );
146 - }
147 -
148 - public function getVersion() {
149 - return __CLASS__ . ': $Id$';
150 - }
151 -
152 -}
153 -
Index: trunk/extensions/UploadWizard/UploadWizard.php
@@ -38,13 +38,12 @@
3939 # Require modules, includeing the special page
4040 foreach ( array( 'SpecialUploadWizard',
4141 'UploadWizardMessages',
42 - 'ApiQueryStashImageInfo',
4342 'UploadWizardHooks',
4443 'UploadWizardTutorial',
4544 'UploadWizardDependencyLoader' ) as $module ) {
4645 $wgAutoloadLocalClasses[$module] = $dir . "/" . $module . ".php";
4746 }
48 -$wgAPIPropModules['stashimageinfo'] = 'ApiQueryStashImageInfo';
 47+
4948
5049 # Let the special page be a special center of unique specialness
5150 $wgSpecialPages['UploadWizard'] = 'SpecialUploadWizard';

Follow-up revisions

RevisionCommit summaryAuthorDate
r76796Followup r76782: is_a() -> instanceof (we dropped PHP 4 support over 3 years ...catrope14:23, 16 November 2010
r76797uploadwizard-deployment: Merge recent revs from trunk: r75995, r76354, r76386...catrope14:47, 16 November 2010

Status & tagging log