Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 242 |
|
0.00% |
0 / 9 |
CRAP | |
0.00% |
0 / 1 |
| CategoryTree | |
0.00% |
0 / 242 |
|
0.00% |
0 / 9 |
5256 | |
0.00% |
0 / 1 |
| __construct | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| setHeaders | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
2 | |||
| getTag | |
0.00% |
0 / 18 |
|
0.00% |
0 / 1 |
30 | |||
| renderChildren | |
0.00% |
0 / 84 |
|
0.00% |
0 / 1 |
812 | |||
| renderParents | |
0.00% |
0 / 24 |
|
0.00% |
0 / 1 |
6 | |||
| renderNode | |
0.00% |
0 / 6 |
|
0.00% |
0 / 1 |
20 | |||
| renderNodeInfo | |
0.00% |
0 / 70 |
|
0.00% |
0 / 1 |
306 | |||
| createCountString | |
0.00% |
0 / 29 |
|
0.00% |
0 / 1 |
90 | |||
| makeTitle | |
0.00% |
0 / 8 |
|
0.00% |
0 / 1 |
30 | |||
| 1 | <?php |
| 2 | /** |
| 3 | * © 2006-2007 Daniel Kinzler |
| 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 Extensions |
| 22 | * @author Daniel Kinzler, brightbyte.de |
| 23 | */ |
| 24 | |
| 25 | namespace MediaWiki\Extension\CategoryTree; |
| 26 | |
| 27 | use MediaWiki\Category\Category; |
| 28 | use MediaWiki\Config\Config; |
| 29 | use MediaWiki\Context\IContextSource; |
| 30 | use MediaWiki\Context\RequestContext; |
| 31 | use MediaWiki\Deferred\LinksUpdate\CategoryLinksTable; |
| 32 | use MediaWiki\Extension\Translate\PageTranslation\TranslatablePage; |
| 33 | use MediaWiki\Html\Html; |
| 34 | use MediaWiki\Language\Language; |
| 35 | use MediaWiki\Linker\LinkRenderer; |
| 36 | use MediaWiki\Output\OutputPage; |
| 37 | use MediaWiki\Page\LinkBatchFactory; |
| 38 | use MediaWiki\Registration\ExtensionRegistry; |
| 39 | use MediaWiki\SpecialPage\SpecialPage; |
| 40 | use MediaWiki\Title\Title; |
| 41 | use Wikimedia\Rdbms\IConnectionProvider; |
| 42 | |
| 43 | /** |
| 44 | * Core functions for the CategoryTree extension, which displays the category structure of a wiki |
| 45 | */ |
| 46 | class CategoryTree { |
| 47 | public readonly OptionManager $optionManager; |
| 48 | |
| 49 | public function __construct( |
| 50 | array $options, |
| 51 | private readonly Config $config, |
| 52 | private readonly Language $contLang, |
| 53 | private readonly IConnectionProvider $dbProvider, |
| 54 | private readonly LinkBatchFactory $linkBatchFactory, |
| 55 | private readonly LinkRenderer $linkRenderer, |
| 56 | ) { |
| 57 | $this->optionManager = new OptionManager( $options, $config ); |
| 58 | } |
| 59 | |
| 60 | /** |
| 61 | * Add ResourceLoader modules to the OutputPage object |
| 62 | */ |
| 63 | public static function setHeaders( OutputPage $outputPage ): void { |
| 64 | # Add the modules |
| 65 | $outputPage->addModuleStyles( 'ext.categoryTree.styles' ); |
| 66 | $outputPage->addModules( 'ext.categoryTree' ); |
| 67 | } |
| 68 | |
| 69 | /** |
| 70 | * Custom tag implementation. This is called by Hooks::parserHook, which is used to |
| 71 | * load CategoryTree on demand. |
| 72 | * @param string $category |
| 73 | * @param bool $hideroot |
| 74 | * @param array $attr |
| 75 | * @param int $depth |
| 76 | * @return string |
| 77 | */ |
| 78 | public function getTag( string $category, bool $hideroot = false, array $attr = [], |
| 79 | int $depth = 1 |
| 80 | ): string { |
| 81 | $title = self::makeTitle( $category ); |
| 82 | if ( !$title ) { |
| 83 | return ''; |
| 84 | } |
| 85 | |
| 86 | if ( isset( $attr['class'] ) ) { |
| 87 | $attr['class'] .= ' CategoryTreeTag'; |
| 88 | } else { |
| 89 | $attr['class'] = 'CategoryTreeTag'; |
| 90 | } |
| 91 | |
| 92 | $attr['data-ct-options'] = $this->optionManager->getOptionsAsJsStructure(); |
| 93 | |
| 94 | if ( !$title->getArticleID() ) { |
| 95 | $html = Html::rawElement( 'span', [ 'class' => 'CategoryTreeNotice' ], |
| 96 | wfMessage( 'categorytree-not-found' ) |
| 97 | ->plaintextParams( $category ) |
| 98 | ->parse() |
| 99 | ); |
| 100 | } else { |
| 101 | if ( !$hideroot ) { |
| 102 | $html = $this->renderNode( $title, $depth ); |
| 103 | } else { |
| 104 | $html = $this->renderChildren( $title, $depth ); |
| 105 | } |
| 106 | } |
| 107 | |
| 108 | return Html::rawElement( 'div', $attr, $html ); |
| 109 | } |
| 110 | |
| 111 | /** |
| 112 | * Returns a string with an HTML representation of the children of the given category. |
| 113 | */ |
| 114 | public function renderChildren( Title $title, int $depth = 1 ): string { |
| 115 | if ( !$title->inNamespace( NS_CATEGORY ) ) { |
| 116 | // Non-categories can't have children. :) |
| 117 | return ''; |
| 118 | } |
| 119 | |
| 120 | $dbr = $this->dbProvider->getReplicaDatabase(); |
| 121 | $categoryLinksDbr = $this->dbProvider->getReplicaDatabase( CategoryLinksTable::VIRTUAL_DOMAIN ); |
| 122 | |
| 123 | $inverse = $this->optionManager->isInverse(); |
| 124 | $mode = $this->optionManager->getOption( 'mode' ); |
| 125 | $namespaces = $this->optionManager->getOption( 'namespaces' ); |
| 126 | |
| 127 | $queryBuilder = $categoryLinksDbr->newSelectQueryBuilder() |
| 128 | ->select( [ |
| 129 | 'page_id', 'page_namespace', 'page_title', 'page_is_redirect', |
| 130 | 'page_len', 'page_latest', 'cl_from', 'lt_title' |
| 131 | ] ) |
| 132 | ->orderBy( [ 'cl_type', 'cl_sortkey' ] ) |
| 133 | ->limit( $this->config->get( 'CategoryTreeMaxChildren' ) ) |
| 134 | ->caller( __METHOD__ ); |
| 135 | |
| 136 | if ( $inverse ) { |
| 137 | $queryBuilder->from( 'categorylinks' ); |
| 138 | } else { |
| 139 | $queryBuilder->from( 'page' ) |
| 140 | ->join( 'categorylinks', null, 'cl_from = page_id' ); |
| 141 | } |
| 142 | |
| 143 | $queryBuilder->join( 'linktarget', null, [ 'lt_id=cl_target_id' ] ); |
| 144 | $queryBuilder->where( [ 'lt_namespace' => NS_CATEGORY ] ); |
| 145 | |
| 146 | if ( $inverse ) { |
| 147 | $queryBuilder |
| 148 | ->leftJoin( 'page', null, [ |
| 149 | 'lt_title = page_title', 'page_namespace' => NS_CATEGORY |
| 150 | ] ) |
| 151 | ->where( [ 'cl_from' => $title->getArticleID() ] ); |
| 152 | } else { |
| 153 | $queryBuilder->where( [ 'lt_title' => $title->getDBkey() ] ); |
| 154 | $queryBuilder->useIndex( [ 'categorylinks' => 'cl_sortkey_id' ] ); |
| 155 | |
| 156 | # namespace filter. |
| 157 | if ( $namespaces ) { |
| 158 | // NOTE: we assume that the $namespaces array contains only integers! |
| 159 | // decodeNamepsaces makes it so. |
| 160 | $queryBuilder->where( [ 'page_namespace' => $namespaces ] ); |
| 161 | } elseif ( $mode !== CategoryTreeMode::ALL ) { |
| 162 | if ( $mode === CategoryTreeMode::PAGES ) { |
| 163 | $queryBuilder->where( [ 'cl_type' => [ 'page', 'subcat' ] ] ); |
| 164 | } else { |
| 165 | $queryBuilder->where( [ 'cl_type' => 'subcat' ] ); |
| 166 | } |
| 167 | } |
| 168 | } |
| 169 | |
| 170 | $res = $queryBuilder->fetchResultSet(); |
| 171 | |
| 172 | // fetch member count if possible |
| 173 | $doCount = !$inverse && $this->config->get( 'CategoryTreeUseCategoryTable' ); |
| 174 | |
| 175 | $categoryData = []; |
| 176 | if ( $doCount ) { |
| 177 | $categoryTitles = []; |
| 178 | foreach ( $res as $row ) { |
| 179 | if ( (int)$row->page_namespace === NS_CATEGORY ) { |
| 180 | $categoryTitles[] = $row->page_title; |
| 181 | } |
| 182 | } |
| 183 | |
| 184 | if ( $categoryTitles !== [] ) { |
| 185 | $categoryRes = $dbr->newSelectQueryBuilder() |
| 186 | ->select( [ 'cat_id', 'cat_title', 'cat_subcats', 'cat_pages', 'cat_files' ] ) |
| 187 | ->from( 'category' ) |
| 188 | ->where( [ 'cat_title' => $categoryTitles ] ) |
| 189 | ->caller( __METHOD__ ) |
| 190 | ->fetchResultSet(); |
| 191 | |
| 192 | foreach ( $categoryRes as $catRow ) { |
| 193 | $categoryData[$catRow->cat_title] = $catRow; |
| 194 | } |
| 195 | } |
| 196 | } |
| 197 | |
| 198 | // collect categories separately from other pages |
| 199 | $categories = ''; |
| 200 | $other = ''; |
| 201 | $suppressTranslations = OptionManager::decodeBoolean( |
| 202 | $this->optionManager->getOption( 'notranslations' ) |
| 203 | ) && ExtensionRegistry::getInstance()->isLoaded( 'Translate' ); |
| 204 | |
| 205 | if ( $suppressTranslations ) { |
| 206 | $lb = $this->linkBatchFactory->newLinkBatch(); |
| 207 | foreach ( $res as $row ) { |
| 208 | $title = Title::newFromText( $row->page_title, $row->page_namespace ); |
| 209 | // Page name could have slashes, check the subpage for valid language built-in codes |
| 210 | if ( $title && $title->getSubpageText() ) { |
| 211 | $lb->addObj( $title->getBaseTitle() ); |
| 212 | } |
| 213 | } |
| 214 | |
| 215 | $lb->execute(); |
| 216 | } |
| 217 | |
| 218 | foreach ( $res as $row ) { |
| 219 | if ( $suppressTranslations ) { |
| 220 | $title = Title::newFromRow( $row ); |
| 221 | $baseTitle = $title->getBaseTitle(); |
| 222 | $page = TranslatablePage::isTranslationPage( $title ); |
| 223 | |
| 224 | if ( ( $page instanceof TranslatablePage ) && $baseTitle->exists() ) { |
| 225 | // T229265: Render only the default pages created and ignore their |
| 226 | // translations. |
| 227 | continue; |
| 228 | } |
| 229 | } |
| 230 | |
| 231 | // NOTE: in inverse mode, the page record may be null, because we use a right join. |
| 232 | // happens for categories with no category page (red cat links) |
| 233 | if ( $inverse && $row->page_title === null ) { |
| 234 | $t = Title::makeTitle( NS_CATEGORY, $row->lt_title ); |
| 235 | } else { |
| 236 | // TODO: translation support; ideally added to Title object |
| 237 | $t = Title::newFromRow( $row ); |
| 238 | } |
| 239 | |
| 240 | $cat = null; |
| 241 | |
| 242 | if ( $doCount && (int)$row->page_namespace === NS_CATEGORY ) { |
| 243 | $catRow = $categoryData[$row->page_title] ?? null; |
| 244 | if ( $catRow ) { |
| 245 | $cat = Category::newFromRow( $catRow, $t ); |
| 246 | } |
| 247 | } |
| 248 | |
| 249 | $s = $this->renderNodeInfo( $t, $cat, $depth - 1 ); |
| 250 | |
| 251 | if ( (int)$row->page_namespace === NS_CATEGORY ) { |
| 252 | $categories .= $s; |
| 253 | } else { |
| 254 | $other .= $s; |
| 255 | } |
| 256 | } |
| 257 | |
| 258 | return $categories . $other; |
| 259 | } |
| 260 | |
| 261 | /** |
| 262 | * Returns a string with an HTML representation of the parents of the given category. |
| 263 | */ |
| 264 | public function renderParents( Title $title ): string { |
| 265 | $dbr = $this->dbProvider->getReplicaDatabase( CategoryLinksTable::VIRTUAL_DOMAIN ); |
| 266 | |
| 267 | $res = $dbr->newSelectQueryBuilder() |
| 268 | ->select( [ 'lt_title' ] ) |
| 269 | ->from( 'categorylinks' ) |
| 270 | ->join( 'linktarget', null, [ 'lt_id=cl_target_id' ] ) |
| 271 | ->where( [ 'cl_from' => $title->getArticleID() ] ) |
| 272 | ->andWhere( [ 'lt_namespace' => NS_CATEGORY ] ) |
| 273 | ->limit( $this->config->get( 'CategoryTreeMaxChildren' ) ) |
| 274 | ->orderBy( 'lt_title' ) |
| 275 | ->caller( __METHOD__ ) |
| 276 | ->fetchResultSet(); |
| 277 | |
| 278 | $special = SpecialPage::getTitleFor( 'CategoryTree' ); |
| 279 | |
| 280 | $s = []; |
| 281 | |
| 282 | foreach ( $res as $row ) { |
| 283 | $t = Title::makeTitle( NS_CATEGORY, $row->lt_title ); |
| 284 | |
| 285 | $s[] = Html::rawElement( 'span', [ 'class' => 'CategoryTreeItem' ], |
| 286 | $this->linkRenderer->makeLink( |
| 287 | $special, |
| 288 | $t->getText(), |
| 289 | [ 'class' => 'CategoryTreeLabel' ], |
| 290 | [ 'target' => $t->getDBkey() ] + $this->optionManager->getOptions() |
| 291 | ) |
| 292 | ); |
| 293 | } |
| 294 | |
| 295 | return implode( wfMessage( 'pipe-separator' )->escaped(), $s ); |
| 296 | } |
| 297 | |
| 298 | /** |
| 299 | * Returns a string with a HTML represenation of the given page. |
| 300 | * @param Title $title |
| 301 | * @param int $children |
| 302 | * @return string |
| 303 | */ |
| 304 | public function renderNode( Title $title, int $children = 0 ): string { |
| 305 | if ( $this->config->get( 'CategoryTreeUseCategoryTable' ) |
| 306 | && $title->inNamespace( NS_CATEGORY ) |
| 307 | && !$this->optionManager->isInverse() |
| 308 | ) { |
| 309 | $cat = Category::newFromTitle( $title ); |
| 310 | } else { |
| 311 | $cat = null; |
| 312 | } |
| 313 | |
| 314 | return $this->renderNodeInfo( $title, $cat, $children ); |
| 315 | } |
| 316 | |
| 317 | /** |
| 318 | * Returns a string with a HTML represenation of the given page. |
| 319 | * $info must be an associative array, containing at least a Title object under the 'title' key. |
| 320 | */ |
| 321 | public function renderNodeInfo( Title $title, ?Category $cat = null, int $children = 0 ): string { |
| 322 | $mode = $this->optionManager->getOption( 'mode' ); |
| 323 | |
| 324 | $isInCatNS = $title->inNamespace( NS_CATEGORY ); |
| 325 | $key = $title->getDBkey(); |
| 326 | |
| 327 | $hideprefix = $this->optionManager->getOption( 'hideprefix' ); |
| 328 | |
| 329 | if ( $hideprefix === CategoryTreeHidePrefix::ALWAYS ) { |
| 330 | $hideprefix = true; |
| 331 | } elseif ( $hideprefix === CategoryTreeHidePrefix::AUTO ) { |
| 332 | $hideprefix = ( $mode === CategoryTreeMode::CATEGORIES ); |
| 333 | } elseif ( $hideprefix === CategoryTreeHidePrefix::CATEGORIES ) { |
| 334 | $hideprefix = $isInCatNS; |
| 335 | } else { |
| 336 | $hideprefix = true; |
| 337 | } |
| 338 | |
| 339 | // when showing only categories, omit namespace in label unless we explicitely defined the |
| 340 | // configuration setting |
| 341 | // patch contributed by Manuel Schneider <manuel.schneider@wikimedia.ch>, Bug 8011 |
| 342 | if ( $hideprefix ) { |
| 343 | $label = $title->getText(); |
| 344 | } else { |
| 345 | $label = $title->getPrefixedText(); |
| 346 | } |
| 347 | |
| 348 | $link = Html::rawElement( 'bdi', [ 'dir' => $this->contLang->getDir() ], |
| 349 | $this->linkRenderer->makeLink( $title, $label ) ); |
| 350 | |
| 351 | $count = false; |
| 352 | $s = ''; |
| 353 | |
| 354 | # NOTE: things in CategoryTree.js rely on the exact order of tags! |
| 355 | # Specifically, the CategoryTreeChildren div must be the first |
| 356 | # sibling with nodeName = DIV of the grandparent of the expland link. |
| 357 | |
| 358 | $s .= Html::openElement( 'div', [ 'class' => 'CategoryTreeSection' ] ); |
| 359 | $s .= Html::openElement( 'div', [ 'class' => 'CategoryTreeItem' ] ); |
| 360 | |
| 361 | $attr = [ 'class' => 'CategoryTreeBullet' ]; |
| 362 | |
| 363 | if ( $isInCatNS ) { |
| 364 | if ( $cat ) { |
| 365 | if ( $mode === CategoryTreeMode::CATEGORIES ) { |
| 366 | $count = $cat->getSubcatCount(); |
| 367 | } elseif ( $mode === CategoryTreeMode::PAGES ) { |
| 368 | $count = $cat->getMemberCount() - $cat->getFileCount(); |
| 369 | } else { |
| 370 | $count = $cat->getMemberCount(); |
| 371 | } |
| 372 | } |
| 373 | if ( $count === 0 ) { |
| 374 | $bullet = ''; |
| 375 | $attr['class'] = 'CategoryTreeEmptyBullet'; |
| 376 | } else { |
| 377 | $linkattr = [ |
| 378 | 'class' => 'CategoryTreeToggle', |
| 379 | 'data-mw-interface' => '', |
| 380 | 'data-ct-title' => $key, |
| 381 | ]; |
| 382 | |
| 383 | if ( $children === 0 ) { |
| 384 | $linkattr['aria-expanded'] = 'false'; |
| 385 | } else { |
| 386 | $linkattr['data-ct-loaded'] = true; |
| 387 | $linkattr['aria-expanded'] = 'true'; |
| 388 | } |
| 389 | |
| 390 | $bullet = Html::element( 'a', $linkattr ) . ' '; |
| 391 | } |
| 392 | } else { |
| 393 | $bullet = ''; |
| 394 | $attr['class'] = 'CategoryTreePageBullet'; |
| 395 | } |
| 396 | $s .= Html::rawElement( 'span', $attr, $bullet ) . ' '; |
| 397 | |
| 398 | $s .= $link; |
| 399 | |
| 400 | if ( $count !== false && $this->optionManager->getOption( 'showcount' ) ) { |
| 401 | $s .= self::createCountString( RequestContext::getMain(), $cat, $count ); |
| 402 | } |
| 403 | |
| 404 | $s .= Html::closeElement( 'div' ); |
| 405 | $s .= Html::openElement( |
| 406 | 'div', |
| 407 | [ |
| 408 | 'class' => 'CategoryTreeChildren', |
| 409 | 'style' => $children === 0 ? 'display:none' : null |
| 410 | ] |
| 411 | ); |
| 412 | |
| 413 | if ( $isInCatNS && $children > 0 ) { |
| 414 | $children = $this->renderChildren( $title, $children ); |
| 415 | if ( $children === '' ) { |
| 416 | $msg = match ( $mode ) { |
| 417 | CategoryTreeMode::CATEGORIES => 'categorytree-no-subcategories', |
| 418 | CategoryTreeMode::PAGES => 'categorytree-no-pages', |
| 419 | CategoryTreeMode::PARENTS => 'categorytree-no-parent-categories', |
| 420 | default => 'categorytree-nothing-found', |
| 421 | }; |
| 422 | $children = Html::element( 'i', [ 'class' => 'CategoryTreeNotice' ], |
| 423 | wfMessage( $msg )->text() |
| 424 | ); |
| 425 | } |
| 426 | $s .= $children; |
| 427 | } |
| 428 | |
| 429 | $s .= Html::closeElement( 'div' ) . Html::closeElement( 'div' ); |
| 430 | |
| 431 | return $s; |
| 432 | } |
| 433 | |
| 434 | /** |
| 435 | * Create a string which format the page, subcat and file counts of a category |
| 436 | */ |
| 437 | public static function createCountString( IContextSource $context, ?Category $cat, |
| 438 | int $countMode |
| 439 | ): string { |
| 440 | $allCount = $cat ? $cat->getMemberCount() : 0; |
| 441 | $subcatCount = $cat ? $cat->getSubcatCount() : 0; |
| 442 | $fileCount = $cat ? $cat->getFileCount() : 0; |
| 443 | $pages = $cat ? $cat->getPageCount( Category::COUNT_CONTENT_PAGES ) : 0; |
| 444 | |
| 445 | $attr = [ |
| 446 | 'title' => $context->msg( 'categorytree-member-counts' ) |
| 447 | ->numParams( $subcatCount, $pages, $fileCount, $allCount, $countMode )->text(), |
| 448 | # numbers and commas get messed up in a mixed dir env |
| 449 | 'dir' => $context->getLanguage()->getDir() |
| 450 | ]; |
| 451 | |
| 452 | # Create a list of category members with only non-zero member counts |
| 453 | $memberNums = []; |
| 454 | if ( $subcatCount ) { |
| 455 | $memberNums[] = $context->msg( 'categorytree-num-categories' ) |
| 456 | ->numParams( $subcatCount )->text(); |
| 457 | } |
| 458 | if ( $pages ) { |
| 459 | $memberNums[] = $context->msg( 'categorytree-num-pages' )->numParams( $pages )->text(); |
| 460 | } |
| 461 | if ( $fileCount ) { |
| 462 | $memberNums[] = $context->msg( 'categorytree-num-files' ) |
| 463 | ->numParams( $fileCount )->text(); |
| 464 | } |
| 465 | $memberNumsShort = $memberNums |
| 466 | ? $context->getLanguage()->commaList( $memberNums ) |
| 467 | : $context->msg( 'categorytree-num-empty' )->text(); |
| 468 | |
| 469 | # Only $5 is actually used in the default message. |
| 470 | # Other arguments can be used in a customized message. |
| 471 | return ' ' . Html::rawElement( |
| 472 | 'span', |
| 473 | $attr, |
| 474 | $context->msg( 'categorytree-member-num' ) |
| 475 | // Do not use numParams on params 1-4, as they are only used for customisation. |
| 476 | ->params( $subcatCount, $pages, $fileCount, $allCount, $memberNumsShort ) |
| 477 | ->escaped() |
| 478 | ); |
| 479 | } |
| 480 | |
| 481 | /** |
| 482 | * Creates a Title object from a user provided (and thus unsafe) string |
| 483 | */ |
| 484 | public static function makeTitle( string $title ): ?Title { |
| 485 | $title = trim( $title ); |
| 486 | |
| 487 | if ( $title === '' ) { |
| 488 | return null; |
| 489 | } |
| 490 | |
| 491 | # The title must be in the category namespace |
| 492 | # Ignore a leading Category: if there is one |
| 493 | $t = Title::newFromText( $title, NS_CATEGORY ); |
| 494 | if ( !$t || !$t->inNamespace( NS_CATEGORY ) || $t->isExternal() ) { |
| 495 | // If we were given something like "Wikipedia:Foo" or "Template:", |
| 496 | // try it again but forced. |
| 497 | $title = "Category:$title"; |
| 498 | $t = Title::newFromText( $title ); |
| 499 | } |
| 500 | return $t; |
| 501 | } |
| 502 | } |