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