Merge "Set a title for the context during import on the cli"
[lhc/web/wiklou.git] / includes / api / ApiQueryLangLinks.php
1 <?php
2 /**
3 *
4 *
5 * Created on May 13, 2007
6 *
7 * Copyright © 2006 Yuri Astrakhan "<Firstname><Lastname>@gmail.com"
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License along
20 * with this program; if not, write to the Free Software Foundation, Inc.,
21 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
22 * http://www.gnu.org/copyleft/gpl.html
23 *
24 * @file
25 */
26
27 /**
28 * A query module to list all langlinks (links to corresponding foreign language pages).
29 *
30 * @ingroup API
31 */
32 class ApiQueryLangLinks extends ApiQueryBase {
33
34 public function __construct( $query, $moduleName ) {
35 parent::__construct( $query, $moduleName, 'll' );
36 }
37
38 public function execute() {
39 if ( $this->getPageSet()->getGoodTitleCount() == 0 ) {
40 return;
41 }
42
43 $params = $this->extractRequestParams();
44 $prop = array_flip( (array)$params['prop'] );
45
46 if ( isset( $params['title'] ) && !isset( $params['lang'] ) ) {
47 $this->dieUsageMsg( array( 'missingparam', 'lang' ) );
48 }
49
50 // Handle deprecated param
51 $this->requireMaxOneParameter( $params, 'url', 'prop' );
52 if ( $params['url'] ) {
53 $prop = array( 'url' => 1 );
54 }
55
56 $this->addFields( array(
57 'll_from',
58 'll_lang',
59 'll_title'
60 ) );
61
62 $this->addTables( 'langlinks' );
63 $this->addWhereFld( 'll_from', array_keys( $this->getPageSet()->getGoodTitles() ) );
64 if ( !is_null( $params['continue'] ) ) {
65 $cont = explode( '|', $params['continue'] );
66 $this->dieContinueUsageIf( count( $cont ) != 2 );
67 $op = $params['dir'] == 'descending' ? '<' : '>';
68 $llfrom = intval( $cont[0] );
69 $lllang = $this->getDB()->addQuotes( $cont[1] );
70 $this->addWhere(
71 "ll_from $op $llfrom OR " .
72 "(ll_from = $llfrom AND " .
73 "ll_lang $op= $lllang)"
74 );
75 }
76
77 //FIXME: (follow-up) To allow extensions to add to the language links, we need
78 // to load them all, add the extra links, then apply paging.
79 // Should not be terrible, it's not going to be more than a few hundred links.
80
81 // Note that, since (ll_from, ll_lang) is a unique key, we don't need
82 // to sort by ll_title to ensure deterministic ordering.
83 $sort = ( $params['dir'] == 'descending' ? ' DESC' : '' );
84 if ( isset( $params['lang'] ) ) {
85 $this->addWhereFld( 'll_lang', $params['lang'] );
86 if ( isset( $params['title'] ) ) {
87 $this->addWhereFld( 'll_title', $params['title'] );
88 }
89 $this->addOption( 'ORDER BY', 'll_from' . $sort );
90 } else {
91 // Don't order by ll_from if it's constant in the WHERE clause
92 if ( count( $this->getPageSet()->getGoodTitles() ) == 1 ) {
93 $this->addOption( 'ORDER BY', 'll_lang' . $sort );
94 } else {
95 $this->addOption( 'ORDER BY', array(
96 'll_from' . $sort,
97 'll_lang' . $sort
98 ) );
99 }
100 }
101
102 $this->addOption( 'LIMIT', $params['limit'] + 1 );
103 $res = $this->select( __METHOD__ );
104
105 $count = 0;
106 foreach ( $res as $row ) {
107 if ( ++$count > $params['limit'] ) {
108 // We've reached the one extra which shows that
109 // there are additional pages to be had. Stop here...
110 $this->setContinueEnumParameter( 'continue', "{$row->ll_from}|{$row->ll_lang}" );
111 break;
112 }
113 $entry = array( 'lang' => $row->ll_lang );
114 if ( isset( $prop['url'] ) ) {
115 $title = Title::newFromText( "{$row->ll_lang}:{$row->ll_title}" );
116 if ( $title ) {
117 $entry['url'] = wfExpandUrl( $title->getFullURL(), PROTO_CURRENT );
118 }
119 }
120 if ( isset( $prop['langname'] ) ) {
121 $entry['langname'] = Language::fetchLanguageName( $row->ll_lang, $params['inlanguagecode'] );
122 }
123 if ( isset( $prop['autonym'] ) ) {
124 $entry['autonym'] = Language::fetchLanguageName( $row->ll_lang );
125 }
126 ApiResult::setContent( $entry, $row->ll_title );
127 $fit = $this->addPageSubItem( $row->ll_from, $entry );
128 if ( !$fit ) {
129 $this->setContinueEnumParameter( 'continue', "{$row->ll_from}|{$row->ll_lang}" );
130 break;
131 }
132 }
133 }
134
135 public function getCacheMode( $params ) {
136 return 'public';
137 }
138
139 public function getAllowedParams() {
140 global $wgContLang;
141 return array(
142 'limit' => array(
143 ApiBase::PARAM_DFLT => 10,
144 ApiBase::PARAM_TYPE => 'limit',
145 ApiBase::PARAM_MIN => 1,
146 ApiBase::PARAM_MAX => ApiBase::LIMIT_BIG1,
147 ApiBase::PARAM_MAX2 => ApiBase::LIMIT_BIG2
148 ),
149 'continue' => null,
150 'url' => array(
151 ApiBase::PARAM_DFLT => false,
152 ApiBase::PARAM_DEPRECATED => true,
153 ),
154 'prop' => array(
155 ApiBase::PARAM_ISMULTI => true,
156 ApiBase::PARAM_TYPE => array(
157 'url',
158 'langname',
159 'autonym',
160 )
161 ),
162 'lang' => null,
163 'title' => null,
164 'dir' => array(
165 ApiBase::PARAM_DFLT => 'ascending',
166 ApiBase::PARAM_TYPE => array(
167 'ascending',
168 'descending'
169 )
170 ),
171 'inlanguagecode' => $wgContLang->getCode(),
172 );
173 }
174
175 public function getParamDescription() {
176 return array(
177 'limit' => 'How many langlinks to return',
178 'continue' => 'When more results are available, use this to continue',
179 'url' => "Whether to get the full URL (Cannot be used with {$this->getModulePrefix()}prop)",
180 'prop' => array(
181 'Which additional properties to get for each interlanguage link',
182 ' url - Adds the full URL',
183 ' langname - Adds the localised language name (best effort, use CLDR extension)',
184 " Use {$this->getModulePrefix()}inlanguagecode to control the language",
185 ' autonym - Adds the native language name',
186 ),
187 'lang' => 'Language code',
188 'title' => "Link to search for. Must be used with {$this->getModulePrefix()}lang",
189 'dir' => 'The direction in which to list',
190 'inlanguagecode' => 'Language code for localised language names',
191 );
192 }
193
194 public function getResultProperties() {
195 return array(
196 '' => array(
197 'lang' => 'string',
198 'url' => array(
199 ApiBase::PROP_TYPE => 'string',
200 ApiBase::PROP_NULLABLE => true
201 ),
202 'langname' => array(
203 ApiBase::PROP_TYPE => 'string',
204 ApiBase::PROP_NULLABLE => true
205 ),
206 'autonym' => array(
207 ApiBase::PROP_TYPE => 'string',
208 ApiBase::PROP_NULLABLE => true
209 ),
210 '*' => 'string'
211 )
212 );
213 }
214
215 public function getDescription() {
216 return 'Returns all interlanguage links from the given page(s).';
217 }
218
219 public function getPossibleErrors() {
220 return array_merge( parent::getPossibleErrors(),
221 $this->getRequireMaxOneParameterErrorMessages(
222 array( 'url', 'prop' )
223 ),
224 array(
225 array( 'missingparam', 'lang' ),
226 )
227 );
228 }
229
230 public function getExamples() {
231 return array(
232 'api.php?action=query&prop=langlinks&titles=Main%20Page&redirects='
233 => 'Get interlanguage links from the [[Main Page]]',
234 );
235 }
236
237 public function getHelpUrls() {
238 return 'https://www.mediawiki.org/wiki/API:Properties#langlinks_.2F_ll';
239 }
240 }