r68257 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r68256‎ | r68257 | r68258 >
Date:23:14, 18 June 2010
Author:jdpond
Status:reverted (Comments)
Tags:
Comment:
1.5.2 Allows you to add to or modify the parameters passed to both the email subject line and the body without modifying the extension code.
Modified paths:
  • /trunk/extensions/NewUserNotif/NewUserNotif.class.php (modified) (history)
  • /trunk/extensions/NewUserNotif/NewUserNotif.php (modified) (history)
  • /trunk/extensions/NewUserNotif/README (modified) (history)

Diff [purge]

Index: trunk/extensions/NewUserNotif/README
@@ -1,4 +1,5 @@
22 NEW USER EMAIL NOTIFICATION EXTENSION
 3+(http://www.mediawiki.org/wiki/Extension:New_User_Email_Notification)
34
45 Version 1.5
56 © 2006-2007 Rob Church
@@ -13,8 +14,9 @@
1415 2. Installation requirements
1516 3. Installing the extension
1617 4. Editing the notification email
17 - 5. Further configuration
18 - 6. Feedback
 18+ 5. Configuration
 19+ 6. Advanced Configuration
 20+ 7. Feedback
1921
2022 == 1. Introduction ==
2123
@@ -51,9 +53,12 @@
5254 $2 is replaced with the username of the new user account;
5355 $3 is replaced with the wiki site name from $wgSitename;
5456 $4 is replaced with the time and date of the account's creation.
 57+ $5 is replaced with the date of the account's creation.
 58+ $6 is replaced with the time of the account's creation.
5559
56 -== 5. Further configuration ==
5760
 61+== 5. Configuration ==
 62+
5863 The behaviour of the extension can be tweaked using three configuration
5964 variables. To override the defaults for these, set them in LocalSettings.php
6065 underneath the call to the extension file.
@@ -73,7 +78,76 @@
7479 $wgNewUserNotifEmailTargets
7580 Array containing email addresses to which a notification should also be sent
7681 Defaults to no additional addresses
 82+
 83+== 6. Advanced Configuration ==
7784
78 -== 6. Feedback ==
 85+As of version 1.5.2 of this extension, you can customize and add parameters passed to both the subject and body messages in your localsettings.php without modifying the extension code.
7986
80 -All feedback, bug reports, etc. welcome via <robchur@gmail.com>.
\ No newline at end of file
 87+You can do this by adding (or changing) parameter values and/or functions that return the desired parameter values to the parameter definition arrays underneath the call to the extension file. In both cases you can use references to $this, $user (created user object), $recipient (target), or from globals $wfContLang, $wgSitename. (For advanced techniques and using other globals, see below).
 88+
 89+$wgNewUserNotifSenderSubjParam
 90+ The list of evaluated parameters passed to the message subject(MediaWiki:Newusernotifsubj). Default is:
 91+ '$wgSitename', // $1 Site Name
 92+
 93+$wgNewUserNotifSenderParam
 94+ The list of evaluated parameters passed to the message body(MediaWiki:Newusernotifbody). Defaults are:
 95+ '$recipient', // $1 Recipient (of notification message)
 96+ '$user->getname()', // $2 User Name
 97+ '$wgSitename', // $3 Site Name
 98+ '$wgContLang->timeAndDate( wfTimestampNow() )', // $4 Time and date stamp
 99+ '$wgContLang->date( wfTimestampNow() )', // $5 Date Stamp
 100+ '$wgContLang->time( wfTimestampNow() )', // $6 Time Stamp
 101+
 102+You can then either edit MediaWiki:Newusernotifbody to make use of the new parameters (e.g. add "\n\nThe request came from $7.") or alternatively if you haven't customised the message you can edit NewUserNotif.i18n.php and make the change there to avoid creating a new wiki page (e.g. if your wiki is in English, change the 'en' array.)
 103+
 104+=== 6.1 Upgrading from 1.5.1 ===
 105+
 106+If you did not customize the passed parameters, this extension works exactly as before and no action need to be taken.
 107+
 108+If you added to or changed the default passed parameters, you will need to correspondingly add to or change the corresponding arrays. You should be able to take the exact same code and add to or replace it in the arrays.
 109+
 110+For example, if you added a 7th parameter (in his case, the email address of the new user), you would just add it (in localsettings.php, below the extension reference) like this:
 111+
 112+<source lang="php">
 113+$wgNewUserNotifSenderParam[] = '$user->getEmail()'; // $7 new user email address
 114+</source>
 115+
 116+The code for the parameter to be passed should be exactly the same (except enclosed in quotes) as it is in your modified wfMsgForContent function call found in private function makeMessage.
 117+
 118+=== 6.2 Examples For Adding Additional Parameters ===
 119+
 120+Here are some commonly used additional parameter you could add to further customize your notification message subject and body:
 121+<source lang="php">
 122+$wgNewUserNotifSenderParam[] = '$user->getEmail()'; // $7 email
 123+$wgNewUserNotifSenderParam[] = 'rawurlencode($wgSitename)'; // $8 Escaped for email message
 124+$wgNewUserNotifSenderParam[] = 'wfGetIP()'; // $9 Submitter's IP Address
 125+</source>
 126+
 127+Of course if you wanted to pass these parameter(s) to the subject message, you would add them to the $wgNewUserNotifSenderSubjParam array instead of the $wgNewUserNotifSenderParam array.
 128+
 129+=== 6.4 Some Technique/Tricks for Adding Additional Parameters ===
 130+
 131+Let's say you wanted to use a global variable or reference other than $this, $user (created user object), $recipient (target), $wfContLang, or $wgSitename.
 132+
 133+You could do so by directly referencing a previously assigned global. Example:
 134+
 135+<source lang="php">
 136+$wgNewUserNotifSenderParam[] = '$GLOBALS["wgUser"]->getEmail()'; // $10 email
 137+</source>
 138+
 139+Note: This isn't the best example, because it would be easier to use the $user object ('$user->getEmail()'), but it gives you an idea how you could reference the global variable (in this case objec) $wgUser directly.
 140+
 141+=== 6.5 Advanced Customization Prior to 1.5.2 ===
 142+
 143+If you want to change the parameters passed in a version prior to 1.5.2, you would need to modify the extension code directly in wfMsgForContent function call found in private function makeMessage of NewUserNotif.class.php.
 144+
 145+== 7. Feedback ==
 146+
 147+Bugs and enhancement requests should be submitted through bugzilla for the MediaWiki Extensions at:
 148+
 149+https://bugzilla.wikimedia.org/enter_bug.cgi?product=MediaWiki%20extensions
 150+
 151+For comments and discussion, see:
 152+
 153+http://www.mediawiki.org/wiki/Extension_talk:New_User_Email_Notification
 154+
Index: trunk/extensions/NewUserNotif/NewUserNotif.php
@@ -15,7 +15,7 @@
1616 $wgExtensionCredits['other'][] = array(
1717 'path' => __FILE__,
1818 'name' => 'New User Email Notification',
19 - 'version' => '1.5.1',
 19+ 'version' => '1.5.2',
2020 'author' => 'Rob Church',
2121 'url' => 'http://www.mediawiki.org/wiki/Extension:New_User_Email_Notification',
2222 'descriptionmsg' => 'newusernotif-desc',
@@ -40,6 +40,26 @@
4141 * Additional email addresses to send mails to
4242 */
4343 $wgNewUserNotifEmailTargets = array();
 44+/**
 45+ * These are the parameters that will be passed into MediaWiki:newusernotifbody
 46+ * Can use anthing available as part of $this, $user (created user object), $recipient (target),
 47+ * or from globals $wfContLang, $wgSitename
 48+ */
 49+$wgNewUserNotifSenderParam = array(
 50+ '$recipient', // $1 Recipient (of notification message)
 51+ '$user->getname()', // $2 User Name
 52+ '$wgSitename', // $3 Site Name
 53+ '$wgContLang->timeAndDate( wfTimestampNow() )', // $4 Time and date stamp
 54+ '$wgContLang->date( wfTimestampNow() )', // $5 Date Stamp
 55+ '$wgContLang->time( wfTimestampNow() )', // $6 Time Stamp
 56+ );
 57+/**
 58+ * These are the parameters that will be passed into MediaWiki:Newusernotifsubj (for use in the "subject:" line)
 59+ * parameters defs have same options as $wgNewUserNotifSenderParam
 60+ */
 61+$wgNewUserNotifSenderSubjParam = array(
 62+ '$wgSitename', // $1 Site Name
 63+ );
4464
4565 /**
4666 * Extension setup
Index: trunk/extensions/NewUserNotif/NewUserNotif.class.php
@@ -39,13 +39,13 @@
4040 * Send email to external addresses
4141 */
4242 private function sendExternalMails() {
43 - global $wgNewUserNotifEmailTargets, $wgSitename;
 43+ global $wgNewUserNotifEmailTargets, $wgNewUserNotifSenderParam, $wgNewUserNotifSenderSubjParam;
4444 foreach( $wgNewUserNotifEmailTargets as $target ) {
4545 userMailer(
4646 new MailAddress( $target ),
4747 new MailAddress( $this->sender ),
48 - wfMsgForContent( 'newusernotifsubj', $wgSitename ),
49 - $this->makeMessage( $target, $this->user )
 48+ $this->makeMessage( $target, $this->user, 'newusernotifsubj', $wgNewUserNotifSenderSubjParam),
 49+ $this->makeMessage( $target, $this->user, 'newusernotifbody', $wgNewUserNotifSenderParam)
5050 );
5151 }
5252 }
@@ -54,13 +54,13 @@
5555 * Send email to users
5656 */
5757 private function sendInternalMails() {
58 - global $wgNewUserNotifTargets, $wgSitename;
 58+ global $wgNewUserNotifTargets, $wgNewUserNotifSenderParam, $wgNewUserNotifSenderSubjParam;
5959 foreach( $wgNewUserNotifTargets as $userSpec ) {
6060 $user = $this->makeUser( $userSpec );
6161 if( $user instanceof User && $user->isEmailConfirmed() ) {
6262 $user->sendMail(
63 - wfMsgForContent( 'newusernotifsubj', $wgSitename ),
64 - $this->makeMessage( $user->getName(), $this->user ),
 63+ $this->makeMessage( $user->getName(), $this->user, 'newusernotifsubj', $wgNewUserNotifSenderSubjParam ),
 64+ $this->makeMessage( $user->getName(), $this->user, 'newusernotifbody', $wgNewUserNotifSenderParam ),
6565 $this->sender
6666 );
6767 }
@@ -82,22 +82,18 @@
8383 }
8484
8585 /**
86 - * Build a notification email
 86+ * Build a notification email message (body and subject)
8787 *
88 - * @param string $recipient Name of the recipient
89 - * @param User $user User that was created
 88+ * @param string $recipient Name of the new user notification email recipient
 89+ * @param User $user User (object) created for new user
 90+ * @param string $msgId Localised Message Identifier
 91+ * @param string $parmArr Array of Strings eval'd to pass parameters to message
 92+ * @return string
9093 */
91 - private function makeMessage( $recipient, $user ) {
92 - global $wgSitename, $wgContLang;
93 - return wfMsgForContent(
94 - 'newusernotifbody',
95 - $recipient,
96 - $user->getName(),
97 - $wgSitename,
98 - $wgContLang->timeAndDate( wfTimestampNow() ),
99 - $wgContLang->date( wfTimestampNow() ),
100 - $wgContLang->time( wfTimestampNow() )
101 - );
 94+ private function makeMessage( $recipient, $user, $msgId, $parmArr) {
 95+ global $wgSitename,$wgContLang;
 96+ eval( "\$retval = wfMsgForContent('".$msgId."',".implode(",",$parmArr).");" );
 97+ return ($retval);
10298 }
10399
104100 /**

Follow-up revisions

RevisionCommit summaryAuthorDate
r81116Partial revert of r68257: bizarre eval() stuff added to NewUserNotif contrary...brion00:31, 28 January 2011

Comments

#Comment by Nikerabbit (talk | contribs)   07:19, 19 June 2010

I don't quite like eval, nor that it suggests editing the i18n file which should not be done..

Please fix the indentation in NewUserNotif.php (one tab per level, spaces if not at line start or don't align at all).

#Comment by Brion VIBBER (talk | contribs)   00:32, 28 January 2011

Agree that the eval() stuff is pretty awful; reverted in r81116.

#Comment by Brion VIBBER (talk | contribs)   01:08, 28 January 2011

Further notes per IRC conversation:

  • this change was made for a one-off customization job; it's a bit kludgy but got the job done for that case
  • alternate recommendations include adding a hook point (if you can edit LocalSettings.php you can write a custom function there which can feed anything you like into a message) or making sure that parserfunctions and templates can be used in the email message to add bunches of stuff (will likely cover a lot of common cases, especially if you do custom parser functions to add your variables)

Status & tagging log