MediaWiki REL1_31
CategoryTreeHooks.php
Go to the documentation of this file.
1<?php
30
31 public static function shouldForceHeaders() {
32 global $wgCategoryTreeSidebarRoot, $wgCategoryTreeHijackPageCategories,
33 $wgCategoryTreeForceHeaders;
34 return $wgCategoryTreeForceHeaders || $wgCategoryTreeSidebarRoot
35 || $wgCategoryTreeHijackPageCategories;
36 }
37
42 public static function initialize() {
43 global $wgRequest;
44 global $wgCategoryTreeDefaultOptions, $wgCategoryTreeDefaultMode;
45 global $wgCategoryTreeCategoryPageOptions, $wgCategoryTreeCategoryPageMode;
46 global $wgCategoryTreeOmitNamespace;
47
48 if ( !isset( $wgCategoryTreeDefaultOptions['mode'] )
49 || is_null( $wgCategoryTreeDefaultOptions['mode'] )
50 ) {
51 $wgCategoryTreeDefaultOptions['mode'] = $wgCategoryTreeDefaultMode;
52 }
53
54 if ( !isset( $wgCategoryTreeDefaultOptions['hideprefix'] )
55 || is_null( $wgCategoryTreeDefaultOptions['hideprefix'] )
56 ) {
57 $wgCategoryTreeDefaultOptions['hideprefix'] = $wgCategoryTreeOmitNamespace;
58 }
59
60 if ( !isset( $wgCategoryTreeCategoryPageOptions['mode'] )
61 || is_null( $wgCategoryTreeCategoryPageOptions['mode'] )
62 ) {
63 $mode = $wgRequest->getVal( 'mode' );
64 $wgCategoryTreeCategoryPageOptions['mode'] = ( $mode )
65 ? CategoryTree::decodeMode( $mode ) : $wgCategoryTreeCategoryPageMode;
66 }
67 }
68
72 public static function setHooks( $parser ) {
73 global $wgCategoryTreeAllowTag;
74 if ( !$wgCategoryTreeAllowTag ) {
75 return;
76 }
77 $parser->setHook( 'categorytree', 'CategoryTreeHooks::parserHook' );
78 $parser->setFunctionHook( 'categorytree', 'CategoryTreeHooks::parserFunction' );
79 }
80
87 public static function parserFunction( $parser ) {
88 $params = func_get_args();
89 array_shift( $params ); // first is $parser, strip it
90
91 // first user-supplied parameter must be category name
92 if ( !$params ) {
93 return ''; // no category specified, return nothing
94 }
95 $cat = array_shift( $params );
96
97 // build associative arguments from flat parameter list
98 $argv = [];
99 foreach ( $params as $p ) {
100 if ( preg_match( '/^\s*(\S.*?)\s*=\s*(.*?)\s*$/', $p, $m ) ) {
101 $k = $m[1];
102 $v = preg_replace( '/^"\s*(.*?)\s*"$/', '$1', $m[2] ); // strip any quotes enclusing the value
103 } else {
104 $k = trim( $p );
105 $v = true;
106 }
107
108 $argv[$k] = $v;
109 }
110
111 // now handle just like a <categorytree> tag
113 return [ $html, 'noparse' => true, 'isHTML' => true ];
114 }
115
122 public static function skinTemplateOutputPageBeforeExec( $skin, $tpl ) {
123 global $wgCategoryTreeSidebarRoot, $wgCategoryTreeSidebarOptions;
124
125 if ( !$wgCategoryTreeSidebarRoot ) {
126 return;
127 }
128
129 $html = self::parserHook( $wgCategoryTreeSidebarRoot, $wgCategoryTreeSidebarOptions );
130 if ( $html ) {
131 $tpl->data['sidebar']['categorytree-portlet'] = $html;
132 }
133 }
134
144 public static function parserHook( $cat, $argv, $parser = null, $allowMissing = false ) {
145 global $wgOut;
146
147 if ( $parser ) {
148 $parser->mOutput->mCategoryTreeTag = true; # flag for use by CategoryTreeHooks::parserOutput
149 } else {
151 }
152
153 $ct = new CategoryTree( $argv );
154
155 $attr = Sanitizer::validateTagAttributes( $argv, 'div' );
156
157 $hideroot = isset( $argv['hideroot'] )
158 ? CategoryTree::decodeBoolean( $argv['hideroot'] ) : null;
159 $onlyroot = isset( $argv['onlyroot'] )
160 ? CategoryTree::decodeBoolean( $argv['onlyroot'] ) : null;
161 $depthArg = isset( $argv['depth'] ) ? (int)$argv['depth'] : null;
162
163 $depth = CategoryTree::capDepth( $ct->getOption( 'mode' ), $depthArg );
164 if ( $onlyroot ) {
165 $depth = 0;
166 }
167
168 return $ct->getTag( $parser, $cat, $hideroot, $attr, $depth, $allowMissing );
169 }
170
178 public static function parserOutput( $outputPage, $parserOutput ) {
179 if ( self::shouldForceHeaders() ) {
180 // Skip, we've already set the headers unconditionally
181 return;
182 }
183 if ( !empty( $parserOutput->mCategoryTreeTag ) ) {
184 CategoryTree::setHeaders( $outputPage );
185 }
186 }
187
194 public static function addHeaders( OutputPage $out ) {
195 if ( !self::shouldForceHeaders() ) {
196 return;
197 }
199 }
200
208 public static function articleFromTitle( $title, &$article ) {
209 if ( $title->getNamespace() == NS_CATEGORY ) {
210 $article = new CategoryTreeCategoryPage( $title );
211 }
212 return true;
213 }
214
222 public static function outputPageMakeCategoryLinks( &$out, $categories, &$links ) {
223 global $wgCategoryTreePageCategoryOptions, $wgCategoryTreeHijackPageCategories;
224
225 if ( !$wgCategoryTreeHijackPageCategories ) {
226 // Not enabled, don't do anything
227 return true;
228 }
229
230 foreach ( $categories as $category => $type ) {
231 $links[$type][] = self::parserHook( $category, $wgCategoryTreePageCategoryOptions, null, true );
232 }
233
234 return false;
235 }
236
243 public static function skinJoinCategoryLinks( $skin, &$links, &$result ) {
244 global $wgCategoryTreeHijackPageCategories;
245 if ( !$wgCategoryTreeHijackPageCategories ) {
246 // Not enabled, don't do anything.
247 return true;
248 }
249 $embed = '<div class="CategoryTreeCategoryBarItem">';
250 $pop = '</div>';
251 $sep = ' ';
252
253 $result = $embed . implode( "{$pop} {$sep} {$embed}", $links ) . $pop;
254
255 return false;
256 }
257
262 public static function getConfigVars( &$vars ) {
263 global $wgCategoryTreeCategoryPageOptions;
264
265 // Look this is pretty bad but Category tree is just whacky, it needs to be rewritten
266 $ct = new CategoryTree( $wgCategoryTreeCategoryPageOptions );
267 $vars['wgCategoryTreePageCategoryOptions'] = $ct->getOptionsAsJsStructure();
268 return true;
269 }
270
277 $specialPage, $trackingCategories
278 ) {
279 $categoryDbKeys = [];
280 foreach ( $trackingCategories as $catMsg => $data ) {
281 foreach ( $data['cats'] as $catTitle ) {
282 $categoryDbKeys[] = $catTitle->getDbKey();
283 }
284 }
285 $categories = [];
286 if ( $categoryDbKeys ) {
288 $res = $dbr->select(
289 'category',
290 [ 'cat_id', 'cat_title', 'cat_pages', 'cat_subcats', 'cat_files' ],
291 [ 'cat_title' => array_unique( $categoryDbKeys ) ],
292 __METHOD__
293 );
294 foreach ( $res as $row ) {
295 $categories[$row->cat_title] = Category::newFromRow( $row );
296 }
297 }
298 $specialPage->categoryTreeCategories = $categories;
299 }
300
308 $specialPage, $catTitle, &$html
309 ) {
310 if ( !isset( $specialPage->categoryTreeCategories ) ) {
311 return;
312 }
313
314 $cat = null;
315 if ( isset( $specialPage->categoryTreeCategories[$catTitle->getDbKey()] ) ) {
316 $cat = $specialPage->categoryTreeCategories[$catTitle->getDbKey()];
317 }
318
319 $html .= CategoryTree::createCountString( $specialPage->getContext(), $cat, 0 );
320 }
321}
wfGetDB( $db, $groups=[], $wiki=false)
Get a Database object.
$wgOut
Definition Setup.php:912
if(! $wgDBerrorLogTZ) $wgRequest
Definition Setup.php:737
global $argv
Hooks for the CategoryTree extension, an AJAX based gadget to display the category structure of a wik...
static parserOutput( $outputPage, $parserOutput)
Hook callback that injects messages and things into the <head> tag, if needed in the current page.
static getConfigVars(&$vars)
static onSpecialTrackingCategoriesGenerateCatLink( $specialPage, $catTitle, &$html)
Hook handler for the SpecialTrackingCategories::generateCatLink hook.
static setHooks( $parser)
static outputPageMakeCategoryLinks(&$out, $categories, &$links)
OutputPageMakeCategoryLinks hook, override category links.
static initialize()
Adjusts config once MediaWiki is fully initialised TODO: Don't do this, lazy initialize the config.
static parserFunction( $parser)
Entry point for the {{#categorytree}} tag parser function.
static addHeaders(OutputPage $out)
BeforePageDisplay and BeforePageDisplayMobile hooks.
static articleFromTitle( $title, &$article)
ArticleFromTitle hook, override category page handling.
static skinJoinCategoryLinks( $skin, &$links, &$result)
static parserHook( $cat, $argv, $parser=null, $allowMissing=false)
Entry point for the <categorytree> tag parser hook.
static skinTemplateOutputPageBeforeExec( $skin, $tpl)
Hook implementation for injecting a category tree into the sidebar.
static onSpecialTrackingCategoriesPreprocess( $specialPage, $trackingCategories)
Hook handler for the SpecialTrackingCategories::preprocess hook.
Core functions for the CategoryTree extension, an AJAX based gadget to display the category structure...
static decodeBoolean( $value)
Helper function to convert a string to a boolean value.
static decodeMode( $mode)
static capDepth( $mode, $depth)
Internal function to cap depth.
static setHeaders( $outputPage)
Add ResourceLoader modules to the OutputPage object.
static createCountString(IContextSource $context, $cat, $countMode)
Create a string which format the page, subcat and file counts of a category.
This class should be covered by a general architecture document which does not exist as of January 20...
$res
Definition database.txt:21
static configuration should be added through ResourceLoaderGetConfigVars instead & $vars
Definition hooks.txt:2228
do that in ParserLimitReportFormat instead $parser
Definition hooks.txt:2603
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:2006
this hook is for auditing only or null if authentication failed before getting that far or null if we can t even determine that probably a stub 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:864
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:2013
const NS_CATEGORY
Definition Defines.php:88
const DB_REPLICA
Definition defines.php:25
$params