]> scripts.mit.edu Git - autoinstalls/mediawiki.git/blob - includes/api/ApiQueryLangLinks.php
MediaWiki 1.17.4
[autoinstalls/mediawiki.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                         if ( $params['url'] ) {
91                                 $title = Title::newFromText( "{$row->ll_lang}:{$row->ll_title}" );
92                                 if ( $title ) {
93                                         $entry['url'] = $title->getFullURL();
94                                 }
95                         }
96                         ApiResult::setContent( $entry, $row->ll_title );
97                         $fit = $this->addPageSubItem( $row->ll_from, $entry );
98                         if ( !$fit ) {
99                                 $this->setContinueEnumParameter( 'continue', "{$row->ll_from}|{$row->ll_lang}" );
100                                 break;
101                         }
102                 }
103         }
104
105         public function getCacheMode( $params ) {
106                 return 'public';
107         }
108
109         public function getAllowedParams() {
110                 return array(
111                         'limit' => array(
112                                 ApiBase::PARAM_DFLT => 10,
113                                 ApiBase::PARAM_TYPE => 'limit',
114                                 ApiBase::PARAM_MIN => 1,
115                                 ApiBase::PARAM_MAX => ApiBase::LIMIT_BIG1,
116                                 ApiBase::PARAM_MAX2 => ApiBase::LIMIT_BIG2
117                         ),
118                         'continue' => null,
119                         'url' => false,
120                 );
121         }
122
123         public function getParamDescription() {
124                 return array(
125                         'limit' => 'How many langlinks to return',
126                         'continue' => 'When more results are available, use this to continue',
127                         'url' => 'Whether to get the full URL',
128                 );
129         }
130
131         public function getDescription() {
132                 return 'Returns all interlanguage links from the given page(s)';
133         }
134
135         public function getPossibleErrors() {
136                 return array_merge( parent::getPossibleErrors(), array(
137                         array( 'code' => '_badcontinue', 'info' => 'Invalid continue param. You should pass the original value returned by the previous query' ),
138                 ) );
139         }
140
141         protected function getExamples() {
142                 return array(
143                         'Get interlanguage links from the [[Main Page]]:',
144                         '  api.php?action=query&prop=langlinks&titles=Main%20Page&redirects=',
145                 );
146         }
147
148         public function getVersion() {
149                 return __CLASS__ . ': $Id$';
150         }
151 }