Deprecate Parser implementation methods (will be private in next release)
[lhc/web/wiklou.git] / includes / CommentStoreComment.php
index afc1374..9f1681d 100644 (file)
@@ -19,8 +19,7 @@
  *
  * @file
  */
-
-use Wikimedia\Rdbms\IDatabase;
+use MediaWiki\MediaWikiServices;
 
 /**
  * CommentStoreComment represents a comment stored by CommentStore. The fields
@@ -42,7 +41,7 @@ class CommentStoreComment {
        public $data;
 
        /**
-        * @private For use by CommentStore only
+        * @private For use by CommentStore only. Use self::newUnsavedComment() instead.
         * @param int|null $id
         * @param string $text
         * @param Message|null $message
@@ -51,7 +50,41 @@ class CommentStoreComment {
        public function __construct( $id, $text, Message $message = null, array $data = null ) {
                $this->id = $id;
                $this->text = $text;
-               $this->message = $message ?: new RawMessage( '$1', [ $text ] );
+               $this->message = $message ?: new RawMessage( '$1', [ Message::plaintextParam( $text ) ] );
                $this->data = $data;
        }
+
+       /**
+        * Create a new, unsaved CommentStoreComment
+        *
+        * @param string|Message|CommentStoreComment $comment Comment text or Message object.
+        *  A CommentStoreComment is also accepted here, in which case it is returned unchanged.
+        * @param array|null $data Structured data to store. Keys beginning with '_' are reserved.
+        *  Ignored if $comment is a CommentStoreComment.
+        * @return CommentStoreComment
+        */
+       public static function newUnsavedComment( $comment, array $data = null ) {
+               if ( $comment instanceof CommentStoreComment ) {
+                       return $comment;
+               }
+
+               if ( $data !== null ) {
+                       foreach ( $data as $k => $v ) {
+                               if ( substr( $k, 0, 1 ) === '_' ) {
+                                       throw new InvalidArgumentException( 'Keys in $data beginning with "_" are reserved' );
+                               }
+                       }
+               }
+
+               if ( $comment instanceof Message ) {
+                       $message = clone $comment;
+                       // Avoid $wgForceUIMsgAsContentMsg
+                       $text = $message->inLanguage( MediaWikiServices::getInstance()->getContentLanguage() )
+                               ->setInterfaceMessageFlag( true )
+                               ->text();
+                       return new CommentStoreComment( null, $text, $message, $data );
+               } else {
+                       return new CommentStoreComment( null, $comment, null, $data );
+               }
+       }
 }