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