From: Sam Reed Date: Mon, 31 Jan 2011 23:06:36 +0000 (+0000) Subject: Re-write raw SQL X-Git-Tag: 1.31.0-rc.0~32277 X-Git-Url: https://git.cyclocoop.org/?a=commitdiff_plain;h=6be32ee1c0ddb005d98a0061e1eb49bbeff585af;p=lhc%2Fweb%2Fwiklou.git Re-write raw SQL Wrap lines, add missing braces --- diff --git a/includes/specials/SpecialRandompage.php b/includes/specials/SpecialRandompage.php index 6299f3841a..6cd9d641d2 100644 --- a/includes/specials/SpecialRandompage.php +++ b/includes/specials/SpecialRandompage.php @@ -43,7 +43,9 @@ class RandomPage extends SpecialPage { } public function setNamespace ( $ns ) { - if( !$ns || $ns < NS_MAIN ) $ns = NS_MAIN; + if( !$ns || $ns < NS_MAIN ) { + $ns = NS_MAIN; + } $this->namespaces = array( $ns ); } @@ -83,10 +85,11 @@ class RandomPage extends SpecialPage { global $wgContLang; $nsNames = array(); foreach( $this->namespaces as $n ) { - if( $n === NS_MAIN ) + if( $n === NS_MAIN ) { $nsNames[] = wfMsgForContent( 'blanknamespace' ); - else + } else { $nsNames[] = $wgContLang->getNsText( $n ); + } } return $wgContLang->commaList( $nsNames ); } @@ -99,7 +102,8 @@ class RandomPage extends SpecialPage { public function getRandomTitle() { $randstr = wfRandom(); $title = null; - if ( !wfRunHooks( 'SpecialRandomGetRandomTitle', array( &$randstr, &$this->isRedir, &$this->namespaces, &$this->extra, &$title ) ) ) { + if ( !wfRunHooks( 'SpecialRandomGetRandomTitle', array( &$randstr, &$this->isRedir, &$this->namespaces, + &$this->extra, &$title ) ) ) { return $title; } $row = $this->selectRandomPageFromDB( $randstr ); @@ -111,23 +115,21 @@ class RandomPage extends SpecialPage { * any more bias than what the page_random scheme * causes anyway. Trust me, I'm a mathematician. :) */ - if( !$row ) + if( !$row ) { $row = $this->selectRandomPageFromDB( "0" ); + } - if( $row ) + if( $row ) { return Title::makeTitleSafe( $row->page_namespace, $row->page_title ); - else + } else { return null; + } } private function selectRandomPageFromDB( $randstr ) { global $wgExtraRandompageSQL; $dbr = wfGetDB( DB_SLAVE ); - $use_index = $dbr->useIndexClause( 'page_random' ); - $page = $dbr->tableName( 'page' ); - - $ns = implode( ",", $this->namespaces ); $redirect = $this->isRedirect() ? 1 : 0; if ( $wgExtraRandompageSQL ) { @@ -136,20 +138,22 @@ class RandomPage extends SpecialPage { if ( $this->addExtraSQL() ) { $this->extra[] = $this->addExtraSQL(); } - $extra = ''; - if ( $this->extra ) { - $extra = 'AND (' . implode( ') AND (', $this->extra ) . ')'; - } - $sql = "SELECT page_title, page_namespace - FROM $page $use_index - WHERE page_namespace IN ( $ns ) - AND page_is_redirect = $redirect - AND page_random >= $randstr - $extra - ORDER BY page_random"; - - $sql = $dbr->limitResult( $sql, 1, 0 ); - $res = $dbr->query( $sql, __METHOD__ ); + + $res = $dbr->doQuery( + 'page', + array( 'page_title', 'page_namespace' ), + array_merge( array( + 'page_namespace' => $this->namespaces, + 'page_is_redirect' => $redirect, + 'page_random >= ' . $randstr + ), $this->extra ), + __METHOD__, + array( + 'ORDER BY' => 'page_random', + 'USE INDEX' => 'page_random' + ) + ); + return $dbr->fetchObject( $res ); }