| 1 |
<?php |
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
namespace MediaWiki\Specials; |
| 22 |
|
| 23 |
use MediaWiki\Html\Html; |
| 24 |
use MediaWiki\HTMLForm\HTMLForm; |
| 25 |
use MediaWiki\MainConfigNames; |
| 26 |
use MediaWiki\MediaWikiServices; |
| 27 |
use MediaWiki\Page\ExistingPageRecord; |
| 28 |
use MediaWiki\Page\PageStore; |
| 29 |
use MediaWiki\SpecialPage\IncludableSpecialPage; |
| 30 |
use MediaWiki\Title\Title; |
| 31 |
use MediaWiki\Title\TitleValue; |
| 32 |
use SearchEngineFactory; |
| 33 |
use Wikimedia\Rdbms\IConnectionProvider; |
| 34 |
use Wikimedia\Rdbms\SelectQueryBuilder; |
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
class SpecialAllPages extends IncludableSpecialPage { |
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
protected $maxPerPage = 345; |
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
protected $nsfromMsg = 'allpagesfrom'; |
| 57 |
|
| 58 |
private IConnectionProvider $dbProvider; |
| 59 |
private SearchEngineFactory $searchEngineFactory; |
| 60 |
private PageStore $pageStore; |
| 61 |
|
| 62 |
public function __construct( |
| 63 |
?IConnectionProvider $dbProvider = null, |
| 64 |
?SearchEngineFactory $searchEngineFactory = null, |
| 65 |
?PageStore $pageStore = null |
| 66 |
) { |
| 67 |
parent::__construct( 'Allpages' ); |
| 68 |
|
| 69 |
$services = MediaWikiServices::getInstance(); |
| 70 |
$this->dbProvider = $dbProvider ?? $services->getConnectionProvider(); |
| 71 |
$this->searchEngineFactory = $searchEngineFactory ?? $services->getSearchEngineFactory(); |
| 72 |
$this->pageStore = $pageStore ?? $services->getPageStore(); |
| 73 |
} |
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
public function execute( $par ) { |
| 81 |
$request = $this->getRequest(); |
| 82 |
$out = $this->getOutput(); |
| 83 |
|
| 84 |
$this->setHeaders(); |
| 85 |
$this->outputHeader(); |
| 86 |
$out->getMetadata()->setPreventClickjacking( false ); |
| 87 |
|
| 88 |
|
| 89 |
$from = $request->getVal( 'from', null ); |
| 90 |
$to = $request->getVal( 'to', null ); |
| 91 |
$namespace = $request->getInt( 'namespace' ); |
| 92 |
|
| 93 |
$miserMode = (bool)$this->getConfig()->get( MainConfigNames::MiserMode ); |
| 94 |
|
| 95 |
|
| 96 |
$hideredirects = $request->getBool( 'hideredirects', false ) && !$miserMode; |
| 97 |
|
| 98 |
$namespaces = $this->getLanguage()->getNamespaces(); |
| 99 |
|
| 100 |
$out->setPageTitleMsg( |
| 101 |
( $namespace > 0 && array_key_exists( $namespace, $namespaces ) ) ? |
| 102 |
$this->msg( 'allinnamespace' )->plaintextParams( str_replace( '_', ' ', $namespaces[$namespace] ) ) : |
| 103 |
$this->msg( 'allarticles' ) |
| 104 |
); |
| 105 |
$out->addModuleStyles( 'mediawiki.special' ); |
| 106 |
|
| 107 |
if ( $par !== null ) { |
| 108 |
$this->showChunk( $namespace, $par, $to, $hideredirects ); |
| 109 |
} elseif ( $from !== null && $to === null ) { |
| 110 |
$this->showChunk( $namespace, $from, $to, $hideredirects ); |
| 111 |
} else { |
| 112 |
$this->showToplevel( $namespace, $from, $to, $hideredirects ); |
| 113 |
} |
| 114 |
} |
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
protected function outputHTMLForm( $namespace = NS_MAIN, |
| 125 |
$from = '', $to = '', $hideRedirects = false |
| 126 |
) { |
| 127 |
$miserMode = (bool)$this->getConfig()->get( MainConfigNames::MiserMode ); |
| 128 |
$formDescriptor = [ |
| 129 |
'from' => [ |
| 130 |
'type' => 'text', |
| 131 |
'name' => 'from', |
| 132 |
'id' => 'nsfrom', |
| 133 |
'size' => 30, |
| 134 |
'label-message' => 'allpagesfrom', |
| 135 |
'default' => str_replace( '_', ' ', $from ), |
| 136 |
], |
| 137 |
'to' => [ |
| 138 |
'type' => 'text', |
| 139 |
'name' => 'to', |
| 140 |
'id' => 'nsto', |
| 141 |
'size' => 30, |
| 142 |
'label-message' => 'allpagesto', |
| 143 |
'default' => str_replace( '_', ' ', $to ), |
| 144 |
], |
| 145 |
'namespace' => [ |
| 146 |
'type' => 'namespaceselect', |
| 147 |
'name' => 'namespace', |
| 148 |
'id' => 'namespace', |
| 149 |
'label-message' => 'namespace', |
| 150 |
'all' => null, |
| 151 |
'default' => $namespace, |
| 152 |
], |
| 153 |
'hideredirects' => [ |
| 154 |
'type' => 'check', |
| 155 |
'name' => 'hideredirects', |
| 156 |
'id' => 'hidredirects', |
| 157 |
'label-message' => 'allpages-hide-redirects', |
| 158 |
'value' => $hideRedirects, |
| 159 |
], |
| 160 |
]; |
| 161 |
|
| 162 |
if ( $miserMode ) { |
| 163 |
unset( $formDescriptor['hideredirects'] ); |
| 164 |
} |
| 165 |
|
| 166 |
$htmlForm = HTMLForm::factory( 'ooui', $formDescriptor, $this->getContext() ); |
| 167 |
$htmlForm |
| 168 |
->setMethod( 'get' ) |
| 169 |
->setTitle( $this->getPageTitle() ) |
| 170 |
->setWrapperLegendMsg( 'allpages' ) |
| 171 |
->setSubmitTextMsg( 'allpagessubmit' ) |
| 172 |
->prepareForm() |
| 173 |
->displayForm( false ); |
| 174 |
} |
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
|
| 180 |
|
| 181 |
|
| 182 |
private function showToplevel( |
| 183 |
$namespace = NS_MAIN, $from = null, $to = null, $hideredirects = false |
| 184 |
) { |
| 185 |
$from = $from ? Title::makeTitleSafe( $namespace, $from ) : null; |
| 186 |
$to = $to ? Title::makeTitleSafe( $namespace, $to ) : null; |
| 187 |
$from = ( $from && $from->isLocal() ) ? $from->getDBkey() : null; |
| 188 |
$to = ( $to && $to->isLocal() ) ? $to->getDBkey() : null; |
| 189 |
|
| 190 |
$this->showChunk( $namespace, $from, $to, $hideredirects ); |
| 191 |
} |
| 192 |
|
| 193 |
|
| 194 |
|
| 195 |
|
| 196 |
|
| 197 |
|
| 198 |
|
| 199 |
private function showChunk( |
| 200 |
$namespace = NS_MAIN, $from = null, $to = null, $hideredirects = false |
| 201 |
) { |
| 202 |
$output = $this->getOutput(); |
| 203 |
|
| 204 |
$fromList = $this->getNamespaceKeyAndText( $namespace, $from ); |
| 205 |
$toList = $this->getNamespaceKeyAndText( $namespace, $to ); |
| 206 |
$namespaces = $this->getContext()->getLanguage()->getNamespaces(); |
| 207 |
$n = 0; |
| 208 |
$prevTitle = null; |
| 209 |
|
| 210 |
if ( !$fromList || !$toList ) { |
| 211 |
$out = $this->msg( 'allpagesbadtitle' )->parseAsBlock(); |
| 212 |
} elseif ( !array_key_exists( $namespace, $namespaces ) ) { |
| 213 |
|
| 214 |
$out = $this->msg( 'allpages-bad-ns', $namespace )->parse(); |
| 215 |
$namespace = NS_MAIN; |
| 216 |
} else { |
| 217 |
[ $namespace, $fromKey, $from ] = $fromList; |
| 218 |
[ , $toKey, $to ] = $toList; |
| 219 |
|
| 220 |
$dbr = $this->dbProvider->getReplicaDatabase(); |
| 221 |
$filterConds = [ 'page_namespace' => $namespace ]; |
| 222 |
if ( $hideredirects ) { |
| 223 |
$filterConds['page_is_redirect'] = 0; |
| 224 |
} |
| 225 |
|
| 226 |
$conds = $filterConds; |
| 227 |
$conds[] = $dbr->expr( 'page_title', '>=', $fromKey ); |
| 228 |
if ( $toKey !== "" ) { |
| 229 |
$conds[] = $dbr->expr( 'page_title', '<=', $toKey ); |
| 230 |
} |
| 231 |
|
| 232 |
$res = $this->pageStore->newSelectQueryBuilder() |
| 233 |
->where( $conds ) |
| 234 |
->caller( __METHOD__ ) |
| 235 |
->orderBy( 'page_title' ) |
| 236 |
->limit( $this->maxPerPage + 1 ) |
| 237 |
->useIndex( 'page_name_title' ) |
| 238 |
->fetchPageRecords(); |
| 239 |
|
| 240 |
|
| 241 |
|
| 242 |
|
| 243 |
|
| 244 |
$pages = iterator_to_array( $res ); |
| 245 |
|
| 246 |
$linkRenderer = $this->getLinkRenderer(); |
| 247 |
if ( count( $pages ) > 0 ) { |
| 248 |
$out = Html::openElement( 'ul', [ 'class' => 'mw-allpages-chunk' ] ); |
| 249 |
|
| 250 |
while ( $n < $this->maxPerPage && $n < count( $pages ) ) { |
| 251 |
$page = $pages[$n]; |
| 252 |
$attributes = $page->isRedirect() ? [ 'class' => 'allpagesredirect' ] : []; |
| 253 |
|
| 254 |
$out .= Html::rawElement( 'li', $attributes, $linkRenderer->makeKnownLink( $page ) ) . "\n"; |
| 255 |
$n++; |
| 256 |
} |
| 257 |
$out .= Html::closeElement( 'ul' ); |
| 258 |
|
| 259 |
if ( count( $pages ) > 2 ) { |
| 260 |
|
| 261 |
|
| 262 |
$out = Html::rawElement( 'div', [ 'class' => 'mw-allpages-body' ], $out ); |
| 263 |
} |
| 264 |
} else { |
| 265 |
$out = ''; |
| 266 |
} |
| 267 |
|
| 268 |
if ( $fromKey !== '' && !$this->including() ) { |
| 269 |
|
| 270 |
$prevConds = $filterConds; |
| 271 |
$prevConds[] = $dbr->expr( 'page_title', '<', $fromKey ); |
| 272 |
$prevKey = $dbr->newSelectQueryBuilder() |
| 273 |
->select( 'page_title' ) |
| 274 |
->from( 'page' ) |
| 275 |
->where( $prevConds ) |
| 276 |
->orderBy( 'page_title', SelectQueryBuilder::SORT_DESC ) |
| 277 |
->offset( $this->maxPerPage - 1 ) |
| 278 |
->caller( __METHOD__ )->fetchField(); |
| 279 |
|
| 280 |
if ( $prevKey === false ) { |
| 281 |
|
| 282 |
|
| 283 |
$prevKey = $dbr->newSelectQueryBuilder() |
| 284 |
->select( 'page_title' ) |
| 285 |
->from( 'page' ) |
| 286 |
->where( $prevConds ) |
| 287 |
->orderBy( 'page_title' ) |
| 288 |
->caller( __METHOD__ )->fetchField(); |
| 289 |
} |
| 290 |
|
| 291 |
if ( $prevKey !== false ) { |
| 292 |
$prevTitle = Title::makeTitle( $namespace, $prevKey ); |
| 293 |
} |
| 294 |
} |
| 295 |
} |
| 296 |
|
| 297 |
if ( $this->including() ) { |
| 298 |
$output->addHTML( $out ); |
| 299 |
return; |
| 300 |
} |
| 301 |
|
| 302 |
$navLinks = []; |
| 303 |
$self = $this->getPageTitle(); |
| 304 |
|
| 305 |
$linkRenderer = $this->getLinkRenderer(); |
| 306 |
|
| 307 |
if ( $prevTitle ) { |
| 308 |
$query = [ 'from' => $prevTitle->getText() ]; |
| 309 |
|
| 310 |
if ( $namespace ) { |
| 311 |
$query['namespace'] = $namespace; |
| 312 |
} |
| 313 |
|
| 314 |
if ( $hideredirects ) { |
| 315 |
$query['hideredirects'] = $hideredirects; |
| 316 |
} |
| 317 |
|
| 318 |
$navLinks[] = $linkRenderer->makeKnownLink( |
| 319 |
$self, |
| 320 |
$this->msg( 'prevpage', $prevTitle->getText() )->text(), |
| 321 |
[], |
| 322 |
$query |
| 323 |
); |
| 324 |
|
| 325 |
} |
| 326 |
|
| 327 |
|
| 328 |
if ( $n === $this->maxPerPage && isset( $pages[$n] ) ) { |
| 329 |
|
| 330 |
$t = TitleValue::newFromPage( $pages[$n] ); |
| 331 |
$query = [ 'from' => $t->getText() ]; |
| 332 |
|
| 333 |
if ( $namespace ) { |
| 334 |
$query['namespace'] = $namespace; |
| 335 |
} |
| 336 |
|
| 337 |
if ( $hideredirects ) { |
| 338 |
$query['hideredirects'] = $hideredirects; |
| 339 |
} |
| 340 |
|
| 341 |
$navLinks[] = $linkRenderer->makeKnownLink( |
| 342 |
$self, |
| 343 |
$this->msg( 'nextpage', $t->getText() )->text(), |
| 344 |
[], |
| 345 |
$query |
| 346 |
); |
| 347 |
} |
| 348 |
|
| 349 |
$this->outputHTMLForm( $namespace, $from ?? '', $to ?? '', $hideredirects ); |
| 350 |
|
| 351 |
if ( count( $navLinks ) ) { |
| 352 |
|
| 353 |
$pagination = Html::rawElement( 'div', |
| 354 |
[ 'class' => 'mw-allpages-nav' ], |
| 355 |
$this->getLanguage()->pipeList( $navLinks ) |
| 356 |
); |
| 357 |
|
| 358 |
$output->addHTML( $pagination ); |
| 359 |
$out .= Html::element( 'hr' ) . $pagination; |
| 360 |
} |
| 361 |
|
| 362 |
$output->addHTML( $out ); |
| 363 |
} |
| 364 |
|
| 365 |
|
| 366 |
|
| 367 |
|
| 368 |
|
| 369 |
|
| 370 |
protected function getNamespaceKeyAndText( $ns, $text ) { |
| 371 |
if ( $text == '' ) { |
| 372 |
|
| 373 |
return [ $ns, '', '' ]; |
| 374 |
} |
| 375 |
|
| 376 |
$t = Title::makeTitleSafe( $ns, $text ); |
| 377 |
if ( $t && $t->isLocal() ) { |
| 378 |
return [ $t->getNamespace(), $t->getDBkey(), $t->getText() ]; |
| 379 |
} elseif ( $t ) { |
| 380 |
return null; |
| 381 |
} |
| 382 |
|
| 383 |
|
| 384 |
$text = preg_replace( '/(#|$)/', 'X$1', $text ); |
| 385 |
$t = Title::makeTitleSafe( $ns, $text ); |
| 386 |
if ( $t && $t->isLocal() ) { |
| 387 |
return [ $t->getNamespace(), '', '' ]; |
| 388 |
} else { |
| 389 |
return null; |
| 390 |
} |
| 391 |
} |
| 392 |
|
| 393 |
|
| 394 |
|
| 395 |
|
| 396 |
|
| 397 |
|
| 398 |
|
| 399 |
|
| 400 |
|
| 401 |
public function prefixSearchSubpages( $search, $limit, $offset ) { |
| 402 |
return $this->prefixSearchString( $search, $limit, $offset, $this->searchEngineFactory ); |
| 403 |
} |
| 404 |
|
| 405 |
protected function getGroupName() { |
| 406 |
return 'pages'; |
| 407 |
} |
| 408 |
} |
| 409 |
|
| 410 |
|
| 411 |
class_alias( SpecialAllPages::class, 'SpecialAllPages' ); |