513640559486162f5fc71e53db703e1ae84241ed
4 * Created on May 14, 2010
6 * API for MediaWiki 1.17+
8 * Copyright © 2010 Sam Reed
9 * Copyright © 2006 Yuri Astrakhan <Firstname><Lastname>@gmail.com
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
21 * You should have received a copy of the GNU General Public License along
22 * with this program; if not, write to the Free Software Foundation, Inc.,
23 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
24 * 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 interwiki links on a page
37 class ApiQueryIWLinks
extends ApiQueryBase
{
39 public function __construct( $query, $moduleName ) {
40 parent
::__construct( $query, $moduleName, 'iw' );
43 public function execute() {
44 if ( $this->getPageSet()->getGoodTitleCount() == 0 ) {
48 $params = $this->extractRequestParams();
49 $this->addFields( array(
55 $this->addTables( 'iwlinks' );
56 $this->addWhereFld( 'iwl_from', array_keys( $this->getPageSet()->getGoodTitles() ) );
57 if ( !is_null( $params['continue'] ) ) {
58 $cont = explode( '|', $params['continue'] );
59 if ( count( $cont ) != 3 ) {
60 $this->dieUsage( 'Invalid continue param. You should pass the ' .
61 'original value returned by the previous query', '_badcontinue' );
63 $iwlfrom = intval( $cont[0] );
64 $iwlprefix = $this->getDB()->strencode( $cont[1] );
65 $iwltitle = $this->getDB()->strencode( $this->titleToKey( $cont[2] ) );
67 "iwl_from > $iwlfrom OR " .
68 "(iwl_from = $iwlfrom AND " .
69 "(iwl_prefix > '$iwlprefix' OR " .
70 "(iwl_prefix = '$iwlprefix' AND " .
71 "iwl_title >= '$iwltitle')))"
75 // Don't order by iwl_from if it's constant in the WHERE clause
76 if ( count( $this->getPageSet()->getGoodTitles() ) == 1 ) {
77 $this->addOption( 'ORDER BY', 'iwl_prefix' );
79 $this->addOption( 'ORDER BY', 'iwl_from, iwl_prefix' );
81 $this->addOption( 'LIMIT', $params['limit'] +
1 );
82 $res = $this->select( __METHOD__
);
86 while ( $row = $db->fetchObject( $res ) ) {
87 if ( ++
$count > $params['limit'] ) {
88 // We've reached the one extra which shows that
89 // there are additional pages to be had. Stop here...
90 $this->setContinueEnumParameter( 'continue', "{$row->iwl_from}|{$row->iwl_prefix}|{$row->iwl_title}" );
93 $entry = array( 'prefix' => $row->iwl_prefix
);
94 ApiResult
::setContent( $entry, $row->iwl_title
);
95 $fit = $this->addPageSubItem( $row->iwl_from
, $entry );
97 $this->setContinueEnumParameter( 'continue', "{$row->iwl_from}|{$row->iwl_prefix}|{$row->iwl_title}" );
101 $db->freeResult( $res );
104 public function getAllowedParams() {
107 ApiBase
::PARAM_DFLT
=> 10,
108 ApiBase
::PARAM_TYPE
=> 'limit',
109 ApiBase
::PARAM_MIN
=> 1,
110 ApiBase
::PARAM_MAX
=> ApiBase
::LIMIT_BIG1
,
111 ApiBase
::PARAM_MAX2
=> ApiBase
::LIMIT_BIG2
117 public function getParamDescription() {
119 'limit' => 'How many interwiki links to return',
120 'continue' => 'When more results are available, use this to continue',
124 public function getDescription() {
125 return 'Returns all interwiki links from the given page(s)';
128 public function getPossibleErrors() {
129 return array_merge( parent
::getPossibleErrors(), array(
130 array( 'code' => '_badcontinue', 'info' => 'Invalid continue param. You should pass the original value returned by the previous query' ),
134 protected function getExamples() {
136 'Get interwiki links from the [[Main Page]]:',
137 ' api.php?action=query&prop=iwlinks&titles=Main%20Page&redirects',
141 public function getVersion() {
142 return __CLASS__
. ': $Id$';