- # NOTE! We use a literal constant in the SQL instead of the RAND()
- # function because RAND() will return a different value for every row
- # in the table. That's both very slow and returns results heavily
- # biased towards low values, as rows later in the table will likely
- # never be reached for comparison.
- #
- # Using a literal constant means the whole thing gets optimized on
- # the index, and the comparison is both fast and fair.
-
- # interpolation and sprintf() can muck up with locale-specific decimal separator
- $randstr = wfRandom();
-
- $db =& wfGetDB( DB_SLAVE );
- $use_index = $db->useIndexClause( 'page_random' );
- $page = $db->tableName( 'page' );
-
- $extra = $wgExtraRandompageSQL ? "AND ($wgExtraRandompageSQL)" : '';
- $sql = "SELECT page_id,page_title
- FROM $page $use_index
- WHERE page_namespace=$namespace AND page_is_redirect=0 $extra
- AND page_random>$randstr
- ORDER BY page_random";
- $sql = $db->limitResult($sql, 1, 0);
- $res = $db->query( $sql, $fname );
-
- $title = null;
- if( $s = $db->fetchObject( $res ) ) {
- $title =& Title::makeTitle( $namespace, $s->page_title );
+
+ /**
+ * Choose a random title.
+ * @return Title object (or null if nothing to choose from)
+ */
+ public function getRandomTitle() {
+ $randstr = wfRandom();
+ $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 );
+ else
+ return null;