Merge "Don't check namespace in SpecialWantedtemplates"
[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( ApiQuery $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 $this->logFeatureUsage( 'prop=langlinks&llurl' );
54 $prop = array( 'url' => 1 );
55 }
56
57 $this->addFields( array(
58 'll_from',
59 'll_lang',
60 'll_title'
61 ) );
62
63 $this->addTables( 'langlinks' );
64 $this->addWhereFld( 'll_from', array_keys( $this->getPageSet()->getGoodTitles() ) );
65 if ( !is_null( $params['continue'] ) ) {
66 $cont = explode( '|', $params['continue'] );
67 $this->dieContinueUsageIf( count( $cont ) != 2 );
68 $op = $params['dir'] == 'descending' ? '<' : '>';
69 $llfrom = intval( $cont[0] );
70 $lllang = $this->getDB()->addQuotes( $cont[1] );
71 $this->addWhere(
72 "ll_from $op $llfrom OR " .
73 "(ll_from = $llfrom AND " .
74 "ll_lang $op= $lllang)"
75 );
76 }
77
78 //FIXME: (follow-up) To allow extensions to add to the language links, we need
79 // to load them all, add the extra links, then apply paging.
80 // Should not be terrible, it's not going to be more than a few hundred links.
81
82 // Note that, since (ll_from, ll_lang) is a unique key, we don't need
83 // to sort by ll_title to ensure deterministic ordering.
84 $sort = ( $params['dir'] == 'descending' ? ' DESC' : '' );
85 if ( isset( $params['lang'] ) ) {
86 $this->addWhereFld( 'll_lang', $params['lang'] );
87 if ( isset( $params['title'] ) ) {
88 $this->addWhereFld( 'll_title', $params['title'] );
89 }
90 $this->addOption( 'ORDER BY', 'll_from' . $sort );
91 } else {
92 // Don't order by ll_from if it's constant in the WHERE clause
93 if ( count( $this->getPageSet()->getGoodTitles() ) == 1 ) {
94 $this->addOption( 'ORDER BY', 'll_lang' . $sort );
95 } else {
96 $this->addOption( 'ORDER BY', array(
97 'll_from' . $sort,
98 'll_lang' . $sort
99 ) );
100 }
101 }
102
103 $this->addOption( 'LIMIT', $params['limit'] + 1 );
104 $res = $this->select( __METHOD__ );
105
106 $count = 0;
107 foreach ( $res as $row ) {
108 if ( ++$count > $params['limit'] ) {
109 // We've reached the one extra which shows that
110 // there are additional pages to be had. Stop here...
111 $this->setContinueEnumParameter( 'continue', "{$row->ll_from}|{$row->ll_lang}" );
112 break;
113 }
114 $entry = array( 'lang' => $row->ll_lang );
115 if ( isset( $prop['url'] ) ) {
116 $title = Title::newFromText( "{$row->ll_lang}:{$row->ll_title}" );
117 if ( $title ) {
118 $entry['url'] = wfExpandUrl( $title->getFullURL(), PROTO_CURRENT );
119 }
120 }
121 if ( isset( $prop['langname'] ) ) {
122 $entry['langname'] = Language::fetchLanguageName( $row->ll_lang, $params['inlanguagecode'] );
123 }
124 if ( isset( $prop['autonym'] ) ) {
125 $entry['autonym'] = Language::fetchLanguageName( $row->ll_lang );
126 }
127 ApiResult::setContentValue( $entry, 'title', $row->ll_title );
128 $fit = $this->addPageSubItem( $row->ll_from, $entry );
129 if ( !$fit ) {
130 $this->setContinueEnumParameter( 'continue', "{$row->ll_from}|{$row->ll_lang}" );
131 break;
132 }
133 }
134 }
135
136 public function getCacheMode( $params ) {
137 return 'public';
138 }
139
140 public function getAllowedParams() {
141 global $wgContLang;
142 return array(
143 'prop' => array(
144 ApiBase::PARAM_ISMULTI => true,
145 ApiBase::PARAM_TYPE => array(
146 'url',
147 'langname',
148 'autonym',
149 ),
150 ApiBase::PARAM_HELP_MSG_PER_VALUE => array(),
151 ),
152 'lang' => null,
153 'title' => null,
154 'dir' => array(
155 ApiBase::PARAM_DFLT => 'ascending',
156 ApiBase::PARAM_TYPE => array(
157 'ascending',
158 'descending'
159 )
160 ),
161 'inlanguagecode' => $wgContLang->getCode(),
162 'limit' => array(
163 ApiBase::PARAM_DFLT => 10,
164 ApiBase::PARAM_TYPE => 'limit',
165 ApiBase::PARAM_MIN => 1,
166 ApiBase::PARAM_MAX => ApiBase::LIMIT_BIG1,
167 ApiBase::PARAM_MAX2 => ApiBase::LIMIT_BIG2
168 ),
169 'continue' => array(
170 ApiBase::PARAM_HELP_MSG => 'api-help-param-continue',
171 ),
172 'url' => array(
173 ApiBase::PARAM_DFLT => false,
174 ApiBase::PARAM_DEPRECATED => true,
175 ),
176 );
177 }
178
179 protected function getExamplesMessages() {
180 return array(
181 'action=query&prop=langlinks&titles=Main%20Page&redirects='
182 => 'apihelp-query+langlinks-example-simple',
183 );
184 }
185
186 public function getHelpUrls() {
187 return 'https://www.mediawiki.org/wiki/API:Langlinks';
188 }
189 }