fixed broken sidebar
[lhc/web/wiklou.git] / includes / SpecialRandompage.php
1 <?php
2 /**
3 * @package MediaWiki
4 * @subpackage SpecialPage
5 */
6
7 /**
8 * Constructor
9 *
10 * @param string $par the namespace to get a random page from (default NS_MAIN),
11 * used as e.g. Special:Randompage/Category
12 */
13 function wfSpecialRandompage( $par = NS_MAIN ) {
14 global $wgOut, $wgTitle, $wgArticle, $wgExtraRandompageSQL, $wgContLang;
15 $fname = 'wfSpecialRandompage';
16
17 # Determine the namespace to get a random page from.
18 $namespace = $wgContLang->getNsIndex($par);
19 if ($namespace === false || $namespace < NS_MAIN) {
20 $namespace = NS_MAIN;
21 }
22
23 # NOTE! We use a literal constant in the SQL instead of the RAND()
24 # function because RAND() will return a different value for every row
25 # in the table. That's both very slow and returns results heavily
26 # biased towards low values, as rows later in the table will likely
27 # never be reached for comparison.
28 #
29 # Using a literal constant means the whole thing gets optimized on
30 # the index, and the comparison is both fast and fair.
31
32 # interpolation and sprintf() can muck up with locale-specific decimal separator
33 $randstr = wfRandom();
34
35 $db =& wfGetDB( DB_SLAVE );
36 $use_index = $db->useIndexClause( 'page_random' );
37 $page = $db->tableName( 'page' );
38
39 $extra = $wgExtraRandompageSQL ? "AND ($wgExtraRandompageSQL)" : '';
40 $sql = "SELECT page_id,page_title
41 FROM $page $use_index
42 WHERE page_namespace=$namespace AND page_is_redirect=0 $extra
43 AND page_random>$randstr
44 ORDER BY page_random
45 LIMIT 1";
46 $res = $db->query( $sql, $fname );
47
48 $title = null;
49 if( $s = $db->fetchObject( $res ) ) {
50 $title =& Title::makeTitle( $namespace, $s->page_title );
51 }
52 if( is_null( $title ) ) {
53 # That's not supposed to happen :)
54 $title =& Title::newFromText( wfMsg( 'mainpage' ) );
55 }
56 $wgOut->reportTime(); # for logfile
57 $wgOut->redirect( $title->getFullUrl() );
58 }
59
60 ?>