Merge "(bug 30625) Return warnings, if they exist, despite ignorewarnings"
[lhc/web/wiklou.git] / includes / api / ApiQueryAllImages.php
1 <?php
2
3 /**
4 * API for MediaWiki 1.12+
5 *
6 * Created on Mar 16, 2008
7 *
8 * Copyright © 2008 Vasiliev Victor vasilvv@gmail.com,
9 * based on ApiQueryAllPages.php
10 *
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
15 *
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
20 *
21 * You should have received a copy of the GNU General Public License along
22 * with this program; if not, write to the Free Software Foundation, Inc.,
23 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
24 * http://www.gnu.org/copyleft/gpl.html
25 *
26 * @file
27 */
28
29 /**
30 * Query module to enumerate all available pages.
31 *
32 * @ingroup API
33 */
34 class ApiQueryAllImages extends ApiQueryGeneratorBase {
35
36 protected $mRepo;
37
38 public function __construct( $query, $moduleName ) {
39 parent::__construct( $query, $moduleName, 'ai' );
40 $this->mRepo = RepoGroup::singleton()->getLocalRepo();
41 }
42
43 /**
44 * Override parent method to make sure to make sure the repo's DB is used
45 * which may not necesarilly be the same as the local DB.
46 *
47 * TODO: allow querying non-local repos.
48 * @return DatabaseBase
49 */
50 protected function getDB() {
51 return $this->mRepo->getSlaveDB();
52 }
53
54 public function execute() {
55 $this->run();
56 }
57
58 public function getCacheMode( $params ) {
59 return 'public';
60 }
61
62 /**
63 * @param $resultPageSet ApiPageSet
64 * @return void
65 */
66 public function executeGenerator( $resultPageSet ) {
67 if ( $resultPageSet->isResolvingRedirects() ) {
68 $this->dieUsage( 'Use "gaifilterredir=nonredirects" option instead of "redirects" when using allimages as a generator', 'params' );
69 }
70
71 $this->run( $resultPageSet );
72 }
73
74 /**
75 * @param $resultPageSet ApiPageSet
76 * @return void
77 */
78 private function run( $resultPageSet = null ) {
79 $repo = $this->mRepo;
80 if ( !$repo instanceof LocalRepo ) {
81 $this->dieUsage( 'Local file repository does not support querying all images', 'unsupportedrepo' );
82 }
83
84 $db = $this->getDB();
85
86 $params = $this->extractRequestParams();
87
88 if ( !is_null( $params['continue'] ) ) {
89 $cont = explode( '|', $params['continue'] );
90 if ( count( $cont ) != 1 ) {
91 $this->dieUsage( "Invalid continue param. You should pass the " .
92 "original value returned by the previous query", "_badcontinue" );
93 }
94 $op = $params['dir'] == 'descending' ? '<' : '>';
95 $cont_from = $db->addQuotes( $cont[0] );
96 $this->addWhere( "img_name $op= $cont_from" );
97 }
98
99 // Image filters
100 $dir = ( $params['dir'] == 'descending' ? 'older' : 'newer' );
101 $from = ( is_null( $params['from'] ) ? null : $this->titlePartToKey( $params['from'] ) );
102 $to = ( is_null( $params['to'] ) ? null : $this->titlePartToKey( $params['to'] ) );
103 $this->addWhereRange( 'img_name', $dir, $from, $to );
104
105 if ( isset( $params['prefix'] ) )
106 $this->addWhere( 'img_name' . $db->buildLike( $this->titlePartToKey( $params['prefix'] ), $db->anyString() ) );
107
108 if ( isset( $params['minsize'] ) ) {
109 $this->addWhere( 'img_size>=' . intval( $params['minsize'] ) );
110 }
111
112 if ( isset( $params['maxsize'] ) ) {
113 $this->addWhere( 'img_size<=' . intval( $params['maxsize'] ) );
114 }
115
116 $sha1 = false;
117 if ( isset( $params['sha1'] ) ) {
118 if ( !$this->validateSha1Hash( $params['sha1'] ) ) {
119 $this->dieUsage( 'The SHA1 hash provided is not valid', 'invalidsha1hash' );
120 }
121 $sha1 = wfBaseConvert( $params['sha1'], 16, 36, 31 );
122 } elseif ( isset( $params['sha1base36'] ) ) {
123 $sha1 = $params['sha1base36'];
124 if ( !$this->validateSha1Base36Hash( $sha1 ) ) {
125 $this->dieUsage( 'The SHA1Base36 hash provided is not valid', 'invalidsha1base36hash' );
126 }
127 }
128 if ( $sha1 ) {
129 $this->addWhereFld( 'img_sha1', $sha1 );
130 }
131
132 if ( !is_null( $params['mime'] ) ) {
133 global $wgMiserMode;
134 if ( $wgMiserMode ) {
135 $this->dieUsage( 'MIME search disabled in Miser Mode', 'mimesearchdisabled' );
136 }
137
138 list( $major, $minor ) = File::splitMime( $params['mime'] );
139
140 $this->addWhereFld( 'img_major_mime', $major );
141 $this->addWhereFld( 'img_minor_mime', $minor );
142 }
143
144 $this->addTables( 'image' );
145
146 $prop = array_flip( $params['prop'] );
147 $this->addFields( LocalFile::selectFields() );
148
149 $limit = $params['limit'];
150 $this->addOption( 'LIMIT', $limit + 1 );
151 $sort = ( $params['dir'] == 'descending' ? ' DESC' : '' );
152 $this->addOption( 'ORDER BY', 'img_name' . $sort );
153
154 $res = $this->select( __METHOD__ );
155
156 $titles = array();
157 $count = 0;
158 $result = $this->getResult();
159 foreach ( $res as $row ) {
160 if ( ++ $count > $limit ) {
161 // We've reached the one extra which shows that there are additional pages to be had. Stop here...
162 $this->setContinueEnumParameter( 'continue', $row->img_name );
163 break;
164 }
165
166 if ( is_null( $resultPageSet ) ) {
167 $file = $repo->newFileFromRow( $row );
168 $info = array_merge( array( 'name' => $row->img_name ),
169 ApiQueryImageInfo::getInfo( $file, $prop, $result ) );
170 self::addTitleInfo( $info, $file->getTitle() );
171
172 $fit = $result->addValue( array( 'query', $this->getModuleName() ), null, $info );
173 if ( !$fit ) {
174 $this->setContinueEnumParameter( 'continue', $row->img_name );
175 break;
176 }
177 } else {
178 $titles[] = Title::makeTitle( NS_FILE, $row->img_name );
179 }
180 }
181
182 if ( is_null( $resultPageSet ) ) {
183 $result->setIndexedTagName_internal( array( 'query', $this->getModuleName() ), 'img' );
184 } else {
185 $resultPageSet->populateFromTitles( $titles );
186 }
187 }
188
189 public function getAllowedParams() {
190 return array (
191 'from' => null,
192 'continue' => null,
193 'to' => null,
194 'prefix' => null,
195 'minsize' => array(
196 ApiBase::PARAM_TYPE => 'integer',
197 ),
198 'maxsize' => array(
199 ApiBase::PARAM_TYPE => 'integer',
200 ),
201 'limit' => array(
202 ApiBase::PARAM_DFLT => 10,
203 ApiBase::PARAM_TYPE => 'limit',
204 ApiBase::PARAM_MIN => 1,
205 ApiBase::PARAM_MAX => ApiBase::LIMIT_BIG1,
206 ApiBase::PARAM_MAX2 => ApiBase::LIMIT_BIG2
207 ),
208 'dir' => array(
209 ApiBase::PARAM_DFLT => 'ascending',
210 ApiBase::PARAM_TYPE => array(
211 'ascending',
212 'descending'
213 )
214 ),
215 'sha1' => null,
216 'sha1base36' => null,
217 'prop' => array(
218 ApiBase::PARAM_TYPE => ApiQueryImageInfo::getPropertyNames( $this->propertyFilter ),
219 ApiBase::PARAM_DFLT => 'timestamp|url',
220 ApiBase::PARAM_ISMULTI => true
221 ),
222 'mime' => null,
223 );
224 }
225
226 public function getParamDescription() {
227 return array(
228 'from' => 'The image title to start enumerating from',
229 'continue' => 'When more results are available, use this to continue',
230 'to' => 'The image title to stop enumerating at',
231 'prefix' => 'Search for all image titles that begin with this value',
232 'dir' => 'The direction in which to list',
233 'minsize' => 'Limit to images with at least this many bytes',
234 'maxsize' => 'Limit to images with at most this many bytes',
235 'limit' => 'How many images in total to return',
236 'sha1' => "SHA1 hash of image. Overrides {$this->getModulePrefix()}sha1base36",
237 'sha1base36' => 'SHA1 hash of image in base 36 (used in MediaWiki)',
238 'prop' => ApiQueryImageInfo::getPropertyDescriptions( $this->propertyFilter ),
239 'mime' => 'What MIME type to search for. e.g. image/jpeg. Disabled in Miser Mode',
240 );
241 }
242
243 private $propertyFilter = array( 'archivename', 'thumbmime' );
244
245 public function getResultProperties() {
246 return array_merge(
247 array(
248 '' => array(
249 'name' => 'string',
250 'ns' => 'namespace',
251 'title' => 'string'
252 )
253 ),
254 ApiQueryImageInfo::getResultPropertiesFiltered( $this->propertyFilter )
255 );
256 }
257
258 public function getDescription() {
259 return 'Enumerate all images sequentially';
260 }
261
262 public function getPossibleErrors() {
263 return array_merge( parent::getPossibleErrors(), array(
264 array( 'code' => 'params', 'info' => 'Use "gaifilterredir=nonredirects" option instead of "redirects" when using allimages as a generator' ),
265 array( 'code' => 'unsupportedrepo', 'info' => 'Local file repository does not support querying all images' ),
266 array( 'code' => 'mimesearchdisabled', 'info' => 'MIME search disabled in Miser Mode' ),
267 array( 'code' => 'invalidsha1hash', 'info' => 'The SHA1 hash provided is not valid' ),
268 array( 'code' => 'invalidsha1base36hash', 'info' => 'The SHA1Base36 hash provided is not valid' ),
269 array( 'code' => '_badcontinue', 'info' => 'Invalid continue param. You should pass the original value returned by the previous query' ),
270 ) );
271 }
272
273 public function getExamples() {
274 return array(
275 'api.php?action=query&list=allimages&aifrom=B' => array(
276 'Simple Use',
277 'Show a list of images starting at the letter "B"',
278 ),
279 'api.php?action=query&generator=allimages&gailimit=4&gaifrom=T&prop=imageinfo' => array(
280 'Using as Generator',
281 'Show info about 4 images starting at the letter "T"',
282 ),
283 );
284 }
285
286 public function getHelpUrls() {
287 return 'https://www.mediawiki.org/wiki/API:Allimages';
288 }
289
290 public function getVersion() {
291 return __CLASS__ . ': $Id$';
292 }
293 }