| 1 |
<?php |
| 2 |
|
| 3 |
s |
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
namespace MediaWiki\Extension\CategoryTree; |
| 26 |
|
| 27 |
use MediaWiki\Config\Config; |
| 28 |
use MediaWiki\Context\RequestContext; |
| 29 |
use MediaWiki\Hook\CategoryViewer__doCategoryQueryHook; |
| 30 |
use MediaWiki\Hook\CategoryViewer__generateLinkHook; |
| 31 |
use MediaWiki\Hook\ParserFirstCallInitHook; |
| 32 |
use MediaWiki\Hook\SkinBuildSidebarHook; |
| 33 |
use MediaWiki\Hook\SpecialTrackingCategories__generateCatLinkHook; |
| 34 |
use MediaWiki\Hook\SpecialTrackingCategories__preprocessHook; |
| 35 |
use MediaWiki\Html\Html; |
| 36 |
use MediaWiki\Linker\LinkRenderer; |
| 37 |
use MediaWiki\Linker\LinkTarget; |
| 38 |
use MediaWiki\Output\Hook\OutputPageRenderCategoryLinkHook; |
| 39 |
use MediaWiki\Output\OutputPage; |
| 40 |
use MediaWiki\Page\ProperPageIdentity; |
| 41 |
use MediaWiki\Parser\Parser; |
| 42 |
use MediaWiki\Parser\PPFrame; |
| 43 |
use MediaWiki\Parser\Sanitizer; |
| 44 |
use MediaWiki\ResourceLoader as RL; |
| 45 |
use MediaWiki\SpecialPage\SpecialPage; |
| 46 |
use MediaWiki\Title\Title; |
| 47 |
use MediaWiki\Title\TitleFormatter; |
| 48 |
use Skin; |
| 49 |
use Wikimedia\Rdbms\IConnectionProvider; |
| 50 |
use Wikimedia\Rdbms\IResultWrapper; |
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
class Hooks implements |
| 59 |
SpecialTrackingCategories__preprocessHook, |
| 60 |
SpecialTrackingCategories__generateCatLinkHook, |
| 61 |
SkinBuildSidebarHook, |
| 62 |
ParserFirstCallInitHook, |
| 63 |
OutputPageRenderCategoryLinkHook, |
| 64 |
CategoryViewer__doCategoryQueryHook, |
| 65 |
CategoryViewer__generateLinkHook |
| 66 |
{ |
| 67 |
private CategoryCache $categoryCache; |
| 68 |
private Config $config; |
| 69 |
private IConnectionProvider $dbProvider; |
| 70 |
private LinkRenderer $linkRenderer; |
| 71 |
private TitleFormatter $titleFormatter; |
| 72 |
|
| 73 |
public function __construct( |
| 74 |
CategoryCache $categoryCache, |
| 75 |
Config $config, |
| 76 |
IConnectionProvider $dbProvider, |
| 77 |
LinkRenderer $linkRenderer, |
| 78 |
TitleFormatter $titleFormatter |
| 79 |
) { |
| 80 |
$this->categoryCache = $categoryCache; |
| 81 |
$this->config = $config; |
| 82 |
$this->dbProvider = $dbProvider; |
| 83 |
$this->linkRenderer = $linkRenderer; |
| 84 |
$this->titleFormatter = $titleFormatter; |
| 85 |
} |
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
public function onParserFirstCallInit( $parser ) { |
| 91 |
if ( !$this->config->get( 'CategoryTreeAllowTag' ) ) { |
| 92 |
return; |
| 93 |
} |
| 94 |
$parser->setHook( 'categorytree', [ $this, 'parserHook' ] ); |
| 95 |
$parser->setFunctionHook( 'categorytree', [ $this, 'parserFunction' ] ); |
| 96 |
} |
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
public function parserFunction( Parser $parser, ...$params ) { |
| 106 |
|
| 107 |
if ( !$params ) { |
| 108 |
|
| 109 |
return ''; |
| 110 |
} |
| 111 |
$cat = array_shift( $params ); |
| 112 |
|
| 113 |
|
| 114 |
$argv = []; |
| 115 |
foreach ( $params as $p ) { |
| 116 |
if ( preg_match( '/^\s*(\S.*?)\s*=\s*(.*?)\s*$/', $p, $m ) ) { |
| 117 |
$k = $m[1]; |
| 118 |
|
| 119 |
$v = preg_replace( '/^"\s*(.*?)\s*"$/', '$1', $m[2] ); |
| 120 |
} else { |
| 121 |
$k = trim( $p ); |
| 122 |
$v = true; |
| 123 |
} |
| 124 |
|
| 125 |
$argv[$k] = $v; |
| 126 |
} |
| 127 |
|
| 128 |
if ( $parser->getOutputType() === Parser::OT_PREPROCESS ) { |
| 129 |
return Html::rawElement( 'categorytree', $argv, $cat ); |
| 130 |
} else { |
| 131 |
|
| 132 |
$html = $this->parserHook( $cat, $argv, $parser ); |
| 133 |
return [ $html, 'noparse' => true, 'isHTML' => true ]; |
| 134 |
} |
| 135 |
} |
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
private function getCategorySidebarBox() { |
| 142 |
if ( !$this->config->get( 'CategoryTreeSidebarRoot' ) ) { |
| 143 |
return false; |
| 144 |
} |
| 145 |
return $this->parserHook( |
| 146 |
$this->config->get( 'CategoryTreeSidebarRoot' ), |
| 147 |
$this->config->get( 'CategoryTreeSidebarOptions' ) |
| 148 |
); |
| 149 |
} |
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
public function onSkinBuildSidebar( $skin, &$sidebar ) { |
| 158 |
$html = $this->getCategorySidebarBox(); |
| 159 |
if ( $html ) { |
| 160 |
$sidebar['categorytree-portlet'] = []; |
| 161 |
CategoryTree::setHeaders( $skin->getOutput() ); |
| 162 |
} |
| 163 |
} |
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
public function onSkinAfterPortlet( $skin, $portlet, &$html ) { |
| 173 |
if ( $portlet === 'categorytree-portlet' ) { |
| 174 |
$box = $this->getCategorySidebarBox(); |
| 175 |
if ( $box ) { |
| 176 |
$html .= $box; |
| 177 |
} |
| 178 |
} |
| 179 |
} |
| 180 |
|
| 181 |
|
| 182 |
|
| 183 |
|
| 184 |
|
| 185 |
|
| 186 |
|
| 187 |
|
| 188 |
|
| 189 |
|
| 190 |
public function parserHook( |
| 191 |
$cat, |
| 192 |
array $argv, |
| 193 |
?Parser $parser = null, |
| 194 |
?PPFrame $frame = null |
| 195 |
) { |
| 196 |
if ( $parser ) { |
| 197 |
$parserOutput = $parser->getOutput(); |
| 198 |
$parserOutput->addModuleStyles( [ 'ext.categoryTree.styles' ] ); |
| 199 |
$parserOutput->addModules( [ 'ext.categoryTree' ] ); |
| 200 |
|
| 201 |
$disableCache = $this->config->get( 'CategoryTreeDisableCache' ); |
| 202 |
if ( $disableCache === true ) { |
| 203 |
$parserOutput->updateCacheExpiry( 0 ); |
| 204 |
} elseif ( is_int( $disableCache ) ) { |
| 205 |
$parserOutput->updateCacheExpiry( $disableCache ); |
| 206 |
} |
| 207 |
} |
| 208 |
|
| 209 |
$ct = new CategoryTree( $argv, $this->config, $this->dbProvider, $this->linkRenderer ); |
| 210 |
|
| 211 |
$attr = Sanitizer::validateTagAttributes( $argv, 'div' ); |
| 212 |
|
| 213 |
$hideroot = isset( $argv['hideroot'] ) |
| 214 |
? OptionManager::decodeBoolean( $argv['hideroot'] ) : false; |
| 215 |
$onlyroot = isset( $argv['onlyroot'] ) |
| 216 |
? OptionManager::decodeBoolean( $argv['onlyroot'] ) : false; |
| 217 |
$depthArg = isset( $argv['depth'] ) ? (int)$argv['depth'] : 1; |
| 218 |
|
| 219 |
$depth = $ct->optionManager->capDepth( $depthArg ); |
| 220 |
if ( $onlyroot ) { |
| 221 |
$depth = 0; |
| 222 |
$message = '<span class="error">' |
| 223 |
. wfMessage( 'categorytree-onlyroot-message' )->inContentLanguage()->parse() |
| 224 |
. '</span>'; |
| 225 |
if ( $parser ) { |
| 226 |
$parser->getOutput()->addWarningMsg( 'categorytree-deprecation-warning' ); |
| 227 |
$parser->addTrackingCategory( 'categorytree-deprecation-category' ); |
| 228 |
} |
| 229 |
} else { |
| 230 |
$message = ''; |
| 231 |
} |
| 232 |
|
| 233 |
return $message . |
| 234 |
$ct->getTag( $cat ?? '', $hideroot, $attr, $depth ); |
| 235 |
} |
| 236 |
|
| 237 |
|
| 238 |
|
| 239 |
|
| 240 |
|
| 241 |
|
| 242 |
|
| 243 |
|
| 244 |
|
| 245 |
public function onOutputPageRenderCategoryLink( |
| 246 |
OutputPage $out, |
| 247 |
ProperPageIdentity $categoryTitle, |
| 248 |
string $text, |
| 249 |
?string &$link |
| 250 |
): void { |
| 251 |
if ( !$this->config->get( 'CategoryTreeHijackPageCategories' ) ) { |
| 252 |
|
| 253 |
return; |
| 254 |
} |
| 255 |
if ( !$categoryTitle->exists() ) { |
| 256 |
|
| 257 |
return; |
| 258 |
} |
| 259 |
|
| 260 |
CategoryTree::setHeaders( $out ); |
| 261 |
|
| 262 |
$options = $this->config->get( 'CategoryTreePageCategoryOptions' ); |
| 263 |
$link = $this->parserHook( |
| 264 |
$this->titleFormatter->getPrefixedText( $categoryTitle ), |
| 265 |
$options |
| 266 |
); |
| 267 |
} |
| 268 |
|
| 269 |
|
| 270 |
|
| 271 |
|
| 272 |
|
| 273 |
|
| 274 |
|
| 275 |
|
| 276 |
|
| 277 |
public static function getDataForJs( RL\Context $context, Config $config ) { |
| 278 |
|
| 279 |
$optionManager = new OptionManager( $config->get( 'CategoryTreeCategoryPageOptions' ), $config ); |
| 280 |
|
| 281 |
return [ |
| 282 |
'defaultCtOptions' => $optionManager->getOptionsAsJsStructure(), |
| 283 |
]; |
| 284 |
} |
| 285 |
|
| 286 |
|
| 287 |
|
| 288 |
|
| 289 |
|
| 290 |
|
| 291 |
|
| 292 |
public function onSpecialTrackingCategories__preprocess( |
| 293 |
$specialPage, |
| 294 |
$trackingCategories |
| 295 |
) { |
| 296 |
$categoryTargets = []; |
| 297 |
foreach ( $trackingCategories as $data ) { |
| 298 |
foreach ( $data['cats'] as $catTitle ) { |
| 299 |
$categoryTargets[] = $catTitle; |
| 300 |
} |
| 301 |
} |
| 302 |
$this->categoryCache->doQuery( $categoryTargets ); |
| 303 |
} |
| 304 |
|
| 305 |
|
| 306 |
|
| 307 |
|
| 308 |
|
| 309 |
|
| 310 |
|
| 311 |
public function onSpecialTrackingCategories__generateCatLink( $specialPage, |
| 312 |
$catTitle, &$html |
| 313 |
) { |
| 314 |
$cat = $this->categoryCache->getCategory( $catTitle ); |
| 315 |
|
| 316 |
$html .= CategoryTree::createCountString( $specialPage->getContext(), $cat, 0 ); |
| 317 |
} |
| 318 |
|
| 319 |
|
| 320 |
|
| 321 |
|
| 322 |
|
| 323 |
public function onCategoryViewer__doCategoryQuery( $type, $res ) { |
| 324 |
if ( $type === 'subcat' && $res ) { |
| 325 |
$this->categoryCache->fillFromQuery( $res ); |
| 326 |
} |
| 327 |
} |
| 328 |
|
| 329 |
|
| 330 |
|
| 331 |
|
| 332 |
|
| 333 |
|
| 334 |
|
| 335 |
|
| 336 |
public function onCategoryViewer__generateLink( $type, $title, $html, &$link ) { |
| 337 |
if ( $type !== 'subcat' || $link !== null ) { |
| 338 |
return true; |
| 339 |
} |
| 340 |
|
| 341 |
$request = RequestContext::getMain()->getRequest(); |
| 342 |
if ( $request->getCheck( 'notree' ) ) { |
| 343 |
return true; |
| 344 |
} |
| 345 |
|
| 346 |
$options = $this->config->get( 'CategoryTreeCategoryPageOptions' ); |
| 347 |
$mode = $request->getRawVal( 'mode' ); |
| 348 |
if ( $mode !== null ) { |
| 349 |
$options['mode'] = $mode; |
| 350 |
} |
| 351 |
$tree = new CategoryTree( $options, $this->config, $this->dbProvider, $this->linkRenderer ); |
| 352 |
|
| 353 |
$cat = $this->categoryCache->getCategory( $title ); |
| 354 |
|
| 355 |
$link = $tree->renderNodeInfo( $title, $cat ); |
| 356 |
|
| 357 |
CategoryTree::setHeaders( RequestContext::getMain()->getOutput() ); |
| 358 |
return false; |
| 359 |
} |
| 360 |
} |