don't check for isset() when variable is always initialized
[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 if ( $wgExtraRandompageSQL ) {
40 $extra = "AND ($wgExtraRandompageSQL)";
41 } else {
42 $extra = '';
43 }
44 $sqlget = "SELECT page_id,page_title
45 FROM $page $use_index
46 WHERE page_namespace=".$namespace." AND page_is_redirect=0 $extra
47 AND page_random>$randstr
48 ORDER BY page_random
49 LIMIT 1";
50 $res = $db->query( $sqlget, $fname );
51
52 $title = null;
53 if( $s = $db->fetchObject( $res ) ) {
54 $title =& Title::makeTitle( $namespace, $s->page_title );
55 }
56 if( is_null( $title ) ) {
57 # That's not supposed to happen :)
58 $title =& Title::newFromText( wfMsg( 'mainpage' ) );
59 }
60 $wgOut->reportTime(); # for logfile
61 $wgOut->redirect( $title->getFullUrl() );
62 }
63
64 ?>