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
25 use MediaWiki\MediaWikiServices
;
28 * Special page to direct the user to a random page
30 * @ingroup SpecialPage
32 class RandomPage
extends SpecialPage
{
33 private $namespaces; // namespaces to select pages from
34 protected $isRedir = false; // should the result be a redirect?
35 protected $extra = []; // Extra SQL statements
37 public function __construct( $name = 'Randompage' ) {
38 $this->namespaces
= MWNamespace
::getContentNamespaces();
39 parent
::__construct( $name );
42 public function getNamespaces() {
43 return $this->namespaces
;
46 public function setNamespace( $ns ) {
47 if ( !$ns ||
$ns < NS_MAIN
) {
50 $this->namespaces
= [ $ns ];
53 // select redirects instead of normal pages?
54 public function isRedirect() {
55 return $this->isRedir
;
58 public function execute( $par ) {
59 if ( is_string( $par ) ) {
60 // Testing for stringiness since we want to catch
61 // the empty string to mean main namespace only.
63 MediaWikiServices
::getInstance()->getContentLanguage()->getNsIndex( $par ) );
66 $title = $this->getRandomTitle();
68 if ( is_null( $title ) ) {
70 // Message: randompage-nopages, randomredirect-nopages
71 $this->getOutput()->addWikiMsg( strtolower( $this->getName() ) . '-nopages',
72 $this->getNsList(), count( $this->namespaces
) );
77 $redirectParam = $this->isRedirect() ?
[ 'redirect' => 'no' ] : [];
78 $query = array_merge( $this->getRequest()->getValues(), $redirectParam );
79 unset( $query['title'] );
80 $this->getOutput()->redirect( $title->getFullURL( $query ) );
84 * Get a comma-delimited list of namespaces we don't have
88 private function getNsList() {
89 $contLang = MediaWikiServices
::getInstance()->getContentLanguage();
91 foreach ( $this->namespaces
as $n ) {
92 if ( $n === NS_MAIN
) {
93 $nsNames[] = $this->msg( 'blanknamespace' )->plain();
95 $nsNames[] = $contLang->getNsText( $n );
99 return $contLang->commaList( $nsNames );
103 * Choose a random title.
104 * @return Title|null Title object (or null if nothing to choose from)
106 public function getRandomTitle() {
107 $randstr = wfRandom();
111 'SpecialRandomGetRandomTitle',
112 [ &$randstr, &$this->isRedir
, &$this->namespaces
, &$this->extra
, &$title ]
117 $row = $this->selectRandomPageFromDB( $randstr );
119 /* If we picked a value that was higher than any in
120 * the DB, wrap around and select the page with the
121 * lowest value instead! One might think this would
122 * skew the distribution, but in fact it won't cause
123 * any more bias than what the page_random scheme
124 * causes anyway. Trust me, I'm a mathematician. :)
127 $row = $this->selectRandomPageFromDB( "0" );
131 return Title
::makeTitleSafe( $row->page_namespace
, $row->page_title
);
137 protected function getQueryInfo( $randstr ) {
138 $redirect = $this->isRedirect() ?
1 : 0;
139 $tables = [ 'page' ];
140 $conds = array_merge( [
141 'page_namespace' => $this->namespaces
,
142 'page_is_redirect' => $redirect,
143 'page_random >= ' . $randstr
147 // Allow extensions to modify the query
148 Hooks
::run( 'RandomPageQuery', [ &$tables, &$conds, &$joinConds ] );
152 'fields' => [ 'page_title', 'page_namespace' ],
155 'ORDER BY' => 'page_random',
158 'join_conds' => $joinConds
162 private function selectRandomPageFromDB( $randstr, $fname = __METHOD__
) {
163 $dbr = wfGetDB( DB_REPLICA
);
165 $query = $this->getQueryInfo( $randstr );
175 return $dbr->fetchObject( $res );
178 protected function getGroupName() {