4 * Created on May 14, 2010
6 * API for MediaWiki 1.17+
8 * Copyright (C) 2010 Sam Reed
9 * Copyright (C) 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 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, 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 * This gives links pointing to the given interwiki
36 class ApiQueryIWBacklinks
extends ApiQueryBase
{
38 public function __construct( $query, $moduleName ) {
39 parent
::__construct( $query, $moduleName, 'iwbl' );
42 public function execute() {
43 $params = $this->extractRequestParams();
45 if ( isset( $params['title'] ) && !isset( $params['prefix'] ) ) {
46 $this->dieUsageMsg( array( 'missingparam', 'prefix' ) );
49 if ( !is_null( $params['continue'] ) ) {
50 $cont = explode( '|', $params['continue'] );
51 if ( count( $cont ) != 3 ) {
52 $this->dieUsage( 'Invalid continue param. You should pass the ' .
53 'original value returned by the previous query', '_badcontinue' );
56 $prefix = $this->getDB()->strencode( $cont[0] );
57 $title = $this->getDB()->strencode( $this->titleToKey( $cont[1] ) );
58 $from = intval( $cont[2] );
60 "iwl_prefix > '$prefix' OR " .
61 "(iwl_prefix = '$prefix' AND " .
62 "(iwl_title > '$title' OR " .
63 "(iwl_title = '$title' AND " .
64 "iwl_from >= $from)))"
68 $prop = array_flip( $params['prop'] );
69 $iwprefix = isset( $prop['iwprefix'] );
70 $iwtitle = isset( $prop['iwtitle'] );
72 $this->addTables( array( 'iwlinks', 'page' ) );
73 $this->addWhere( 'iwl_from = page_id' );
75 $this->addFields( array( 'page_id', 'page_title', 'page_namespace', 'page_is_redirect',
76 'iwl_from', 'iwl_prefix', 'iwl_title' ) );
78 if ( isset( $params['prefix'] ) ) {
79 $this->addWhereFld( 'iwl_prefix', $params['prefix'] );
80 if ( isset( $params['title'] ) ) {
81 $this->addWhereFld( 'iwl_title', $params['title'] );
82 $this->addOption( 'ORDER BY', 'iwl_from' );
84 $this->addOption( 'ORDER BY', 'iwl_title, iwl_from' );
87 $this->addOption( 'ORDER BY', 'iwl_prefix, iwl_title, iwl_from' );
90 $this->addOption( 'LIMIT', $params['limit'] +
1 );
93 $res = $this->select( __METHOD__
);
96 $result = $this->getResult();
97 foreach ( $res as $row ) {
98 if ( ++
$count > $params['limit'] ) {
99 // We've reached the one extra which shows that there are additional pages to be had. Stop here...
100 // Continue string preserved in case the redirect query doesn't pass the limit
101 $this->setContinueEnumParameter( 'continue', "{$row->iwl_prefix}|{$row->iwl_title}|{$row->iwl_from}" );
107 $entry['pageid'] = intval( $row->page_id
);
108 $entry['ns'] = $row->page_namespace
;
109 $entry['title'] = $row->page_title
;
111 if ( $row->page_is_redirect
) {
112 $entry['redirect'] = '';
116 $entry['iwprefix'] = $row->iwl_prefix
;
120 $entry['iwtitle'] = $row->iwl_title
;
123 $fit = $result->addValue( array( 'query', $this->getModuleName() ), null, $entry );
125 $this->setContinueEnumParameter( 'continue', "{$row->iwl_prefix}|{$row->iwl_title}|{$row->iwl_from}" );
130 $this->getResult()->setIndexedTagName_internal(
131 array( 'query', $this->getModuleName() ),
136 public function getAllowedParams() {
142 ApiBase
::PARAM_DFLT
=> 10,
143 ApiBase
::PARAM_TYPE
=> 'limit',
144 ApiBase
::PARAM_MIN
=> 1,
145 ApiBase
::PARAM_MAX
=> ApiBase
::LIMIT_BIG1
,
146 ApiBase
::PARAM_MAX2
=> ApiBase
::LIMIT_BIG2
149 ApiBase
::PARAM_ISMULTI
=> true,
150 ApiBase
::PARAM_DFLT
=> '',
151 ApiBase
::PARAM_TYPE
=> array(
159 public function getParamDescription() {
161 'prefix' => 'Prefix for the interwiki',
162 'title' => "Interwiki link to search for. Must be used with {$this->getModulePrefix()}prefix",
163 'continue' => 'When more results are available, use this to continue',
165 'Which properties to get',
166 ' iwprefix - Adds the prefix of the interwiki',
167 ' iwtitle - Adds the title of the interwiki',
169 'limit' => 'How many total pages to return',
173 public function getDescription() {
174 return array( 'Find all pages that link to the given interwiki link.',
175 'Can be used to find all links with a prefix, or',
176 'all links to a title (with a given prefix).',
177 'Using neither parameter is effectively "All IW Links"',
181 public function getPossibleErrors() {
182 return array_merge( parent
::getPossibleErrors(), array(
183 array( 'missingparam', 'prefix' ),
184 array( 'code' => '_badcontinue', 'info' => 'Invalid continue param. You should pass the original value returned by the previous query' ),
188 protected function getExamples() {
190 'api.php?action=query&list=iwbacklinks&iwbltitle=Test&iwblprefix=wikibooks',
191 //'api.php?action=query&generator=iwbacklinks&giwbltitle=Test&iwblprefix=wikibooks&prop=info'
195 public function getVersion() {
196 return __CLASS__
. ': $Id$';