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