summaryrefslogtreecommitdiff
path: root/includes/SpecialRandomredirect.php
blob: 2cb2498b1eba0f899179783f3ac3cccbf06048a5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
<?php

/**
 * Special page to direct the user to a random redirect page (minus the second redirect)
 *
 * @package MediaWiki
 * @subpackage Special pages
 * @author Rob Church <robchur@gmail.com>
 * @licence GNU General Public Licence 2.0 or later
 */

/**
 * Main execution point
 * @param $par Namespace to select the redirect from
 */
function wfSpecialRandomredirect( $par = NULL ) {
	global $wgOut, $wgExtraRandompageSQL, $wgContLang;
	$fname = 'wfSpecialRandomredirect';

	# Validate the namespace
	$namespace = $wgContLang->getNsIndex( $par );
	if( $namespace === false || $namespace < NS_MAIN )
		$namespace = NS_MAIN;

	# Same logic as RandomPage
	$randstr = wfRandom();

	$dbr =& wfGetDB( DB_SLAVE );
	$use_index = $dbr->useIndexClause( 'page_random' );
	$page = $dbr->tableName( 'page' );

	$extra = $wgExtraRandompageSQL ? "AND ($wgExtraRandompageSQL)" : '';
	$sql = "SELECT page_id,page_title
		FROM $page $use_index
		WHERE page_namespace = $namespace AND page_is_redirect = 1 $extra
		AND page_random > $randstr
		ORDER BY page_random";
		
	$sql = $dbr->limitResult( $sql, 1, 0 );
	$res = $dbr->query( $sql, $fname );

	$title = NULL;
	if( $row = $dbr->fetchObject( $res ) )
		$title = Title::makeTitleSafe( $namespace, $row->page_title );

	# Catch dud titles and return to the main page
	if( is_null( $title ) )
		$title = Title::newMainPage();
		
	$wgOut->reportTime();
	$wgOut->redirect( $title->getFullUrl( 'redirect=no' ) );
}

?>