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 getCacheMode( $params ) {
00048 return 'public';
00049 }
00050
00051 public function executeGenerator($resultPageSet) {
00052 if ($resultPageSet->isResolvingRedirects())
00053 $this->dieUsage('Use "gaifilterredir=nonredirects" option instead of "redirects" when using allimages as a generator', 'params');
00054
00055 $this->run($resultPageSet);
00056 }
00057
00058 private function run($resultPageSet = null) {
00059 $repo = RepoGroup::singleton()->getLocalRepo();
00060 if ( !$repo instanceof LocalRepo )
00061 $this->dieUsage('Local file repository does not support querying all images', 'unsupportedrepo');
00062
00063 $db = $this->getDB();
00064
00065 $params = $this->extractRequestParams();
00066
00067
00068 $dir = ($params['dir'] == 'descending' ? 'older' : 'newer');
00069 $from = (is_null($params['from']) ? null : $this->titlePartToKey($params['from']));
00070 $this->addWhereRange('img_name', $dir, $from, null);
00071 if (isset ($params['prefix']))
00072 $this->addWhere("img_name LIKE '" . $db->escapeLike($this->titlePartToKey($params['prefix'])) . "%'");
00073
00074 if (isset ($params['minsize'])) {
00075 $this->addWhere('img_size>=' . intval($params['minsize']));
00076 }
00077
00078 if (isset ($params['maxsize'])) {
00079 $this->addWhere('img_size<=' . intval($params['maxsize']));
00080 }
00081
00082 $sha1 = false;
00083 if( isset( $params['sha1'] ) ) {
00084 $sha1 = wfBaseConvert( $params['sha1'], 16, 36, 31 );
00085 } elseif( isset( $params['sha1base36'] ) ) {
00086 $sha1 = $params['sha1base36'];
00087 }
00088 if( $sha1 ) {
00089 $this->addWhere( 'img_sha1=' . $db->addQuotes( $sha1 ) );
00090 }
00091
00092 $this->addTables('image');
00093
00094 $prop = array_flip($params['prop']);
00095 $this->addFields( LocalFile::selectFields() );
00096
00097 $limit = $params['limit'];
00098 $this->addOption('LIMIT', $limit+1);
00099 $this->addOption('ORDER BY', 'img_name' .
00100 ($params['dir'] == 'descending' ? ' DESC' : ''));
00101
00102 $res = $this->select(__METHOD__);
00103
00104 $titles = array();
00105 $count = 0;
00106 $result = $this->getResult();
00107 while ($row = $db->fetchObject($res)) {
00108 if (++ $count > $limit) {
00109
00110
00111 $this->setContinueEnumParameter('from', $this->keyToTitle($row->img_name));
00112 break;
00113 }
00114
00115 if (is_null($resultPageSet)) {
00116 $file = $repo->newFileFromRow( $row );
00117 $info = array_merge(array('name' => $row->img_name),
00118 ApiQueryImageInfo::getInfo($file, $prop, $result));
00119 $fit = $result->addValue(array('query', $this->getModuleName()), null, $info);
00120 if( !$fit ) {
00121 $this->setContinueEnumParameter('from', $this->keyToTitle($row->img_name));
00122 break;
00123 }
00124 } else {
00125 $titles[] = Title::makeTitle(NS_IMAGE, $row->img_name);
00126 }
00127 }
00128 $db->freeResult($res);
00129
00130 if (is_null($resultPageSet)) {
00131 $result->setIndexedTagName_internal(array('query', $this->getModuleName()), 'img');
00132 } else {
00133 $resultPageSet->populateFromTitles($titles);
00134 }
00135 }
00136
00137 public function getAllowedParams() {
00138 return array (
00139 'from' => null,
00140 'prefix' => null,
00141 'minsize' => array (
00142 ApiBase :: PARAM_TYPE => 'integer',
00143 ),
00144 'maxsize' => array (
00145 ApiBase :: PARAM_TYPE => 'integer',
00146 ),
00147 'limit' => array (
00148 ApiBase :: PARAM_DFLT => 10,
00149 ApiBase :: PARAM_TYPE => 'limit',
00150 ApiBase :: PARAM_MIN => 1,
00151 ApiBase :: PARAM_MAX => ApiBase :: LIMIT_BIG1,
00152 ApiBase :: PARAM_MAX2 => ApiBase :: LIMIT_BIG2
00153 ),
00154 'dir' => array (
00155 ApiBase :: PARAM_DFLT => 'ascending',
00156 ApiBase :: PARAM_TYPE => array (
00157 'ascending',
00158 'descending'
00159 )
00160 ),
00161 'sha1' => null,
00162 'sha1base36' => null,
00163 'prop' => array (
00164 ApiBase :: PARAM_TYPE => array(
00165 'timestamp',
00166 'user',
00167 'comment',
00168 'url',
00169 'size',
00170 'dimensions',
00171 'mime',
00172 'sha1',
00173 'metadata',
00174 'bitdepth',
00175 ),
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 protected function getExamples() {
00201 return array (
00202 'Simple Use',
00203 ' Show a list of images starting at the letter "B"',
00204 ' api.php?action=query&list=allimages&aifrom=B',
00205 'Using as Generator',
00206 ' Show info about 4 images starting at the letter "T"',
00207 ' api.php?action=query&generator=allimages&gailimit=4&gaifrom=T&prop=imageinfo',
00208 );
00209 }
00210
00211 public function getVersion() {
00212 return __CLASS__ . ': $Id: ApiQueryAllimages.php 69986 2010-07-27 03:57:39Z tstarling $';
00213 }
00214 }