| 1 |
<?php |
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
namespace MediaWiki\Category; |
| 23 |
|
| 24 |
use MediaWiki\Config\ServiceOptions; |
| 25 |
use MediaWiki\Linker\LinkTarget; |
| 26 |
use MediaWiki\MainConfigNames; |
| 27 |
use MediaWiki\Page\PageReference; |
| 28 |
use MediaWiki\Parser\Parser; |
| 29 |
use MediaWiki\Registration\ExtensionRegistry; |
| 30 |
use MediaWiki\Title\NamespaceInfo; |
| 31 |
use MediaWiki\Title\Title; |
| 32 |
use MediaWiki\Title\TitleParser; |
| 33 |
use Psr\Log\LoggerInterface; |
| 34 |
use Wikimedia\Parsoid\Core\ContentMetadataCollector; |
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
class TrackingCategories { |
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
public const CONSTRUCTOR_OPTIONS = [ |
| 48 |
MainConfigNames::TrackingCategories, |
| 49 |
MainConfigNames::EnableMagicLinks, |
| 50 |
]; |
| 51 |
|
| 52 |
|
| 53 |
private $options; |
| 54 |
|
| 55 |
|
| 56 |
private $namespaceInfo; |
| 57 |
|
| 58 |
|
| 59 |
private $titleParser; |
| 60 |
|
| 61 |
|
| 62 |
private $extensionRegistry; |
| 63 |
|
| 64 |
|
| 65 |
private $logger; |
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
private const CORE_TRACKING_CATEGORIES = [ |
| 71 |
'broken-file-category', |
| 72 |
'duplicate-args-category', |
| 73 |
'expansion-depth-exceeded-category', |
| 74 |
'expensive-parserfunction-category', |
| 75 |
'hidden-category-category', |
| 76 |
'index-category', |
| 77 |
'node-count-exceeded-category', |
| 78 |
'noindex-category', |
| 79 |
'nonnumeric-formatnum', |
| 80 |
'post-expand-template-argument-category', |
| 81 |
'post-expand-template-inclusion-category', |
| 82 |
'restricted-displaytitle-ignored', |
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
'template-equals-category', |
| 87 |
'template-loop-category', |
| 88 |
'unstrip-depth-category', |
| 89 |
'unstrip-size-category', |
| 90 |
'bad-language-code-category', |
| 91 |
'bad-double-underscore-category', |
| 92 |
'double-px-category', |
| 93 |
]; |
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
public function __construct( |
| 102 |
ServiceOptions $options, |
| 103 |
NamespaceInfo $namespaceInfo, |
| 104 |
TitleParser $titleParser, |
| 105 |
LoggerInterface $logger |
| 106 |
) { |
| 107 |
$options->assertRequiredOptions( self::CONSTRUCTOR_OPTIONS ); |
| 108 |
$this->options = $options; |
| 109 |
$this->namespaceInfo = $namespaceInfo; |
| 110 |
$this->titleParser = $titleParser; |
| 111 |
$this->logger = $logger; |
| 112 |
|
| 113 |
|
| 114 |
$this->extensionRegistry = ExtensionRegistry::getInstance(); |
| 115 |
} |
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
public function getTrackingCategories() { |
| 128 |
$categories = array_merge( |
| 129 |
self::CORE_TRACKING_CATEGORIES, |
| 130 |
$this->extensionRegistry->getAttribute( MainConfigNames::TrackingCategories ), |
| 131 |
$this->options->get( MainConfigNames::TrackingCategories ) |
| 132 |
); |
| 133 |
|
| 134 |
|
| 135 |
$enableMagicLinks = $this->options->get( MainConfigNames::EnableMagicLinks ); |
| 136 |
if ( $enableMagicLinks['ISBN'] ) { |
| 137 |
$categories[] = 'magiclink-tracking-isbn'; |
| 138 |
} |
| 139 |
if ( $enableMagicLinks['RFC'] ) { |
| 140 |
$categories[] = 'magiclink-tracking-rfc'; |
| 141 |
} |
| 142 |
if ( $enableMagicLinks['PMID'] ) { |
| 143 |
$categories[] = 'magiclink-tracking-pmid'; |
| 144 |
} |
| 145 |
|
| 146 |
$trackingCategories = []; |
| 147 |
foreach ( $categories as $catMsg ) { |
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
$msgObj = wfMessage( $catMsg )->inContentLanguage(); |
| 156 |
$allCats = []; |
| 157 |
$catMsgTitle = $this->titleParser->makeTitleValueSafe( NS_MEDIAWIKI, $catMsg ); |
| 158 |
if ( !$catMsgTitle ) { |
| 159 |
continue; |
| 160 |
} |
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
if ( strpos( $msgObj->plain(), '{{' ) !== false ) { |
| 165 |
$ns = $this->namespaceInfo->getValidNamespaces(); |
| 166 |
foreach ( $ns as $namesp ) { |
| 167 |
$tempTitle = $this->titleParser->makeTitleValueSafe( $namesp, $catMsg ); |
| 168 |
if ( !$tempTitle ) { |
| 169 |
continue; |
| 170 |
} |
| 171 |
|
| 172 |
|
| 173 |
$tempTitle = Title::newFromLinkTarget( $tempTitle ); |
| 174 |
$catName = $msgObj->page( $tempTitle )->text(); |
| 175 |
|
| 176 |
if ( $catName !== '-' ) { |
| 177 |
$catTitle = $this->titleParser->makeTitleValueSafe( NS_CATEGORY, $catName ); |
| 178 |
if ( $catTitle ) { |
| 179 |
$allCats[] = $catTitle; |
| 180 |
} |
| 181 |
} |
| 182 |
} |
| 183 |
} else { |
| 184 |
$catName = $msgObj->text(); |
| 185 |
|
| 186 |
if ( $catName !== '-' ) { |
| 187 |
$catTitle = $this->titleParser->makeTitleValueSafe( NS_CATEGORY, $catName ); |
| 188 |
if ( $catTitle ) { |
| 189 |
$allCats[] = $catTitle; |
| 190 |
} |
| 191 |
} |
| 192 |
} |
| 193 |
$trackingCategories[$catMsg] = [ |
| 194 |
'cats' => $allCats, |
| 195 |
'msg' => $catMsgTitle, |
| 196 |
]; |
| 197 |
} |
| 198 |
|
| 199 |
return $trackingCategories; |
| 200 |
} |
| 201 |
|
| 202 |
|
| 203 |
|
| 204 |
|
| 205 |
|
| 206 |
|
| 207 |
|
| 208 |
|
| 209 |
|
| 210 |
public function resolveTrackingCategory( string $msg, ?PageReference $contextPage ): ?LinkTarget { |
| 211 |
if ( !$contextPage ) { |
| 212 |
$this->logger->debug( "Not adding tracking category $msg to missing page!" ); |
| 213 |
return null; |
| 214 |
} |
| 215 |
|
| 216 |
if ( $contextPage->getNamespace() === NS_SPECIAL ) { |
| 217 |
$this->logger->debug( "Not adding tracking category $msg to special page!" ); |
| 218 |
return null; |
| 219 |
} |
| 220 |
|
| 221 |
|
| 222 |
|
| 223 |
$cat = wfMessage( $msg ) |
| 224 |
->page( $contextPage ) |
| 225 |
->inContentLanguage() |
| 226 |
->text(); |
| 227 |
|
| 228 |
|
| 229 |
if ( $cat === '-' ) { |
| 230 |
return null; |
| 231 |
} |
| 232 |
|
| 233 |
$containerCategory = $this->titleParser->makeTitleValueSafe( NS_CATEGORY, $cat ); |
| 234 |
if ( $containerCategory === null ) { |
| 235 |
$this->logger->debug( "[[MediaWiki:$msg]] is not a valid title!" ); |
| 236 |
return null; |
| 237 |
} |
| 238 |
return $containerCategory; |
| 239 |
} |
| 240 |
|
| 241 |
|
| 242 |
|
| 243 |
|
| 244 |
|
| 245 |
|
| 246 |
|
| 247 |
|
| 248 |
|
| 249 |
|
| 250 |
|
| 251 |
|
| 252 |
|
| 253 |
|
| 254 |
|
| 255 |
|
| 256 |
|
| 257 |
|
| 258 |
public function addTrackingCategory( |
| 259 |
ContentMetadataCollector $parserOutput, |
| 260 |
string $msg, |
| 261 |
?PageReference $contextPage |
| 262 |
): bool { |
| 263 |
$categoryPage = $this->resolveTrackingCategory( $msg, $contextPage ); |
| 264 |
if ( $categoryPage === null ) { |
| 265 |
return false; |
| 266 |
} |
| 267 |
$parserOutput->addCategory( $categoryPage ); |
| 268 |
return true; |
| 269 |
} |
| 270 |
} |
| 271 |
|
| 272 |
|
| 273 |
class_alias( TrackingCategories::class, 'TrackingCategories' ); |