summaryrefslogtreecommitdiff
path: root/www/wiki/extensions/CategoryTree/includes/CategoryTreeHooks.php
blob: a30d4932be01dd2a05cc80ea8947e6db8f8963d9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
<?php
/**
 * © 2006-2008 Daniel Kinzler and others
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 * http://www.gnu.org/copyleft/gpl.html
 *
 * @file
 * @ingroup Extensions
 * @author Daniel Kinzler, brightbyte.de
 */

/**
 * Hooks for the CategoryTree extension, an AJAX based gadget
 * to display the category structure of a wiki
 */
class CategoryTreeHooks {

	public static function shouldForceHeaders() {
		global $wgCategoryTreeSidebarRoot, $wgCategoryTreeHijackPageCategories,
			$wgCategoryTreeForceHeaders;
		return $wgCategoryTreeForceHeaders || $wgCategoryTreeSidebarRoot
			|| $wgCategoryTreeHijackPageCategories;
	}

	/**
	 * Adjusts config once MediaWiki is fully initialised
	 * TODO: Don't do this, lazy initialize the config
	 */
	public static function initialize() {
		global $wgRequest;
		global $wgCategoryTreeDefaultOptions, $wgCategoryTreeDefaultMode;
		global $wgCategoryTreeCategoryPageOptions, $wgCategoryTreeCategoryPageMode;
		global $wgCategoryTreeOmitNamespace;

		if ( !isset( $wgCategoryTreeDefaultOptions['mode'] )
			|| is_null( $wgCategoryTreeDefaultOptions['mode'] )
		) {
			$wgCategoryTreeDefaultOptions['mode'] = $wgCategoryTreeDefaultMode;
		}

		if ( !isset( $wgCategoryTreeDefaultOptions['hideprefix'] )
			|| is_null( $wgCategoryTreeDefaultOptions['hideprefix'] )
		) {
			$wgCategoryTreeDefaultOptions['hideprefix'] = $wgCategoryTreeOmitNamespace;
		}

		if ( !isset( $wgCategoryTreeCategoryPageOptions['mode'] )
			|| is_null( $wgCategoryTreeCategoryPageOptions['mode'] )
		) {
			$mode = $wgRequest->getVal( 'mode' );
			$wgCategoryTreeCategoryPageOptions['mode'] = ( $mode )
				? CategoryTree::decodeMode( $mode ) : $wgCategoryTreeCategoryPageMode;
		}
	}

	/**
	 * @param Parser $parser
	 */
	public static function setHooks( $parser ) {
		global $wgCategoryTreeAllowTag;
		if ( !$wgCategoryTreeAllowTag ) {
			return;
		}
		$parser->setHook( 'categorytree', 'CategoryTreeHooks::parserHook' );
		$parser->setFunctionHook( 'categorytree', 'CategoryTreeHooks::parserFunction' );
	}

	/**
	 * Entry point for the {{#categorytree}} tag parser function.
	 * This is a wrapper around CategoryTreeHooks::parserHook
	 * @param Parser $parser
	 * @return array|string
	 */
	public static function parserFunction( $parser ) {
		$params = func_get_args();
		array_shift( $params ); // first is $parser, strip it

		// first user-supplied parameter must be category name
		if ( !$params ) {
			return ''; // no category specified, return nothing
		}
		$cat = array_shift( $params );

		// build associative arguments from flat parameter list
		$argv = [];
		foreach ( $params as $p ) {
			if ( preg_match( '/^\s*(\S.*?)\s*=\s*(.*?)\s*$/', $p, $m ) ) {
				$k = $m[1];
				$v = preg_replace( '/^"\s*(.*?)\s*"$/', '$1', $m[2] ); // strip any quotes enclusing the value
			} else {
				$k = trim( $p );
				$v = true;
			}

			$argv[$k] = $v;
		}

		// now handle just like a <categorytree> tag
		$html = self::parserHook( $cat, $argv, $parser );
		return [ $html, 'noparse' => true, 'isHTML' => true ];
	}

	/**
	 * Hook implementation for injecting a category tree into the sidebar.
	 * Only does anything if $wgCategoryTreeSidebarRoot is set to a category name.
	 * @param Skin $skin
	 * @param SkinTemplate $tpl
	 */
	public static function skinTemplateOutputPageBeforeExec( $skin, $tpl ) {
		global $wgCategoryTreeSidebarRoot, $wgCategoryTreeSidebarOptions;

		if ( !$wgCategoryTreeSidebarRoot ) {
			return;
		}

		$html = self::parserHook( $wgCategoryTreeSidebarRoot, $wgCategoryTreeSidebarOptions );
		if ( $html ) {
			$tpl->data['sidebar']['categorytree-portlet'] = $html;
		}
	}

	/**
	 * Entry point for the <categorytree> tag parser hook.
	 * This loads CategoryTreeFunctions.php and calls CategoryTree::getTag()
	 * @param string $cat
	 * @param array $argv
	 * @param Parser $parser
	 * @param bool $allowMissing
	 * @return bool|string
	 */
	public static function parserHook( $cat, $argv, $parser = null, $allowMissing = false ) {
		global $wgOut;

		if ( $parser ) {
			$parser->mOutput->mCategoryTreeTag = true; # flag for use by CategoryTreeHooks::parserOutput
		} else {
			CategoryTree::setHeaders( $wgOut );
		}

		$ct = new CategoryTree( $argv );

		$attr = Sanitizer::validateTagAttributes( $argv, 'div' );

		$hideroot = isset( $argv['hideroot'] )
			? CategoryTree::decodeBoolean( $argv['hideroot'] ) : null;
		$onlyroot = isset( $argv['onlyroot'] )
			? CategoryTree::decodeBoolean( $argv['onlyroot'] ) : null;
		$depthArg = isset( $argv['depth'] ) ? (int)$argv['depth'] : null;

		$depth = CategoryTree::capDepth( $ct->getOption( 'mode' ), $depthArg );
		if ( $onlyroot ) {
			$depth = 0;
		}

		return $ct->getTag( $parser, $cat, $hideroot, $attr, $depth, $allowMissing );
	}

	/**
	 * Hook callback that injects messages and things into the <head> tag,
	 * if needed in the current page.
	 * Does nothing if $parserOutput->mCategoryTreeTag is not set
	 * @param OutputPage $outputPage
	 * @param ParserOutput $parserOutput
	 */
	public static function parserOutput( $outputPage, $parserOutput ) {
		if ( self::shouldForceHeaders() ) {
			// Skip, we've already set the headers unconditionally
			return;
		}
		if ( !empty( $parserOutput->mCategoryTreeTag ) ) {
			CategoryTree::setHeaders( $outputPage );
		}
	}

	/**
	 * BeforePageDisplay and BeforePageDisplayMobile hooks.
	 * These hooks are used when $wgCategoryTreeForceHeaders is set.
	 * Otherwise similar to CategoryTreeHooks::parserOutput.
	 * @param OutputPage $out
	 */
	public static function addHeaders( OutputPage $out ) {
		if ( !self::shouldForceHeaders() ) {
			return;
		}
		CategoryTree::setHeaders( $out );
	}

	/**
	 * ArticleFromTitle hook, override category page handling
	 *
	 * @param Title $title
	 * @param Article &$article
	 * @return bool
	 */
	public static function articleFromTitle( $title, &$article ) {
		if ( $title->getNamespace() == NS_CATEGORY ) {
			$article = new CategoryTreeCategoryPage( $title );
		}
		return true;
	}

	/**
	 * OutputPageMakeCategoryLinks hook, override category links
	 * @param OutputPage &$out
	 * @param array $categories
	 * @param array &$links
	 * @return bool
	 */
	public static function outputPageMakeCategoryLinks( &$out, $categories, &$links ) {
		global $wgCategoryTreePageCategoryOptions, $wgCategoryTreeHijackPageCategories;

		if ( !$wgCategoryTreeHijackPageCategories ) {
			// Not enabled, don't do anything
			return true;
		}

		foreach ( $categories as $category => $type ) {
			$links[$type][] = self::parserHook( $category, $wgCategoryTreePageCategoryOptions, null, true );
		}

		return false;
	}

	/**
	 * @param Skin $skin
	 * @param array &$links
	 * @param string &$result
	 * @return bool
	 */
	public static function skinJoinCategoryLinks( $skin, &$links, &$result ) {
		global $wgCategoryTreeHijackPageCategories;
		if ( !$wgCategoryTreeHijackPageCategories ) {
			// Not enabled, don't do anything.
			return true;
		}
		$embed = '<div class="CategoryTreeCategoryBarItem">';
		$pop = '</div>';
		$sep = ' ';

		$result = $embed . implode( "{$pop} {$sep} {$embed}", $links ) . $pop;

		return false;
	}

	/**
	 * @param array &$vars
	 * @return bool
	 */
	public static function getConfigVars( &$vars ) {
		global $wgCategoryTreeCategoryPageOptions;

		// Look this is pretty bad but Category tree is just whacky, it needs to be rewritten
		$ct = new CategoryTree( $wgCategoryTreeCategoryPageOptions );
		$vars['wgCategoryTreePageCategoryOptions'] = $ct->getOptionsAsJsStructure();
		return true;
	}

	/**
	 * Hook handler for the SpecialTrackingCategories::preprocess hook
	 * @param SpecialPage $specialPage SpecialTrackingCategories object
	 * @param array $trackingCategories [ 'msg' => Title, 'cats' => Title[] ]
	 */
	public static function onSpecialTrackingCategoriesPreprocess(
		$specialPage, $trackingCategories
	) {
		$categoryDbKeys = [];
		foreach ( $trackingCategories as $catMsg => $data ) {
			foreach ( $data['cats'] as $catTitle ) {
				$categoryDbKeys[] = $catTitle->getDbKey();
			}
		}
		$categories = [];
		if ( $categoryDbKeys ) {
			$dbr = wfGetDB( DB_REPLICA );
			$res = $dbr->select(
				'category',
				[ 'cat_id', 'cat_title', 'cat_pages', 'cat_subcats', 'cat_files' ],
				[ 'cat_title' => array_unique( $categoryDbKeys ) ],
				__METHOD__
			);
			foreach ( $res as $row ) {
				$categories[$row->cat_title] = Category::newFromRow( $row );
			}
		}
		$specialPage->categoryTreeCategories = $categories;
	}

	/**
	 * Hook handler for the SpecialTrackingCategories::generateCatLink hook
	 * @param SpecialPage $specialPage SpecialTrackingCategories object
	 * @param Title $catTitle Title object of the linked category
	 * @param string &$html Result html
	 */
	public static function onSpecialTrackingCategoriesGenerateCatLink(
		$specialPage, $catTitle, &$html
	) {
		if ( !isset( $specialPage->categoryTreeCategories ) ) {
			return;
		}

		$cat = null;
		if ( isset( $specialPage->categoryTreeCategories[$catTitle->getDbKey()] ) ) {
			$cat = $specialPage->categoryTreeCategories[$catTitle->getDbKey()];
		}

		$html .= CategoryTree::createCountString( $specialPage->getContext(), $cat, 0 );
	}
}