00001 <?php
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027 if ( !defined( 'MEDIAWIKI' ) ) {
00028
00029 require_once ( 'ApiQueryBase.php' );
00030 }
00031
00037 class ApiQueryAllimages extends ApiQueryGeneratorBase {
00038
00039 public function __construct( $query, $moduleName ) {
00040 parent :: __construct( $query, $moduleName, 'ai' );
00041 $this->mRepo = RepoGroup::singleton()->getLocalRepo();
00042 }
00043
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
00062 public function executeGenerator( $resultPageSet ) {
00063 if ( $resultPageSet->isResolvingRedirects() )
00064 $this->dieUsage( 'Use "gaifilterredir=nonredirects" option instead of "redirects" when using allimages as a generator', 'params' );
00065
00066 $this->run( $resultPageSet );
00067 }
00068
00069 private function run( $resultPageSet = null ) {
00070 $repo = $this->mRepo;
00071 if ( !$repo instanceof LocalRepo )
00072 $this->dieUsage( 'Local file repository does not support querying all images', 'unsupportedrepo' );
00073
00074 $db = $this->getDB();
00075
00076 $params = $this->extractRequestParams();
00077
00078
00079 $dir = ( $params['dir'] == 'descending' ? 'older' : 'newer' );
00080 $from = ( is_null( $params['from'] ) ? null : $this->titlePartToKey( $params['from'] ) );
00081 $this->addWhereRange( 'img_name', $dir, $from, null );
00082 if ( isset ( $params['prefix'] ) )
00083 $this->addWhere( 'img_name' . $db->buildLike( $this->titlePartToKey( $params['prefix'] ), $db->anyString() ) );
00084
00085 if ( isset ( $params['minsize'] ) ) {
00086 $this->addWhere( 'img_size>=' . intval( $params['minsize'] ) );
00087 }
00088
00089 if ( isset ( $params['maxsize'] ) ) {
00090 $this->addWhere( 'img_size<=' . intval( $params['maxsize'] ) );
00091 }
00092
00093 $sha1 = false;
00094 if ( isset( $params['sha1'] ) ) {
00095 $sha1 = wfBaseConvert( $params['sha1'], 16, 36, 31 );
00096 } elseif ( isset( $params['sha1base36'] ) ) {
00097 $sha1 = $params['sha1base36'];
00098 }
00099 if ( $sha1 ) {
00100 $this->addWhere( 'img_sha1=' . $db->addQuotes( $sha1 ) );
00101 }
00102
00103 $this->addTables( 'image' );
00104
00105 $prop = array_flip( $params['prop'] );
00106 $this->addFields( LocalFile::selectFields() );
00107
00108 $limit = $params['limit'];
00109 $this->addOption( 'LIMIT', $limit + 1 );
00110 $this->addOption( 'ORDER BY', 'img_name' .
00111 ( $params['dir'] == 'descending' ? ' DESC' : '' ) );
00112
00113 $res = $this->select( __METHOD__ );
00114
00115 $titles = array();
00116 $count = 0;
00117 $result = $this->getResult();
00118 while ( $row = $db->fetchObject( $res ) ) {
00119 if ( ++ $count > $limit ) {
00120
00121
00122 $this->setContinueEnumParameter( 'from', $this->keyToTitle( $row->img_name ) );
00123 break;
00124 }
00125
00126 if ( is_null( $resultPageSet ) ) {
00127 $file = $repo->newFileFromRow( $row );
00128 $info = array_merge( array( 'name' => $row->img_name ),
00129 ApiQueryImageInfo::getInfo( $file, $prop, $result ) );
00130 $fit = $result->addValue( array( 'query', $this->getModuleName() ), null, $info );
00131 if ( !$fit ) {
00132 $this->setContinueEnumParameter( 'from', $this->keyToTitle( $row->img_name ) );
00133 break;
00134 }
00135 } else {
00136 $titles[] = Title::makeTitle( NS_IMAGE, $row->img_name );
00137 }
00138 }
00139 $db->freeResult( $res );
00140
00141 if ( is_null( $resultPageSet ) ) {
00142 $result->setIndexedTagName_internal( array( 'query', $this->getModuleName() ), 'img' );
00143 } else {
00144 $resultPageSet->populateFromTitles( $titles );
00145 }
00146 }
00147
00148 public function getAllowedParams() {
00149 return array (
00150 'from' => null,
00151 'prefix' => null,
00152 'minsize' => array (
00153 ApiBase :: PARAM_TYPE => 'integer',
00154 ),
00155 'maxsize' => array (
00156 ApiBase :: PARAM_TYPE => 'integer',
00157 ),
00158 'limit' => array (
00159 ApiBase :: PARAM_DFLT => 10,
00160 ApiBase :: PARAM_TYPE => 'limit',
00161 ApiBase :: PARAM_MIN => 1,
00162 ApiBase :: PARAM_MAX => ApiBase :: LIMIT_BIG1,
00163 ApiBase :: PARAM_MAX2 => ApiBase :: LIMIT_BIG2
00164 ),
00165 'dir' => array (
00166 ApiBase :: PARAM_DFLT => 'ascending',
00167 ApiBase :: PARAM_TYPE => array (
00168 'ascending',
00169 'descending'
00170 )
00171 ),
00172 'sha1' => null,
00173 'sha1base36' => null,
00174 'prop' => array (
00175 ApiBase :: PARAM_TYPE => ApiQueryImageInfo::getPropertyNames(),
00176 ApiBase :: PARAM_DFLT => 'timestamp|url',
00177 ApiBase :: PARAM_ISMULTI => true
00178 )
00179 );
00180 }
00181
00182 public function getParamDescription() {
00183 return array (
00184 'from' => 'The image title to start enumerating from.',
00185 'prefix' => 'Search for all image titles that begin with this value.',
00186 'dir' => 'The direction in which to list',
00187 'minsize' => 'Limit to images with at least this many bytes',
00188 'maxsize' => 'Limit to images with at most this many bytes',
00189 'limit' => 'How many total images to return.',
00190 'sha1' => 'SHA1 hash of image',
00191 'sha1base36' => 'SHA1 hash of image in base 36 (used in MediaWiki)',
00192 'prop' => 'Which properties to get',
00193 );
00194 }
00195
00196 public function getDescription() {
00197 return 'Enumerate all images sequentially';
00198 }
00199
00200 public function getPossibleErrors() {
00201 return array_merge( parent::getPossibleErrors(), array(
00202 array( 'code' => 'params', 'info' => 'Use "gaifilterredir=nonredirects" option instead of "redirects" when using allimages as a generator' ),
00203 array( 'code' => 'unsupportedrepo', 'info' => 'Local file repository does not support querying all images' ),
00204 ) );
00205 }
00206
00207 protected function getExamples() {
00208 return array (
00209 'Simple Use',
00210 ' Show a list of images starting at the letter "B"',
00211 ' api.php?action=query&list=allimages&aifrom=B',
00212 'Using as Generator',
00213 ' Show info about 4 images starting at the letter "T"',
00214 ' api.php?action=query&generator=allimages&gailimit=4&gaifrom=T&prop=imageinfo',
00215 );
00216 }
00217
00218 public function getVersion() {
00219 return __CLASS__ . ': $Id: ApiQueryAllimages.php 69932 2010-07-26 08:03:21Z tstarling $';
00220 }
00221 }