Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
84.00% |
21 / 25 |
|
0.00% |
0 / 2 |
CRAP | |
0.00% |
0 / 1 |
CommentStoreComment | |
87.50% |
21 / 24 |
|
0.00% |
0 / 2 |
9.16 | |
0.00% |
0 / 1 |
__construct | |
81.82% |
9 / 11 |
|
0.00% |
0 / 1 |
3.05 | |||
newUnsavedComment | |
92.31% |
12 / 13 |
|
0.00% |
0 / 1 |
6.02 |
1 | <?php |
2 | /** |
3 | * This program is free software; you can redistribute it and/or modify |
4 | * it under the terms of the GNU General Public License as published by |
5 | * the Free Software Foundation; either version 2 of the License, or |
6 | * (at your option) any later version. |
7 | * |
8 | * This program is distributed in the hope that it will be useful, |
9 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
10 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
11 | * GNU General Public License for more details. |
12 | * |
13 | * You should have received a copy of the GNU General Public License along |
14 | * with this program; if not, write to the Free Software Foundation, Inc., |
15 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
16 | * http://www.gnu.org/copyleft/gpl.html |
17 | * |
18 | * @file |
19 | */ |
20 | |
21 | namespace MediaWiki\CommentStore; |
22 | |
23 | use InvalidArgumentException; |
24 | use MediaWiki\Language\RawMessage; |
25 | use MediaWiki\MediaWikiServices; |
26 | use MediaWiki\Message\Message; |
27 | |
28 | /** |
29 | * Value object for a comment stored by CommentStore. |
30 | * |
31 | * The fields should be considered read-only. |
32 | * |
33 | * @ingroup CommentStore |
34 | * @since 1.30 |
35 | */ |
36 | class CommentStoreComment { |
37 | |
38 | /** @var int|null Comment ID, if any */ |
39 | public $id; |
40 | |
41 | /** @var string Text version of the comment */ |
42 | public $text; |
43 | |
44 | /** @var Message Message version of the comment. Might be a RawMessage */ |
45 | public $message; |
46 | |
47 | /** @var array|null Structured data of the comment */ |
48 | public $data; |
49 | |
50 | /** |
51 | * @internal For use by CommentStore only. Use self::newUnsavedComment() instead. |
52 | * @param int|null $id |
53 | * @param string $text |
54 | * @param Message|null $message |
55 | * @param array|null $data |
56 | */ |
57 | public function __construct( $id, $text, Message $message = null, array $data = null ) { |
58 | if ( $text === null ) { |
59 | // TODO: Turn this warning into a proper type hint once we have |
60 | // found and fixed any offenders (T355751). |
61 | wfLogWarning( 'Comment text should not be null!' ); |
62 | $text = ''; |
63 | } |
64 | |
65 | $this->id = (int)$id; |
66 | $this->text = (string)$text; |
67 | $this->message = $message |
68 | ?: new RawMessage( |
69 | '$1', |
70 | [ Message::plaintextParam( $this->text ) ] |
71 | ); |
72 | $this->data = $data; |
73 | } |
74 | |
75 | /** |
76 | * Create a new, unsaved CommentStoreComment |
77 | * |
78 | * @param string|Message|CommentStoreComment $comment Comment text or Message object. |
79 | * A CommentStoreComment is also accepted here, in which case it is returned unchanged. |
80 | * @param array|null $data Structured data to store. Keys beginning with '_' are reserved. |
81 | * Ignored if $comment is a CommentStoreComment. |
82 | * @return CommentStoreComment |
83 | */ |
84 | public static function newUnsavedComment( $comment, array $data = null ) { |
85 | if ( $comment instanceof CommentStoreComment ) { |
86 | return $comment; |
87 | } |
88 | |
89 | if ( $data !== null ) { |
90 | foreach ( $data as $k => $v ) { |
91 | if ( substr( $k, 0, 1 ) === '_' ) { |
92 | throw new InvalidArgumentException( 'Keys in $data beginning with "_" are reserved' ); |
93 | } |
94 | } |
95 | } |
96 | |
97 | if ( $comment instanceof Message ) { |
98 | $message = clone $comment; |
99 | // Avoid $wgForceUIMsgAsContentMsg |
100 | $text = $message->inLanguage( MediaWikiServices::getInstance()->getContentLanguage() ) |
101 | ->setInterfaceMessageFlag( true ) |
102 | ->text(); |
103 | return new CommentStoreComment( null, $text, $message, $data ); |
104 | } else { |
105 | return new CommentStoreComment( null, $comment, null, $data ); |
106 | } |
107 | } |
108 | } |
109 | |
110 | /** @deprecated class alias since 1.40 */ |
111 | class_alias( CommentStoreComment::class, 'CommentStoreComment' ); |