Corrected grammatical error.
[lhc/web/wiklou.git] / includes / specials / SpecialRandompage.php
1 <?php
2 /**
3 * Implements Special:Randompage
4 *
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.
9 *
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.
14 *
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
19 *
20 * @file
21 * @ingroup SpecialPage
22 * @author Rob Church <robchur@gmail.com>, Ilmari Karonen
23 */
24
25 use MediaWiki\MediaWikiServices;
26
27 /**
28 * Special page to direct the user to a random page
29 *
30 * @ingroup SpecialPage
31 */
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
36
37 public function __construct( $name = 'Randompage' ) {
38 $this->namespaces = MWNamespace::getContentNamespaces();
39 parent::__construct( $name );
40 }
41
42 public function getNamespaces() {
43 return $this->namespaces;
44 }
45
46 public function setNamespace( $ns ) {
47 if ( !$ns || $ns < NS_MAIN ) {
48 $ns = NS_MAIN;
49 }
50 $this->namespaces = [ $ns ];
51 }
52
53 // select redirects instead of normal pages?
54 public function isRedirect() {
55 return $this->isRedir;
56 }
57
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.
62 $this->setNamespace(
63 MediaWikiServices::getInstance()->getContentLanguage()->getNsIndex( $par ) );
64 }
65
66 $title = $this->getRandomTitle();
67
68 if ( is_null( $title ) ) {
69 $this->setHeaders();
70 // Message: randompage-nopages, randomredirect-nopages
71 $this->getOutput()->addWikiMsg( strtolower( $this->getName() ) . '-nopages',
72 $this->getNsList(), count( $this->namespaces ) );
73
74 return;
75 }
76
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 ) );
81 }
82
83 /**
84 * Get a comma-delimited list of namespaces we don't have
85 * any pages in
86 * @return string
87 */
88 private function getNsList() {
89 $contLang = MediaWikiServices::getInstance()->getContentLanguage();
90 $nsNames = [];
91 foreach ( $this->namespaces as $n ) {
92 if ( $n === NS_MAIN ) {
93 $nsNames[] = $this->msg( 'blanknamespace' )->plain();
94 } else {
95 $nsNames[] = $contLang->getNsText( $n );
96 }
97 }
98
99 return $contLang->commaList( $nsNames );
100 }
101
102 /**
103 * Choose a random title.
104 * @return Title|null Title object (or null if nothing to choose from)
105 */
106 public function getRandomTitle() {
107 $randstr = wfRandom();
108 $title = null;
109
110 if ( !Hooks::run(
111 'SpecialRandomGetRandomTitle',
112 [ &$randstr, &$this->isRedir, &$this->namespaces, &$this->extra, &$title ]
113 ) ) {
114 return $title;
115 }
116
117 $row = $this->selectRandomPageFromDB( $randstr );
118
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. :)
125 */
126 if ( !$row ) {
127 $row = $this->selectRandomPageFromDB( "0" );
128 }
129
130 if ( $row ) {
131 return Title::makeTitleSafe( $row->page_namespace, $row->page_title );
132 }
133
134 return null;
135 }
136
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
144 ], $this->extra );
145 $joinConds = [];
146
147 // Allow extensions to modify the query
148 Hooks::run( 'RandomPageQuery', [ &$tables, &$conds, &$joinConds ] );
149
150 return [
151 'tables' => $tables,
152 'fields' => [ 'page_title', 'page_namespace' ],
153 'conds' => $conds,
154 'options' => [
155 'ORDER BY' => 'page_random',
156 'LIMIT' => 1,
157 ],
158 'join_conds' => $joinConds
159 ];
160 }
161
162 private function selectRandomPageFromDB( $randstr, $fname = __METHOD__ ) {
163 $dbr = wfGetDB( DB_REPLICA );
164
165 $query = $this->getQueryInfo( $randstr );
166 $res = $dbr->select(
167 $query['tables'],
168 $query['fields'],
169 $query['conds'],
170 $fname,
171 $query['options'],
172 $query['join_conds']
173 );
174
175 return $dbr->fetchObject( $res );
176 }
177
178 protected function getGroupName() {
179 return 'redirects';
180 }
181 }