MediaWiki  master
ApiQueryAllImages.php
Go to the documentation of this file.
00001 <?php
00002 
00034 class ApiQueryAllImages extends ApiQueryGeneratorBase {
00035 
00036         protected $mRepo;
00037 
00038         public function __construct( $query, $moduleName ) {
00039                 parent::__construct( $query, $moduleName, 'ai' );
00040                 $this->mRepo = RepoGroup::singleton()->getLocalRepo();
00041         }
00042 
00050         protected function getDB() {
00051                 return $this->mRepo->getSlaveDB();
00052         }
00053 
00054         public function execute() {
00055                 $this->run();
00056         }
00057 
00058         public function getCacheMode( $params ) {
00059                 return 'public';
00060         }
00061 
00066         public function executeGenerator( $resultPageSet ) {
00067                 if ( $resultPageSet->isResolvingRedirects() ) {
00068                         $this->dieUsage( 'Use "gaifilterredir=nonredirects" option instead of "redirects" when using allimages as a generator', 'params' );
00069                 }
00070 
00071                 $this->run( $resultPageSet );
00072         }
00073 
00078         private function run( $resultPageSet = null ) {
00079                 $repo = $this->mRepo;
00080                 if ( !$repo instanceof LocalRepo ) {
00081                         $this->dieUsage( 'Local file repository does not support querying all images', 'unsupportedrepo' );
00082                 }
00083 
00084                 $db = $this->getDB();
00085 
00086                 $params = $this->extractRequestParams();
00087 
00088                 if ( !is_null( $params['continue'] ) ) {
00089                         $cont = explode( '|', $params['continue'] );
00090                         if ( count( $cont ) != 1 ) {
00091                                 $this->dieUsage( "Invalid continue param. You should pass the " .
00092                                         "original value returned by the previous query", "_badcontinue" );
00093                         }
00094                         $op = $params['dir'] == 'descending' ? '<' : '>';
00095                         $cont_from = $db->addQuotes( $cont[0] );
00096                         $this->addWhere( "img_name $op= $cont_from" );
00097                 }
00098 
00099                 // Image filters
00100                 $dir = ( $params['dir'] == 'descending' ? 'older' : 'newer' );
00101                 $from = ( is_null( $params['from'] ) ? null : $this->titlePartToKey( $params['from'] ) );
00102                 $to = ( is_null( $params['to'] ) ? null : $this->titlePartToKey( $params['to'] ) );
00103                 $this->addWhereRange( 'img_name', $dir, $from, $to );
00104 
00105                 if ( isset( $params['prefix'] ) )
00106                         $this->addWhere( 'img_name' . $db->buildLike( $this->titlePartToKey( $params['prefix'] ), $db->anyString() ) );
00107 
00108                 if ( isset( $params['minsize'] ) ) {
00109                         $this->addWhere( 'img_size>=' . intval( $params['minsize'] ) );
00110                 }
00111 
00112                 if ( isset( $params['maxsize'] ) ) {
00113                         $this->addWhere( 'img_size<=' . intval( $params['maxsize'] ) );
00114                 }
00115 
00116                 $sha1 = false;
00117                 if ( isset( $params['sha1'] ) ) {
00118                         if ( !$this->validateSha1Hash( $params['sha1'] ) ) {
00119                                 $this->dieUsage( 'The SHA1 hash provided is not valid', 'invalidsha1hash' );
00120                         }
00121                         $sha1 = wfBaseConvert( $params['sha1'], 16, 36, 31 );
00122                 } elseif ( isset( $params['sha1base36'] ) ) {
00123                         $sha1 = $params['sha1base36'];
00124                         if ( !$this->validateSha1Base36Hash( $sha1 ) ) {
00125                                 $this->dieUsage( 'The SHA1Base36 hash provided is not valid', 'invalidsha1base36hash' );
00126                         }
00127                 }
00128                 if ( $sha1 ) {
00129                         $this->addWhereFld( 'img_sha1', $sha1 );
00130                 }
00131 
00132                 if ( !is_null( $params['mime'] ) ) {
00133                         global $wgMiserMode;
00134                         if ( $wgMiserMode  ) {
00135                                 $this->dieUsage( 'MIME search disabled in Miser Mode', 'mimesearchdisabled' );
00136                         }
00137 
00138                         list( $major, $minor ) = File::splitMime( $params['mime'] );
00139 
00140                         $this->addWhereFld( 'img_major_mime', $major );
00141                         $this->addWhereFld( 'img_minor_mime', $minor );
00142                 }
00143 
00144                 $this->addTables( 'image' );
00145 
00146                 $prop = array_flip( $params['prop'] );
00147                 $this->addFields( LocalFile::selectFields() );
00148 
00149                 $limit = $params['limit'];
00150                 $this->addOption( 'LIMIT', $limit + 1 );
00151                 $sort = ( $params['dir'] == 'descending' ? ' DESC' : '' );
00152                 $this->addOption( 'ORDER BY', 'img_name' . $sort );
00153 
00154                 $res = $this->select( __METHOD__ );
00155 
00156                 $titles = array();
00157                 $count = 0;
00158                 $result = $this->getResult();
00159                 foreach ( $res as $row ) {
00160                         if ( ++ $count > $limit ) {
00161                                 // We've reached the one extra which shows that there are additional pages to be had. Stop here...
00162                                 $this->setContinueEnumParameter( 'continue', $row->img_name );
00163                                 break;
00164                         }
00165 
00166                         if ( is_null( $resultPageSet ) ) {
00167                                 $file = $repo->newFileFromRow( $row );
00168                                 $info = array_merge( array( 'name' => $row->img_name ),
00169                                         ApiQueryImageInfo::getInfo( $file, $prop, $result ) );
00170                                 self::addTitleInfo( $info, $file->getTitle() );
00171 
00172                                 $fit = $result->addValue( array( 'query', $this->getModuleName() ), null, $info );
00173                                 if ( !$fit ) {
00174                                         $this->setContinueEnumParameter( 'continue', $row->img_name );
00175                                         break;
00176                                 }
00177                         } else {
00178                                 $titles[] = Title::makeTitle( NS_FILE, $row->img_name );
00179                         }
00180                 }
00181 
00182                 if ( is_null( $resultPageSet ) ) {
00183                         $result->setIndexedTagName_internal( array( 'query', $this->getModuleName() ), 'img' );
00184                 } else {
00185                         $resultPageSet->populateFromTitles( $titles );
00186                 }
00187         }
00188 
00189         public function getAllowedParams() {
00190                 return array (
00191                         'from' => null,
00192                         'continue' => null,
00193                         'to' => null,
00194                         'prefix' => null,
00195                         'minsize' => array(
00196                                 ApiBase::PARAM_TYPE => 'integer',
00197                         ),
00198                         'maxsize' => array(
00199                                 ApiBase::PARAM_TYPE => 'integer',
00200                         ),
00201                         'limit' => array(
00202                                 ApiBase::PARAM_DFLT => 10,
00203                                 ApiBase::PARAM_TYPE => 'limit',
00204                                 ApiBase::PARAM_MIN => 1,
00205                                 ApiBase::PARAM_MAX => ApiBase::LIMIT_BIG1,
00206                                 ApiBase::PARAM_MAX2 => ApiBase::LIMIT_BIG2
00207                         ),
00208                         'dir' => array(
00209                                 ApiBase::PARAM_DFLT => 'ascending',
00210                                 ApiBase::PARAM_TYPE => array(
00211                                         'ascending',
00212                                         'descending'
00213                                 )
00214                         ),
00215                         'sha1' => null,
00216                         'sha1base36' => null,
00217                         'prop' => array(
00218                                 ApiBase::PARAM_TYPE => ApiQueryImageInfo::getPropertyNames( $this->propertyFilter ),
00219                                 ApiBase::PARAM_DFLT => 'timestamp|url',
00220                                 ApiBase::PARAM_ISMULTI => true
00221                         ),
00222                         'mime' => null,
00223                 );
00224         }
00225 
00226         public function getParamDescription() {
00227                 return array(
00228                         'from' => 'The image title to start enumerating from',
00229                         'continue' => 'When more results are available, use this to continue',
00230                         'to' => 'The image title to stop enumerating at',
00231                         'prefix' => 'Search for all image titles that begin with this value',
00232                         'dir' => 'The direction in which to list',
00233                         'minsize' => 'Limit to images with at least this many bytes',
00234                         'maxsize' => 'Limit to images with at most this many bytes',
00235                         'limit' => 'How many images in total to return',
00236                         'sha1' => "SHA1 hash of image. Overrides {$this->getModulePrefix()}sha1base36",
00237                         'sha1base36' => 'SHA1 hash of image in base 36 (used in MediaWiki)',
00238                         'prop' => ApiQueryImageInfo::getPropertyDescriptions( $this->propertyFilter ),
00239                         'mime' => 'What MIME type to search for. e.g. image/jpeg. Disabled in Miser Mode',
00240                 );
00241         }
00242 
00243         private $propertyFilter = array( 'archivename', 'thumbmime' );
00244 
00245         public function getResultProperties() {
00246                 return array_merge(
00247                         array(
00248                                 '' => array(
00249                                         'name' => 'string',
00250                                         'ns' => 'namespace',
00251                                         'title' => 'string'
00252                                 )
00253                         ),
00254                         ApiQueryImageInfo::getResultPropertiesFiltered( $this->propertyFilter )
00255                 );
00256         }
00257 
00258         public function getDescription() {
00259                 return 'Enumerate all images sequentially';
00260         }
00261 
00262         public function getPossibleErrors() {
00263                 return array_merge( parent::getPossibleErrors(), array(
00264                         array( 'code' => 'params', 'info' => 'Use "gaifilterredir=nonredirects" option instead of "redirects" when using allimages as a generator' ),
00265                         array( 'code' => 'unsupportedrepo', 'info' => 'Local file repository does not support querying all images' ),
00266                         array( 'code' => 'mimesearchdisabled', 'info' => 'MIME search disabled in Miser Mode' ),
00267                         array( 'code' => 'invalidsha1hash', 'info' => 'The SHA1 hash provided is not valid' ),
00268                         array( 'code' => 'invalidsha1base36hash', 'info' => 'The SHA1Base36 hash provided is not valid' ),
00269                         array( 'code' => '_badcontinue', 'info' => 'Invalid continue param. You should pass the original value returned by the previous query' ),
00270                 ) );
00271         }
00272 
00273         public function getExamples() {
00274                 return array(
00275                         'api.php?action=query&list=allimages&aifrom=B' => array(
00276                                 'Simple Use',
00277                                 'Show a list of images starting at the letter "B"',
00278                         ),
00279                         'api.php?action=query&generator=allimages&gailimit=4&gaifrom=T&prop=imageinfo' => array(
00280                                 'Using as Generator',
00281                                 'Show info about 4 images starting at the letter "T"',
00282                         ),
00283                 );
00284         }
00285 
00286         public function getHelpUrls() {
00287                 return 'https://www.mediawiki.org/wiki/API:Allimages';
00288         }
00289 
00290         public function getVersion() {
00291                 return __CLASS__ . ': $Id$';
00292         }
00293 }