Debian Codegraph @

includes/specials/SpecialRandomPage.php

raw symbols SCIP
1 <?php
2 /**
3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation; either version 2 of the License, or
6 * (at your option) any later version.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License along
14 * with this program; if not, write to the Free Software Foundation, Inc.,
15 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
16 * http://www.gnu.org/copyleft/gpl.html
17 *
18 * @file
19 */
20
21 namespace MediaWiki\Specials;
22
23 use MediaWiki\SpecialPage\SpecialPage;
24 use MediaWiki\Title\NamespaceInfo;
25 use MediaWiki\Title\Title;
26 use Wikimedia\Rdbms\IConnectionProvider;
27
28 /**
29 * Redirect to a random page
30 *
31 * @ingroup SpecialPage
32 * @author Rob Church <[email protected]>, Ilmari Karonen
33 */
34 class SpecialRandomPage extends SpecialPage {
35 /** @var int[] namespaces to select pages from */
36 private $namespaces;
37 /** @var bool should the result be a redirect? */
38 protected $isRedir = false;
39 /** @var array Extra SQL statements */
40 protected $extra = [];
41
42 private IConnectionProvider $dbProvider;
43
44 /**
45 * @param IConnectionProvider $dbProvider
46 * @param NamespaceInfo $nsInfo
47 */
48 public function __construct(
49 IConnectionProvider $dbProvider,
50 NamespaceInfo $nsInfo
51 ) {
52 parent::__construct( 'Randompage' );
53 $this->dbProvider = $dbProvider;
54 $this->namespaces = $nsInfo->getContentNamespaces();
55 }
56
57 public function getNamespaces() {
58 return $this->namespaces;
59 }
60
61 public function setNamespace( $ns ) {
62 if ( !$this->isValidNS( $ns ) ) {
63 $ns = NS_MAIN;
64 }
65 $this->namespaces = [ $ns ];
66 }
67
68 private function isValidNS( $ns ) {
69 return $ns !== false && $ns >= 0;
70 }
71
72 // select redirects instead of normal pages?
73 public function isRedirect() {
74 return $this->isRedir;
75 }
76
77 public function execute( $par ) {
78 $this->parsePar( $par );
79
80 $title = $this->getRandomTitle();
81
82 if ( $title === null ) {
83 $this->setHeaders();
84 // Message: randompage-nopages, randomredirect-nopages
85 $this->getOutput()->addWikiMsg( strtolower( $this->getName() ) . '-nopages',
86 $this->getNsList(), count( $this->namespaces ) );
87
88 return;
89 }
90
91 $redirectParam = $this->isRedirect() ? [ 'redirect' => 'no' ] : [];
92 $query = array_merge( $this->getRequest()->getValues(), $redirectParam );
93 unset( $query['title'] );
94 $this->getOutput()->redirect( $title->getFullURL( $query ) );
95 }
96
97 /**
98 * Parse the subpage parameter that specifies namespaces
99 *
100 * @param string $par Subpage to special page
101 */
102 private function parsePar( $par ) {
103 // Testing for stringiness since we want to catch
104 // the empty string to mean main namespace only.
105 if ( is_string( $par ) ) {
106 $ns = $this->getContentLanguage()->getNsIndex( $par );
107 if ( $ns === false && strpos( $par, ',' ) !== false ) {
108 $nsList = [];
109 // Comma separated list
110 $parSplit = explode( ',', $par );
111 foreach ( $parSplit as $potentialNs ) {
112 $ns = $this->getContentLanguage()->getNsIndex( $potentialNs );
113 if ( $this->isValidNS( $ns ) ) {
114 $nsList[] = $ns;
115 }
116 // Remove duplicate values, and re-index array
117 $nsList = array_unique( $nsList );
118 $nsList = array_values( $nsList );
119 if ( $nsList !== [] ) {
120 $this->namespaces = $nsList;
121 }
122 }
123 } else {
124 // Note, that the case of $par being something
125 // like "main" which is not a namespace, falls
126 // through to here, and sets NS_MAIN, allowing
127 // Special:Random/main or Special:Random/article
128 // to work as expected.
129 $this->setNamespace( $this->getContentLanguage()->getNsIndex( $par ) );
130 }
131 }
132 }
133
134 /**
135 * Get a comma-delimited list of namespaces we don't have
136 * any pages in
137 * @return string
138 */
139 private function getNsList() {
140 $contLang = $this->getContentLanguage();
141 $nsNames = [];
142 foreach ( $this->namespaces as $n ) {
143 if ( $n === NS_MAIN ) {
144 $nsNames[] = $this->msg( 'blanknamespace' )->plain();
145 } else {
146 $nsNames[] = $contLang->getNsText( $n );
147 }
148 }
149
150 return $contLang->commaList( $nsNames );
151 }
152
153 /**
154 * Choose a random title.
155 * @return Title|null Title object (or null if nothing to choose from)
156 */
157 public function getRandomTitle() {
158 $randstr = wfRandom();
159 $title = null;
160
161 if ( !$this->getHookRunner()->onSpecialRandomGetRandomTitle(
162 $randstr, $this->isRedir, $this->namespaces,
163 // @phan-suppress-next-line PhanTypeMismatchArgument Type mismatch on pass-by-ref args
164 $this->extra, $title )
165 ) {
166 return $title;
167 }
168
169 $row = $this->selectRandomPageFromDB( $randstr, __METHOD__ );
170
171 /* If we picked a value that was higher than any in
172 * the DB, wrap around and select the page with the
173 * lowest value instead! One might think this would
174 * skew the distribution, but in fact it won't cause
175 * any more bias than what the page_random scheme
176 * causes anyway. Trust me, I'm a mathematician. :)
177 */
178 if ( !$row ) {
179 $row = $this->selectRandomPageFromDB( 0, __METHOD__ );
180 }
181
182 if ( $row ) {
183 return Title::makeTitleSafe( $row->page_namespace, $row->page_title );
184 }
185
186 return null;
187 }
188
189 protected function getQueryInfo( $randstr ) {
190 $dbr = $this->dbProvider->getReplicaDatabase();
191 $redirect = $this->isRedirect() ? 1 : 0;
192 $tables = [ 'page' ];
193 $conds = array_merge( [
194 'page_namespace' => $this->namespaces,
195 'page_is_redirect' => $redirect,
196 $dbr->expr( 'page_random', '>=', $randstr ),
197 ], $this->extra );
198 $joinConds = [];
199
200 // Allow extensions to modify the query
201 $this->getHookRunner()->onRandomPageQuery( $tables, $conds, $joinConds );
202
203 return [
204 'tables' => $tables,
205 'fields' => [ 'page_title', 'page_namespace' ],
206 'conds' => $conds,
207 'options' => [
208 'ORDER BY' => 'page_random',
209 'LIMIT' => 1,
210 ],
211 'join_conds' => $joinConds
212 ];
213 }
214
215 private function selectRandomPageFromDB( $randstr, $fname ) {
216 $dbr = $this->dbProvider->getReplicaDatabase();
217
218 $query = $this->getQueryInfo( $randstr );
219 return $dbr->newSelectQueryBuilder()
220 ->queryInfo( $query )
221 ->caller( $fname )
222 ->fetchRow();
223 }
224
225 protected function getGroupName() {
226 return 'redirects';
227 }
228 }
229
230 /**
231 * Retain the old class name for backwards compatibility.
232 * @deprecated since 1.41
233 */
234 class_alias( SpecialRandomPage::class, 'SpecialRandomPage' );