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, $wgRequest;
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 $redirectParam = $this->isRedirect() ?
array( 'redirect' => 'no' ) : array();
52 $query = array_merge( $wgRequest->getValues(), $redirectParam );
53 unset( $query['title'] );
54 $wgOut->redirect( $title->getFullUrl( $query ) );
58 * Get a comma-delimited list of namespaces we don't have
62 private function getNsList() {
65 foreach( $this->namespaces
as $n ) {
67 $nsNames[] = wfMsgForContent( 'blanknamespace' );
69 $nsNames[] = $wgContLang->getNsText( $n );
71 return $wgContLang->commaList( $nsNames );
76 * Choose a random title.
77 * @return Title object (or null if nothing to choose from)
79 public function getRandomTitle() {
80 $randstr = wfRandom();
82 if ( !wfRunHooks( 'SpecialRandomGetRandomTitle', array( &$randstr, &$this->isRedir
, &$this->namespaces
, &$this->extra
, &$title ) ) ) {
85 $row = $this->selectRandomPageFromDB( $randstr );
87 /* If we picked a value that was higher than any in
88 * the DB, wrap around and select the page with the
89 * lowest value instead! One might think this would
90 * skew the distribution, but in fact it won't cause
91 * any more bias than what the page_random scheme
92 * causes anyway. Trust me, I'm a mathematician. :)
95 $row = $this->selectRandomPageFromDB( "0" );
98 return Title
::makeTitleSafe( $row->page_namespace
, $row->page_title
);
103 private function selectRandomPageFromDB( $randstr ) {
104 global $wgExtraRandompageSQL;
105 $dbr = wfGetDB( DB_SLAVE
);
107 $use_index = $dbr->useIndexClause( 'page_random' );
108 $page = $dbr->tableName( 'page' );
110 $ns = implode( ",", $this->namespaces
);
111 $redirect = $this->isRedirect() ?
1 : 0;
113 if ( $wgExtraRandompageSQL ) {
114 $this->extra
[] = $wgExtraRandompageSQL;
116 if ( $this->addExtraSQL() ) {
117 $this->extra
[] = $this->addExtraSQL();
120 if ( $this->extra
) {
121 $extra = 'AND (' . implode( ') AND (', $this->extra
) . ')';
123 $sql = "SELECT page_title, page_namespace
124 FROM $page $use_index
125 WHERE page_namespace IN ( $ns )
126 AND page_is_redirect = $redirect
127 AND page_random >= $randstr
129 ORDER BY page_random";
131 $sql = $dbr->limitResult( $sql, 1, 0 );
132 $res = $dbr->query( $sql, __METHOD__
);
133 return $dbr->fetchObject( $res );
136 /* an alternative to $wgExtraRandompageSQL so subclasses
137 * can add their own SQL by overriding this function
138 * @deprecated, append to $this->extra instead
140 public function addExtraSQL() {