Limit ApiQueryImageInfo transform calls
authorBrad Jorsch <bjorsch@wikimedia.org>
Fri, 1 Feb 2013 23:14:09 +0000 (18:14 -0500)
committerBrad Jorsch <bjorsch@wikimedia.org>
Wed, 20 Feb 2013 18:51:16 +0000 (13:51 -0500)
Apparently calls to File::transform can be slow. Add code to limit these
calls in prop=imageinfo.

The only effect for clients will be a possibility of seeing iicontinue
more often.

Once this is merged, TimedMediaHandler's ApiQueryVideoInfo.php should be
updated in the same way.

Change-Id: Id0ec6a0afb85ca73eefb5b8370c2147c37f037e9

RELEASE-NOTES-1.21
includes/api/ApiQueryImageInfo.php

index 6e811fc..3afd4bf 100644 (file)
@@ -219,6 +219,8 @@ production.
 * Add supports for all pageset capabilities - generators, redirects, converttitles to
   action=purge and action=setnotificationtimestamp.
 * (bug 43251) prop=pageprops&ppprop= now accepts multiple props to query.
+* ApiQueryImageInfo will now limit the number of calls to File::transform made
+  in any one query. If there are too many, iicontinue will be returned.
 
 === API internal changes in 1.21 ===
 * For debugging only, a new global $wgDebugAPI removes many API restrictions when true.
index 351753c..34f78e7 100644 (file)
@@ -30,6 +30,8 @@
  * @ingroup API
  */
 class ApiQueryImageInfo extends ApiQueryBase {
+       const TRANSFORM_LIMIT = 50;
+       private static $transformCount = 0;
 
        public function __construct( $query, $moduleName, $prefix = 'ii' ) {
                // We allow a subclass to override the prefix, to create a related API module.
@@ -90,6 +92,19 @@ class ApiQueryImageInfo extends ApiQueryBase {
 
                                $img = $images[$title];
 
+                               if ( self::getTransformCount() >= self::TRANSFORM_LIMIT ) {
+                                       if ( count( $pageIds[NS_FILE] ) == 1 ) {
+                                               // See the 'the user is screwed' comment below
+                                               $this->setContinueEnumParameter( 'start',
+                                                       $start !== null ? $start : wfTimestamp( TS_ISO_8601, $img->getTimestamp() )
+                                               );
+                                       } else {
+                                               $this->setContinueEnumParameter( 'continue',
+                                                       $this->getContinueStr( $img, $start ) );
+                                       }
+                                       break;
+                               }
+
                                $fit = $result->addValue(
                                        array( 'query', 'pages', intval( $pageId ) ),
                                        'imagerepository', $img->getRepoName()
@@ -337,6 +352,7 @@ class ApiQueryImageInfo extends ApiQueryBase {
                if ( $url ) {
                        if ( !is_null( $thumbParams ) ) {
                                $mto = $file->transform( $thumbParams );
+                               self::$transformCount++;
                                if ( $mto && !$mto->isError() ) {
                                        $vals['thumburl'] = wfExpandUrl( $mto->getUrl(), PROTO_CURRENT );
 
@@ -396,6 +412,17 @@ class ApiQueryImageInfo extends ApiQueryBase {
                return $vals;
        }
 
+       /**
+        * Get the count of image transformations performed
+        *
+        * If this is >= TRANSFORM_LIMIT, you should probably stop processing images.
+        *
+        * @return integer count
+        */
+       static function getTransformCount() {
+               return self::$transformCount;
+       }
+
        /**
         *
         * @param $metadata Array