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