MediaWiki REL1_40
ApiQueryImages.php
Go to the documentation of this file.
1<?php
26
34
39 public function __construct( ApiQuery $query, $moduleName ) {
40 parent::__construct( $query, $moduleName, 'im' );
41 }
42
43 public function execute() {
44 $this->run();
45 }
46
47 public function executeGenerator( $resultPageSet ) {
48 $this->run( $resultPageSet );
49 }
50
54 private function run( $resultPageSet = null ) {
55 $pages = $this->getPageSet()->getGoodPages();
56 if ( $pages === [] ) {
57 return; // nothing to do
58 }
59
60 $params = $this->extractRequestParams();
61 $this->addFields( [
62 'il_from',
63 'il_to'
64 ] );
65
66 $this->addTables( 'imagelinks' );
67 $this->addWhereFld( 'il_from', array_keys( $pages ) );
68 if ( $params['continue'] !== null ) {
69 $db = $this->getDB();
70 $cont = $this->parseContinueParamOrDie( $params['continue'], [ 'int', 'string' ] );
71 $op = $params['dir'] == 'descending' ? '<=' : '>=';
72 $this->addWhere( $db->buildComparison( $op, [
73 'il_from' => $cont[0],
74 'il_to' => $cont[1],
75 ] ) );
76 }
77
78 $sort = ( $params['dir'] == 'descending' ? ' DESC' : '' );
79 // Don't order by il_from if it's constant in the WHERE clause
80 if ( count( $pages ) === 1 ) {
81 $this->addOption( 'ORDER BY', 'il_to' . $sort );
82 } else {
83 $this->addOption( 'ORDER BY', [
84 'il_from' . $sort,
85 'il_to' . $sort
86 ] );
87 }
88 $this->addOption( 'LIMIT', $params['limit'] + 1 );
89
90 if ( $params['images'] ) {
91 $images = [];
92 foreach ( $params['images'] as $img ) {
93 $title = Title::newFromText( $img );
94 if ( !$title || $title->getNamespace() !== NS_FILE ) {
95 $this->addWarning( [ 'apiwarn-notfile', wfEscapeWikiText( $img ) ] );
96 } else {
97 $images[] = $title->getDBkey();
98 }
99 }
100 if ( !$images ) {
101 // No titles so no results
102 return;
103 }
104 $this->addWhereFld( 'il_to', $images );
105 }
106
107 $res = $this->select( __METHOD__ );
108
109 if ( $resultPageSet === null ) {
110 $count = 0;
111 foreach ( $res as $row ) {
112 if ( ++$count > $params['limit'] ) {
113 // We've reached the one extra which shows that
114 // there are additional pages to be had. Stop here...
115 $this->setContinueEnumParameter( 'continue', $row->il_from . '|' . $row->il_to );
116 break;
117 }
118 $vals = [];
119 ApiQueryBase::addTitleInfo( $vals, Title::makeTitle( NS_FILE, $row->il_to ) );
120 $fit = $this->addPageSubItem( $row->il_from, $vals );
121 if ( !$fit ) {
122 $this->setContinueEnumParameter( 'continue', $row->il_from . '|' . $row->il_to );
123 break;
124 }
125 }
126 } else {
127 $titles = [];
128 $count = 0;
129 foreach ( $res as $row ) {
130 if ( ++$count > $params['limit'] ) {
131 // We've reached the one extra which shows that
132 // there are additional pages to be had. Stop here...
133 $this->setContinueEnumParameter( 'continue', $row->il_from . '|' . $row->il_to );
134 break;
135 }
136 $titles[] = Title::makeTitle( NS_FILE, $row->il_to );
137 }
138 $resultPageSet->populateFromTitles( $titles );
139 }
140 }
141
142 public function getCacheMode( $params ) {
143 return 'public';
144 }
145
146 public function getAllowedParams() {
147 return [
148 'limit' => [
149 ParamValidator::PARAM_DEFAULT => 10,
150 ParamValidator::PARAM_TYPE => 'limit',
151 IntegerDef::PARAM_MIN => 1,
152 IntegerDef::PARAM_MAX => ApiBase::LIMIT_BIG1,
153 IntegerDef::PARAM_MAX2 => ApiBase::LIMIT_BIG2
154 ],
155 'continue' => [
156 ApiBase::PARAM_HELP_MSG => 'api-help-param-continue',
157 ],
158 'images' => [
159 ParamValidator::PARAM_ISMULTI => true,
160 ],
161 'dir' => [
162 ParamValidator::PARAM_DEFAULT => 'ascending',
163 ParamValidator::PARAM_TYPE => [
164 'ascending',
165 'descending'
166 ]
167 ],
168 ];
169 }
170
171 protected function getExamplesMessages() {
172 $title = Title::newMainPage()->getPrefixedText();
173 $mp = rawurlencode( $title );
174
175 return [
176 "action=query&prop=images&titles={$mp}"
177 => 'apihelp-query+images-example-simple',
178 "action=query&generator=images&titles={$mp}&prop=info"
179 => 'apihelp-query+images-example-generator',
180 ];
181 }
182
183 public function getHelpUrls() {
184 return 'https://www.mediawiki.org/wiki/Special:MyLanguage/API:Images';
185 }
186}
const NS_FILE
Definition Defines.php:70
wfEscapeWikiText( $text)
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:1649
const LIMIT_BIG1
Fast query, standard limit.
Definition ApiBase.php:229
extractRequestParams( $options=[])
Using getAllowedParams(), this function makes an array of the values provided by the user,...
Definition ApiBase.php:773
const PARAM_HELP_MSG
(string|array|Message) Specify an alternative i18n documentation message for this parameter.
Definition ApiBase.php:166
addWarning( $msg, $code=null, $data=null)
Add a warning for this module.
Definition ApiBase.php:1378
const LIMIT_BIG2
Fast query, apihighlimits limit.
Definition ApiBase.php:231
static addTitleInfo(&$arr, $title, $prefix='')
Add information (title and namespace) about a Title object to a result array.
addFields( $value)
Add a set of fields to select to the internal array.
addPageSubItem( $pageId, $item, $elemname=null)
Same as addPageSubItems(), but one element of $data at a time.
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.
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.
addWhereFld( $field, $value)
Equivalent to addWhere( [ $field => $value ] )
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.
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...
executeGenerator( $resultPageSet)
Execute this module as a generator.
getCacheMode( $params)
Get the cache mode for the data generated by this module.
__construct(ApiQuery $query, $moduleName)
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.
getExamplesMessages()
Returns usage examples for this module.
getHelpUrls()
Return links to more detailed help pages about the module.
This is the main query class.
Definition ApiQuery.php:42
Represents a title within MediaWiki.
Definition Title.php:82
Service for formatting and validating API parameters.
Type definition for integer types.