Merge "Add CollationFa"
[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 protected $mRepo;
36
37 public function __construct( ApiQuery $query, $moduleName ) {
38 parent::__construct( $query, $moduleName, 'ai' );
39 $this->mRepo = RepoGroup::singleton()->getLocalRepo();
40 }
41
42 /**
43 * Override parent method to make sure the repo's DB is used
44 * which may not necessarily be the same as the local DB.
45 *
46 * TODO: allow querying non-local repos.
47 * @return IDatabase
48 */
49 protected function getDB() {
50 return $this->mRepo->getReplicaDB();
51 }
52
53 public function execute() {
54 $this->run();
55 }
56
57 public function getCacheMode( $params ) {
58 return 'public';
59 }
60
61 /**
62 * @param ApiPageSet $resultPageSet
63 * @return void
64 */
65 public function executeGenerator( $resultPageSet ) {
66 if ( $resultPageSet->isResolvingRedirects() ) {
67 $this->dieWithError( 'apierror-allimages-redirect', 'invalidparammix' );
68 }
69
70 $this->run( $resultPageSet );
71 }
72
73 /**
74 * @param ApiPageSet $resultPageSet
75 * @return void
76 */
77 private function run( $resultPageSet = null ) {
78 $repo = $this->mRepo;
79 if ( !$repo instanceof LocalRepo ) {
80 $this->dieWithError( 'apierror-unsupportedrepo' );
81 }
82
83 $prefix = $this->getModulePrefix();
84
85 $db = $this->getDB();
86
87 $params = $this->extractRequestParams();
88
89 // Table and return fields
90 $this->addTables( 'image' );
91
92 $prop = array_flip( $params['prop'] );
93 $this->addFields( LocalFile::selectFields() );
94
95 $ascendingOrder = true;
96 if ( $params['dir'] == 'descending' || $params['dir'] == 'older' ) {
97 $ascendingOrder = false;
98 }
99
100 if ( $params['sort'] == 'name' ) {
101 // Check mutually exclusive params
102 $disallowed = [ 'start', 'end', 'user' ];
103 foreach ( $disallowed as $pname ) {
104 if ( isset( $params[$pname] ) ) {
105 $this->dieWithError(
106 [
107 'apierror-invalidparammix-mustusewith',
108 "{$prefix}{$pname}",
109 "{$prefix}sort=timestamp"
110 ],
111 'invalidparammix'
112 );
113 }
114 }
115 if ( $params['filterbots'] != 'all' ) {
116 $this->dieWithError(
117 [
118 'apierror-invalidparammix-mustusewith',
119 "{$prefix}filterbots",
120 "{$prefix}sort=timestamp"
121 ],
122 'invalidparammix'
123 );
124 }
125
126 // Pagination
127 if ( !is_null( $params['continue'] ) ) {
128 $cont = explode( '|', $params['continue'] );
129 $this->dieContinueUsageIf( count( $cont ) != 1 );
130 $op = ( $ascendingOrder ? '>' : '<' );
131 $continueFrom = $db->addQuotes( $cont[0] );
132 $this->addWhere( "img_name $op= $continueFrom" );
133 }
134
135 // Image filters
136 $from = ( $params['from'] === null ? null : $this->titlePartToKey( $params['from'], NS_FILE ) );
137 $to = ( $params['to'] === null ? null : $this->titlePartToKey( $params['to'], NS_FILE ) );
138 $this->addWhereRange( 'img_name', ( $ascendingOrder ? 'newer' : 'older' ), $from, $to );
139
140 if ( isset( $params['prefix'] ) ) {
141 $this->addWhere( 'img_name' . $db->buildLike(
142 $this->titlePartToKey( $params['prefix'], NS_FILE ),
143 $db->anyString() ) );
144 }
145 } else {
146 // Check mutually exclusive params
147 $disallowed = [ 'from', 'to', 'prefix' ];
148 foreach ( $disallowed as $pname ) {
149 if ( isset( $params[$pname] ) ) {
150 $this->dieWithError(
151 [
152 'apierror-invalidparammix-mustusewith',
153 "{$prefix}{$pname}",
154 "{$prefix}sort=name"
155 ],
156 'invalidparammix'
157 );
158 }
159 }
160 if ( !is_null( $params['user'] ) && $params['filterbots'] != 'all' ) {
161 // Since filterbots checks if each user has the bot right, it
162 // doesn't make sense to use it with user
163 $this->dieWithError(
164 [ 'apierror-invalidparammix-cannotusewith', "{$prefix}user", "{$prefix}filterbots" ]
165 );
166 }
167
168 // Pagination
169 $this->addTimestampWhereRange(
170 'img_timestamp',
171 $ascendingOrder ? 'newer' : 'older',
172 $params['start'],
173 $params['end']
174 );
175 // Include in ORDER BY for uniqueness
176 $this->addWhereRange( 'img_name', $ascendingOrder ? 'newer' : 'older', null, null );
177
178 if ( !is_null( $params['continue'] ) ) {
179 $cont = explode( '|', $params['continue'] );
180 $this->dieContinueUsageIf( count( $cont ) != 2 );
181 $op = ( $ascendingOrder ? '>' : '<' );
182 $continueTimestamp = $db->addQuotes( $db->timestamp( $cont[0] ) );
183 $continueName = $db->addQuotes( $cont[1] );
184 $this->addWhere( "img_timestamp $op $continueTimestamp OR " .
185 "(img_timestamp = $continueTimestamp AND " .
186 "img_name $op= $continueName)"
187 );
188 }
189
190 // Image filters
191 if ( !is_null( $params['user'] ) ) {
192 $this->addWhereFld( 'img_user_text', $params['user'] );
193 }
194 if ( $params['filterbots'] != 'all' ) {
195 $this->addTables( 'user_groups' );
196 $this->addJoinConds( [ 'user_groups' => [
197 'LEFT JOIN',
198 [
199 'ug_group' => User::getGroupsWithPermission( 'bot' ),
200 'ug_user = img_user'
201 ]
202 ] ] );
203 $groupCond = ( $params['filterbots'] == 'nobots' ? 'NULL' : 'NOT NULL' );
204 $this->addWhere( "ug_group IS $groupCond" );
205 }
206 }
207
208 // Filters not depending on sort
209 if ( isset( $params['minsize'] ) ) {
210 $this->addWhere( 'img_size>=' . intval( $params['minsize'] ) );
211 }
212
213 if ( isset( $params['maxsize'] ) ) {
214 $this->addWhere( 'img_size<=' . intval( $params['maxsize'] ) );
215 }
216
217 $sha1 = false;
218 if ( isset( $params['sha1'] ) ) {
219 $sha1 = strtolower( $params['sha1'] );
220 if ( !$this->validateSha1Hash( $sha1 ) ) {
221 $this->dieWithError( 'apierror-invalidsha1hash' );
222 }
223 $sha1 = Wikimedia\base_convert( $sha1, 16, 36, 31 );
224 } elseif ( isset( $params['sha1base36'] ) ) {
225 $sha1 = strtolower( $params['sha1base36'] );
226 if ( !$this->validateSha1Base36Hash( $sha1 ) ) {
227 $this->dieWithError( 'apierror-invalidsha1base36hash' );
228 }
229 }
230 if ( $sha1 ) {
231 $this->addWhereFld( 'img_sha1', $sha1 );
232 }
233
234 if ( !is_null( $params['mime'] ) ) {
235 if ( $this->getConfig()->get( 'MiserMode' ) ) {
236 $this->dieWithError( 'apierror-mimesearchdisabled' );
237 }
238
239 $mimeConds = [];
240 foreach ( $params['mime'] as $mime ) {
241 list( $major, $minor ) = File::splitMime( $mime );
242 $mimeConds[] = $db->makeList(
243 [
244 'img_major_mime' => $major,
245 'img_minor_mime' => $minor,
246 ],
247 LIST_AND
248 );
249 }
250 // safeguard against internal_api_error_DBQueryError
251 if ( count( $mimeConds ) > 0 ) {
252 $this->addWhere( $db->makeList( $mimeConds, LIST_OR ) );
253 } else {
254 // no MIME types, no files
255 $this->getResult()->addValue( 'query', $this->getModuleName(), [] );
256 return;
257 }
258 }
259
260 $limit = $params['limit'];
261 $this->addOption( 'LIMIT', $limit + 1 );
262 $sortFlag = '';
263 if ( !$ascendingOrder ) {
264 $sortFlag = ' DESC';
265 }
266 if ( $params['sort'] == 'timestamp' ) {
267 $this->addOption( 'ORDER BY', 'img_timestamp' . $sortFlag );
268 if ( !is_null( $params['user'] ) ) {
269 $this->addOption( 'USE INDEX', [ 'image' => 'img_usertext_timestamp' ] );
270 } else {
271 $this->addOption( 'USE INDEX', [ 'image' => 'img_timestamp' ] );
272 }
273 } else {
274 $this->addOption( 'ORDER BY', 'img_name' . $sortFlag );
275 }
276
277 $res = $this->select( __METHOD__ );
278
279 $titles = [];
280 $count = 0;
281 $result = $this->getResult();
282 foreach ( $res as $row ) {
283 if ( ++$count > $limit ) {
284 // We've reached the one extra which shows that there are
285 // additional pages to be had. Stop here...
286 if ( $params['sort'] == 'name' ) {
287 $this->setContinueEnumParameter( 'continue', $row->img_name );
288 } else {
289 $this->setContinueEnumParameter( 'continue', "$row->img_timestamp|$row->img_name" );
290 }
291 break;
292 }
293
294 if ( is_null( $resultPageSet ) ) {
295 $file = $repo->newFileFromRow( $row );
296 $info = array_merge( [ 'name' => $row->img_name ],
297 ApiQueryImageInfo::getInfo( $file, $prop, $result ) );
298 self::addTitleInfo( $info, $file->getTitle() );
299
300 $fit = $result->addValue( [ 'query', $this->getModuleName() ], null, $info );
301 if ( !$fit ) {
302 if ( $params['sort'] == 'name' ) {
303 $this->setContinueEnumParameter( 'continue', $row->img_name );
304 } else {
305 $this->setContinueEnumParameter( 'continue', "$row->img_timestamp|$row->img_name" );
306 }
307 break;
308 }
309 } else {
310 $titles[] = Title::makeTitle( NS_FILE, $row->img_name );
311 }
312 }
313
314 if ( is_null( $resultPageSet ) ) {
315 $result->addIndexedTagName( [ 'query', $this->getModuleName() ], 'img' );
316 } else {
317 $resultPageSet->populateFromTitles( $titles );
318 }
319 }
320
321 public function getAllowedParams() {
322 $ret = [
323 'sort' => [
324 ApiBase::PARAM_DFLT => 'name',
325 ApiBase::PARAM_TYPE => [
326 'name',
327 'timestamp'
328 ]
329 ],
330 'dir' => [
331 ApiBase::PARAM_DFLT => 'ascending',
332 ApiBase::PARAM_TYPE => [
333 // sort=name
334 'ascending',
335 'descending',
336 // sort=timestamp
337 'newer',
338 'older'
339 ]
340 ],
341 'from' => null,
342 'to' => null,
343 'continue' => [
344 ApiBase::PARAM_HELP_MSG => 'api-help-param-continue',
345 ],
346 'start' => [
347 ApiBase::PARAM_TYPE => 'timestamp'
348 ],
349 'end' => [
350 ApiBase::PARAM_TYPE => 'timestamp'
351 ],
352 'prop' => [
353 ApiBase::PARAM_TYPE => ApiQueryImageInfo::getPropertyNames( $this->propertyFilter ),
354 ApiBase::PARAM_DFLT => 'timestamp|url',
355 ApiBase::PARAM_ISMULTI => true,
356 ApiBase::PARAM_HELP_MSG => 'apihelp-query+imageinfo-param-prop',
357 ApiBase::PARAM_HELP_MSG_PER_VALUE =>
358 ApiQueryImageInfo::getPropertyMessages( $this->propertyFilter ),
359 ],
360 'prefix' => null,
361 'minsize' => [
362 ApiBase::PARAM_TYPE => 'integer',
363 ],
364 'maxsize' => [
365 ApiBase::PARAM_TYPE => 'integer',
366 ],
367 'sha1' => null,
368 'sha1base36' => null,
369 'user' => [
370 ApiBase::PARAM_TYPE => 'user'
371 ],
372 'filterbots' => [
373 ApiBase::PARAM_DFLT => 'all',
374 ApiBase::PARAM_TYPE => [
375 'all',
376 'bots',
377 'nobots'
378 ]
379 ],
380 'mime' => [
381 ApiBase::PARAM_ISMULTI => true,
382 ],
383 'limit' => [
384 ApiBase::PARAM_DFLT => 10,
385 ApiBase::PARAM_TYPE => 'limit',
386 ApiBase::PARAM_MIN => 1,
387 ApiBase::PARAM_MAX => ApiBase::LIMIT_BIG1,
388 ApiBase::PARAM_MAX2 => ApiBase::LIMIT_BIG2
389 ],
390 ];
391
392 if ( $this->getConfig()->get( 'MiserMode' ) ) {
393 $ret['mime'][ApiBase::PARAM_HELP_MSG] = 'api-help-param-disabled-in-miser-mode';
394 }
395
396 return $ret;
397 }
398
399 private $propertyFilter = [ 'archivename', 'thumbmime', 'uploadwarning' ];
400
401 protected function getExamplesMessages() {
402 return [
403 'action=query&list=allimages&aifrom=B'
404 => 'apihelp-query+allimages-example-B',
405 'action=query&list=allimages&aiprop=user|timestamp|url&' .
406 'aisort=timestamp&aidir=older'
407 => 'apihelp-query+allimages-example-recent',
408 'action=query&list=allimages&aimime=image/png|image/gif'
409 => 'apihelp-query+allimages-example-mimetypes',
410 'action=query&generator=allimages&gailimit=4&' .
411 'gaifrom=T&prop=imageinfo'
412 => 'apihelp-query+allimages-example-generator',
413 ];
414 }
415
416 public function getHelpUrls() {
417 return 'https://www.mediawiki.org/wiki/API:Allimages';
418 }
419 }