Index: branches/wmf/1.17wmf1/extensions/NewUserMessage/NewUserMessage.class.php |
— | — | @@ -140,6 +140,7 @@ |
141 | 141 | $talk = $user->getTalkPage(); |
142 | 142 | |
143 | 143 | if ( !$talk->exists() ) { |
| 144 | + $article = new Article( $talk ); |
144 | 145 | $subject = self::fetchSubject(); |
145 | 146 | $text = self::fetchText(); |
146 | 147 | $signature = self::fetchSignature(); |
— | — | @@ -150,8 +151,8 @@ |
151 | 152 | $subject = self::substString( $subject, $user, $editor, $talk, "preparse" ); |
152 | 153 | $text = self::substString( $text, $user, $editor, $talk ); |
153 | 154 | |
154 | | - return $user->leaveUserMessage( $subject, $text, $signature, $editSummary, |
155 | | - $editor, $flags ); |
| 155 | + return self::leaveUserMessage( $user, $article, $subject, $text, |
| 156 | + $signature, $editSummary, $editor, $flags ); |
156 | 157 | } |
157 | 158 | } |
158 | 159 | |
— | — | @@ -178,4 +179,62 @@ |
179 | 180 | $names[] = 'msg:newusermessage-editor'; |
180 | 181 | return true; |
181 | 182 | } |
| 183 | + |
| 184 | + /** |
| 185 | + * Leave a user a message |
| 186 | + * @param $subject String the subject of the message |
| 187 | + * @param $text String the message to leave |
| 188 | + * @param $signature String Text to leave in the signature |
| 189 | + * @param $summary String the summary for this change, defaults to |
| 190 | + * "Leave system message." |
| 191 | + * @param $editor User The user leaving the message, defaults to |
| 192 | + * "{{MediaWiki:usermessage-editor}}" |
| 193 | + * @param $flags Int default edit flags |
| 194 | + * |
| 195 | + * @return boolean true if it was successful |
| 196 | + */ |
| 197 | + public static function leaveUserMessage( $user, $article, $subject, $text, $signature, |
| 198 | + $summary, $editor, $flags ) { |
| 199 | + |
| 200 | + $text = self::formatUserMessage( $subject, $text, $signature ); |
| 201 | + $flags = $article->checkFlags( $flags ); |
| 202 | + |
| 203 | + if ( $flags & EDIT_UPDATE ) { |
| 204 | + $text = $article->getContent() . $text; |
| 205 | + } |
| 206 | + |
| 207 | + $dbw = wfGetDB( DB_MASTER ); |
| 208 | + $dbw->begin(); |
| 209 | + |
| 210 | + try { |
| 211 | + $status = $article->doEdit( $text, $summary, $flags, false, $editor ); |
| 212 | + } catch ( DBQueryError $e ) { |
| 213 | + $status = Status::newFatal( 'DB Error' ); |
| 214 | + } |
| 215 | + |
| 216 | + if ( $status->isGood() ) { |
| 217 | + // Set newtalk with the right user ID |
| 218 | + $user->setNewtalk( true ); |
| 219 | + $dbw->commit(); |
| 220 | + } else { |
| 221 | + // The article was concurrently created |
| 222 | + wfDebug( __METHOD__ . ": Error ".$status->getWikiText() ); |
| 223 | + $dbw->rollback(); |
| 224 | + } |
| 225 | + |
| 226 | + return $status->isGood(); |
| 227 | + } |
| 228 | + |
| 229 | + /** |
| 230 | + * Format the user message using a hook, a template, or, failing these, a static format. |
| 231 | + * @param $subject String the subject of the message |
| 232 | + * @param $text String the content of the message |
| 233 | + * @param $signature String the signature, if provided. |
| 234 | + */ |
| 235 | + static protected function formatUserMessage( $subject, $text, $signature ) { |
| 236 | + $signature = empty($signature) ? "~~~~~" : "{$signature} ~~~~~"; |
| 237 | + $text = "\n== $subject ==\n\n$text\n\n-- $signature"; |
| 238 | + |
| 239 | + return $text; |
| 240 | + } |
182 | 241 | } |