* (bug 8437) Make Title::loadRestrictions() initialise $mRestrictions properly
[lhc/web/wiklou.git] / includes / SpecialRandomredirect.php
1 <?php
2
3 /**
4 * Special page to direct the user to a random redirect page (minus the second redirect)
5 *
6 * @package MediaWiki
7 * @subpackage Special pages
8 * @author Rob Church <robchur@gmail.com>
9 * @licence GNU General Public Licence 2.0 or later
10 */
11
12 /**
13 * Main execution point
14 * @param $par Namespace to select the redirect from
15 */
16 function wfSpecialRandomredirect( $par = NULL ) {
17 global $wgOut, $wgExtraRandompageSQL, $wgContLang;
18 $fname = 'wfSpecialRandomredirect';
19
20 # Validate the namespace
21 $namespace = $wgContLang->getNsIndex( $par );
22 if( $namespace === false || $namespace < NS_MAIN )
23 $namespace = NS_MAIN;
24
25 # Same logic as RandomPage
26 $randstr = wfRandom();
27
28 $dbr =& wfGetDB( DB_SLAVE );
29 $use_index = $dbr->useIndexClause( 'page_random' );
30 $page = $dbr->tableName( 'page' );
31
32 $extra = $wgExtraRandompageSQL ? "AND ($wgExtraRandompageSQL)" : '';
33 $sql = "SELECT page_id,page_title
34 FROM $page $use_index
35 WHERE page_namespace = $namespace AND page_is_redirect = 1 $extra
36 AND page_random > $randstr
37 ORDER BY page_random";
38
39 $sql = $dbr->limitResult( $sql, 1, 0 );
40 $res = $dbr->query( $sql, $fname );
41
42 $title = NULL;
43 if( $row = $dbr->fetchObject( $res ) )
44 $title = Title::makeTitleSafe( $namespace, $row->page_title );
45
46 # Catch dud titles and return to the main page
47 if( is_null( $title ) )
48 $title = Title::newMainPage();
49
50 $wgOut->reportTime();
51 $wgOut->redirect( $title->getFullUrl( 'redirect=no' ) );
52 }
53
54 ?>