r82049 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r82048‎ | r82049 | r82050 >
Date:07:30, 13 February 2011
Author:bawolff
Status:ok (Comments)
Tags:
Comment:
(follow-up r81558) Per suggestion, make this use media handler's getParamString/parseParamString

Additionally, rename makeThumbParam back to getScale since that makes more sense now.
Also update the version number used in ForeignAPIRepo user-agent, since this is kind of significant change.
Modified paths:
  • /trunk/phase3/includes/api/ApiQueryImageInfo.php (modified) (history)
  • /trunk/phase3/includes/api/ApiQueryStashImageInfo.php (modified) (history)
  • /trunk/phase3/includes/filerepo/ForeignAPIFile.php (modified) (history)
  • /trunk/phase3/includes/filerepo/ForeignAPIRepo.php (modified) (history)

Diff [purge]

Index: trunk/phase3/includes/filerepo/ForeignAPIRepo.php
@@ -25,7 +25,7 @@
2626 /* This version string is used in the user agent for requests and will help
2727 * server maintainers in identify ForeignAPI usage.
2828 * Update the version every time you make breaking or significant changes. */
29 - const VERSION = "2.0";
 29+ const VERSION = "2.1";
3030
3131 var $fileFactory = array( 'ForeignAPIFile', 'newFromTitle' );
3232 /* Check back with Commons after a day */
@@ -225,7 +225,7 @@
226226 * @param $name String is a dbkey form of a title
227227 * @param $width
228228 * @param $height
229 - * @param String $param Other rendering parameters (page number, etc). | separated.
 229+ * @param String $param Other rendering parameters (page number, etc) from handler's makeParamString.
230230 */
231231 function getThumbUrlFromCache( $name, $width, $height, $params="" ) {
232232 global $wgMemc;
Index: trunk/phase3/includes/filerepo/ForeignAPIFile.php
@@ -77,18 +77,9 @@
7878 return parent::transform( $params, $flags );
7979 }
8080
81 - $otherParams = "";
82 - // Not using implode, since associative array.
83 - foreach ( $params as $name => $value ) {
84 - if ( $name === 'width' || $name === 'height' ) {
85 - continue;
86 - }
87 - $otherParams .= "{$name}={$value}|";
88 - }
89 - // Remove the last |
90 - if ( $otherParams !== "" ) {
91 - $otherParams = substr( $otherParams, 0, -1 );
92 - }
 81+ // Note, the this->canRender() check above implies
 82+ // that we have a handler, and it can do makeParamString.
 83+ $otherParams = $this->handler->makeParamString( $params );
9384
9485 $thumbUrl = $this->repo->getThumbUrlFromCache(
9586 $this->getName(),
Index: trunk/phase3/includes/api/ApiQueryImageInfo.php
@@ -50,7 +50,7 @@
5151
5252 $prop = array_flip( $params['prop'] );
5353
54 - $thumbParams = $this->makeThumbParams( $params );
 54+ $scale = $this->getScale( $params );
5555
5656 $pageIds = $this->getPageSet()->getAllTitlesByNamespace();
5757 if ( !empty( $pageIds[NS_FILE] ) ) {
@@ -108,8 +108,8 @@
109109 break;
110110 }
111111
112 - // Check if we can make the requested thumbnail
113 - $this->validateThumbParams( $img, $thumbParams );
 112+ // Check if we can make the requested thumbnail, and get transform parameters.
 113+ $finalThumbParams = $this->mergeThumbParams( $img, $scale, $params['urlparam'] );
114114
115115 // Get information about the current version first
116116 // Check that the current version is within the start-end boundaries
@@ -122,7 +122,7 @@
123123 $gotOne = true;
124124
125125 $fit = $this->addPageSubItem( $pageId,
126 - self::getInfo( $img, $prop, $result, $thumbParams ) );
 126+ self::getInfo( $img, $prop, $result, $finalThumbParams ) );
127127 if ( !$fit ) {
128128 if ( count( $pageIds[NS_IMAGE] ) == 1 ) {
129129 // See the 'the user is screwed' comment above
@@ -151,7 +151,7 @@
152152 break;
153153 }
154154 $fit = $this->addPageSubItem( $pageId,
155 - self::getInfo( $oldie, $prop, $result, $thumbParams ) );
 155+ self::getInfo( $oldie, $prop, $result, $finalThumbParams ) );
156156 if ( !$fit ) {
157157 if ( count( $pageIds[NS_IMAGE] ) == 1 ) {
158158 $this->setContinueEnumParameter( 'start',
@@ -184,24 +184,10 @@
185185
186186 /**
187187 * From parameters, construct a 'scale' array
188 - * @param $params Array:
 188+ * @param $params Array: Parameters passed to api.
189189 * @return Array or Null: key-val array of 'width' and 'height', or null
190190 */
191191 public function getScale( $params ) {
192 - wfDeprecated( __METHOD__ );
193 - if ( !isset( $params['urlparam'] ) ) {
194 - // In case there are subclasses that
195 - // don't have this param set to anything.
196 - $params['urlparam'] = null;
197 - }
198 - return $this->makeThumbParams( $params );
199 - }
200 -
201 - /* Take parameters for transforming thumbnail, validate and turn into array.
202 - * @param $params Array: Parameters from the request.
203 - * @return Array or null: If array, suitable to passing to $file->transform.
204 - */
205 - public function makeThumbParams( $params ) {
206192 $p = $this->getModulePrefix();
207193
208194 // Height and width.
@@ -221,50 +207,54 @@
222208 return $scale;
223209 }
224210
225 - // Other parameters.
226 - if ( is_array( $params['urlparam'] ) ) {
227 - foreach( $params['urlparam'] as $item ) {
228 - $parameter = explode( '=', $item, 2 );
229 -
230 - if ( count( $parameter ) !== 2
231 - || $parameter[0] === 'width'
232 - || $parameter[0] === 'height'
233 - ) {
234 - $this->dieUsage( "Invalid value for {$p}urlparam ($item)", "urlparam" );
235 - }
236 - $scale[$parameter[0]] = $parameter[1];
237 - }
238 - }
239211 return $scale;
240212 }
241213
242 - /** Validate the thumb parameters, give error if invalid.
 214+ /** Validate and merge scale parameters with handler thumb parameters, give error if invalid.
243215 *
244 - * We do this later than makeThumbParams, since we need the image
 216+ * We do this later than getScale, since we need the image
245217 * to know which handler, since handlers can make their own parameters.
246218 * @param File $image Image that params are for.
247 - * @param Array $thumbParams thumbnail parameters
 219+ * @param Array $thumbParams thumbnail parameters from getScale
 220+ * @param String String of otherParams (iiurlparam).
 221+ * @return Array of parameters for transform.
248222 */
249 - protected function validateThumbParams ( $image, $thumbParams ) {
250 - if ( !$thumbParams ) return;
 223+ protected function mergeThumbParams ( $image, $thumbParams, $otherParams ) {
 224+ if ( !$otherParams ) return $thumbParams;
251225 $p = $this->getModulePrefix();
252226
253227 $h = $image->getHandler();
254228 if ( !$h ) {
255229 $this->setWarning( 'Could not create thumbnail because ' .
256230 $image->getName() . ' does not have an associated image handler' );
257 - return;
 231+ return $thumbParams;
258232 }
259 - foreach ( $thumbParams as $name => $value ) {
 233+
 234+ $paramList = $h->parseParamString( $otherParams );
 235+ if ( !$paramList ) {
 236+ // Just set a warning (instead of dieUsage), as in many cases
 237+ // we could still render the image using width and height parameters,
 238+ // and this type of thing could happen between different versions of
 239+ // handlers.
 240+ $this->setWarning( "Could not parse {$p}urlparam for " . $image->getName()
 241+ . '. Using only width and height' );
 242+ return $thumbParams;
 243+ }
 244+
 245+ if ( isset( $paramList['width'] ) ) {
 246+ if ( $paramList['width'] !== $thumbParams['width'] ) {
 247+ $this->dieUsage( "{$p}urlparam had width of {$paramList['width']} but "
 248+ . "{$p}urlwidth was {$thumbParams['width']}", "urlparam_urlwidth_mismatch" );
 249+ }
 250+ }
 251+
 252+ foreach ( $paramList as $name => $value ) {
260253 if ( !$h->validateParam( $name, $value ) ) {
261 - /* This doesn't work well with height=-1 placeholder */
262 - if ( $name === 'height' ) continue;
263254 $this->dieUsage( "Invalid value for {$p}urlparam ($name=$value)", "urlparam" );
264255 }
265256 }
266 - // This could also potentially check normaliseParams as well, However that seems
267 - // to fall more into a thumbnail rendering error than a user input error, and
268 - // will be checked by the transform functions.
 257+
 258+ return $thumbParams + $paramList;
269259 }
270260
271261 /**
@@ -423,7 +413,8 @@
424414 ApiBase::PARAM_DFLT => -1
425415 ),
426416 'urlparam' => array(
427 - ApiBase::PARAM_ISMULTI => true,
 417+ ApiBase::PARAM_DFLT => '',
 418+ ApiBase::PARAM_TYPE => 'string',
428419 ),
429420 'continue' => null,
430421 );
@@ -490,8 +481,8 @@
491482 'urlwidth' => array( "If {$p}prop=url is set, a URL to an image scaled to this width will be returned.",
492483 'Only the current version of the image can be scaled' ),
493484 'urlheight' => "Similar to {$p}urlwidth. Cannot be used without {$p}urlwidth",
494 - 'urlparam' => array( "Other rending parameters, such as page=2 for multipaged documents.",
495 - "Multiple parameters should be seperated with a |. {$p}urlwidth must also be used"),
 485+ 'urlparam' => array( "A handler specific parameter string. For example, pdf's ",
 486+ "might use 'page15-100px'. {$p}urlwidth must be used and be consistent with {$p}urlparam" ),
496487 'limit' => 'How many image revisions to return',
497488 'start' => 'Timestamp to start listing from',
498489 'end' => 'Timestamp to stop listing at',
@@ -508,7 +499,9 @@
509500 return array_merge( parent::getPossibleErrors(), array(
510501 array( 'code' => 'iiurlwidth', 'info' => 'iiurlheight cannot be used without iiurlwidth' ),
511502 array( 'code' => 'urlparam', 'info' => "Invalid value for {$p}urlparam" ),
512 - array( 'code' => 'urlparam_no_width', 'info' => "iiurlparam requires {$p}urlwidth" ),
 503+ array( 'code' => 'urlparam_no_width', 'info' => "{$p}urlparam requires {$p}urlwidth" ),
 504+ array( 'code' => 'urlparam_urlwidth_mismatch', 'info' => "The width set in {$p}urlparm doesnt't " .
 505+ "match the one in {$p}urlwidth" ),
513506 ) );
514507 }
515508
Index: trunk/phase3/includes/api/ApiQueryStashImageInfo.php
@@ -37,7 +37,7 @@
3838
3939 $prop = array_flip( $params['prop'] );
4040
41 - $scale = $this->makeThumbParams( $params );
 41+ $scale = $this->getScale( $params );
4242
4343 $result = $this->getResult();
4444
@@ -46,8 +46,8 @@
4747
4848 foreach ( $params['sessionkey'] as $sessionkey ) {
4949 $file = $stash->getFile( $sessionkey );
50 - $this->validateThumbParams( $file, $scale );
51 - $imageInfo = self::getInfo( $file, $prop, $result, $scale );
 50+ $finalThumbParam = $this->mergeThumbParams( $file, $scale, $params['urlparam'] );
 51+ $imageInfo = self::getInfo( $file, $prop, $result, $finalThumbParam );
5252 $result->addValue( array( 'query', $this->getModuleName() ), null, $imageInfo );
5353 $result->setIndexedTagName_internal( array( 'query', $this->getModuleName() ), $modulePrefix );
5454 }
@@ -100,7 +100,8 @@
101101 ApiBase::PARAM_DFLT => -1
102102 ),
103103 'urlparam' => array(
104 - ApiBase::PARAM_ISMULTI => true,
 104+ ApiBase::PARAM_TYPE => 'string',
 105+ ApiBase::PARAM_DFLT => '',
105106 ),
106107 );
107108 }
@@ -127,8 +128,8 @@
128129 'sessionkey' => 'Session key that identifies a previous upload that was stashed temporarily.',
129130 'urlwidth' => "If {$p}prop=url is set, a URL to an image scaled to this width will be returned.",
130131 'urlheight' => "Similar to {$p}urlwidth. Cannot be used without {$p}urlwidth",
131 - 'urlparam' => array( "Other rending parameters, such as page=2 for multipaged documents.",
132 - "Multiple parameters should be seperated with a |. {$p}urlwidth must also be used" ),
 132+ 'urlparam' => array( "A handler specific parameter string. For example, pdf's ",
 133+ "might use 'page15-100px'. {$p}urlwidth must be used and be consistent with {$p}urlparam" ),
133134 );
134135 }
135136

Follow-up revisions

RevisionCommit summaryAuthorDate
r82075Follow-up r82049: Fix strict comparison because MediaHandler::parseParamStrin...btongminh20:38, 13 February 2011

Past revisions this follows-up on

RevisionCommit summaryAuthorDate
r81558(bug 26548) Make multi-paged documents (PDFs) work with ForeignAPIRepo (aka I...bawolff08:49, 5 February 2011

Comments

#Comment by Bryan (talk | contribs)   20:40, 13 February 2011

Looks ok, good work.

A problem is that you need to have the same image handlers installed as the remote wiki to parse the param string, but I don't see a way around this. This was the case anyway before.

Status & tagging log