Add OptIn.sql to addwiki.php
[lhc/web/wiklou.git] / includes / api / ApiQueryBacklinks.php
1 <?php
2
3 /*
4 * Created on Oct 16, 2006
5 *
6 * API for MediaWiki 1.8+
7 *
8 * Copyright (C) 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 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
23 * http://www.gnu.org/copyleft/gpl.html
24 */
25
26 if ( !defined( 'MEDIAWIKI' ) ) {
27 // Eclipse helper - will be ignored in production
28 require_once ( "ApiQueryBase.php" );
29 }
30
31 /**
32 * This is a three-in-one module to query:
33 * * backlinks - links pointing to the given page,
34 * * embeddedin - what pages transclude the given page within themselves,
35 * * imageusage - what pages use the given image
36 *
37 * @ingroup API
38 */
39 class ApiQueryBacklinks extends ApiQueryGeneratorBase {
40
41 private $params, $rootTitle, $contRedirs, $contLevel, $contTitle, $contID, $redirID, $redirect;
42 private $bl_ns, $bl_from, $bl_table, $bl_code, $bl_title, $bl_sort, $bl_fields, $hasNS;
43 private $pageMap, $resultArr;
44
45 // output element name, database column field prefix, database table
46 private $backlinksSettings = array (
47 'backlinks' => array (
48 'code' => 'bl',
49 'prefix' => 'pl',
50 'linktbl' => 'pagelinks'
51 ),
52 'embeddedin' => array (
53 'code' => 'ei',
54 'prefix' => 'tl',
55 'linktbl' => 'templatelinks'
56 ),
57 'imageusage' => array (
58 'code' => 'iu',
59 'prefix' => 'il',
60 'linktbl' => 'imagelinks'
61 )
62 );
63
64 public function __construct( $query, $moduleName ) {
65 extract( $this->backlinksSettings[$moduleName] );
66 $this->resultArr = array();
67
68 parent :: __construct( $query, $moduleName, $code );
69 $this->bl_ns = $prefix . '_namespace';
70 $this->bl_from = $prefix . '_from';
71 $this->bl_table = $linktbl;
72 $this->bl_code = $code;
73
74 $this->hasNS = $moduleName !== 'imageusage';
75 if ( $this->hasNS ) {
76 $this->bl_title = $prefix . '_title';
77 $this->bl_sort = "{$this->bl_ns}, {$this->bl_title}, {$this->bl_from}";
78 $this->bl_fields = array (
79 $this->bl_ns,
80 $this->bl_title
81 );
82 } else {
83 $this->bl_title = $prefix . '_to';
84 $this->bl_sort = "{$this->bl_title}, {$this->bl_from}";
85 $this->bl_fields = array (
86 $this->bl_title
87 );
88 }
89 }
90
91 public function execute() {
92 $this->run();
93 }
94
95 public function executeGenerator( $resultPageSet ) {
96 $this->run( $resultPageSet );
97 }
98
99 private function prepareFirstQuery( $resultPageSet = null ) {
100 /* SELECT page_id, page_title, page_namespace, page_is_redirect
101 * FROM pagelinks, page WHERE pl_from=page_id
102 * AND pl_title='Foo' AND pl_namespace=0
103 * LIMIT 11 ORDER BY pl_from
104 */
105 $db = $this->getDB();
106 $this->addTables( array( $this->bl_table, 'page' ) );
107 $this->addWhere( "{$this->bl_from}=page_id" );
108 if ( is_null( $resultPageSet ) )
109 $this->addFields( array( 'page_id', 'page_title', 'page_namespace' ) );
110 else
111 $this->addFields( $resultPageSet->getPageTableFields() );
112
113 $this->addFields( 'page_is_redirect' );
114 $this->addWhereFld( $this->bl_title, $this->rootTitle->getDBkey() );
115
116 if ( $this->hasNS )
117 $this->addWhereFld( $this->bl_ns, $this->rootTitle->getNamespace() );
118 $this->addWhereFld( 'page_namespace', $this->params['namespace'] );
119
120 if ( !is_null( $this->contID ) )
121 $this->addWhere( "{$this->bl_from}>={$this->contID}" );
122
123 if ( $this->params['filterredir'] == 'redirects' )
124 $this->addWhereFld( 'page_is_redirect', 1 );
125 else if ( $this->params['filterredir'] == 'nonredirects' && !$this->redirect )
126 // bug 22245 - Check for !redirect, as filtering nonredirects, when getting what links to them is contradictory
127 $this->addWhereFld( 'page_is_redirect', 0 );
128
129 $this->addOption( 'LIMIT', $this->params['limit'] + 1 );
130 $this->addOption( 'ORDER BY', $this->bl_from );
131 $this->addOption( 'STRAIGHT_JOIN' );
132 }
133
134 private function prepareSecondQuery( $resultPageSet = null ) {
135 /* SELECT page_id, page_title, page_namespace, page_is_redirect, pl_title, pl_namespace
136 FROM pagelinks, page WHERE pl_from=page_id
137 AND (pl_title='Foo' AND pl_namespace=0) OR (pl_title='Bar' AND pl_namespace=1)
138 ORDER BY pl_namespace, pl_title, pl_from LIMIT 11
139 */
140 $db = $this->getDB();
141 $this->addTables( array( 'page', $this->bl_table ) );
142 $this->addWhere( "{$this->bl_from}=page_id" );
143
144 if ( is_null( $resultPageSet ) )
145 $this->addFields( array( 'page_id', 'page_title', 'page_namespace', 'page_is_redirect' ) );
146 else
147 $this->addFields( $resultPageSet->getPageTableFields() );
148
149 $this->addFields( $this->bl_title );
150 if ( $this->hasNS )
151 $this->addFields( $this->bl_ns );
152
153 // We can't use LinkBatch here because $this->hasNS may be false
154 $titleWhere = array();
155 foreach ( $this->redirTitles as $t )
156 $titleWhere[] = "{$this->bl_title} = " . $db->addQuotes( $t->getDBkey() ) .
157 ( $this->hasNS ? " AND {$this->bl_ns} = '{$t->getNamespace()}'" : "" );
158 $this->addWhere( $db->makeList( $titleWhere, LIST_OR ) );
159 $this->addWhereFld( 'page_namespace', $this->params['namespace'] );
160
161 if ( !is_null( $this->redirID ) )
162 {
163 $first = $this->redirTitles[0];
164 $title = $db->strencode( $first->getDBkey() );
165 $ns = $first->getNamespace();
166 $from = $this->redirID;
167 if ( $this->hasNS )
168 $this->addWhere( "{$this->bl_ns} > $ns OR " .
169 "({$this->bl_ns} = $ns AND " .
170 "({$this->bl_title} > '$title' OR " .
171 "({$this->bl_title} = '$title' AND " .
172 "{$this->bl_from} >= $from)))" );
173 else
174 $this->addWhere( "{$this->bl_title} > '$title' OR " .
175 "({$this->bl_title} = '$title' AND " .
176 "{$this->bl_from} >= $from)" );
177
178 }
179 if ( $this->params['filterredir'] == 'redirects' )
180 $this->addWhereFld( 'page_is_redirect', 1 );
181 else if ( $this->params['filterredir'] == 'nonredirects' )
182 $this->addWhereFld( 'page_is_redirect', 0 );
183
184 $this->addOption( 'LIMIT', $this->params['limit'] + 1 );
185 $this->addOption( 'ORDER BY', $this->bl_sort );
186 $this->addOption( 'USE INDEX', array( 'page' => 'PRIMARY' ) );
187 }
188
189 private function run( $resultPageSet = null ) {
190 $this->params = $this->extractRequestParams( false );
191 $this->redirect = isset( $this->params['redirect'] ) && $this->params['redirect'];
192 $userMax = ( $this->redirect ? ApiBase::LIMIT_BIG1 / 2 : ApiBase::LIMIT_BIG1 );
193 $botMax = ( $this->redirect ? ApiBase::LIMIT_BIG2 / 2 : ApiBase::LIMIT_BIG2 );
194 if ( $this->params['limit'] == 'max' ) {
195 $this->params['limit'] = $this->getMain()->canApiHighLimits() ? $botMax : $userMax;
196 $this->getResult()->addValue( 'limits', $this->getModuleName(), $this->params['limit'] );
197 }
198
199 $this->processContinue();
200 $this->prepareFirstQuery( $resultPageSet );
201
202 $db = $this->getDB();
203 $res = $this->select( __METHOD__ . '::firstQuery' );
204
205 $count = 0;
206 $this->pageMap = array(); // Maps ns and title to pageid
207 $this->continueStr = null;
208 $this->redirTitles = array();
209 while ( $row = $db->fetchObject( $res ) ) {
210 if ( ++ $count > $this->params['limit'] ) {
211 // We've reached the one extra which shows that there are additional pages to be had. Stop here...
212 // Continue string preserved in case the redirect query doesn't pass the limit
213 $this->continueStr = $this->getContinueStr( $row->page_id );
214 break;
215 }
216
217 if ( is_null( $resultPageSet ) )
218 $this->extractRowInfo( $row );
219 else
220 {
221 $this->pageMap[$row->page_namespace][$row->page_title] = $row->page_id;
222 if ( $row->page_is_redirect )
223 $this->redirTitles[] = Title::makeTitle( $row->page_namespace, $row->page_title );
224
225 $resultPageSet->processDbRow( $row );
226 }
227 }
228 $db->freeResult( $res );
229
230 if ( $this->redirect && count( $this->redirTitles ) )
231 {
232 $this->resetQueryParams();
233 $this->prepareSecondQuery( $resultPageSet );
234 $res = $this->select( __METHOD__ . '::secondQuery' );
235 $count = 0;
236 while ( $row = $db->fetchObject( $res ) )
237 {
238 if ( ++$count > $this->params['limit'] )
239 {
240 // We've reached the one extra which shows that there are additional pages to be had. Stop here...
241 // We need to keep the parent page of this redir in
242 if ( $this->hasNS )
243 $parentID = $this->pageMap[$row-> { $this->bl_ns } ][$row-> { $this->bl_title } ];
244 else
245 $parentID = $this->pageMap[NS_IMAGE][$row-> { $this->bl_title } ];
246 $this->continueStr = $this->getContinueRedirStr( $parentID, $row->page_id );
247 break;
248 }
249
250 if ( is_null( $resultPageSet ) )
251 $this->extractRedirRowInfo( $row );
252 else
253 $resultPageSet->processDbRow( $row );
254 }
255 $db->freeResult( $res );
256 }
257 if ( is_null( $resultPageSet ) ) {
258 // Try to add the result data in one go and pray that it fits
259 $fit = $this->getResult()->addValue( 'query', $this->getModuleName(), array_values( $this->resultArr ) );
260 if ( !$fit )
261 {
262 // It didn't fit. Add elements one by one until the
263 // result is full.
264 foreach ( $this->resultArr as $pageID => $arr )
265 {
266 // Add the basic entry without redirlinks first
267 $fit = $this->getResult()->addValue(
268 array( 'query', $this->getModuleName() ),
269 null, array_diff_key( $arr, array( 'redirlinks' => '' ) ) );
270 if ( !$fit )
271 {
272 $this->continueStr = $this->getContinueStr( $pageID );
273 break;
274 }
275
276 $hasRedirs = false;
277 foreach ( (array)@$arr['redirlinks'] as $key => $redir )
278 {
279 $fit = $this->getResult()->addValue(
280 array( 'query', $this->getModuleName(), $pageID, 'redirlinks' ),
281 $key, $redir );
282 if ( !$fit )
283 {
284 $this->continueStr = $this->getContinueRedirStr( $pageID, $redir['pageid'] );
285 break;
286 }
287 $hasRedirs = true;
288 }
289 if ( $hasRedirs )
290 $this->getResult()->setIndexedTagName_internal(
291 array( 'query', $this->getModuleName(), $pageID, 'redirlinks' ),
292 $this->bl_code );
293 if ( !$fit )
294 break;
295 }
296 }
297
298 $this->getResult()->setIndexedTagName_internal(
299 array( 'query', $this->getModuleName() ),
300 $this->bl_code );
301 }
302 if ( !is_null( $this->continueStr ) )
303 $this->setContinueEnumParameter( 'continue', $this->continueStr );
304 }
305
306 private function extractRowInfo( $row ) {
307 $this->pageMap[$row->page_namespace][$row->page_title] = $row->page_id;
308 $t = Title::makeTitle( $row->page_namespace, $row->page_title );
309 $a = array( 'pageid' => intval( $row->page_id ) );
310 ApiQueryBase::addTitleInfo( $a, $t );
311 if ( $row->page_is_redirect )
312 {
313 $a['redirect'] = '';
314 $this->redirTitles[] = $t;
315 }
316 // Put all the results in an array first
317 $this->resultArr[$a['pageid']] = $a;
318 }
319
320 private function extractRedirRowInfo( $row )
321 {
322 $a['pageid'] = intval( $row->page_id );
323 ApiQueryBase::addTitleInfo( $a, Title::makeTitle( $row->page_namespace, $row->page_title ) );
324 if ( $row->page_is_redirect )
325 $a['redirect'] = '';
326 $ns = $this->hasNS ? $row-> { $this->bl_ns } : NS_FILE;
327 $parentID = $this->pageMap[$ns][$row-> { $this->bl_title } ];
328 // Put all the results in an array first
329 $this->resultArr[$parentID]['redirlinks'][] = $a;
330 $this->getResult()->setIndexedTagName( $this->resultArr[$parentID]['redirlinks'], $this->bl_code );
331 }
332
333 protected function processContinue() {
334 if ( !is_null( $this->params['continue'] ) )
335 $this->parseContinueParam();
336 else {
337 if ( $this->params['title'] !== "" ) {
338 $title = Title::newFromText( $this->params['title'] );
339 if ( !$title ) {
340 $this->dieUsageMsg( array( 'invalidtitle', $this->params['title'] ) );
341 } else {
342 $this->rootTitle = $title;
343 }
344 } else {
345 $this->dieUsageMsg( array( 'missingparam', 'title' ) );
346 }
347 }
348
349 // only image titles are allowed for the root in imageinfo mode
350 if ( !$this->hasNS && $this->rootTitle->getNamespace() !== NS_FILE )
351 $this->dieUsage( "The title for {$this->getModuleName()} query must be an image", 'bad_image_title' );
352 }
353
354 protected function parseContinueParam() {
355 $continueList = explode( '|', $this->params['continue'] );
356 // expected format:
357 // ns | key | id1 [| id2]
358 // ns+key: root title
359 // id1: first-level page ID to continue from
360 // id2: second-level page ID to continue from
361
362 // null stuff out now so we know what's set and what isn't
363 $this->rootTitle = $this->contID = $this->redirID = null;
364 $rootNs = intval( $continueList[0] );
365 if ( $rootNs === 0 && $continueList[0] !== '0' )
366 // Illegal continue parameter
367 $this->dieUsage( "Invalid continue param. You should pass the original value returned by the previous query", "_badcontinue" );
368 $this->rootTitle = Title::makeTitleSafe( $rootNs, $continueList[1] );
369
370 if ( !$this->rootTitle )
371 $this->dieUsage( "Invalid continue param. You should pass the original value returned by the previous query", "_badcontinue" );
372 $contID = intval( $continueList[2] );
373
374 if ( $contID === 0 && $continueList[2] !== '0' )
375 $this->dieUsage( "Invalid continue param. You should pass the original value returned by the previous query", "_badcontinue" );
376 $this->contID = $contID;
377 $redirID = intval( @$continueList[3] );
378
379 if ( $redirID === 0 && @$continueList[3] !== '0' )
380 // This one isn't required
381 return;
382 $this->redirID = $redirID;
383
384 }
385
386 protected function getContinueStr( $lastPageID ) {
387 return $this->rootTitle->getNamespace() .
388 '|' . $this->rootTitle->getDBkey() .
389 '|' . $lastPageID;
390 }
391
392 protected function getContinueRedirStr( $lastPageID, $lastRedirID ) {
393 return $this->getContinueStr( $lastPageID ) . '|' . $lastRedirID;
394 }
395
396 public function getAllowedParams() {
397 $retval = array (
398 'title' => null,
399 'continue' => null,
400 'namespace' => array (
401 ApiBase :: PARAM_ISMULTI => true,
402 ApiBase :: PARAM_TYPE => 'namespace'
403 ),
404 'filterredir' => array(
405 ApiBase :: PARAM_DFLT => 'all',
406 ApiBase :: PARAM_TYPE => array(
407 'all',
408 'redirects',
409 'nonredirects'
410 )
411 ),
412 'limit' => array (
413 ApiBase :: PARAM_DFLT => 10,
414 ApiBase :: PARAM_TYPE => 'limit',
415 ApiBase :: PARAM_MIN => 1,
416 ApiBase :: PARAM_MAX => ApiBase :: LIMIT_BIG1,
417 ApiBase :: PARAM_MAX2 => ApiBase :: LIMIT_BIG2
418 )
419 );
420 if ( $this->getModuleName() == 'embeddedin' )
421 return $retval;
422 $retval['redirect'] = false;
423 return $retval;
424 }
425
426 public function getParamDescription() {
427 $retval = array (
428 'title' => 'Title to search.',
429 'continue' => 'When more results are available, use this to continue.',
430 'namespace' => 'The namespace to enumerate.',
431 );
432 if ( $this->getModuleName() != 'embeddedin' )
433 return array_merge( $retval, array(
434 'redirect' => 'If linking page is a redirect, find all pages that link to that redirect as well. Maximum limit is halved.',
435 'filterredir' => "How to filter for redirects. If set to nonredirects when {$this->bl_code}redirect is enabled, this is only applied to the second level",
436 'limit' => "How many total pages to return. If {$this->bl_code}redirect is enabled, limit applies to each level separately (which means you may get up to 2 * limit results)."
437 ) );
438 return array_merge( $retval, array(
439 'filterredir' => 'How to filter for redirects',
440 'limit' => 'How many total pages to return.'
441 ) );
442 }
443
444 public function getDescription() {
445 switch ( $this->getModuleName() ) {
446 case 'backlinks' :
447 return 'Find all pages that link to the given page';
448 case 'embeddedin' :
449 return 'Find all pages that embed (transclude) the given title';
450 case 'imageusage' :
451 return 'Find all pages that use the given image title.';
452 default :
453 ApiBase :: dieDebug( __METHOD__, 'Unknown module name' );
454 }
455 }
456
457 public function getPossibleErrors() {
458 return array_merge( parent::getPossibleErrors(), array(
459 array( 'invalidtitle', 'title' ),
460 array( 'missingparam', 'title' ),
461 array( 'code' => 'bad_image_title', 'info' => "The title for {$this->getModuleName()} query must be an image" ),
462 array( 'code' => '_badcontinue', 'info' => 'Invalid continue param. You should pass the original value returned by the previous query' ),
463 ) );
464 }
465
466 protected function getExamples() {
467 static $examples = array (
468 'backlinks' => array (
469 "api.php?action=query&list=backlinks&bltitle=Main%20Page",
470 "api.php?action=query&generator=backlinks&gbltitle=Main%20Page&prop=info"
471 ),
472 'embeddedin' => array (
473 "api.php?action=query&list=embeddedin&eititle=Template:Stub",
474 "api.php?action=query&generator=embeddedin&geititle=Template:Stub&prop=info"
475 ),
476 'imageusage' => array (
477 "api.php?action=query&list=imageusage&iutitle=File:Albert%20Einstein%20Head.jpg",
478 "api.php?action=query&generator=imageusage&giutitle=File:Albert%20Einstein%20Head.jpg&prop=info"
479 )
480 );
481
482 return $examples[$this->getModuleName()];
483 }
484
485 public function getVersion() {
486 return __CLASS__ . ': $Id$';
487 }
488 }