| 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, string $text, ?Message $message = null, ?array $data = null ) { |
| 58 | $this->id = (int)$id; |
| 59 | $this->text = $text; |
| 60 | $this->message = $message |
| 61 | ?: new RawMessage( |
| 62 | '$1', |
| 63 | [ Message::plaintextParam( $this->text ) ] |
| 64 | ); |
| 65 | $this->data = $data; |
| 66 | } |
| 67 | |
| 68 | /** |
| 69 | * Create a new, unsaved CommentStoreComment |
| 70 | * |
| 71 | * @param string|Message|CommentStoreComment $comment Comment text or Message object. |
| 72 | * A CommentStoreComment is also accepted here, in which case it is returned unchanged. |
| 73 | * @param array|null $data Structured data to store. Keys beginning with '_' are reserved. |
| 74 | * Ignored if $comment is a CommentStoreComment. |
| 75 | * @return CommentStoreComment |
| 76 | */ |
| 77 | public static function newUnsavedComment( $comment, ?array $data = null ) { |
| 78 | if ( $comment instanceof CommentStoreComment ) { |
| 79 | return $comment; |
| 80 | } |
| 81 | |
| 82 | if ( $data !== null ) { |
| 83 | foreach ( $data as $k => $v ) { |
| 84 | if ( substr( $k, 0, 1 ) === '_' ) { |
| 85 | throw new InvalidArgumentException( 'Keys in $data beginning with "_" are reserved' ); |
| 86 | } |
| 87 | } |
| 88 | } |
| 89 | |
| 90 | if ( $comment instanceof Message ) { |
| 91 | $message = clone $comment; |
| 92 | // Avoid $wgForceUIMsgAsContentMsg |
| 93 | $text = $message->inLanguage( MediaWikiServices::getInstance()->getContentLanguage() ) |
| 94 | ->setInterfaceMessageFlag( true ) |
| 95 | ->text(); |
| 96 | return new CommentStoreComment( null, $text, $message, $data ); |
| 97 | } else { |
| 98 | return new CommentStoreComment( null, $comment, null, $data ); |
| 99 | } |
| 100 | } |
| 101 | } |
| 102 | |
| 103 | /** @deprecated class alias since 1.40 */ |
| 104 | class_alias( CommentStoreComment::class, 'CommentStoreComment' ); |