3 * API for MediaWiki 1.8+
5 * Created on May 13, 2007
7 * Copyright © 2006 Yuri Astrakhan <Firstname><Lastname>@gmail.com
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.
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.
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
27 if ( !defined( 'MEDIAWIKI' ) ) {
28 // Eclipse helper - will be ignored in production
29 require_once( "ApiQueryBase.php" );
33 * A query module to list all langlinks (links to correspanding foreign language pages).
37 class ApiQueryLangLinks extends ApiQueryBase {
39 public function __construct( $query, $moduleName ) {
40 parent::__construct( $query, $moduleName, 'll' );
43 public function execute() {
44 if ( $this->getPageSet()->getGoodTitleCount() == 0 ) {
48 $params = $this->extractRequestParams();
49 $this->addFields( array(
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' );
63 $llfrom = intval( $cont[0] );
64 $lllang = $this->getDB()->strencode( $cont[1] );
66 "ll_from > $llfrom OR " .
67 "(ll_from = $llfrom AND " .
68 "ll_lang >= '$lllang')"
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' );
76 $this->addOption( 'ORDER BY', 'll_from, ll_lang' );
78 $this->addOption( 'LIMIT', $params['limit'] + 1 );
79 $res = $this->select( __METHOD__ );
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}" );
89 $entry = array( 'lang' => $row->ll_lang );
90 if ( $params['url'] ) {
91 $title = Title::newFromText( "{$row->ll_lang}:{$row->ll_title}" );
93 $entry['url'] = $title->getFullURL();
96 ApiResult::setContent( $entry, $row->ll_title );
97 $fit = $this->addPageSubItem( $row->ll_from, $entry );
99 $this->setContinueEnumParameter( 'continue', "{$row->ll_from}|{$row->ll_lang}" );
105 public function getCacheMode( $params ) {
109 public function getAllowedParams() {
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
123 public function getParamDescription() {
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',
131 public function getDescription() {
132 return 'Returns all interlanguage links from the given page(s)';
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' ),
141 protected function getExamples() {
143 'Get interlanguage links from the [[Main Page]]:',
144 ' api.php?action=query&prop=langlinks&titles=Main%20Page&redirects=',
148 public function getVersion() {
149 return __CLASS__ . ': $Id$';