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