MediaWiki
REL1_19
|
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 // Image filters 00089 $dir = ( $params['dir'] == 'descending' ? 'older' : 'newer' ); 00090 $from = ( is_null( $params['from'] ) ? null : $this->titlePartToKey( $params['from'] ) ); 00091 $to = ( is_null( $params['to'] ) ? null : $this->titlePartToKey( $params['to'] ) ); 00092 $this->addWhereRange( 'img_name', $dir, $from, $to ); 00093 00094 if ( isset( $params['prefix'] ) ) 00095 $this->addWhere( 'img_name' . $db->buildLike( $this->titlePartToKey( $params['prefix'] ), $db->anyString() ) ); 00096 00097 if ( isset( $params['minsize'] ) ) { 00098 $this->addWhere( 'img_size>=' . intval( $params['minsize'] ) ); 00099 } 00100 00101 if ( isset( $params['maxsize'] ) ) { 00102 $this->addWhere( 'img_size<=' . intval( $params['maxsize'] ) ); 00103 } 00104 00105 $sha1 = false; 00106 if ( isset( $params['sha1'] ) ) { 00107 if ( !$this->validateSha1Hash( $params['sha1'] ) ) { 00108 $this->dieUsage( 'The SHA1 hash provided is not valid', 'invalidsha1hash' ); 00109 } 00110 $sha1 = wfBaseConvert( $params['sha1'], 16, 36, 31 ); 00111 } elseif ( isset( $params['sha1base36'] ) ) { 00112 $sha1 = $params['sha1base36']; 00113 if ( !$this->validateSha1Base36Hash( $sha1 ) ) { 00114 $this->dieUsage( 'The SHA1Base36 hash provided is not valid', 'invalidsha1base36hash' ); 00115 } 00116 } 00117 if ( $sha1 ) { 00118 $this->addWhereFld( 'img_sha1', $sha1 ); 00119 } 00120 00121 if ( !is_null( $params['mime'] ) ) { 00122 global $wgMiserMode; 00123 if ( $wgMiserMode ) { 00124 $this->dieUsage( 'MIME search disabled in Miser Mode', 'mimesearchdisabled' ); 00125 } 00126 00127 list( $major, $minor ) = File::splitMime( $params['mime'] ); 00128 00129 $this->addWhereFld( 'img_major_mime', $major ); 00130 $this->addWhereFld( 'img_minor_mime', $minor ); 00131 } 00132 00133 $this->addTables( 'image' ); 00134 00135 $prop = array_flip( $params['prop'] ); 00136 $this->addFields( LocalFile::selectFields() ); 00137 00138 $limit = $params['limit']; 00139 $this->addOption( 'LIMIT', $limit + 1 ); 00140 $this->addOption( 'ORDER BY', 'img_name' . 00141 ( $params['dir'] == 'descending' ? ' DESC' : '' ) ); 00142 00143 $res = $this->select( __METHOD__ ); 00144 00145 $titles = array(); 00146 $count = 0; 00147 $result = $this->getResult(); 00148 foreach ( $res as $row ) { 00149 if ( ++ $count > $limit ) { 00150 // We've reached the one extra which shows that there are additional pages to be had. Stop here... 00151 // TODO: Security issue - if the user has no right to view next title, it will still be shown 00152 $this->setContinueEnumParameter( 'from', $this->keyToTitle( $row->img_name ) ); 00153 break; 00154 } 00155 00156 if ( is_null( $resultPageSet ) ) { 00157 $file = $repo->newFileFromRow( $row ); 00158 $info = array_merge( array( 'name' => $row->img_name ), 00159 ApiQueryImageInfo::getInfo( $file, $prop, $result ) ); 00160 self::addTitleInfo( $info, $file->getTitle() ); 00161 00162 $fit = $result->addValue( array( 'query', $this->getModuleName() ), null, $info ); 00163 if ( !$fit ) { 00164 $this->setContinueEnumParameter( 'from', $this->keyToTitle( $row->img_name ) ); 00165 break; 00166 } 00167 } else { 00168 $titles[] = Title::makeTitle( NS_IMAGE, $row->img_name ); 00169 } 00170 } 00171 00172 if ( is_null( $resultPageSet ) ) { 00173 $result->setIndexedTagName_internal( array( 'query', $this->getModuleName() ), 'img' ); 00174 } else { 00175 $resultPageSet->populateFromTitles( $titles ); 00176 } 00177 } 00178 00179 public function getAllowedParams() { 00180 return array ( 00181 'from' => null, 00182 'to' => null, 00183 'prefix' => null, 00184 'minsize' => array( 00185 ApiBase::PARAM_TYPE => 'integer', 00186 ), 00187 'maxsize' => array( 00188 ApiBase::PARAM_TYPE => 'integer', 00189 ), 00190 'limit' => array( 00191 ApiBase::PARAM_DFLT => 10, 00192 ApiBase::PARAM_TYPE => 'limit', 00193 ApiBase::PARAM_MIN => 1, 00194 ApiBase::PARAM_MAX => ApiBase::LIMIT_BIG1, 00195 ApiBase::PARAM_MAX2 => ApiBase::LIMIT_BIG2 00196 ), 00197 'dir' => array( 00198 ApiBase::PARAM_DFLT => 'ascending', 00199 ApiBase::PARAM_TYPE => array( 00200 'ascending', 00201 'descending' 00202 ) 00203 ), 00204 'sha1' => null, 00205 'sha1base36' => null, 00206 'prop' => array( 00207 ApiBase::PARAM_TYPE => ApiQueryImageInfo::getPropertyNames( $this->propertyFilter ), 00208 ApiBase::PARAM_DFLT => 'timestamp|url', 00209 ApiBase::PARAM_ISMULTI => true 00210 ), 00211 'mime' => null, 00212 ); 00213 } 00214 00215 public function getParamDescription() { 00216 return array( 00217 'from' => 'The image title to start enumerating from', 00218 'to' => 'The image title to stop enumerating at', 00219 'prefix' => 'Search for all image titles that begin with this value', 00220 'dir' => 'The direction in which to list', 00221 'minsize' => 'Limit to images with at least this many bytes', 00222 'maxsize' => 'Limit to images with at most this many bytes', 00223 'limit' => 'How many images in total to return', 00224 'sha1' => "SHA1 hash of image. Overrides {$this->getModulePrefix()}sha1base36", 00225 'sha1base36' => 'SHA1 hash of image in base 36 (used in MediaWiki)', 00226 'prop' => ApiQueryImageInfo::getPropertyDescriptions( $this->propertyFilter ), 00227 'mime' => 'What MIME type to search for. e.g. image/jpeg. Disabled in Miser Mode', 00228 ); 00229 } 00230 00231 private $propertyFilter = array( 'archivename' ); 00232 00233 public function getDescription() { 00234 return 'Enumerate all images sequentially'; 00235 } 00236 00237 public function getPossibleErrors() { 00238 return array_merge( parent::getPossibleErrors(), array( 00239 array( 'code' => 'params', 'info' => 'Use "gaifilterredir=nonredirects" option instead of "redirects" when using allimages as a generator' ), 00240 array( 'code' => 'unsupportedrepo', 'info' => 'Local file repository does not support querying all images' ), 00241 array( 'code' => 'mimesearchdisabled', 'info' => 'MIME search disabled in Miser Mode' ), 00242 array( 'code' => 'invalidsha1hash', 'info' => 'The SHA1 hash provided is not valid' ), 00243 array( 'code' => 'invalidsha1base36hash', 'info' => 'The SHA1Base36 hash provided is not valid' ), 00244 ) ); 00245 } 00246 00247 public function getExamples() { 00248 return array( 00249 'api.php?action=query&list=allimages&aifrom=B' => array( 00250 'Simple Use', 00251 'Show a list of images starting at the letter "B"', 00252 ), 00253 'api.php?action=query&generator=allimages&gailimit=4&gaifrom=T&prop=imageinfo' => array( 00254 'Using as Generator', 00255 'Show info about 4 images starting at the letter "T"', 00256 ), 00257 ); 00258 } 00259 00260 public function getHelpUrls() { 00261 return 'https://www.mediawiki.org/wiki/API:Allimages'; 00262 } 00263 00264 public function getVersion() { 00265 return __CLASS__ . ': $Id$'; 00266 } 00267 }