Don't make two database requests to load the same object, again.
authorAlexandre Emsenhuber <ialex.wiki@gmail.com>
Sat, 28 Apr 2012 13:45:37 +0000 (15:45 +0200)
committerAlexandre Emsenhuber <ialex.wiki@gmail.com>
Fri, 4 May 2012 20:10:36 +0000 (22:10 +0200)
I know there's no second parameter to WikiPage::newFromID(), but this will change soon.

This follows-up I098dd36619fff3610be6894037220d3472b809d5.

Change-Id: Ic28b7e05db51e55a5f49fed70c042ba11e4d97fe

includes/api/ApiBase.php
includes/api/ApiDelete.php
includes/api/ApiEditPage.php
includes/api/ApiProtect.php
includes/api/ApiQueryBacklinks.php
includes/api/ApiQueryCategoryMembers.php

index fc2ef77..f0386a1 100644 (file)
@@ -700,24 +700,36 @@ abstract class ApiBase extends ContextSource {
 
        /**
         * @param $params array
-        * @return Title
+        * @param $load bool|string Whether load the object's state from the database:
+        *        - false: don't load (if the pageid is given, it will still be loaded)
+        *        - 'fromdb': load from a slave database
+        *        - 'fromdbmaster': load from the master database
+        * @return WikiPage
         */
-       public function getTitleOrPageId( $params ) {
+       public function getTitleOrPageId( $params, $load = false ) {
                $this->requireOnlyOneParameter( $params, 'title', 'pageid' );
 
-               $titleObj = null;
+               $pageObj = null;
                if ( isset( $params['title'] ) ) {
                        $titleObj = Title::newFromText( $params['title'] );
                        if ( !$titleObj ) {
                                $this->dieUsageMsg( array( 'invalidtitle', $params['title'] ) );
                        }
+                       $pageObj = WikiPage::factory( $titleObj );
+                       if ( $load !== false ) {
+                               $pageObj->loadPageData( $load );
+                       }
                } elseif ( isset( $params['pageid'] ) ) {
-                       $titleObj = Title::newFromID( $params['pageid'] );
-                       if ( !$titleObj ) {
+                       if ( $load === false ) {
+                               $load = 'fromdb';
+                       }
+                       $pageObj = WikiPage::newFromID( $params['pageid'], $load );
+                       if ( !$pageObj ) {
                                $this->dieUsageMsg( array( 'nosuchpageid', $params['pageid'] ) );
                        }
                }
-               return $titleObj;
+
+               return $pageObj;
        }
 
        /**
index 21e7b80..cefdaac 100644 (file)
@@ -46,13 +46,12 @@ class ApiDelete extends ApiBase {
        public function execute() {
                $params = $this->extractRequestParams();
 
-               $titleObj = $this->getTitleOrPageId( $params );
-               $pageObj = WikiPage::factory( $titleObj );
-               $pageObj->loadPageData( 'fromdbmaster' );
+               $pageObj = $this->getTitleOrPageId( $params, 'fromdbmaster' );
                if ( !$pageObj->exists() ) {
                        $this->dieUsageMsg( 'notanarticle' );
                }
 
+               $titleObj = $pageObj->getTitle();
                $reason = ( isset( $params['reason'] ) ? $params['reason'] : null );
                $user = $this->getUser();
 
index 11f4757..19b1950 100644 (file)
@@ -48,7 +48,8 @@ class ApiEditPage extends ApiBase {
                        $this->dieUsageMsg( 'missingtext' );
                }
 
-               $titleObj = $this->getTitleOrPageId( $params );
+               $pageObj = $this->getTitleOrPageId( $params );
+               $titleObj = $pageObj->getTitle();
                if ( $titleObj->isExternal() ) {
                        $this->dieUsageMsg( array( 'invalidtitle', $params['title'] ) );
                }
index 97e79ff..d156468 100644 (file)
@@ -37,8 +37,8 @@ class ApiProtect extends ApiBase {
                global $wgRestrictionLevels;
                $params = $this->extractRequestParams();
 
-               $titleObj = $this->getTitleOrPageId( $params );
-               $pageObj = WikiPage::factory( $titleObj );
+               $pageObj = $this->getTitleOrPageId( $params, 'fromdbmaster' );
+               $titleObj = $pageObj->getTitle();
 
                $errors = $titleObj->getUserPermissionsErrors( 'protect', $this->getUser() );
                if ( $errors ) {
index bf9aa3d..8903714 100644 (file)
@@ -369,7 +369,7 @@ class ApiQueryBacklinks extends ApiQueryGeneratorBase {
                if ( !is_null( $this->params['continue'] ) ) {
                        $this->parseContinueParam();
                } else {
-                       $this->rootTitle = $this->getTitleOrPageId( $this->params );
+                       $this->rootTitle = $this->getTitleOrPageId( $this->params )->getTitle();
                }
 
                // only image titles are allowed for the root in imageinfo mode
index 8fff94d..82c28f4 100644 (file)
@@ -54,7 +54,7 @@ class ApiQueryCategoryMembers extends ApiQueryGeneratorBase {
        private function run( $resultPageSet = null ) {
                $params = $this->extractRequestParams();
 
-               $categoryTitle = $this->getTitleOrPageId( $params );
+               $categoryTitle = $this->getTitleOrPageId( $params )->getTitle();
                if ( $categoryTitle->getNamespace() != NS_CATEGORY ) {
                        $this->dieUsage( 'The category name you entered is not valid', 'invalidcategory' );
                }