MediaWiki master
ApiQueryIWBacklinks.php
Go to the documentation of this file.
1<?php
12namespace MediaWiki\Api;
13
18
24
25 public function __construct( ApiQuery $query, string $moduleName ) {
26 parent::__construct( $query, $moduleName, 'iwbl' );
27 }
28
29 public function execute() {
30 $this->run();
31 }
32
34 public function executeGenerator( $resultPageSet ) {
35 $this->run( $resultPageSet );
36 }
37
42 public function run( $resultPageSet = null ) {
43 $params = $this->extractRequestParams();
44
45 if ( isset( $params['title'] ) && !isset( $params['prefix'] ) ) {
46 $this->dieWithError(
47 [
48 'apierror-invalidparammix-mustusewith',
49 $this->encodeParamName( 'title' ),
50 $this->encodeParamName( 'prefix' ),
51 ],
52 'invalidparammix'
53 );
54 }
55
56 $this->setVirtualDomain( InterwikiLinksTable::VIRTUAL_DOMAIN );
57 $db = $this->getDB();
58
59 if ( $params['continue'] !== null ) {
60 $cont = $this->parseContinueParamOrDie( $params['continue'], [ 'string', 'string', 'int' ] );
61 $op = $params['dir'] == 'descending' ? '<=' : '>=';
62 $this->addWhere( $db->buildComparison( $op, [
63 'iwl_prefix' => $cont[0],
64 'iwl_title' => $cont[1],
65 'iwl_from' => $cont[2],
66 ] ) );
67 }
68
69 $prop = array_fill_keys( $params['prop'], true );
70 $iwprefix = isset( $prop['iwprefix'] );
71 $iwtitle = isset( $prop['iwtitle'] );
72
73 $this->addTables( [ 'iwlinks', 'page' ] );
74 $this->addWhere( 'iwl_from = page_id' );
75
76 $this->addFields( [ 'page_id', 'page_title', 'page_namespace', 'page_is_redirect',
77 'iwl_from', 'iwl_prefix', 'iwl_title' ] );
78
79 $sort = ( $params['dir'] == 'descending' ? ' DESC' : '' );
80 if ( isset( $params['prefix'] ) ) {
81 $this->addWhereFld( 'iwl_prefix', $params['prefix'] );
82 if ( isset( $params['title'] ) ) {
83 $this->addWhereFld( 'iwl_title', $params['title'] );
84 $this->addOption( 'ORDER BY', 'iwl_from' . $sort );
85 } else {
86 $this->addOption( 'ORDER BY', [
87 'iwl_title' . $sort,
88 'iwl_from' . $sort
89 ] );
90 }
91 } else {
92 $this->addOption( 'ORDER BY', [
93 'iwl_prefix' . $sort,
94 'iwl_title' . $sort,
95 'iwl_from' . $sort
96 ] );
97 }
98
99 $this->addOption( 'LIMIT', $params['limit'] + 1 );
100
101 $res = $this->select( __METHOD__ );
102
103 $pages = [];
104
105 $count = 0;
106 $result = $this->getResult();
107
108 if ( $resultPageSet === null ) {
109 $this->executeGenderCacheFromResultWrapper( $res, __METHOD__ );
110 }
111
112 foreach ( $res as $row ) {
113 if ( ++$count > $params['limit'] ) {
114 // We've reached the one extra which shows that there are
115 // additional pages to be had. Stop here...
116 // Continue string preserved in case the redirect query doesn't
117 // pass the limit
119 'continue',
120 "{$row->iwl_prefix}|{$row->iwl_title}|{$row->iwl_from}"
121 );
122 break;
123 }
124
125 if ( $resultPageSet !== null ) {
126 $pages[] = Title::newFromRow( $row );
127 } else {
128 $entry = [ 'pageid' => (int)$row->page_id ];
129
130 $title = Title::makeTitle( $row->page_namespace, $row->page_title );
131 ApiQueryBase::addTitleInfo( $entry, $title );
132
133 if ( $row->page_is_redirect ) {
134 $entry['redirect'] = true;
135 }
136
137 if ( $iwprefix ) {
138 $entry['iwprefix'] = $row->iwl_prefix;
139 }
140
141 if ( $iwtitle ) {
142 $entry['iwtitle'] = $row->iwl_title;
143 }
144
145 $fit = $result->addValue( [ 'query', $this->getModuleName() ], null, $entry );
146 if ( !$fit ) {
148 'continue',
149 "{$row->iwl_prefix}|{$row->iwl_title}|{$row->iwl_from}"
150 );
151 break;
152 }
153 }
154 }
155
156 if ( $resultPageSet === null ) {
157 $result->addIndexedTagName( [ 'query', $this->getModuleName() ], 'iw' );
158 } else {
159 $resultPageSet->populateFromTitles( $pages );
160 }
161 }
162
164 public function getCacheMode( $params ) {
165 return 'public';
166 }
167
169 public function getAllowedParams() {
170 return [
171 'prefix' => null,
172 'title' => null,
173 'continue' => [
174 ApiBase::PARAM_HELP_MSG => 'api-help-param-continue',
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 'prop' => [
184 ParamValidator::PARAM_ISMULTI => true,
185 ParamValidator::PARAM_DEFAULT => '',
186 ParamValidator::PARAM_TYPE => [
187 'iwprefix',
188 'iwtitle',
189 ],
191 ],
192 'dir' => [
193 ParamValidator::PARAM_DEFAULT => 'ascending',
194 ParamValidator::PARAM_TYPE => [
195 'ascending',
196 'descending'
197 ]
198 ],
199 ];
200 }
201
203 protected function getExamplesMessages() {
204 return [
205 'action=query&list=iwbacklinks&iwbltitle=Test&iwblprefix=wikibooks'
206 => 'apihelp-query+iwbacklinks-example-simple',
207 'action=query&generator=iwbacklinks&giwbltitle=Test&giwblprefix=wikibooks&prop=info'
208 => 'apihelp-query+iwbacklinks-example-generator',
209 ];
210 }
211
213 public function getHelpUrls() {
214 return 'https://www.mediawiki.org/wiki/Special:MyLanguage/API:Iwbacklinks';
215 }
216}
217
219class_alias( ApiQueryIWBacklinks::class, 'ApiQueryIWBacklinks' );
dieWithError( $msg, $code=null, $data=null, $httpCode=0)
Abort execution with an error.
Definition ApiBase.php:1511
getModuleName()
Get the name of the module being executed by this instance.
Definition ApiBase.php:543
parseContinueParamOrDie(string $continue, array $types)
Parse the 'continue' parameter in the usual format and validate the types of each part,...
Definition ApiBase.php:1696
getResult()
Get the result object.
Definition ApiBase.php:682
const PARAM_HELP_MSG_PER_VALUE
((string|array|Message)[]) When PARAM_TYPE is an array, or 'string' with PARAM_ISMULTI,...
Definition ApiBase.php:207
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
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
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.
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.
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:36
makeTitle( $linkId)
Convert a link ID to a Title.to override Title
Represents a title within MediaWiki.
Definition Title.php:69
Service for formatting and validating API parameters.
Type definition for integer types.