MediaWiki master
ApiQueryLangBacklinks.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, 'lbl' );
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['lang'] ) ) {
46 $this->dieWithError(
47 [
48 'apierror-invalidparammix-mustusewith',
49 $this->encodeParamName( 'title' ),
50 $this->encodeParamName( 'lang' )
51 ],
52 'nolang'
53 );
54 }
55
56 if ( $params['continue'] !== null ) {
57 $cont = $this->parseContinueParamOrDie( $params['continue'], [ 'string', 'string', 'int' ] );
58 $db = $this->getDB();
59 $op = $params['dir'] == 'descending' ? '<=' : '>=';
60 $this->addWhere( $db->buildComparison( $op, [
61 'll_lang' => $cont[0],
62 'll_title' => $cont[1],
63 'll_from' => $cont[2],
64 ] ) );
65 }
66
67 $prop = array_fill_keys( $params['prop'], true );
68 $lllang = isset( $prop['lllang'] );
69 $lltitle = isset( $prop['lltitle'] );
70
71 $this->addTables( [ 'langlinks', 'page' ] );
72 $this->addWhere( 'll_from = page_id' );
73
74 $this->addFields( [ 'page_id', 'page_title', 'page_namespace', 'page_is_redirect',
75 'll_from', 'll_lang', 'll_title' ] );
76
77 $sort = ( $params['dir'] == 'descending' ? ' DESC' : '' );
78 if ( isset( $params['lang'] ) ) {
79 $this->addWhereFld( 'll_lang', $params['lang'] );
80 if ( isset( $params['title'] ) ) {
81 $this->addWhereFld( 'll_title', $params['title'] );
82 $this->addOption( 'ORDER BY', 'll_from' . $sort );
83 } else {
84 $this->addOption( 'ORDER BY', [
85 'll_title' . $sort,
86 'll_from' . $sort
87 ] );
88 }
89 } else {
90 $this->addOption( 'ORDER BY', [
91 'll_lang' . $sort,
92 'll_title' . $sort,
93 'll_from' . $sort
94 ] );
95 }
96
97 $this->addOption( 'LIMIT', $params['limit'] + 1 );
98
99 $this->setVirtualDomain( LangLinksTable::VIRTUAL_DOMAIN );
100 $res = $this->select( __METHOD__ );
101
102 $pages = [];
103
104 $count = 0;
105 $result = $this->getResult();
106
107 if ( $resultPageSet === null ) {
108 $this->executeGenderCacheFromResultWrapper( $res, __METHOD__ );
109 }
110
111 foreach ( $res as $row ) {
112 if ( ++$count > $params['limit'] ) {
113 // We've reached the one extra which shows that there are
114 // additional pages to be had. Stop here... Continue string
115 // preserved in case the redirect query doesn't pass the limit.
117 'continue',
118 "{$row->ll_lang}|{$row->ll_title}|{$row->ll_from}"
119 );
120 break;
121 }
122
123 if ( $resultPageSet !== null ) {
124 $pages[] = Title::newFromRow( $row );
125 } else {
126 $entry = [ 'pageid' => (int)$row->page_id ];
127
128 $title = Title::makeTitle( $row->page_namespace, $row->page_title );
129 ApiQueryBase::addTitleInfo( $entry, $title );
130
131 if ( $row->page_is_redirect ) {
132 $entry['redirect'] = true;
133 }
134
135 if ( $lllang ) {
136 $entry['lllang'] = $row->ll_lang;
137 }
138
139 if ( $lltitle ) {
140 $entry['lltitle'] = $row->ll_title;
141 }
142
143 $fit = $result->addValue( [ 'query', $this->getModuleName() ], null, $entry );
144 if ( !$fit ) {
146 'continue',
147 "{$row->ll_lang}|{$row->ll_title}|{$row->ll_from}"
148 );
149 break;
150 }
151 }
152 }
153
154 if ( $resultPageSet === null ) {
155 $result->addIndexedTagName( [ 'query', $this->getModuleName() ], 'll' );
156 } else {
157 $resultPageSet->populateFromTitles( $pages );
158 }
159 }
160
162 public function getCacheMode( $params ) {
163 return 'public';
164 }
165
167 public function getAllowedParams() {
168 return [
169 'lang' => null,
170 'title' => null,
171 'continue' => [
172 ApiBase::PARAM_HELP_MSG => 'api-help-param-continue',
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 'prop' => [
182 ParamValidator::PARAM_ISMULTI => true,
183 ParamValidator::PARAM_DEFAULT => '',
184 ParamValidator::PARAM_TYPE => [
185 'lllang',
186 'lltitle',
187 ],
189 ],
190 'dir' => [
191 ParamValidator::PARAM_DEFAULT => 'ascending',
192 ParamValidator::PARAM_TYPE => [
193 'ascending',
194 'descending'
195 ]
196 ],
197 ];
198 }
199
201 protected function getExamplesMessages() {
202 return [
203 'action=query&list=langbacklinks&lbltitle=Test&lbllang=fr'
204 => 'apihelp-query+langbacklinks-example-simple',
205 'action=query&generator=langbacklinks&glbltitle=Test&glbllang=fr&prop=info'
206 => 'apihelp-query+langbacklinks-example-generator',
207 ];
208 }
209
211 public function getHelpUrls() {
212 return 'https://www.mediawiki.org/wiki/Special:MyLanguage/API:Langbacklinks';
213 }
214}
215
217class_alias( ApiQueryLangBacklinks::class, 'ApiQueryLangBacklinks' );
dieWithError( $msg, $code=null, $data=null, $httpCode=0)
Abort execution with an error.
Definition ApiBase.php:1522
getModuleName()
Get the name of the module being executed by this instance.
Definition ApiBase.php:557
parseContinueParamOrDie(string $continue, array $types)
Parse the 'continue' parameter in the usual format and validate the types of each part,...
Definition ApiBase.php:1707
getResult()
Get the result object.
Definition ApiBase.php:696
const PARAM_HELP_MSG_PER_VALUE
((string|array|Message)[]) When PARAM_TYPE is an array, or 'string' with PARAM_ISMULTI,...
Definition ApiBase.php:206
const PARAM_HELP_MSG
(string|array|Message) Specify an alternative i18n documentation message for this parameter.
Definition ApiBase.php:166
const LIMIT_BIG2
Fast query, apihighlimits limit.
Definition ApiBase.php:233
extractRequestParams( $options=[])
Using getAllowedParams(), this function makes an array of the values provided by the user,...
Definition ApiBase.php:837
const LIMIT_BIG1
Fast query, standard limit.
Definition ApiBase.php:231
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.