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