r66696 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r66695‎ | r66696 | r66697 >
Date:18:30, 20 May 2010
Author:mah
Status:deferred (Comments)
Tags:
Comment:
Make template fetching and substitution work. Simplify the code more.
Modified paths:
  • /trunk/extensions/NewUserMessage/NewUserMessage.class.php (modified) (history)

Diff [purge]

Index: trunk/extensions/NewUserMessage/NewUserMessage.class.php
@@ -51,16 +51,35 @@
5252 }
5353
5454 /**
 55+ * Return the template name if it exists, or '' otherwise.
 56+ * @returns string
 57+ */
 58+ static function fetchTemplateIfExists( $template ) {
 59+ $text = Title::newFromText( $template );
 60+
 61+ if ( !$text ) {
 62+ wfDebug( __METHOD__ . ": '$template' is not a valid title.\n" );
 63+ return '';
 64+ } elseif ( $text->getNamespace() !== NS_TEMPLATE ) {
 65+ wfDebug( __METHOD__ . ": '$template' is not a valid Template.\n" );
 66+ return '';
 67+ } elseif ( !$text->exists() ) {
 68+ return '';
 69+
 70+ if ( $template->getNamespace() == NS_TEMPLATE ) {
 71+ $text = $template->getText();
 72+ }
 73+ }
 74+
 75+ return $text->getText();
 76+ }
 77+
 78+ /**
5579 * Produce a subject for the message.
5680 * @returns String
5781 */
5882 static function fetchSubject() {
59 - $subject = '';
60 - if ( wfRunHooks( 'SetupNewUserMessageSubject', array( &$subject ) ) ) {
61 - $subject = wfMsg( 'newusermessage-template-subject' );
62 - }
63 -
64 - return $subject;
 83+ return self::fetchTemplateIfExists( wfMsg( 'newusermessage-template-subject' ) );
6584 }
6685
6786 /**
@@ -68,21 +87,7 @@
6988 * @returns String
7089 */
7190 static function fetchText() {
72 - $text = '';
73 - if ( wfRunHooks( 'SetupNewUserMessageBody', array( &$text ) ) ) {
74 - $text = wfMsg( 'newusermessage-template-body' );
75 -
76 - $template = Title::newFromText( $text );
77 - if ( !$template ) {
78 - wfDebug( __METHOD__ . ": invalid title in newusermessage-template-body\n" );
79 - return;
80 - }
81 -
82 - if ( $template->getNamespace() == NS_TEMPLATE ) {
83 - $text = $template->getText();
84 - }
85 - }
86 - return $text;
 91+ return self::fetchTemplateIfExists( wfMsg( 'newusermessage-template-body' ) );
8792 }
8893
8994 /**
@@ -103,25 +108,29 @@
104109 * Take care of substition on the string in a uniform manner
105110 * @param $str String
106111 * @param $user User
 112+ * @param $editor User
 113+ * @param $talk Article
107114 * @param $preparse if provided, then preparse the string using a Parser
108115 * @returns String
109116 */
110 - static private function substString( $str, $user, $preparse = null ) {
 117+ static private function substString( $str, $user, $editor, $talk, $preparse = null ) {
111118 $realName = $user->getRealName();
112119 $name = $user->getName();
113120
114 - $str = "{{subst:{{$str}}}|realName=$realName|name=$name}}";
 121+ // Add (any) content to [[MediaWiki:Newusermessage-substitute]] to substitute the
 122+ // welcome template.
 123+ $substitute = wfMsgForContent( 'newusermessage-substitute' );
115124
 125+ if ( $substitute ) {
 126+ $str = "{{subst:{{$str}}}|realName=$realName|name=$name}}";
 127+ } else {
 128+ $str = "{{{$str}|realName=$realName|name=$name}}";
 129+ }
 130+
116131 if ( $preparse ) {
117 - /* Create the final subject text.
118 - * Always substituted and processed by parser to avoid awkward subjects
119 - */
120 - $parser = new Parser;
121 - $parser->setOutputType( 'wiki' );
122 - $parserOptions = new ParserOptions;
 132+ global $wgParser;
123133
124 - $str = $parser->preSaveTransform($str, $talk /* as dummy */,
125 - $editor, $parserOptions );
 134+ $str = $wgParser->preSaveTransform($str, $talk, $editor, new ParserOptions );
126135 }
127136
128137 return $str;
@@ -144,15 +153,11 @@
145154 $editor = self::fetchEditor();
146155 $flags = self::fetchFlags();
147156
148 - // Add (any) content to [[MediaWiki:Newusermessage-substitute]] to substitute the welcome template.
149 - $substitute = wfMsgForContent( 'newusermessage-substitute' );
 157+ $subject = self::substString( $subject, $user, $editor, $talk, "preparse" );
 158+ $text = self::substString( $text, $user, $editor, $talk );
150159
151 - if ( $substitute ) {
152 - $subject = self::substString( $subject, $user, "preparse" );
153 - $text = self::substString( $text, $user );
154 - }
155 -
156 - return $user->leaveUserMessage( $subject, $text, $signature, $editSummary, $editor, $flags );
 160+ return $user->leaveUserMessage( $subject, $text, $signature, $editSummary,
 161+ $editor, $flags );
157162 }
158163 }
159164

Follow-up revisions

RevisionCommit summaryAuthorDate
r67773follow-up r66696 — thought I had committed this fixmah04:34, 10 June 2010

Comments

#Comment by Reedy (talk | contribs)   18:33, 20 May 2010

+ } elseif ( !$text->exists() ) { + return ; + + if ( $template->getNamespace() == NS_TEMPLATE ) { + $text = $template->getText(); + } + }


Surely you're always returning ? :/

#Comment by Catrope (talk | contribs)   18:36, 20 May 2010

Reformatted version of comment above:

+		} elseif ( !$text->exists() ) {
+			return '';
+
+			if ( $template->getNamespace() == NS_TEMPLATE ) {
+				$text = $template->getText();
+			}
+		}

Surely you're always returning ''? :|

#Comment by MarkAHershberger (talk | contribs)   00:44, 21 May 2010

Thanks for point out my cut-n-paste error :)

#Comment by MarkAHershberger (talk | contribs)   05:03, 10 June 2010

See r67773

Status & tagging log