From 658ae44bd13978a00820431887a21e8527709808 Mon Sep 17 00:00:00 2001 From: Alexandre Emsenhuber Date: Tue, 17 Apr 2012 13:13:57 +0200 Subject: [PATCH] Optimise WikiPage::newFromID() to load all the data in one database query. * Separated the last part of WikiPage::loadPageData() into loadFromRow() to put database access and object loading into separate methods (and also for consistency with other classes) * Added WikiPage::newFromRow() to be able to get a WikiPage object from an already-existing database row * Modified WikiPage::newFromID() to do a single database request to get all the necessary data and make it use the new newFromRow() method Change-Id: I2db423f2dba10cc3db4f4b2e7a7b9d99cd114f33 --- includes/WikiPage.php | 34 ++++++++++++++++++++++++++++++---- 1 file changed, 30 insertions(+), 4 deletions(-) diff --git a/includes/WikiPage.php b/includes/WikiPage.php index 6cad466414..2678f54328 100644 --- a/includes/WikiPage.php +++ b/includes/WikiPage.php @@ -121,11 +121,26 @@ class WikiPage extends Page { * @return WikiPage|null */ public static function newFromID( $id ) { - $t = Title::newFromID( $id ); - if ( $t ) { - return self::factory( $t ); + $dbr = wfGetDB( DB_SLAVE ); + $row = $dbr->selectRow( 'page', self::selectFields(), array( 'page_id' => $id ), __METHOD__ ); + if ( !$row ) { + return null; } - return null; + return self::newFromRow( $row ); + } + + /** + * Constructor from a database row + * + * @since 1.20 + * @param $row object: database row containing at least fields returned + * by selectFields(). + * @return WikiPage + */ + public static function newFromRow( $row ) { + $page = self::factory( Title::newFromRow( $row ) ); + $page->loadFromRow( $row ); + return $page; } /** @@ -256,6 +271,17 @@ class WikiPage extends Page { } } + $this->loadFromRow( $data ); + } + + /** + * Load the object from a database row + * + * @since 1.20 + * @param $data object: database row containing at least fields returned + * by selectFields() + */ + public function loadFromRow( $data ) { $lc = LinkCache::singleton(); if ( $data ) { -- 2.20.1