MediaWiki master
ApiQueryLangBacklinks.php
Go to the documentation of this file.
1<?php
12namespace MediaWiki\Api;
13
17
23
24 public function __construct( ApiQuery $query, string $moduleName ) {
25 parent::__construct( $query, $moduleName, 'lbl' );
26 }
27
28 public function execute() {
29 $this->run();
30 }
31
33 public function executeGenerator( $resultPageSet ) {
34 $this->run( $resultPageSet );
35 }
36
41 public function run( $resultPageSet = null ) {
42 $params = $this->extractRequestParams();
43
44 if ( isset( $params['title'] ) && !isset( $params['lang'] ) ) {
45 $this->dieWithError(
46 [
47 'apierror-invalidparammix-mustusewith',
48 $this->encodeParamName( 'title' ),
49 $this->encodeParamName( 'lang' )
50 ],
51 'nolang'
52 );
53 }
54
55 if ( $params['continue'] !== null ) {
56 $cont = $this->parseContinueParamOrDie( $params['continue'], [ 'string', 'string', 'int' ] );
57 $db = $this->getDB();
58 $op = $params['dir'] == 'descending' ? '<=' : '>=';
59 $this->addWhere( $db->buildComparison( $op, [
60 'll_lang' => $cont[0],
61 'll_title' => $cont[1],
62 'll_from' => $cont[2],
63 ] ) );
64 }
65
66 $prop = array_fill_keys( $params['prop'], true );
67 $lllang = isset( $prop['lllang'] );
68 $lltitle = isset( $prop['lltitle'] );
69
70 $this->addTables( [ 'langlinks', 'page' ] );
71 $this->addWhere( 'll_from = page_id' );
72
73 $this->addFields( [ 'page_id', 'page_title', 'page_namespace', 'page_is_redirect',
74 'll_from', 'll_lang', 'll_title' ] );
75
76 $sort = ( $params['dir'] == 'descending' ? ' DESC' : '' );
77 if ( isset( $params['lang'] ) ) {
78 $this->addWhereFld( 'll_lang', $params['lang'] );
79 if ( isset( $params['title'] ) ) {
80 $this->addWhereFld( 'll_title', $params['title'] );
81 $this->addOption( 'ORDER BY', 'll_from' . $sort );
82 } else {
83 $this->addOption( 'ORDER BY', [
84 'll_title' . $sort,
85 'll_from' . $sort
86 ] );
87 }
88 } else {
89 $this->addOption( 'ORDER BY', [
90 'll_lang' . $sort,
91 'll_title' . $sort,
92 'll_from' . $sort
93 ] );
94 }
95
96 $this->addOption( 'LIMIT', $params['limit'] + 1 );
97
98 $res = $this->select( __METHOD__ );
99
100 $pages = [];
101
102 $count = 0;
103 $result = $this->getResult();
104
105 if ( $resultPageSet === null ) {
106 $this->executeGenderCacheFromResultWrapper( $res, __METHOD__ );
107 }
108
109 foreach ( $res as $row ) {
110 if ( ++$count > $params['limit'] ) {
111 // We've reached the one extra which shows that there are
112 // additional pages to be had. Stop here... Continue string
113 // preserved in case the redirect query doesn't pass the limit.
115 'continue',
116 "{$row->ll_lang}|{$row->ll_title}|{$row->ll_from}"
117 );
118 break;
119 }
120
121 if ( $resultPageSet !== null ) {
122 $pages[] = Title::newFromRow( $row );
123 } else {
124 $entry = [ 'pageid' => (int)$row->page_id ];
125
126 $title = Title::makeTitle( $row->page_namespace, $row->page_title );
127 ApiQueryBase::addTitleInfo( $entry, $title );
128
129 if ( $row->page_is_redirect ) {
130 $entry['redirect'] = true;
131 }
132
133 if ( $lllang ) {
134 $entry['lllang'] = $row->ll_lang;
135 }
136
137 if ( $lltitle ) {
138 $entry['lltitle'] = $row->ll_title;
139 }
140
141 $fit = $result->addValue( [ 'query', $this->getModuleName() ], null, $entry );
142 if ( !$fit ) {
144 'continue',
145 "{$row->ll_lang}|{$row->ll_title}|{$row->ll_from}"
146 );
147 break;
148 }
149 }
150 }
151
152 if ( $resultPageSet === null ) {
153 $result->addIndexedTagName( [ 'query', $this->getModuleName() ], 'll' );
154 } else {
155 $resultPageSet->populateFromTitles( $pages );
156 }
157 }
158
160 public function getCacheMode( $params ) {
161 return 'public';
162 }
163
165 public function getAllowedParams() {
166 return [
167 'lang' => null,
168 'title' => null,
169 'continue' => [
170 ApiBase::PARAM_HELP_MSG => 'api-help-param-continue',
171 ],
172 'limit' => [
173 ParamValidator::PARAM_DEFAULT => 10,
174 ParamValidator::PARAM_TYPE => 'limit',
175 IntegerDef::PARAM_MIN => 1,
176 IntegerDef::PARAM_MAX => ApiBase::LIMIT_BIG1,
177 IntegerDef::PARAM_MAX2 => ApiBase::LIMIT_BIG2
178 ],
179 'prop' => [
180 ParamValidator::PARAM_ISMULTI => true,
181 ParamValidator::PARAM_DEFAULT => '',
182 ParamValidator::PARAM_TYPE => [
183 'lllang',
184 'lltitle',
185 ],
187 ],
188 'dir' => [
189 ParamValidator::PARAM_DEFAULT => 'ascending',
190 ParamValidator::PARAM_TYPE => [
191 'ascending',
192 'descending'
193 ]
194 ],
195 ];
196 }
197
199 protected function getExamplesMessages() {
200 return [
201 'action=query&list=langbacklinks&lbltitle=Test&lbllang=fr'
202 => 'apihelp-query+langbacklinks-example-simple',
203 'action=query&generator=langbacklinks&glbltitle=Test&glbllang=fr&prop=info'
204 => 'apihelp-query+langbacklinks-example-generator',
205 ];
206 }
207
209 public function getHelpUrls() {
210 return 'https://www.mediawiki.org/wiki/Special:MyLanguage/API:Langbacklinks';
211 }
212}
213
215class_alias( ApiQueryLangBacklinks::class, 'ApiQueryLangBacklinks' );
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.
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.