MediaWiki REL1_39
ApiQueryImages.php
Go to the documentation of this file.
1<?php
25
33
38 public function __construct( ApiQuery $query, $moduleName ) {
39 parent::__construct( $query, $moduleName, 'im' );
40 }
41
42 public function execute() {
43 $this->run();
44 }
45
46 public function executeGenerator( $resultPageSet ) {
47 $this->run( $resultPageSet );
48 }
49
53 private function run( $resultPageSet = null ) {
54 $pages = $this->getPageSet()->getGoodPages();
55 if ( $pages === [] ) {
56 return; // nothing to do
57 }
58
59 $params = $this->extractRequestParams();
60 $this->addFields( [
61 'il_from',
62 'il_to'
63 ] );
64
65 $this->addTables( 'imagelinks' );
66 $this->addWhereFld( 'il_from', array_keys( $pages ) );
67 if ( $params['continue'] !== null ) {
68 $cont = explode( '|', $params['continue'] );
69 $this->dieContinueUsageIf( count( $cont ) != 2 );
70 $op = $params['dir'] == 'descending' ? '<' : '>';
71 $ilfrom = (int)$cont[0];
72 $ilto = $this->getDB()->addQuotes( $cont[1] );
73 $this->addWhere(
74 "il_from $op $ilfrom OR " .
75 "(il_from = $ilfrom AND " .
76 "il_to $op= $ilto)"
77 );
78 }
79
80 $sort = ( $params['dir'] == 'descending' ? ' DESC' : '' );
81 // Don't order by il_from if it's constant in the WHERE clause
82 if ( count( $pages ) === 1 ) {
83 $this->addOption( 'ORDER BY', 'il_to' . $sort );
84 } else {
85 $this->addOption( 'ORDER BY', [
86 'il_from' . $sort,
87 'il_to' . $sort
88 ] );
89 }
90 $this->addOption( 'LIMIT', $params['limit'] + 1 );
91
92 if ( $params['images'] ) {
93 $images = [];
94 foreach ( $params['images'] as $img ) {
95 $title = Title::newFromText( $img );
96 if ( !$title || $title->getNamespace() !== NS_FILE ) {
97 $this->addWarning( [ 'apiwarn-notfile', wfEscapeWikiText( $img ) ] );
98 } else {
99 $images[] = $title->getDBkey();
100 }
101 }
102 if ( !$images ) {
103 // No titles so no results
104 return;
105 }
106 $this->addWhereFld( 'il_to', $images );
107 }
108
109 $res = $this->select( __METHOD__ );
110
111 if ( $resultPageSet === null ) {
112 $count = 0;
113 foreach ( $res as $row ) {
114 if ( ++$count > $params['limit'] ) {
115 // We've reached the one extra which shows that
116 // there are additional pages to be had. Stop here...
117 $this->setContinueEnumParameter( 'continue', $row->il_from . '|' . $row->il_to );
118 break;
119 }
120 $vals = [];
121 ApiQueryBase::addTitleInfo( $vals, Title::makeTitle( NS_FILE, $row->il_to ) );
122 $fit = $this->addPageSubItem( $row->il_from, $vals );
123 if ( !$fit ) {
124 $this->setContinueEnumParameter( 'continue', $row->il_from . '|' . $row->il_to );
125 break;
126 }
127 }
128 } else {
129 $titles = [];
130 $count = 0;
131 foreach ( $res as $row ) {
132 if ( ++$count > $params['limit'] ) {
133 // We've reached the one extra which shows that
134 // there are additional pages to be had. Stop here...
135 $this->setContinueEnumParameter( 'continue', $row->il_from . '|' . $row->il_to );
136 break;
137 }
138 $titles[] = Title::makeTitle( NS_FILE, $row->il_to );
139 }
140 $resultPageSet->populateFromTitles( $titles );
141 }
142 }
143
144 public function getCacheMode( $params ) {
145 return 'public';
146 }
147
148 public function getAllowedParams() {
149 return [
150 'limit' => [
151 ParamValidator::PARAM_DEFAULT => 10,
152 ParamValidator::PARAM_TYPE => 'limit',
153 IntegerDef::PARAM_MIN => 1,
154 IntegerDef::PARAM_MAX => ApiBase::LIMIT_BIG1,
155 IntegerDef::PARAM_MAX2 => ApiBase::LIMIT_BIG2
156 ],
157 'continue' => [
158 ApiBase::PARAM_HELP_MSG => 'api-help-param-continue',
159 ],
160 'images' => [
161 ParamValidator::PARAM_ISMULTI => true,
162 ],
163 'dir' => [
164 ParamValidator::PARAM_DEFAULT => 'ascending',
165 ParamValidator::PARAM_TYPE => [
166 'ascending',
167 'descending'
168 ]
169 ],
170 ];
171 }
172
173 protected function getExamplesMessages() {
174 $title = Title::newMainPage()->getPrefixedText();
175 $mp = rawurlencode( $title );
176
177 return [
178 "action=query&prop=images&titles={$mp}"
179 => 'apihelp-query+images-example-simple',
180 "action=query&generator=images&titles={$mp}&prop=info"
181 => 'apihelp-query+images-example-generator',
182 ];
183 }
184
185 public function getHelpUrls() {
186 return 'https://www.mediawiki.org/wiki/Special:MyLanguage/API:Images';
187 }
188}
const NS_FILE
Definition Defines.php:70
wfEscapeWikiText( $text)
Escapes the given text so that it may be output using addWikiText() without any linking,...
dieContinueUsageIf( $condition)
Die with the 'badcontinue' error.
Definition ApiBase.php:1643
const LIMIT_BIG1
Fast query, standard limit.
Definition ApiBase.php:221
extractRequestParams( $options=[])
Using getAllowedParams(), this function makes an array of the values provided by the user,...
Definition ApiBase.php:765
const PARAM_HELP_MSG
(string|array|Message) Specify an alternative i18n documentation message for this parameter.
Definition ApiBase.php:163
addWarning( $msg, $code=null, $data=null)
Add a warning for this module.
Definition ApiBase.php:1372
const LIMIT_BIG2
Fast query, apihighlimits limit.
Definition ApiBase.php:223
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:41
static newFromText( $text, $defaultNamespace=NS_MAIN)
Create a new Title from text, such as what one would find in a link.
Definition Title.php:370
static makeTitle( $ns, $title, $fragment='', $interwiki='')
Create a new Title from a namespace index and a DB key.
Definition Title.php:638
Service for formatting and validating API parameters.
Type definition for integer types.