wiki.techinc.nl/includes/CommentStore/CommentStoreComment.php
daniel 6f1f783c77 CommentStore: Force message to be a string
Apparently, we end with the comment text being null sometimes.
That should not happen, and can lead to failures down the road.
So we need to detect and prevent this case early.

Bug: T355751
Change-Id: Ifc91416d297e9a569bc7a2981732e5dd808429aa
2024-01-25 15:02:05 +00:00

113 lines
3.3 KiB
PHP

<?php
/**
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
* http://www.gnu.org/copyleft/gpl.html
*
* @file
*/
namespace MediaWiki\CommentStore;
use InvalidArgumentException;
use MediaWiki\Language\RawMessage;
use MediaWiki\MediaWikiServices;
use Message;
/**
* Value object for a comment stored by CommentStore.
*
* The fields should be considered read-only.
*
* @ingroup CommentStore
* @since 1.30
*/
class CommentStoreComment {
/** @var int|null Comment ID, if any */
public $id;
/** @var string Text version of the comment */
public $text;
/** @var Message Message version of the comment. Might be a RawMessage */
public $message;
/** @var array|null Structured data of the comment */
public $data;
/**
* @internal For use by CommentStore only. Use self::newUnsavedComment() instead.
* @param int|null $id
* @param string $text
* @param Message|null $message
* @param array|null $data
*/
public function __construct( $id, $text, Message $message = null, array $data = null ) {
if ( $text === null ) {
// TODO: Turn this warning into a proper type hint once we have
// found and fixed any offenders (T355751).
wfLogWarning( 'Comment text should not be null!' );
$text = '';
}
$this->id = (int)$id;
$this->text = (string)$text;
$this->message = $message
?: new RawMessage(
'$1',
[ Message::plaintextParam( $this->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 ) {
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;
// Avoid $wgForceUIMsgAsContentMsg
$text = $message->inLanguage( MediaWikiServices::getInstance()->getContentLanguage() )
->setInterfaceMessageFlag( true )
->text();
return new CommentStoreComment( null, $text, $message, $data );
} else {
return new CommentStoreComment( null, $comment, null, $data );
}
}
}
/**
* @deprecated since 1.40
*/
class_alias( CommentStoreComment::class, 'CommentStoreComment' );