X-Git-Url: http://git.heureux-cyclage.org/?a=blobdiff_plain;f=includes%2FCommentStoreComment.php;h=9f1681dff08a9c15d118116f928c3ddbff733798;hb=e900893531e76fb8f80c1c9b5be459fd02862c3c;hp=afc13742239f197206dc3b47a537145a3d7354d3;hpb=b6b8526a04c072ed8a4b283f2c4ece8f4edda861;p=lhc%2Fweb%2Fwiklou.git diff --git a/includes/CommentStoreComment.php b/includes/CommentStoreComment.php index afc1374223..9f1681dff0 100644 --- a/includes/CommentStoreComment.php +++ b/includes/CommentStoreComment.php @@ -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 ); + } + } }