| 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\Cache\LinkCache; |
| 24 |
use MediaWiki\Html\Html; |
| 25 |
use MediaWiki\HTMLForm\Field\HTMLCheckField; |
| 26 |
use MediaWiki\HTMLForm\HTMLForm; |
| 27 |
use MediaWiki\Title\Title; |
| 28 |
use Wikimedia\Rdbms\IConnectionProvider; |
| 29 |
use Wikimedia\Rdbms\IExpression; |
| 30 |
use Wikimedia\Rdbms\LikeValue; |
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
class SpecialPrefixIndex extends SpecialAllPages { |
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
protected $stripPrefix = false; |
| 46 |
|
| 47 |
|
| 48 |
protected $hideRedirects = false; |
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
private IConnectionProvider $dbProvider; |
| 54 |
private LinkCache $linkCache; |
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
public function __construct( |
| 61 |
IConnectionProvider $dbProvider, |
| 62 |
LinkCache $linkCache |
| 63 |
) { |
| 64 |
parent::__construct( $dbProvider ); |
| 65 |
$this->mName = 'Prefixindex'; |
| 66 |
$this->dbProvider = $dbProvider; |
| 67 |
$this->linkCache = $linkCache; |
| 68 |
} |
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
public function execute( $par ) { |
| 75 |
$this->setHeaders(); |
| 76 |
$this->outputHeader(); |
| 77 |
|
| 78 |
$out = $this->getOutput(); |
| 79 |
$out->addModuleStyles( 'mediawiki.special' ); |
| 80 |
|
| 81 |
|
| 82 |
$request = $this->getRequest(); |
| 83 |
$from = $request->getVal( 'from', '' ); |
| 84 |
$prefix = $request->getVal( 'prefix', '' ); |
| 85 |
$ns = $request->getIntOrNull( 'namespace' ); |
| 86 |
$namespace = (int)$ns; |
| 87 |
$this->hideRedirects = $request->getBool( 'hideredirects', $this->hideRedirects ); |
| 88 |
$this->stripPrefix = $request->getBool( 'stripprefix', $this->stripPrefix ); |
| 89 |
|
| 90 |
$namespaces = $this->getContentLanguage()->getNamespaces(); |
| 91 |
$out->setPageTitleMsg( |
| 92 |
( $namespace > 0 && array_key_exists( $namespace, $namespaces ) ) |
| 93 |
? $this->msg( 'prefixindex-namespace' )->plaintextParams( |
| 94 |
str_replace( '_', ' ', $namespaces[$namespace] ) |
| 95 |
) |
| 96 |
: $this->msg( 'prefixindex' ) |
| 97 |
); |
| 98 |
|
| 99 |
$showme = ''; |
| 100 |
if ( $par !== null ) { |
| 101 |
$showme = $par; |
| 102 |
} elseif ( $prefix != '' ) { |
| 103 |
$showme = $prefix; |
| 104 |
} elseif ( $from != '' && $ns === null ) { |
| 105 |
|
| 106 |
|
| 107 |
$showme = $from; |
| 108 |
} |
| 109 |
|
| 110 |
|
| 111 |
if ( $this->including() || $showme != '' || $ns !== null ) { |
| 112 |
$this->showPrefixChunk( $namespace, $showme, $from ); |
| 113 |
} else { |
| 114 |
$out->addHTML( $this->namespacePrefixForm( $namespace, '' )->getHTML( false ) ); |
| 115 |
} |
| 116 |
} |
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
protected function namespacePrefixForm( $namespace = NS_MAIN, $from = '' ): HTMLForm { |
| 125 |
$formDescriptor = [ |
| 126 |
'prefix' => [ |
| 127 |
'label-message' => 'allpagesprefix', |
| 128 |
'name' => 'prefix', |
| 129 |
'id' => 'nsfrom', |
| 130 |
'type' => 'text', |
| 131 |
'size' => '30', |
| 132 |
'default' => str_replace( '_', ' ', $from ), |
| 133 |
], |
| 134 |
'namespace' => [ |
| 135 |
'type' => 'namespaceselect', |
| 136 |
'name' => 'namespace', |
| 137 |
'id' => 'namespace', |
| 138 |
'label-message' => 'namespace', |
| 139 |
'all' => null, |
| 140 |
'default' => $namespace, |
| 141 |
], |
| 142 |
'hidedirects' => [ |
| 143 |
'class' => HTMLCheckField::class, |
| 144 |
'name' => 'hideredirects', |
| 145 |
'label-message' => 'allpages-hide-redirects', |
| 146 |
], |
| 147 |
'stripprefix' => [ |
| 148 |
'class' => HTMLCheckField::class, |
| 149 |
'name' => 'stripprefix', |
| 150 |
'label-message' => 'prefixindex-strip', |
| 151 |
], |
| 152 |
]; |
| 153 |
|
| 154 |
$this->getHookRunner()->onSpecialPrefixIndexGetFormFilters( $this->getContext(), $formDescriptor ); |
| 155 |
|
| 156 |
$htmlForm = HTMLForm::factory( 'ooui', $formDescriptor, $this->getContext() ) |
| 157 |
->setMethod( 'get' ) |
| 158 |
->setTitle( $this->getPageTitle() ) |
| 159 |
->setWrapperLegendMsg( 'prefixindex' ) |
| 160 |
->setSubmitTextMsg( 'prefixindex-submit' ); |
| 161 |
|
| 162 |
return $htmlForm->prepareForm(); |
| 163 |
} |
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
protected function showPrefixChunk( $namespace, $prefix, $from = null ) { |
| 171 |
$from ??= $prefix; |
| 172 |
|
| 173 |
$fromList = $this->getNamespaceKeyAndText( $namespace, $from ); |
| 174 |
$prefixList = $this->getNamespaceKeyAndText( $namespace, $prefix ); |
| 175 |
$namespaces = $this->getContentLanguage()->getNamespaces(); |
| 176 |
$res = null; |
| 177 |
$n = 0; |
| 178 |
$nextRow = null; |
| 179 |
$preparedHtmlForm = $this->namespacePrefixForm( $namespace, $prefix ); |
| 180 |
|
| 181 |
if ( !$prefixList || !$fromList ) { |
| 182 |
$out = $this->msg( 'allpagesbadtitle' )->parseAsBlock(); |
| 183 |
} elseif ( !array_key_exists( $namespace, $namespaces ) ) { |
| 184 |
|
| 185 |
$out = $this->msg( 'allpages-bad-ns', $namespace )->parse(); |
| 186 |
$namespace = NS_MAIN; |
| 187 |
} else { |
| 188 |
[ $namespace, $prefixKey, $prefix ] = $prefixList; |
| 189 |
[ , $fromKey, ] = $fromList; |
| 190 |
|
| 191 |
|
| 192 |
|
| 193 |
$dbr = $this->dbProvider->getReplicaDatabase(); |
| 194 |
$queryBuiler = $dbr->newSelectQueryBuilder() |
| 195 |
->select( LinkCache::getSelectFields() ) |
| 196 |
->from( 'page' ) |
| 197 |
->where( [ |
| 198 |
'page_namespace' => $namespace, |
| 199 |
$dbr->expr( |
| 200 |
'page_title', |
| 201 |
IExpression::LIKE, |
| 202 |
new LikeValue( $prefixKey, $dbr->anyString() ) |
| 203 |
), |
| 204 |
$dbr->expr( 'page_title', '>=', $fromKey ), |
| 205 |
] ) |
| 206 |
->orderBy( 'page_title' ) |
| 207 |
->limit( $this->maxPerPage + 1 ) |
| 208 |
->useIndex( 'page_name_title' ); |
| 209 |
|
| 210 |
if ( $this->hideRedirects ) { |
| 211 |
$queryBuiler->andWhere( [ 'page_is_redirect' => 0 ] ); |
| 212 |
} |
| 213 |
|
| 214 |
$this->getHookRunner()->onSpecialPrefixIndexQuery( $preparedHtmlForm->mFieldData, $queryBuiler ); |
| 215 |
|
| 216 |
$res = $queryBuiler->caller( __METHOD__ )->fetchResultSet(); |
| 217 |
|
| 218 |
|
| 219 |
|
| 220 |
if ( $res->numRows() > 0 ) { |
| 221 |
$out = Html::openElement( 'ul', [ 'class' => 'mw-prefixindex-list' ] ); |
| 222 |
|
| 223 |
$prefixLength = strlen( $prefix ); |
| 224 |
foreach ( $res as $row ) { |
| 225 |
if ( $n >= $this->maxPerPage ) { |
| 226 |
$nextRow = $row; |
| 227 |
break; |
| 228 |
} |
| 229 |
$title = Title::newFromRow( $row ); |
| 230 |
|
| 231 |
$this->linkCache->addGoodLinkObjFromRow( $title, $row ); |
| 232 |
$displayed = $title->getText(); |
| 233 |
|
| 234 |
if ( $this->stripPrefix && $prefixLength !== strlen( $displayed ) ) { |
| 235 |
$displayed = substr( $displayed, $prefixLength ); |
| 236 |
} |
| 237 |
$link = ( $title->isRedirect() ? '<div class="allpagesredirect">' : '' ) . |
| 238 |
$this->getLinkRenderer()->makeKnownLink( |
| 239 |
$title, |
| 240 |
$displayed |
| 241 |
) . |
| 242 |
( $title->isRedirect() ? '</div>' : '' ); |
| 243 |
|
| 244 |
$out .= "<li>$link</li>\n"; |
| 245 |
$n++; |
| 246 |
|
| 247 |
} |
| 248 |
$out .= Html::closeElement( 'ul' ); |
| 249 |
|
| 250 |
if ( $res->numRows() > 2 ) { |
| 251 |
|
| 252 |
|
| 253 |
$out = Html::rawElement( 'div', [ 'class' => 'mw-prefixindex-body' ], $out ); |
| 254 |
} |
| 255 |
} else { |
| 256 |
$out = ''; |
| 257 |
} |
| 258 |
} |
| 259 |
|
| 260 |
$output = $this->getOutput(); |
| 261 |
|
| 262 |
if ( $this->including() ) { |
| 263 |
|
| 264 |
|
| 265 |
$output->addHTML( $out ); |
| 266 |
return; |
| 267 |
} |
| 268 |
|
| 269 |
$topOut = $preparedHtmlForm->getHTML( false ); |
| 270 |
|
| 271 |
if ( $res && ( $n == $this->maxPerPage ) && $nextRow ) { |
| 272 |
$query = [ |
| 273 |
'from' => $nextRow->page_title, |
| 274 |
'prefix' => $prefix, |
| 275 |
'hideredirects' => $this->hideRedirects, |
| 276 |
'stripprefix' => $this->stripPrefix, |
| 277 |
]; |
| 278 |
|
| 279 |
if ( $namespace || $prefix == '' ) { |
| 280 |
|
| 281 |
|
| 282 |
$query['namespace'] = $namespace; |
| 283 |
} |
| 284 |
|
| 285 |
$nextLink = $this->getLinkRenderer()->makeKnownLink( |
| 286 |
$this->getPageTitle(), |
| 287 |
$this->msg( 'nextpage', str_replace( '_', ' ', $nextRow->page_title ) )->text(), |
| 288 |
[], |
| 289 |
$query |
| 290 |
); |
| 291 |
|
| 292 |
|
| 293 |
$topOut .= Html::rawElement( 'div', |
| 294 |
[ 'class' => 'mw-prefixindex-nav' ], |
| 295 |
$nextLink |
| 296 |
); |
| 297 |
|
| 298 |
|
| 299 |
$out .= "\n" . Html::element( 'hr' ) . |
| 300 |
Html::rawElement( |
| 301 |
'div', |
| 302 |
[ 'class' => 'mw-prefixindex-nav' ], |
| 303 |
$nextLink |
| 304 |
); |
| 305 |
|
| 306 |
} |
| 307 |
|
| 308 |
$output->addHTML( $topOut . $out ); |
| 309 |
} |
| 310 |
|
| 311 |
protected function getGroupName() { |
| 312 |
return 'pages'; |
| 313 |
} |
| 314 |
} |
| 315 |
|
| 316 |
|
| 317 |
|
| 318 |
|
| 319 |
|
| 320 |
class_alias( SpecialPrefixIndex::class, 'SpecialPrefixindex' ); |