Debian Codegraph @

includes/api/ApiQueryLangBacklinks.php

raw symbols SCIP
1 <?php
2 /**
3 * API for MediaWiki 1.17+
4 *
5 * Copyright © 2011 Sam Reed
6 * Copyright © 2006 Yuri Astrakhan "<Firstname><Lastname>@gmail.com"
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License along
19 * with this program; if not, write to the Free Software Foundation, Inc.,
20 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
21 * http://www.gnu.org/copyleft/gpl.html
22 *
23 * @file
24 */
25
26 namespace MediaWiki\Api;
27
28 use MediaWiki\Title\Title;
29 use Wikimedia\ParamValidator\ParamValidator;
30 use Wikimedia\ParamValidator\TypeDef\IntegerDef;
31
32 /**
33 * This gives links pointing to the given interwiki
34 * @ingroup API
35 */
36 class ApiQueryLangBacklinks extends ApiQueryGeneratorBase {
37
38 public function __construct( ApiQuery $query, string $moduleName ) {
39 parent::__construct( $query, $moduleName, 'lbl' );
40 }
41
42 public function execute() {
43 $this->run();
44 }
45
46 public function executeGenerator( $resultPageSet ) {
47 $this->run( $resultPageSet );
48 }
49
50 /**
51 * @param ApiPageSet|null $resultPageSet
52 * @return void
53 */
54 public function run( $resultPageSet = null ) {
55 $params = $this->extractRequestParams();
56
57 if ( isset( $params['title'] ) && !isset( $params['lang'] ) ) {
58 $this->dieWithError(
59 [
60 'apierror-invalidparammix-mustusewith',
61 $this->encodeParamName( 'title' ),
62 $this->encodeParamName( 'lang' )
63 ],
64 'nolang'
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 'll_lang' => $cont[0],
74 'll_title' => $cont[1],
75 'll_from' => $cont[2],
76 ] ) );
77 }
78
79 $prop = array_fill_keys( $params['prop'], true );
80 $lllang = isset( $prop['lllang'] );
81 $lltitle = isset( $prop['lltitle'] );
82
83 $this->addTables( [ 'langlinks', 'page' ] );
84 $this->addWhere( 'll_from = page_id' );
85
86 $this->addFields( [ 'page_id', 'page_title', 'page_namespace', 'page_is_redirect',
87 'll_from', 'll_lang', 'll_title' ] );
88
89 $sort = ( $params['dir'] == 'descending' ? ' DESC' : '' );
90 if ( isset( $params['lang'] ) ) {
91 $this->addWhereFld( 'll_lang', $params['lang'] );
92 if ( isset( $params['title'] ) ) {
93 $this->addWhereFld( 'll_title', $params['title'] );
94 $this->addOption( 'ORDER BY', 'll_from' . $sort );
95 } else {
96 $this->addOption( 'ORDER BY', [
97 'll_title' . $sort,
98 'll_from' . $sort
99 ] );
100 }
101 } else {
102 $this->addOption( 'ORDER BY', [
103 'll_lang' . $sort,
104 'll_title' . $sort,
105 'll_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... Continue string
126 // preserved in case the redirect query doesn't pass the limit.
127 $this->setContinueEnumParameter(
128 'continue',
129 "{$row->ll_lang}|{$row->ll_title}|{$row->ll_from}"
130 );
131 break;
132 }
133
134 if ( $resultPageSet !== null ) {
135 $pages[] = Title::newFromRow( $row );
136 } else {
137 $entry = [ 'pageid' => (int)$row->page_id ];
138
139 $title = Title::makeTitle( $row->page_namespace, $row->page_title );
140 ApiQueryBase::addTitleInfo( $entry, $title );
141
142 if ( $row->page_is_redirect ) {
143 $entry['redirect'] = true;
144 }
145
146 if ( $lllang ) {
147 $entry['lllang'] = $row->ll_lang;
148 }
149
150 if ( $lltitle ) {
151 $entry['lltitle'] = $row->ll_title;
152 }
153
154 $fit = $result->addValue( [ 'query', $this->getModuleName() ], null, $entry );
155 if ( !$fit ) {
156 $this->setContinueEnumParameter(
157 'continue',
158 "{$row->ll_lang}|{$row->ll_title}|{$row->ll_from}"
159 );
160 break;
161 }
162 }
163 }
164
165 if ( $resultPageSet === null ) {
166 $result->addIndexedTagName( [ 'query', $this->getModuleName() ], 'll' );
167 } else {
168 $resultPageSet->populateFromTitles( $pages );
169 }
170 }
171
172 public function getCacheMode( $params ) {
173 return 'public';
174 }
175
176 public function getAllowedParams() {
177 return [
178 'lang' => null,
179 'title' => null,
180 'continue' => [
181 ApiBase::PARAM_HELP_MSG => 'api-help-param-continue',
182 ],
183 'limit' => [
184 ParamValidator::PARAM_DEFAULT => 10,
185 ParamValidator::PARAM_TYPE => 'limit',
186 IntegerDef::PARAM_MIN => 1,
187 IntegerDef::PARAM_MAX => ApiBase::LIMIT_BIG1,
188 IntegerDef::PARAM_MAX2 => ApiBase::LIMIT_BIG2
189 ],
190 'prop' => [
191 ParamValidator::PARAM_ISMULTI => true,
192 ParamValidator::PARAM_DEFAULT => '',
193 ParamValidator::PARAM_TYPE => [
194 'lllang',
195 'lltitle',
196 ],
197 ApiBase::PARAM_HELP_MSG_PER_VALUE => [],
198 ],
199 'dir' => [
200 ParamValidator::PARAM_DEFAULT => 'ascending',
201 ParamValidator::PARAM_TYPE => [
202 'ascending',
203 'descending'
204 ]
205 ],
206 ];
207 }
208
209 protected function getExamplesMessages() {
210 return [
211 'action=query&list=langbacklinks&lbltitle=Test&lbllang=fr'
212 => 'apihelp-query+langbacklinks-example-simple',
213 'action=query&generator=langbacklinks&glbltitle=Test&glbllang=fr&prop=info'
214 => 'apihelp-query+langbacklinks-example-generator',
215 ];
216 }
217
218 public function getHelpUrls() {
219 return 'https://www.mediawiki.org/wiki/Special:MyLanguage/API:Langbacklinks';
220 }
221 }
222
223 /** @deprecated class alias since 1.43 */
224 class_alias( ApiQueryLangBacklinks::class, 'ApiQueryLangBacklinks' );