r93523 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r93522‎ | r93523 | r93524 >
Date:13:55, 30 July 2011
Author:ashley
Status:deferred (Comments)
Tags:
Comment:
Automatic Board Welcome extension -- automatically posts a welcome message on new users' user boards on account creation.
The message is sent by a randomly-chosen administrator (one who is a member of the 'sysop' group), who is not blocked.
Requires SocialProfile, obviously.
Modified paths:
  • /trunk/extensions/AutomaticBoardWelcome (added) (history)
  • /trunk/extensions/AutomaticBoardWelcome/AutomaticBoardWelcome.php (added) (history)

Diff [purge]

Index: trunk/extensions/AutomaticBoardWelcome/AutomaticBoardWelcome.php
@@ -0,0 +1,84 @@
 2+<?php
 3+/**
 4+ * Automatic Board Welcome -- automatically posts a welcome message on new
 5+ * users' user boards on account creation.
 6+ * The message is sent by a randomly-chosen administrator (one who is a member
 7+ * of the 'sysop' group).
 8+ *
 9+ * @file
 10+ * @ingroup Extensions
 11+ * @version 0.1
 12+ * @date 20 July 2011
 13+ * @author Jack Phoenix <jack@countervandalism.net>
 14+ * @license http://en.wikipedia.org/wiki/Public_domain Public domain
 15+ */
 16+if ( !defined( 'MEDIAWIKI' ) ) {
 17+ die( 'This is not a valid entry point to MediaWiki.' );
 18+}
 19+
 20+// Extension credits that will show up on Special:Version
 21+$wgExtensionCredits['other'][] = array(
 22+ 'name' => 'Automatic Board Welcome',
 23+ 'version' => '0.1',
 24+ 'author' => 'Jack Phoenix',
 25+ 'description' => 'Automatically posts [[MediaWiki:User-board-welcome-message|a welcome message]] on new users\' user boards after account creation',
 26+ 'url' => 'http://www.mediawiki.org/wiki/Extension:Automatic_Board_Welcome',
 27+);
 28+
 29+$wgHooks['AddNewAccount'][] = 'wfSendUserBoardMessageOnRegistration';
 30+/**
 31+ * Send the message if the UserBoard class exists (duh!) and the welcome
 32+ * message has some content.
 33+ *
 34+ * @param $user User: the new User object being created
 35+ * @param $byEmail Boolean: true if the account was created by e-mail
 36+ * @return Boolean: true
 37+ */
 38+function wfSendUserBoardMessageOnRegistration( $user, $byEmail ) {
 39+ if ( class_exists( 'UserBoard' ) && $user instanceof User ) {
 40+ $message = trim( wfMsgForContent( 'user-board-welcome-message' ) );
 41+
 42+ // If the welcome message is empty, short-circuit right away.
 43+ if( wfEmptyMsg( 'user-board-welcome-message', $message ) ) {
 44+ return true;
 45+ }
 46+
 47+ $dbr = wfGetDB( DB_SLAVE );
 48+ // Get all users who are in the 'sysop' group from the database
 49+ $res = $dbr->select(
 50+ 'user_groups',
 51+ array( 'ug_group', 'ug_user' ),
 52+ array( 'ug_group' => 'sysop' ),
 53+ __METHOD__
 54+ );
 55+
 56+ $adminUids = array();
 57+ foreach ( $res as $row ) {
 58+ $adminUids[] = $row->ug_user;
 59+ }
 60+
 61+ // Pick one UID from the array of admin user IDs
 62+ $random = array_rand( array_flip( $adminUids ), 1 );
 63+ $sender = User::newFromId( $random );
 64+
 65+ // Ignore blocked users who have +sysop and only send out the message
 66+ // when we can, i.e. when the DB is *not* locked
 67+ if ( !$sender->isBlocked() && !wfReadOnly() ) {
 68+ $senderUid = $sender->getId();
 69+ $senderName = $sender->getName();
 70+
 71+ $b = new UserBoard();
 72+ $b->sendBoardMessage(
 73+ $senderUid, // sender's UID
 74+ $senderName, // sender's name
 75+ $user->getId(),
 76+ $user->getName(),
 77+ // passing the senderName as an argument here so that we can do
 78+ // stuff like [[User talk:$1|contact me]] or w/e in the message
 79+ wfMsgForContent( 'user-board-welcome-message', $senderName )
 80+ // the final argument is message type: 0 (default) for public
 81+ );
 82+ }
 83+ }
 84+ return true;
 85+}
\ No newline at end of file
Property changes on: trunk/extensions/AutomaticBoardWelcome/AutomaticBoardWelcome.php
___________________________________________________________________
Added: svn:eol-style
186 + native

Follow-up revisions

RevisionCommit summaryAuthorDate
r93538AutomaticBoardWelcome: follow-up to r93523: ignore blocked sysops (thanks iAl...ashley16:10, 30 July 2011
r94878Add new extension (r93523) to translatewiki.net...raymond08:36, 18 August 2011

Comments

#Comment by Nikerabbit (talk | contribs)   14:34, 30 July 2011

Looks like that wfEmptyMsg call might fail in recent version of MediaWiki which ignores the second parameter and users interface language instead.

#Comment by Jack Phoenix (talk | contribs)   16:15, 30 July 2011

If by recent you mean 1.17 or newer, I wouldn't worry too much about it since SocialProfile currently runs as intended only on 1.16. See also bug #29984.

#Comment by IAlex (talk | contribs)   14:41, 30 July 2011

Unless I'm mistaken, the message is not sent at all if the user selected to send the message is blocked. Shouldn't the extension select another sysop in such case?

#Comment by Jack Phoenix (talk | contribs)   16:12, 30 July 2011

Good catch, thanks! This should be fixed as of r93538.

Status & tagging log