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