MediaWiki master
ApiQueryIWLinks.php
Go to the documentation of this file.
1<?php
26namespace MediaWiki\Api;
27
32
39
40 private UrlUtils $urlUtils;
41
42 public function __construct( ApiQuery $query, string $moduleName, UrlUtils $urlUtils ) {
43 parent::__construct( $query, $moduleName, 'iw' );
44
45 $this->urlUtils = $urlUtils;
46 }
47
48 public function execute() {
49 $pages = $this->getPageSet()->getGoodPages();
50 if ( $pages === [] ) {
51 return;
52 }
53
55 $prop = array_fill_keys( (array)$params['prop'], true );
56
57 if ( isset( $params['title'] ) && !isset( $params['prefix'] ) ) {
58 $this->dieWithError(
59 [
60 'apierror-invalidparammix-mustusewith',
61 $this->encodeParamName( 'title' ),
62 $this->encodeParamName( 'prefix' ),
63 ],
64 'invalidparammix'
65 );
66 }
67
68 // Handle deprecated param
69 $this->requireMaxOneParameter( $params, 'url', 'prop' );
70 if ( $params['url'] ) {
71 $prop = [ 'url' => 1 ];
72 }
73
74 $this->addFields( [
75 'iwl_from',
76 'iwl_prefix',
77 'iwl_title'
78 ] );
79
80 $this->addTables( 'iwlinks' );
81 $this->addWhereFld( 'iwl_from', array_keys( $pages ) );
82
83 if ( $params['continue'] !== null ) {
84 $cont = $this->parseContinueParamOrDie( $params['continue'], [ 'int', 'string', 'string' ] );
85 $op = $params['dir'] == 'descending' ? '<=' : '>=';
86 $db = $this->getDB();
87 $this->addWhere( $db->buildComparison( $op, [
88 'iwl_from' => $cont[0],
89 'iwl_prefix' => $cont[1],
90 'iwl_title' => $cont[2],
91 ] ) );
92 }
93
94 $sort = ( $params['dir'] == 'descending' ? ' DESC' : '' );
95 if ( isset( $params['prefix'] ) ) {
96 $this->addWhereFld( 'iwl_prefix', $params['prefix'] );
97 if ( isset( $params['title'] ) ) {
98 $this->addWhereFld( 'iwl_title', $params['title'] );
99 $this->addOption( 'ORDER BY', 'iwl_from' . $sort );
100 } else {
101 $this->addOption( 'ORDER BY', [
102 'iwl_from' . $sort,
103 'iwl_title' . $sort
104 ] );
105 }
106 } else {
107 // Don't order by iwl_from if it's constant in the WHERE clause
108 if ( count( $pages ) === 1 ) {
109 $this->addOption( 'ORDER BY', 'iwl_prefix' . $sort );
110 } else {
111 $this->addOption( 'ORDER BY', [
112 'iwl_from' . $sort,
113 'iwl_prefix' . $sort,
114 'iwl_title' . $sort
115 ] );
116 }
117 }
118
119 $this->addOption( 'LIMIT', $params['limit'] + 1 );
120 $res = $this->select( __METHOD__ );
121
122 $count = 0;
123 foreach ( $res as $row ) {
124 if ( ++$count > $params['limit'] ) {
125 // We've reached the one extra which shows that
126 // there are additional pages to be had. Stop here...
128 'continue',
129 "{$row->iwl_from}|{$row->iwl_prefix}|{$row->iwl_title}"
130 );
131 break;
132 }
133 $entry = [ 'prefix' => $row->iwl_prefix ];
134
135 if ( isset( $prop['url'] ) ) {
136 $title = Title::newFromText( "{$row->iwl_prefix}:{$row->iwl_title}" );
137 if ( $title ) {
138 $entry['url'] = (string)$this->urlUtils->expand( $title->getFullURL(), PROTO_CURRENT );
139 }
140 }
141
142 ApiResult::setContentValue( $entry, 'title', $row->iwl_title );
143 $fit = $this->addPageSubItem( $row->iwl_from, $entry );
144 if ( !$fit ) {
146 'continue',
147 "{$row->iwl_from}|{$row->iwl_prefix}|{$row->iwl_title}"
148 );
149 break;
150 }
151 }
152 }
153
154 public function getCacheMode( $params ) {
155 return 'public';
156 }
157
158 public function getAllowedParams() {
159 return [
160 'prop' => [
161 ParamValidator::PARAM_ISMULTI => true,
162 ParamValidator::PARAM_TYPE => [
163 'url',
164 ],
166 ],
167 'prefix' => null,
168 'title' => null,
169 'dir' => [
170 ParamValidator::PARAM_DEFAULT => 'ascending',
171 ParamValidator::PARAM_TYPE => [
172 'ascending',
173 'descending'
174 ]
175 ],
176 'limit' => [
177 ParamValidator::PARAM_DEFAULT => 10,
178 ParamValidator::PARAM_TYPE => 'limit',
179 IntegerDef::PARAM_MIN => 1,
180 IntegerDef::PARAM_MAX => ApiBase::LIMIT_BIG1,
181 IntegerDef::PARAM_MAX2 => ApiBase::LIMIT_BIG2
182 ],
183 'continue' => [
184 ApiBase::PARAM_HELP_MSG => 'api-help-param-continue',
185 ],
186 'url' => [
187 ParamValidator::PARAM_DEFAULT => false,
188 ParamValidator::PARAM_DEPRECATED => true,
189 ],
190 ];
191 }
192
193 protected function getExamplesMessages() {
194 $title = Title::newMainPage()->getPrefixedText();
195 $mp = rawurlencode( $title );
196
197 return [
198 "action=query&prop=iwlinks&titles={$mp}"
199 => 'apihelp-query+iwlinks-example-simple',
200 ];
201 }
202
203 public function getHelpUrls() {
204 return 'https://www.mediawiki.org/wiki/Special:MyLanguage/API:Iwlinks';
205 }
206}
207
209class_alias( ApiQueryIWLinks::class, 'ApiQueryIWLinks' );
const PROTO_CURRENT
Definition Defines.php:215
array $params
The job parameters.
dieWithError( $msg, $code=null, $data=null, $httpCode=0)
Abort execution with an error.
Definition ApiBase.php:1522
parseContinueParamOrDie(string $continue, array $types)
Parse the 'continue' parameter in the usual format and validate the types of each part,...
Definition ApiBase.php:1707
const PARAM_HELP_MSG_PER_VALUE
((string|array|Message)[]) When PARAM_TYPE is an array, or 'string' with PARAM_ISMULTI,...
Definition ApiBase.php:221
requireMaxOneParameter( $params,... $required)
Dies if more than one parameter from a certain set of parameters are set and not false.
Definition ApiBase.php:1011
const PARAM_HELP_MSG
(string|array|Message) Specify an alternative i18n documentation message for this parameter.
Definition ApiBase.php:181
const LIMIT_BIG2
Fast query, apihighlimits limit.
Definition ApiBase.php:248
encodeParamName( $paramName)
This method mangles parameter name based on the prefix supplied to the constructor.
Definition ApiBase.php:815
extractRequestParams( $options=[])
Using getAllowedParams(), this function makes an array of the values provided by the user,...
Definition ApiBase.php:837
const LIMIT_BIG1
Fast query, standard limit.
Definition ApiBase.php:246
This is a base class for all Query modules.
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.
addPageSubItem( $pageId, $item, $elemname=null)
Same as addPageSubItems(), but one element of $data at a time.
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.
getPageSet()
Get the PageSet object to work on.
setContinueEnumParameter( $paramName, $paramValue)
Set a query-continue value.
addWhereFld( $field, $value)
Equivalent to addWhere( [ $field => $value ] )
addFields( $value)
Add a set of fields to select to the internal array.
This is the main query class.
Definition ApiQuery.php:48
static setContentValue(array &$arr, $name, $value, $flags=0)
Add an output value to the array by name and mark as META_CONTENT.
Represents a title within MediaWiki.
Definition Title.php:78
A service to expand, parse, and otherwise manipulate URLs.
Definition UrlUtils.php:16
Service for formatting and validating API parameters.
Type definition for integer types.