Merge "Undo feature: Add a wpUndidRevision field that can be used to distinguish...
[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 correspanding 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
45 if ( isset( $params['title'] ) && !isset( $params['lang'] ) ) {
46 $this->dieUsageMsg( array( 'missingparam', 'lang' ) );
47 }
48
49 $this->addFields( array(
50 'll_from',
51 'll_lang',
52 'll_title'
53 ) );
54
55 $this->addTables( 'langlinks' );
56 $this->addWhereFld( 'll_from', array_keys( $this->getPageSet()->getGoodTitles() ) );
57 if ( !is_null( $params['continue'] ) ) {
58 $cont = explode( '|', $params['continue'] );
59 if ( count( $cont ) != 2 ) {
60 $this->dieUsage( 'Invalid continue param. You should pass the ' .
61 'original value returned by the previous query', '_badcontinue' );
62 }
63 $op = $params['dir'] == 'descending' ? '<' : '>';
64 $llfrom = intval( $cont[0] );
65 $lllang = $this->getDB()->addQuotes( $cont[1] );
66 $this->addWhere(
67 "ll_from $op $llfrom OR " .
68 "(ll_from = $llfrom AND " .
69 "ll_lang $op= $lllang)"
70 );
71 }
72
73 $sort = ( $params['dir'] == 'descending' ? ' DESC' : '' );
74 if ( isset( $params['lang'] ) ) {
75 $this->addWhereFld( 'll_lang', $params['lang'] );
76 if ( isset( $params['title'] ) ) {
77 $this->addWhereFld( 'll_title', $params['title'] );
78 $this->addOption( 'ORDER BY', 'll_from' . $sort );
79 } else {
80 $this->addOption( 'ORDER BY', array(
81 'll_title' . $sort,
82 'll_from' . $sort
83 ));
84 }
85 } else {
86 // Don't order by ll_from if it's constant in the WHERE clause
87 if ( count( $this->getPageSet()->getGoodTitles() ) == 1 ) {
88 $this->addOption( 'ORDER BY', 'll_lang' . $sort );
89 } else {
90 $this->addOption( 'ORDER BY', array(
91 'll_from' . $sort,
92 'll_lang' . $sort
93 ));
94 }
95 }
96
97 $this->addOption( 'LIMIT', $params['limit'] + 1 );
98 $res = $this->select( __METHOD__ );
99
100 $count = 0;
101 foreach ( $res as $row ) {
102 if ( ++$count > $params['limit'] ) {
103 // We've reached the one extra which shows that
104 // there are additional pages to be had. Stop here...
105 $this->setContinueEnumParameter( 'continue', "{$row->ll_from}|{$row->ll_lang}" );
106 break;
107 }
108 $entry = array( 'lang' => $row->ll_lang );
109 if ( $params['url'] ) {
110 $title = Title::newFromText( "{$row->ll_lang}:{$row->ll_title}" );
111 if ( $title ) {
112 $entry['url'] = wfExpandUrl( $title->getFullURL(), PROTO_CURRENT );
113 }
114 }
115 ApiResult::setContent( $entry, $row->ll_title );
116 $fit = $this->addPageSubItem( $row->ll_from, $entry );
117 if ( !$fit ) {
118 $this->setContinueEnumParameter( 'continue', "{$row->ll_from}|{$row->ll_lang}" );
119 break;
120 }
121 }
122 }
123
124 public function getCacheMode( $params ) {
125 return 'public';
126 }
127
128 public function getAllowedParams() {
129 return array(
130 'limit' => array(
131 ApiBase::PARAM_DFLT => 10,
132 ApiBase::PARAM_TYPE => 'limit',
133 ApiBase::PARAM_MIN => 1,
134 ApiBase::PARAM_MAX => ApiBase::LIMIT_BIG1,
135 ApiBase::PARAM_MAX2 => ApiBase::LIMIT_BIG2
136 ),
137 'continue' => null,
138 'url' => false,
139 'lang' => null,
140 'title' => null,
141 'dir' => array(
142 ApiBase::PARAM_DFLT => 'ascending',
143 ApiBase::PARAM_TYPE => array(
144 'ascending',
145 'descending'
146 )
147 ),
148 );
149 }
150
151 public function getParamDescription() {
152 return array(
153 'limit' => 'How many langlinks to return',
154 'continue' => 'When more results are available, use this to continue',
155 'url' => 'Whether to get the full URL',
156 'lang' => 'Language code',
157 'title' => "Link to search for. Must be used with {$this->getModulePrefix()}lang",
158 'dir' => 'The direction in which to list',
159 );
160 }
161
162 public function getResultProperties() {
163 return array(
164 '' => array(
165 'lang' => 'string',
166 'url' => array(
167 ApiBase::PROP_TYPE => 'string',
168 ApiBase::PROP_NULLABLE => true
169 ),
170 '*' => 'string'
171 )
172 );
173 }
174
175 public function getDescription() {
176 return 'Returns all interlanguage links from the given page(s)';
177 }
178
179 public function getPossibleErrors() {
180 return array_merge( parent::getPossibleErrors(), array(
181 array( 'missingparam', 'lang' ),
182 array( 'code' => '_badcontinue', 'info' => 'Invalid continue param. You should pass the original value returned by the previous query' ),
183 ) );
184 }
185
186 public function getExamples() {
187 return array(
188 'api.php?action=query&prop=langlinks&titles=Main%20Page&redirects=' => 'Get interlanguage links from the [[Main Page]]',
189 );
190 }
191
192 public function getHelpUrls() {
193 return 'https://www.mediawiki.org/wiki/API:Properties#langlinks_.2F_ll';
194 }
195
196 public function getVersion() {
197 return __CLASS__ . ': $Id$';
198 }
199 }