f16e804e9e08ed6901ffc21818f5492bfe38c5da
[lhc/web/wiklou.git] / includes / api / ApiQueryLangLinks.php
1 <?php
2 /**
3 * API for MediaWiki 1.8+
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 $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 // Don't order by ll_from if it's constant in the WHERE clause
73 if ( count( $this->getPageSet()->getGoodTitles() ) == 1 ) {
74 $this->addOption( 'ORDER BY', 'll_lang' );
75 } else {
76 $this->addOption( 'ORDER BY', 'll_from, ll_lang' );
77 }
78 $this->addOption( 'LIMIT', $params['limit'] + 1 );
79 $res = $this->select( __METHOD__ );
80
81 $count = 0;
82 foreach ( $res as $row ) {
83 if ( ++$count > $params['limit'] ) {
84 // We've reached the one extra which shows that
85 // there are additional pages to be had. Stop here...
86 $this->setContinueEnumParameter( 'continue', "{$row->ll_from}|{$row->ll_lang}" );
87 break;
88 }
89 $entry = array( 'lang' => $row->ll_lang );
90 ApiResult::setContent( $entry, $row->ll_title );
91 $fit = $this->addPageSubItem( $row->ll_from, $entry );
92 if ( !$fit ) {
93 $this->setContinueEnumParameter( 'continue', "{$row->ll_from}|{$row->ll_lang}" );
94 break;
95 }
96 }
97 }
98
99 public function getCacheMode( $params ) {
100 return 'public';
101 }
102
103 public function getAllowedParams() {
104 return array(
105 'limit' => array(
106 ApiBase::PARAM_DFLT => 10,
107 ApiBase::PARAM_TYPE => 'limit',
108 ApiBase::PARAM_MIN => 1,
109 ApiBase::PARAM_MAX => ApiBase::LIMIT_BIG1,
110 ApiBase::PARAM_MAX2 => ApiBase::LIMIT_BIG2
111 ),
112 'continue' => null,
113 );
114 }
115
116 public function getParamDescription() {
117 return array(
118 'limit' => 'How many langlinks to return',
119 'continue' => 'When more results are available, use this to continue',
120 );
121 }
122
123 public function getDescription() {
124 return 'Returns all interlanguage links from the given page(s)';
125 }
126
127 public function getPossibleErrors() {
128 return array_merge( parent::getPossibleErrors(), array(
129 array( 'code' => '_badcontinue', 'info' => 'Invalid continue param. You should pass the original value returned by the previous query' ),
130 ) );
131 }
132
133 protected function getExamples() {
134 return array(
135 'Get interlanguage links from the [[Main Page]]:',
136 ' api.php?action=query&prop=langlinks&titles=Main%20Page&redirects',
137 );
138 }
139
140 public function getVersion() {
141 return __CLASS__ . ': $Id$';
142 }
143 }