Merge "Special:Upload should not crash on failing previews"
[mediawiki.git] / includes / api / ApiQueryLangLinks.php
blob55e3c85265a58574f6ac2e4fe5a461dd11b0c867
1 <?php
2 /**
5 * Created on May 13, 2007
7 * Copyright © 2006 Yuri Astrakhan "<Firstname><Lastname>@gmail.com"
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.
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.
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
24 * @file
27 /**
28 * A query module to list all langlinks (links to corresponding foreign language pages).
30 * @ingroup API
32 class ApiQueryLangLinks extends ApiQueryBase {
34 public function __construct( ApiQuery $query, $moduleName ) {
35 parent::__construct( $query, $moduleName, 'll' );
38 public function execute() {
39 if ( $this->getPageSet()->getGoodTitleCount() == 0 ) {
40 return;
43 $params = $this->extractRequestParams();
44 $prop = array_flip( (array)$params['prop'] );
46 if ( isset( $params['title'] ) && !isset( $params['lang'] ) ) {
47 $this->dieWithError(
49 'apierror-invalidparammix-mustusewith',
50 $this->encodeParamName( 'title' ),
51 $this->encodeParamName( 'lang' ),
53 'invalidparammix'
57 // Handle deprecated param
58 $this->requireMaxOneParameter( $params, 'url', 'prop' );
59 if ( $params['url'] ) {
60 $prop = [ 'url' => 1 ];
63 $this->addFields( [
64 'll_from',
65 'll_lang',
66 'll_title'
67 ] );
69 $this->addTables( 'langlinks' );
70 $this->addWhereFld( 'll_from', array_keys( $this->getPageSet()->getGoodTitles() ) );
71 if ( !is_null( $params['continue'] ) ) {
72 $cont = explode( '|', $params['continue'] );
73 $this->dieContinueUsageIf( count( $cont ) != 2 );
74 $op = $params['dir'] == 'descending' ? '<' : '>';
75 $llfrom = intval( $cont[0] );
76 $lllang = $this->getDB()->addQuotes( $cont[1] );
77 $this->addWhere(
78 "ll_from $op $llfrom OR " .
79 "(ll_from = $llfrom AND " .
80 "ll_lang $op= $lllang)"
84 // FIXME: (follow-up) To allow extensions to add to the language links, we need
85 // to load them all, add the extra links, then apply paging.
86 // Should not be terrible, it's not going to be more than a few hundred links.
88 // Note that, since (ll_from, ll_lang) is a unique key, we don't need
89 // to sort by ll_title to ensure deterministic ordering.
90 $sort = ( $params['dir'] == 'descending' ? ' DESC' : '' );
91 if ( isset( $params['lang'] ) ) {
92 $this->addWhereFld( 'll_lang', $params['lang'] );
93 if ( isset( $params['title'] ) ) {
94 $this->addWhereFld( 'll_title', $params['title'] );
96 $this->addOption( 'ORDER BY', 'll_from' . $sort );
97 } else {
98 // Don't order by ll_from if it's constant in the WHERE clause
99 if ( count( $this->getPageSet()->getGoodTitles() ) == 1 ) {
100 $this->addOption( 'ORDER BY', 'll_lang' . $sort );
101 } else {
102 $this->addOption( 'ORDER BY', [
103 'll_from' . $sort,
104 'll_lang' . $sort
105 ] );
109 $this->addOption( 'LIMIT', $params['limit'] + 1 );
110 $res = $this->select( __METHOD__ );
112 $count = 0;
113 foreach ( $res as $row ) {
114 if ( ++$count > $params['limit'] ) {
115 // We've reached the one extra which shows that
116 // there are additional pages to be had. Stop here...
117 $this->setContinueEnumParameter( 'continue', "{$row->ll_from}|{$row->ll_lang}" );
118 break;
120 $entry = [ 'lang' => $row->ll_lang ];
121 if ( isset( $prop['url'] ) ) {
122 $title = Title::newFromText( "{$row->ll_lang}:{$row->ll_title}" );
123 if ( $title ) {
124 $entry['url'] = wfExpandUrl( $title->getFullURL(), PROTO_CURRENT );
127 if ( isset( $prop['langname'] ) ) {
128 $entry['langname'] = Language::fetchLanguageName( $row->ll_lang, $params['inlanguagecode'] );
130 if ( isset( $prop['autonym'] ) ) {
131 $entry['autonym'] = Language::fetchLanguageName( $row->ll_lang );
133 ApiResult::setContentValue( $entry, 'title', $row->ll_title );
134 $fit = $this->addPageSubItem( $row->ll_from, $entry );
135 if ( !$fit ) {
136 $this->setContinueEnumParameter( 'continue', "{$row->ll_from}|{$row->ll_lang}" );
137 break;
142 public function getCacheMode( $params ) {
143 return 'public';
146 public function getAllowedParams() {
147 global $wgContLang;
148 return [
149 'prop' => [
150 ApiBase::PARAM_ISMULTI => true,
151 ApiBase::PARAM_TYPE => [
152 'url',
153 'langname',
154 'autonym',
156 ApiBase::PARAM_HELP_MSG_PER_VALUE => [],
158 'lang' => null,
159 'title' => null,
160 'dir' => [
161 ApiBase::PARAM_DFLT => 'ascending',
162 ApiBase::PARAM_TYPE => [
163 'ascending',
164 'descending'
167 'inlanguagecode' => $wgContLang->getCode(),
168 'limit' => [
169 ApiBase::PARAM_DFLT => 10,
170 ApiBase::PARAM_TYPE => 'limit',
171 ApiBase::PARAM_MIN => 1,
172 ApiBase::PARAM_MAX => ApiBase::LIMIT_BIG1,
173 ApiBase::PARAM_MAX2 => ApiBase::LIMIT_BIG2
175 'continue' => [
176 ApiBase::PARAM_HELP_MSG => 'api-help-param-continue',
178 'url' => [
179 ApiBase::PARAM_DFLT => false,
180 ApiBase::PARAM_DEPRECATED => true,
185 protected function getExamplesMessages() {
186 return [
187 'action=query&prop=langlinks&titles=Main%20Page&redirects='
188 => 'apihelp-query+langlinks-example-simple',
192 public function getHelpUrls() {
193 return 'https://www.mediawiki.org/wiki/API:Langlinks';