Merge "Warn if stateful ParserOutput transforms are used"
[lhc/web/wiklou.git] / includes / api / ApiQueryLangLinks.php
1 <?php
2 /**
3 * Copyright © 2006 Yuri Astrakhan "<Firstname><Lastname>@gmail.com"
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
19 *
20 * @file
21 */
22
23 /**
24 * A query module to list all langlinks (links to corresponding foreign language pages).
25 *
26 * @ingroup API
27 */
28 class ApiQueryLangLinks extends ApiQueryBase {
29
30 public function __construct( ApiQuery $query, $moduleName ) {
31 parent::__construct( $query, $moduleName, 'll' );
32 }
33
34 public function execute() {
35 if ( $this->getPageSet()->getGoodTitleCount() == 0 ) {
36 return;
37 }
38
39 $params = $this->extractRequestParams();
40 $prop = array_flip( (array)$params['prop'] );
41
42 if ( isset( $params['title'] ) && !isset( $params['lang'] ) ) {
43 $this->dieWithError(
44 [
45 'apierror-invalidparammix-mustusewith',
46 $this->encodeParamName( 'title' ),
47 $this->encodeParamName( 'lang' ),
48 ],
49 'invalidparammix'
50 );
51 }
52
53 // Handle deprecated param
54 $this->requireMaxOneParameter( $params, 'url', 'prop' );
55 if ( $params['url'] ) {
56 $prop = [ 'url' => 1 ];
57 }
58
59 $this->addFields( [
60 'll_from',
61 'll_lang',
62 'll_title'
63 ] );
64
65 $this->addTables( 'langlinks' );
66 $this->addWhereFld( 'll_from', array_keys( $this->getPageSet()->getGoodTitles() ) );
67 if ( !is_null( $params['continue'] ) ) {
68 $cont = explode( '|', $params['continue'] );
69 $this->dieContinueUsageIf( count( $cont ) != 2 );
70 $op = $params['dir'] == 'descending' ? '<' : '>';
71 $llfrom = intval( $cont[0] );
72 $lllang = $this->getDB()->addQuotes( $cont[1] );
73 $this->addWhere(
74 "ll_from $op $llfrom OR " .
75 "(ll_from = $llfrom AND " .
76 "ll_lang $op= $lllang)"
77 );
78 }
79
80 // FIXME: (follow-up) To allow extensions to add to the language links, we need
81 // to load them all, add the extra links, then apply paging.
82 // Should not be terrible, it's not going to be more than a few hundred links.
83
84 // Note that, since (ll_from, ll_lang) is a unique key, we don't need
85 // to sort by ll_title to ensure deterministic ordering.
86 $sort = ( $params['dir'] == 'descending' ? ' DESC' : '' );
87 if ( isset( $params['lang'] ) ) {
88 $this->addWhereFld( 'll_lang', $params['lang'] );
89 if ( isset( $params['title'] ) ) {
90 $this->addWhereFld( 'll_title', $params['title'] );
91 }
92 $this->addOption( 'ORDER BY', 'll_from' . $sort );
93 } else {
94 // Don't order by ll_from if it's constant in the WHERE clause
95 if ( count( $this->getPageSet()->getGoodTitles() ) == 1 ) {
96 $this->addOption( 'ORDER BY', 'll_lang' . $sort );
97 } else {
98 $this->addOption( 'ORDER BY', [
99 'll_from' . $sort,
100 'll_lang' . $sort
101 ] );
102 }
103 }
104
105 $this->addOption( 'LIMIT', $params['limit'] + 1 );
106 $res = $this->select( __METHOD__ );
107
108 $count = 0;
109 foreach ( $res as $row ) {
110 if ( ++$count > $params['limit'] ) {
111 // We've reached the one extra which shows that
112 // there are additional pages to be had. Stop here...
113 $this->setContinueEnumParameter( 'continue', "{$row->ll_from}|{$row->ll_lang}" );
114 break;
115 }
116 $entry = [ 'lang' => $row->ll_lang ];
117 if ( isset( $prop['url'] ) ) {
118 $title = Title::newFromText( "{$row->ll_lang}:{$row->ll_title}" );
119 if ( $title ) {
120 $entry['url'] = wfExpandUrl( $title->getFullURL(), PROTO_CURRENT );
121 }
122 }
123 if ( isset( $prop['langname'] ) ) {
124 $entry['langname'] = Language::fetchLanguageName( $row->ll_lang, $params['inlanguagecode'] );
125 }
126 if ( isset( $prop['autonym'] ) ) {
127 $entry['autonym'] = Language::fetchLanguageName( $row->ll_lang );
128 }
129 ApiResult::setContentValue( $entry, 'title', $row->ll_title );
130 $fit = $this->addPageSubItem( $row->ll_from, $entry );
131 if ( !$fit ) {
132 $this->setContinueEnumParameter( 'continue', "{$row->ll_from}|{$row->ll_lang}" );
133 break;
134 }
135 }
136 }
137
138 public function getCacheMode( $params ) {
139 return 'public';
140 }
141
142 public function getAllowedParams() {
143 global $wgContLang;
144 return [
145 'prop' => [
146 ApiBase::PARAM_ISMULTI => true,
147 ApiBase::PARAM_TYPE => [
148 'url',
149 'langname',
150 'autonym',
151 ],
152 ApiBase::PARAM_HELP_MSG_PER_VALUE => [],
153 ],
154 'lang' => null,
155 'title' => null,
156 'dir' => [
157 ApiBase::PARAM_DFLT => 'ascending',
158 ApiBase::PARAM_TYPE => [
159 'ascending',
160 'descending'
161 ]
162 ],
163 'inlanguagecode' => $wgContLang->getCode(),
164 'limit' => [
165 ApiBase::PARAM_DFLT => 10,
166 ApiBase::PARAM_TYPE => 'limit',
167 ApiBase::PARAM_MIN => 1,
168 ApiBase::PARAM_MAX => ApiBase::LIMIT_BIG1,
169 ApiBase::PARAM_MAX2 => ApiBase::LIMIT_BIG2
170 ],
171 'continue' => [
172 ApiBase::PARAM_HELP_MSG => 'api-help-param-continue',
173 ],
174 'url' => [
175 ApiBase::PARAM_DFLT => false,
176 ApiBase::PARAM_DEPRECATED => true,
177 ],
178 ];
179 }
180
181 protected function getExamplesMessages() {
182 return [
183 'action=query&prop=langlinks&titles=Main%20Page&redirects='
184 => 'apihelp-query+langlinks-example-simple',
185 ];
186 }
187
188 public function getHelpUrls() {
189 return 'https://www.mediawiki.org/wiki/Special:MyLanguage/API:Langlinks';
190 }
191 }