Index: trunk/extensions/UserFunctions/UserFunctions.i18n.magic.php |
— | — | @@ -0,0 +1,16 @@ |
| 2 | +<?php |
| 3 | + |
| 4 | +$magicWords = array(); |
| 5 | + |
| 6 | +/** English (English) */ |
| 7 | +$magicWords['en'] = array( |
| 8 | + 'ifanon' => array( 0, 'ifanon' ), |
| 9 | + 'ifblocked' => array( 0, 'ifblocked' ), |
| 10 | + 'ifsysop' => array( 0, 'ifsysop' ), |
| 11 | + 'realname' => array( 0, 'realname' ), |
| 12 | + 'username' => array( 0, 'username' ), |
| 13 | + 'useremail' => array( 0, 'useremail' ), |
| 14 | + 'nickname' => array( 0, 'nickname' ), |
| 15 | + 'ifingroup' => array( 0, 'ifingroup' ), |
| 16 | + 'ip' => array( 0, 'ip' ), |
| 17 | +); |
Index: trunk/extensions/UserFunctions/UserFunctions_body.php |
— | — | @@ -0,0 +1,149 @@ |
| 2 | +<?php |
| 3 | + |
| 4 | +class ExtUserFunctions { |
| 5 | + |
| 6 | + function clearState(&$parser) { |
| 7 | + $parser->pf_ifexist_breakdown = array(); |
| 8 | + return true; |
| 9 | + } |
| 10 | + |
| 11 | + function ifanon( &$parser, $then = '', $else = '' ) { |
| 12 | + global $wgUser; |
| 13 | + $parser->disableCache(); |
| 14 | + |
| 15 | + if($wgUser->isAnon()){ |
| 16 | + return $then; |
| 17 | + } |
| 18 | + return $else; |
| 19 | + } |
| 20 | + |
| 21 | + function ifanonObj( &$parser, $frame, $args ) { |
| 22 | + global $wgUser; |
| 23 | + |
| 24 | + if($wgUser->isAnon()){ |
| 25 | + return isset( $args[0] ) ? trim( $frame->expand( $args[0] ) ) : ''; |
| 26 | + } else { |
| 27 | + return isset( $args[1] ) ? trim( $frame->expand( $args[1] ) ) : ''; |
| 28 | + } |
| 29 | + } |
| 30 | + |
| 31 | + function ifblocked( &$parser, $then = '', $else = '' ) { |
| 32 | + global $wgUser; |
| 33 | + $parser->disableCache(); |
| 34 | + |
| 35 | + if($wgUser->isBlocked()) { |
| 36 | + return $then; |
| 37 | + } |
| 38 | + return $else; |
| 39 | + } |
| 40 | + |
| 41 | + function ifblockedObj( &$parser, $frame, $args ) { |
| 42 | + global $wgUser; |
| 43 | + |
| 44 | + if($wgUser->isBlocked()){ |
| 45 | + return isset( $args[0] ) ? trim( $frame->expand( $args[0] ) ) : ''; |
| 46 | + } else { |
| 47 | + return isset( $args[1] ) ? trim( $frame->expand( $args[1] ) ) : ''; |
| 48 | + } |
| 49 | + } |
| 50 | + |
| 51 | + function ifsysop( &$parser, $then = '', $else = '' ) { |
| 52 | + global $wgUser; |
| 53 | + $parser->disableCache(); |
| 54 | + |
| 55 | + if($wgUser->isAllowed('protect')) { |
| 56 | + return $then; |
| 57 | + } |
| 58 | + return $else; |
| 59 | + } |
| 60 | + |
| 61 | + function ifsysopObj( &$parser, $frame, $args ) { |
| 62 | + global $wgUser; |
| 63 | + |
| 64 | + if($wgUser->isAllowed('protect')){ |
| 65 | + return isset( $args[0] ) ? trim( $frame->expand( $args[0] ) ) : ''; |
| 66 | + } else { |
| 67 | + return isset( $args[1] ) ? trim( $frame->expand( $args[1] ) ) : ''; |
| 68 | + } |
| 69 | + |
| 70 | + } |
| 71 | + |
| 72 | + function ifingroup( &$parser, $grp = '', $then = '', $else = '' ) { |
| 73 | + global $wgUser; |
| 74 | + $parser->disableCache(); |
| 75 | + |
| 76 | + if($grp!==''){ |
| 77 | + if(in_array($grp,$wgUser->getEffectiveGroups())) { |
| 78 | + return $then; |
| 79 | + } |
| 80 | + return $else; |
| 81 | + } else { |
| 82 | + return $else; |
| 83 | + } |
| 84 | + } |
| 85 | + |
| 86 | + function ifingroupObj( &$parser, $frame, $args ) { |
| 87 | + global $wgUser; |
| 88 | + $grp = isset( $args[0] ) ? trim( $frame->expand( $args[0] ) ) : ''; |
| 89 | + |
| 90 | + if($grp!==''){ |
| 91 | + if(in_array($grp,$wgUser->getEffectiveGroups())) { |
| 92 | + return isset( $args[1] ) ? trim( $frame->expand( $args[1] ) ) : ''; |
| 93 | + } |
| 94 | + return isset( $args[2] ) ? trim( $frame->expand( $args[2] ) ) : ''; |
| 95 | + } else { |
| 96 | + return isset( $args[2] ) ? trim( $frame->expand( $args[2] ) ) : ''; |
| 97 | + } |
| 98 | + } |
| 99 | + |
| 100 | + function realname( &$parser, $alt = '' ) { |
| 101 | + global $wgUser; |
| 102 | + $parser->disableCache(); |
| 103 | + |
| 104 | + if($wgUser->isAnon() && $alt!=='') { |
| 105 | + return $alt; |
| 106 | + } |
| 107 | + return $wgUser->getRealName(); |
| 108 | + } |
| 109 | + |
| 110 | + function username( &$parser, $alt = '' ) { |
| 111 | + global $wgUser; |
| 112 | + $parser->disableCache(); |
| 113 | + |
| 114 | + if($wgUser->isAnon() && $alt!=='') { |
| 115 | + return $alt; |
| 116 | + } |
| 117 | + return $wgUser->getName(); |
| 118 | + } |
| 119 | + |
| 120 | + function useremail( &$parser, $alt = '' ) { |
| 121 | + global $wgUser; |
| 122 | + $parser->disableCache(); |
| 123 | + |
| 124 | + if($wgUser->isAnon() && $alt!=='') { |
| 125 | + return $alt; |
| 126 | + } |
| 127 | + return $wgUser->getEmail(); |
| 128 | + } |
| 129 | + |
| 130 | + function nickname( &$parser, $alt = '' ) { |
| 131 | + global $wgUser; |
| 132 | + $parser->disableCache(); |
| 133 | + |
| 134 | + if($wgUser->isAnon()) { |
| 135 | + if ( $alt!=='') { |
| 136 | + return $alt; |
| 137 | + } |
| 138 | + return $wgUser->getName(); |
| 139 | + } |
| 140 | + $nickname = $wgUser->getOption( 'nickname' ); |
| 141 | + $nickname = $nickname === '' ? $wgUser->getName() : $nickname; |
| 142 | + return $nickname; |
| 143 | + } |
| 144 | + |
| 145 | + function ip( &$parser ) { |
| 146 | + $parser->disableCache(); |
| 147 | + return wfGetIP(); |
| 148 | + } |
| 149 | + |
| 150 | +} |
Index: trunk/extensions/UserFunctions/UserFunctions.i18n.php |
— | — | @@ -0,0 +1,13 @@ |
| 2 | +<?php |
| 3 | +/** |
| 4 | + * Internationalisation file for extension UserFunctions. |
| 5 | + * |
| 6 | + * @file |
| 7 | + * @ingroup Extensions |
| 8 | + */ |
| 9 | + |
| 10 | +$messages = array(); |
| 11 | + |
| 12 | +$messages['en'] = array( |
| 13 | + 'ufunc_desc' => 'Enhance parser with user functions', |
| 14 | +); |
Index: trunk/extensions/UserFunctions/UserFunctions.php |
— | — | @@ -0,0 +1,98 @@ |
| 2 | +<?php |
| 3 | +/** |
| 4 | + * UserFunctions extension - Provides a set of dynamic parser functions that trigger on the current user. |
| 5 | + * @version 2.0 - 2011/12/13 (Based on ParserFunctions) |
| 6 | + * |
| 7 | + * @link http://www.mediawiki.org/wiki/Extension:UserFunctions Documentation |
| 8 | + * |
| 9 | + * @file UserFunctions.php |
| 10 | + * @ingroup Extensions |
| 11 | + * @package MediaWiki |
| 12 | + * @author Algorithm |
| 13 | + * @author Lexw |
| 14 | + * @author Louperivois |
| 15 | + * @author Wikinaut |
| 16 | + * @author Kghbln |
| 17 | + * @author Toniher |
| 18 | + * @copyright (C) 2006 Algorithm |
| 19 | + * @license http://www.gnu.org/copyleft/gpl.html GNU General Public License 2.0 or later |
| 20 | + */ |
| 21 | + |
| 22 | +if ( !defined( 'MEDIAWIKI' ) ) { |
| 23 | + die( 'This file is a MediaWiki extension, it is not a valid entry point' ); |
| 24 | +} |
| 25 | + |
| 26 | +$wgExtensionFunctions[] = 'wfSetupUserFunctions'; |
| 27 | +$wgExtensionCredits['parserhook'][] = array( |
| 28 | + 'path' => __FILE__, |
| 29 | + 'name' => 'UserFunctions', |
| 30 | + 'version' => '2.0', |
| 31 | + 'url' => 'http://www.mediawiki.org/wiki/Extension:UserFunctions', |
| 32 | + 'author' => array( 'Algorithm ', 'Toniher', 'Kghbln', 'al.' ), |
| 33 | + 'description' => 'Enhance parser with user functions', |
| 34 | + 'descriptionmsg' => 'ufunc_desc', |
| 35 | +); |
| 36 | + |
| 37 | + |
| 38 | +$wgAutoloadClasses['ExtUserFunctions'] = dirname(__FILE__).'/UserFunctions_body.php'; |
| 39 | +$wgExtensionMessagesFiles['UserFunctions'] = dirname( __FILE__ ) . '/UserFunctions.i18n.php'; |
| 40 | +$wgExtensionMessagesFiles['UserFunctionsMagic'] = dirname( __FILE__ ) . '/UserFunctions.i18n.magic.php'; |
| 41 | + |
| 42 | + |
| 43 | +function wfSetupUserFunctions() { |
| 44 | + global $wgUFHookStub, $wgHooks; |
| 45 | + |
| 46 | + $wgUFHookStub = new UserFunctions_HookStub; |
| 47 | + |
| 48 | + $wgHooks['ParserFirstCallInit'][] = array( &$wgUFHookStub, 'registerParser' ); |
| 49 | + $wgHooks['ParserClearState'][] = array( &$wgUFHookStub, 'clearState' ); |
| 50 | +} |
| 51 | + |
| 52 | +/** |
| 53 | + * Stub class to defer loading of the bulk of the code until a User function is |
| 54 | + * actually used. |
| 55 | + */ |
| 56 | +class UserFunctions_HookStub { |
| 57 | + var $realObj; |
| 58 | + |
| 59 | + function registerParser( &$parser ) { |
| 60 | + |
| 61 | + if ( defined( get_class( $parser ) . '::SFH_OBJECT_ARGS' ) ) { |
| 62 | + // These functions accept DOM-style arguments |
| 63 | + $parser->setFunctionHook( 'ifanon', array( &$this, 'ifanonObj' ), SFH_OBJECT_ARGS ); |
| 64 | + $parser->setFunctionHook( 'ifblocked', array( &$this, 'ifblockedObj' ), SFH_OBJECT_ARGS ); |
| 65 | + $parser->setFunctionHook( 'ifsysop', array( &$this, 'ifsysopObj' ), SFH_OBJECT_ARGS ); |
| 66 | + $parser->setFunctionHook( 'ifingroup', array( &$this, 'ifingroupObj' ), SFH_OBJECT_ARGS ); |
| 67 | + } else { |
| 68 | + $parser->setFunctionHook( 'ifanon', array( &$this, 'ifanon' ) ); |
| 69 | + $parser->setFunctionHook( 'ifblocked', array( &$this, 'ifblocked' ) ); |
| 70 | + $parser->setFunctionHook( 'ifsysop', array( &$this, 'ifsysop' ) ); |
| 71 | + $parser->setFunctionHook( 'ifingroup', array( &$this, 'ifingroup' ) ); |
| 72 | + } |
| 73 | + |
| 74 | + $parser->setFunctionHook( 'realname', array( &$this, 'realname' ) ); |
| 75 | + $parser->setFunctionHook( 'username', array( &$this, 'username' ) ); |
| 76 | + $parser->setFunctionHook( 'useremail', array( &$this, 'useremail' ) ); |
| 77 | + $parser->setFunctionHook( 'nickname', array( &$this, 'nickname' ) ); |
| 78 | + $parser->setFunctionHook( 'ip', array( &$this, 'ip' ) ); |
| 79 | + |
| 80 | + return true; |
| 81 | + } |
| 82 | + |
| 83 | + /** Defer ParserClearState */ |
| 84 | + function clearState( &$parser ) { |
| 85 | + if ( !is_null( $this->realObj ) ) { |
| 86 | + $this->realObj->clearState( $parser ); |
| 87 | + } |
| 88 | + return true; |
| 89 | + } |
| 90 | + |
| 91 | + /** Pass through function call */ |
| 92 | + function __call( $name, $args ) { |
| 93 | + if ( is_null( $this->realObj ) ) { |
| 94 | + $this->realObj = new ExtUserFunctions; |
| 95 | + $this->realObj->clearState( $args[0] ); |
| 96 | + } |
| 97 | + return call_user_func_array( array( $this->realObj, $name ), $args ); |
| 98 | + } |
| 99 | +} |