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 }
00042
00043 public function execute() {
00044 $this->run();
00045 }
00046
00047 public function executeGenerator($resultPageSet) {
00048 if ($resultPageSet->isResolvingRedirects())
00049 $this->dieUsage('Use "gaifilterredir=nonredirects" option instead of "redirects" when using allimages as a generator', 'params');
00050
00051 $this->run($resultPageSet);
00052 }
00053
00054 private function run($resultPageSet = null) {
00055 $repo = RepoGroup::singleton()->getLocalRepo();
00056 if ( !$repo instanceof LocalRepo )
00057 $this->dieUsage('Local file repository does not support querying all images', 'unsupportedrepo');
00058
00059 $db = $this->getDB();
00060
00061 $params = $this->extractRequestParams();
00062
00063
00064 $dir = ($params['dir'] == 'descending' ? 'older' : 'newer');
00065 $from = (is_null($params['from']) ? null : $this->titlePartToKey($params['from']));
00066 $this->addWhereRange('img_name', $dir, $from, null);
00067 if (isset ($params['prefix']))
00068 $this->addWhere("img_name LIKE '" . $db->escapeLike($this->titlePartToKey($params['prefix'])) . "%'");
00069
00070 if (isset ($params['minsize'])) {
00071 $this->addWhere('img_size>=' . intval($params['minsize']));
00072 }
00073
00074 if (isset ($params['maxsize'])) {
00075 $this->addWhere('img_size<=' . intval($params['maxsize']));
00076 }
00077
00078 $sha1 = false;
00079 if( isset( $params['sha1'] ) ) {
00080 $sha1 = wfBaseConvert( $params['sha1'], 16, 36, 31 );
00081 } elseif( isset( $params['sha1base36'] ) ) {
00082 $sha1 = $params['sha1base36'];
00083 }
00084 if( $sha1 ) {
00085 $this->addWhere( 'img_sha1=' . $db->addQuotes( $sha1 ) );
00086 }
00087
00088 $this->addTables('image');
00089
00090 $prop = array_flip($params['prop']);
00091 $this->addFields( LocalFile::selectFields() );
00092
00093 $limit = $params['limit'];
00094 $this->addOption('LIMIT', $limit+1);
00095 $this->addOption('ORDER BY', 'img_name' .
00096 ($params['dir'] == 'descending' ? ' DESC' : ''));
00097
00098 $res = $this->select(__METHOD__);
00099
00100 $titles = array();
00101 $count = 0;
00102 $result = $this->getResult();
00103 while ($row = $db->fetchObject($res)) {
00104 if (++ $count > $limit) {
00105
00106
00107 $this->setContinueEnumParameter('from', $this->keyToTitle($row->img_name));
00108 break;
00109 }
00110
00111 if (is_null($resultPageSet)) {
00112 $file = $repo->newFileFromRow( $row );
00113 $info = array_merge(array('name' => $row->img_name),
00114 ApiQueryImageInfo::getInfo($file, $prop, $result));
00115 $fit = $result->addValue(array('query', $this->getModuleName()), null, $info);
00116 if( !$fit ) {
00117 $this->setContinueEnumParameter('from', $this->keyToTitle($row->img_name));
00118 break;
00119 }
00120 } else {
00121 $titles[] = Title::makeTitle(NS_IMAGE, $row->img_name);
00122 }
00123 }
00124 $db->freeResult($res);
00125
00126 if (is_null($resultPageSet)) {
00127 $result->setIndexedTagName_internal(array('query', $this->getModuleName()), 'img');
00128 } else {
00129 $resultPageSet->populateFromTitles($titles);
00130 }
00131 }
00132
00133 public function getAllowedParams() {
00134 return array (
00135 'from' => null,
00136 'prefix' => null,
00137 'minsize' => array (
00138 ApiBase :: PARAM_TYPE => 'integer',
00139 ),
00140 'maxsize' => array (
00141 ApiBase :: PARAM_TYPE => 'integer',
00142 ),
00143 'limit' => array (
00144 ApiBase :: PARAM_DFLT => 10,
00145 ApiBase :: PARAM_TYPE => 'limit',
00146 ApiBase :: PARAM_MIN => 1,
00147 ApiBase :: PARAM_MAX => ApiBase :: LIMIT_BIG1,
00148 ApiBase :: PARAM_MAX2 => ApiBase :: LIMIT_BIG2
00149 ),
00150 'dir' => array (
00151 ApiBase :: PARAM_DFLT => 'ascending',
00152 ApiBase :: PARAM_TYPE => array (
00153 'ascending',
00154 'descending'
00155 )
00156 ),
00157 'sha1' => null,
00158 'sha1base36' => null,
00159 'prop' => array (
00160 ApiBase :: PARAM_TYPE => array(
00161 'timestamp',
00162 'user',
00163 'comment',
00164 'url',
00165 'size',
00166 'dimensions',
00167 'mime',
00168 'sha1',
00169 'metadata',
00170 'bitdepth',
00171 ),
00172 ApiBase :: PARAM_DFLT => 'timestamp|url',
00173 ApiBase :: PARAM_ISMULTI => true
00174 )
00175 );
00176 }
00177
00178 public function getParamDescription() {
00179 return array (
00180 'from' => 'The image title to start enumerating from.',
00181 'prefix' => 'Search for all image titles that begin with this value.',
00182 'dir' => 'The direction in which to list',
00183 'minsize' => 'Limit to images with at least this many bytes',
00184 'maxsize' => 'Limit to images with at most this many bytes',
00185 'limit' => 'How many total images to return.',
00186 'sha1' => 'SHA1 hash of image',
00187 'sha1base36' => 'SHA1 hash of image in base 36 (used in MediaWiki)',
00188 'prop' => 'Which properties to get',
00189 );
00190 }
00191
00192 public function getDescription() {
00193 return 'Enumerate all images sequentially';
00194 }
00195
00196 protected function getExamples() {
00197 return array (
00198 'Simple Use',
00199 ' Show a list of images starting at the letter "B"',
00200 ' api.php?action=query&list=allimages&aifrom=B',
00201 'Using as Generator',
00202 ' Show info about 4 images starting at the letter "T"',
00203 ' api.php?action=query&generator=allimages&gailimit=4&gaifrom=T&prop=imageinfo',
00204 );
00205 }
00206
00207 public function getVersion() {
00208 return __CLASS__ . ': $Id: ApiQueryAllimages.php 46845 2009-02-05 14:30:59Z catrope $';
00209 }
00210 }