From c4db61bcd858391b19129415bcc80dd672726779 Mon Sep 17 00:00:00 2001 From: Ilmari Karonen Date: Thu, 19 Apr 2007 10:54:48 +0000 Subject: [PATCH] actually, there's no need to make it so complicated after all... :/ --- includes/GlobalFunctions.php | 5 ++- includes/SpecialRandompage.php | 61 ++++++++++++---------------------- 2 files changed, 23 insertions(+), 43 deletions(-) diff --git a/includes/GlobalFunctions.php b/includes/GlobalFunctions.php index 26cbd0db6b..291224b832 100644 --- a/includes/GlobalFunctions.php +++ b/includes/GlobalFunctions.php @@ -129,14 +129,13 @@ function wfSeedRandom() { * not likely to give duplicate values for any realistic * number of articles. * - * @param float $limit Upper limit of returned values, default 1. * @return string */ -function wfRandom( $limit = 1 ) { +function wfRandom() { # The maximum random value is "only" 2^31-1, so get two random # values to reduce the chance of dupes $max = mt_getrandmax() + 1; - $rand = number_format( (mt_rand() * $max + mt_rand()) * $limit + $rand = number_format( (mt_rand() * $max + mt_rand()) / $max / $max, 12, '.', '' ); return $rand; } diff --git a/includes/SpecialRandompage.php b/includes/SpecialRandompage.php index 051c9f94d7..bf39ca6f7a 100644 --- a/includes/SpecialRandompage.php +++ b/includes/SpecialRandompage.php @@ -57,11 +57,15 @@ class RandomPage { $randstr = wfRandom(); $row = $this->selectRandomPageFromDB( $randstr ); - if( !$row ) { - // Try again with a normalized value - $randstr = wfRandom( $this->getMaxPageRandom() ); - $row = $this->selectRandomPageFromDB( $randstr ); - } + /* If we picked a value that was higher than any in + * the DB, wrap around and select the page with the + * lowest value instead! One might think this would + * skew the distribution, but in fact it won't cause + * any more bias than what the page_random scheme + * causes anyway. Trust me, I'm a mathematician. :) + */ + if( !$row ) + $row = $this->selectRandomPageFromDB( "0" ); if( $row ) return Title::makeTitleSafe( $this->namespace, $row->page_title ); @@ -75,48 +79,25 @@ class RandomPage { $dbr = wfGetDB( DB_SLAVE ); - $from = $this->getSQLFrom( $dbr ); - $where = $this->getSQLWhere( $dbr ); + $use_index = $dbr->useIndexClause( 'page_random' ); + $page = $dbr->tableName( 'page' ); + + $ns = (int) $this->namespace; + $redirect = $this->redirect ? 1 : 0; - $sql = "SELECT page_title FROM $from - WHERE $where AND page_random > $randstr + $extra = $wgExtraRandompageSQL ? "AND ($wgExtraRandompageSQL)" : ""; + $sql = "SELECT page_title + FROM $page $use_index + WHERE page_namespace = $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, $fname ); return $dbr->fetchObject( $res ); } - - private function getMaxPageRandom () { - $fname = 'RandomPage::getMaxPageRandom'; - - $dbr = wfGetDB( DB_SLAVE ); - - $from = $this->getSQLFrom( $dbr ); - $where = $this->getSQLWhere( $dbr ); - - $sql = "SELECT MAX(page_random) AS max FROM $from WHERE $where"; - - $sql = $dbr->limitResult( $sql, 1, 0 ); - $res = $dbr->query( $sql, $fname ); - $row = $dbr->fetchObject( $res ); - - return $row ? $row->max : 0; - } - - private function getSQLFrom ( $dbr ) { - $use_index = $dbr->useIndexClause( 'page_random' ); - $page = $dbr->tableName( 'page' ); - return "$page $use_index"; - } - - private function getSQLWhere ( $dbr ) { - global $wgExtraRandompageSQL; - $ns = (int) $this->namespace; - $redirect = $this->redirect ? 1 : 0; - $extra = $wgExtraRandompageSQL ? " AND ($wgExtraRandompageSQL)" : ""; - return "page_namespace = $ns AND page_is_redirect = $redirect" . $extra; - } } ?> -- 2.20.1