-
Notifications
You must be signed in to change notification settings - Fork 0
/
ParserFun.php
108 lines (96 loc) · 2.54 KB
/
ParserFun.php
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
<?php
/**
* 'Parser Fun' adds a parser function '#parse' for parsing wikitext and introduces the
* 'THIS:' prefix for page information related magic variables
*
* Documentation: https://www.mediawiki.org/wiki/Extension:Parser_Fun
* Support: https://www.mediawiki.org/wiki/Extension_talk:Parser_Fun
* Source code: https://phabricator.wikimedia.org/diffusion/EPFU/
*
* @license: ISC license
* @author: Daniel Werner < [email protected] >
*
* @file ParserFun.php
* @ingroup Parse
*/
// Ensure that the script cannot be executed outside of MediaWiki.
if( !defined( 'MEDIAWIKI' ) ) {
die( 'This is an extension to MediaWiki and cannot be run standalone.' );
}
/**
* Extension class of the 'Parser Fun' extension.
* Handling the functionality around the 'THIS' magic word feature.
*/
class ExtParserFun {
/**
* Version of the 'Parser Fun' extension.
*
* @since 0.1
*
* @var string
*/
const VERSION = '0.5.0';
static function init( Parser &$parser ) {
if( self::isEnabledFunction( 'this' ) ) {
// only register function if not disabled by configuration
$parser->setFunctionHook( 'this', array( 'ParserFunThis', 'pfObj_this' ), Parser::SFH_NO_HASH | Parser::SFH_OBJECT_ARGS );
}
return true;
}
/**
* returns whether a certain variable/parser function is active by the local wiki configuration.
*
* @since 0.2
*
* @param string $word
* @return bool
*/
static function isEnabledFunction( $word ) {
global $egParserFunEnabledFunctions;
return in_array( $word, $egParserFunEnabledFunctions );
}
/**
* Returns the extensions base installation directory.
*
* @since 0.1
*
* @return string
*/
static function getDir() {
static $dir = null;
if( $dir === null ) {
$dir = dirname( __FILE__ );
}
return $dir;
}
##################
# Hooks Handling #
##################
static function onParserGetVariableValueSwitch( Parser &$parser, &$cache, &$magicWordId, &$ret, $frame = null ) {
if( $frame === null ) {
// unsupported MW version
return true;
}
switch( $magicWordId ) {
/** THIS **/
case 'this':
$ret = ParserFunThis::pfObj_this( $parser, $frame, null );
break;
/** CALLER **/
case 'caller':
$ret = ParserFunCaller::getCallerVar( $frame );
break;
}
return true;
}
static function onMagicWordwgVariableIDs( &$variableIds ) {
// only register variables if not disabled by configuration
if( self::isEnabledFunction( 'this' ) ) {
$variableIds[] = 'this';
}
if( self::isEnabledFunction( 'caller' ) ) {
$variableIds[] = 'caller';
}
return true;
}
}