2004-02-18 02:15:00 +00:00
|
|
|
<?php
|
2004-09-02 23:28:24 +00:00
|
|
|
/**
|
2004-09-03 23:00:01 +00:00
|
|
|
* @package MediaWiki
|
|
|
|
|
* @subpackage SpecialPage
|
2004-09-02 23:28:24 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Constructor
|
2005-04-16 23:51:42 +00:00
|
|
|
*
|
2005-08-02 13:35:19 +00:00
|
|
|
* @param string $par the namespace to get a random page from (default NS_MAIN),
|
2005-04-16 23:51:42 +00:00
|
|
|
* used as e.g. Special:Randompage/Category
|
2004-09-02 23:28:24 +00:00
|
|
|
*/
|
2005-04-16 23:51:42 +00:00
|
|
|
function wfSpecialRandompage( $par = NS_MAIN ) {
|
2005-12-04 18:27:59 +00:00
|
|
|
global $wgOut, $wgExtraRandompageSQL, $wgContLang;
|
2004-09-02 23:28:24 +00:00
|
|
|
$fname = 'wfSpecialRandompage';
|
2003-04-14 23:10:40 +00:00
|
|
|
|
2005-04-16 23:51:42 +00:00
|
|
|
# Determine the namespace to get a random page from.
|
|
|
|
|
$namespace = $wgContLang->getNsIndex($par);
|
2005-04-30 01:19:09 +00:00
|
|
|
if ($namespace === false || $namespace < NS_MAIN) {
|
2005-04-16 23:51:42 +00:00
|
|
|
$namespace = NS_MAIN;
|
|
|
|
|
}
|
2005-08-02 13:35:19 +00:00
|
|
|
|
2004-10-02 02:27:30 +00:00
|
|
|
# NOTE! We use a literal constant in the SQL instead of the RAND()
|
|
|
|
|
# function because RAND() will return a different value for every row
|
|
|
|
|
# in the table. That's both very slow and returns results heavily
|
|
|
|
|
# biased towards low values, as rows later in the table will likely
|
|
|
|
|
# never be reached for comparison.
|
|
|
|
|
#
|
|
|
|
|
# Using a literal constant means the whole thing gets optimized on
|
|
|
|
|
# the index, and the comparison is both fast and fair.
|
2005-08-02 13:35:19 +00:00
|
|
|
|
2003-06-06 18:38:14 +00:00
|
|
|
# interpolation and sprintf() can muck up with locale-specific decimal separator
|
2004-10-11 17:34:39 +00:00
|
|
|
$randstr = wfRandom();
|
2005-08-02 13:35:19 +00:00
|
|
|
|
2004-07-18 08:48:43 +00:00
|
|
|
$db =& wfGetDB( DB_SLAVE );
|
2004-12-19 08:00:50 +00:00
|
|
|
$use_index = $db->useIndexClause( 'page_random' );
|
|
|
|
|
$page = $db->tableName( 'page' );
|
2004-07-18 08:48:43 +00:00
|
|
|
|
2005-04-29 19:15:50 +00:00
|
|
|
$extra = $wgExtraRandompageSQL ? "AND ($wgExtraRandompageSQL)" : '';
|
|
|
|
|
$sql = "SELECT page_id,page_title
|
2004-12-19 08:00:50 +00:00
|
|
|
FROM $page $use_index
|
2005-04-29 19:15:50 +00:00
|
|
|
WHERE page_namespace=$namespace AND page_is_redirect=0 $extra
|
2004-12-19 08:00:50 +00:00
|
|
|
AND page_random>$randstr
|
2005-08-02 13:35:19 +00:00
|
|
|
ORDER BY page_random";
|
|
|
|
|
$sql = $db->limitResult($sql, 1, 0);
|
2005-04-29 19:15:50 +00:00
|
|
|
$res = $db->query( $sql, $fname );
|
2005-08-02 13:35:19 +00:00
|
|
|
|
2004-10-02 02:27:30 +00:00
|
|
|
$title = null;
|
2004-07-10 03:09:26 +00:00
|
|
|
if( $s = $db->fetchObject( $res ) ) {
|
2005-04-16 23:51:42 +00:00
|
|
|
$title =& Title::makeTitle( $namespace, $s->page_title );
|
2005-08-02 13:35:19 +00:00
|
|
|
}
|
2004-10-02 02:27:30 +00:00
|
|
|
if( is_null( $title ) ) {
|
|
|
|
|
# That's not supposed to happen :)
|
2005-08-01 23:58:51 +00:00
|
|
|
$title = Title::newFromText( wfMsg( 'mainpage' ) );
|
2003-04-14 23:10:40 +00:00
|
|
|
}
|
|
|
|
|
$wgOut->reportTime(); # for logfile
|
2004-10-02 02:27:30 +00:00
|
|
|
$wgOut->redirect( $title->getFullUrl() );
|
2003-04-14 23:10:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
?>
|