3 * @addtogroup SpecialPage
9 * @param $par The namespace to get a random page from (default NS_MAIN),
10 * used as e.g. Special:Randompage/Category
12 function wfSpecialRandompage( $par = NS_MAIN
) {
13 global $wgOut, $wgExtraRandompageSQL;
14 $fname = 'wfSpecialRandompage';
17 $t = Title
::newFromText ( $par . ":Dummy" ) ;
18 $namespace = $t->getNamespace () ;
20 # NOTE! We use a literal constant in the SQL instead of the RAND()
21 # function because RAND() will return a different value for every row
22 # in the table. That's both very slow and returns results heavily
23 # biased towards low values, as rows later in the table will likely
24 # never be reached for comparison.
26 # Using a literal constant means the whole thing gets optimized on
27 # the index, and the comparison is both fast and fair.
29 # interpolation and sprintf() can muck up with locale-specific decimal separator
30 $randstr = wfRandom();
32 $db = wfGetDB( DB_SLAVE
);
33 $use_index = $db->useIndexClause( 'page_random' );
34 $page = $db->tableName( 'page' );
36 $extra = $wgExtraRandompageSQL ?
"AND ($wgExtraRandompageSQL)" : '';
37 $sql = "SELECT page_id,page_title
39 WHERE page_namespace=$namespace AND page_is_redirect=0 $extra
40 AND page_random>$randstr
41 ORDER BY page_random";
42 $sql = $db->limitResult($sql, 1, 0);
43 $res = $db->query( $sql, $fname );
46 if( $s = $db->fetchObject( $res ) ) {
47 $title =& Title
::makeTitle( $namespace, $s->page_title
);
49 if( is_null( $title ) ) {
50 # That's not supposed to happen :)
51 $title = Title
::newMainPage();
53 $wgOut->reportTime(); # for logfile
54 $wgOut->redirect( $title->getFullUrl() );