summaryrefslogtreecommitdiff
blob: f4714d493e9dd43f67a6428af825b688fb84617d (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
<?php
/**
 * UserFunctions extension - Provides a set of dynamic parser functions that trigger on the current user.
 * @version 2.7.0 - 2017/07/26 (Based on ParserFunctions)
 *
 * @link https://www.mediawiki.org/wiki/Extension:UserFunctions Documentation
 * @link https://www.mediawiki.org/wiki/Extension_talk:UserFunctions Support
 * @link https://phabricator.wikimedia.org/diffusion/EUFU/ Source code
 *
 * @file UserFunctions.php
 * @ingroup Extensions
 * @package MediaWiki
 * @author Algorithm
 * @author Lexw
 * @author Louperivois
 * @author Wikinaut
 * @author Kghbln
 * @author Toniher
 * @author Uckelman
 * @copyright (C) 2006 Algorithm
 * @license https://www.gnu.org/copyleft/gpl.html GNU General Public License 2.0 or later
 */

// Ensure that the script cannot be executed outside of MediaWiki.
if ( !defined( 'MEDIAWIKI' ) ) {
	die( 'This file is a MediaWiki extension, it is not a valid entry point' );
}

//self executing anonymous function to prevent global scope assumptions
call_user_func( function() {

	/**
	 * Enable Personal Data Functions
	 * Set this to true if you want your users to be able to use the following functions:
	 * realname, username, useremail, nickname, ip
	 * WARNING: These functions can be used to leak your user's email addresses and real names.
	 * If unsure, don't activate these features.
	**/
	$GLOBALS['wgUFEnablePersonalDataFunctions'] = false;

	/** Allow to be used in places such as SF form **/
	$GLOBALS['wgUFEnableSpecialContexts'] = true;

	/** Restrict to certain namespaces **/
	$GLOBALS['wgUFAllowedNamespaces'] = array(
		NS_MEDIAWIKI => true
	);

	$GLOBALS['wgExtensionCredits']['parserhook'][] = array(
		'path' => __FILE__,
		'name' => 'UserFunctions',
		'version' => '2.7.0',
		'url' => 'https://www.mediawiki.org/wiki/Extension:UserFunctions',
		'author' => array(
			'Ross McClure',
			'Toni Hermoso Pulido',
			'...'
			),
		'descriptionmsg' => 'userfunctions-desc',
		'license-name' => 'GPL-2.0-or-later'
	);

	$GLOBALS['wgAutoloadClasses']['ExtUserFunctions'] = __DIR__ .'/UserFunctions_body.php';
	$GLOBALS['wgMessagesDirs']['UserFunctions'] = __DIR__ . '/i18n';
	$GLOBALS['wgExtensionMessagesFiles']['UserFunctionsMagic'] = __DIR__ . '/UserFunctions.i18n.magic.php';

	$GLOBALS['wgHooks']['ParserFirstCallInit'][] = 'wfRegisterUserFunctions';
});

/**
 * @param $parser Parser
 * @return bool
 */

function wfRegisterUserFunctions( $parser ) {
	global $wgUFEnablePersonalDataFunctions, $wgUFAllowedNamespaces, $wgUFEnableSpecialContexts;

	// Whether it's a Special Page or a Maintenance Script
	$special = false;

	// Initialize NS
	$title = RequestContext::getMain()->getTitle();
	$cur_ns = $title === null ? -1 : $title->getNamespace();
	if ( $cur_ns == -1 ) {
		$special = true;
	}

	$process = false;

	// As far it's not special case, check if current page NS is in the allowed list
	if ( !$special ) {
		if ( isset( $wgUFAllowedNamespaces[$cur_ns] ) ) {
			if ( $wgUFAllowedNamespaces[$cur_ns] ) {
				$process = true;
			}
		}
	}
	else {
		if ( $wgUFEnableSpecialContexts ) {
			if ( $special ) {
					$process = true;
			}
		}
	}

	if ( $process ) {
		// These functions accept DOM-style arguments

		$parser->setFunctionHook( 'ifanon', 'ExtUserFunctions::ifanonObj', Parser::SFH_OBJECT_ARGS );
		$parser->setFunctionHook( 'ifblocked', 'ExtUserFunctions::ifblockedObj', Parser::SFH_OBJECT_ARGS );
		$parser->setFunctionHook( 'ifsysop', 'ExtUserFunctions::ifsysopObj', Parser::SFH_OBJECT_ARGS );
		$parser->setFunctionHook( 'ifingroup', 'ExtUserFunctions::ifingroupObj', Parser::SFH_OBJECT_ARGS );

		if ($wgUFEnablePersonalDataFunctions) {
			$parser->setFunctionHook( 'realname', 'ExtUserFunctions::realname' );
			$parser->setFunctionHook( 'username', 'ExtUserFunctions::username' );
			$parser->setFunctionHook( 'useremail', 'ExtUserFunctions::useremail' );
			$parser->setFunctionHook( 'nickname', 'ExtUserFunctions::nickname' );
			$parser->setFunctionHook( 'ip', 'ExtUserFunctions::ip' );
		}

	}

	return true;
}