3 * Copyright © 2006 Yuri Astrakhan "<Firstname><Lastname>@gmail.com"
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.
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.
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
24 * A query module to list all langlinks (links to corresponding foreign language pages).
28 class ApiQueryLangLinks
extends ApiQueryBase
{
30 public function __construct( ApiQuery
$query, $moduleName ) {
31 parent
::__construct( $query, $moduleName, 'll' );
34 public function execute() {
35 if ( $this->getPageSet()->getGoodTitleCount() == 0 ) {
39 $params = $this->extractRequestParams();
40 $prop = array_flip( (array)$params['prop'] );
42 if ( isset( $params['title'] ) && !isset( $params['lang'] ) ) {
45 'apierror-invalidparammix-mustusewith',
46 $this->encodeParamName( 'title' ),
47 $this->encodeParamName( 'lang' ),
53 // Handle deprecated param
54 $this->requireMaxOneParameter( $params, 'url', 'prop' );
55 if ( $params['url'] ) {
56 $prop = [ 'url' => 1 ];
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] );
74 "ll_from $op $llfrom OR " .
75 "(ll_from = $llfrom AND " .
76 "ll_lang $op= $lllang)"
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.
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'] );
92 $this->addOption( 'ORDER BY', 'll_from' . $sort );
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 );
98 $this->addOption( 'ORDER BY', [
105 $this->addOption( 'LIMIT', $params['limit'] +
1 );
106 $res = $this->select( __METHOD__
);
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}" );
116 $entry = [ 'lang' => $row->ll_lang
];
117 if ( isset( $prop['url'] ) ) {
118 $title = Title
::newFromText( "{$row->ll_lang}:{$row->ll_title}" );
120 $entry['url'] = wfExpandUrl( $title->getFullURL(), PROTO_CURRENT
);
123 if ( isset( $prop['langname'] ) ) {
124 $entry['langname'] = Language
::fetchLanguageName( $row->ll_lang
, $params['inlanguagecode'] );
126 if ( isset( $prop['autonym'] ) ) {
127 $entry['autonym'] = Language
::fetchLanguageName( $row->ll_lang
);
129 ApiResult
::setContentValue( $entry, 'title', $row->ll_title
);
130 $fit = $this->addPageSubItem( $row->ll_from
, $entry );
132 $this->setContinueEnumParameter( 'continue', "{$row->ll_from}|{$row->ll_lang}" );
138 public function getCacheMode( $params ) {
142 public function getAllowedParams() {
146 ApiBase
::PARAM_ISMULTI
=> true,
147 ApiBase
::PARAM_TYPE
=> [
152 ApiBase
::PARAM_HELP_MSG_PER_VALUE
=> [],
157 ApiBase
::PARAM_DFLT
=> 'ascending',
158 ApiBase
::PARAM_TYPE
=> [
163 'inlanguagecode' => $wgContLang->getCode(),
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
172 ApiBase
::PARAM_HELP_MSG
=> 'api-help-param-continue',
175 ApiBase
::PARAM_DFLT
=> false,
176 ApiBase
::PARAM_DEPRECATED
=> true,
181 protected function getExamplesMessages() {
183 'action=query&prop=langlinks&titles=Main%20Page&redirects='
184 => 'apihelp-query+langlinks-example-simple',
188 public function getHelpUrls() {
189 return 'https://www.mediawiki.org/wiki/Special:MyLanguage/API:Langlinks';