4 * Special page to direct the user to a random redirect page (minus the second redirect)
7 * @subpackage Special pages
8 * @author Rob Church <robchur@gmail.com>
9 * @licence GNU General Public Licence 2.0 or later
13 * Main execution point
14 * @param $par Namespace to select the redirect from
16 function wfSpecialRandomredirect( $par = NULL ) {
17 global $wgOut, $wgExtraRandompageSQL, $wgContLang;
18 $fname = 'wfSpecialRandomredirect';
20 # Validate the namespace
21 $namespace = $wgContLang->getNsIndex( $par );
22 if( $namespace === false ||
$namespace < NS_MAIN
)
25 # Same logic as RandomPage
26 $randstr = wfRandom();
28 $dbr =& wfGetDB( DB_SLAVE
);
29 $use_index = $dbr->useIndexClause( 'page_random' );
30 $page = $dbr->tableName( 'page' );
32 $extra = $wgExtraRandompageSQL ?
"AND ($wgExtraRandompageSQL)" : '';
33 $sql = "SELECT page_id,page_title
35 WHERE page_namespace = $namespace AND page_is_redirect = 1 $extra
36 AND page_random > $randstr
37 ORDER BY page_random";
39 $sql = $dbr->limitResult( $sql, 1, 0 );
40 $res = $dbr->query( $sql, $fname );
43 if( $row = $dbr->fetchObject( $res ) )
44 $title = Title
::makeTitleSafe( $namespace, $row->page_title
);
46 # Catch dud titles and return to the main page
47 if( is_null( $title ) )
48 $title = Title
::newFromText( wfMsg( 'mainpage' ) );
51 $wgOut->redirect( $title->getFullUrl( 'redirect=no' ) );