Merge "Added some extra tests for ORMRow class"
[lhc/web/wiklou.git] / includes / api / ApiQueryBacklinks.php
index 6d22160..fcb9c4b 100644 (file)
@@ -1,11 +1,10 @@
 <?php
-
-/*
- * Created on Oct 16, 2006
+/**
  *
- * API for MediaWiki 1.8+
  *
- * Copyright (C) 2006 Yuri Astrakhan <Firstname><Lastname>@gmail.com
+ * Created on Oct 16, 2006
+ *
+ * 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
  * with this program; if not, write to the Free Software Foundation, Inc.,
  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  * http://www.gnu.org/copyleft/gpl.html
+ *
+ * @file
  */
 
-if ( !defined( 'MEDIAWIKI' ) ) {
-       // Eclipse helper - will be ignored in production
-       require_once( "ApiQueryBase.php" );
-}
-
 /**
  * This is a three-in-one module to query:
  *   * backlinks  - links pointing to the given page,
@@ -38,38 +34,59 @@ if ( !defined( 'MEDIAWIKI' ) ) {
  */
 class ApiQueryBacklinks extends ApiQueryGeneratorBase {
 
-       private $params, $rootTitle, $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(
                'backlinks' => array(
                        'code' => 'bl',
                        'prefix' => 'pl',
-                       'linktbl' => 'pagelinks'
+                       'linktbl' => 'pagelinks',
+                       'helpurl' => 'https://www.mediawiki.org/wiki/API:Backlinks',
                ),
                'embeddedin' => array(
                        'code' => 'ei',
                        'prefix' => 'tl',
-                       'linktbl' => 'templatelinks'
+                       'linktbl' => 'templatelinks',
+                       'helpurl' => 'https://www.mediawiki.org/wiki/API:Embeddedin',
                ),
                'imageusage' => array(
                        'code' => 'iu',
                        'prefix' => 'il',
-                       'linktbl' => 'imagelinks'
+                       'linktbl' => 'imagelinks',
+                       'helpurl' => 'https://www.mediawiki.org/wiki/API:Imageusage',
                )
        );
 
        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->helpUrl = $settings['helpurl'];
 
                $this->hasNS = $moduleName !== 'imageusage';
                if ( $this->hasNS ) {
@@ -100,6 +117,10 @@ class ApiQueryBacklinks extends ApiQueryGeneratorBase {
                $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
@@ -138,6 +159,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
@@ -163,25 +188,25 @@ class ApiQueryBacklinks extends ApiQueryGeneratorBase {
                $titleWhere = array();
                foreach ( $this->redirTitles as $t ) {
                        $titleWhere[] = "{$this->bl_title} = " . $db->addQuotes( $t->getDBkey() ) .
-                                       ( $this->hasNS ? " AND {$this->bl_ns} = '{$t->getNamespace()}'" : '' );
+                                       ( $this->hasNS ? " AND {$this->bl_ns} = {$t->getNamespace()}" : '' );
                }
                $this->addWhere( $db->makeList( $titleWhere, LIST_OR ) );
                $this->addWhereFld( 'page_namespace', $this->params['namespace'] );
 
                if ( !is_null( $this->redirID ) ) {
                        $first = $this->redirTitles[0];
-                       $title = $db->strencode( $first->getDBkey() );
+                       $title = $db->addQuotes( $first->getDBkey() );
                        $ns = $first->getNamespace();
                        $from = $this->redirID;
                        if ( $this->hasNS ) {
                                $this->addWhere( "{$this->bl_ns} > $ns OR " .
                                                "({$this->bl_ns} = $ns AND " .
-                                               "({$this->bl_title} > '$title' OR " .
-                                               "({$this->bl_title} = '$title' AND " .
+                                               "({$this->bl_title} > $title OR " .
+                                               "({$this->bl_title} = $title AND " .
                                                "{$this->bl_from} >= $from)))" );
                        } else {
-                               $this->addWhere( "{$this->bl_title} > '$title' OR " .
-                                               "({$this->bl_title} = '$title' AND " .
+                               $this->addWhere( "{$this->bl_title} > $title OR " .
+                                               "({$this->bl_title} = $title AND " .
                                                "{$this->bl_from} >= $from)" );
                        }
                }
@@ -196,14 +221,21 @@ 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'];
                $userMax = ( $this->redirect ? ApiBase::LIMIT_BIG1 / 2 : ApiBase::LIMIT_BIG1 );
                $botMax  = ( $this->redirect ? ApiBase::LIMIT_BIG2 / 2 : ApiBase::LIMIT_BIG2 );
+
+               $result = $this->getResult();
+
                if ( $this->params['limit'] == 'max' ) {
                        $this->params['limit'] = $this->getMain()->canApiHighLimits() ? $botMax : $userMax;
-                       $this->getResult()->setParsedLimit( $this->getModuleName(), $this->params['limit'] );
+                       $result->setParsedLimit( $this->getModuleName(), $this->params['limit'] );
                }
 
                $this->processContinue();
@@ -212,9 +244,7 @@ class ApiQueryBacklinks extends ApiQueryGeneratorBase {
                $res = $this->select( __METHOD__ . '::firstQuery' );
 
                $count = 0;
-               $this->pageMap = array(); // Maps ns and title to pageid
-               $this->continueStr = null;
-               $this->redirTitles = array();
+
                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...
@@ -262,13 +292,13 @@ class ApiQueryBacklinks extends ApiQueryGeneratorBase {
                }
                if ( is_null( $resultPageSet ) ) {
                        // Try to add the result data in one go and pray that it fits
-                       $fit = $this->getResult()->addValue( 'query', $this->getModuleName(), array_values( $this->resultArr ) );
+                       $fit = $result->addValue( 'query', $this->getModuleName(), array_values( $this->resultArr ) );
                        if ( !$fit ) {
                                // It didn't fit. Add elements one by one until the
                                // result is full.
                                foreach ( $this->resultArr as $pageID => $arr ) {
                                        // Add the basic entry without redirlinks first
-                                       $fit = $this->getResult()->addValue(
+                                       $fit = $result->addValue(
                                                array( 'query', $this->getModuleName() ),
                                                null, array_diff_key( $arr, array( 'redirlinks' => '' ) ) );
                                        if ( !$fit ) {
@@ -277,8 +307,9 @@ class ApiQueryBacklinks extends ApiQueryGeneratorBase {
                                        }
 
                                        $hasRedirs = false;
-                                       foreach ( (array)@$arr['redirlinks'] as $key => $redir ) {
-                                               $fit = $this->getResult()->addValue(
+                                       $redirLinks = isset( $arr['redirlinks'] ) ? $arr['redirlinks'] : array();
+                                       foreach ( (array)$redirLinks as $key => $redir ) {
+                                               $fit = $result->addValue(
                                                        array( 'query', $this->getModuleName(), $pageID, 'redirlinks' ),
                                                        $key, $redir );
                                                if ( !$fit ) {
@@ -288,7 +319,7 @@ class ApiQueryBacklinks extends ApiQueryGeneratorBase {
                                                $hasRedirs = true;
                                        }
                                        if ( $hasRedirs ) {
-                                               $this->getResult()->setIndexedTagName_internal(
+                                               $result->setIndexedTagName_internal(
                                                        array( 'query', $this->getModuleName(), $pageID, 'redirlinks' ),
                                                        $this->bl_code );
                                        }
@@ -298,7 +329,7 @@ class ApiQueryBacklinks extends ApiQueryGeneratorBase {
                                }
                        }
 
-                       $this->getResult()->setIndexedTagName_internal(
+                       $result->setIndexedTagName_internal(
                                array( 'query', $this->getModuleName() ),
                                $this->bl_code
                        );
@@ -338,14 +369,7 @@ class ApiQueryBacklinks extends ApiQueryGeneratorBase {
                if ( !is_null( $this->params['continue'] ) ) {
                        $this->parseContinueParam();
                } else {
-                       if ( $this->params['title'] !== '' ) {
-                               $title = Title::newFromText( $this->params['title'] );
-                               if ( !$title ) {
-                                       $this->dieUsageMsg( array( 'invalidtitle', $this->params['title'] ) );
-                               } else {
-                                       $this->rootTitle = $title;
-                               }
-                       }
+                       $this->rootTitle = $this->getTitleOrPageId( $this->params )->getTitle();
                }
 
                // only image titles are allowed for the root in imageinfo mode
@@ -380,9 +404,10 @@ class ApiQueryBacklinks extends ApiQueryGeneratorBase {
                        $this->dieUsage( 'Invalid continue param. You should pass the original value returned by the previous query', '_badcontinue' );
                }
                $this->contID = $contID;
-               $redirID = intval( @$continueList[3] );
+               $id2 = isset( $continueList[3] ) ? $continueList[3] : null;
+               $redirID = intval( $id2 );
 
-               if ( $redirID === 0 && @$continueList[3] !== '0' ) {
+               if ( $redirID === 0 && $id2 !== '0' ) {
                        // This one isn't required
                        return;
                }
@@ -404,7 +429,9 @@ class ApiQueryBacklinks extends ApiQueryGeneratorBase {
                $retval = array(
                        'title' => array(
                                ApiBase::PARAM_TYPE => 'string',
-                               ApiBase::PARAM_REQUIRED => 1
+                       ),
+                       'pageid' => array(
+                               ApiBase::PARAM_TYPE => 'integer',
                        ),
                        'continue' => null,
                        'namespace' => array(
@@ -436,7 +463,8 @@ class ApiQueryBacklinks extends ApiQueryGeneratorBase {
 
        public function getParamDescription() {
                $retval = array(
-                       'title' => 'Title to search',
+                       'title' => "Title to search. Cannot be used together with {$this->bl_code}pageid",
+                       'pageid' => "Pageid to search. Cannot be used together with {$this->bl_code}title",
                        'continue' => 'When more results are available, use this to continue',
                        'namespace' => 'The namespace to enumerate',
                );
@@ -453,6 +481,17 @@ class ApiQueryBacklinks extends ApiQueryGeneratorBase {
                ) );
        }
 
+       public function getResultProperties() {
+               return array(
+                       '' => array(
+                               'pageid' => 'integer',
+                               'ns' => 'namespace',
+                               'title' => 'string',
+                               'redirect' => 'boolean'
+                       )
+               );
+       }
+
        public function getDescription() {
                switch ( $this->getModuleName() ) {
                        case 'backlinks':
@@ -467,15 +506,16 @@ 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' ),
-               ) );
+               return array_merge( parent::getPossibleErrors(),
+                       $this->getTitleOrPageIdErrorMessage(),
+                       array(
+                               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' ),
+                       )
+               );
        }
 
-       protected function getExamples() {
+       public function getExamples() {
                static $examples = array(
                        'backlinks' => array(
                                'api.php?action=query&list=backlinks&bltitle=Main%20Page',
@@ -494,6 +534,10 @@ class ApiQueryBacklinks extends ApiQueryGeneratorBase {
                return $examples[$this->getModuleName()];
        }
 
+       public function getHelpUrls() {
+               return $this->helpUrl;
+       }
+
        public function getVersion() {
                return __CLASS__ . ': $Id$';
        }