MediaWiki  master
ApiQueryAllImages.php
Go to the documentation of this file.
1 <?php
2 
33 
40 
44  protected $mRepo;
45 
47  private $groupPermissionsLookup;
48 
55  public function __construct(
56  ApiQuery $query,
57  $moduleName,
58  RepoGroup $repoGroup,
59  GroupPermissionsLookup $groupPermissionsLookup
60  ) {
61  parent::__construct( $query, $moduleName, 'ai' );
62  $this->mRepo = $repoGroup->getLocalRepo();
63  $this->groupPermissionsLookup = $groupPermissionsLookup;
64  }
65 
73  protected function getDB() {
74  return $this->mRepo->getReplicaDB();
75  }
76 
77  public function execute() {
78  $this->run();
79  }
80 
81  public function getCacheMode( $params ) {
82  return 'public';
83  }
84 
89  public function executeGenerator( $resultPageSet ) {
90  if ( $resultPageSet->isResolvingRedirects() ) {
91  $this->dieWithError( 'apierror-allimages-redirect', 'invalidparammix' );
92  }
93 
94  $this->run( $resultPageSet );
95  }
96 
101  private function run( $resultPageSet = null ) {
102  $repo = $this->mRepo;
103  if ( !$repo instanceof LocalRepo ) {
104  $this->dieWithError( 'apierror-unsupportedrepo' );
105  }
106 
107  $prefix = $this->getModulePrefix();
108 
109  $db = $this->getDB();
110 
111  $params = $this->extractRequestParams();
112 
113  // Table and return fields
114  $prop = array_fill_keys( $params['prop'], true );
115 
116  $fileQuery = LocalFile::getQueryInfo();
117  $this->addTables( $fileQuery['tables'] );
118  $this->addFields( $fileQuery['fields'] );
119  $this->addJoinConds( $fileQuery['joins'] );
120 
121  $ascendingOrder = true;
122  if ( $params['dir'] == 'descending' || $params['dir'] == 'older' ) {
123  $ascendingOrder = false;
124  }
125 
126  if ( $params['sort'] == 'name' ) {
127  // Check mutually exclusive params
128  $disallowed = [ 'start', 'end', 'user' ];
129  foreach ( $disallowed as $pname ) {
130  if ( isset( $params[$pname] ) ) {
131  $this->dieWithError(
132  [
133  'apierror-invalidparammix-mustusewith',
134  "{$prefix}{$pname}",
135  "{$prefix}sort=timestamp"
136  ],
137  'invalidparammix'
138  );
139  }
140  }
141  if ( $params['filterbots'] != 'all' ) {
142  $this->dieWithError(
143  [
144  'apierror-invalidparammix-mustusewith',
145  "{$prefix}filterbots",
146  "{$prefix}sort=timestamp"
147  ],
148  'invalidparammix'
149  );
150  }
151 
152  // Pagination
153  if ( $params['continue'] !== null ) {
154  $cont = $this->parseContinueParamOrDie( $params['continue'], [ 'string' ] );
155  $op = $ascendingOrder ? '>=' : '<=';
156  $this->addWhere( $db->buildComparison( $op, [ 'img_name' => $cont[0] ] ) );
157  }
158 
159  // Image filters
160  $from = $params['from'] === null ? null : $this->titlePartToKey( $params['from'], NS_FILE );
161  $to = $params['to'] === null ? null : $this->titlePartToKey( $params['to'], NS_FILE );
162  $this->addWhereRange( 'img_name', $ascendingOrder ? 'newer' : 'older', $from, $to );
163 
164  if ( isset( $params['prefix'] ) ) {
165  $this->addWhere( 'img_name' . $db->buildLike(
166  $this->titlePartToKey( $params['prefix'], NS_FILE ),
167  $db->anyString() ) );
168  }
169  } else {
170  // Check mutually exclusive params
171  $disallowed = [ 'from', 'to', 'prefix' ];
172  foreach ( $disallowed as $pname ) {
173  if ( isset( $params[$pname] ) ) {
174  $this->dieWithError(
175  [
176  'apierror-invalidparammix-mustusewith',
177  "{$prefix}{$pname}",
178  "{$prefix}sort=name"
179  ],
180  'invalidparammix'
181  );
182  }
183  }
184  if ( $params['user'] !== null && $params['filterbots'] != 'all' ) {
185  // Since filterbots checks if each user has the bot right, it
186  // doesn't make sense to use it with user
187  $this->dieWithError(
188  [ 'apierror-invalidparammix-cannotusewith', "{$prefix}user", "{$prefix}filterbots" ]
189  );
190  }
191 
192  // Pagination
193  $this->addTimestampWhereRange(
194  'img_timestamp',
195  $ascendingOrder ? 'newer' : 'older',
196  $params['start'],
197  $params['end']
198  );
199  // Include in ORDER BY for uniqueness
200  $this->addWhereRange( 'img_name', $ascendingOrder ? 'newer' : 'older', null, null );
201 
202  if ( $params['continue'] !== null ) {
203  $cont = $this->parseContinueParamOrDie( $params['continue'], [ 'timestamp', 'string' ] );
204  $op = ( $ascendingOrder ? '>=' : '<=' );
205  $this->addWhere( $db->buildComparison( $op, [
206  'img_timestamp' => $db->timestamp( $cont[0] ),
207  'img_name' => $cont[1],
208  ] ) );
209  }
210 
211  // Image filters
212  if ( $params['user'] !== null ) {
213  $this->addWhereFld( $fileQuery['fields']['img_user_text'], $params['user'] );
214  }
215  if ( $params['filterbots'] != 'all' ) {
216  $this->addTables( 'user_groups' );
217  $this->addJoinConds( [ 'user_groups' => [
218  'LEFT JOIN',
219  [
220  'ug_group' => $this->groupPermissionsLookup->getGroupsWithPermission( 'bot' ),
221  'ug_user = actor_user',
222  'ug_expiry IS NULL OR ug_expiry >= ' . $db->addQuotes( $db->timestamp() )
223  ]
224  ] ] );
225  $groupCond = $params['filterbots'] == 'nobots' ? 'NULL' : 'NOT NULL';
226  $this->addWhere( "ug_group IS $groupCond" );
227  }
228  }
229 
230  // Filters not depending on sort
231  if ( isset( $params['minsize'] ) ) {
232  $this->addWhere( 'img_size>=' . (int)$params['minsize'] );
233  }
234 
235  if ( isset( $params['maxsize'] ) ) {
236  $this->addWhere( 'img_size<=' . (int)$params['maxsize'] );
237  }
238 
239  $sha1 = false;
240  if ( isset( $params['sha1'] ) ) {
241  $sha1 = strtolower( $params['sha1'] );
242  if ( !$this->validateSha1Hash( $sha1 ) ) {
243  $this->dieWithError( 'apierror-invalidsha1hash' );
244  }
245  $sha1 = Wikimedia\base_convert( $sha1, 16, 36, 31 );
246  } elseif ( isset( $params['sha1base36'] ) ) {
247  $sha1 = strtolower( $params['sha1base36'] );
248  if ( !$this->validateSha1Base36Hash( $sha1 ) ) {
249  $this->dieWithError( 'apierror-invalidsha1base36hash' );
250  }
251  }
252  if ( $sha1 ) {
253  $this->addWhereFld( 'img_sha1', $sha1 );
254  }
255 
256  if ( $params['mime'] !== null ) {
257  if ( $this->getConfig()->get( MainConfigNames::MiserMode ) ) {
258  $this->dieWithError( 'apierror-mimesearchdisabled' );
259  }
260 
261  $mimeConds = [];
262  foreach ( $params['mime'] as $mime ) {
263  [ $major, $minor ] = File::splitMime( $mime );
264  $mimeConds[] = $db->makeList(
265  [
266  'img_major_mime' => $major,
267  'img_minor_mime' => $minor,
268  ],
269  LIST_AND
270  );
271  }
272  // safeguard against internal_api_error_DBQueryError
273  if ( count( $mimeConds ) > 0 ) {
274  $this->addWhere( $db->makeList( $mimeConds, LIST_OR ) );
275  } else {
276  // no MIME types, no files
277  $this->getResult()->addValue( 'query', $this->getModuleName(), [] );
278  return;
279  }
280  }
281 
282  $limit = $params['limit'];
283  $this->addOption( 'LIMIT', $limit + 1 );
284 
285  $res = $this->select( __METHOD__ );
286 
287  $titles = [];
288  $count = 0;
289  $result = $this->getResult();
290  foreach ( $res as $row ) {
291  if ( ++$count > $limit ) {
292  // We've reached the one extra which shows that there are
293  // additional pages to be had. Stop here...
294  if ( $params['sort'] == 'name' ) {
295  $this->setContinueEnumParameter( 'continue', $row->img_name );
296  } else {
297  $this->setContinueEnumParameter( 'continue', "$row->img_timestamp|$row->img_name" );
298  }
299  break;
300  }
301 
302  if ( $resultPageSet === null ) {
303  $file = $repo->newFileFromRow( $row );
304  $info = array_merge( [ 'name' => $row->img_name ],
305  ApiQueryImageInfo::getInfo( $file, $prop, $result ) );
306  self::addTitleInfo( $info, $file->getTitle() );
307 
308  $fit = $result->addValue( [ 'query', $this->getModuleName() ], null, $info );
309  if ( !$fit ) {
310  if ( $params['sort'] == 'name' ) {
311  $this->setContinueEnumParameter( 'continue', $row->img_name );
312  } else {
313  $this->setContinueEnumParameter( 'continue', "$row->img_timestamp|$row->img_name" );
314  }
315  break;
316  }
317  } else {
318  $titles[] = Title::makeTitle( NS_FILE, $row->img_name );
319  }
320  }
321 
322  if ( $resultPageSet === null ) {
323  $result->addIndexedTagName( [ 'query', $this->getModuleName() ], 'img' );
324  } else {
325  $resultPageSet->populateFromTitles( $titles );
326  }
327  }
328 
329  public function getAllowedParams() {
330  $ret = [
331  'sort' => [
332  ParamValidator::PARAM_DEFAULT => 'name',
333  ParamValidator::PARAM_TYPE => [
334  'name',
335  'timestamp'
336  ]
337  ],
338  'dir' => [
339  ParamValidator::PARAM_DEFAULT => 'ascending',
340  ParamValidator::PARAM_TYPE => [
341  // sort=name
342  'ascending',
343  'descending',
344  // sort=timestamp
345  'newer',
346  'older'
347  ]
348  ],
349  'from' => null,
350  'to' => null,
351  'continue' => [
352  ApiBase::PARAM_HELP_MSG => 'api-help-param-continue',
353  ],
354  'start' => [
355  ParamValidator::PARAM_TYPE => 'timestamp'
356  ],
357  'end' => [
358  ParamValidator::PARAM_TYPE => 'timestamp'
359  ],
360  'prop' => [
361  ParamValidator::PARAM_TYPE => ApiQueryImageInfo::getPropertyNames( $this->propertyFilter ),
362  ParamValidator::PARAM_DEFAULT => 'timestamp|url',
363  ParamValidator::PARAM_ISMULTI => true,
364  ApiBase::PARAM_HELP_MSG => 'apihelp-query+imageinfo-param-prop',
366  ApiQueryImageInfo::getPropertyMessages( $this->propertyFilter ),
367  ],
368  'prefix' => null,
369  'minsize' => [
370  ParamValidator::PARAM_TYPE => 'integer',
371  ],
372  'maxsize' => [
373  ParamValidator::PARAM_TYPE => 'integer',
374  ],
375  'sha1' => null,
376  'sha1base36' => null,
377  'user' => [
378  ParamValidator::PARAM_TYPE => 'user',
379  UserDef::PARAM_ALLOWED_USER_TYPES => [ 'name', 'ip', 'id', 'interwiki' ],
380  ],
381  'filterbots' => [
382  ParamValidator::PARAM_DEFAULT => 'all',
383  ParamValidator::PARAM_TYPE => [
384  'all',
385  'bots',
386  'nobots'
387  ]
388  ],
389  'mime' => [
390  ParamValidator::PARAM_ISMULTI => true,
391  ],
392  'limit' => [
393  ParamValidator::PARAM_DEFAULT => 10,
394  ParamValidator::PARAM_TYPE => 'limit',
395  IntegerDef::PARAM_MIN => 1,
396  IntegerDef::PARAM_MAX => ApiBase::LIMIT_BIG1,
397  IntegerDef::PARAM_MAX2 => ApiBase::LIMIT_BIG2
398  ],
399  ];
400 
401  if ( $this->getConfig()->get( MainConfigNames::MiserMode ) ) {
402  $ret['mime'][ApiBase::PARAM_HELP_MSG] = 'api-help-param-disabled-in-miser-mode';
403  }
404 
405  return $ret;
406  }
407 
408  private $propertyFilter = [ 'archivename', 'thumbmime', 'uploadwarning' ];
409 
410  protected function getExamplesMessages() {
411  return [
412  'action=query&list=allimages&aifrom=B'
413  => 'apihelp-query+allimages-example-b',
414  'action=query&list=allimages&aiprop=user|timestamp|url&' .
415  'aisort=timestamp&aidir=older'
416  => 'apihelp-query+allimages-example-recent',
417  'action=query&list=allimages&aimime=image/png|image/gif'
418  => 'apihelp-query+allimages-example-mimetypes',
419  'action=query&generator=allimages&gailimit=4&' .
420  'gaifrom=T&prop=imageinfo'
421  => 'apihelp-query+allimages-example-generator',
422  ];
423  }
424 
425  public function getHelpUrls() {
426  return 'https://www.mediawiki.org/wiki/Special:MyLanguage/API:Allimages';
427  }
428 }
const NS_FILE
Definition: Defines.php:70
const LIST_OR
Definition: Defines.php:46
const LIST_AND
Definition: Defines.php:43
dieWithError( $msg, $code=null, $data=null, $httpCode=0)
Abort execution with an error.
Definition: ApiBase.php:1455
getModulePrefix()
Get parameter prefix (usually two letters or an empty string).
Definition: ApiBase.php:507
parseContinueParamOrDie(string $continue, array $types)
Parse the 'continue' parameter in the usual format and validate the types of each part,...
Definition: ApiBase.php:1644
const PARAM_HELP_MSG_PER_VALUE
((string|array|Message)[]) When PARAM_TYPE is an array, this is an array mapping those values to $msg...
Definition: ApiBase.php:197
const LIMIT_BIG1
Fast query, standard limit.
Definition: ApiBase.php:222
getResult()
Get the result object.
Definition: ApiBase.php:630
extractRequestParams( $options=[])
Using getAllowedParams(), this function makes an array of the values provided by the user,...
Definition: ApiBase.php:766
const PARAM_HELP_MSG
(string|array|Message) Specify an alternative i18n documentation message for this parameter.
Definition: ApiBase.php:164
const LIMIT_BIG2
Fast query, apihighlimits limit.
Definition: ApiBase.php:224
getModuleName()
Get the name of the module being executed by this instance.
Definition: ApiBase.php:499
Query module to enumerate all images.
getExamplesMessages()
Returns usage examples for this module.
getCacheMode( $params)
Get the cache mode for the data generated by this module.
getDB()
Override parent method to make sure the repo's DB is used which may not necessarily be the same as th...
executeGenerator( $resultPageSet)
getAllowedParams()
Returns an array of allowed parameters (parameter name) => (default value) or (parameter name) => (ar...
execute()
Evaluates the parameters, performs the requested query, and sets up the result.
__construct(ApiQuery $query, $moduleName, RepoGroup $repoGroup, GroupPermissionsLookup $groupPermissionsLookup)
getHelpUrls()
Return links to more detailed help pages about the module.
static addTitleInfo(&$arr, $title, $prefix='')
Add information (title and namespace) about a Title object to a result array.
validateSha1Base36Hash( $hash)
addWhereRange( $field, $dir, $start, $end, $sort=true)
Add a WHERE clause corresponding to a range, and an ORDER BY clause to sort in the right direction.
validateSha1Hash( $hash)
addFields( $value)
Add a set of fields to select to the internal array.
addOption( $name, $value=null)
Add an option such as LIMIT or USE INDEX.
addTables( $tables, $alias=null)
Add a set of tables to the internal array.
addTimestampWhereRange( $field, $dir, $start, $end, $sort=true)
Add a WHERE clause corresponding to a range, similar to addWhereRange, but converts $start and $end t...
select( $method, $extraQuery=[], array &$hookData=null)
Execute a SELECT query based on the values in the internal arrays.
addJoinConds( $join_conds)
Add a set of JOIN conditions to the internal array.
addWhereFld( $field, $value)
Equivalent to addWhere( [ $field => $value ] )
titlePartToKey( $titlePart, $namespace=NS_MAIN)
Convert an input title or title prefix into a dbkey.
addWhere( $value)
Add a set of WHERE clauses to the internal array.
setContinueEnumParameter( $paramName, $paramValue)
Overridden to set the generator param if in generator mode.
static getPropertyNames( $filter=[])
Returns all possible parameters to iiprop.
static getInfo( $file, $prop, $result, $thumbParams=null, $opts=false)
Get result information for an image revision.
static getPropertyMessages( $filter=[])
Returns messages for all possible parameters to iiprop.
This is the main query class.
Definition: ApiQuery.php:41
static splitMime(?string $mime)
Split an internet media type into its two components; if not a two-part name, set the minor type to '...
Definition: File.php:306
static getQueryInfo(array $options=[])
Return the tables, fields, and join conditions to be selected to create a new localfile object.
Definition: LocalFile.php:270
Local repository that stores files in the local filesystem and registers them in the wiki's own datab...
Definition: LocalRepo.php:39
A class containing constants representing the names of configuration variables.
Prioritized list of file repositories.
Definition: RepoGroup.php:29
getLocalRepo()
Get the local repository, i.e.
Definition: RepoGroup.php:342
static makeTitle( $ns, $title, $fragment='', $interwiki='')
Create a new Title from a namespace index and a DB key.
Definition: Title.php:641
Service for formatting and validating API parameters.
Type definition for integer types.
Definition: IntegerDef.php:23
Basic database interface for live and lazy-loaded relation database handles.
Definition: IDatabase.php:40
$mime
Definition: router.php:60
if(PHP_SAPI !='cli-server') if(!isset( $_SERVER['SCRIPT_FILENAME'])) $file
Item class for a filearchive table row.
Definition: router.php:42