Added post-commit callback support to DB classes.
[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 /**
29 * This gives links pointing to the given interwiki
30 * @ingroup API
31 */
32 class ApiQueryIWBacklinks extends ApiQueryGeneratorBase {
33
34 public function __construct( $query, $moduleName ) {
35 parent::__construct( $query, $moduleName, 'iwbl' );
36 }
37
38 public function execute() {
39 $this->run();
40 }
41
42 public function executeGenerator( $resultPageSet ) {
43 $this->run( $resultPageSet );
44 }
45
46 /**
47 * @param $resultPageSet ApiPageSet
48 * @return void
49 */
50 public function run( $resultPageSet = null ) {
51 $params = $this->extractRequestParams();
52
53 if ( isset( $params['title'] ) && !isset( $params['prefix'] ) ) {
54 $this->dieUsageMsg( array( 'missingparam', 'prefix' ) );
55 }
56
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' );
62 }
63
64 $db = $this->getDB();
65 $op = $params['dir'] == 'descending' ? '<' : '>';
66 $prefix = $db->addQuotes( $cont[0] );
67 $title = $db->addQuotes( $cont[1] );
68 $from = intval( $cont[2] );
69 $this->addWhere(
70 "iwl_prefix $op $prefix OR " .
71 "(iwl_prefix = $prefix AND " .
72 "(iwl_title $op $title OR " .
73 "(iwl_title = $title AND " .
74 "iwl_from $op= $from)))"
75 );
76 }
77
78 $prop = array_flip( $params['prop'] );
79 $iwprefix = isset( $prop['iwprefix'] );
80 $iwtitle = isset( $prop['iwtitle'] );
81
82 $this->addTables( array( 'iwlinks', 'page' ) );
83 $this->addWhere( 'iwl_from = page_id' );
84
85 $this->addFields( array( 'page_id', 'page_title', 'page_namespace', 'page_is_redirect',
86 'iwl_from', 'iwl_prefix', 'iwl_title' ) );
87
88 $sort = ( $params['dir'] == 'descending' ? ' DESC' : '' );
89 if ( isset( $params['prefix'] ) ) {
90 $this->addWhereFld( 'iwl_prefix', $params['prefix'] );
91 if ( isset( $params['title'] ) ) {
92 $this->addWhereFld( 'iwl_title', $params['title'] );
93 $this->addOption( 'ORDER BY', 'iwl_from' . $sort );
94 } else {
95 $this->addOption( 'ORDER BY', array(
96 'iwl_title' . $sort,
97 'iwl_from' . $sort
98 ));
99 }
100 } else {
101 $this->addOption( 'ORDER BY', array(
102 'iwl_prefix' . $sort,
103 'iwl_title' . $sort,
104 'iwl_from' . $sort
105 ));
106 }
107
108 $this->addOption( 'LIMIT', $params['limit'] + 1 );
109
110 $res = $this->select( __METHOD__ );
111
112 $pages = array();
113
114 $count = 0;
115 $result = $this->getResult();
116 foreach ( $res as $row ) {
117 if ( ++ $count > $params['limit'] ) {
118 // We've reached the one extra which shows that there are additional pages to be had. Stop here...
119 // Continue string preserved in case the redirect query doesn't pass the limit
120 $this->setContinueEnumParameter( 'continue', "{$row->iwl_prefix}|{$row->iwl_title}|{$row->iwl_from}" );
121 break;
122 }
123
124 if ( !is_null( $resultPageSet ) ) {
125 $pages[] = Title::newFromRow( $row );
126 } else {
127 $entry = array( 'pageid' => $row->page_id );
128
129 $title = Title::makeTitle( $row->page_namespace, $row->page_title );
130 ApiQueryBase::addTitleInfo( $entry, $title );
131
132 if ( $row->page_is_redirect ) {
133 $entry['redirect'] = '';
134 }
135
136 if ( $iwprefix ) {
137 $entry['iwprefix'] = $row->iwl_prefix;
138 }
139
140 if ( $iwtitle ) {
141 $entry['iwtitle'] = $row->iwl_title;
142 }
143
144 $fit = $result->addValue( array( 'query', $this->getModuleName() ), null, $entry );
145 if ( !$fit ) {
146 $this->setContinueEnumParameter( 'continue', "{$row->iwl_prefix}|{$row->iwl_title}|{$row->iwl_from}" );
147 break;
148 }
149 }
150 }
151
152 if ( is_null( $resultPageSet ) ) {
153 $result->setIndexedTagName_internal( array( 'query', $this->getModuleName() ), 'iw' );
154 } else {
155 $resultPageSet->populateFromTitles( $pages );
156 }
157 }
158
159 public function getCacheMode( $params ) {
160 return 'public';
161 }
162
163 public function getAllowedParams() {
164 return array(
165 'prefix' => null,
166 'title' => null,
167 'continue' => null,
168 'limit' => array(
169 ApiBase::PARAM_DFLT => 10,
170 ApiBase::PARAM_TYPE => 'limit',
171 ApiBase::PARAM_MIN => 1,
172 ApiBase::PARAM_MAX => ApiBase::LIMIT_BIG1,
173 ApiBase::PARAM_MAX2 => ApiBase::LIMIT_BIG2
174 ),
175 'prop' => array(
176 ApiBase::PARAM_ISMULTI => true,
177 ApiBase::PARAM_DFLT => '',
178 ApiBase::PARAM_TYPE => array(
179 'iwprefix',
180 'iwtitle',
181 ),
182 ),
183 'dir' => array(
184 ApiBase::PARAM_DFLT => 'ascending',
185 ApiBase::PARAM_TYPE => array(
186 'ascending',
187 'descending'
188 )
189 ),
190 );
191 }
192
193 public function getParamDescription() {
194 return array(
195 'prefix' => 'Prefix for the interwiki',
196 'title' => "Interwiki link to search for. Must be used with {$this->getModulePrefix()}prefix",
197 'continue' => 'When more results are available, use this to continue',
198 'prop' => array(
199 'Which properties to get',
200 ' iwprefix - Adds the prefix of the interwiki',
201 ' iwtitle - Adds the title of the interwiki',
202 ),
203 'limit' => 'How many total pages to return',
204 'dir' => 'The direction in which to list',
205 );
206 }
207
208 public function getResultProperties() {
209 return array(
210 '' => array(
211 'pageid' => 'integer',
212 'ns' => 'namespace',
213 'title' => 'string',
214 'redirect' => 'boolean'
215 ),
216 'iwprefix' => array(
217 'iwprefix' => 'string'
218 ),
219 'iwtitle' => array(
220 'iwtitle' => 'string'
221 )
222 );
223 }
224
225 public function getDescription() {
226 return array( 'Find all pages that link to the given interwiki link.',
227 'Can be used to find all links with a prefix, or',
228 'all links to a title (with a given prefix).',
229 'Using neither parameter is effectively "All IW Links"',
230 );
231 }
232
233 public function getPossibleErrors() {
234 return array_merge( parent::getPossibleErrors(), array(
235 array( 'missingparam', 'prefix' ),
236 array( 'code' => '_badcontinue', 'info' => 'Invalid continue param. You should pass the original value returned by the previous query' ),
237 ) );
238 }
239
240 public function getExamples() {
241 return array(
242 'api.php?action=query&list=iwbacklinks&iwbltitle=Test&iwblprefix=wikibooks',
243 'api.php?action=query&generator=iwbacklinks&giwbltitle=Test&giwblprefix=wikibooks&prop=info'
244 );
245 }
246
247 public function getVersion() {
248 return __CLASS__ . ': $Id$';
249 }
250 }