Index: trunk/extensions/NewUserMessage/NewUserMessage.i18n.php |
— | — | @@ -9,20 +9,29 @@ |
10 | 10 | |
11 | 11 | /** English |
12 | 12 | * @author [http://www.organicdesign.co.nz/User:Nad Nad] |
| 13 | + * @author Siebrand |
13 | 14 | */ |
14 | 15 | $messages['en'] = array( |
15 | 16 | 'newusermessage-desc' => "Adds a message to newly created user's talk pages", |
16 | 17 | 'newuseredit-summary' => 'Adding [[{{int:newusermessage-template}}|welcome message]] to new user\'s talk page', |
17 | 18 | '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 |
18 | 21 | 'newusermessage-editor' => 'New user message', # The username used for the edit |
19 | 22 | 'newusermessage-substitute' => '', # do not translate or duplicate this message to other languages |
20 | 23 | ); |
21 | 24 | |
22 | 25 | /** Message documentation (Message documentation) |
23 | 26 | * @author Purodha |
| 27 | + * @author Siebrand |
24 | 28 | */ |
25 | 29 | $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.', |
27 | 36 | ); |
28 | 37 | |
29 | 38 | /** Afrikaans (Afrikaans) |
Index: trunk/extensions/NewUserMessage/NewUserMessage.php |
— | — | @@ -9,16 +9,16 @@ |
10 | 10 | * @copyright 2007-10-15 [http://www.organicdesign.co.nz/nad User:Nad] |
11 | 11 | */ |
12 | 12 | |
13 | | -if (!defined('MEDIAWIKI')) |
14 | | - die('Not an entry point.'); |
| 13 | +if ( !defined( 'MEDIAWIKI' ) ) |
| 14 | + die( 'Not an entry point.' ); |
15 | 15 | |
16 | | -define('NEWUSERMESSAGE_VERSION','2.2, 2009-06-01'); |
| 16 | +define( 'NEWUSERMESSAGE_VERSION', '3.0, 2009-11-28' ); |
17 | 17 | |
18 | 18 | $wgNewUserSuppressRC = false; // Specify whether or not the new user message creation should show up in recent changes |
19 | 19 | $wgNewUserMinorEdit = true; // Should the new user message creation be a minor edit? |
20 | 20 | $wgNewUserMessageOnAutoCreate = false; // Should auto creation (CentralAuth) trigger a new user message? |
21 | 21 | |
22 | | -$dir = dirname(__FILE__) . '/'; |
| 22 | +$dir = dirname( __FILE__ ) . '/'; |
23 | 23 | $wgExtensionMessagesFiles['NewUserMessage'] = $dir . 'NewUserMessage.i18n.php'; |
24 | 24 | $wgAutoloadClasses['NewUserMessage'] = $dir . 'NewUserMessage.class.php'; |
25 | 25 | |
Index: trunk/extensions/NewUserMessage/NewUserMessage.class.php |
— | — | @@ -7,83 +7,148 @@ |
8 | 8 | * @author [http://www.organicdesign.co.nz/nad User:Nad] |
9 | 9 | * @license GNU General Public Licence 2.0 or later |
10 | 10 | * @copyright 2007-10-15 [http://www.organicdesign.co.nz/nad User:Nad] |
| 11 | + * @copyright 2009 Siebrand Mazeland |
11 | 12 | */ |
12 | 13 | |
13 | | -if (!defined('MEDIAWIKI')) |
14 | | - die('Not an entry point.'); |
| 14 | +if ( !defined( 'MEDIAWIKI' ) ) |
| 15 | + die( 'Not an entry point.' ); |
15 | 16 | |
16 | 17 | class NewUserMessage { |
17 | 18 | /* |
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 |
19 | 20 | */ |
20 | 21 | static function createNewUserMessage( $user ) { |
21 | 22 | $talk = $user->getTalkPage(); |
22 | 23 | |
23 | 24 | if ( !$talk->exists() ) { |
24 | | - global $wgUser, $wgNewUserMinorEdit, $wgNewUserSuppressRC; |
| 25 | + global $wgUser, $wgLqtTalkPages; |
25 | 26 | |
26 | 27 | $name = $user->getName(); |
27 | 28 | $realName = $user->getRealName(); |
28 | 29 | |
29 | 30 | wfLoadExtensionMessages( 'NewUserMessage' ); |
30 | 31 | |
31 | | - $article = new Article($talk); |
| 32 | + $article = new Article( $talk ); |
| 33 | + $editSummary = wfMsgForContent( 'newuseredit-summary' ); |
32 | 34 | |
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 |
35 | 37 | $editor = User::newFromName( wfMsgForContent( 'newusermessage-editor' ) ); |
36 | 38 | if ( !$editor->isLoggedIn() ) { |
37 | 39 | $editor->addToDatabase(); |
38 | 40 | } |
39 | 41 | |
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' ); |
52 | 44 | |
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' ); |
56 | 48 | |
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; |
72 | 53 | } |
73 | | - } |
74 | | - try { |
75 | | - $article->doEdit( $text, wfMsgForContent( 'newuseredit-summary' ), $flags, false, $editor ); |
76 | | - } catch ( DBQueryError $e ) { |
77 | | - $good = false; |
78 | | - } |
79 | 54 | |
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 ); |
88 | 153 | } |
89 | 154 | } |
90 | 155 | return true; |
— | — | @@ -92,7 +157,7 @@ |
93 | 158 | static function createNewUserMessageAutoCreated( $user ) { |
94 | 159 | global $wgNewUserMessageOnAutoCreate; |
95 | 160 | |
96 | | - if( $wgNewUserMessageOnAutoCreate ) { |
| 161 | + if ( $wgNewUserMessageOnAutoCreate ) { |
97 | 162 | NewUserMessage::createNewUserMessage( $user ); |
98 | 163 | } |
99 | 164 | |
— | — | @@ -104,4 +169,68 @@ |
105 | 170 | $names[] = 'msg:newusermessage-editor'; |
106 | 171 | return true; |
107 | 172 | } |
| 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 | + } |
108 | 237 | } |