Added release notes for 'ContentHandler::runLegacyHooks' removal
[mediawiki.git] / includes / api / ApiQueryAllImages.php
blobe3e5ed6c9f48cf48c8115e287fa339c411886a4e
1 <?php
3 /**
4 * API for MediaWiki 1.12+
6 * Created on Mar 16, 2008
8 * Copyright © 2008 Vasiliev Victor vasilvv@gmail.com,
9 * based on ApiQueryAllPages.php
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.
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.
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
26 * @file
29 /**
30 * Query module to enumerate all available pages.
32 * @ingroup API
34 class ApiQueryAllImages extends ApiQueryGeneratorBase {
35 protected $mRepo;
37 public function __construct( ApiQuery $query, $moduleName ) {
38 parent::__construct( $query, $moduleName, 'ai' );
39 $this->mRepo = RepoGroup::singleton()->getLocalRepo();
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.
46 * TODO: allow querying non-local repos.
47 * @return IDatabase
49 protected function getDB() {
50 return $this->mRepo->getReplicaDB();
53 public function execute() {
54 $this->run();
57 public function getCacheMode( $params ) {
58 return 'public';
61 /**
62 * @param ApiPageSet $resultPageSet
63 * @return void
65 public function executeGenerator( $resultPageSet ) {
66 if ( $resultPageSet->isResolvingRedirects() ) {
67 $this->dieWithError( 'apierror-allimages-redirect', 'invalidparammix' );
70 $this->run( $resultPageSet );
73 /**
74 * @param ApiPageSet $resultPageSet
75 * @return void
77 private function run( $resultPageSet = null ) {
78 $repo = $this->mRepo;
79 if ( !$repo instanceof LocalRepo ) {
80 $this->dieWithError( 'apierror-unsupportedrepo' );
83 $prefix = $this->getModulePrefix();
85 $db = $this->getDB();
87 $params = $this->extractRequestParams();
89 // Table and return fields
90 $this->addTables( 'image' );
92 $prop = array_flip( $params['prop'] );
93 $this->addFields( LocalFile::selectFields() );
95 $ascendingOrder = true;
96 if ( $params['dir'] == 'descending' || $params['dir'] == 'older' ) {
97 $ascendingOrder = false;
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(
107 'apierror-invalidparammix-mustusewith',
108 "{$prefix}{$pname}",
109 "{$prefix}sort=timestamp"
111 'invalidparammix'
115 if ( $params['filterbots'] != 'all' ) {
116 $this->dieWithError(
118 'apierror-invalidparammix-mustusewith',
119 "{$prefix}filterbots",
120 "{$prefix}sort=timestamp"
122 'invalidparammix'
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" );
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 );
140 if ( isset( $params['prefix'] ) ) {
141 $this->addWhere( 'img_name' . $db->buildLike(
142 $this->titlePartToKey( $params['prefix'], NS_FILE ),
143 $db->anyString() ) );
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(
152 'apierror-invalidparammix-mustusewith',
153 "{$prefix}{$pname}",
154 "{$prefix}sort=name"
156 'invalidparammix'
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" ]
168 // Pagination
169 $this->addTimestampWhereRange(
170 'img_timestamp',
171 $ascendingOrder ? 'newer' : 'older',
172 $params['start'],
173 $params['end']
175 // Include in ORDER BY for uniqueness
176 $this->addWhereRange( 'img_name', $ascendingOrder ? 'newer' : 'older', null, null );
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)"
190 // Image filters
191 if ( !is_null( $params['user'] ) ) {
192 $this->addWhereFld( 'img_user_text', $params['user'] );
194 if ( $params['filterbots'] != 'all' ) {
195 $this->addTables( 'user_groups' );
196 $this->addJoinConds( [ 'user_groups' => [
197 'LEFT JOIN',
199 'ug_group' => User::getGroupsWithPermission( 'bot' ),
200 'ug_user = img_user'
202 ] ] );
203 $groupCond = ( $params['filterbots'] == 'nobots' ? 'NULL' : 'NOT NULL' );
204 $this->addWhere( "ug_group IS $groupCond" );
208 // Filters not depending on sort
209 if ( isset( $params['minsize'] ) ) {
210 $this->addWhere( 'img_size>=' . intval( $params['minsize'] ) );
213 if ( isset( $params['maxsize'] ) ) {
214 $this->addWhere( 'img_size<=' . intval( $params['maxsize'] ) );
217 $sha1 = false;
218 if ( isset( $params['sha1'] ) ) {
219 $sha1 = strtolower( $params['sha1'] );
220 if ( !$this->validateSha1Hash( $sha1 ) ) {
221 $this->dieWithError( 'apierror-invalidsha1hash' );
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' );
230 if ( $sha1 ) {
231 $this->addWhereFld( 'img_sha1', $sha1 );
234 if ( !is_null( $params['mime'] ) ) {
235 if ( $this->getConfig()->get( 'MiserMode' ) ) {
236 $this->dieWithError( 'apierror-mimesearchdisabled' );
239 $mimeConds = [];
240 foreach ( $params['mime'] as $mime ) {
241 list( $major, $minor ) = File::splitMime( $mime );
242 $mimeConds[] = $db->makeList(
244 'img_major_mime' => $major,
245 'img_minor_mime' => $minor,
247 LIST_AND
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;
260 $limit = $params['limit'];
261 $this->addOption( 'LIMIT', $limit + 1 );
262 $sortFlag = '';
263 if ( !$ascendingOrder ) {
264 $sortFlag = ' DESC';
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' ] );
273 } else {
274 $this->addOption( 'ORDER BY', 'img_name' . $sortFlag );
277 $res = $this->select( __METHOD__ );
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" );
291 break;
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() );
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" );
307 break;
309 } else {
310 $titles[] = Title::makeTitle( NS_FILE, $row->img_name );
314 if ( is_null( $resultPageSet ) ) {
315 $result->addIndexedTagName( [ 'query', $this->getModuleName() ], 'img' );
316 } else {
317 $resultPageSet->populateFromTitles( $titles );
321 public function getAllowedParams() {
322 $ret = [
323 'sort' => [
324 ApiBase::PARAM_DFLT => 'name',
325 ApiBase::PARAM_TYPE => [
326 'name',
327 'timestamp'
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'
341 'from' => null,
342 'to' => null,
343 'continue' => [
344 ApiBase::PARAM_HELP_MSG => 'api-help-param-continue',
346 'start' => [
347 ApiBase::PARAM_TYPE => 'timestamp'
349 'end' => [
350 ApiBase::PARAM_TYPE => 'timestamp'
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 ),
360 'prefix' => null,
361 'minsize' => [
362 ApiBase::PARAM_TYPE => 'integer',
364 'maxsize' => [
365 ApiBase::PARAM_TYPE => 'integer',
367 'sha1' => null,
368 'sha1base36' => null,
369 'user' => [
370 ApiBase::PARAM_TYPE => 'user'
372 'filterbots' => [
373 ApiBase::PARAM_DFLT => 'all',
374 ApiBase::PARAM_TYPE => [
375 'all',
376 'bots',
377 'nobots'
380 'mime' => [
381 ApiBase::PARAM_ISMULTI => true,
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
392 if ( $this->getConfig()->get( 'MiserMode' ) ) {
393 $ret['mime'][ApiBase::PARAM_HELP_MSG] = 'api-help-param-disabled-in-miser-mode';
396 return $ret;
399 private $propertyFilter = [ 'archivename', 'thumbmime', 'uploadwarning' ];
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',
416 public function getHelpUrls() {
417 return 'https://www.mediawiki.org/wiki/API:Allimages';