r66441 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r66440‎ | r66441 | r66442 >
Date:21:07, 14 May 2010
Author:mah
Status:deferred
Tags:
Comment:
Make NewUserMessage & LiquidThreads use the new User->leaveUserMessage() method. Also, hookify NewUserMessage some more so most logic can be in NewUserMessage.
Modified paths:
  • /trunk/extensions/LiquidThreads/LiquidThreads.php (modified) (history)
  • /trunk/extensions/LiquidThreads/classes/Hooks.php (modified) (history)
  • /trunk/extensions/LiquidThreads/i18n/Lqt.i18n.php (modified) (history)
  • /trunk/extensions/NewUserMessage/NewUserMessage.class.php (modified) (history)

Diff [purge]

Index: trunk/extensions/NewUserMessage/NewUserMessage.class.php
@@ -50,9 +50,7 @@
5151 // Add (any) content to [[MediaWiki:Newusermessage-substitute]] to substitute the welcome template.
5252 $substitute = wfMsgForContent( 'newusermessage-substitute' );
5353
54 - if ( wfRunHooks( 'CreateNewUserMessage', array( $user, $editor, $editSummary, $substitute, $signature ) ) ) {
55 - self::setupAndLeaveMessage( $user, $editor, $editSummary, $substitute, $signature );
56 - }
 54+ self::setupAndLeaveMessage( $user, $editor, $editSummary, $substitute, $signature );
5755 }
5856 return true;
5957 }
@@ -67,35 +65,64 @@
6866 */
6967 static function setupAndLeaveMessage( $user, $editor, $editSummary, $substitute, $signature ) {
7068 $talk = $user->getTalkPage();
 69+ $article = new Article( $talk );
7170
72 - $templateTitleText = wfMsg( 'newusermessage-template' );
73 - $templateTitle = Title::newFromText( $templateTitleText );
74 - if ( !$templateTitle ) {
75 - wfDebug( __METHOD__ . ": invalid title in newusermessage-template\n" );
76 - return;
 71+ $subject = '';
 72+ if ( wfRunHooks( 'SetupNewUserMessageSubject', array( &$subject ) ) ) {
 73+ $subject = wfMsg( 'newusermessage-template-subject' );
7774 }
7875
79 - if ( $templateTitle->getNamespace() == NS_TEMPLATE ) {
80 - $templateTitleText = $templateTitle->getText();
 76+ $text = '';
 77+ if ( wfRunHooks( 'SetupNewUserMessageBody', array( &$text ) ) ) {
 78+ $text = wfMsg( 'newusermessage-template-body' );
 79+
 80+ $template = Title::newFromText( $text );
 81+ if ( !$template ) {
 82+ wfDebug( __METHOD__ . ": invalid title in newusermessage-template-body\n" );
 83+ return;
 84+ }
 85+
 86+ if ( $template->getNamespace() == NS_TEMPLATE ) {
 87+ $text = $template->getText();
 88+ }
8189 }
8290
 91+ if ( $substitute ) {
 92+ $subject = self::substString( $subject, $user, "preparse" );
 93+ $text = self::substString( $text, $user );
 94+ }
 95+
 96+ global $wgNewUserMinorEdit, $wgNewUserSuppressRC;
 97+
 98+ $flags = EDIT_NEW;
 99+ if ( $wgNewUserMinorEdit ) $flags = $flags | EDIT_MINOR;
 100+ if ( $wgNewUserSuppressRC ) $flags = $flags | EDIT_SUPPRESS_RC;
 101+
 102+ return $user->leaveUserMessage( $subject, $text, $signature, $editSummary, $editor, $flags );
 103+ }
 104+
 105+ static private function substString( $str, $user, $preparse = null ) {
83106 $realName = $user->getRealName();
84107 $name = $user->getName();
85 - $article = new Article( $talk );
86108
87 - if ( $substitute ) {
88 - $text = "{{subst:{$templateTitleText}|$name|$realName}}";
89 - } else {
90 - $text = "{{{$templateTitleText}|$name|$realName}}";
91 - }
 109+ $str = "{{subst:{{$str}}}|realName=$realName|name=$name}}";
92110
93 - if ( $signature ) {
94 - $text .= "\n-- {$signature} ~~~~~";
 111+ if ( $preparse ) {
 112+ /* Create the final subject text.
 113+ * Always substituted and processed by parser to avoid awkward subjects
 114+ */
 115+ $parser = new Parser;
 116+ $parser->setOutputType( 'wiki' );
 117+ $parserOptions = new ParserOptions;
 118+
 119+ $str = $parser->preSaveTransform($str, $talk /* as dummy */,
 120+ $editor, $parserOptions );
95121 }
96122
97 - self::writeWelcomeMessage( $user, $article, $text, $editSummary, $editor );
 123+ return $str;
98124 }
99125
 126+
100127 /**
101128 * Hook function to create a message on an auto-created user
102129 * @param $user User object of the user
@@ -120,67 +147,4 @@
121148 $names[] = 'msg:newusermessage-editor';
122149 return true;
123150 }
124 -
125 - /**
126 - * Create a page with text
127 - * @param $user User object: user that was just created
128 - * @param $article Article object: the article where $text is to be put
129 - * @param $text String: text to put in $article
130 - * @param $summary String: edit summary text
131 - * @param $editor User object: user that will make the edit
132 - */
133 - public static function writeWelcomeMessage( $user, $article, $text, $summary, $editor ) {
134 - global $wgNewUserMinorEdit, $wgNewUserSuppressRC;
135 -
136 - wfLoadExtensionMessages( 'NewUserMessage' );
137 -
138 - $flags = EDIT_NEW;
139 - if ( $wgNewUserMinorEdit ) $flags = $flags | EDIT_MINOR;
140 - if ( $wgNewUserSuppressRC ) $flags = $flags | EDIT_SUPPRESS_RC;
141 -
142 - $dbw = wfGetDB( DB_MASTER );
143 - $dbw->begin();
144 - $good = true;
145 -
146 - try {
147 - $article->doEdit( $text, $summary, $flags, false, $editor );
148 - } catch ( DBQueryError $e ) {
149 - $good = false;
150 - }
151 -
152 - if ( $good ) {
153 - // Set newtalk with the right user ID
154 - $user->setNewtalk( true );
155 - $dbw->commit();
156 - } else {
157 - // The article was concurrently created
158 - wfDebug( __METHOD__ . ": the article has already been created despite !\$talk->exists()\n" );
159 - $dbw->rollback();
160 - }
161 - }
162 -
163 - /**
164 - * Returns the text contents of a template page set in given key contents
165 - * Returns empty string if no text could be retrieved.
166 - * @param $key String: message key that should contain a template page name
167 - */
168 - public static function getTextForPageInKey( $key ) {
169 - $templateTitleText = wfMsgForContent( $key );
170 - $templateTitle = Title::newFromText( $templateTitleText );
171 -
172 - // Do not continue if there is no valid subject title
173 - if ( !$templateTitle ) {
174 - wfDebug( __METHOD__ . ": invalid title in " . $key . "\n" );
175 - return '';
176 - }
177 -
178 - // Get the subject text from the page
179 - if ( $templateTitle->getNamespace() == NS_TEMPLATE ) {
180 - return $templateTitle->getText();
181 - } else {
182 - // There is no subject text
183 - wfDebug( __METHOD__ . ": " . $templateTitleText . " must be in NS_TEMPLATE\n" );
184 - return '';
185 - }
186 - }
187151 }
Index: trunk/extensions/LiquidThreads/i18n/Lqt.i18n.php
@@ -314,6 +314,10 @@
315315
316316 // Reply subpage name
317317 'lqt-reply-subpage' => 'reply',
 318+
 319+ // New User Welcome Message
 320+ 'lqt-newusermessage-template-subject' => 'Template:Welcome-subject',
 321+ 'lqt-newusermessage-template-body' => 'Template:Welcome-body',
318322 );
319323
320324 /** Message documentation (Message documentation)
@@ -518,6 +522,8 @@
519523 'lqt-reply-subpage' => 'Part of the page title when a LiquidThread answer is given. Should probably be translated as a noun and not as a verb.
520524
521525 {{Identical|Reply}}',
 526+ 'lqt-newusermessage-template-subject' => 'Always substituted. Used if LiquidThreads is enabled in the User_talk namespace. Do not translate or duplicate this message to other languages',
 527+ 'lqt-newusermessage-template-body' => 'Used if LiquidThreads is enabled in the User_talk namespace. Do not translate or duplicate this message to other languages',
522528 );
523529
524530 /** Faeag Rotuma (Faeag Rotuma)
Index: trunk/extensions/LiquidThreads/LiquidThreads.php
@@ -98,8 +98,13 @@
9999 $wgHooks['TitleGetRestrictionTypes'][] = 'LqtHooks::getProtectionTypes';
100100
101101 // New User Messages
102 -$wgHooks['CreateNewUserMessage'][] = 'LqtHooks::createNewUserMessage';
 102+$wgHooks['SetupNewUserMessageSubject'][] = 'LqtHooks::setupNewUserMessageSubject';
 103+$wgHooks['SetupNewUserMessageSubject'][] = 'LqtHooks::setupNewUserMessageBody';
103104
 105+// User Message
 106+$wgHooks['SetupUserMessageArticle'][] = 'LqtHooks::setupUserMessageArticle';
 107+$wgHooks['AfterUserMessage'][] = 'LqtHooks::afterUserMessage';
 108+
104109 // Special pages
105110 $wgSpecialPages['MoveThread'] = 'SpecialMoveThread';
106111 $wgSpecialPages['NewMessages'] = 'SpecialNewMessages';
Index: trunk/extensions/LiquidThreads/classes/Hooks.php
@@ -483,98 +483,108 @@
484484 return true;
485485 }
486486
487 - static function createNewUserMessage( $user, $editor, $editSummary, $substitute, $signature ) {
488 - global $wgLqtTalkPages;
 487+ static function setupNewUserMessageSubject( &$subject ) {
 488+ wfLoadExtensionMessages( 'LiquidThreads' );
489489
490 - $talk = $user->getTalkPage(); // We already know it exists.
 490+ $subject = self::getTextForPageInKey( 'lqt-newusermessage-template-subject' );
 491+ // Do not continue if there is no valid subject title
 492+ if ( !$subject ) {
 493+ wfDebug( __METHOD__ . ": no text found for the subject\n" );
 494+ return true;
 495+ }
491496
492 - if ( $wgLqtTalkPages && LqtDispatch::isLqtPage( $talk ) ) {
493 - $article = new Article( $talk );
494 - $realName = $user->getRealName();
495 - $name = $user->getName();
 497+ return false;
 498+ }
496499
497 - // Get subject text
498 - $threadSubject = NewUserMessage::getTextForPageInKey( 'newusermessage-template-subject' );
 500+ static function setupNewUserMessageBody( &$text ) {
 501+ wfLoadExtensionMessages( 'LiquidThreads' );
499502
500 - // Do not continue if there is no valid subject title
501 - if ( !$threadSubject ) {
502 - wfDebug( __METHOD__ . ": no text found for the subject\n" );
503 - return true;
504 - }
 503+ // Get the body text
 504+ $text = self::getTextForPageInKey( 'lqt-newusermessage-template-body' );
505505
506 - /** Create the final subject text.
507 - * Always substituted and processed by parser to avoid awkward subjects
508 - * Use real name if new user provided it
509 - */
510 - $parser = new Parser;
511 - $parser->setOutputType( 'wiki' );
512 - $parserOptions = new ParserOptions;
 506+ // Do not continue if there is no body text
 507+ if ( !$text ) {
 508+ wfDebug( __METHOD__ . ": no text found for the body\n" );
 509+ return true;
 510+ }
513511
514 - if ( $realName ) {
515 - $threadSubject = $parser->preSaveTransform(
516 - "{{subst:{$threadSubject}|$realName}}",
517 - $talk /* as dummy */,
518 - $editor, $parserOptions
519 - );
520 - } else {
521 - $threadSubject = $parser->preSaveTransform(
522 - "{{subst:{$threadSubject}|$name}}",
523 - $talk /* as dummy */,
524 - $editor,
525 - $parserOptions
526 - );
527 - }
 512+ return false;
 513+ }
528514
529 - // Get the body text
530 - $threadBody = NewUserMessage::getTextForPageInKey( 'newusermessage-template-body' );
 515+ static function setupUserMessageArticle( &$article, $subject, $user, $editor ) {
 516+ global $wgLqtTalkPages;
531517
532 - // Do not continue if there is no body text
533 - if ( !$threadBody ) {
534 - wfDebug( __METHOD__ . ": no text found for the body\n" );
535 - return true;
536 - }
 518+ if ( $wgLqtTalkPages && LqtDispatch::isLqtPage( $article->getTitle() ) ) {
 519+ $threadTitle = Threads::newThreadTitle( $subject, $article );
537520
538 - // Create the final body text after checking if the template is to be substituted.
539 - if ( $substitute ) {
540 - $threadBody = "{{subst:{$threadBody}|$name|$realName}}";
541 - } else {
542 - $threadBody = "{{{$threadBody}|$name|$realName}}";
543 - }
544 -
545 - $threadTitle = Threads::newThreadTitle( $threadSubject, $article );
546 -
547521 if ( !$threadTitle ) {
548522 wfDebug( __METHOD__ . ": invalid title $threadTitle\n" );
549523 return true;
550524 }
551525
552 - $threadArticle = new Article( $threadTitle );
553 - NewUserMessage::writeWelcomeMessage( $user, $threadArticle, $threadBody, $editSummary, $editor );
 526+ $article = new Article( $threadTitle );
 527+ return false;
 528+ }
 529+ return true;
 530+ }
554531
 532+ static function afterUserMessage( $user, $article, $summary, $signature, $editor, $text ) {
 533+ global $wgLqtTalkPages;
 534+ $talk = $user->getTalkPage();
 535+
 536+ if ( $wgLqtTalkPages && LqtDispatch::isLqtPage( $talk ) ) {
555537 // Need to edit as another user. Lqt does not provide an interface to alternative users,
556538 // so replacing $wgUser here.
557539 global $wgUser;
558540 $parkedUser = $wgUser;
559541 $wgUser = $editor;
560542
 543+ $title = preg_replace( "{/[^/]+}", "", $article->getTitle()->getBaseText() );
 544+ $baseArticle = new Article( Title::newFromText( $title ) );
 545+ $threadTitle = preg_replace( "{.*/([^/]+)}", '$1', $article->getTitle()->getBaseText() );
 546+
561547 LqtView::newPostMetadataUpdates(
562548 array(
563 - 'talkpage' => $article,
564 - 'text' => $threadBody,
565 - 'summary' => $editSummary,
566 - 'root' => $threadArticle,
567 - 'subject' => $threadSubject,
 549+ 'talkpage' => $baseArticle,
 550+ 'text' => $text,
 551+ 'summary' => $summary,
 552+ 'root' => $article,
 553+ 'subject' => $threadTitle,
568554 'signature' => $signature,
569555 )
570556 );
571557
572558 // Set $wgUser back to the newly created user
573559 $wgUser = $parkedUser;
574 -
575 - // Stop processing after this hook.
576560 return false;
577561 }
578562 return true;
579563 }
580564
 565+ /**
 566+ * Returns the text contents of a template page set in given key contents
 567+ * Returns empty string if no text could be retrieved.
 568+ * @param $key String: message key that should contain a template page name
 569+ */
 570+ private static function getTextForPageInKey( $key ) {
 571+ wfLoadExtensionMessages( 'LiquidThreads' );
 572+
 573+ $templateTitleText = wfMsgForContent( $key );
 574+ $templateTitle = Title::newFromText( $templateTitleText );
 575+
 576+ // Do not continue if there is no valid subject title
 577+ if ( !$templateTitle ) {
 578+ wfDebug( __METHOD__ . ": invalid title in " . $key . "\n" );
 579+ return '';
 580+ }
 581+
 582+ // Get the subject text from the page
 583+ if ( $templateTitle->getNamespace() == NS_TEMPLATE ) {
 584+ return $templateTitle->getText();
 585+ } else {
 586+ // There is no subject text
 587+ wfDebug( __METHOD__ . ": " . $templateTitleText . " must be in NS_TEMPLATE\n" );
 588+ return '';
 589+ }
 590+ }
581591 }

Follow-up revisions

RevisionCommit summaryAuthorDate
r66447Update ignored messages per r66441siebrand21:45, 14 May 2010

Status & tagging log