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 four 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
61 protected static $tempTables = [
63 'table' => 'revision_comment_temp',
64 'pk' => 'revcomment_rev',
65 'field' => 'revcomment_comment_id',
68 'img_description' => [
69 'table' => 'image_comment_temp',
70 'pk' => 'imgcomment_name',
71 'field' => 'imgcomment_description_id',
72 'joinPK' => 'img_name',
77 * Fields that formerly used $tempTables
78 * @var array Key is '$key', value is the MediaWiki version in which it was
79 * removed from $tempTables.
81 protected static $formerTempTables = [];
88 protected $key = null;
90 /** @var int One of the MIGRATION_* constants */
93 /** @var array[] Cache for `self::getJoin()` */
94 protected $joinCache = [];
96 /** @var Language Language to use for comment truncation */
100 * @param Language $lang Language to use for comment truncation. Defaults
101 * to content language.
102 * @param int $migrationStage One of the MIGRATION_* constants
104 public function __construct( Language
$lang, $migrationStage ) {
105 $this->stage
= $migrationStage;
110 * Static constructor for easier chaining
111 * @deprecated in 1.31 Should not be constructed with a $key, use CommentStore::getStore
112 * @param string $key A key such as "rev_comment" identifying the comment
113 * field being fetched.
114 * @return CommentStore
116 public static function newKey( $key ) {
117 global $wgCommentTableSchemaMigrationStage;
118 // TODO uncomment once not used in extensions
119 // wfDeprecated( __METHOD__, '1.31' );
120 $store = new CommentStore( MediaWikiServices
::getInstance()->getContentLanguage(),
121 $wgCommentTableSchemaMigrationStage );
128 * @deprecated in 1.31 Use DI to inject a CommentStore instance into your class.
129 * @return CommentStore
131 public static function getStore() {
132 return MediaWikiServices
::getInstance()->getCommentStore();
136 * Compat method allowing use of self::newKey until removed.
137 * @param string|null $methodKey
138 * @throws InvalidArgumentException
141 private function getKey( $methodKey = null ) {
142 $key = $this->key ??
$methodKey;
143 if ( $key === null ) {
144 // @codeCoverageIgnoreStart
145 throw new InvalidArgumentException( '$key should not be null' );
146 // @codeCoverageIgnoreEnd
152 * Get SELECT fields for the comment key
154 * Each resulting row should be passed to `self::getCommentLegacy()` to get the
157 * @note Use of this method may require a subsequent database query to
158 * actually fetch the comment. If possible, use `self::getJoin()` instead.
161 * @since 1.31 Method signature changed, $key parameter added (with deprecated back compat)
162 * @param string|null $key A key such as "rev_comment" identifying the comment
163 * field being fetched.
164 * @return string[] to include in the `$vars` to `IDatabase->select()`. All
165 * fields are aliased, so `+` is safe to use.
167 public function getFields( $key = null ) {
168 $key = $this->getKey( $key );
170 if ( $this->stage
=== MIGRATION_OLD
) {
171 $fields["{$key}_text"] = $key;
172 $fields["{$key}_data"] = 'NULL';
173 $fields["{$key}_cid"] = 'NULL';
175 if ( $this->stage
< MIGRATION_NEW
) {
176 $fields["{$key}_old"] = $key;
178 if ( isset( self
::$tempTables[$key] ) ) {
179 $fields["{$key}_pk"] = self
::$tempTables[$key]['joinPK'];
181 $fields["{$key}_id"] = "{$key}_id";
188 * Get SELECT fields and joins for the comment key
190 * Each resulting row should be passed to `self::getComment()` to get the
194 * @since 1.31 Method signature changed, $key parameter added (with deprecated back compat)
195 * @param string|null $key A key such as "rev_comment" identifying the comment
196 * field being fetched.
197 * @return array With three keys:
198 * - tables: (string[]) to include in the `$table` to `IDatabase->select()`
199 * - fields: (string[]) to include in the `$vars` to `IDatabase->select()`
200 * - joins: (array) to include in the `$join_conds` to `IDatabase->select()`
201 * All tables, fields, and joins are aliased, so `+` is safe to use.
203 public function getJoin( $key = null ) {
204 $key = $this->getKey( $key );
205 if ( !array_key_exists( $key, $this->joinCache
) ) {
210 if ( $this->stage
=== MIGRATION_OLD
) {
211 $fields["{$key}_text"] = $key;
212 $fields["{$key}_data"] = 'NULL';
213 $fields["{$key}_cid"] = 'NULL';
215 $join = $this->stage
=== MIGRATION_NEW ?
'JOIN' : 'LEFT JOIN';
217 if ( isset( self
::$tempTables[$key] ) ) {
218 $t = self
::$tempTables[$key];
219 $alias = "temp_$key";
220 $tables[$alias] = $t['table'];
221 $joins[$alias] = [ $join, "{$alias}.{$t['pk']} = {$t['joinPK']}" ];
222 $joinField = "{$alias}.{$t['field']}";
224 $joinField = "{$key}_id";
227 $alias = "comment_$key";
228 $tables[$alias] = 'comment';
229 $joins[$alias] = [ $join, "{$alias}.comment_id = {$joinField}" ];
231 if ( $this->stage
=== MIGRATION_NEW
) {
232 $fields["{$key}_text"] = "{$alias}.comment_text";
234 $fields["{$key}_text"] = "COALESCE( {$alias}.comment_text, $key )";
236 $fields["{$key}_data"] = "{$alias}.comment_data";
237 $fields["{$key}_cid"] = "{$alias}.comment_id";
240 $this->joinCache
[$key] = [
247 return $this->joinCache
[$key];
251 * Extract the comment from a row
253 * Shared implementation for getComment() and getCommentLegacy()
255 * @param IDatabase|null $db Database handle for getCommentLegacy(), or null for getComment()
256 * @param string $key A key such as "rev_comment" identifying the comment
257 * field being fetched.
258 * @param object|array $row
259 * @param bool $fallback
260 * @return CommentStoreComment
262 private function getCommentInternal( IDatabase
$db = null, $key, $row, $fallback = false ) {
264 if ( array_key_exists( "{$key}_text", $row ) && array_key_exists( "{$key}_data", $row ) ) {
265 $cid = $row["{$key}_cid"] ??
null;
266 $text = $row["{$key}_text"];
267 $data = $row["{$key}_data"];
268 } elseif ( $this->stage
=== MIGRATION_OLD
) {
270 if ( $fallback && isset( $row[$key] ) ) {
271 wfLogWarning( "Using deprecated fallback handling for comment $key" );
274 wfLogWarning( "Missing {$key}_text and {$key}_data fields in row with MIGRATION_OLD" );
279 if ( isset( self
::$tempTables[$key] ) ) {
280 if ( array_key_exists( "{$key}_pk", $row ) ) {
282 throw new InvalidArgumentException(
283 "\$row does not contain fields needed for comment $key and getComment(), but "
284 . "does have fields for getCommentLegacy()"
287 $t = self
::$tempTables[$key];
288 $id = $row["{$key}_pk"];
289 $row2 = $db->selectRow(
290 [ $t['table'], 'comment' ],
291 [ 'comment_id', 'comment_text', 'comment_data' ],
295 [ 'comment' => [ 'JOIN', [ "comment_id = {$t['field']}" ] ] ]
297 } elseif ( $fallback && isset( $row[$key] ) ) {
298 wfLogWarning( "Using deprecated fallback handling for comment $key" );
299 $row2 = (object)[ 'comment_text' => $row[$key], 'comment_data' => null ];
301 throw new InvalidArgumentException( "\$row does not contain fields needed for comment $key" );
304 if ( array_key_exists( "{$key}_id", $row ) ) {
306 throw new InvalidArgumentException(
307 "\$row does not contain fields needed for comment $key and getComment(), but "
308 . "does have fields for getCommentLegacy()"
311 $id = $row["{$key}_id"];
312 $row2 = $db->selectRow(
314 [ 'comment_id', 'comment_text', 'comment_data' ],
315 [ 'comment_id' => $id ],
318 } elseif ( $fallback && isset( $row[$key] ) ) {
319 wfLogWarning( "Using deprecated fallback handling for comment $key" );
320 $row2 = (object)[ 'comment_text' => $row[$key], 'comment_data' => null ];
322 throw new InvalidArgumentException( "\$row does not contain fields needed for comment $key" );
327 $cid = $row2->comment_id
;
328 $text = $row2->comment_text
;
329 $data = $row2->comment_data
;
330 } elseif ( $this->stage
< MIGRATION_NEW
&& array_key_exists( "{$key}_old", $row ) ) {
332 $text = $row["{$key}_old"];
335 // @codeCoverageIgnoreStart
336 wfLogWarning( "Missing comment row for $key, id=$id" );
340 // @codeCoverageIgnoreEnd
345 if ( $data !== null ) {
346 $data = FormatJson
::decode( $data );
347 if ( !is_object( $data ) ) {
348 // @codeCoverageIgnoreStart
349 wfLogWarning( "Invalid JSON object in comment: $data" );
351 // @codeCoverageIgnoreEnd
353 $data = (array)$data;
354 if ( isset( $data['_message'] ) ) {
355 $msg = self
::decodeMessage( $data['_message'] )
356 ->setInterfaceMessageFlag( true );
358 if ( !empty( $data['_null'] ) ) {
361 foreach ( $data as $k => $v ) {
362 if ( substr( $k, 0, 1 ) === '_' ) {
370 return new CommentStoreComment( $cid, $text, $msg, $data );
374 * Extract the comment from a row
376 * Use `self::getJoin()` to ensure the row contains the needed data.
378 * If you need to fake a comment in a row for some reason, set fields
379 * `{$key}_text` (string) and `{$key}_data` (JSON string or null).
382 * @since 1.31 Method signature changed, $key parameter added (with deprecated back compat)
383 * @param string $key A key such as "rev_comment" identifying the comment
384 * field being fetched.
385 * @param object|array|null $row Result row.
386 * @param bool $fallback If true, fall back as well as possible instead of throwing an exception.
387 * @return CommentStoreComment
389 public function getComment( $key, $row = null, $fallback = false ) {
390 // Compat for method sig change in 1.31 (introduction of $key)
391 if ( $this->key
!== null ) {
394 $key = $this->getKey();
396 if ( $row === null ) {
397 // @codeCoverageIgnoreStart
398 throw new InvalidArgumentException( '$row must not be null' );
399 // @codeCoverageIgnoreEnd
401 return $this->getCommentInternal( null, $key, $row, $fallback );
405 * Extract the comment from a row, with legacy lookups.
407 * If `$row` might have been generated using `self::getFields()` rather
408 * than `self::getJoin()`, use this. Prefer `self::getComment()` if you
409 * know callers used `self::getJoin()` for the row fetch.
411 * If you need to fake a comment in a row for some reason, set fields
412 * `{$key}_text` (string) and `{$key}_data` (JSON string or null).
415 * @since 1.31 Method signature changed, $key parameter added (with deprecated back compat)
416 * @param IDatabase $db Database handle to use for lookup
417 * @param string $key A key such as "rev_comment" identifying the comment
418 * field being fetched.
419 * @param object|array|null $row Result row.
420 * @param bool $fallback If true, fall back as well as possible instead of throwing an exception.
421 * @return CommentStoreComment
423 public function getCommentLegacy( IDatabase
$db, $key, $row = null, $fallback = false ) {
424 // Compat for method sig change in 1.31 (introduction of $key)
425 if ( $this->key
!== null ) {
428 $key = $this->getKey();
430 if ( $row === null ) {
431 // @codeCoverageIgnoreStart
432 throw new InvalidArgumentException( '$row must not be null' );
433 // @codeCoverageIgnoreEnd
435 return $this->getCommentInternal( $db, $key, $row, $fallback );
439 * Create a new CommentStoreComment, inserting it into the database if necessary
441 * If a comment is going to be passed to `self::insert()` or the like
442 * multiple times, it will be more efficient to pass a CommentStoreComment
443 * once rather than making `self::insert()` do it every time through.
445 * @note When passing a CommentStoreComment, this may set `$comment->id` if
446 * it's not already set. If `$comment->id` is already set, it will not be
447 * verified that the specified comment actually exists or that it
448 * corresponds to the comment text, message, and/or data in the
449 * CommentStoreComment.
450 * @param IDatabase $dbw Database handle to insert on. Unused if `$comment`
451 * is a CommentStoreComment and `$comment->id` is set.
452 * @param string|Message|CommentStoreComment $comment Comment text or Message object, or
453 * a CommentStoreComment.
454 * @param array|null $data Structured data to store. Keys beginning with '_' are reserved.
455 * Ignored if $comment is a CommentStoreComment.
456 * @return CommentStoreComment
458 public function createComment( IDatabase
$dbw, $comment, array $data = null ) {
459 $comment = CommentStoreComment
::newUnsavedComment( $comment, $data );
461 # Truncate comment in a Unicode-sensitive manner
462 $comment->text
= $this->lang
->truncateForVisual( $comment->text
, self
::COMMENT_CHARACTER_LIMIT
);
464 if ( $this->stage
> MIGRATION_OLD
&& !$comment->id
) {
465 $dbData = $comment->data
;
466 if ( !$comment->message
instanceof RawMessage
) {
467 if ( $dbData === null ) {
468 $dbData = [ '_null' => true ];
470 $dbData['_message'] = self
::encodeMessage( $comment->message
);
472 if ( $dbData !== null ) {
473 $dbData = FormatJson
::encode( (object)$dbData, false, FormatJson
::ALL_OK
);
474 $len = strlen( $dbData );
475 if ( $len > self
::MAX_DATA_LENGTH
) {
476 $max = self
::MAX_DATA_LENGTH
;
477 throw new OverflowException( "Comment data is too long ($len bytes, maximum is $max)" );
481 $hash = self
::hash( $comment->text
, $dbData );
482 $comment->id
= $dbw->selectField(
486 'comment_hash' => $hash,
487 'comment_text' => $comment->text
,
488 'comment_data' => $dbData,
492 if ( !$comment->id
) {
496 'comment_hash' => $hash,
497 'comment_text' => $comment->text
,
498 'comment_data' => $dbData,
502 $comment->id
= $dbw->insertId();
510 * Implementation for `self::insert()` and `self::insertWithTempTable()`
511 * @param IDatabase $dbw
512 * @param string $key A key such as "rev_comment" identifying the comment
513 * field being fetched.
514 * @param string|Message|CommentStoreComment $comment
515 * @param array|null $data
516 * @return array [ array $fields, callable $callback ]
518 private function insertInternal( IDatabase
$dbw, $key, $comment, $data ) {
522 $comment = $this->createComment( $dbw, $comment, $data );
524 if ( $this->stage
<= MIGRATION_WRITE_BOTH
) {
525 $fields[$key] = $this->lang
->truncateForDatabase( $comment->text
, 255 );
528 if ( $this->stage
>= MIGRATION_WRITE_BOTH
) {
529 if ( isset( self
::$tempTables[$key] ) ) {
530 $t = self
::$tempTables[$key];
532 $commentId = $comment->id
;
533 $callback = function ( $id ) use ( $dbw, $commentId, $t, $func ) {
538 $t['field'] => $commentId,
544 $fields["{$key}_id"] = $comment->id
;
548 return [ $fields, $callback ];
552 * Insert a comment in preparation for a row that references it
554 * @note It's recommended to include both the call to this method and the
555 * row insert in the same transaction.
558 * @since 1.31 Method signature changed, $key parameter added (with deprecated back compat)
559 * @param IDatabase $dbw Database handle to insert on
560 * @param string $key A key such as "rev_comment" identifying the comment
561 * field being fetched.
562 * @param string|Message|CommentStoreComment|null $comment As for `self::createComment()`
563 * @param array|null $data As for `self::createComment()`
564 * @return array Fields for the insert or update
566 public function insert( IDatabase
$dbw, $key, $comment = null, $data = null ) {
567 // Compat for method sig change in 1.31 (introduction of $key)
568 if ( $this->key
!== null ) {
573 if ( $comment === null ) {
574 // @codeCoverageIgnoreStart
575 throw new InvalidArgumentException( '$comment can not be null' );
576 // @codeCoverageIgnoreEnd
579 if ( isset( self
::$tempTables[$key] ) ) {
580 throw new InvalidArgumentException( "Must use insertWithTempTable() for $key" );
583 list( $fields ) = $this->insertInternal( $dbw, $key, $comment, $data );
588 * Insert a comment in a temporary table in preparation for a row that references it
590 * This is currently needed for "rev_comment" and "img_description". In the
591 * future that requirement will be removed.
593 * @note It's recommended to include both the call to this method and the
594 * row insert in the same transaction.
597 * @since 1.31 Method signature changed, $key parameter added (with deprecated back compat)
598 * @param IDatabase $dbw Database handle to insert on
599 * @param string $key A key such as "rev_comment" identifying the comment
600 * field being fetched.
601 * @param string|Message|CommentStoreComment|null $comment As for `self::createComment()`
602 * @param array|null $data As for `self::createComment()`
603 * @return array Two values:
604 * - array Fields for the insert or update
605 * - callable Function to call when the primary key of the row being
606 * inserted/updated is known. Pass it that primary key.
608 public function insertWithTempTable( IDatabase
$dbw, $key, $comment = null, $data = null ) {
609 // Compat for method sig change in 1.31 (introduction of $key)
610 if ( $this->key
!== null ) {
613 $key = $this->getKey();
615 if ( $comment === null ) {
616 // @codeCoverageIgnoreStart
617 throw new InvalidArgumentException( '$comment can not be null' );
618 // @codeCoverageIgnoreEnd
621 if ( isset( self
::$formerTempTables[$key] ) ) {
622 wfDeprecated( __METHOD__
. " for $key", self
::$formerTempTables[$key] );
623 } elseif ( !isset( self
::$tempTables[$key] ) ) {
624 throw new InvalidArgumentException( "Must use insert() for $key" );
627 list( $fields, $callback ) = $this->insertInternal( $dbw, $key, $comment, $data );
629 $callback = function () {
633 return [ $fields, $callback ];
637 * Encode a Message as a PHP data structure
638 * @param Message $msg
641 protected static function encodeMessage( Message
$msg ) {
642 $key = count( $msg->getKeysToTry() ) > 1 ?
$msg->getKeysToTry() : $msg->getKey();
643 $params = $msg->getParams();
644 foreach ( $params as &$param ) {
645 if ( $param instanceof Message
) {
647 'message' => self
::encodeMessage( $param )
651 array_unshift( $params, $key );
656 * Decode a message that was encoded by self::encodeMessage()
660 protected static function decodeMessage( $data ) {
661 $key = array_shift( $data );
662 foreach ( $data as &$param ) {
663 if ( is_object( $param ) ) {
664 $param = (array)$param;
666 if ( is_array( $param ) && count( $param ) === 1 && isset( $param['message'] ) ) {
667 $param = self
::decodeMessage( $param['message'] );
670 return new Message( $key, $data );
674 * Hashing function for comment storage
675 * @param string $text Comment text
676 * @param string|null $data Comment data
677 * @return int 32-bit signed integer
679 public static function hash( $text, $data ) {
680 $hash = crc32( $text ) ^
crc32( (string)$data );
682 // 64-bit PHP returns an unsigned CRC, change it to signed for
683 // insertion into the database.
684 if ( $hash >= 0x80000000 ) {