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