Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
25.88% |
22 / 85 |
|
41.67% |
5 / 12 |
CRAP | |
0.00% |
0 / 1 |
SpecialRandomPage | |
26.19% |
22 / 84 |
|
41.67% |
5 / 12 |
343.25 | |
0.00% |
0 / 1 |
__construct | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
1 | |||
getNamespaces | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
setNamespace | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
2 | |||
isValidNS | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
2 | |||
isRedirect | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
execute | |
0.00% |
0 / 11 |
|
0.00% |
0 / 1 |
12 | |||
parsePar | |
100.00% |
14 / 14 |
|
100.00% |
1 / 1 |
7 | |||
getNsList | |
0.00% |
0 / 7 |
|
0.00% |
0 / 1 |
12 | |||
getRandomTitle | |
0.00% |
0 / 12 |
|
0.00% |
0 / 1 |
20 | |||
getQueryInfo | |
0.00% |
0 / 19 |
|
0.00% |
0 / 1 |
6 | |||
selectRandomPageFromDB | |
0.00% |
0 / 11 |
|
0.00% |
0 / 1 |
2 | |||
getGroupName | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 |
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 | namespace MediaWiki\Specials; |
26 | |
27 | use MediaWiki\SpecialPage\SpecialPage; |
28 | use MediaWiki\Title\NamespaceInfo; |
29 | use MediaWiki\Title\Title; |
30 | use Wikimedia\Rdbms\IConnectionProvider; |
31 | |
32 | /** |
33 | * Special page to direct the user to a random page |
34 | * |
35 | * @ingroup SpecialPage |
36 | */ |
37 | class SpecialRandomPage extends SpecialPage { |
38 | private $namespaces; // namespaces to select pages from |
39 | protected $isRedir = false; // should the result be a redirect? |
40 | protected $extra = []; // Extra SQL statements |
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 | $redirect = $this->isRedirect() ? 1 : 0; |
191 | $tables = [ 'page' ]; |
192 | $conds = array_merge( [ |
193 | 'page_namespace' => $this->namespaces, |
194 | 'page_is_redirect' => $redirect, |
195 | 'page_random >= ' . $randstr |
196 | ], $this->extra ); |
197 | $joinConds = []; |
198 | |
199 | // Allow extensions to modify the query |
200 | $this->getHookRunner()->onRandomPageQuery( $tables, $conds, $joinConds ); |
201 | |
202 | return [ |
203 | 'tables' => $tables, |
204 | 'fields' => [ 'page_title', 'page_namespace' ], |
205 | 'conds' => $conds, |
206 | 'options' => [ |
207 | 'ORDER BY' => 'page_random', |
208 | 'LIMIT' => 1, |
209 | ], |
210 | 'join_conds' => $joinConds |
211 | ]; |
212 | } |
213 | |
214 | private function selectRandomPageFromDB( $randstr, $fname = __METHOD__ ) { |
215 | $dbr = $this->dbProvider->getReplicaDatabase(); |
216 | |
217 | $query = $this->getQueryInfo( $randstr ); |
218 | $res = $dbr->select( |
219 | $query['tables'], |
220 | $query['fields'], |
221 | $query['conds'], |
222 | $fname, |
223 | $query['options'], |
224 | $query['join_conds'] |
225 | ); |
226 | |
227 | return $res->fetchObject(); |
228 | } |
229 | |
230 | protected function getGroupName() { |
231 | return 'redirects'; |
232 | } |
233 | } |
234 | |
235 | /** |
236 | * Retain the old class name for backwards compatibility. |
237 | * @deprecated since 1.41 |
238 | */ |
239 | class_alias( SpecialRandomPage::class, 'SpecialRandomPage' ); |