X-Git-Url: https://git.heureux-cyclage.org/?p=lhc%2Fweb%2Fwiklou.git;a=blobdiff_plain;f=includes%2FCommentStore.php;h=0d679d37db02b8c480d8cbf806e86afe33c5eae0;hp=2ed21d199554f68dd1ffdd89c938ef21da845275;hb=51fb1e29a0276bee59c57715d1a998c87593ee67;hpb=1d7a1bf8bddf0908e4f572c82268733f63126a13 diff --git a/includes/CommentStore.php b/includes/CommentStore.php index 2ed21d1995..0d679d37db 100644 --- a/includes/CommentStore.php +++ b/includes/CommentStore.php @@ -29,10 +29,24 @@ use Wikimedia\Rdbms\IDatabase; */ class CommentStore { - /** Maximum length of a comment. Longer comments will be truncated. */ + /** + * Maximum length of a comment in UTF-8 characters. Longer comments will be truncated. + * @note This must be at least 255 and not greater than floor( MAX_COMMENT_LENGTH / 4 ). + */ + const COMMENT_CHARACTER_LIMIT = 1000; + + /** + * Maximum length of a comment in bytes. Longer comments will be truncated. + * @note This value is determined by the size of the underlying database field, + * currently BLOB in MySQL/MariaDB. + */ const MAX_COMMENT_LENGTH = 65535; - /** Maximum length of serialized data. Longer data will result in an exception. */ + /** + * Maximum length of serialized data in bytes. Longer data will result in an exception. + * @note This value is determined by the size of the underlying database field, + * currently BLOB in MySQL/MariaDB. + */ const MAX_DATA_LENGTH = 65535; /** @@ -367,29 +381,19 @@ class CommentStore { * @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 ); + if ( mb_strlen( $comment->text, 'UTF-8' ) > self::COMMENT_CHARACTER_LIMIT ) { + $ellipsis = wfMessage( 'ellipsis' )->inLanguage( $this->lang )->escaped(); + if ( mb_strlen( $ellipsis ) >= self::COMMENT_CHARACTER_LIMIT ) { + // WTF? + $ellipsis = '...'; + } + $maxLength = self::COMMENT_CHARACTER_LIMIT - mb_strlen( $ellipsis, 'UTF-8' ); + $comment->text = mb_substr( $comment->text, 0, $maxLength, 'UTF-8' ) . $ellipsis; + } if ( $this->stage > MIGRATION_OLD && !$comment->id ) { $dbData = $comment->data;