4 * Special page to direct the user to a random page
7 * @author Rob Church <robchur@gmail.com>, Ilmari Karonen
8 * @license GNU General Public Licence 2.0 or later
10 class RandomPage
extends SpecialPage
{
11 private $namespaces; // namespaces to select pages from
12 protected $isRedir = false; // should the result be a redirect?
13 protected $extra = array(); // Extra SQL statements
15 public function __construct( $name = 'Randompage' ){
16 global $wgContentNamespaces;
17 $this->namespaces
= $wgContentNamespaces;
18 parent
::__construct( $name );
21 public function getNamespaces() {
22 return $this->namespaces
;
25 public function setNamespace ( $ns ) {
26 if( !$ns ||
$ns < NS_MAIN
) $ns = NS_MAIN
;
27 $this->namespaces
= array( $ns );
30 // select redirects instead of normal pages?
31 public function isRedirect(){
32 return $this->isRedir
;
35 public function execute( $par ) {
36 global $wgOut, $wgContLang;
39 $this->setNamespace( $wgContLang->getNsIndex( $par ) );
42 $title = $this->getRandomTitle();
44 if( is_null( $title ) ) {
46 $wgOut->addWikiMsg( strtolower( $this->mName
) . '-nopages',
47 $this->getNsList(), count( $this->namespaces
) );
51 $query = $this->isRedirect() ?
'redirect=no' : '';
52 $wgOut->redirect( $title->getFullUrl( $query ) );
56 * Get a comma-delimited list of namespaces we don't have
60 private function getNsList() {
63 foreach( $this->namespaces
as $n ) {
65 $nsNames[] = wfMsgForContent( 'blanknamespace' );
67 $nsNames[] = $wgContLang->getNsText( $n );
69 return $wgContLang->commaList( $nsNames );
74 * Choose a random title.
75 * @return Title object (or null if nothing to choose from)
77 public function getRandomTitle() {
78 $randstr = wfRandom();
80 if ( !wfRunHooks( 'SpecialRandomGetRandomTitle', array( &$randstr, &$this->isRedir
, &$this->namespaces
, &$this->extra
, &$title ) ) ) {
83 $row = $this->selectRandomPageFromDB( $randstr );
85 /* If we picked a value that was higher than any in
86 * the DB, wrap around and select the page with the
87 * lowest value instead! One might think this would
88 * skew the distribution, but in fact it won't cause
89 * any more bias than what the page_random scheme
90 * causes anyway. Trust me, I'm a mathematician. :)
93 $row = $this->selectRandomPageFromDB( "0" );
96 return Title
::makeTitleSafe( $row->page_namespace
, $row->page_title
);
101 private function selectRandomPageFromDB( $randstr ) {
102 global $wgExtraRandompageSQL;
103 $dbr = wfGetDB( DB_SLAVE
);
105 $use_index = $dbr->useIndexClause( 'page_random' );
106 $page = $dbr->tableName( 'page' );
108 $ns = implode( ",", $this->namespaces
);
109 $redirect = $this->isRedirect() ?
1 : 0;
111 if ( $wgExtraRandompageSQL ) {
112 $this->extra
[] = $wgExtraRandompageSQL;
114 if ( $this->addExtraSQL() ) {
115 $this->extra
[] = $this->addExtraSQL();
118 if ( $this->extra
) {
119 $extra = 'AND (' . implode( ') AND (', $this->extra
) . ')';
121 $sql = "SELECT page_title, page_namespace
122 FROM $page $use_index
123 WHERE page_namespace IN ( $ns )
124 AND page_is_redirect = $redirect
125 AND page_random >= $randstr
127 ORDER BY page_random";
129 $sql = $dbr->limitResult( $sql, 1, 0 );
130 $res = $dbr->query( $sql, __METHOD__
);
131 return $dbr->fetchObject( $res );
134 /* an alternative to $wgExtraRandompageSQL so subclasses
135 * can add their own SQL by overriding this function
136 * @deprecated, append to $this->extra instead
138 public function addExtraSQL() {