MediaWiki master
ApiQueryIWBacklinks.php
Go to the documentation of this file.
1<?php
26namespace MediaWiki\Api;
27
31
37
38 public function __construct( ApiQuery $query, string $moduleName ) {
39 parent::__construct( $query, $moduleName, 'iwbl' );
40 }
41
42 public function execute() {
43 $this->run();
44 }
45
46 public function executeGenerator( $resultPageSet ) {
47 $this->run( $resultPageSet );
48 }
49
54 public function run( $resultPageSet = null ) {
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 if ( $params['continue'] !== null ) {
69 $cont = $this->parseContinueParamOrDie( $params['continue'], [ 'string', 'string', 'int' ] );
70 $db = $this->getDB();
71 $op = $params['dir'] == 'descending' ? '<=' : '>=';
72 $this->addWhere( $db->buildComparison( $op, [
73 'iwl_prefix' => $cont[0],
74 'iwl_title' => $cont[1],
75 'iwl_from' => $cont[2],
76 ] ) );
77 }
78
79 $prop = array_fill_keys( $params['prop'], true );
80 $iwprefix = isset( $prop['iwprefix'] );
81 $iwtitle = isset( $prop['iwtitle'] );
82
83 $this->addTables( [ 'iwlinks', 'page' ] );
84 $this->addWhere( 'iwl_from = page_id' );
85
86 $this->addFields( [ 'page_id', 'page_title', 'page_namespace', 'page_is_redirect',
87 'iwl_from', 'iwl_prefix', 'iwl_title' ] );
88
89 $sort = ( $params['dir'] == 'descending' ? ' DESC' : '' );
90 if ( isset( $params['prefix'] ) ) {
91 $this->addWhereFld( 'iwl_prefix', $params['prefix'] );
92 if ( isset( $params['title'] ) ) {
93 $this->addWhereFld( 'iwl_title', $params['title'] );
94 $this->addOption( 'ORDER BY', 'iwl_from' . $sort );
95 } else {
96 $this->addOption( 'ORDER BY', [
97 'iwl_title' . $sort,
98 'iwl_from' . $sort
99 ] );
100 }
101 } else {
102 $this->addOption( 'ORDER BY', [
103 'iwl_prefix' . $sort,
104 'iwl_title' . $sort,
105 'iwl_from' . $sort
106 ] );
107 }
108
109 $this->addOption( 'LIMIT', $params['limit'] + 1 );
110
111 $res = $this->select( __METHOD__ );
112
113 $pages = [];
114
115 $count = 0;
116 $result = $this->getResult();
117
118 if ( $resultPageSet === null ) {
119 $this->executeGenderCacheFromResultWrapper( $res, __METHOD__ );
120 }
121
122 foreach ( $res as $row ) {
123 if ( ++$count > $params['limit'] ) {
124 // We've reached the one extra which shows that there are
125 // additional pages to be had. Stop here...
126 // Continue string preserved in case the redirect query doesn't
127 // pass the limit
129 'continue',
130 "{$row->iwl_prefix}|{$row->iwl_title}|{$row->iwl_from}"
131 );
132 break;
133 }
134
135 if ( $resultPageSet !== null ) {
136 $pages[] = Title::newFromRow( $row );
137 } else {
138 $entry = [ 'pageid' => (int)$row->page_id ];
139
140 $title = Title::makeTitle( $row->page_namespace, $row->page_title );
141 ApiQueryBase::addTitleInfo( $entry, $title );
142
143 if ( $row->page_is_redirect ) {
144 $entry['redirect'] = true;
145 }
146
147 if ( $iwprefix ) {
148 $entry['iwprefix'] = $row->iwl_prefix;
149 }
150
151 if ( $iwtitle ) {
152 $entry['iwtitle'] = $row->iwl_title;
153 }
154
155 $fit = $result->addValue( [ 'query', $this->getModuleName() ], null, $entry );
156 if ( !$fit ) {
158 'continue',
159 "{$row->iwl_prefix}|{$row->iwl_title}|{$row->iwl_from}"
160 );
161 break;
162 }
163 }
164 }
165
166 if ( $resultPageSet === null ) {
167 $result->addIndexedTagName( [ 'query', $this->getModuleName() ], 'iw' );
168 } else {
169 $resultPageSet->populateFromTitles( $pages );
170 }
171 }
172
173 public function getCacheMode( $params ) {
174 return 'public';
175 }
176
177 public function getAllowedParams() {
178 return [
179 'prefix' => null,
180 'title' => null,
181 'continue' => [
182 ApiBase::PARAM_HELP_MSG => 'api-help-param-continue',
183 ],
184 'limit' => [
185 ParamValidator::PARAM_DEFAULT => 10,
186 ParamValidator::PARAM_TYPE => 'limit',
187 IntegerDef::PARAM_MIN => 1,
188 IntegerDef::PARAM_MAX => ApiBase::LIMIT_BIG1,
189 IntegerDef::PARAM_MAX2 => ApiBase::LIMIT_BIG2
190 ],
191 'prop' => [
192 ParamValidator::PARAM_ISMULTI => true,
193 ParamValidator::PARAM_DEFAULT => '',
194 ParamValidator::PARAM_TYPE => [
195 'iwprefix',
196 'iwtitle',
197 ],
199 ],
200 'dir' => [
201 ParamValidator::PARAM_DEFAULT => 'ascending',
202 ParamValidator::PARAM_TYPE => [
203 'ascending',
204 'descending'
205 ]
206 ],
207 ];
208 }
209
210 protected function getExamplesMessages() {
211 return [
212 'action=query&list=iwbacklinks&iwbltitle=Test&iwblprefix=wikibooks'
213 => 'apihelp-query+iwbacklinks-example-simple',
214 'action=query&generator=iwbacklinks&giwbltitle=Test&giwblprefix=wikibooks&prop=info'
215 => 'apihelp-query+iwbacklinks-example-generator',
216 ];
217 }
218
219 public function getHelpUrls() {
220 return 'https://www.mediawiki.org/wiki/Special:MyLanguage/API:Iwbacklinks';
221 }
222}
223
225class_alias( ApiQueryIWBacklinks::class, 'ApiQueryIWBacklinks' );
array $params
The job parameters.
run()
Run the job.
dieWithError( $msg, $code=null, $data=null, $httpCode=0)
Abort execution with an error.
Definition ApiBase.php:1577
getModuleName()
Get the name of the module being executed by this instance.
Definition ApiBase.php:571
parseContinueParamOrDie(string $continue, array $types)
Parse the 'continue' parameter in the usual format and validate the types of each part,...
Definition ApiBase.php:1768
getResult()
Get the result object.
Definition ApiBase.php:710
const PARAM_HELP_MSG_PER_VALUE
((string|array|Message)[]) When PARAM_TYPE is an array, or 'string' with PARAM_ISMULTI,...
Definition ApiBase.php:224
const PARAM_HELP_MSG
(string|array|Message) Specify an alternative i18n documentation message for this parameter.
Definition ApiBase.php:184
const LIMIT_BIG2
Fast query, apihighlimits limit.
Definition ApiBase.php:251
extractRequestParams( $options=[])
Using getAllowedParams(), this function makes an array of the values provided by the user,...
Definition ApiBase.php:851
const LIMIT_BIG1
Fast query, standard limit.
Definition ApiBase.php:249
addOption( $name, $value=null)
Add an option such as LIMIT or USE INDEX.
static addTitleInfo(&$arr, $title, $prefix='')
Add information (title and namespace) about a Title object to a result array.
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.
addWhere( $value)
Add a set of WHERE clauses to the internal array.
executeGenderCacheFromResultWrapper(IResultWrapper $res, $fname=__METHOD__, $fieldPrefix='page')
Preprocess the result set to fill the GenderCache with the necessary information before using self::a...
addWhereFld( $field, $value)
Equivalent to addWhere( [ $field => $value ] )
addFields( $value)
Add a set of fields to select to the internal array.
setContinueEnumParameter( $paramName, $paramValue)
Overridden to set the generator param if in generator mode.
encodeParamName( $paramName)
Overrides ApiBase to prepend 'g' to every generator parameter.
This is the main query class.
Definition ApiQuery.php:48
Represents a title within MediaWiki.
Definition Title.php:78
Service for formatting and validating API parameters.
Type definition for integer types.