r23946 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r23945‎ | r23946 | r23947 >
Date:10:25, 10 July 2007
Author:robchurch
Status:old
Tags:
Comment:
* Refactor, split class into own file, etc.
* Update to work with 1.8.0 and later, the branched version will work for older installs
Modified paths:
  • /trunk/extensions/NewUserNotif/NewUserNotif.class.php (added) (history)
  • /trunk/extensions/NewUserNotif/NewUserNotif.i18n.php (added) (history)
  • /trunk/extensions/NewUserNotif/NewUserNotif.php (modified) (history)
  • /trunk/extensions/NewUserNotif/README (modified) (history)

Diff [purge]

Index: trunk/extensions/NewUserNotif/NewUserNotif.class.php
@@ -0,0 +1,111 @@
 2+<?php
 3+
 4+/**
 5+ * Extension to provide customisable email notification of new user creation
 6+ *
 7+ * @addtogroup Extensions
 8+ * @author Rob Church <robchur@gmail.com>
 9+ */
 10+
 11+require_once( 'UserMailer.php' );
 12+
 13+class NewUserNotifier {
 14+
 15+ private $sender;
 16+ private $user;
 17+
 18+ /**
 19+ * Constructor
 20+ */
 21+ public function NewUserNotifier() {
 22+ global $wgNewUserNotifSender;
 23+ $this->sender = $wgNewUserNotifSender;
 24+ }
 25+
 26+ /**
 27+ * Send all email notifications
 28+ *
 29+ * @param User $user User that was created
 30+ */
 31+ public function execute( $user ) {
 32+ $this->user = $user;
 33+ $this->sendExternalMails();
 34+ $this->sendInternalMails();
 35+ }
 36+
 37+ /**
 38+ * Send email to external addresses
 39+ */
 40+ private function sendExternalMails() {
 41+ global $wgNewUserNotifEmailTargets, $wgSitename;
 42+ foreach( $wgNewUserNotifEmailTargets as $target ) {
 43+ userMailer(
 44+ new MailAddress( $target ),
 45+ new MailAddress( $this->sender ),
 46+ wfMsgForContent( 'newusernotifsubj', $wgSitename ),
 47+ $this->makeMessage( $target, $this->user )
 48+ );
 49+ }
 50+ }
 51+
 52+ /**
 53+ * Send email to users
 54+ */
 55+ private function sendInternalMails() {
 56+ global $wgNewUserNotifTargets, $wgSitename;
 57+ foreach( $wgNewUserNotifTargets as $userSpec ) {
 58+ $user = $this->makeUser( $userSpec );
 59+ if( $user instanceof User && $user->isEmailConfirmed() ) {
 60+ $user->sendMail(
 61+ wfMsgForContent( 'newusernotifsubj', $wgSitename ),
 62+ $this->makeMessage( $user->getName(), $this->user ),
 63+ $this->sender
 64+ );
 65+ }
 66+ }
 67+ }
 68+
 69+ /**
 70+ * Initialise a user from an identifier or a username
 71+ *
 72+ * @param mixed $spec User identifier or name
 73+ * @return User
 74+ */
 75+ private function makeUser( $spec ) {
 76+ $name = is_integer( $spec ) ? User::whoIs( $spec ) : $spec;
 77+ $user = User::newFromName( $name );
 78+ if( $user instanceof User && $user->getId() > 0 )
 79+ return $user;
 80+ return null;
 81+ }
 82+
 83+ /**
 84+ * Build a notification email
 85+ *
 86+ * @param string $recipient Name of the recipient
 87+ * @param User $user User that was created
 88+ */
 89+ private function makeMessage( $recipient, $user ) {
 90+ global $wgSitename, $wgContLang;
 91+ return wfMsgForContent(
 92+ 'newusernotifbody',
 93+ $recipient,
 94+ $user->getName(),
 95+ $wgSitename,
 96+ $wgContLang->timeAndDate( wfTimestampNow() )
 97+ );
 98+ }
 99+
 100+ /**
 101+ * Hook account creation
 102+ *
 103+ * @param User $user User that was created
 104+ * @return bool
 105+ */
 106+ public static function hook( $user ) {
 107+ $notifier = new self();
 108+ $notifier->execute( $user );
 109+ return true;
 110+ }
 111+
 112+}
\ No newline at end of file
Property changes on: trunk/extensions/NewUserNotif/NewUserNotif.class.php
___________________________________________________________________
Added: svn:eol-style
1113 + native
Index: trunk/extensions/NewUserNotif/NewUserNotif.i18n.php
@@ -0,0 +1,20 @@
 2+<?php
 3+
 4+/**
 5+ * Internationalisation file for the New User Email Notification extension
 6+ *
 7+ * @addtogroup Extensions
 8+ * @author Rob Church <robchur@gmail.com>
 9+ */
 10+
 11+function efNewUserNotifMessages() {
 12+ $messages = array(
 13+
 14+'en' => array(
 15+ 'newusernotifsubj' => 'New User Notification for $1',
 16+ 'newusernotifbody' => "Hello $1,\n\nA new user account, $2, has been created on $3 at $4.",
 17+),
 18+
 19+ );
 20+ return $messages;
 21+}
\ No newline at end of file
Property changes on: trunk/extensions/NewUserNotif/NewUserNotif.i18n.php
___________________________________________________________________
Added: svn:eol-style
122 + native
Index: trunk/extensions/NewUserNotif/README
@@ -1,7 +1,7 @@
22 NEW USER EMAIL NOTIFICATION EXTENSION
33
4 - Version 1.3
5 - © 2006 Rob Church
 4+ Version 1.4
 5+ © 2006-2007 Rob Church
66
77 This is free software licenced under the GNU General Public Licence. Please
88 see http://www.gnu.org/copyleft/gpl.html for further details, including the
@@ -14,8 +14,7 @@
1515 3. Installing the extension
1616 4. Editing the notification email
1717 5. Further configuration
18 - 6. Change log
19 - 7. Feedback
 18+ 6. Feedback
2019
2120 == 1. Introduction ==
2221
@@ -26,11 +25,11 @@
2726
2827 == 2. Installation requirements ==
2928
30 -This extension requires MediaWiki 1.5.0 or later.
 29+This extension requires MediaWiki 1.8.0 or later.
3130
3231 == 3. Installing the extension ==
3332
34 -To install the extension, place all extension files into a NewUserNotif
 33+To install the extension, place all extension files into a NewUserNotif/
3534 directory within your MediaWiki extensions/ directory, then edit
3635 LocalSettings.php and add the following line
3736
@@ -43,7 +42,7 @@
4443 to customise them, as detailed below:
4544
4645 MediaWiki:Newusernotifsubj
47 - This file contains the subject line for the email.
 46+ This file contains the subject line for the email;
4847 $1 is replaced with the wiki site name from $wgSitename.
4948
5049 MediaWiki:Newusernotifbody
@@ -75,23 +74,6 @@
7675 Array containing email addresses to which a notification should also be sent
7776 Defaults to no additional addresses
7877
79 -== 6. Change log ==
 78+== 6. Feedback ==
8079
81 -Version 1.1
82 -17/02/2006
83 - Added external email addresses feature
84 -
85 -Version 1.2
86 -30/04/2006
87 - Update to work with new hook format in MediaWiki 1.7
88 - Fix date/time handling
89 -
90 -Version 1.3
91 -01/05/2006
92 - Rewrite for performance and code cleanliness
93 - Fix backwards-compatibility with MediaWiki 1.5
94 - Support usernames as values for $wgNewUserNotifTargets
95 -
96 -== 7. Feedback ==
97 -
9880 All feedback, bug reports, etc. welcome via <robchur@gmail.com>.
\ No newline at end of file
Index: trunk/extensions/NewUserNotif/NewUserNotif.php
@@ -11,148 +11,41 @@
1212
1313 if( defined( 'MEDIAWIKI' ) ) {
1414
15 - require_once( 'UserMailer.php' );
16 -
 15+ $wgAutoloadClasses['NewUserNotifier'] = dirname( __FILE__ ) . '/NewUserNotif.class.php';
1716 $wgExtensionFunctions[] = 'efNewUserNotifSetup';
18 - $wgExtensionCredits['other'][] = array( 'name' => 'New User Email Notification', 'author' => 'Rob Church', 'url' => 'http://www.mediawiki.org/wiki/Extension:New_User_Email_Notification' );
 17+ $wgExtensionCredits['other'][] = array(
 18+ 'name' => 'New User Email Notification',
 19+ 'author' => 'Rob Church',
 20+ 'url' => 'http://www.mediawiki.org/wiki/Extension:New_User_Email_Notification',
 21+ 'description' => 'Sends email notification when user accounts are created',
 22+ );
1923
 24+ /**
 25+ * Email address to use as the sender
 26+ */
2027 $wgNewUserNotifSender = $wgPasswordSender;
 28+
 29+ /**
 30+ * Users who should receive notification mails
 31+ */
2132 $wgNewUserNotifTargets[] = 1;
 33+
 34+ /**
 35+ * Additional email addresses to send mails to
 36+ */
2237 $wgNewUserNotifEmailTargets = array();
2338
 39+ /**
 40+ * Extension setup
 41+ */
2442 function efNewUserNotifSetup() {
2543 global $wgHooks, $wgMessageCache;
26 - $wgHooks['AddNewAccount'][] = 'efNewUserNotif';
27 - $wgMessageCache->addMessage( 'newusernotifsubj', 'New User Notification for $1' );
28 - $wgMessageCache->addMessage( 'newusernotifbody', "Hello $1,\n\nA new user account, $2, has been created on $3 at $4." );
 44+ $wgHooks['AddNewAccount'][] = 'NewUserNotifier::hook';
 45+ require_once( dirname( __FILE__ ) . '/NewUserNotif.i18n.php' );
 46+ foreach( efNewUserNotifMessages() as $lang => $messages )
 47+ $wgMessageCache->addMessages( $messages, $lang );
2948 }
3049
31 - function efNewUserNotif( $user = NULL ) {
32 - global $wgUser;
33 - # Backwards-compatible fiddling
34 - if( is_null( $user ) )
35 - $user =& $wgUser;
36 - $notifier = new NewUserNotifier();
37 - $notifier->execute( $user );
38 -
39 - return true;
40 - }
41 -
42 - class NewUserNotifier {
43 -
44 - var $sender;
45 - var $user;
46 -
47 - /**
48 - * Constructor
49 - * Set up the sender
50 - */
51 - function NewUserNotifier() {
52 - global $wgNewUserNotifSender;
53 - $this->sender = $wgNewUserNotifSender;
54 - }
55 -
56 - /**
57 - * Format an email address in a backwards-compatible fashion
58 - * 1.6 introduced a wrapper for it, but this isn't helpful for 1.5 installs
59 - *
60 - * @param $address Email address
61 - * @return mixed
62 - */
63 - function mailAddress( $address ) {
64 - return class_exists( 'MailAddress' ) ? new MailAddress( $address ) : $address;
65 - }
66 -
67 - /**
68 - * Send all email notifications
69 - *
70 - * @param $user User that was created
71 - */
72 - function execute( &$user ) {
73 - $this->user =& $user;
74 - $this->sendExternalMails();
75 - $this->sendInternalMails();
76 - }
77 -
78 - /**
79 - * Return the subject for notification emails
80 - *
81 - * @return string
82 - */
83 - function makeSubject() {
84 - global $wgSitename;
85 - static $subject = false;
86 - if( !$subject )
87 - $subject = wfMsgForContent( 'newusernotifsubj', $wgSitename );
88 - return $subject;
89 - }
90 -
91 - /**
92 - * Return the text of a notification email
93 - *
94 - * @param $recipient Name of the recipient
95 - * @param $user User that was created
96 - */
97 - function makeMessage( $recipient, &$user ) {
98 - global $wgSitename;
99 - return wfMsgForContent( 'newusernotifbody', $recipient, $user->getName(), $wgSitename, $this->makeTimestamp() );
100 - }
101 -
102 - /**
103 - * Return an appropriate timestamp
104 - *
105 - * @param $lang Language object
106 - * @return string
107 - */
108 - function makeTimestamp() {
109 - global $wgContLang;
110 - return $wgContLang->timeAndDate( wfTimestampNow() );
111 - }
112 -
113 - /**
114 - * Send email to external addresses
115 - */
116 - function sendExternalMails() {
117 - global $wgNewUserNotifEmailTargets, $wgContLang;
118 - foreach( $wgNewUserNotifEmailTargets as $target ) {
119 - $message = $this->makeMessage( $target, $this->user );
120 - userMailer( $this->mailAddress( $target ), $this->mailAddress( $this->sender ), $this->makeSubject(), $message );
121 - }
122 - }
123 -
124 - /**
125 - * Send email to users
126 - */
127 - function sendInternalMails() {
128 - global $wgNewUserNotifTargets;
129 - foreach( $wgNewUserNotifTargets as $userSpec ) {
130 - if( $user =& $this->makeUser( $userSpec ) ) {
131 - $message = $this->makeMessage( $user->getName(), $this->user );
132 - if( $user->isEmailConfirmed() )
133 - $user->sendMail( $this->makeSubject(), $message, $this->sender );
134 - }
135 - }
136 - }
137 -
138 - /**
139 - * Initialise a user from an identifier or a username
140 - *
141 - * @param $spec User identifier or name
142 - * @return mixed
143 - */
144 - function makeUser( $spec ) {
145 - $name = is_integer( $spec ) ? User::whoIs( $spec ) : $spec;
146 - $user = User::newFromName( $name );
147 - if( is_object( $user ) ) {
148 - $user->loadFromDatabase();
149 - if( $user->getId() > 0 )
150 - return $user;
151 - }
152 - return false;
153 - }
154 -
155 - }
156 -
15750 } else {
15851 echo( "This file is an extension to the MediaWiki software and cannot be used standalone.\n" );
15952 die( 1 );

Status & tagging log