2004-02-18 02:15:00 +00:00
|
|
|
<?php
|
2007-04-19 10:19:27 +00:00
|
|
|
|
2004-09-02 23:28:24 +00:00
|
|
|
/**
|
2007-04-19 10:19:27 +00:00
|
|
|
* Special page to direct the user to a random page
|
|
|
|
|
*
|
WARNING: HUGE COMMIT
Doxygen documentation update:
* Changed alls @addtogroup to @ingroup. @addtogroup adds the comment to the group description, but doesn't add the file, class, function, ... to the group like @ingroup does. See for example http://svn.wikimedia.org/doc/group__SpecialPage.html where it's impossible to see related files, classes, ... that should belong to that group.
* Added @file to file description, it seems that it should be explicitely decalred for file descriptions, otherwise doxygen will think that the comment document the first class, variabled, function, ... that is in that file.
* Removed some empty comments
* Removed some ?>
Added following groups:
* ExternalStorage
* JobQueue
* MaintenanceLanguage
One more thing: there are still a lot of warnings when generating the doc.
2008-05-20 17:13:28 +00:00
|
|
|
* @ingroup SpecialPage
|
2007-04-19 10:19:27 +00:00
|
|
|
* @author Rob Church <robchur@gmail.com>, Ilmari Karonen
|
|
|
|
|
* @license GNU General Public Licence 2.0 or later
|
2004-09-02 23:28:24 +00:00
|
|
|
*/
|
2008-01-14 10:23:48 +00:00
|
|
|
class RandomPage extends SpecialPage {
|
2009-01-06 23:25:03 +00:00
|
|
|
private $namespaces; // namespaces to select pages from
|
2007-04-19 10:19:27 +00:00
|
|
|
|
2009-08-16 06:23:07 +00:00
|
|
|
public function __construct( $name = 'Randompage' ){
|
2009-01-06 23:25:03 +00:00
|
|
|
global $wgContentNamespaces;
|
|
|
|
|
$this->namespaces = $wgContentNamespaces;
|
2008-04-14 07:45:50 +00:00
|
|
|
parent::__construct( $name );
|
2008-01-14 10:23:48 +00:00
|
|
|
}
|
2008-01-14 12:50:20 +00:00
|
|
|
|
2009-01-06 23:25:03 +00:00
|
|
|
public function getNamespaces() {
|
|
|
|
|
return $this->namespaces;
|
2008-01-14 10:23:48 +00:00
|
|
|
}
|
2008-04-14 07:45:50 +00:00
|
|
|
|
2007-04-19 10:19:27 +00:00
|
|
|
public function setNamespace ( $ns ) {
|
2009-03-05 13:38:17 +00:00
|
|
|
if( !$ns || $ns < NS_MAIN ) $ns = NS_MAIN;
|
2009-01-06 23:25:03 +00:00
|
|
|
$this->namespaces = array( $ns );
|
2007-04-19 10:19:27 +00:00
|
|
|
}
|
2008-04-14 07:45:50 +00:00
|
|
|
|
2008-01-14 12:50:20 +00:00
|
|
|
// select redirects instead of normal pages?
|
|
|
|
|
// Overriden by SpecialRandomredirect
|
|
|
|
|
public function isRedirect(){
|
|
|
|
|
return false;
|
2007-04-19 10:19:27 +00:00
|
|
|
}
|
2008-04-14 07:45:50 +00:00
|
|
|
|
2008-01-14 12:50:20 +00:00
|
|
|
public function execute( $par ) {
|
2008-01-14 10:23:48 +00:00
|
|
|
global $wgOut, $wgContLang;
|
|
|
|
|
|
2009-04-27 07:20:23 +00:00
|
|
|
if ($par) {
|
2008-01-14 10:23:48 +00:00
|
|
|
$this->setNamespace( $wgContLang->getNsIndex( $par ) );
|
2009-04-27 07:20:23 +00:00
|
|
|
}
|
2008-01-14 10:23:48 +00:00
|
|
|
|
|
|
|
|
$title = $this->getRandomTitle();
|
|
|
|
|
|
|
|
|
|
if( is_null( $title ) ) {
|
2008-01-14 12:50:20 +00:00
|
|
|
$this->setHeaders();
|
2009-08-16 06:23:07 +00:00
|
|
|
$wgOut->addWikiMsg( strtolower( $this->mName ) . '-nopages',
|
|
|
|
|
$this->getNsList(), count( $this->namespaces ) );
|
2008-01-14 10:23:48 +00:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2008-01-14 12:50:20 +00:00
|
|
|
$query = $this->isRedirect() ? 'redirect=no' : '';
|
|
|
|
|
$wgOut->redirect( $title->getFullUrl( $query ) );
|
2008-01-14 10:23:48 +00:00
|
|
|
}
|
|
|
|
|
|
2009-08-16 06:23:07 +00:00
|
|
|
/**
|
|
|
|
|
* Get a comma-delimited list of namespaces we don't have
|
|
|
|
|
* any pages in
|
|
|
|
|
* @return String
|
|
|
|
|
*/
|
|
|
|
|
private function getNsList() {
|
|
|
|
|
global $wgContLang;
|
|
|
|
|
$nsNames = array();
|
|
|
|
|
foreach( $this->namespaces as $n ) {
|
|
|
|
|
if( $n === NS_MAIN )
|
|
|
|
|
$nsNames[] = wfMsgForContent( 'blanknamespace' );
|
|
|
|
|
else
|
|
|
|
|
$nsNames[] = $wgContLang->getNsText( $n );
|
|
|
|
|
}
|
|
|
|
|
return $wgContLang->commaList( $nsNames );
|
|
|
|
|
}
|
|
|
|
|
|
2007-04-19 10:19:27 +00:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Choose a random title.
|
|
|
|
|
* @return Title object (or null if nothing to choose from)
|
|
|
|
|
*/
|
2008-01-14 12:50:20 +00:00
|
|
|
public function getRandomTitle() {
|
2007-04-19 10:19:27 +00:00
|
|
|
$randstr = wfRandom();
|
|
|
|
|
$row = $this->selectRandomPageFromDB( $randstr );
|
|
|
|
|
|
2007-04-19 10:54:48 +00:00
|
|
|
/* If we picked a value that was higher than any in
|
|
|
|
|
* the DB, wrap around and select the page with the
|
|
|
|
|
* lowest value instead! One might think this would
|
|
|
|
|
* skew the distribution, but in fact it won't cause
|
|
|
|
|
* any more bias than what the page_random scheme
|
|
|
|
|
* causes anyway. Trust me, I'm a mathematician. :)
|
|
|
|
|
*/
|
|
|
|
|
if( !$row )
|
|
|
|
|
$row = $this->selectRandomPageFromDB( "0" );
|
2007-04-19 10:19:27 +00:00
|
|
|
|
|
|
|
|
if( $row )
|
2009-01-06 23:25:03 +00:00
|
|
|
return Title::makeTitleSafe( $row->page_namespace, $row->page_title );
|
2007-04-19 10:19:27 +00:00
|
|
|
else
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
2008-01-14 12:50:20 +00:00
|
|
|
private function selectRandomPageFromDB( $randstr ) {
|
2007-04-19 10:19:27 +00:00
|
|
|
global $wgExtraRandompageSQL;
|
|
|
|
|
$dbr = wfGetDB( DB_SLAVE );
|
|
|
|
|
|
2007-04-19 10:54:48 +00:00
|
|
|
$use_index = $dbr->useIndexClause( 'page_random' );
|
|
|
|
|
$page = $dbr->tableName( 'page' );
|
|
|
|
|
|
2009-01-06 23:25:03 +00:00
|
|
|
$ns = implode( ",", $this->namespaces );
|
2008-01-14 12:50:20 +00:00
|
|
|
$redirect = $this->isRedirect() ? 1 : 0;
|
2007-04-19 10:19:27 +00:00
|
|
|
|
2007-04-19 10:54:48 +00:00
|
|
|
$extra = $wgExtraRandompageSQL ? "AND ($wgExtraRandompageSQL)" : "";
|
2009-07-13 20:27:34 +00:00
|
|
|
$extra .= $this->addExtraSQL() ? "AND (".$this->addExtraSQL().")" : "";
|
2009-08-10 20:44:31 +00:00
|
|
|
$sql = "SELECT page_title, page_namespace
|
|
|
|
|
FROM $page $use_index
|
|
|
|
|
WHERE page_namespace IN ( $ns )
|
|
|
|
|
AND page_is_redirect = $redirect
|
|
|
|
|
AND page_random >= $randstr
|
|
|
|
|
$extra
|
|
|
|
|
ORDER BY page_random";
|
|
|
|
|
|
|
|
|
|
$sql = $dbr->limitResult( $sql, 1, 0 );
|
2009-04-27 07:20:23 +00:00
|
|
|
$res = $dbr->query( $sql, __METHOD__ );
|
2007-04-19 10:19:27 +00:00
|
|
|
return $dbr->fetchObject( $res );
|
|
|
|
|
}
|
2009-06-06 16:43:08 +00:00
|
|
|
|
2009-08-10 20:44:31 +00:00
|
|
|
// an alternative to $wgExtraRandompageSQL so extensions
|
2009-06-06 16:43:08 +00:00
|
|
|
// can add their own SQL by overriding this function
|
|
|
|
|
public function addExtraSQL() {
|
|
|
|
|
return '';
|
|
|
|
|
}
|
2007-04-19 10:19:27 +00:00
|
|
|
}
|