Index: trunk/phase3/includes/GlobalFunctions.php |
— | — | @@ -129,14 +129,13 @@ |
130 | 130 | * not likely to give duplicate values for any realistic |
131 | 131 | * number of articles. |
132 | 132 | * |
133 | | - * @param float $limit Upper limit of returned values, default 1. |
134 | 133 | * @return string |
135 | 134 | */ |
136 | | -function wfRandom( $limit = 1 ) { |
| 135 | +function wfRandom() { |
137 | 136 | # The maximum random value is "only" 2^31-1, so get two random |
138 | 137 | # values to reduce the chance of dupes |
139 | 138 | $max = mt_getrandmax() + 1; |
140 | | - $rand = number_format( (mt_rand() * $max + mt_rand()) * $limit |
| 139 | + $rand = number_format( (mt_rand() * $max + mt_rand()) |
141 | 140 | / $max / $max, 12, '.', '' ); |
142 | 141 | return $rand; |
143 | 142 | } |
Index: trunk/phase3/includes/SpecialRandompage.php |
— | — | @@ -57,11 +57,15 @@ |
58 | 58 | $randstr = wfRandom(); |
59 | 59 | $row = $this->selectRandomPageFromDB( $randstr ); |
60 | 60 | |
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" ); |
66 | 70 | |
67 | 71 | if( $row ) |
68 | 72 | return Title::makeTitleSafe( $this->namespace, $row->page_title ); |
— | — | @@ -75,48 +79,25 @@ |
76 | 80 | |
77 | 81 | $dbr = wfGetDB( DB_SLAVE ); |
78 | 82 | |
79 | | - $from = $this->getSQLFrom( $dbr ); |
80 | | - $where = $this->getSQLWhere( $dbr ); |
| 83 | + $use_index = $dbr->useIndexClause( 'page_random' ); |
| 84 | + $page = $dbr->tableName( 'page' ); |
81 | 85 | |
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 |
84 | 96 | ORDER BY page_random"; |
85 | 97 | |
86 | 98 | $sql = $dbr->limitResult( $sql, 1, 0 ); |
87 | 99 | $res = $dbr->query( $sql, $fname ); |
88 | 100 | return $dbr->fetchObject( $res ); |
89 | 101 | } |
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 | | - } |
121 | 102 | } |
122 | 103 | |
123 | 104 | ?> |