stylize.php on API code
[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 $this->addFields( 'page_is_redirect' );
113 $this->addWhereFld( $this->bl_title, $this->rootTitle->getDBkey() );
114 if ( $this->hasNS )
115 $this->addWhereFld( $this->bl_ns, $this->rootTitle->getNamespace() );
116 $this->addWhereFld( 'page_namespace', $this->params['namespace'] );
117 if ( !is_null( $this->contID ) )
118 $this->addWhere( "{$this->bl_from}>={$this->contID}" );
119 if ( $this->params['filterredir'] == 'redirects' )
120 $this->addWhereFld( 'page_is_redirect', 1 );
121 if ( $this->params['filterredir'] == 'nonredirects' )
122 $this->addWhereFld( 'page_is_redirect', 0 );
123 $this->addOption( 'LIMIT', $this->params['limit'] + 1 );
124 $this->addOption( 'ORDER BY', $this->bl_from );
125 $this->addOption( 'STRAIGHT_JOIN' );
126 }
127
128 private function prepareSecondQuery( $resultPageSet = null ) {
129 /* SELECT page_id, page_title, page_namespace, page_is_redirect, pl_title, pl_namespace
130 FROM pagelinks, page WHERE pl_from=page_id
131 AND (pl_title='Foo' AND pl_namespace=0) OR (pl_title='Bar' AND pl_namespace=1)
132 ORDER BY pl_namespace, pl_title, pl_from LIMIT 11
133 */
134 $db = $this->getDB();
135 $this->addTables( array( 'page', $this->bl_table ) );
136 $this->addWhere( "{$this->bl_from}=page_id" );
137 if ( is_null( $resultPageSet ) )
138 $this->addFields( array( 'page_id', 'page_title', 'page_namespace', 'page_is_redirect' ) );
139 else
140 $this->addFields( $resultPageSet->getPageTableFields() );
141 $this->addFields( $this->bl_title );
142 if ( $this->hasNS )
143 $this->addFields( $this->bl_ns );
144 // We can't use LinkBatch here because $this->hasNS may be false
145 $titleWhere = array();
146 foreach ( $this->redirTitles as $t )
147 $titleWhere[] = "{$this->bl_title} = " . $db->addQuotes( $t->getDBkey() ) .
148 ( $this->hasNS ? " AND {$this->bl_ns} = '{$t->getNamespace()}'" : "" );
149 $this->addWhere( $db->makeList( $titleWhere, LIST_OR ) );
150 $this->addWhereFld( 'page_namespace', $this->params['namespace'] );
151 if ( !is_null( $this->redirID ) )
152 {
153 $first = $this->redirTitles[0];
154 $title = $db->strencode( $first->getDBkey() );
155 $ns = $first->getNamespace();
156 $from = $this->redirID;
157 if ( $this->hasNS )
158 $this->addWhere( "{$this->bl_ns} > $ns OR " .
159 "({$this->bl_ns} = $ns AND " .
160 "({$this->bl_title} > '$title' OR " .
161 "({$this->bl_title} = '$title' AND " .
162 "{$this->bl_from} >= $from)))" );
163 else
164 $this->addWhere( "{$this->bl_title} > '$title' OR " .
165 "({$this->bl_title} = '$title' AND " .
166 "{$this->bl_from} >= $from)" );
167
168 }
169 if ( $this->params['filterredir'] == 'redirects' )
170 $this->addWhereFld( 'page_is_redirect', 1 );
171 if ( $this->params['filterredir'] == 'nonredirects' )
172 $this->addWhereFld( 'page_is_redirect', 0 );
173 $this->addOption( 'LIMIT', $this->params['limit'] + 1 );
174 $this->addOption( 'ORDER BY', $this->bl_sort );
175 $this->addOption( 'USE INDEX', array( 'page' => 'PRIMARY' ) );
176 }
177
178 private function run( $resultPageSet = null ) {
179 $this->params = $this->extractRequestParams( false );
180 $this->redirect = isset( $this->params['redirect'] ) && $this->params['redirect'];
181 $userMax = ( $this->redirect ? ApiBase::LIMIT_BIG1 / 2 : ApiBase::LIMIT_BIG1 );
182 $botMax = ( $this->redirect ? ApiBase::LIMIT_BIG2 / 2 : ApiBase::LIMIT_BIG2 );
183 if ( $this->params['limit'] == 'max' ) {
184 $this->params['limit'] = $this->getMain()->canApiHighLimits() ? $botMax : $userMax;
185 $this->getResult()->addValue( 'limits', $this->getModuleName(), $this->params['limit'] );
186 }
187
188 $this->processContinue();
189 $this->prepareFirstQuery( $resultPageSet );
190
191 $db = $this->getDB();
192 $res = $this->select( __METHOD__ . '::firstQuery' );
193
194 $count = 0;
195 $this->pageMap = array(); // Maps ns and title to pageid
196 $this->continueStr = null;
197 $this->redirTitles = array();
198 while ( $row = $db->fetchObject( $res ) ) {
199 if ( ++ $count > $this->params['limit'] ) {
200 // We've reached the one extra which shows that there are additional pages to be had. Stop here...
201 // Continue string preserved in case the redirect query doesn't pass the limit
202 $this->continueStr = $this->getContinueStr( $row->page_id );
203 break;
204 }
205
206 if ( is_null( $resultPageSet ) )
207 $this->extractRowInfo( $row );
208 else
209 {
210 $this->pageMap[$row->page_namespace][$row->page_title] = $row->page_id;
211 if ( $row->page_is_redirect )
212 $this->redirTitles[] = Title::makeTitle( $row->page_namespace, $row->page_title );
213 $resultPageSet->processDbRow( $row );
214 }
215 }
216 $db->freeResult( $res );
217
218 if ( $this->redirect && count( $this->redirTitles ) )
219 {
220 $this->resetQueryParams();
221 $this->prepareSecondQuery( $resultPageSet );
222 $res = $this->select( __METHOD__ . '::secondQuery' );
223 $count = 0;
224 while ( $row = $db->fetchObject( $res ) )
225 {
226 if ( ++$count > $this->params['limit'] )
227 {
228 // We've reached the one extra which shows that there are additional pages to be had. Stop here...
229 // We need to keep the parent page of this redir in
230 if ( $this->hasNS )
231 $parentID = $this->pageMap[$row-> { $this->bl_ns } ][$row-> { $this->bl_title } ];
232 else
233 $parentID = $this->pageMap[NS_IMAGE][$row-> { $this->bl_title } ];
234 $this->continueStr = $this->getContinueRedirStr( $parentID, $row->page_id );
235 break;
236 }
237
238 if ( is_null( $resultPageSet ) )
239 $this->extractRedirRowInfo( $row );
240 else
241 $resultPageSet->processDbRow( $row );
242 }
243 $db->freeResult( $res );
244 }
245 if ( is_null( $resultPageSet ) ) {
246 // Try to add the result data in one go and pray that it fits
247 $fit = $this->getResult()->addValue( 'query', $this->getModuleName(), array_values( $this->resultArr ) );
248 if ( !$fit )
249 {
250 // It didn't fit. Add elements one by one until the
251 // result is full.
252 foreach ( $this->resultArr as $pageID => $arr )
253 {
254 // Add the basic entry without redirlinks first
255 $fit = $this->getResult()->addValue(
256 array( 'query', $this->getModuleName() ),
257 null, array_diff_key( $arr, array( 'redirlinks' => '' ) ) );
258 if ( !$fit )
259 {
260 $this->continueStr = $this->getContinueStr( $pageID );
261 break;
262 }
263
264 $hasRedirs = false;
265 foreach ( (array)@$arr['redirlinks'] as $key => $redir )
266 {
267 $fit = $this->getResult()->addValue(
268 array( 'query', $this->getModuleName(), $pageID, 'redirlinks' ),
269 $key, $redir );
270 if ( !$fit )
271 {
272 $this->continueStr = $this->getContinueRedirStr( $pageID, $redir['pageid'] );
273 break;
274 }
275 $hasRedirs = true;
276 }
277 if ( $hasRedirs )
278 $this->getResult()->setIndexedTagName_internal(
279 array( 'query', $this->getModuleName(), $pageID, 'redirlinks' ),
280 $this->bl_code );
281 if ( !$fit )
282 break;
283 }
284 }
285
286 $this->getResult()->setIndexedTagName_internal(
287 array( 'query', $this->getModuleName() ),
288 $this->bl_code );
289 }
290 if ( !is_null( $this->continueStr ) )
291 $this->setContinueEnumParameter( 'continue', $this->continueStr );
292 }
293
294 private function extractRowInfo( $row ) {
295 $this->pageMap[$row->page_namespace][$row->page_title] = $row->page_id;
296 $t = Title::makeTitle( $row->page_namespace, $row->page_title );
297 $a = array( 'pageid' => intval( $row->page_id ) );
298 ApiQueryBase::addTitleInfo( $a, $t );
299 if ( $row->page_is_redirect )
300 {
301 $a['redirect'] = '';
302 $this->redirTitles[] = $t;
303 }
304 // Put all the results in an array first
305 $this->resultArr[$a['pageid']] = $a;
306 }
307
308 private function extractRedirRowInfo( $row )
309 {
310 $a['pageid'] = intval( $row->page_id );
311 ApiQueryBase::addTitleInfo( $a, Title::makeTitle( $row->page_namespace, $row->page_title ) );
312 if ( $row->page_is_redirect )
313 $a['redirect'] = '';
314 $ns = $this->hasNS ? $row-> { $this->bl_ns } : NS_FILE;
315 $parentID = $this->pageMap[$ns][$row-> { $this->bl_title } ];
316 // Put all the results in an array first
317 $this->resultArr[$parentID]['redirlinks'][] = $a;
318 $this->getResult()->setIndexedTagName( $this->resultArr[$parentID]['redirlinks'], $this->bl_code );
319 }
320
321 protected function processContinue() {
322 if ( !is_null( $this->params['continue'] ) )
323 $this->parseContinueParam();
324 else {
325 if ( $this->params['title'] !== "" ) {
326 $title = Title::newFromText( $this->params['title'] );
327 if ( !$title ) {
328 $this->dieUsageMsg( array( 'invalidtitle', $this->params['title'] ) );
329 } else {
330 $this->rootTitle = $title;
331 }
332 } else {
333 $this->dieUsageMsg( array( 'missingparam', 'title' ) );
334 }
335 }
336
337 // only image titles are allowed for the root in imageinfo mode
338 if ( !$this->hasNS && $this->rootTitle->getNamespace() !== NS_FILE )
339 $this->dieUsage( "The title for {$this->getModuleName()} query must be an image", 'bad_image_title' );
340 }
341
342 protected function parseContinueParam() {
343 $continueList = explode( '|', $this->params['continue'] );
344 // expected format:
345 // ns | key | id1 [| id2]
346 // ns+key: root title
347 // id1: first-level page ID to continue from
348 // id2: second-level page ID to continue from
349
350 // null stuff out now so we know what's set and what isn't
351 $this->rootTitle = $this->contID = $this->redirID = null;
352 $rootNs = intval( $continueList[0] );
353 if ( $rootNs === 0 && $continueList[0] !== '0' )
354 // Illegal continue parameter
355 $this->dieUsage( "Invalid continue param. You should pass the original value returned by the previous query", "_badcontinue" );
356 $this->rootTitle = Title::makeTitleSafe( $rootNs, $continueList[1] );
357 if ( !$this->rootTitle )
358 $this->dieUsage( "Invalid continue param. You should pass the original value returned by the previous query", "_badcontinue" );
359 $contID = intval( $continueList[2] );
360 if ( $contID === 0 && $continueList[2] !== '0' )
361 $this->dieUsage( "Invalid continue param. You should pass the original value returned by the previous query", "_badcontinue" );
362 $this->contID = $contID;
363 $redirID = intval( @$continueList[3] );
364 if ( $redirID === 0 && @$continueList[3] !== '0' )
365 // This one isn't required
366 return;
367 $this->redirID = $redirID;
368
369 }
370
371 protected function getContinueStr( $lastPageID ) {
372 return $this->rootTitle->getNamespace() .
373 '|' . $this->rootTitle->getDBkey() .
374 '|' . $lastPageID;
375 }
376
377 protected function getContinueRedirStr( $lastPageID, $lastRedirID ) {
378 return $this->getContinueStr( $lastPageID ) . '|' . $lastRedirID;
379 }
380
381 public function getAllowedParams() {
382 $retval = array (
383 'title' => null,
384 'continue' => null,
385 'namespace' => array (
386 ApiBase :: PARAM_ISMULTI => true,
387 ApiBase :: PARAM_TYPE => 'namespace'
388 ),
389 'filterredir' => array(
390 ApiBase :: PARAM_DFLT => 'all',
391 ApiBase :: PARAM_TYPE => array(
392 'all',
393 'redirects',
394 'nonredirects'
395 )
396 ),
397 'limit' => array (
398 ApiBase :: PARAM_DFLT => 10,
399 ApiBase :: PARAM_TYPE => 'limit',
400 ApiBase :: PARAM_MIN => 1,
401 ApiBase :: PARAM_MAX => ApiBase :: LIMIT_BIG1,
402 ApiBase :: PARAM_MAX2 => ApiBase :: LIMIT_BIG2
403 )
404 );
405 if ( $this->getModuleName() == 'embeddedin' )
406 return $retval;
407 $retval['redirect'] = false;
408 return $retval;
409 }
410
411 public function getParamDescription() {
412 $retval = array (
413 'title' => 'Title to search.',
414 'continue' => 'When more results are available, use this to continue.',
415 'namespace' => 'The namespace to enumerate.',
416 'filterredir' => 'How to filter for redirects'
417 );
418 if ( $this->getModuleName() != 'embeddedin' )
419 return array_merge( $retval, array(
420 'redirect' => 'If linking page is a redirect, find all pages that link to that redirect as well. Maximum limit is halved.',
421 '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)."
422 ) );
423 return array_merge( $retval, array(
424 'limit' => "How many total pages to return."
425 ) );
426 }
427
428 public function getDescription() {
429 switch ( $this->getModuleName() ) {
430 case 'backlinks' :
431 return 'Find all pages that link to the given page';
432 case 'embeddedin' :
433 return 'Find all pages that embed (transclude) the given title';
434 case 'imageusage' :
435 return 'Find all pages that use the given image title.';
436 default :
437 ApiBase :: dieDebug( __METHOD__, 'Unknown module name' );
438 }
439 }
440
441 protected function getExamples() {
442 static $examples = array (
443 'backlinks' => array (
444 "api.php?action=query&list=backlinks&bltitle=Main%20Page",
445 "api.php?action=query&generator=backlinks&gbltitle=Main%20Page&prop=info"
446 ),
447 'embeddedin' => array (
448 "api.php?action=query&list=embeddedin&eititle=Template:Stub",
449 "api.php?action=query&generator=embeddedin&geititle=Template:Stub&prop=info"
450 ),
451 'imageusage' => array (
452 "api.php?action=query&list=imageusage&iutitle=File:Albert%20Einstein%20Head.jpg",
453 "api.php?action=query&generator=imageusage&giutitle=File:Albert%20Einstein%20Head.jpg&prop=info"
454 )
455 );
456
457 return $examples[$this->getModuleName()];
458 }
459
460 public function getVersion() {
461 return __CLASS__ . ': $Id$';
462 }
463 }