3 * Manage storage of comments in the database
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.
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.
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
23 use MediaWiki\MediaWikiServices
;
24 use Wikimedia\Rdbms\IDatabase
;
27 * CommentStore handles storage of comments (edit summaries, log reasons, etc)
34 * Maximum length of a comment in UTF-8 characters. Longer comments will be truncated.
35 * @note This must be at least 255 and not greater than floor( MAX_COMMENT_LENGTH / 4 ).
37 const COMMENT_CHARACTER_LIMIT
= 500;
40 * Maximum length of a comment in bytes. Longer comments will be truncated.
41 * @note This value is determined by the size of the underlying database field,
42 * currently BLOB in MySQL/MariaDB.
44 const MAX_COMMENT_LENGTH
= 65535;
47 * Maximum length of serialized data in bytes. Longer data will result in an exception.
48 * @note This value is determined by the size of the underlying database field,
49 * currently BLOB in MySQL/MariaDB.
51 const MAX_DATA_LENGTH
= 65535;
54 * Define fields that use temporary tables for transitional purposes
55 * @var array Keys are '$key', values are arrays with these possible fields:
56 * - table: Temporary table name
57 * - pk: Temporary table column referring to the main table's primary key
58 * - field: Temporary table column referring comment.comment_id
59 * - joinPK: Main table's primary key
60 * - stage: Migration stage
61 * - deprecatedIn: Version when using insertWithTempTable() was deprecated
63 protected $tempTables = [
65 'table' => 'revision_comment_temp',
66 'pk' => 'revcomment_rev',
67 'field' => 'revcomment_comment_id',
69 'stage' => MIGRATION_OLD
,
70 'deprecatedIn' => null,
72 'img_description' => [
73 'table' => 'image_comment_temp',
74 'pk' => 'imgcomment_name',
75 'field' => 'imgcomment_description_id',
76 'joinPK' => 'img_name',
77 'stage' => MIGRATION_WRITE_NEW
,
78 'deprecatedIn' => '1.32',
87 protected $key = null;
89 /** @var int One of the MIGRATION_* constants */
92 /** @var array[] Cache for `self::getJoin()` */
93 protected $joinCache = [];
95 /** @var Language Language to use for comment truncation */
99 * @param Language $lang Language to use for comment truncation. Defaults
100 * to content language.
101 * @param int $migrationStage One of the MIGRATION_* constants
103 public function __construct( Language
$lang, $migrationStage ) {
104 $this->stage
= $migrationStage;
109 * Static constructor for easier chaining
110 * @deprecated in 1.31 Should not be constructed with a $key, use CommentStore::getStore
111 * @param string $key A key such as "rev_comment" identifying the comment
112 * field being fetched.
113 * @return CommentStore
115 public static function newKey( $key ) {
116 global $wgCommentTableSchemaMigrationStage;
117 wfDeprecated( __METHOD__
, '1.31' );
118 $store = new CommentStore( MediaWikiServices
::getInstance()->getContentLanguage(),
119 $wgCommentTableSchemaMigrationStage );
126 * @deprecated in 1.31 Use DI to inject a CommentStore instance into your class.
127 * @return CommentStore
129 public static function getStore() {
130 return MediaWikiServices
::getInstance()->getCommentStore();
134 * Compat method allowing use of self::newKey until removed.
135 * @param string|null $methodKey
136 * @throws InvalidArgumentException
139 private function getKey( $methodKey = null ) {
140 $key = $this->key ??
$methodKey;
141 if ( $key === null ) {
142 // @codeCoverageIgnoreStart
143 throw new InvalidArgumentException( '$key should not be null' );
144 // @codeCoverageIgnoreEnd
150 * Get SELECT fields for the comment key
152 * Each resulting row should be passed to `self::getCommentLegacy()` to get the
155 * @note Use of this method may require a subsequent database query to
156 * actually fetch the comment. If possible, use `self::getJoin()` instead.
159 * @since 1.31 Method signature changed, $key parameter added (with deprecated back compat)
160 * @param string|null $key A key such as "rev_comment" identifying the comment
161 * field being fetched.
162 * @return string[] to include in the `$vars` to `IDatabase->select()`. All
163 * fields are aliased, so `+` is safe to use.
165 public function getFields( $key = null ) {
166 $key = $this->getKey( $key );
168 if ( $this->stage
=== MIGRATION_OLD
) {
169 $fields["{$key}_text"] = $key;
170 $fields["{$key}_data"] = 'NULL';
171 $fields["{$key}_cid"] = 'NULL';
173 if ( $this->stage
< MIGRATION_NEW
) {
174 $fields["{$key}_old"] = $key;
177 $tempTableStage = isset( $this->tempTables
[$key] )
178 ?
$this->tempTables
[$key]['stage'] : MIGRATION_NEW
;
179 if ( $tempTableStage < MIGRATION_NEW
) {
180 $fields["{$key}_pk"] = $this->tempTables
[$key]['joinPK'];
182 if ( $tempTableStage > MIGRATION_OLD
) {
183 $fields["{$key}_id"] = "{$key}_id";
190 * Get SELECT fields and joins for the comment key
192 * Each resulting row should be passed to `self::getComment()` to get the
196 * @since 1.31 Method signature changed, $key parameter added (with deprecated back compat)
197 * @param string|null $key A key such as "rev_comment" identifying the comment
198 * field being fetched.
199 * @return array With three keys:
200 * - tables: (string[]) to include in the `$table` to `IDatabase->select()`
201 * - fields: (string[]) to include in the `$vars` to `IDatabase->select()`
202 * - joins: (array) to include in the `$join_conds` to `IDatabase->select()`
203 * All tables, fields, and joins are aliased, so `+` is safe to use.
205 public function getJoin( $key = null ) {
206 $key = $this->getKey( $key );
207 if ( !array_key_exists( $key, $this->joinCache
) ) {
212 if ( $this->stage
=== MIGRATION_OLD
) {
213 $fields["{$key}_text"] = $key;
214 $fields["{$key}_data"] = 'NULL';
215 $fields["{$key}_cid"] = 'NULL';
217 $join = $this->stage
=== MIGRATION_NEW ?
'JOIN' : 'LEFT JOIN';
219 $tempTableStage = isset( $this->tempTables
[$key] )
220 ?
$this->tempTables
[$key]['stage'] : MIGRATION_NEW
;
221 if ( $tempTableStage < MIGRATION_NEW
) {
222 $t = $this->tempTables
[$key];
223 $alias = "temp_$key";
224 $tables[$alias] = $t['table'];
225 $joins[$alias] = [ $join, "{$alias}.{$t['pk']} = {$t['joinPK']}" ];
226 if ( $tempTableStage === MIGRATION_OLD
) {
227 $joinField = "{$alias}.{$t['field']}";
229 $joins[$alias][0] = 'LEFT JOIN';
230 $joinField = "(CASE WHEN {$key}_id != 0 THEN {$key}_id ELSE {$alias}.{$t['field']} END)";
233 $joinField = "{$key}_id";
236 $alias = "comment_$key";
237 $tables[$alias] = 'comment';
238 $joins[$alias] = [ $join, "{$alias}.comment_id = {$joinField}" ];
240 if ( $this->stage
=== MIGRATION_NEW
) {
241 $fields["{$key}_text"] = "{$alias}.comment_text";
243 $fields["{$key}_text"] = "COALESCE( {$alias}.comment_text, $key )";
245 $fields["{$key}_data"] = "{$alias}.comment_data";
246 $fields["{$key}_cid"] = "{$alias}.comment_id";
249 $this->joinCache
[$key] = [
256 return $this->joinCache
[$key];
260 * Extract the comment from a row
262 * Shared implementation for getComment() and getCommentLegacy()
264 * @param IDatabase|null $db Database handle for getCommentLegacy(), or null for getComment()
265 * @param string $key A key such as "rev_comment" identifying the comment
266 * field being fetched.
267 * @param object|array $row
268 * @param bool $fallback
269 * @return CommentStoreComment
271 private function getCommentInternal( IDatabase
$db = null, $key, $row, $fallback = false ) {
273 if ( array_key_exists( "{$key}_text", $row ) && array_key_exists( "{$key}_data", $row ) ) {
274 $cid = $row["{$key}_cid"] ??
null;
275 $text = $row["{$key}_text"];
276 $data = $row["{$key}_data"];
277 } elseif ( $this->stage
=== MIGRATION_OLD
) {
279 if ( $fallback && isset( $row[$key] ) ) {
280 wfLogWarning( "Using deprecated fallback handling for comment $key" );
283 wfLogWarning( "Missing {$key}_text and {$key}_data fields in row with MIGRATION_OLD" );
288 $tempTableStage = isset( $this->tempTables
[$key] )
289 ?
$this->tempTables
[$key]['stage'] : MIGRATION_NEW
;
291 if ( $tempTableStage > MIGRATION_OLD
&& array_key_exists( "{$key}_id", $row ) ) {
293 throw new InvalidArgumentException(
294 "\$row does not contain fields needed for comment $key and getComment(), but "
295 . "does have fields for getCommentLegacy()"
298 $id = $row["{$key}_id"];
299 $row2 = $db->selectRow(
301 [ 'comment_id', 'comment_text', 'comment_data' ],
302 [ 'comment_id' => $id ],
306 if ( !$row2 && $tempTableStage < MIGRATION_NEW
&& array_key_exists( "{$key}_pk", $row ) ) {
308 throw new InvalidArgumentException(
309 "\$row does not contain fields needed for comment $key and getComment(), but "
310 . "does have fields for getCommentLegacy()"
313 $t = $this->tempTables
[$key];
314 $id = $row["{$key}_pk"];
315 $row2 = $db->selectRow(
316 [ $t['table'], 'comment' ],
317 [ 'comment_id', 'comment_text', 'comment_data' ],
321 [ 'comment' => [ 'JOIN', [ "comment_id = {$t['field']}" ] ] ]
324 if ( $row2 === null && $fallback && isset( $row[$key] ) ) {
325 wfLogWarning( "Using deprecated fallback handling for comment $key" );
326 $row2 = (object)[ 'comment_text' => $row[$key], 'comment_data' => null ];
328 if ( $row2 === null ) {
329 throw new InvalidArgumentException( "\$row does not contain fields needed for comment $key" );
333 $cid = $row2->comment_id
;
334 $text = $row2->comment_text
;
335 $data = $row2->comment_data
;
336 } elseif ( $this->stage
< MIGRATION_NEW
&& array_key_exists( "{$key}_old", $row ) ) {
338 $text = $row["{$key}_old"];
341 // @codeCoverageIgnoreStart
342 wfLogWarning( "Missing comment row for $key, id=$id" );
346 // @codeCoverageIgnoreEnd
351 if ( $data !== null ) {
352 $data = FormatJson
::decode( $data );
353 if ( !is_object( $data ) ) {
354 // @codeCoverageIgnoreStart
355 wfLogWarning( "Invalid JSON object in comment: $data" );
357 // @codeCoverageIgnoreEnd
359 $data = (array)$data;
360 if ( isset( $data['_message'] ) ) {
361 $msg = self
::decodeMessage( $data['_message'] )
362 ->setInterfaceMessageFlag( true );
364 if ( !empty( $data['_null'] ) ) {
367 foreach ( $data as $k => $v ) {
368 if ( substr( $k, 0, 1 ) === '_' ) {
376 return new CommentStoreComment( $cid, $text, $msg, $data );
380 * Extract the comment from a row
382 * Use `self::getJoin()` to ensure the row contains the needed data.
384 * If you need to fake a comment in a row for some reason, set fields
385 * `{$key}_text` (string) and `{$key}_data` (JSON string or null).
388 * @since 1.31 Method signature changed, $key parameter added (with deprecated back compat)
389 * @param string $key A key such as "rev_comment" identifying the comment
390 * field being fetched.
391 * @param object|array|null $row Result row.
392 * @param bool $fallback If true, fall back as well as possible instead of throwing an exception.
393 * @return CommentStoreComment
395 public function getComment( $key, $row = null, $fallback = false ) {
396 // Compat for method sig change in 1.31 (introduction of $key)
397 if ( $this->key
!== null ) {
400 $key = $this->getKey();
402 if ( $row === null ) {
403 // @codeCoverageIgnoreStart
404 throw new InvalidArgumentException( '$row must not be null' );
405 // @codeCoverageIgnoreEnd
407 return $this->getCommentInternal( null, $key, $row, $fallback );
411 * Extract the comment from a row, with legacy lookups.
413 * If `$row` might have been generated using `self::getFields()` rather
414 * than `self::getJoin()`, use this. Prefer `self::getComment()` if you
415 * know callers used `self::getJoin()` for the row fetch.
417 * If you need to fake a comment in a row for some reason, set fields
418 * `{$key}_text` (string) and `{$key}_data` (JSON string or null).
421 * @since 1.31 Method signature changed, $key parameter added (with deprecated back compat)
422 * @param IDatabase $db Database handle to use for lookup
423 * @param string $key A key such as "rev_comment" identifying the comment
424 * field being fetched.
425 * @param object|array|null $row Result row.
426 * @param bool $fallback If true, fall back as well as possible instead of throwing an exception.
427 * @return CommentStoreComment
429 public function getCommentLegacy( IDatabase
$db, $key, $row = null, $fallback = false ) {
430 // Compat for method sig change in 1.31 (introduction of $key)
431 if ( $this->key
!== null ) {
434 $key = $this->getKey();
436 if ( $row === null ) {
437 // @codeCoverageIgnoreStart
438 throw new InvalidArgumentException( '$row must not be null' );
439 // @codeCoverageIgnoreEnd
441 return $this->getCommentInternal( $db, $key, $row, $fallback );
445 * Create a new CommentStoreComment, inserting it into the database if necessary
447 * If a comment is going to be passed to `self::insert()` or the like
448 * multiple times, it will be more efficient to pass a CommentStoreComment
449 * once rather than making `self::insert()` do it every time through.
451 * @note When passing a CommentStoreComment, this may set `$comment->id` if
452 * it's not already set. If `$comment->id` is already set, it will not be
453 * verified that the specified comment actually exists or that it
454 * corresponds to the comment text, message, and/or data in the
455 * CommentStoreComment.
456 * @param IDatabase $dbw Database handle to insert on. Unused if `$comment`
457 * is a CommentStoreComment and `$comment->id` is set.
458 * @param string|Message|CommentStoreComment $comment Comment text or Message object, or
459 * a CommentStoreComment.
460 * @param array|null $data Structured data to store. Keys beginning with '_' are reserved.
461 * Ignored if $comment is a CommentStoreComment.
462 * @return CommentStoreComment
464 public function createComment( IDatabase
$dbw, $comment, array $data = null ) {
465 $comment = CommentStoreComment
::newUnsavedComment( $comment, $data );
467 # Truncate comment in a Unicode-sensitive manner
468 $comment->text
= $this->lang
->truncateForVisual( $comment->text
, self
::COMMENT_CHARACTER_LIMIT
);
470 if ( $this->stage
> MIGRATION_OLD
&& !$comment->id
) {
471 $dbData = $comment->data
;
472 if ( !$comment->message
instanceof RawMessage
) {
473 if ( $dbData === null ) {
474 $dbData = [ '_null' => true ];
476 $dbData['_message'] = self
::encodeMessage( $comment->message
);
478 if ( $dbData !== null ) {
479 $dbData = FormatJson
::encode( (object)$dbData, false, FormatJson
::ALL_OK
);
480 $len = strlen( $dbData );
481 if ( $len > self
::MAX_DATA_LENGTH
) {
482 $max = self
::MAX_DATA_LENGTH
;
483 throw new OverflowException( "Comment data is too long ($len bytes, maximum is $max)" );
487 $hash = self
::hash( $comment->text
, $dbData );
488 $comment->id
= $dbw->selectField(
492 'comment_hash' => $hash,
493 'comment_text' => $comment->text
,
494 'comment_data' => $dbData,
498 if ( !$comment->id
) {
502 'comment_hash' => $hash,
503 'comment_text' => $comment->text
,
504 'comment_data' => $dbData,
508 $comment->id
= $dbw->insertId();
516 * Implementation for `self::insert()` and `self::insertWithTempTable()`
517 * @param IDatabase $dbw
518 * @param string $key A key such as "rev_comment" identifying the comment
519 * field being fetched.
520 * @param string|Message|CommentStoreComment $comment
521 * @param array|null $data
522 * @return array [ array $fields, callable $callback ]
524 private function insertInternal( IDatabase
$dbw, $key, $comment, $data ) {
528 $comment = $this->createComment( $dbw, $comment, $data );
530 if ( $this->stage
<= MIGRATION_WRITE_BOTH
) {
531 $fields[$key] = $this->lang
->truncateForDatabase( $comment->text
, 255 );
534 if ( $this->stage
>= MIGRATION_WRITE_BOTH
) {
535 $tempTableStage = isset( $this->tempTables
[$key] )
536 ?
$this->tempTables
[$key]['stage'] : MIGRATION_NEW
;
537 if ( $tempTableStage <= MIGRATION_WRITE_BOTH
) {
538 $t = $this->tempTables
[$key];
540 $commentId = $comment->id
;
541 $callback = function ( $id ) use ( $dbw, $commentId, $t, $func ) {
546 $t['field'] => $commentId,
552 if ( $tempTableStage >= MIGRATION_WRITE_BOTH
) {
553 $fields["{$key}_id"] = $comment->id
;
557 return [ $fields, $callback ];
561 * Insert a comment in preparation for a row that references it
563 * @note It's recommended to include both the call to this method and the
564 * row insert in the same transaction.
567 * @since 1.31 Method signature changed, $key parameter added (with deprecated back compat)
568 * @param IDatabase $dbw Database handle to insert on
569 * @param string $key A key such as "rev_comment" identifying the comment
570 * field being fetched.
571 * @param string|Message|CommentStoreComment|null $comment As for `self::createComment()`
572 * @param array|null $data As for `self::createComment()`
573 * @return array Fields for the insert or update
575 public function insert( IDatabase
$dbw, $key, $comment = null, $data = null ) {
576 // Compat for method sig change in 1.31 (introduction of $key)
577 if ( $this->key
!== null ) {
582 if ( $comment === null ) {
583 // @codeCoverageIgnoreStart
584 throw new InvalidArgumentException( '$comment can not be null' );
585 // @codeCoverageIgnoreEnd
588 $tempTableStage = isset( $this->tempTables
[$key] )
589 ?
$this->tempTables
[$key]['stage'] : MIGRATION_NEW
;
590 if ( $tempTableStage < MIGRATION_WRITE_NEW
) {
591 throw new InvalidArgumentException( "Must use insertWithTempTable() for $key" );
594 list( $fields ) = $this->insertInternal( $dbw, $key, $comment, $data );
599 * Insert a comment in a temporary table in preparation for a row that references it
601 * This is currently needed for "rev_comment" and "img_description". In the
602 * future that requirement will be removed.
604 * @note It's recommended to include both the call to this method and the
605 * row insert in the same transaction.
608 * @since 1.31 Method signature changed, $key parameter added (with deprecated back compat)
609 * @param IDatabase $dbw Database handle to insert on
610 * @param string $key A key such as "rev_comment" identifying the comment
611 * field being fetched.
612 * @param string|Message|CommentStoreComment|null $comment As for `self::createComment()`
613 * @param array|null $data As for `self::createComment()`
614 * @return array Two values:
615 * - array Fields for the insert or update
616 * - callable Function to call when the primary key of the row being
617 * inserted/updated is known. Pass it that primary key.
619 public function insertWithTempTable( IDatabase
$dbw, $key, $comment = null, $data = null ) {
620 // Compat for method sig change in 1.31 (introduction of $key)
621 if ( $this->key
!== null ) {
624 $key = $this->getKey();
626 if ( $comment === null ) {
627 // @codeCoverageIgnoreStart
628 throw new InvalidArgumentException( '$comment can not be null' );
629 // @codeCoverageIgnoreEnd
632 if ( !isset( $this->tempTables
[$key] ) ) {
633 throw new InvalidArgumentException( "Must use insert() for $key" );
634 } elseif ( isset( $this->tempTables
[$key]['deprecatedIn'] ) ) {
635 wfDeprecated( __METHOD__
. " for $key", $this->tempTables
[$key]['deprecatedIn'] );
638 list( $fields, $callback ) = $this->insertInternal( $dbw, $key, $comment, $data );
640 $callback = function () {
644 return [ $fields, $callback ];
648 * Encode a Message as a PHP data structure
649 * @param Message $msg
652 protected static function encodeMessage( Message
$msg ) {
653 $key = count( $msg->getKeysToTry() ) > 1 ?
$msg->getKeysToTry() : $msg->getKey();
654 $params = $msg->getParams();
655 foreach ( $params as &$param ) {
656 if ( $param instanceof Message
) {
658 'message' => self
::encodeMessage( $param )
662 array_unshift( $params, $key );
667 * Decode a message that was encoded by self::encodeMessage()
671 protected static function decodeMessage( $data ) {
672 $key = array_shift( $data );
673 foreach ( $data as &$param ) {
674 if ( is_object( $param ) ) {
675 $param = (array)$param;
677 if ( is_array( $param ) && count( $param ) === 1 && isset( $param['message'] ) ) {
678 $param = self
::decodeMessage( $param['message'] );
681 return new Message( $key, $data );
685 * Hashing function for comment storage
686 * @param string $text Comment text
687 * @param string|null $data Comment data
688 * @return int 32-bit signed integer
690 public static function hash( $text, $data ) {
691 $hash = crc32( $text ) ^
crc32( (string)$data );
693 // 64-bit PHP returns an unsigned CRC, change it to signed for
694 // insertion into the database.
695 if ( $hash >= 0x80000000 ) {