Merge "Whitelist the <wbr> element."
[mediawiki.git] / includes / specials / SpecialRandompage.php
blobe4f31f4cad431f4b3b39845129905ba6adf8dcef
1 <?php
2 /**
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
20 * @file
21 * @ingroup SpecialPage
22 * @author Rob Church <robchur@gmail.com>, Ilmari Karonen
25 /**
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 $this->namespaces = MWNamespace::getContentNamespaces();
37 parent::__construct( $name );
40 public function getNamespaces() {
41 return $this->namespaces;
44 public function setNamespace( $ns ) {
45 if ( !$ns || $ns < NS_MAIN ) {
46 $ns = NS_MAIN;
48 $this->namespaces = array( $ns );
51 // select redirects instead of normal pages?
52 public function isRedirect() {
53 return $this->isRedir;
56 public function execute( $par ) {
57 global $wgContLang;
59 if ( $par ) {
60 $this->setNamespace( $wgContLang->getNsIndex( $par ) );
63 $title = $this->getRandomTitle();
65 if ( is_null( $title ) ) {
66 $this->setHeaders();
67 $this->getOutput()->addWikiMsg( strtolower( $this->getName() ) . '-nopages',
68 $this->getNsList(), count( $this->namespaces ) );
70 return;
73 $redirectParam = $this->isRedirect() ? array( 'redirect' => 'no' ) : array();
74 $query = array_merge( $this->getRequest()->getValues(), $redirectParam );
75 unset( $query['title'] );
76 $this->getOutput()->redirect( $title->getFullURL( $query ) );
79 /**
80 * Get a comma-delimited list of namespaces we don't have
81 * any pages in
82 * @return String
84 private function getNsList() {
85 global $wgContLang;
86 $nsNames = array();
87 foreach ( $this->namespaces as $n ) {
88 if ( $n === NS_MAIN ) {
89 $nsNames[] = $this->msg( 'blanknamespace' )->plain();
90 } else {
91 $nsNames[] = $wgContLang->getNsText( $n );
95 return $wgContLang->commaList( $nsNames );
98 /**
99 * Choose a random title.
100 * @return Title object (or null if nothing to choose from)
102 public function getRandomTitle() {
103 $randstr = wfRandom();
104 $title = null;
106 if ( !wfRunHooks(
107 'SpecialRandomGetRandomTitle',
108 array( &$randstr, &$this->isRedir, &$this->namespaces, &$this->extra, &$title )
109 ) ) {
110 return $title;
113 $row = $this->selectRandomPageFromDB( $randstr );
115 /* If we picked a value that was higher than any in
116 * the DB, wrap around and select the page with the
117 * lowest value instead! One might think this would
118 * skew the distribution, but in fact it won't cause
119 * any more bias than what the page_random scheme
120 * causes anyway. Trust me, I'm a mathematician. :)
122 if ( !$row ) {
123 $row = $this->selectRandomPageFromDB( "0" );
126 if ( $row ) {
127 return Title::makeTitleSafe( $row->page_namespace, $row->page_title );
130 return null;
133 protected function getQueryInfo( $randstr ) {
134 $redirect = $this->isRedirect() ? 1 : 0;
136 return array(
137 'tables' => array( 'page' ),
138 'fields' => array( 'page_title', 'page_namespace' ),
139 'conds' => array_merge( array(
140 'page_namespace' => $this->namespaces,
141 'page_is_redirect' => $redirect,
142 'page_random >= ' . $randstr
143 ), $this->extra ),
144 'options' => array(
145 'ORDER BY' => 'page_random',
146 'USE INDEX' => 'page_random',
147 'LIMIT' => 1,
149 'join_conds' => array()
153 private function selectRandomPageFromDB( $randstr, $fname = __METHOD__ ) {
154 $dbr = wfGetDB( DB_SLAVE );
156 $query = $this->getQueryInfo( $randstr );
157 $res = $dbr->select(
158 $query['tables'],
159 $query['fields'],
160 $query['conds'],
161 $fname,
162 $query['options'],
163 $query['join_conds']
166 return $dbr->fetchObject( $res );
169 protected function getGroupName() {
170 return 'redirects';