r106597 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r106596‎ | r106597 | r106598 >
Date:20:32, 18 December 2011
Author:toniher
Status:deferred (Comments)
Tags:
Comment:
added UserFunction
Modified paths:
  • /trunk/extensions/UserFunctions (added) (history)
  • /trunk/extensions/UserFunctions/UserFunctions.i18n.magic.php (added) (history)
  • /trunk/extensions/UserFunctions/UserFunctions.i18n.php (added) (history)
  • /trunk/extensions/UserFunctions/UserFunctions.php (added) (history)
  • /trunk/extensions/UserFunctions/UserFunctions_body.php (added) (history)

Diff [purge]

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+}

Follow-up revisions

RevisionCommit summaryAuthorDate
r106634r106597: Consistency tweaks in preparation for adding extension to translatew...raymond10:16, 19 December 2011
r106635r106597: Adding extension to translatewiki.netraymond10:17, 19 December 2011
r106762and ...toniher07:10, 20 December 2011
r106986Adding parameter to restrict functions, using parser for getting User objecttoniher21:56, 21 December 2011
r107044adding Wikinauttoniher08:51, 22 December 2011

Comments

#Comment by Johnduhart (talk | contribs)   00:42, 19 December 2011

Please setup auto-props and fix the eol-style

#Comment by Toniher (talk | contribs)   22:35, 19 December 2011

Apologies. I thought I had configured it properly, but it seems not :( I'll double-check in my next commits.

#Comment by Platonides (talk | contribs)   21:05, 19 December 2011

I wonder, what's the usecase of these functions?

I understand you may want to chose a piece of content just to sysops or blocked users, but what's the gain in showing someone its own email?

#Comment by Toniher (talk | contribs)   22:40, 19 December 2011

I am not presently using it in my own wikis but, I imagine that, combined with a regexp extension, this could be used for filtering users with certain email addresses (imagine institutional email addresses from a specific company or department).

#Comment by Platonides (talk | contribs)   23:07, 20 December 2011

No, this wouldn't be possible. In order to limit registration or group membership by the provided (and verified!) email address, the extension would need to be completely different.

It's fine as a 'first mediawiki extension' task, but if we don't have a use case for this, I'd prefer to remove it. That serves to keep the repository "clean", but also because I don't really feel comfortable with the potential of these functions to publish what we generally consider private data.

#Comment by Toniher (talk | contribs)   00:12, 21 December 2011

Hi Platonides. I didn't mean the registration, that is a separate story. I agree that makes sense to be more cautious. I am going to add the possibility to filter which functions are enabled and I would let conservative defaults. I've just seen this was actually already suggested in the extension discussion page. Would you mind to comment there? Thanks for your comments.

#Comment by Wikinaut (talk | contribs)   21:02, 21 December 2011

> what's the gain in showing someone its own email? I use the usermail variable on certain "template pages" in my enterprise wiki, like a routing slip template. Users can log in and visit the routing slip template which is then ready for being printed, with their own name and own e-mail address. This was the reason why I introduced the variable some time ago.

I had the need, and introduced it. You may like it. Or not.

#Comment by Platonides (talk | contribs)   00:16, 22 December 2011

Well, that's a use case.

I hadn't noticed that you had participated on it. Kghbln removed you from the author list at the page template (maybe because you don't appear in the article history?).

If I understand correctly the extension history, this commit (previously at github) is a rewrite of the UserFunctions extension (keeping the interface), which used to be developed in mediawiki.org and before in meta.

Toniher, instead of "al." for "others" in the author list, you should use '...' which gets automagically translated to the 'other' in the local language.

#Comment by Wikinaut (talk | contribs)   00:31, 22 December 2011

I added the usermail variable in https://www.mediawiki.org/w/index.php?title=Extension%3AUserFunctions&action=historysubmit&diff=303076&oldid=203235 . I guess, Kghbln (hallo!) removed my username by mistake.

#Comment by Kghbln (talk | contribs)   20:23, 23 December 2011

Hi Wikinaut, I did this after Toniher rewrote the extension since I thought that the main contributors should be shown there. This was not meant to jugde the effort you put into this extension. Still, this is a wiki so you may of cause add yourself again. I guess $wgExtensionCredits is more important.

#Comment by Wikinaut (talk | contribs)   00:33, 22 December 2011

I think, we should always add _all_ authors' names in the extension credit string.

#Comment by Jack Phoenix (talk | contribs)   00:49, 22 December 2011

That wouldn't work in the long run. Think of extensions used on WMF sites and other big extensions with lots of patch contributors etc. — what constitutes as an author? Is changing one line of code enough to call yourself an author of the extension? And so on. While it would be very nice to credit everyone who contributed to the work, it just isn't practical to list them all in $wgExtensionCredits' author array.

#Comment by Wikinaut (talk | contribs)   01:01, 22 December 2011

My comment was meant as a general advice, or if you like suggestion, which could perhaps find its way into the Guidelines ( https://www.mediawiki.org/wiki/Manual:Developing_extensions and perhaps https://www.mediawiki.org/wiki/Manual:Coding_conventions ).

BTW, the "useremail" addition was more than a single-line change as you can see, but I do not insist of being mentioned.

#Comment by Kghbln (talk | contribs)   20:16, 23 December 2011

I cannot think of usecases on open wikis, but on closed wikis. On my closed wikis I show users their e-mail-address more directly to make sure that I does not contain mistakes/is current and to make sure that they actually provided one. The rate of problems coming from that reduced significantly.

#Comment by Toniher (talk | contribs)   11:32, 22 December 2011

I've already addressed, at least partially, some of the concerns that were raised in this review. Thanks for your comments.

Status & tagging log