r59519 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r59518‎ | r59519 | r59520 >
Date:16:32, 28 November 2009
Author:siebrand
Status:deferred
Tags:
Comment:
NewUserMessage will create a thread if configured correctly when LiquidThreads is used.

Updated documentation on http://www.mediawiki.org/wiki/Extension:NewUserMessage
Modified paths:
  • /trunk/extensions/NewUserMessage/NewUserMessage.class.php (modified) (history)
  • /trunk/extensions/NewUserMessage/NewUserMessage.i18n.php (modified) (history)
  • /trunk/extensions/NewUserMessage/NewUserMessage.php (modified) (history)

Diff [purge]

Index: trunk/extensions/NewUserMessage/NewUserMessage.i18n.php
@@ -9,20 +9,29 @@
1010
1111 /** English
1212 * @author [http://www.organicdesign.co.nz/User:Nad Nad]
 13+ * @author Siebrand
1314 */
1415 $messages['en'] = array(
1516 'newusermessage-desc' => "Adds a message to newly created user's talk pages",
1617 'newuseredit-summary' => 'Adding [[{{int:newusermessage-template}}|welcome message]] to new user\'s talk page',
1718 'newusermessage-template' => 'Template:Welcome', # The title of the message template
 19+ 'newusermessage-template-subject' => 'Template:Welcome-subject', # Always substituted. Used if LiquidThreads is enabled in the User_talk namespace. Do not translate or duplicate this message to other languages
 20+ 'newusermessage-template-body' => 'Template:Welcome-body', # Used if LiquidThreads is enabled in the User_talk namespace. Do not translate or duplicate this message to other languages
1821 'newusermessage-editor' => 'New user message', # The username used for the edit
1922 'newusermessage-substitute' => '', # do not translate or duplicate this message to other languages
2023 );
2124
2225 /** Message documentation (Message documentation)
2326 * @author Purodha
 27+ * @author Siebrand
2428 */
2529 $messages['qqq'] = array(
26 - 'newusermessage-desc' => 'Short description of this extension, shown on [[Special:Version]]. Do not translate or change links.',
 30+ 'newusermessage-desc' => '{{desc}}',
 31+ 'newuseredit-summary' => 'The edit summary used when placing a new user message.',
 32+ 'newusermessage-subject' => 'Used if LiquidThreads is enabled in the User_talk namespace. Do not translate or duplicate this message to other languages.',
 33+ 'newusermessage-body' => 'Used if LiquidThreads is enabled in the User_talk namespace. Do not translate or duplicate this message to other languages.',
 34+ 'newusermessage-editor' => 'The username used for the edit',
 35+ 'newusermessage-substitute' => 'If this message is not empty, the new user message templates will be substituted. Do not translate or duplicate this message to other languages.',
2736 );
2837
2938 /** Afrikaans (Afrikaans)
Index: trunk/extensions/NewUserMessage/NewUserMessage.php
@@ -9,16 +9,16 @@
1010 * @copyright 2007-10-15 [http://www.organicdesign.co.nz/nad User:Nad]
1111 */
1212
13 -if (!defined('MEDIAWIKI'))
14 - die('Not an entry point.');
 13+if ( !defined( 'MEDIAWIKI' ) )
 14+ die( 'Not an entry point.' );
1515
16 -define('NEWUSERMESSAGE_VERSION','2.2, 2009-06-01');
 16+define( 'NEWUSERMESSAGE_VERSION', '3.0, 2009-11-28' );
1717
1818 $wgNewUserSuppressRC = false; // Specify whether or not the new user message creation should show up in recent changes
1919 $wgNewUserMinorEdit = true; // Should the new user message creation be a minor edit?
2020 $wgNewUserMessageOnAutoCreate = false; // Should auto creation (CentralAuth) trigger a new user message?
2121
22 -$dir = dirname(__FILE__) . '/';
 22+$dir = dirname( __FILE__ ) . '/';
2323 $wgExtensionMessagesFiles['NewUserMessage'] = $dir . 'NewUserMessage.i18n.php';
2424 $wgAutoloadClasses['NewUserMessage'] = $dir . 'NewUserMessage.class.php';
2525
Index: trunk/extensions/NewUserMessage/NewUserMessage.class.php
@@ -7,83 +7,148 @@
88 * @author [http://www.organicdesign.co.nz/nad User:Nad]
99 * @license GNU General Public Licence 2.0 or later
1010 * @copyright 2007-10-15 [http://www.organicdesign.co.nz/nad User:Nad]
 11+ * @copyright 2009 Siebrand Mazeland
1112 */
1213
13 -if (!defined('MEDIAWIKI'))
14 - die('Not an entry point.');
 14+if ( !defined( 'MEDIAWIKI' ) )
 15+ die( 'Not an entry point.' );
1516
1617 class NewUserMessage {
1718 /*
18 - * Add the template message if the users talk page doesn't already exist
 19+ * Add the template message if the users talk page does not already exist
1920 */
2021 static function createNewUserMessage( $user ) {
2122 $talk = $user->getTalkPage();
2223
2324 if ( !$talk->exists() ) {
24 - global $wgUser, $wgNewUserMinorEdit, $wgNewUserSuppressRC;
 25+ global $wgUser, $wgLqtTalkPages;
2526
2627 $name = $user->getName();
2728 $realName = $user->getRealName();
2829
2930 wfLoadExtensionMessages( 'NewUserMessage' );
3031
31 - $article = new Article($talk);
 32+ $article = new Article( $talk );
 33+ $editSummary = wfMsgForContent( 'newuseredit-summary' );
3234
33 - // Create a user object for the editing user and add it to the database
34 - // if it's not there already
 35+ // Create a user object for the editing user and add it to the
 36+ // database if it is not there already
3537 $editor = User::newFromName( wfMsgForContent( 'newusermessage-editor' ) );
3638 if ( !$editor->isLoggedIn() ) {
3739 $editor->addToDatabase();
3840 }
3941
40 - $flags = EDIT_NEW;
41 - $templateTitleText = wfMsg( 'newusermessage-template' );
42 - $templateTitle = Title::newFromText( $templateTitleText );
43 - if ( !$templateTitle ) {
44 - wfDebug( __METHOD__. ": invalid title in newusermessage-template\n" );
45 - return true;
46 - }
47 - if ( $templateTitle->getNamespace() == NS_TEMPLATE ) {
48 - $templateTitleText = $templateTitle->getText();
49 - }
50 - if ($wgNewUserMinorEdit) $flags = $flags | EDIT_MINOR;
51 - if ($wgNewUserSuppressRC) $flags = $flags | EDIT_SUPPRESS_RC;
 42+ // Add (any) content to [[MediaWiki:Newusermessage-substitute]] to substitute the welcome template.
 43+ $substitute = wfMsgForContent( 'newusermessage-substitute' );
5244
53 - $dbw = wfGetDB( DB_MASTER );
54 - $dbw->begin();
55 - $good = true;
 45+ if ( $wgLqtTalkPages && LqtDispatch::isLqtPage( $talk ) ) { // Create a thread on the talk page if LiquidThreads is installed
 46+ // Get subject text
 47+ $threadSubject = self::getTextForPageInKey( 'newusermessage-template-subject' );
5648
57 - // Add (any) content to [[MediaWiki:Newusermessage-substitute]] to substitute the welcome template.
58 - $substitute = wfMsgForContent( 'newusermessage-substitute' );
59 - if ( $substitute ) {
60 - $text = "{{subst:{$templateTitleText}|$name|$realName}}";
61 - } else {
62 - $text = "{{{$templateTitleText}|$name|$realName}}";
63 - }
64 - $signatures = wfMsgForContent( 'newusermessage-signatures' );
65 - if ( !wfEmptyMsg( 'newusermessage-signatures', $signatures ) ) {
66 - $pattern = '/^\* ?(.*?)$/m';
67 - preg_match_all( $pattern, $signatures, $signatureList, PREG_SET_ORDER );
68 - if ( count( $signatureList ) > 0 ) {
69 - $rand = rand( 0, count( $signatureList ) - 1 );
70 - $signature = $signatureList[$rand][1];
71 - $text .= "\n-- {$signature} ~~~~~";
 49+ // Do not continue if there is no valid subject title
 50+ if ( !$threadSubject ) {
 51+ wfDebug( __METHOD__ . ": no text found for the subject\n" );
 52+ return true;
7253 }
73 - }
74 - try {
75 - $article->doEdit( $text, wfMsgForContent( 'newuseredit-summary' ), $flags, false, $editor );
76 - } catch ( DBQueryError $e ) {
77 - $good = false;
78 - }
7954
80 - if ( $good ) {
81 - // Set newtalk with the right user ID
82 - $user->setNewtalk( true );
83 - $dbw->commit();
84 - } else {
85 - // The article was concurrently created
86 - wfDebug( __METHOD__. ": the article has already been created despite !\$talk->exists()\n" );
87 - $dbw->rollback();
 55+ /** Create the final subject text.
 56+ * Always substituted and processed by parser to avoid awkward subjects
 57+ * Use real name if new user provided it
 58+ */
 59+ $parser = new Parser;
 60+ $parser->setOutputType( 'wiki' );
 61+ $parserOptions = new ParserOptions;
 62+
 63+ if ( $realName ) {
 64+ $threadSubject = $parser->preSaveTransform(
 65+ "{{subst:{$threadSubject}|$realName}}",
 66+ $talk /* as dummy */,
 67+ $editor, $parserOptions
 68+ );
 69+ } else {
 70+ $threadSubject = $parser->preSaveTransform(
 71+ "{{subst:{$threadSubject}|$name}}",
 72+ $talk /* as dummy */,
 73+ $editor,
 74+ $parserOptions
 75+ );
 76+ }
 77+
 78+ // Get the body text
 79+ $threadBody = self::getTextForPageInKey( 'newusermessage-template-body' );
 80+
 81+ // Do not continue if there is no body text
 82+ if ( !$threadBody ) {
 83+ wfDebug( __METHOD__ . ": no text found for the body\n" );
 84+ return true;
 85+ }
 86+
 87+ // Create the final body text after checking if the template is to be substituted.
 88+ if ( $substitute ) {
 89+ $threadBody = "{{subst:{$threadBody}|$name|$realName}}";
 90+ } else {
 91+ $threadBody = "{{{$threadBody}|$name|$realName}}";
 92+ }
 93+
 94+ $threadTitle = Threads::newThreadTitle( $threadSubject, $article );
 95+
 96+ if ( !$threadTitle ) {
 97+ wfDebug( __METHOD__ . ": invalid title $threadTitle\n" );
 98+ return true;
 99+ }
 100+
 101+ $threadArticle = new Article( $threadTitle );
 102+ self::writeWelcomeMessage( $user, $threadArticle, $threadBody, $editSummary, $editor );
 103+
 104+ // Need to edit as another user. Lqt does not provide an interface to alternative users,
 105+ // so replacing $wgUser here.
 106+ $parkedUser = $wgUser;
 107+ $wgUser = $editor;
 108+
 109+ LqtView::postEditUpdates(
 110+ 'new',
 111+ null, // $edit_applies_to
 112+ $threadArticle,
 113+ $article,
 114+ $threadSubject,
 115+ $editSummary,
 116+ null, // $thread,
 117+ $threadBody
 118+ );
 119+
 120+ // Set $wgUser back to the newly created user
 121+ $wgUser = $parkedUser;
 122+ } else { // Processing without LiquidThreads
 123+ $templateTitleText = wfMsg( 'newusermessage-template' );
 124+ $templateTitle = Title::newFromText( $templateTitleText );
 125+ if ( !$templateTitle ) {
 126+ wfDebug( __METHOD__ . ": invalid title in newusermessage-template\n" );
 127+ return true;
 128+ }
 129+
 130+ if ( $templateTitle->getNamespace() == NS_TEMPLATE ) {
 131+ $templateTitleText = $templateTitle->getText();
 132+ }
 133+
 134+ if ( $substitute ) {
 135+ $text = "{{subst:{$templateTitleText}|$name|$realName}}";
 136+ } else {
 137+ $text = "{{{$templateTitleText}|$name|$realName}}";
 138+ }
 139+
 140+ $signatures = wfMsgForContent( 'newusermessage-signatures' );
 141+
 142+ if ( !wfEmptyMsg( 'newusermessage-signatures', $signatures ) ) {
 143+ $pattern = '/^\* ?(.*?)$/m';
 144+ preg_match_all( $pattern, $signatures, $signatureList, PREG_SET_ORDER );
 145+ if ( count( $signatureList ) > 0 ) {
 146+ $rand = rand( 0, count( $signatureList ) - 1 );
 147+ $signature = $signatureList[$rand][1];
 148+ $text .= "\n-- {$signature} ~~~~~";
 149+ }
 150+ }
 151+
 152+ self::writeWelcomeMessage( $user, $article, $text, $editSummary, $editor );
88153 }
89154 }
90155 return true;
@@ -92,7 +157,7 @@
93158 static function createNewUserMessageAutoCreated( $user ) {
94159 global $wgNewUserMessageOnAutoCreate;
95160
96 - if( $wgNewUserMessageOnAutoCreate ) {
 161+ if ( $wgNewUserMessageOnAutoCreate ) {
97162 NewUserMessage::createNewUserMessage( $user );
98163 }
99164
@@ -104,4 +169,68 @@
105170 $names[] = 'msg:newusermessage-editor';
106171 return true;
107172 }
 173+
 174+ /**
 175+ * Create a page with text
 176+ * @param $user User object: user that was just created
 177+ * @param $article Article object: the article where $text is to be put
 178+ * @param $text String: text to put in $article
 179+ * @param $summary String: edit summary text
 180+ * @param $editor User object: user that will make the edit
 181+ */
 182+ private static function writeWelcomeMessage( $user, $article, $text, $summary, $editor ) {
 183+ global $wgNewUserMinorEdit, $wgNewUserSuppressRC;
 184+
 185+ wfLoadExtensionMessages( 'NewUserMessage' );
 186+
 187+ $flags = EDIT_NEW;
 188+ if ( $wgNewUserMinorEdit ) $flags = $flags | EDIT_MINOR;
 189+ if ( $wgNewUserSuppressRC ) $flags = $flags | EDIT_SUPPRESS_RC;
 190+
 191+ $dbw = wfGetDB( DB_MASTER );
 192+ $dbw->begin();
 193+ $good = true;
 194+
 195+ try {
 196+
 197+ $article->doEdit( $text, $summary, $flags, false, $editor );
 198+ } catch ( DBQueryError $e ) {
 199+ $good = false;
 200+ }
 201+
 202+ if ( $good ) {
 203+ // Set newtalk with the right user ID
 204+ $user->setNewtalk( true );
 205+ $dbw->commit();
 206+ } else {
 207+ // The article was concurrently created
 208+ wfDebug( __METHOD__ . ": the article has already been created despite !\$talk->exists()\n" );
 209+ $dbw->rollback();
 210+ }
 211+ }
 212+
 213+ /**
 214+ * Returns the text contents of a template page set in given key contents
 215+ * Returns empty string if no text could be retrieved.
 216+ * @param $key String: message key that should contain a template page name
 217+ */
 218+ private static function getTextForPageInKey( $key ) {
 219+ $templateTitleText = wfMsgForContent( $key );
 220+ $templateTitle = Title::newFromText( $templateTitleText );
 221+
 222+ // Do not continue if there is no valid subject title
 223+ if ( !$templateTitle ) {
 224+ wfDebug( __METHOD__ . ": invalid title in " . $key . "\n" );
 225+ return '';
 226+ }
 227+
 228+ // Get the subject text from the page
 229+ if ( $templateTitle->getNamespace() == NS_TEMPLATE ) {
 230+ return $templateTitle->getText();
 231+ } else {
 232+ // There is no subject text
 233+ wfDebug( __METHOD__ . ": " . $templateTitleText . " must be in NS_TEMPLATE\n" );
 234+ return '';
 235+ }
 236+ }
108237 }

Status & tagging log