From: Alexandre Emsenhuber Date: Sat, 28 Apr 2012 13:45:37 +0000 (+0200) Subject: Don't make two database requests to load the same object, again. X-Git-Tag: 1.31.0-rc.0~23688^2 X-Git-Url: https://git.cyclocoop.org/%7B%24www_url%7Dadmin/compta/exercices/journal.php?a=commitdiff_plain;h=18cc2200381f387aa5cac3f24540afff3ce9eca4;p=lhc%2Fweb%2Fwiklou.git Don't make two database requests to load the same object, again. I know there's no second parameter to WikiPage::newFromID(), but this will change soon. This follows-up I098dd36619fff3610be6894037220d3472b809d5. Change-Id: Ic28b7e05db51e55a5f49fed70c042ba11e4d97fe --- diff --git a/includes/api/ApiBase.php b/includes/api/ApiBase.php index fc2ef77247..f0386a1b35 100644 --- a/includes/api/ApiBase.php +++ b/includes/api/ApiBase.php @@ -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; } /** diff --git a/includes/api/ApiDelete.php b/includes/api/ApiDelete.php index 21e7b80927..cefdaac783 100644 --- a/includes/api/ApiDelete.php +++ b/includes/api/ApiDelete.php @@ -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(); diff --git a/includes/api/ApiEditPage.php b/includes/api/ApiEditPage.php index 11f4757119..19b1950cb6 100644 --- a/includes/api/ApiEditPage.php +++ b/includes/api/ApiEditPage.php @@ -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'] ) ); } diff --git a/includes/api/ApiProtect.php b/includes/api/ApiProtect.php index 97e79ffe18..d1564689ec 100644 --- a/includes/api/ApiProtect.php +++ b/includes/api/ApiProtect.php @@ -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 ) { diff --git a/includes/api/ApiQueryBacklinks.php b/includes/api/ApiQueryBacklinks.php index bf9aa3d6b3..8903714fc3 100644 --- a/includes/api/ApiQueryBacklinks.php +++ b/includes/api/ApiQueryBacklinks.php @@ -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 diff --git a/includes/api/ApiQueryCategoryMembers.php b/includes/api/ApiQueryCategoryMembers.php index 8fff94dc79..82c28f4f92 100644 --- a/includes/api/ApiQueryCategoryMembers.php +++ b/includes/api/ApiQueryCategoryMembers.php @@ -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' ); }