r21371 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r21370‎ | r21371 | r21372 >
Date:10:54, 19 April 2007
Author:vyznev
Status:old
Tags:
Comment:
actually, there's no need to make it so complicated after all... :/
Modified paths:
  • /trunk/phase3/includes/GlobalFunctions.php (modified) (history)
  • /trunk/phase3/includes/SpecialRandompage.php (modified) (history)

Diff [purge]

Index: trunk/phase3/includes/GlobalFunctions.php
@@ -129,14 +129,13 @@
130130 * not likely to give duplicate values for any realistic
131131 * number of articles.
132132 *
133 - * @param float $limit Upper limit of returned values, default 1.
134133 * @return string
135134 */
136 -function wfRandom( $limit = 1 ) {
 135+function wfRandom() {
137136 # The maximum random value is "only" 2^31-1, so get two random
138137 # values to reduce the chance of dupes
139138 $max = mt_getrandmax() + 1;
140 - $rand = number_format( (mt_rand() * $max + mt_rand()) * $limit
 139+ $rand = number_format( (mt_rand() * $max + mt_rand())
141140 / $max / $max, 12, '.', '' );
142141 return $rand;
143142 }
Index: trunk/phase3/includes/SpecialRandompage.php
@@ -57,11 +57,15 @@
5858 $randstr = wfRandom();
5959 $row = $this->selectRandomPageFromDB( $randstr );
6060
61 - if( !$row ) {
62 - // Try again with a normalized value
63 - $randstr = wfRandom( $this->getMaxPageRandom() );
64 - $row = $this->selectRandomPageFromDB( $randstr );
65 - }
 61+ /* If we picked a value that was higher than any in
 62+ * the DB, wrap around and select the page with the
 63+ * lowest value instead! One might think this would
 64+ * skew the distribution, but in fact it won't cause
 65+ * any more bias than what the page_random scheme
 66+ * causes anyway. Trust me, I'm a mathematician. :)
 67+ */
 68+ if( !$row )
 69+ $row = $this->selectRandomPageFromDB( "0" );
6670
6771 if( $row )
6872 return Title::makeTitleSafe( $this->namespace, $row->page_title );
@@ -75,48 +79,25 @@
7680
7781 $dbr = wfGetDB( DB_SLAVE );
7882
79 - $from = $this->getSQLFrom( $dbr );
80 - $where = $this->getSQLWhere( $dbr );
 83+ $use_index = $dbr->useIndexClause( 'page_random' );
 84+ $page = $dbr->tableName( 'page' );
8185
82 - $sql = "SELECT page_title FROM $from
83 - WHERE $where AND page_random > $randstr
 86+ $ns = (int) $this->namespace;
 87+ $redirect = $this->redirect ? 1 : 0;
 88+
 89+ $extra = $wgExtraRandompageSQL ? "AND ($wgExtraRandompageSQL)" : "";
 90+ $sql = "SELECT page_title
 91+ FROM $page $use_index
 92+ WHERE page_namespace = $ns
 93+ AND page_is_redirect = $redirect
 94+ AND page_random >= $randstr
 95+ $extra
8496 ORDER BY page_random";
8597
8698 $sql = $dbr->limitResult( $sql, 1, 0 );
8799 $res = $dbr->query( $sql, $fname );
88100 return $dbr->fetchObject( $res );
89101 }
90 -
91 - private function getMaxPageRandom () {
92 - $fname = 'RandomPage::getMaxPageRandom';
93 -
94 - $dbr = wfGetDB( DB_SLAVE );
95 -
96 - $from = $this->getSQLFrom( $dbr );
97 - $where = $this->getSQLWhere( $dbr );
98 -
99 - $sql = "SELECT MAX(page_random) AS max FROM $from WHERE $where";
100 -
101 - $sql = $dbr->limitResult( $sql, 1, 0 );
102 - $res = $dbr->query( $sql, $fname );
103 - $row = $dbr->fetchObject( $res );
104 -
105 - return $row ? $row->max : 0;
106 - }
107 -
108 - private function getSQLFrom ( $dbr ) {
109 - $use_index = $dbr->useIndexClause( 'page_random' );
110 - $page = $dbr->tableName( 'page' );
111 - return "$page $use_index";
112 - }
113 -
114 - private function getSQLWhere ( $dbr ) {
115 - global $wgExtraRandompageSQL;
116 - $ns = (int) $this->namespace;
117 - $redirect = $this->redirect ? 1 : 0;
118 - $extra = $wgExtraRandompageSQL ? " AND ($wgExtraRandompageSQL)" : "";
119 - return "page_namespace = $ns AND page_is_redirect = $redirect" . $extra;
120 - }
121102 }
122103
123104 ?>