CommentStoreComment.php 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. <?php
  2. /**
  3. * Value object for CommentStore
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 2 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License along
  16. * with this program; if not, write to the Free Software Foundation, Inc.,
  17. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  18. * http://www.gnu.org/copyleft/gpl.html
  19. *
  20. * @file
  21. */
  22. use MediaWiki\MediaWikiServices;
  23. /**
  24. * CommentStoreComment represents a comment stored by CommentStore. The fields
  25. * should be considered read-only.
  26. * @since 1.30
  27. */
  28. class CommentStoreComment {
  29. /** @var int|null Comment ID, if any */
  30. public $id;
  31. /** @var string Text version of the comment */
  32. public $text;
  33. /** @var Message Message version of the comment. Might be a RawMessage */
  34. public $message;
  35. /** @var array|null Structured data of the comment */
  36. public $data;
  37. /**
  38. * @private For use by CommentStore only. Use self::newUnsavedComment() instead.
  39. * @param int|null $id
  40. * @param string $text
  41. * @param Message|null $message
  42. * @param array|null $data
  43. */
  44. public function __construct( $id, $text, Message $message = null, array $data = null ) {
  45. $this->id = $id;
  46. $this->text = $text;
  47. $this->message = $message ?: new RawMessage( '$1', [ Message::plaintextParam( $text ) ] );
  48. $this->data = $data;
  49. }
  50. /**
  51. * Create a new, unsaved CommentStoreComment
  52. *
  53. * @param string|Message|CommentStoreComment $comment Comment text or Message object.
  54. * A CommentStoreComment is also accepted here, in which case it is returned unchanged.
  55. * @param array|null $data Structured data to store. Keys beginning with '_' are reserved.
  56. * Ignored if $comment is a CommentStoreComment.
  57. * @return CommentStoreComment
  58. */
  59. public static function newUnsavedComment( $comment, array $data = null ) {
  60. if ( $comment instanceof CommentStoreComment ) {
  61. return $comment;
  62. }
  63. if ( $data !== null ) {
  64. foreach ( $data as $k => $v ) {
  65. if ( substr( $k, 0, 1 ) === '_' ) {
  66. throw new InvalidArgumentException( 'Keys in $data beginning with "_" are reserved' );
  67. }
  68. }
  69. }
  70. if ( $comment instanceof Message ) {
  71. $message = clone $comment;
  72. // Avoid $wgForceUIMsgAsContentMsg
  73. $text = $message->inLanguage( MediaWikiServices::getInstance()->getContentLanguage() )
  74. ->setInterfaceMessageFlag( true )
  75. ->text();
  76. return new CommentStoreComment( null, $text, $message, $data );
  77. } else {
  78. return new CommentStoreComment( null, $comment, null, $data );
  79. }
  80. }
  81. }