From b56197b7bab6e589a0ed068e493c08652eec4f5d Mon Sep 17 00:00:00 2001 From: Sam Reed Date: Mon, 31 Jan 2011 23:41:38 +0000 Subject: [PATCH] Function out query building, to allow easier overriding --- includes/specials/SpecialRandompage.php | 38 ++++++++++++++++--------- 1 file changed, 25 insertions(+), 13 deletions(-) diff --git a/includes/specials/SpecialRandompage.php b/includes/specials/SpecialRandompage.php index dcb6f5c96b..4e3febe3b9 100644 --- a/includes/specials/SpecialRandompage.php +++ b/includes/specials/SpecialRandompage.php @@ -94,7 +94,6 @@ class RandomPage extends SpecialPage { return $wgContLang->commaList( $nsNames ); } - /** * Choose a random title. * @return Title object (or null if nothing to choose from) @@ -126,12 +125,10 @@ class RandomPage extends SpecialPage { } } - private function selectRandomPageFromDB( $randstr ) { + protected function getQueryInfo( $randstr ) { global $wgExtraRandompageSQL; - $dbr = wfGetDB( DB_SLAVE ); - $redirect = $this->isRedirect() ? 1 : 0; - + if ( $wgExtraRandompageSQL ) { $this->extra[] = $wgExtraRandompageSQL; } @@ -139,19 +136,34 @@ class RandomPage extends SpecialPage { $this->extra[] = $this->addExtraSQL(); } - $res = $dbr->select( - 'page', - array( 'page_title', 'page_namespace' ), - array_merge( array( + return array( + 'tables' => array( 'page' ), + 'fields' => array( 'page_title', 'page_namespace' ), + 'conds' => array_merge( array( 'page_namespace' => $this->namespaces, 'page_is_redirect' => $redirect, 'page_random >= ' . $randstr ), $this->extra ), - __METHOD__, - array( + 'options' => array( 'ORDER BY' => 'page_random', - 'USE INDEX' => 'page_random' - ) + 'USE INDEX' => 'page_random', + 'LIMIT' => 1, + ), + 'join_conds' => array() + ); + } + + private function selectRandomPageFromDB( $randstr, $fname = __METHOD__ ) { + $dbr = wfGetDB( DB_SLAVE ); + + $query = $this->getQueryInfo( $randstr ); + $res = $dbr->select( + $query['tables'], + $query['fields'], + $query['conds'], + $fname, + $query['options'], + $query['join_conds'] ); return $dbr->fetchObject( $res ); -- 2.20.1