* New message: talkpagelinktext
[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 * @addtogroup Special pages
7 * @author Rob Church <robchur@gmail.com>
8 * @license GNU General Public Licence 2.0 or later
9 */
10
11 /**
12 * Main execution point
13 * @param $par Namespace to select the redirect from
14 */
15 function wfSpecialRandomredirect( $par = NULL ) {
16 global $wgOut, $wgExtraRandompageSQL, $wgContLang;
17 $fname = 'wfSpecialRandomredirect';
18
19 # Validate the namespace
20 $namespace = $wgContLang->getNsIndex( $par );
21 if( $namespace === false || $namespace < NS_MAIN )
22 $namespace = NS_MAIN;
23
24 # Same logic as RandomPage
25 $randstr = wfRandom();
26
27 $dbr = wfGetDB( DB_SLAVE );
28 $use_index = $dbr->useIndexClause( 'page_random' );
29 $page = $dbr->tableName( 'page' );
30
31 $extra = $wgExtraRandompageSQL ? "AND ($wgExtraRandompageSQL)" : '';
32 $sql = "SELECT page_id,page_title
33 FROM $page $use_index
34 WHERE page_namespace = $namespace AND page_is_redirect = 1 $extra
35 AND page_random > $randstr
36 ORDER BY page_random";
37
38 $sql = $dbr->limitResult( $sql, 1, 0 );
39 $res = $dbr->query( $sql, $fname );
40
41 $title = NULL;
42 if( $row = $dbr->fetchObject( $res ) )
43 $title = Title::makeTitleSafe( $namespace, $row->page_title );
44
45 # Catch dud titles and return to the main page
46 if( is_null( $title ) )
47 $title = Title::newMainPage();
48
49 $wgOut->reportTime();
50 $wgOut->redirect( $title->getFullUrl( 'redirect=no' ) );
51 }
52
53 ?>