3 * Implements Special:Randompage
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
21 * @ingroup SpecialPage
22 * @author Rob Church <robchur@gmail.com>, Ilmari Karonen
26 * Special page to direct the user to a random page
28 * @ingroup SpecialPage
30 class RandomPage
extends SpecialPage
{
31 private $namespaces; // namespaces to select pages from
32 protected $isRedir = false; // should the result be a redirect?
33 protected $extra = array(); // Extra SQL statements
35 public function __construct( $name = 'Randompage' ){
36 global $wgContentNamespaces;
37 $this->namespaces
= $wgContentNamespaces;
38 parent
::__construct( $name );
41 public function getNamespaces() {
42 return $this->namespaces
;
45 public function setNamespace ( $ns ) {
46 if( !$ns ||
$ns < NS_MAIN
) $ns = NS_MAIN
;
47 $this->namespaces
= array( $ns );
50 // select redirects instead of normal pages?
51 public function isRedirect(){
52 return $this->isRedir
;
55 public function execute( $par ) {
56 global $wgOut, $wgContLang, $wgRequest;
59 $this->setNamespace( $wgContLang->getNsIndex( $par ) );
62 $title = $this->getRandomTitle();
64 if( is_null( $title ) ) {
66 $wgOut->addWikiMsg( strtolower( $this->mName
) . '-nopages',
67 $this->getNsList(), count( $this->namespaces
) );
71 $redirectParam = $this->isRedirect() ?
array( 'redirect' => 'no' ) : array();
72 $query = array_merge( $wgRequest->getValues(), $redirectParam );
73 unset( $query['title'] );
74 $wgOut->redirect( $title->getFullUrl( $query ) );
78 * Get a comma-delimited list of namespaces we don't have
82 private function getNsList() {
85 foreach( $this->namespaces
as $n ) {
87 $nsNames[] = wfMsgForContent( 'blanknamespace' );
89 $nsNames[] = $wgContLang->getNsText( $n );
91 return $wgContLang->commaList( $nsNames );
96 * Choose a random title.
97 * @return Title object (or null if nothing to choose from)
99 public function getRandomTitle() {
100 $randstr = wfRandom();
102 if ( !wfRunHooks( 'SpecialRandomGetRandomTitle', array( &$randstr, &$this->isRedir
, &$this->namespaces
, &$this->extra
, &$title ) ) ) {
105 $row = $this->selectRandomPageFromDB( $randstr );
107 /* If we picked a value that was higher than any in
108 * the DB, wrap around and select the page with the
109 * lowest value instead! One might think this would
110 * skew the distribution, but in fact it won't cause
111 * any more bias than what the page_random scheme
112 * causes anyway. Trust me, I'm a mathematician. :)
115 $row = $this->selectRandomPageFromDB( "0" );
118 return Title
::makeTitleSafe( $row->page_namespace
, $row->page_title
);
123 private function selectRandomPageFromDB( $randstr ) {
124 global $wgExtraRandompageSQL;
125 $dbr = wfGetDB( DB_SLAVE
);
127 $use_index = $dbr->useIndexClause( 'page_random' );
128 $page = $dbr->tableName( 'page' );
130 $ns = implode( ",", $this->namespaces
);
131 $redirect = $this->isRedirect() ?
1 : 0;
133 if ( $wgExtraRandompageSQL ) {
134 $this->extra
[] = $wgExtraRandompageSQL;
136 if ( $this->addExtraSQL() ) {
137 $this->extra
[] = $this->addExtraSQL();
140 if ( $this->extra
) {
141 $extra = 'AND (' . implode( ') AND (', $this->extra
) . ')';
143 $sql = "SELECT page_title, page_namespace
144 FROM $page $use_index
145 WHERE page_namespace IN ( $ns )
146 AND page_is_redirect = $redirect
147 AND page_random >= $randstr
149 ORDER BY page_random";
151 $sql = $dbr->limitResult( $sql, 1, 0 );
152 $res = $dbr->query( $sql, __METHOD__
);
153 return $dbr->fetchObject( $res );
156 /* an alternative to $wgExtraRandompageSQL so subclasses
157 * can add their own SQL by overriding this function
158 * @deprecated, append to $this->extra instead
160 public function addExtraSQL() {