24use InvalidArgumentException;
67 private $joinCache = [];
97 return [
"{$key}_id" =>
"{$key}_id" ];
119 if ( !array_key_exists( $key, $this->joinCache ) ) {
124 $alias =
"comment_$key";
125 $tables[$alias] =
'comment';
126 $joins[$alias] = [
'JOIN',
"{$alias}.comment_id = {$key}_id" ];
128 $fields[
"{$key}_text"] =
"{$alias}.comment_text";
129 $fields[
"{$key}_data"] =
"{$alias}.comment_data";
130 $fields[
"{$key}_cid"] =
"{$alias}.comment_id";
132 $this->joinCache[$key] = [
139 return $this->joinCache[$key];
156 if ( array_key_exists(
"{$key}_text", $row ) && array_key_exists(
"{$key}_data", $row ) ) {
157 $cid = $row[
"{$key}_cid"] ??
null;
158 $text = $row[
"{$key}_text"];
159 $data = $row[
"{$key}_data"];
162 if ( array_key_exists(
"{$key}_id", $row ) ) {
164 throw new InvalidArgumentException(
165 "\$row does not contain fields needed for comment $key and getComment(), but "
166 .
"does have fields for getCommentLegacy()"
169 $id = $row[
"{$key}_id"];
171 ->select( [
'comment_id',
'comment_text',
'comment_data' ] )
173 ->where( [
'comment_id' => $id ] )
174 ->caller( __METHOD__ )->fetchRow();
176 if ( $row2 ===
null &&
$fallback && isset( $row[$key] ) ) {
177 wfLogWarning(
"Using deprecated fallback handling for comment $key" );
178 $row2 = (object)[
'comment_text' => $row[$key],
'comment_data' =>
null ];
180 if ( $row2 ===
null ) {
181 throw new InvalidArgumentException(
"\$row does not contain fields needed for comment $key" );
185 $cid = $row2->comment_id;
186 $text = $row2->comment_text;
187 $data = $row2->comment_data;
200 if ( $data !==
null ) {
202 if ( !is_array( $data ) ) {
204 wfLogWarning(
"Invalid JSON object in comment: $data" );
208 if ( isset( $data[
'_message'] ) ) {
209 $msg = self::decodeMessage( $data[
'_message'] )
212 if ( !empty( $data[
'_null'] ) ) {
215 foreach ( $data as $k => $v ) {
216 if ( substr( $k, 0, 1 ) ===
'_' ) {
224 return new CommentStoreComment( $cid, $text, $msg, $data );
244 if ( $row ===
null ) {
246 throw new InvalidArgumentException(
'$row must not be null' );
249 return $this->getCommentInternal(
null, $key, $row,
$fallback );
272 if ( $row ===
null ) {
274 throw new InvalidArgumentException(
'$row must not be null' );
277 return $this->getCommentInternal( $db, $key, $row,
$fallback );
303 # Truncate comment in a Unicode-sensitive manner
304 $comment->text = $this->lang->truncateForVisual( $comment->text, self::COMMENT_CHARACTER_LIMIT );
306 if ( !$comment->id ) {
307 $dbData = $comment->data;
308 if ( !$comment->message instanceof
RawMessage ) {
309 $dbData ??= [
'_null' =>
true ];
310 $dbData[
'_message'] = self::encodeMessage( $comment->message );
312 if ( $dbData !==
null ) {
314 $len = strlen( $dbData );
315 if ( $len > self::MAX_DATA_LENGTH ) {
317 throw new OverflowException(
"Comment data is too long ($len bytes, maximum is $max)" );
321 $hash =
self::hash( $comment->text, $dbData );
323 ->select(
'comment_id' )
326 'comment_hash' => $hash,
327 'comment_text' => $comment->text,
328 'comment_data' => $dbData,
330 ->caller( __METHOD__ )->fetchField();
333 ->insertInto(
'comment' )
334 ->row( [
'comment_hash' => $hash,
'comment_text' => $comment->text,
'comment_data' => $dbData ] )
335 ->caller( __METHOD__ )->execute();
338 $comment->id = (int)$commentId;
361 if ( $comment ===
null ) {
363 throw new InvalidArgumentException(
'$comment can not be null' );
368 return [
"{$key}_id" => $comment->id ];
376 private static function encodeMessage(
Message $msg ) {
379 foreach ( $params as &$param ) {
380 if ( $param instanceof
Message ) {
382 'message' => self::encodeMessage( $param )
386 array_unshift( $params, $key );
395 private static function decodeMessage( $data ) {
396 $key = array_shift( $data );
397 foreach ( $data as &$param ) {
398 if ( is_object( $param ) ) {
399 $param = (array)$param;
401 if ( is_array( $param ) && count( $param ) === 1 && isset( $param[
'message'] ) ) {
402 $param = self::decodeMessage( $param[
'message'] );
405 return new Message( $key, $data );
414 public static function hash( $text, $data ) {
415 $hash = crc32( $text ) ^ crc32( (
string)$data );
419 if ( $hash >= 0x80000000 ) {
431class_alias( CommentStore::class,
'CommentStore' );
wfLogWarning( $msg, $callerOffset=1, $level=E_USER_WARNING)
Send a warning as a PHP error and the debug log.
Base class for language-specific code.
The Message class deals with fetching and processing of interface message into a variety of formats.
getParams()
Returns the message parameters.
setInterfaceMessageFlag( $interface)
Allows manipulating the interface message flag directly.
getKey()
Returns the message key.