MediaWiki  1.33.0
CategoryTreeHooks.php
Go to the documentation of this file.
1 <?php
30 
35  public static function shouldForceHeaders() {
36  global $wgCategoryTreeForceHeaders;
37  return $wgCategoryTreeForceHeaders;
38  }
39 
44  public static function initialize() {
45  global $wgRequest;
46  global $wgCategoryTreeDefaultOptions, $wgCategoryTreeDefaultMode;
47  global $wgCategoryTreeCategoryPageOptions, $wgCategoryTreeCategoryPageMode;
48  global $wgCategoryTreeOmitNamespace;
49 
50  if ( !isset( $wgCategoryTreeDefaultOptions['mode'] )
51  || is_null( $wgCategoryTreeDefaultOptions['mode'] )
52  ) {
53  $wgCategoryTreeDefaultOptions['mode'] = $wgCategoryTreeDefaultMode;
54  }
55 
56  if ( !isset( $wgCategoryTreeDefaultOptions['hideprefix'] )
57  || is_null( $wgCategoryTreeDefaultOptions['hideprefix'] )
58  ) {
59  $wgCategoryTreeDefaultOptions['hideprefix'] = $wgCategoryTreeOmitNamespace;
60  }
61 
62  if ( !isset( $wgCategoryTreeCategoryPageOptions['mode'] )
63  || is_null( $wgCategoryTreeCategoryPageOptions['mode'] )
64  ) {
65  $mode = $wgRequest->getVal( 'mode' );
66  $wgCategoryTreeCategoryPageOptions['mode'] = ( $mode )
67  ? CategoryTree::decodeMode( $mode ) : $wgCategoryTreeCategoryPageMode;
68  }
69  }
70 
74  public static function setHooks( Parser $parser ) {
75  global $wgCategoryTreeAllowTag;
76  if ( !$wgCategoryTreeAllowTag ) {
77  return;
78  }
79  $parser->setHook( 'categorytree', 'CategoryTreeHooks::parserHook' );
80  $parser->setFunctionHook( 'categorytree', 'CategoryTreeHooks::parserFunction' );
81  }
82 
90  public static function parserFunction( Parser $parser, ...$params ) {
91  // first user-supplied parameter must be category name
92  if ( !$params ) {
93  // no category specified, return nothing
94  return '';
95  }
96  $cat = array_shift( $params );
97 
98  // build associative arguments from flat parameter list
99  $argv = [];
100  foreach ( $params as $p ) {
101  if ( preg_match( '/^\s*(\S.*?)\s*=\s*(.*?)\s*$/', $p, $m ) ) {
102  $k = $m[1];
103  // strip any quotes enclusing the value
104  $v = preg_replace( '/^"\s*(.*?)\s*"$/', '$1', $m[2] );
105  } else {
106  $k = trim( $p );
107  $v = true;
108  }
109 
110  $argv[$k] = $v;
111  }
112 
113  // now handle just like a <categorytree> tag
114  $html = self::parserHook( $cat, $argv, $parser );
115  return [ $html, 'noparse' => true, 'isHTML' => true ];
116  }
117 
124  public static function onSkinBuildSidebar( Skin $skin, array &$sidebar ) {
125  global $wgCategoryTreeSidebarRoot, $wgCategoryTreeSidebarOptions;
126 
127  if ( !$wgCategoryTreeSidebarRoot ) {
128  return;
129  }
130 
131  $html = self::parserHook( $wgCategoryTreeSidebarRoot, $wgCategoryTreeSidebarOptions );
132  if ( $html ) {
133  $sidebar['categorytree-portlet'] = $html;
134  CategoryTree::setHeaders( $skin->getOutput() );
135  }
136  }
137 
148  public static function parserHook(
149  $cat,
150  array $argv,
151  Parser $parser = null,
152  $allowMissing = false
153  ) {
154  if ( $parser ) {
155  # flag for use by CategoryTreeHooks::parserOutput
156  $parser->mOutput->mCategoryTreeTag = true;
157  }
158 
159  $ct = new CategoryTree( $argv );
160 
161  $attr = Sanitizer::validateTagAttributes( $argv, 'div' );
162 
163  $hideroot = isset( $argv['hideroot'] )
164  ? CategoryTree::decodeBoolean( $argv['hideroot'] ) : null;
165  $onlyroot = isset( $argv['onlyroot'] )
166  ? CategoryTree::decodeBoolean( $argv['onlyroot'] ) : null;
167  $depthArg = isset( $argv['depth'] ) ? (int)$argv['depth'] : null;
168 
169  $depth = CategoryTree::capDepth( $ct->getOption( 'mode' ), $depthArg );
170  if ( $onlyroot ) {
171  $depth = 0;
172  }
173 
174  return $ct->getTag( $parser, $cat, $hideroot, $attr, $depth, $allowMissing );
175  }
176 
185  public static function parserOutput( OutputPage $outputPage, ParserOutput $parserOutput ) {
186  if ( self::shouldForceHeaders() ) {
187  // Skip, we've already set the headers unconditionally
188  return;
189  }
190  if ( !empty( $parserOutput->mCategoryTreeTag ) ) {
191  CategoryTree::setHeaders( $outputPage );
192  }
193  }
194 
201  public static function addHeaders( OutputPage $out ) {
202  if ( !self::shouldForceHeaders() ) {
203  return;
204  }
206  }
207 
214  public static function articleFromTitle( Title $title, Article &$article = null ) {
215  if ( $title->getNamespace() == NS_CATEGORY ) {
217  }
218  }
219 
227  public static function outputPageMakeCategoryLinks(
228  OutputPage &$out,
229  array $categories,
230  array &$links
231  ) {
232  global $wgCategoryTreePageCategoryOptions, $wgCategoryTreeHijackPageCategories;
233 
234  if ( !$wgCategoryTreeHijackPageCategories ) {
235  // Not enabled, don't do anything
236  return true;
237  }
238 
239  foreach ( $categories as $category => $type ) {
240  $links[$type][] = self::parserHook( $category, $wgCategoryTreePageCategoryOptions, null, true );
242  }
243 
244  return false;
245  }
246 
253  public static function getDataForJs() {
254  global $wgCategoryTreeCategoryPageOptions;
255 
256  // Look, this is pretty bad but CategoryTree is just whacky, it needs to be rewritten
257  $ct = new CategoryTree( $wgCategoryTreeCategoryPageOptions );
258 
259  return [
260  'defaultCtOptions' => $ct->getOptionsAsJsStructure(),
261  ];
262  }
263 
271  SpecialPage $specialPage, array $trackingCategories
272  ) {
273  $categoryDbKeys = [];
274  foreach ( $trackingCategories as $catMsg => $data ) {
275  foreach ( $data['cats'] as $catTitle ) {
276  $categoryDbKeys[] = $catTitle->getDbKey();
277  }
278  }
279  $categories = [];
280  if ( $categoryDbKeys ) {
281  $dbr = wfGetDB( DB_REPLICA );
282  $res = $dbr->select(
283  'category',
284  [ 'cat_id', 'cat_title', 'cat_pages', 'cat_subcats', 'cat_files' ],
285  [ 'cat_title' => array_unique( $categoryDbKeys ) ],
286  __METHOD__
287  );
288  foreach ( $res as $row ) {
289  $categories[$row->cat_title] = Category::newFromRow( $row );
290  }
291  }
292  $specialPage->categoryTreeCategories = $categories;
293  }
294 
303  SpecialPage $specialPage, Title $catTitle, &$html
304  ) {
305  if ( !isset( $specialPage->categoryTreeCategories ) ) {
306  return;
307  }
308 
309  $cat = null;
310  if ( isset( $specialPage->categoryTreeCategories[$catTitle->getDbKey()] ) ) {
311  $cat = $specialPage->categoryTreeCategories[$catTitle->getDbKey()];
312  }
313 
314  $html .= CategoryTree::createCountString( $specialPage->getContext(), $cat, 0 );
315  }
316 }
CategoryTreeHooks\setHooks
static setHooks(Parser $parser)
Definition: CategoryTreeHooks.php:74
CategoryTreeHooks\onSkinBuildSidebar
static onSkinBuildSidebar(Skin $skin, array &$sidebar)
Hook implementation for injecting a category tree into the sidebar.
Definition: CategoryTreeHooks.php:124
$article
return true to allow those checks to and false if checking is done remove or add to the links of a group of changes in EnhancedChangesList Hook subscribers can return false to omit this line from recentchanges use this to change the tables headers change it to an object instance and return false override the list derivative used the name of the old file & $article
Definition: hooks.txt:1476
CategoryTreeHooks\outputPageMakeCategoryLinks
static outputPageMakeCategoryLinks(OutputPage &$out, array $categories, array &$links)
OutputPageMakeCategoryLinks hook, override category links.
Definition: CategoryTreeHooks.php:227
ParserOutput
Definition: ParserOutput.php:25
CategoryTreeHooks\addHeaders
static addHeaders(OutputPage $out)
BeforePageDisplay and BeforePageDisplayMobile hooks.
Definition: CategoryTreeHooks.php:201
CategoryTreeHooks\parserFunction
static parserFunction(Parser $parser,... $params)
Entry point for the {{#categorytree}} tag parser function.
Definition: CategoryTreeHooks.php:90
CategoryTree\capDepth
static capDepth( $mode, $depth)
Internal function to cap depth @suppress PhanPluginDuplicateConditionalNullCoalescing until PHP7 is r...
Definition: CategoryTree.php:802
CategoryTree\decodeMode
static decodeMode( $mode)
Definition: CategoryTree.php:139
$out
this hook is for auditing only or null if authentication failed before getting that far or null if we can t even determine that When $user is not it can be in the form of< username >< more info > e g for bot passwords intended to be added to log contexts Fields it might only if the login was with a bot password it is not rendered in wiki pages or galleries in category pages allow injecting custom HTML after the section Any uses of the hook need to handle escaping see BaseTemplate::getToolbox and BaseTemplate::makeListItem for details on the format of individual items inside of this array or by returning and letting standard HTTP rendering take place modifiable or by returning false and taking over the output $out
Definition: hooks.txt:780
CategoryTree
Core functions for the CategoryTree extension, an AJAX based gadget to display the category structure...
Definition: CategoryTree.php:29
$params
$params
Definition: styleTest.css.php:44
CategoryTreeHooks\initialize
static initialize()
Adjusts config once MediaWiki is fully initialised TODO: Don't do this, lazy initialize the config.
Definition: CategoryTreeHooks.php:44
$res
$res
Definition: database.txt:21
CategoryTreeHooks\parserOutput
static parserOutput(OutputPage $outputPage, ParserOutput $parserOutput)
Hook callback that injects messages and things into the <head> tag, if needed in the current page.
Definition: CategoryTreeHooks.php:185
php
injection txt This is an overview of how MediaWiki makes use of dependency injection The design described here grew from the discussion of RFC T384 The term dependency this means that anything an object needs to operate should be injected from the the object itself should only know narrow no concrete implementation of the logic it relies on The requirement to inject everything typically results in an architecture that based on two main types of and essentially stateless service objects that use other service objects to operate on the value objects As of the beginning MediaWiki is only starting to use the DI approach Much of the code still relies on global state or direct resulting in a highly cyclical dependency which acts as the top level factory for services in MediaWiki which can be used to gain access to default instances of various services MediaWikiServices however also allows new services to be defined and default services to be redefined Services are defined or redefined by providing a callback the instantiator that will return a new instance of the service When it will create an instance of MediaWikiServices and populate it with the services defined in the files listed by thereby bootstrapping the DI framework Per $wgServiceWiringFiles lists includes ServiceWiring php
Definition: injection.txt:35
CategoryTree\createCountString
static createCountString(IContextSource $context, Category $cat=null, $countMode)
Create a string which format the page, subcat and file counts of a category @suppress PhanParamReqAft...
Definition: CategoryTree.php:718
$dbr
$dbr
Definition: testCompression.php:50
$data
$data
Utility to generate mapping file used in mw.Title (phpCharToUpper.json)
Definition: generatePhpCharToUpperMappings.php:13
CategoryTreeHooks\shouldForceHeaders
static shouldForceHeaders()
Definition: CategoryTreeHooks.php:35
$html
null means default in associative array with keys and values unescaped Should be merged with default with a value of false meaning to suppress the attribute in associative array with keys and values unescaped noclasses just before the function returns a value If you return an< a > element with HTML attributes $attribs and contents $html will be returned If you return $ret will be returned and may include noclasses & $html
Definition: hooks.txt:1985
$title
namespace and then decline to actually register it file or subcat img or subcat $title
Definition: hooks.txt:925
CategoryTreeHooks\parserHook
static parserHook( $cat, array $argv, Parser $parser=null, $allowMissing=false)
Entry point for the <categorytree> tag parser hook.
Definition: CategoryTreeHooks.php:148
wfGetDB
wfGetDB( $db, $groups=[], $wiki=false)
Get a Database object.
Definition: GlobalFunctions.php:2636
CategoryTreeHooks
Hooks for the CategoryTree extension, an AJAX based gadget to display the category structure of a wik...
Definition: CategoryTreeHooks.php:29
CategoryTreeHooks\onSpecialTrackingCategoriesGenerateCatLink
static onSpecialTrackingCategoriesGenerateCatLink(SpecialPage $specialPage, Title $catTitle, &$html)
Hook handler for the SpecialTrackingCategories::generateCatLink hook @suppress PhanUndeclaredProperty...
Definition: CategoryTreeHooks.php:302
$parser
see documentation in includes Linker php for Linker::makeImageLink or false for current used if you return false $parser
Definition: hooks.txt:1802
DB_REPLICA
const DB_REPLICA
Definition: defines.php:25
NS_CATEGORY
const NS_CATEGORY
Definition: Defines.php:78
array
The wiki should then use memcached to cache various data To use multiple just add more items to the array To increase the weight of a make its entry a array("192.168.0.1:11211", 2))
SpecialPage\getContext
getContext()
Gets the context this SpecialPage is executed in.
Definition: SpecialPage.php:698
Category\newFromRow
static newFromRow( $row, $title=null)
Factory function, for constructing a Category object from a result set.
Definition: Category.php:179
SpecialPage
Parent class for all special pages.
Definition: SpecialPage.php:36
CategoryTreeHooks\onSpecialTrackingCategoriesPreprocess
static onSpecialTrackingCategoriesPreprocess(SpecialPage $specialPage, array $trackingCategories)
Hook handler for the SpecialTrackingCategories::preprocess hook @suppress PhanUndeclaredProperty Spec...
Definition: CategoryTreeHooks.php:270
CategoryTree\decodeBoolean
static decodeBoolean( $value)
Helper function to convert a string to a boolean value.
Definition: CategoryTree.php:176
CategoryTreeCategoryPage
Definition: CategoryTreeCategoryPage.php:21
Title
Represents a title within MediaWiki.
Definition: Title.php:40
CategoryTreeHooks\getDataForJs
static getDataForJs()
Get exported data for the "ext.categoryTree" ResourceLoader module.
Definition: CategoryTreeHooks.php:253
CategoryTreeHooks\articleFromTitle
static articleFromTitle(Title $title, Article &$article=null)
ArticleFromTitle hook, override category page handling.
Definition: CategoryTreeHooks.php:214
as
This document is intended to provide useful advice for parties seeking to redistribute MediaWiki to end users It s targeted particularly at maintainers for Linux since it s been observed that distribution packages of MediaWiki often break We ve consistently had to recommend that users seeking support use official tarballs instead of their distribution s and this often solves whatever problem the user is having It would be nice if this could such as
Definition: distributors.txt:9
$skin
null means default in associative array with keys and values unescaped Should be merged with default with a value of false meaning to suppress the attribute in associative array with keys and values unescaped noclasses just before the function returns a value If you return an< a > element with HTML attributes $attribs and contents $html will be returned If you return $ret will be returned $skin
Definition: hooks.txt:1985
true
null means default in associative array with keys and values unescaped Should be merged with default with a value of false meaning to suppress the attribute in associative array with keys and values unescaped noclasses just before the function returns a value If you return true
Definition: hooks.txt:1985
Article
Class for viewing MediaWiki article and history.
Definition: Article.php:37
$wgRequest
if(! $wgDBerrorLogTZ) $wgRequest
Definition: Setup.php:728
Skin
The main skin class which provides methods and properties for all other skins.
Definition: Skin.php:38
CategoryTree\setHeaders
static setHeaders(OutputPage $outputPage)
Add ResourceLoader modules to the OutputPage object.
Definition: CategoryTree.php:254
$type
$type
Definition: testCompression.php:48