Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
26.83% |
22 / 82 |
|
41.67% |
5 / 12 |
CRAP | |
0.00% |
0 / 1 |
| SpecialRandomPage | |
27.16% |
22 / 81 |
|
41.67% |
5 / 12 |
330.98 | |
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 / 21 |
|
0.00% |
0 / 1 |
6 | |||
| selectRandomPageFromDB | |
0.00% |
0 / 6 |
|
0.00% |
0 / 1 |
2 | |||
| getGroupName | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| 1 | <?php |
| 2 | /** |
| 3 | * @license GPL-2.0-or-later |
| 4 | * @file |
| 5 | */ |
| 6 | |
| 7 | namespace MediaWiki\Specials; |
| 8 | |
| 9 | use MediaWiki\SpecialPage\SpecialPage; |
| 10 | use MediaWiki\Title\NamespaceInfo; |
| 11 | use MediaWiki\Title\Title; |
| 12 | use Wikimedia\Rdbms\IConnectionProvider; |
| 13 | |
| 14 | /** |
| 15 | * Redirect to a random page |
| 16 | * |
| 17 | * @ingroup SpecialPage |
| 18 | * @author Rob Church <robchur@gmail.com>, Ilmari Karonen |
| 19 | */ |
| 20 | class SpecialRandomPage extends SpecialPage { |
| 21 | /** @var int[] namespaces to select pages from */ |
| 22 | private $namespaces; |
| 23 | /** @var bool should the result be a redirect? */ |
| 24 | protected $isRedir = false; |
| 25 | /** @var array Extra SQL statements */ |
| 26 | protected $extra = []; |
| 27 | |
| 28 | private IConnectionProvider $dbProvider; |
| 29 | |
| 30 | public function __construct( |
| 31 | IConnectionProvider $dbProvider, |
| 32 | NamespaceInfo $nsInfo |
| 33 | ) { |
| 34 | parent::__construct( 'Randompage' ); |
| 35 | $this->dbProvider = $dbProvider; |
| 36 | $this->namespaces = $nsInfo->getContentNamespaces(); |
| 37 | } |
| 38 | |
| 39 | /** |
| 40 | * @return int[] |
| 41 | */ |
| 42 | public function getNamespaces() { |
| 43 | return $this->namespaces; |
| 44 | } |
| 45 | |
| 46 | /** |
| 47 | * @param int|false $ns |
| 48 | */ |
| 49 | public function setNamespace( $ns ) { |
| 50 | if ( !$this->isValidNS( $ns ) ) { |
| 51 | $ns = NS_MAIN; |
| 52 | } |
| 53 | $this->namespaces = [ $ns ]; |
| 54 | } |
| 55 | |
| 56 | /** |
| 57 | * @param int|false $ns |
| 58 | */ |
| 59 | private function isValidNS( $ns ): bool { |
| 60 | return $ns !== false && $ns >= 0; |
| 61 | } |
| 62 | |
| 63 | /** |
| 64 | * select redirects instead of normal pages? |
| 65 | * @return bool |
| 66 | */ |
| 67 | public function isRedirect() { |
| 68 | return $this->isRedir; |
| 69 | } |
| 70 | |
| 71 | /** @inheritDoc */ |
| 72 | public function execute( $par ) { |
| 73 | $this->parsePar( $par ); |
| 74 | |
| 75 | $title = $this->getRandomTitle(); |
| 76 | |
| 77 | if ( $title === null ) { |
| 78 | $this->setHeaders(); |
| 79 | // Message: randompage-nopages, randomredirect-nopages |
| 80 | $this->getOutput()->addWikiMsg( strtolower( $this->getName() ) . '-nopages', |
| 81 | $this->getNsList(), count( $this->namespaces ) ); |
| 82 | |
| 83 | return; |
| 84 | } |
| 85 | |
| 86 | $redirectParam = $this->isRedirect() ? [ 'redirect' => 'no' ] : []; |
| 87 | $query = array_merge( $this->getRequest()->getQueryValues(), $redirectParam ); |
| 88 | unset( $query['title'] ); |
| 89 | $this->getOutput()->redirect( $title->getFullURL( $query ) ); |
| 90 | } |
| 91 | |
| 92 | /** |
| 93 | * Parse the subpage parameter that specifies namespaces |
| 94 | * |
| 95 | * @param string $par Subpage to special page |
| 96 | */ |
| 97 | private function parsePar( $par ) { |
| 98 | // Testing for stringiness since we want to catch |
| 99 | // the empty string to mean main namespace only. |
| 100 | if ( is_string( $par ) ) { |
| 101 | $ns = $this->getContentLanguage()->getNsIndex( $par ); |
| 102 | if ( $ns === false && str_contains( $par, ',' ) ) { |
| 103 | $nsList = []; |
| 104 | // Comma separated list |
| 105 | $parSplit = explode( ',', $par ); |
| 106 | foreach ( $parSplit as $potentialNs ) { |
| 107 | $ns = $this->getContentLanguage()->getNsIndex( $potentialNs ); |
| 108 | if ( $this->isValidNS( $ns ) ) { |
| 109 | $nsList[] = $ns; |
| 110 | } |
| 111 | // Remove duplicate values, and re-index array |
| 112 | $nsList = array_unique( $nsList ); |
| 113 | $nsList = array_values( $nsList ); |
| 114 | if ( $nsList !== [] ) { |
| 115 | $this->namespaces = $nsList; |
| 116 | } |
| 117 | } |
| 118 | } else { |
| 119 | // Note, that the case of $par being something |
| 120 | // like "main" which is not a namespace, falls |
| 121 | // through to here, and sets NS_MAIN, allowing |
| 122 | // Special:Random/main or Special:Random/article |
| 123 | // to work as expected. |
| 124 | $this->setNamespace( $this->getContentLanguage()->getNsIndex( $par ) ); |
| 125 | } |
| 126 | } |
| 127 | } |
| 128 | |
| 129 | /** |
| 130 | * Get a comma-delimited list of namespaces we don't have |
| 131 | * any pages in |
| 132 | * @return string |
| 133 | */ |
| 134 | private function getNsList() { |
| 135 | $contLang = $this->getContentLanguage(); |
| 136 | $nsNames = []; |
| 137 | foreach ( $this->namespaces as $n ) { |
| 138 | if ( $n === NS_MAIN ) { |
| 139 | $nsNames[] = $this->msg( 'blanknamespace' )->plain(); |
| 140 | } else { |
| 141 | $nsNames[] = $contLang->getNsText( $n ); |
| 142 | } |
| 143 | } |
| 144 | |
| 145 | return $contLang->commaList( $nsNames ); |
| 146 | } |
| 147 | |
| 148 | /** |
| 149 | * Choose a random title. |
| 150 | * @return Title|null Title object (or null if nothing to choose from) |
| 151 | */ |
| 152 | public function getRandomTitle() { |
| 153 | $randstr = wfRandom(); |
| 154 | $title = null; |
| 155 | |
| 156 | if ( !$this->getHookRunner()->onSpecialRandomGetRandomTitle( |
| 157 | $randstr, $this->isRedir, $this->namespaces, |
| 158 | // @phan-suppress-next-line PhanTypeMismatchArgument Type mismatch on pass-by-ref args |
| 159 | $this->extra, $title ) |
| 160 | ) { |
| 161 | return $title; |
| 162 | } |
| 163 | |
| 164 | $row = $this->selectRandomPageFromDB( $randstr, __METHOD__ ); |
| 165 | |
| 166 | /* If we picked a value that was higher than any in |
| 167 | * the DB, wrap around and select the page with the |
| 168 | * lowest value instead! One might think this would |
| 169 | * skew the distribution, but in fact it won't cause |
| 170 | * any more bias than what the page_random scheme |
| 171 | * causes anyway. Trust me, I'm a mathematician. :) |
| 172 | */ |
| 173 | if ( !$row ) { |
| 174 | $row = $this->selectRandomPageFromDB( 0, __METHOD__ ); |
| 175 | } |
| 176 | |
| 177 | if ( $row ) { |
| 178 | return Title::makeTitleSafe( $row->page_namespace, $row->page_title ); |
| 179 | } |
| 180 | |
| 181 | return null; |
| 182 | } |
| 183 | |
| 184 | /** |
| 185 | * @param string $randstr |
| 186 | * @return array |
| 187 | */ |
| 188 | protected function getQueryInfo( $randstr ) { |
| 189 | $dbr = $this->dbProvider->getReplicaDatabase(); |
| 190 | $redirect = $this->isRedirect() ? 1 : 0; |
| 191 | $tables = [ 'page' ]; |
| 192 | $conds = [ |
| 193 | 'page_namespace' => $this->namespaces, |
| 194 | 'page_is_redirect' => $redirect, |
| 195 | $dbr->expr( 'page_random', '>=', $randstr ), |
| 196 | ...$this->extra, |
| 197 | ]; |
| 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 | /** |
| 216 | * @param int|string $randstr |
| 217 | * @param string $fname |
| 218 | * @return \stdClass|false |
| 219 | */ |
| 220 | private function selectRandomPageFromDB( $randstr, string $fname ) { |
| 221 | $dbr = $this->dbProvider->getReplicaDatabase(); |
| 222 | |
| 223 | $query = $this->getQueryInfo( $randstr ); |
| 224 | return $dbr->newSelectQueryBuilder() |
| 225 | ->queryInfo( $query ) |
| 226 | ->caller( $fname ) |
| 227 | ->fetchRow(); |
| 228 | } |
| 229 | |
| 230 | /** @inheritDoc */ |
| 231 | protected function getGroupName() { |
| 232 | return 'redirects'; |
| 233 | } |
| 234 | } |
| 235 | |
| 236 | /** |
| 237 | * Retain the old class name for backwards compatibility. |
| 238 | * @deprecated since 1.41 |
| 239 | */ |
| 240 | class_alias( SpecialRandomPage::class, 'SpecialRandomPage' ); |