Bug 27341 - Add drto param to list=deletedrevs
[lhc/web/wiklou.git] / includes / api / ApiQueryBacklinks.php
index a78bf08..6335f6c 100644 (file)
@@ -1,11 +1,10 @@
 <?php
-
-/*
- * Created on Oct 16, 2006
+/**
+ *
  *
- * API for MediaWiki 1.8+
+ * Created on Oct 16, 2006
  *
- * Copyright (C) 2006 Yuri Astrakhan <Firstname><Lastname>@gmail.com
+ * Copyright © 2006 Yuri Astrakhan <Firstname><Lastname>@gmail.com
  *
  * This program is free software; you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by
  *
  * You should have received a copy of the GNU General Public License along
  * with this program; if not, write to the Free Software Foundation, Inc.,
- * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
+ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  * http://www.gnu.org/copyleft/gpl.html
+ *
+ * @file
  */
 
 if ( !defined( 'MEDIAWIKI' ) ) {
@@ -38,9 +39,24 @@ if ( !defined( 'MEDIAWIKI' ) ) {
  */
 class ApiQueryBacklinks extends ApiQueryGeneratorBase {
 
-       private $params, $rootTitle, $contRedirs, $contLevel, $contTitle, $contID, $redirID, $redirect;
+       /**
+        * @var Title
+        */
+       private $rootTitle;
+
+       private $params, $contID, $redirID, $redirect;
        private $bl_ns, $bl_from, $bl_table, $bl_code, $bl_title, $bl_sort, $bl_fields, $hasNS;
-       private $pageMap, $resultArr;
+
+       /**
+        * Maps ns and title to pageid
+        *
+        * @var array
+        */
+       private $pageMap = array();
+       private $resultArr;
+
+       private $redirTitles = array();
+       private $continueStr = null;
 
        // output element name, database column field prefix, database table
        private $backlinksSettings = array(
@@ -62,13 +78,15 @@ class ApiQueryBacklinks extends ApiQueryGeneratorBase {
        );
 
        public function __construct( $query, $moduleName ) {
-               extract( $this->backlinksSettings[$moduleName] );
+               $settings = $this->backlinksSettings[$moduleName];
+               $prefix = $settings['prefix'];
+               $code = $settings['code'];
                $this->resultArr = array();
 
                parent::__construct( $query, $moduleName, $code );
                $this->bl_ns = $prefix . '_namespace';
                $this->bl_from = $prefix . '_from';
-               $this->bl_table = $linktbl;
+               $this->bl_table = $settings['linktbl'];
                $this->bl_code = $code;
 
                $this->hasNS = $moduleName !== 'imageusage';
@@ -92,17 +110,24 @@ class ApiQueryBacklinks extends ApiQueryGeneratorBase {
                $this->run();
        }
 
+       public function getCacheMode( $params ) {
+               return 'public';
+       }
+
        public function executeGenerator( $resultPageSet ) {
                $this->run( $resultPageSet );
        }
 
+       /**
+        * @param $resultPageSet ApiPageSet
+        * @return void
+        */
        private function prepareFirstQuery( $resultPageSet = null ) {
                /* SELECT page_id, page_title, page_namespace, page_is_redirect
                 * FROM pagelinks, page WHERE pl_from=page_id
                 * AND pl_title='Foo' AND pl_namespace=0
                 * LIMIT 11 ORDER BY pl_from
                 */
-               $db = $this->getDB();
                $this->addTables( array( $this->bl_table, 'page' ) );
                $this->addWhere( "{$this->bl_from}=page_id" );
                if ( is_null( $resultPageSet ) ) {
@@ -135,6 +160,10 @@ class ApiQueryBacklinks extends ApiQueryGeneratorBase {
                $this->addOption( 'STRAIGHT_JOIN' );
        }
 
+       /**
+        * @param $resultPageSet ApiPageSet
+        * @return void
+        */
        private function prepareSecondQuery( $resultPageSet = null ) {
                /* SELECT page_id, page_title, page_namespace, page_is_redirect, pl_title, pl_namespace
                   FROM pagelinks, page WHERE pl_from=page_id
@@ -193,6 +222,10 @@ class ApiQueryBacklinks extends ApiQueryGeneratorBase {
                $this->addOption( 'USE INDEX', array( 'page' => 'PRIMARY' ) );
        }
 
+       /**
+        * @param $resultPageSet ApiPageSet
+        * @return void
+        */
        private function run( $resultPageSet = null ) {
                $this->params = $this->extractRequestParams( false );
                $this->redirect = isset( $this->params['redirect'] ) && $this->params['redirect'];
@@ -200,20 +233,17 @@ class ApiQueryBacklinks extends ApiQueryGeneratorBase {
                $botMax  = ( $this->redirect ? ApiBase::LIMIT_BIG2 / 2 : ApiBase::LIMIT_BIG2 );
                if ( $this->params['limit'] == 'max' ) {
                        $this->params['limit'] = $this->getMain()->canApiHighLimits() ? $botMax : $userMax;
-                       $this->getResult()->addValue( 'limits', $this->getModuleName(), $this->params['limit'] );
+                       $this->getResult()->setParsedLimit( $this->getModuleName(), $this->params['limit'] );
                }
 
                $this->processContinue();
                $this->prepareFirstQuery( $resultPageSet );
 
-               $db = $this->getDB();
                $res = $this->select( __METHOD__ . '::firstQuery' );
 
                $count = 0;
-               $this->pageMap = array(); // Maps ns and title to pageid
-               $this->continueStr = null;
-               $this->redirTitles = array();
-               while ( $row = $db->fetchObject( $res ) ) {
+
+               foreach ( $res as $row ) {
                        if ( ++ $count > $this->params['limit'] ) {
                                // We've reached the one extra which shows that there are additional pages to be had. Stop here...
                                // Continue string preserved in case the redirect query doesn't pass the limit
@@ -232,14 +262,13 @@ class ApiQueryBacklinks extends ApiQueryGeneratorBase {
                                $resultPageSet->processDbRow( $row );
                        }
                }
-               $db->freeResult( $res );
 
                if ( $this->redirect && count( $this->redirTitles ) ) {
                        $this->resetQueryParams();
                        $this->prepareSecondQuery( $resultPageSet );
                        $res = $this->select( __METHOD__ . '::secondQuery' );
                        $count = 0;
-                       while ( $row = $db->fetchObject( $res ) ) {
+                       foreach ( $res as $row ) {
                                if ( ++$count > $this->params['limit'] ) {
                                        // We've reached the one extra which shows that there are additional pages to be had. Stop here...
                                        // We need to keep the parent page of this redir in
@@ -258,7 +287,6 @@ class ApiQueryBacklinks extends ApiQueryGeneratorBase {
                                        $resultPageSet->processDbRow( $row );
                                }
                        }
-                       $db->freeResult( $res );
                }
                if ( is_null( $resultPageSet ) ) {
                        // Try to add the result data in one go and pray that it fits
@@ -345,8 +373,6 @@ class ApiQueryBacklinks extends ApiQueryGeneratorBase {
                                } else {
                                        $this->rootTitle = $title;
                                }
-                       } else {
-                               $this->dieUsageMsg( array( 'missingparam', 'title' ) );
                        }
                }
 
@@ -404,7 +430,10 @@ class ApiQueryBacklinks extends ApiQueryGeneratorBase {
 
        public function getAllowedParams() {
                $retval = array(
-                       'title' => null,
+                       'title' => array(
+                               ApiBase::PARAM_TYPE => 'string',
+                               ApiBase::PARAM_REQUIRED => true
+                       ),
                        'continue' => null,
                        'namespace' => array(
                                ApiBase::PARAM_ISMULTI => true,
@@ -468,7 +497,6 @@ class ApiQueryBacklinks extends ApiQueryGeneratorBase {
        public function getPossibleErrors() {
                return array_merge( parent::getPossibleErrors(), array(
                        array( 'invalidtitle', 'title' ),
-                       array( 'missingparam', 'title' ),
                        array( 'code' => 'bad_image_title', 'info' => "The title for {$this->getModuleName()} query must be an image" ),
                        array( 'code' => '_badcontinue', 'info' => 'Invalid continue param. You should pass the original value returned by the previous query' ),
                ) );