Anomie has uploaded a new change for review. ( 
https://gerrit.wikimedia.org/r/377319 )

Change subject: Add CommentStoreComment::newUnsavedComment()
......................................................................

Add CommentStoreComment::newUnsavedComment()

Revision (or more accurately, its upcoming MCR replacement) should allow
for the full flexibility of CommentStore in its interface, and the most
straightforward way to do that is to use CommentStoreComment. But for
that to work sanely, we need a non-@private way to create a new
CommentStoreComment that hasn't been saved to the database yet.

Change-Id: I76028e8ef3da4568a16e098cdfbc806a67b814e3
---
M includes/CommentStore.php
M includes/CommentStoreComment.php
2 files changed, 37 insertions(+), 21 deletions(-)


  git pull ssh://gerrit.wikimedia.org:29418/mediawiki/core 
refs/changes/19/377319/1

diff --git a/includes/CommentStore.php b/includes/CommentStore.php
index 2ed21d1..b8a31e6 100644
--- a/includes/CommentStore.php
+++ b/includes/CommentStore.php
@@ -367,26 +367,7 @@
         * @return CommentStoreComment
         */
        public function createComment( IDatabase $dbw, $comment, array $data = 
null ) {
-               global $wgContLang;
-
-               if ( !$comment instanceof CommentStoreComment ) {
-                       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();
-                               $comment = new CommentStoreComment( null, 
$text, $message, $data );
-                       } else {
-                               $comment = new CommentStoreComment( null, 
$comment, null, $data );
-                       }
-               }
+               $comment = CommentStoreComment::newUnsavedComment( $comment, 
$data );
 
                # Truncate comment in a Unicode-sensitive manner
                $comment->text = $this->lang->truncate( $comment->text, 
self::MAX_COMMENT_LENGTH );
diff --git a/includes/CommentStoreComment.php b/includes/CommentStoreComment.php
index afc1374..3920ba0 100644
--- a/includes/CommentStoreComment.php
+++ b/includes/CommentStoreComment.php
@@ -42,7 +42,7 @@
        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 +54,39 @@
                $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 );
+               }
+       }
 }

-- 
To view, visit https://gerrit.wikimedia.org/r/377319
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings

Gerrit-MessageType: newchange
Gerrit-Change-Id: I76028e8ef3da4568a16e098cdfbc806a67b814e3
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/core
Gerrit-Branch: master
Gerrit-Owner: Anomie <[email protected]>

_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to