From 1e0067d361d137e4018cf62a90c65d2bfb0bb932 Mon Sep 17 00:00:00 2001 From: Alex Legler Date: Sat, 3 Jan 2015 21:00:55 +0100 Subject: Add UserFunctions --- UserFunctions/UserFunctions_body.php | 189 +++++++++++++++++++++++++++++++++++ 1 file changed, 189 insertions(+) create mode 100644 UserFunctions/UserFunctions_body.php (limited to 'UserFunctions/UserFunctions_body.php') diff --git a/UserFunctions/UserFunctions_body.php b/UserFunctions/UserFunctions_body.php new file mode 100644 index 00000000..6c273551 --- /dev/null +++ b/UserFunctions/UserFunctions_body.php @@ -0,0 +1,189 @@ +pf_ifexist_breakdown = array(); + return true; + } + + /** + * Register ParserClearState hook. + * We defer this until needed to avoid the loading of the code of this file + * when no parser function is actually called. + */ + public static function registerClearHook() { + static $done = false; + if( !$done ) { + global $wgHooks; + $wgHooks['ParserClearState'][] = __CLASS__ . '::clearState'; + $done = true; + } + } + + /** + * @return User + * Using $wgUser Incompatibility with SMW using via $parser + **/ + private static function getUserObj() { + global $wgUser; + return $wgUser; + } + + /** + * @param $parser Parser + * @param $frame PPFrame + * @param $args array + * @return string + */ + public static function ifanonObj( $parser, $frame, $args ) { + $parser->disableCache(); + $pUser = self::getUserObj(); + + if( $pUser->isAnon() ){ + return isset( $args[0] ) ? trim( $frame->expand( $args[0] ) ) : ''; + } else { + return isset( $args[1] ) ? trim( $frame->expand( $args[1] ) ) : ''; + } + } + + /** + * @param $parser Parser + * @param $frame PPFrame + * @param $args array + * @return string + */ + public static function ifblockedObj( $parser, $frame, $args ) { + $parser->disableCache(); + $pUser = self::getUserObj(); + + if( $pUser->isBlocked() ){ + return isset( $args[0] ) ? trim( $frame->expand( $args[0] ) ) : ''; + } else { + return isset( $args[1] ) ? trim( $frame->expand( $args[1] ) ) : ''; + } + } + + /** + * @param $parser Parser + * @param $frame PPFrame + * @param $args array + * @return string + */ + public static function ifsysopObj( $parser, $frame, $args ) { + $parser->disableCache(); + $pUser = self::getUserObj(); + + if( $pUser->isAllowed( 'protect' ) ){ + return isset( $args[0] ) ? trim( $frame->expand( $args[0] ) ) : ''; + } else { + return isset( $args[1] ) ? trim( $frame->expand( $args[1] ) ) : ''; + } + } + + /** + * @param $parser Parser + * @param $frame PPFrame + * @param $args array + * @return string + */ + public static function ifingroupObj ( $parser, $frame, $args ) { + $parser->disableCache(); + $pUser = self::getUserObj(); + + $grp = isset( $args[0] ) ? trim( $frame->expand( $args[0] ) ) : ''; + + if( $grp!=='' ) { + # Considering multiple groups + $allgrp = explode(",", $grp); + + $userGroups = $pUser->getEffectiveGroups(); + foreach ( $allgrp as $elgrp ) { + if ( in_array( trim( $elgrp ), $userGroups ) ) { + return isset( $args[1] ) ? trim( $frame->expand( $args[1] ) ) : ''; + } + } + } + return isset( $args[2] ) ? trim( $frame->expand( $args[2] ) ) : ''; + } + + /** + * @param $parser Parser + * @param $alt string + * @return String + */ + public static function realname( $parser, $alt = '' ) { + $parser->disableCache(); + $pUser = self::getUserObj(); + + if( $pUser->isAnon() && $alt !== '' ) { + return $alt; + } + return $pUser->getRealName(); + } + + /** + * @param $parser Parser + * @param $alt string + * @return String + */ + public static function username( $parser, $alt = '' ) { + $parser->disableCache(); + $pUser = self::getUserObj(); + + if( $pUser->isAnon() && $alt !== '' ) { + return $alt; + } + return $pUser->getName(); + } + + /** + * @param $parser Parser + * @param $alt string + * @return String + */ + public static function useremail( $parser, $alt = '' ) { + $parser->disableCache(); + $pUser = self::getUserObj(); + + if($pUser->isAnon() && $alt!=='') { + return $alt; + } + return $pUser->getEmail(); + } + + /** + * @param $parser Parser + * @param $alt string + * @return String + */ + public static function nickname( $parser, $alt = '' ) { + $parser->disableCache(); + $pUser = self::getUserObj(); + + if( $pUser->isAnon() ) { + if ( $alt !== '' ) { + return $alt; + } + return $pUser->getName(); + } + $nickname = $pUser->getOption( 'nickname' ); + $nickname = $nickname === '' ? $pUser->getName() : $nickname; + return $nickname; + } + + /** + * @param $parser Parser + * @return string + */ + public static function ip( $parser ) { + $parser->disableCache(); + $request = self::getUserObj()->getRequest(); + return $request->getIP(); + } + +} -- cgit v1.2.3-65-gdbad