Use Doxygen @addtogroup instead of phpdoc @package && @subpackage
[lhc/web/wiklou.git] / includes / SpecialRandompage.php
1 <?php
2 /**
3 * @addtogroup SpecialPage
4 */
5
6 /**
7 * Constructor
8 *
9 * @param $par The namespace to get a random page from (default NS_MAIN),
10 * used as e.g. Special:Randompage/Category
11 */
12 function wfSpecialRandompage( $par = NS_MAIN ) {
13 global $wgOut, $wgExtraRandompageSQL;
14 $fname = 'wfSpecialRandompage';
15
16 # Determine namespace
17 $t = Title::newFromText ( $par . ":Dummy" ) ;
18 $namespace = $t->getNamespace () ;
19
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.
25 #
26 # Using a literal constant means the whole thing gets optimized on
27 # the index, and the comparison is both fast and fair.
28
29 # interpolation and sprintf() can muck up with locale-specific decimal separator
30 $randstr = wfRandom();
31
32 $db =& wfGetDB( DB_SLAVE );
33 $use_index = $db->useIndexClause( 'page_random' );
34 $page = $db->tableName( 'page' );
35
36 $extra = $wgExtraRandompageSQL ? "AND ($wgExtraRandompageSQL)" : '';
37 $sql = "SELECT page_id,page_title
38 FROM $page $use_index
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 );
44
45 $title = null;
46 if( $s = $db->fetchObject( $res ) ) {
47 $title =& Title::makeTitle( $namespace, $s->page_title );
48 }
49 if( is_null( $title ) ) {
50 # That's not supposed to happen :)
51 $title = Title::newMainPage();
52 }
53 $wgOut->reportTime(); # for logfile
54 $wgOut->redirect( $title->getFullUrl() );
55 }
56
57 ?>