MediaWiki master
ApiQueryImages.php
Go to the documentation of this file.
1<?php
9namespace MediaWiki\Api;
10
17
25
26 private LinksMigration $linksMigration;
27
28 public function __construct( ApiQuery $query, string $moduleName, LinksMigration $linksMigration ) {
29 parent::__construct( $query, $moduleName, 'im' );
30 $this->linksMigration = $linksMigration;
31 }
32
33 public function execute() {
34 $this->run();
35 }
36
38 public function executeGenerator( $resultPageSet ) {
39 $this->run( $resultPageSet );
40 }
41
45 private function run( $resultPageSet = null ) {
46 $pages = $this->getPageSet()->getGoodPages();
47 if ( $pages === [] ) {
48 return; // nothing to do
49 }
50
51 $params = $this->extractRequestParams();
52
53 $migrationStage = $this->getConfig()->get( MainConfigNames::ImageLinksSchemaMigrationStage );
54 $queryInfo = $this->linksMigration->getQueryInfo( 'imagelinks' );
55
56 $this->addTables( $queryInfo['tables'] );
57
58 if ( $migrationStage & SCHEMA_COMPAT_READ_NEW ) {
59 $this->addFields( [ 'il_from', 'il_to' => 'lt_title' ] );
60 $this->addJoinConds( $queryInfo['joins'] );
61 } else {
62 $this->addFields( [ 'il_from', 'il_to' ] );
63 }
64
65 $this->addWhereFld( 'il_from', array_keys( $pages ) );
66 if ( $params['continue'] !== null ) {
67 $cont = $this->parseContinueParamOrDie( $params['continue'], [ 'int', 'string' ] );
68 $op = $params['dir'] == 'descending' ? '<=' : '>=';
69 $comparison = [ 'il_from' => $cont[0] ];
70 if ( $migrationStage & SCHEMA_COMPAT_READ_NEW ) {
71 $comparison['lt_namespace'] = NS_FILE;
72 $comparison['lt_title'] = $cont[1];
73 } else {
74 $comparison['il_to'] = $cont[1];
75 }
76 $this->addWhere( $this->getDB()->buildComparison( $op, $comparison ) );
77 }
78
79 $sort = ( $params['dir'] == 'descending' ? ' DESC' : '' );
80 // Don't order by il_from if it's constant in the WHERE clause
81 if ( count( $pages ) === 1 ) {
82 $this->addOption( 'ORDER BY', 'il_to' . $sort );
83 } else {
84 $this->addOption( 'ORDER BY', [
85 'il_from' . $sort,
86 'il_to' . $sort
87 ] );
88 }
89 $this->addOption( 'LIMIT', $params['limit'] + 1 );
90
91 if ( $params['images'] ) {
92 $images = [];
93 foreach ( $params['images'] as $img ) {
94 $title = Title::newFromText( $img );
95 if ( !$title || $title->getNamespace() !== NS_FILE ) {
96 $this->addWarning( [ 'apiwarn-notfile', wfEscapeWikiText( $img ) ] );
97 } else {
98 $images[] = $title->getDBkey();
99 }
100 }
101 if ( !$images ) {
102 // No titles so no results
103 return;
104 }
105 if ( $migrationStage & SCHEMA_COMPAT_READ_NEW ) {
106 $this->addWhereFld( 'lt_title', $images );
107 $this->addWhereFld( 'lt_namespace', NS_FILE );
108 } else {
109 $this->addWhereFld( 'il_to', $images );
110 }
111 }
112
113 $this->setVirtualDomain( ImageLinksTable::VIRTUAL_DOMAIN );
114 $res = $this->select( __METHOD__ );
115 $this->resetVirtualDomain();
116
117 if ( $resultPageSet === null ) {
118 $count = 0;
119 foreach ( $res as $row ) {
120 if ( ++$count > $params['limit'] ) {
121 // We've reached the one extra which shows that
122 // there are additional pages to be had. Stop here...
123 $this->setContinueEnumParameter( 'continue', $row->il_from . '|' . $row->il_to );
124 break;
125 }
126 $vals = [];
127 ApiQueryBase::addTitleInfo( $vals, Title::makeTitle( NS_FILE, $row->il_to ) );
128 $fit = $this->addPageSubItem( $row->il_from, $vals );
129 if ( !$fit ) {
130 $this->setContinueEnumParameter( 'continue', $row->il_from . '|' . $row->il_to );
131 break;
132 }
133 }
134 } else {
135 $titles = [];
136 $count = 0;
137 foreach ( $res as $row ) {
138 if ( ++$count > $params['limit'] ) {
139 // We've reached the one extra which shows that
140 // there are additional pages to be had. Stop here...
141 $this->setContinueEnumParameter( 'continue', $row->il_from . '|' . $row->il_to );
142 break;
143 }
144 $titles[] = Title::makeTitle( NS_FILE, $row->il_to );
145 }
146 $resultPageSet->populateFromTitles( $titles );
147 }
148 }
149
151 public function getCacheMode( $params ) {
152 return 'public';
153 }
154
156 public function getAllowedParams() {
157 return [
158 'limit' => [
159 ParamValidator::PARAM_DEFAULT => 10,
160 ParamValidator::PARAM_TYPE => 'limit',
161 IntegerDef::PARAM_MIN => 1,
162 IntegerDef::PARAM_MAX => ApiBase::LIMIT_BIG1,
163 IntegerDef::PARAM_MAX2 => ApiBase::LIMIT_BIG2
164 ],
165 'continue' => [
166 ApiBase::PARAM_HELP_MSG => 'api-help-param-continue',
167 ],
168 'images' => [
169 ParamValidator::PARAM_ISMULTI => true,
170 ],
171 'dir' => [
172 ParamValidator::PARAM_DEFAULT => 'ascending',
173 ParamValidator::PARAM_TYPE => [
174 'ascending',
175 'descending'
176 ]
177 ],
178 ];
179 }
180
182 protected function getExamplesMessages() {
183 $title = Title::newMainPage()->getPrefixedText();
184 $mp = rawurlencode( $title );
185
186 return [
187 "action=query&prop=images&titles={$mp}"
188 => 'apihelp-query+images-example-simple',
189 "action=query&generator=images&titles={$mp}&prop=info"
190 => 'apihelp-query+images-example-generator',
191 ];
192 }
193
195 public function getHelpUrls() {
196 return 'https://www.mediawiki.org/wiki/Special:MyLanguage/API:Images';
197 }
198}
199
201class_alias( ApiQueryImages::class, 'ApiQueryImages' );
const SCHEMA_COMPAT_READ_NEW
Definition Defines.php:298
const NS_FILE
Definition Defines.php:57
wfEscapeWikiText( $input)
Escapes the given text so that it may be output using addWikiText() without any linking,...
parseContinueParamOrDie(string $continue, array $types)
Parse the 'continue' parameter in the usual format and validate the types of each part,...
Definition ApiBase.php:1696
addWarning( $msg, $code=null, $data=null)
Add a warning for this module.
Definition ApiBase.php:1429
const PARAM_HELP_MSG
(string|array|Message) Specify an alternative i18n documentation message for this parameter.
Definition ApiBase.php:167
const LIMIT_BIG2
Fast query, apihighlimits limit.
Definition ApiBase.php:234
extractRequestParams( $options=[])
Using getAllowedParams(), this function makes an array of the values provided by the user,...
Definition ApiBase.php:823
const LIMIT_BIG1
Fast query, standard limit.
Definition ApiBase.php:232
addOption( $name, $value=null)
Add an option such as LIMIT or USE INDEX.
static addTitleInfo(&$arr, $title, $prefix='')
Add information (title and namespace) about a Title object to a result array.
addTables( $tables, $alias=null)
Add a set of tables to the internal array.
addPageSubItem( $pageId, $item, $elemname=null)
Same as addPageSubItems(), but one element of $data at a time.
addJoinConds( $join_conds)
Add a set of JOIN conditions to the internal array.
resetVirtualDomain()
Reset the virtual domain to the main database.
setVirtualDomain(string|false $virtualDomain)
Set the Query database connection (read-only)
getDB()
Get the Query database connection (read-only).
select( $method, $extraQuery=[], ?array &$hookData=null)
Execute a SELECT query based on the values in the internal arrays.
addWhere( $value)
Add a set of WHERE clauses to the internal array.
addWhereFld( $field, $value)
Equivalent to addWhere( [ $field => $value ] )
addFields( $value)
Add a set of fields to select to the internal array.
setContinueEnumParameter( $paramName, $paramValue)
Overridden to set the generator param if in generator mode.
getPageSet()
Get the PageSet object to work on.
This query adds an "<images>" subelement to all pages with the list of images embedded into those pag...
getAllowedParams()
Returns an array of allowed parameters (parameter name) => (default value) or (parameter name) => (ar...
__construct(ApiQuery $query, string $moduleName, LinksMigration $linksMigration)
getHelpUrls()
Return links to more detailed help pages about the module.1.25, returning boolean false is deprecated...
executeGenerator( $resultPageSet)
Execute this module as a generator.
getExamplesMessages()
Returns usage examples for this module.Return value has query strings as keys, with values being eith...
execute()
Evaluates the parameters, performs the requested query, and sets up the result.
getCacheMode( $params)
Get the cache mode for the data generated by this module.Override this in the module subclass....
This is the main query class.
Definition ApiQuery.php:36
makeTitle( $linkId)
Convert a link ID to a Title.to override Title
Service for compat reading of links tables.
A class containing constants representing the names of configuration variables.
const ImageLinksSchemaMigrationStage
Name constant for the ImageLinksSchemaMigrationStage setting, for use with Config::get()
Represents a title within MediaWiki.
Definition Title.php:69
Service for formatting and validating API parameters.
Type definition for integer types.
array $params
The job parameters.