more accurate documentation for addValue() method
[lhc/web/wiklou.git] / includes / api / ApiQueryAllimages.php
index 89cc9db..bdb819d 100644 (file)
@@ -46,7 +46,7 @@ class ApiQueryAllimages extends ApiQueryGeneratorBase {
        }
 
        /**
-        * Overide parent method to make sure to make sure the repo's DB is used
+        * Override parent method to make sure to make sure the repo's DB is used
         * which may not necesarilly be the same as the local DB.
         *
         * TODO: allow querying non-local repos.
@@ -109,12 +109,30 @@ class ApiQueryAllimages extends ApiQueryGeneratorBase {
 
                $sha1 = false;
                if ( isset( $params['sha1'] ) ) {
+                       if ( !$this->validateSha1Hash( $params['sha1'] ) ) {
+                               $this->dieUsage( 'The SHA1 hash provided is not valid', 'invalidsha1hash' );
+                       }
                        $sha1 = wfBaseConvert( $params['sha1'], 16, 36, 31 );
                } elseif ( isset( $params['sha1base36'] ) ) {
                        $sha1 = $params['sha1base36'];
+                       if ( !$this->validateSha1Base36Hash( $sha1 ) ) {
+                               $this->dieUsage( 'The SHA1Base36 hash provided is not valid', 'invalidsha1base36hash' );
+                       }
                }
                if ( $sha1 ) {
-                       $this->addWhere( 'img_sha1=' . $db->addQuotes( $sha1 ) );
+                       $this->addWhereFld( 'img_sha1', $sha1 );
+               }
+
+               if ( !is_null( $params['mime'] ) ) {
+                       global $wgMiserMode;
+                       if ( $wgMiserMode  ) {
+                               $this->dieUsage( 'MIME search disabled in Miser Mode', 'mimesearchdisabled' );
+                       }
+
+                       list( $major, $minor ) = File::splitMime( $params['mime'] );
+
+                       $this->addWhereFld( 'img_major_mime', $major );
+                       $this->addWhereFld( 'img_minor_mime', $minor );
                }
 
                $this->addTables( 'image' );
@@ -144,8 +162,8 @@ class ApiQueryAllimages extends ApiQueryGeneratorBase {
                                $file = $repo->newFileFromRow( $row );
                                $info = array_merge( array( 'name' => $row->img_name ),
                                        ApiQueryImageInfo::getInfo( $file, $prop, $result ) );
-                               self::addTitleInfo( $info, $file->getTitle() ); 
-                                       
+                               self::addTitleInfo( $info, $file->getTitle() );
+
                                $fit = $result->addValue( array( 'query', $this->getModuleName() ), null, $info );
                                if ( !$fit ) {
                                        $this->setContinueEnumParameter( 'from', $this->keyToTitle( $row->img_name ) );
@@ -191,10 +209,11 @@ class ApiQueryAllimages extends ApiQueryGeneratorBase {
                        'sha1' => null,
                        'sha1base36' => null,
                        'prop' => array(
-                               ApiBase::PARAM_TYPE => ApiQueryImageInfo::getPropertyNames(),
+                               ApiBase::PARAM_TYPE => ApiQueryImageInfo::getPropertyNames( $this->propertyFilter ),
                                ApiBase::PARAM_DFLT => 'timestamp|url',
                                ApiBase::PARAM_ISMULTI => true
-                       )
+                       ),
+                       'mime' => null,
                );
        }
 
@@ -209,10 +228,13 @@ class ApiQueryAllimages extends ApiQueryGeneratorBase {
                        'limit' => 'How many images in total to return',
                        'sha1' => "SHA1 hash of image. Overrides {$this->getModulePrefix()}sha1base36",
                        'sha1base36' => 'SHA1 hash of image in base 36 (used in MediaWiki)',
-                       'prop' => ApiQueryImageInfo::getPropertyDescriptions(),
+                       'prop' => ApiQueryImageInfo::getPropertyDescriptions( $this->propertyFilter ),
+                       'mime' => 'What MIME type to search for. e.g. image/jpeg. Disabled in Miser Mode',
                );
        }
 
+       private $propertyFilter = array( 'archivename' );
+
        public function getDescription() {
                return 'Enumerate all images sequentially';
        }
@@ -221,10 +243,13 @@ class ApiQueryAllimages extends ApiQueryGeneratorBase {
                return array_merge( parent::getPossibleErrors(), array(
                        array( 'code' => 'params', 'info' => 'Use "gaifilterredir=nonredirects" option instead of "redirects" when using allimages as a generator' ),
                        array( 'code' => 'unsupportedrepo', 'info' => 'Local file repository does not support querying all images' ),
+                       array( 'code' => 'mimesearchdisabled', 'info' => 'MIME search disabled in Miser Mode' ),
+                       array( 'code' => 'invalidsha1hash', 'info' => 'The SHA1 hash provided is not valid' ),
+                       array( 'code' => 'invalidsha1base36hash', 'info' => 'The SHA1Base36 hash provided is not valid' ),
                ) );
        }
 
-       protected function getExamples() {
+       public function getExamples() {
                return array(
                        'Simple Use',
                        ' Show a list of images starting at the letter "B"',
@@ -235,6 +260,10 @@ class ApiQueryAllimages extends ApiQueryGeneratorBase {
                );
        }
 
+       public function getHelpUrls() {
+               return 'http://www.mediawiki.org/wiki/API:Allimages';
+       }
+
        public function getVersion() {
                return __CLASS__ . ': $Id$';
        }