Update description of ApiQueryIWBacklinks as per IRC with Roan
[lhc/web/wiklou.git] / includes / api / ApiQueryIWBacklinks.php
1 <?php
2
3 /*
4 * Created on May 14, 2010
5 *
6 * API for MediaWiki 1.17+
7 *
8 * Copyright (C) 2010 Sam Reed
9 * Copyright (C) 2006 Yuri Astrakhan <Firstname><Lastname>@gmail.com
10 *
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.
15 *
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.
20 *
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
25 */
26
27 if ( !defined( 'MEDIAWIKI' ) ) {
28 // Eclipse helper - will be ignored in production
29 require_once( "ApiQueryBase.php" );
30 }
31
32 /**
33 * This gives links pointing to the given interwiki
34 * @ingroup API
35 */
36 class ApiQueryIWBacklinks extends ApiQueryBase {
37
38 public function __construct( $query, $moduleName ) {
39 parent::__construct( $query, $moduleName, 'iwbl' );
40 }
41
42 public function execute() {
43 $params = $this->extractRequestParams( false );
44
45 $userMax = ( $this->redirect ? ApiBase::LIMIT_BIG1 / 2 : ApiBase::LIMIT_BIG1 );
46 $botMax = ( $this->redirect ? ApiBase::LIMIT_BIG2 / 2 : ApiBase::LIMIT_BIG2 );
47
48 if ( $params['limit'] == 'max' ) {
49 $params['limit'] = $this->getMain()->canApiHighLimits() ? $botMax : $userMax;
50 $this->getResult()->addValue( 'limits', $this->getModuleName(), $params['limit'] );
51 }
52
53 if ( !is_null( $params['continue'] ) ) {
54 $cont = explode( '|', $params['continue'] );
55 if ( count( $cont ) != 3 ) {
56 $this->dieUsage( 'Invalid continue param. You should pass the ' .
57 'original value returned by the previous query', '_badcontinue' );
58 }
59
60 $from = intval( $cont[0] );
61 $prefix = $this->getDB()->strencode( $cont[1] );
62 $title = $this->getDB()->strencode( $this->titleToKey( $cont[2] ) );
63 $this->addWhere(
64 "iwl_from > $from OR " .
65 "(iwl_from = $from AND " .
66 "(iwl_prefix > '$prefix' OR " .
67 "(iwl_prefix = '$prefix' AND " .
68 "iwl_title >= '$title')))"
69 );
70 }
71
72 $this->addTables( array( 'iwlinks', 'page' ) );
73 $this->addWhere( 'iwl_from = page_id' );
74
75 $this->addFields( array( 'page_id', 'page_title', 'page_namespace', 'page_is_redirect',
76 'iwl_from', 'iwl_prefix', 'iwl_title' ) );
77
78 if ( $params['prefix'] !== '' ) {
79 $this->addWhereFld( 'iwl_prefix', $params['prefix'] );
80 }
81
82 if ( $params['title'] !== '' ) {
83 $this->addWhereFld( 'iwl_title', $params['title'] );
84 }
85
86 $this->addWhereFld( 'page_namespace', $params['namespace'] );
87
88 $this->addOption( 'LIMIT', $params['limit'] + 1 );
89 $this->addOption( 'ORDER BY', 'iwl_from' );
90 $this->addOption( 'STRAIGHT_JOIN' );
91
92 $db = $this->getDB();
93 $res = $this->select( __METHOD__ . '::firstQuery' );
94
95 $count = 0;
96 $result = $this->getResult();
97 while ( $row = $db->fetchObject( $res ) ) {
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_from}|{$row->iwl_prefix}|{$row->iwl_title}" );
102 break;
103 }
104
105 $entry = array();
106
107 $entry['pageid'] = intval( $row->page_id );
108 $entry['ns'] = $row->page_namespace;
109 $entry['title'] = $row->page_title;
110
111 if ( $row->page_is_redirect ) {
112 $entry['redirect'] = '';
113 }
114
115 $fit = $result->addValue( array( 'query', $this->getModuleName() ), null, $entry );
116 if ( !$fit ) {
117 $this->setContinueEnumParameter( 'continue', "{$row->iwl_from}|{$row->iwl_prefix}|{$row->iwl_title}" );
118 break;
119 }
120 }
121 $db->freeResult( $res );
122
123 $this->getResult()->setIndexedTagName_internal(
124 array( 'query', $this->getModuleName() ),
125 'iw'
126 );
127 }
128
129 public function getAllowedParams() {
130 return array(
131 'prefix' => null,
132 'title' => null,
133 'continue' => null,
134 'namespace' => array(
135 ApiBase::PARAM_ISMULTI => true,
136 ApiBase::PARAM_TYPE => 'namespace'
137 ),
138 'limit' => array(
139 ApiBase::PARAM_DFLT => 10,
140 ApiBase::PARAM_TYPE => 'limit',
141 ApiBase::PARAM_MIN => 1,
142 ApiBase::PARAM_MAX => ApiBase::LIMIT_BIG1,
143 ApiBase::PARAM_MAX2 => ApiBase::LIMIT_BIG2
144 )
145 );
146 }
147
148 public function getParamDescription() {
149 return array(
150 'prefix' => 'Prefix for the interwiki',
151 'title' => 'Interwiki link to search for',
152 'continue' => 'When more results are available, use this to continue',
153 'namespace' => 'The namespace to enumerate',
154 'limit' => 'How many total pages to return',
155 );
156 }
157
158 public function getDescription() {
159 return array('Find all pages that link to the given interwiki link.',
160 'Can be used to find all links with a prefix, or',
161 'all links to a title (any prefix).',
162 'Using neither parameter is effectively "All IW Links"',
163 );
164 }
165
166 public function getPossibleErrors() {
167 return array_merge( parent::getPossibleErrors(), array(
168 array( 'invalidtitle', 'title' ),
169 array( 'missingparam', 'title' ),
170 array( 'code' => '_badcontinue', 'info' => 'Invalid continue param. You should pass the original value returned by the previous query' ),
171 ) );
172 }
173
174 protected function getExamples() {
175 return array(
176 'api.php?action=query&list=iwbacklinks&iwbltitle=Test&iwblprefix=wikibooks',
177 'api.php?action=query&generator=iwbacklinks&giwbltitle=Test&iwblprefix=wikibooks&prop=info'
178 );
179 }
180
181 public function getVersion() {
182 return __CLASS__ . ': $Id$';
183 }
184 }