4 * @subpackage SpecialPage
10 function wfSpecialRandompage() {
11 global $wgOut, $wgTitle, $wgArticle, $wgExtraRandompageSQL;
12 $fname = 'wfSpecialRandompage';
14 # NOTE! We use a literal constant in the SQL instead of the RAND()
15 # function because RAND() will return a different value for every row
16 # in the table. That's both very slow and returns results heavily
17 # biased towards low values, as rows later in the table will likely
18 # never be reached for comparison.
20 # Using a literal constant means the whole thing gets optimized on
21 # the index, and the comparison is both fast and fair.
23 # interpolation and sprintf() can muck up with locale-specific decimal separator
24 $randstr = wfRandom();
26 $db =& wfGetDB( DB_SLAVE
);
27 $use_index = $db->useIndexClause( 'page_random' );
28 $page = $db->tableName( 'page' );
30 if ( $wgExtraRandompageSQL ) {
31 $extra = "AND ($wgExtraRandompageSQL)";
35 $sqlget = "SELECT page_id,page_title
37 WHERE page_namespace=0 AND page_is_redirect=0 $extra
38 AND page_random>$randstr
41 $res = $db->query( $sqlget, $fname );
44 if( $s = $db->fetchObject( $res ) ) {
45 $title =& Title
::makeTitle( NS_MAIN
, $s->page_title
);
47 if( is_null( $title ) ) {
48 # That's not supposed to happen :)
49 $title =& Title
::newFromText( wfMsg( 'mainpage' ) );
51 $wgOut->reportTime(); # for logfile
52 $wgOut->redirect( $title->getFullUrl() );