http://www.mediawiki.org/wiki/Special:Code/MediaWiki/59519

Revision: 59519
Author:   siebrand
Date:     2009-11-28 16:32:39 +0000 (Sat, 28 Nov 2009)

Log Message:
-----------
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
    trunk/extensions/NewUserMessage/NewUserMessage.i18n.php
    trunk/extensions/NewUserMessage/NewUserMessage.php

Modified: trunk/extensions/NewUserMessage/NewUserMessage.class.php
===================================================================
--- trunk/extensions/NewUserMessage/NewUserMessage.class.php    2009-11-28 
15:02:33 UTC (rev 59518)
+++ trunk/extensions/NewUserMessage/NewUserMessage.class.php    2009-11-28 
16:32:39 UTC (rev 59519)
@@ -7,83 +7,148 @@
  * @author [http://www.organicdesign.co.nz/nad User:Nad]
  * @license GNU General Public Licence 2.0 or later
  * @copyright 2007-10-15 [http://www.organicdesign.co.nz/nad User:Nad]
+ * @copyright 2009 Siebrand Mazeland
  */
 
-if (!defined('MEDIAWIKI'))
-       die('Not an entry point.');
+if ( !defined( 'MEDIAWIKI' ) )
+       die( 'Not an entry point.' );
 
 class NewUserMessage {
        /*
-        * Add the template message if the users talk page doesn't already exist
+        * Add the template message if the users talk page does not already 
exist
         */
        static function createNewUserMessage( $user ) {
                $talk = $user->getTalkPage();
 
                if ( !$talk->exists() ) {
-                       global $wgUser, $wgNewUserMinorEdit, 
$wgNewUserSuppressRC;
+                       global $wgUser, $wgLqtTalkPages;
 
                        $name = $user->getName();
                        $realName = $user->getRealName();
 
                        wfLoadExtensionMessages( 'NewUserMessage' );
 
-                       $article = new Article($talk);
+                       $article = new Article( $talk );
+                       $editSummary = wfMsgForContent( 'newuseredit-summary' );
 
-                       // Create a user object for the editing user and add it 
to the database
-                       // if it's not there already
+                       // Create a user object for the editing user and add it 
to the
+                       // database if it is not there already
                        $editor = User::newFromName( wfMsgForContent( 
'newusermessage-editor' ) );
                        if ( !$editor->isLoggedIn() ) {
                                $editor->addToDatabase();
                        }
 
-                       $flags = EDIT_NEW;
-                       $templateTitleText = wfMsg( 'newusermessage-template' );
-                       $templateTitle = Title::newFromText( $templateTitleText 
);
-                       if ( !$templateTitle ) {
-                               wfDebug( __METHOD__. ": invalid title in 
newusermessage-template\n" );
-                               return true;
-                       }
-                       if ( $templateTitle->getNamespace() == NS_TEMPLATE ) {
-                               $templateTitleText = $templateTitle->getText();
-                       }
-                       if ($wgNewUserMinorEdit) $flags = $flags | EDIT_MINOR;
-                       if ($wgNewUserSuppressRC) $flags = $flags | 
EDIT_SUPPRESS_RC;
+                       // Add (any) content to 
[[MediaWiki:Newusermessage-substitute]] to substitute the welcome template.
+                       $substitute = wfMsgForContent( 
'newusermessage-substitute' );
 
-                       $dbw = wfGetDB( DB_MASTER );
-                       $dbw->begin();
-                       $good = true;
+                       if ( $wgLqtTalkPages && LqtDispatch::isLqtPage( $talk ) 
) { // Create a thread on the talk page if LiquidThreads is installed
+                               // Get subject text
+                               $threadSubject = self::getTextForPageInKey( 
'newusermessage-template-subject' );
 
-                       // Add (any) content to 
[[MediaWiki:Newusermessage-substitute]] to substitute the welcome template.
-                       $substitute = wfMsgForContent( 
'newusermessage-substitute' );
-                       if ( $substitute ) {
-                               $text = 
"{{subst:{$templateTitleText}|$name|$realName}}";
-                       } else {
-                               $text = 
"{{{$templateTitleText}|$name|$realName}}";
-                       }
-                       $signatures = wfMsgForContent( 
'newusermessage-signatures' );
-                       if ( !wfEmptyMsg( 'newusermessage-signatures', 
$signatures ) ) {
-                               $pattern = '/^\* ?(.*?)$/m';
-                               preg_match_all( $pattern, $signatures, 
$signatureList, PREG_SET_ORDER );
-                               if ( count( $signatureList ) > 0 ) {
-                                       $rand = rand( 0, count( $signatureList 
) - 1 );
-                                       $signature = $signatureList[$rand][1];
-                                       $text .= "\n-- {$signature} ~~~~~";
+                               // Do not continue if there is no valid subject 
title
+                               if ( !$threadSubject ) {
+                                       wfDebug( __METHOD__ . ": no text found 
for the subject\n" );
+                                       return true;
                                }
-                       }
-                       try {
-                               $article->doEdit( $text, wfMsgForContent( 
'newuseredit-summary' ), $flags, false, $editor );
-                       } catch ( DBQueryError $e ) {
-                               $good = false;
-                       }
 
-                       if ( $good ) {
-                               // Set newtalk with the right user ID
-                               $user->setNewtalk( true );
-                               $dbw->commit();
-                       } else {
-                               // The article was concurrently created
-                               wfDebug( __METHOD__. ": the article has already 
been created despite !\$talk->exists()\n" );
-                               $dbw->rollback();
+                               /** Create the final subject text.
+                                * Always substituted and processed by parser 
to avoid awkward subjects
+                                * Use real name if new user provided it
+                                */
+                               $parser = new Parser;
+                               $parser->setOutputType( 'wiki' );
+                               $parserOptions = new ParserOptions;
+
+                               if ( $realName ) {
+                                       $threadSubject = 
$parser->preSaveTransform(
+                                               
"{{subst:{$threadSubject}|$realName}}",
+                                               $talk /* as dummy */,
+                                               $editor, $parserOptions
+                                       );
+                               } else {
+                                       $threadSubject = 
$parser->preSaveTransform(
+                                               
"{{subst:{$threadSubject}|$name}}",
+                                               $talk /* as dummy */,
+                                               $editor,
+                                               $parserOptions
+                                       );
+                               }
+
+                               // Get the body text
+                               $threadBody = self::getTextForPageInKey( 
'newusermessage-template-body' );
+
+                               // Do not continue if there is no body text
+                               if ( !$threadBody ) {
+                                       wfDebug( __METHOD__ . ": no text found 
for the body\n" );
+                                       return true;
+                               }
+
+                               // Create the final body text after checking if 
the template is to be substituted.
+                               if ( $substitute ) {
+                                       $threadBody = 
"{{subst:{$threadBody}|$name|$realName}}";
+                               } else {
+                                       $threadBody = 
"{{{$threadBody}|$name|$realName}}";
+                               }
+
+                               $threadTitle = Threads::newThreadTitle( 
$threadSubject, $article );
+
+                               if ( !$threadTitle ) {
+                                       wfDebug( __METHOD__ . ": invalid title 
$threadTitle\n" );
+                                       return true;
+                               }
+
+                               $threadArticle = new Article( $threadTitle );
+                               self::writeWelcomeMessage( $user, 
$threadArticle,  $threadBody, $editSummary, $editor );
+
+                               // Need to edit as another user. Lqt does not 
provide an interface to alternative users,
+                               // so replacing $wgUser here.
+                               $parkedUser = $wgUser;
+                               $wgUser = $editor;
+
+                               LqtView::postEditUpdates(
+                                       'new',
+                                       null, // $edit_applies_to
+                                       $threadArticle,
+                                       $article,
+                                       $threadSubject,
+                                       $editSummary,
+                                       null, // $thread,
+                                       $threadBody
+                               );
+
+                               // Set $wgUser back to the newly created user
+                               $wgUser = $parkedUser;
+                       } else { // Processing without LiquidThreads
+                               $templateTitleText = wfMsg( 
'newusermessage-template' );
+                               $templateTitle = Title::newFromText( 
$templateTitleText );
+                               if ( !$templateTitle ) {
+                                       wfDebug( __METHOD__ . ": invalid title 
in newusermessage-template\n" );
+                                       return true;
+                               }
+
+                               if ( $templateTitle->getNamespace() == 
NS_TEMPLATE ) {
+                                       $templateTitleText = 
$templateTitle->getText();
+                               }
+
+                               if ( $substitute ) {
+                                       $text = 
"{{subst:{$templateTitleText}|$name|$realName}}";
+                               } else {
+                                       $text = 
"{{{$templateTitleText}|$name|$realName}}";
+                               }
+
+                               $signatures = wfMsgForContent( 
'newusermessage-signatures' );
+
+                               if ( !wfEmptyMsg( 'newusermessage-signatures', 
$signatures ) ) {
+                                       $pattern = '/^\* ?(.*?)$/m';
+                                       preg_match_all( $pattern, $signatures, 
$signatureList, PREG_SET_ORDER );
+                                       if ( count( $signatureList ) > 0 ) {
+                                               $rand = rand( 0, count( 
$signatureList ) - 1 );
+                                               $signature = 
$signatureList[$rand][1];
+                                               $text .= "\n-- {$signature} 
~~~~~";
+                                       }
+                               }
+
+                               self::writeWelcomeMessage( $user, $article,  
$text, $editSummary, $editor );
                        }
                }
                return true;
@@ -92,7 +157,7 @@
        static function createNewUserMessageAutoCreated( $user ) {
                global $wgNewUserMessageOnAutoCreate;
 
-               if( $wgNewUserMessageOnAutoCreate ) {
+               if ( $wgNewUserMessageOnAutoCreate ) {
                        NewUserMessage::createNewUserMessage( $user );
                }
 
@@ -104,4 +169,68 @@
                $names[] = 'msg:newusermessage-editor';
                return true;
        }
+
+       /**
+        * Create a page with text
+        * @param $user User object: user that was just created
+        * @param $article Article object: the article where $text is to be put
+        * @param $text String: text to put in $article
+        * @param $summary String: edit summary text
+        * @param $editor User object: user that will make the edit
+        */
+       private static function writeWelcomeMessage( $user, $article, $text, 
$summary, $editor ) {
+               global $wgNewUserMinorEdit, $wgNewUserSuppressRC;
+
+               wfLoadExtensionMessages( 'NewUserMessage' );
+
+               $flags = EDIT_NEW;
+               if ( $wgNewUserMinorEdit ) $flags = $flags | EDIT_MINOR;
+               if ( $wgNewUserSuppressRC ) $flags = $flags | EDIT_SUPPRESS_RC;
+
+               $dbw = wfGetDB( DB_MASTER );
+               $dbw->begin();
+               $good = true;
+
+               try {
+
+                       $article->doEdit( $text, $summary, $flags, false, 
$editor );
+               } catch ( DBQueryError $e ) {
+                       $good = false;
+                       }
+
+               if ( $good ) {
+                       // Set newtalk with the right user ID
+                       $user->setNewtalk( true );
+                       $dbw->commit();
+               } else {
+                       // The article was concurrently created
+                       wfDebug( __METHOD__ . ": the article has already been 
created despite !\$talk->exists()\n" );
+                       $dbw->rollback();
+               }
+       }
+
+       /**
+        * Returns the text contents of a template page set in given key 
contents
+        * Returns empty string if no text could be retrieved.
+        * @param $key String: message key that should contain a template page 
name
+        */
+       private static function getTextForPageInKey( $key ) {
+               $templateTitleText = wfMsgForContent( $key );
+               $templateTitle = Title::newFromText( $templateTitleText );
+
+               // Do not continue if there is no valid subject title
+               if ( !$templateTitle ) {
+                       wfDebug( __METHOD__ . ": invalid title in " . $key . 
"\n" );
+                       return '';
+               }
+
+               // Get the subject text from the page
+               if ( $templateTitle->getNamespace() == NS_TEMPLATE ) {
+                       return $templateTitle->getText();
+               } else {
+                       // There is no subject text
+                       wfDebug( __METHOD__ . ": " . $templateTitleText . " 
must be in NS_TEMPLATE\n" );
+                       return '';
+               }
+       }
 }

Modified: trunk/extensions/NewUserMessage/NewUserMessage.i18n.php
===================================================================
--- trunk/extensions/NewUserMessage/NewUserMessage.i18n.php     2009-11-28 
15:02:33 UTC (rev 59518)
+++ trunk/extensions/NewUserMessage/NewUserMessage.i18n.php     2009-11-28 
16:32:39 UTC (rev 59519)
@@ -9,20 +9,29 @@
 
 /** English
  * @author [http://www.organicdesign.co.nz/User:Nad Nad]
+ * @author Siebrand
  */
 $messages['en'] = array(
        'newusermessage-desc' => "Adds a message to newly created user's talk 
pages",
        'newuseredit-summary' => 'Adding 
[[{{int:newusermessage-template}}|welcome message]] to new user\'s talk page',
        'newusermessage-template' => 'Template:Welcome', # The title of the 
message template
+       '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
+       '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
        'newusermessage-editor' => 'New user message', # The username used for 
the edit
        'newusermessage-substitute' => '', # do not translate or duplicate this 
message to other languages
 );
 
 /** Message documentation (Message documentation)
  * @author Purodha
+ * @author Siebrand
  */
 $messages['qqq'] = array(
-       'newusermessage-desc' => 'Short description of this extension, shown on 
[[Special:Version]]. Do not translate or change links.',
+       'newusermessage-desc' => '{{desc}}',
+       'newuseredit-summary' => 'The edit summary used when placing a new user 
message.',
+       'newusermessage-subject' => 'Used if LiquidThreads is enabled in the 
User_talk namespace. Do not translate or duplicate this message to other 
languages.',
+       'newusermessage-body' => 'Used if LiquidThreads is enabled in the 
User_talk namespace. Do not translate or duplicate this message to other 
languages.',
+       'newusermessage-editor' => 'The username used for the edit',
+       '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.',
 );
 
 /** Afrikaans (Afrikaans)

Modified: trunk/extensions/NewUserMessage/NewUserMessage.php
===================================================================
--- trunk/extensions/NewUserMessage/NewUserMessage.php  2009-11-28 15:02:33 UTC 
(rev 59518)
+++ trunk/extensions/NewUserMessage/NewUserMessage.php  2009-11-28 16:32:39 UTC 
(rev 59519)
@@ -9,16 +9,16 @@
  * @copyright 2007-10-15 [http://www.organicdesign.co.nz/nad User:Nad]
  */
 
-if (!defined('MEDIAWIKI'))
-       die('Not an entry point.');
+if ( !defined( 'MEDIAWIKI' ) )
+       die( 'Not an entry point.' );
 
-define('NEWUSERMESSAGE_VERSION','2.2, 2009-06-01');
+define( 'NEWUSERMESSAGE_VERSION', '3.0, 2009-11-28' );
 
 $wgNewUserSuppressRC = false;           // Specify whether or not the new user 
message creation should show up in recent changes
 $wgNewUserMinorEdit = true;             // Should the new user message 
creation be a minor edit?
 $wgNewUserMessageOnAutoCreate = false;  // Should auto creation (CentralAuth) 
trigger a new user message?
 
-$dir = dirname(__FILE__) . '/';
+$dir = dirname( __FILE__ ) . '/';
 $wgExtensionMessagesFiles['NewUserMessage'] = $dir . 'NewUserMessage.i18n.php';
 $wgAutoloadClasses['NewUserMessage'] = $dir . 'NewUserMessage.class.php';
 



_______________________________________________
MediaWiki-CVS mailing list
MediaWiki-CVS@lists.wikimedia.org
https://lists.wikimedia.org/mailman/listinfo/mediawiki-cvs

Reply via email to