Normalise casing of svn:keywords Id
[lhc/web/wiklou.git] / includes / api / ApiQueryIWBacklinks.php
1 <?php
2 /**
3 * API for MediaWiki 1.17+
4 *
5 * Created on May 14, 2010
6 *
7 * Copyright © 2010 Sam Reed
8 * Copyright © 2006 Yuri Astrakhan <Firstname><Lastname>@gmail.com
9 *
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
14 *
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License along
21 * with this program; if not, write to the Free Software Foundation, Inc.,
22 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
23 * http://www.gnu.org/copyleft/gpl.html
24 *
25 * @file
26 */
27
28 if ( !defined( 'MEDIAWIKI' ) ) {
29 // Eclipse helper - will be ignored in production
30 require_once( "ApiQueryBase.php" );
31 }
32
33 /**
34 * This gives links pointing to the given interwiki
35 * @ingroup API
36 */
37 class ApiQueryIWBacklinks extends ApiQueryGeneratorBase {
38
39 public function __construct( $query, $moduleName ) {
40 parent::__construct( $query, $moduleName, 'iwbl' );
41 }
42
43 public function execute() {
44 $this->run();
45 }
46
47 public function executeGenerator( $resultPageSet ) {
48 $this->run( $resultPageSet );
49 }
50
51 public function run( $resultPageSet = null ) {
52 $params = $this->extractRequestParams();
53
54 if ( isset( $params['title'] ) && !isset( $params['prefix'] ) ) {
55 $this->dieUsageMsg( array( 'missingparam', 'prefix' ) );
56 }
57
58 if ( !is_null( $params['continue'] ) ) {
59 $cont = explode( '|', $params['continue'] );
60 if ( count( $cont ) != 3 ) {
61 $this->dieUsage( 'Invalid continue param. You should pass the ' .
62 'original value returned by the previous query', '_badcontinue' );
63 }
64
65 $prefix = $this->getDB()->strencode( $cont[0] );
66 $title = $this->getDB()->strencode( $this->titleToKey( $cont[1] ) );
67 $from = intval( $cont[2] );
68 $this->addWhere(
69 "iwl_prefix > '$prefix' OR " .
70 "(iwl_prefix = '$prefix' AND " .
71 "(iwl_title > '$title' OR " .
72 "(iwl_title = '$title' AND " .
73 "iwl_from >= $from)))"
74 );
75 }
76
77 $prop = array_flip( $params['prop'] );
78 $iwprefix = isset( $prop['iwprefix'] );
79 $iwtitle = isset( $prop['iwtitle'] );
80
81 $this->addTables( array( 'iwlinks', 'page' ) );
82 $this->addWhere( 'iwl_from = page_id' );
83
84 $this->addFields( array( 'page_id', 'page_title', 'page_namespace', 'page_is_redirect',
85 'iwl_from', 'iwl_prefix', 'iwl_title' ) );
86
87 if ( isset( $params['prefix'] ) ) {
88 $this->addWhereFld( 'iwl_prefix', $params['prefix'] );
89 if ( isset( $params['title'] ) ) {
90 $this->addWhereFld( 'iwl_title', $params['title'] );
91 $this->addOption( 'ORDER BY', 'iwl_from' );
92 } else {
93 $this->addOption( 'ORDER BY', 'iwl_title, iwl_from' );
94 }
95 } else {
96 $this->addOption( 'ORDER BY', 'iwl_prefix, iwl_title, iwl_from' );
97 }
98
99 $this->addOption( 'LIMIT', $params['limit'] + 1 );
100
101 $res = $this->select( __METHOD__ );
102
103 $pages = array();
104
105 $count = 0;
106 $result = $this->getResult();
107 foreach ( $res as $row ) {
108 if ( ++ $count > $params['limit'] ) {
109 // We've reached the one extra which shows that there are additional pages to be had. Stop here...
110 // Continue string preserved in case the redirect query doesn't pass the limit
111 $this->setContinueEnumParameter( 'continue', "{$row->iwl_prefix}|{$row->iwl_title}|{$row->iwl_from}" );
112 break;
113 }
114
115 if ( !is_null( $resultPageSet ) ) {
116 $pages[] = Title::newFromRow( $row );
117 } else {
118 $entry = array();
119
120 $entry['pageid'] = intval( $row->page_id );
121 $entry['ns'] = intval( $row->page_namespace );
122 $entry['title'] = $row->page_title;
123
124 if ( $row->page_is_redirect ) {
125 $entry['redirect'] = '';
126 }
127
128 if ( $iwprefix ) {
129 $entry['iwprefix'] = $row->iwl_prefix;
130 }
131
132 if ( $iwtitle ) {
133 $entry['iwtitle'] = $row->iwl_title;
134 }
135
136 $fit = $result->addValue( array( 'query', $this->getModuleName() ), null, $entry );
137 if ( !$fit ) {
138 $this->setContinueEnumParameter( 'continue', "{$row->iwl_prefix}|{$row->iwl_title}|{$row->iwl_from}" );
139 break;
140 }
141 }
142 }
143
144 if ( is_null( $resultPageSet ) ) {
145 $result->setIndexedTagName_internal( array( 'query', $this->getModuleName() ), 'iw' );
146 } else {
147 $resultPageSet->populateFromTitles( $pages );
148 }
149 }
150
151 public function getCacheMode( $params ) {
152 return 'public';
153 }
154
155 public function getAllowedParams() {
156 return array(
157 'prefix' => null,
158 'title' => null,
159 'continue' => null,
160 'limit' => array(
161 ApiBase::PARAM_DFLT => 10,
162 ApiBase::PARAM_TYPE => 'limit',
163 ApiBase::PARAM_MIN => 1,
164 ApiBase::PARAM_MAX => ApiBase::LIMIT_BIG1,
165 ApiBase::PARAM_MAX2 => ApiBase::LIMIT_BIG2
166 ),
167 'prop' => array(
168 ApiBase::PARAM_ISMULTI => true,
169 ApiBase::PARAM_DFLT => '',
170 ApiBase::PARAM_TYPE => array(
171 'iwprefix',
172 'iwtitle',
173 ),
174 ),
175 );
176 }
177
178 public function getParamDescription() {
179 return array(
180 'prefix' => 'Prefix for the interwiki',
181 'title' => "Interwiki link to search for. Must be used with {$this->getModulePrefix()}prefix",
182 'continue' => 'When more results are available, use this to continue',
183 'prop' => array(
184 'Which properties to get',
185 ' iwprefix - Adds the prefix of the interwiki',
186 ' iwtitle - Adds the title of the interwiki',
187 ),
188 'limit' => 'How many total pages to return',
189 );
190 }
191
192 public function getDescription() {
193 return array( 'Find all pages that link to the given interwiki link.',
194 'Can be used to find all links with a prefix, or',
195 'all links to a title (with a given prefix).',
196 'Using neither parameter is effectively "All IW Links"',
197 );
198 }
199
200 public function getPossibleErrors() {
201 return array_merge( parent::getPossibleErrors(), array(
202 array( 'missingparam', 'prefix' ),
203 array( 'code' => '_badcontinue', 'info' => 'Invalid continue param. You should pass the original value returned by the previous query' ),
204 ) );
205 }
206
207 protected function getExamples() {
208 return array(
209 'api.php?action=query&list=iwbacklinks&iwbltitle=Test&iwblprefix=wikibooks',
210 'api.php?action=query&generator=iwbacklinks&giwbltitle=Test&iwblprefix=wikibooks&prop=info'
211 );
212 }
213
214 public function getVersion() {
215 return __CLASS__ . ': $Id$';
216 }
217 }