X-Git-Url: https://git.heureux-cyclage.org/?a=blobdiff_plain;f=includes%2FCommentStoreComment.php;h=7ed86d66c7691c3e4407bdbe886b159c13afae7a;hb=3f484f6241a104338f1f7408b859374505cb0aa8;hp=afc13742239f197206dc3b47a537145a3d7354d3;hpb=237d3271fd313ebe09858a5c442a91216a7b61cf;p=lhc%2Fweb%2Fwiklou.git diff --git a/includes/CommentStoreComment.php b/includes/CommentStoreComment.php index afc1374223..7ed86d66c7 100644 --- a/includes/CommentStoreComment.php +++ b/includes/CommentStoreComment.php @@ -20,8 +20,6 @@ * @file */ -use Wikimedia\Rdbms\IDatabase; - /** * CommentStoreComment represents a comment stored by CommentStore. The fields * should be considered read-only. @@ -42,7 +40,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 @@ -54,4 +52,39 @@ class CommentStoreComment { $this->message = $message ?: new RawMessage( '$1', [ $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 ) { + global $wgContLang; + + 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; + $text = $message->inLanguage( $wgContLang ) // Avoid $wgForceUIMsgAsContentMsg + ->setInterfaceMessageFlag( true ) + ->text(); + return new CommentStoreComment( null, $text, $message, $data ); + } else { + return new CommentStoreComment( null, $comment, null, $data ); + } + } }