MediaWiki REL1_33
CommentStore.php
Go to the documentation of this file.
1<?php
25
32
38
44 const MAX_COMMENT_LENGTH = 65535;
45
51 const MAX_DATA_LENGTH = 65535;
52
63 protected $tempTables = [
64 'rev_comment' => [
65 'table' => 'revision_comment_temp',
66 'pk' => 'revcomment_rev',
67 'field' => 'revcomment_comment_id',
68 'joinPK' => 'rev_id',
69 'stage' => MIGRATION_OLD,
70 'deprecatedIn' => null,
71 ],
72 'img_description' => [
73 'stage' => MIGRATION_NEW,
74 'deprecatedIn' => '1.32',
75 ],
76 ];
77
83 protected $key = null;
84
90 protected $stage;
91
93 protected $joinCache = [];
94
96 protected $lang;
97
104 public function __construct( Language $lang, $migrationStage ) {
105 $this->stage = $migrationStage;
106 $this->lang = $lang;
107 }
108
116 public static function newKey( $key ) {
117 wfDeprecated( __METHOD__, '1.31' );
118 $store = new CommentStore(
119 MediaWikiServices::getInstance()->getContentLanguage(), MIGRATION_NEW
120 );
121 $store->key = $key;
122 return $store;
123 }
124
130 public static function getStore() {
131 return MediaWikiServices::getInstance()->getCommentStore();
132 }
133
140 private function getKey( $methodKey = null ) {
141 $key = $this->key ?? $methodKey;
142 if ( $key === null ) {
143 // @codeCoverageIgnoreStart
144 throw new InvalidArgumentException( '$key should not be null' );
145 // @codeCoverageIgnoreEnd
146 }
147 return $key;
148 }
149
166 public function getFields( $key = null ) {
167 $key = $this->getKey( $key );
168 $fields = [];
169 if ( $this->stage === MIGRATION_OLD ) {
170 $fields["{$key}_text"] = $key;
171 $fields["{$key}_data"] = 'NULL';
172 $fields["{$key}_cid"] = 'NULL';
173 } else {
174 if ( $this->stage < MIGRATION_NEW ) {
175 $fields["{$key}_old"] = $key;
176 }
177
178 $tempTableStage = isset( $this->tempTables[$key] )
179 ? $this->tempTables[$key]['stage'] : MIGRATION_NEW;
180 if ( $tempTableStage < MIGRATION_NEW ) {
181 $fields["{$key}_pk"] = $this->tempTables[$key]['joinPK'];
182 }
183 if ( $tempTableStage > MIGRATION_OLD ) {
184 $fields["{$key}_id"] = "{$key}_id";
185 }
186 }
187 return $fields;
188 }
189
207 public function getJoin( $key = null ) {
208 $key = $this->getKey( $key );
209 if ( !array_key_exists( $key, $this->joinCache ) ) {
210 $tables = [];
211 $fields = [];
212 $joins = [];
213
214 if ( $this->stage === MIGRATION_OLD ) {
215 $fields["{$key}_text"] = $key;
216 $fields["{$key}_data"] = 'NULL';
217 $fields["{$key}_cid"] = 'NULL';
218 } else {
219 $join = $this->stage === MIGRATION_NEW ? 'JOIN' : 'LEFT JOIN';
220
221 $tempTableStage = isset( $this->tempTables[$key] )
222 ? $this->tempTables[$key]['stage'] : MIGRATION_NEW;
223 if ( $tempTableStage < MIGRATION_NEW ) {
224 $t = $this->tempTables[$key];
225 $alias = "temp_$key";
226 $tables[$alias] = $t['table'];
227 $joins[$alias] = [ $join, "{$alias}.{$t['pk']} = {$t['joinPK']}" ];
228 if ( $tempTableStage === MIGRATION_OLD ) {
229 $joinField = "{$alias}.{$t['field']}";
230 } else {
231 // Nothing hits this code path for now, but will in the future when we set
232 // $this->tempTables['rev_comment']['stage'] to MIGRATION_WRITE_NEW while
233 // merging revision_comment_temp into revision.
234 // @codeCoverageIgnoreStart
235 $joins[$alias][0] = 'LEFT JOIN';
236 $joinField = "(CASE WHEN {$key}_id != 0 THEN {$key}_id ELSE {$alias}.{$t['field']} END)";
237 throw new LogicException( 'Nothing should reach this code path at this time' );
238 // @codeCoverageIgnoreEnd
239 }
240 } else {
241 $joinField = "{$key}_id";
242 }
243
244 $alias = "comment_$key";
245 $tables[$alias] = 'comment';
246 $joins[$alias] = [ $join, "{$alias}.comment_id = {$joinField}" ];
247
248 if ( $this->stage === MIGRATION_NEW ) {
249 $fields["{$key}_text"] = "{$alias}.comment_text";
250 } else {
251 $fields["{$key}_text"] = "COALESCE( {$alias}.comment_text, $key )";
252 }
253 $fields["{$key}_data"] = "{$alias}.comment_data";
254 $fields["{$key}_cid"] = "{$alias}.comment_id";
255 }
256
257 $this->joinCache[$key] = [
258 'tables' => $tables,
259 'fields' => $fields,
260 'joins' => $joins,
261 ];
262 }
263
264 return $this->joinCache[$key];
265 }
266
279 private function getCommentInternal( IDatabase $db = null, $key, $row, $fallback = false ) {
280 $row = (array)$row;
281 if ( array_key_exists( "{$key}_text", $row ) && array_key_exists( "{$key}_data", $row ) ) {
282 $cid = $row["{$key}_cid"] ?? null;
283 $text = $row["{$key}_text"];
284 $data = $row["{$key}_data"];
285 } elseif ( $this->stage === MIGRATION_OLD ) {
286 $cid = null;
287 if ( $fallback && isset( $row[$key] ) ) {
288 wfLogWarning( "Using deprecated fallback handling for comment $key" );
289 $text = $row[$key];
290 } else {
291 wfLogWarning( "Missing {$key}_text and {$key}_data fields in row with MIGRATION_OLD" );
292 $text = '';
293 }
294 $data = null;
295 } else {
296 $tempTableStage = isset( $this->tempTables[$key] )
297 ? $this->tempTables[$key]['stage'] : MIGRATION_NEW;
298 $row2 = null;
299 if ( $tempTableStage > MIGRATION_OLD && array_key_exists( "{$key}_id", $row ) ) {
300 if ( !$db ) {
301 throw new InvalidArgumentException(
302 "\$row does not contain fields needed for comment $key and getComment(), but "
303 . "does have fields for getCommentLegacy()"
304 );
305 }
306 $id = $row["{$key}_id"];
307 $row2 = $db->selectRow(
308 'comment',
309 [ 'comment_id', 'comment_text', 'comment_data' ],
310 [ 'comment_id' => $id ],
311 __METHOD__
312 );
313 }
314 if ( !$row2 && $tempTableStage < MIGRATION_NEW && array_key_exists( "{$key}_pk", $row ) ) {
315 if ( !$db ) {
316 throw new InvalidArgumentException(
317 "\$row does not contain fields needed for comment $key and getComment(), but "
318 . "does have fields for getCommentLegacy()"
319 );
320 }
321 $t = $this->tempTables[$key];
322 $id = $row["{$key}_pk"];
323 $row2 = $db->selectRow(
324 [ $t['table'], 'comment' ],
325 [ 'comment_id', 'comment_text', 'comment_data' ],
326 [ $t['pk'] => $id ],
327 __METHOD__,
328 [],
329 [ 'comment' => [ 'JOIN', [ "comment_id = {$t['field']}" ] ] ]
330 );
331 }
332 if ( $row2 === null && $fallback && isset( $row[$key] ) ) {
333 wfLogWarning( "Using deprecated fallback handling for comment $key" );
334 $row2 = (object)[ 'comment_text' => $row[$key], 'comment_data' => null ];
335 }
336 if ( $row2 === null ) {
337 throw new InvalidArgumentException( "\$row does not contain fields needed for comment $key" );
338 }
339
340 if ( $row2 ) {
341 $cid = $row2->comment_id;
342 $text = $row2->comment_text;
343 $data = $row2->comment_data;
344 } elseif ( $this->stage < MIGRATION_NEW && array_key_exists( "{$key}_old", $row ) ) {
345 $cid = null;
346 $text = $row["{$key}_old"];
347 $data = null;
348 } else {
349 // @codeCoverageIgnoreStart
350 wfLogWarning( "Missing comment row for $key, id=$id" );
351 $cid = null;
352 $text = '';
353 $data = null;
354 // @codeCoverageIgnoreEnd
355 }
356 }
357
358 $msg = null;
359 if ( $data !== null ) {
360 $data = FormatJson::decode( $data, true );
361 if ( !is_array( $data ) ) {
362 // @codeCoverageIgnoreStart
363 wfLogWarning( "Invalid JSON object in comment: $data" );
364 $data = null;
365 // @codeCoverageIgnoreEnd
366 } else {
367 if ( isset( $data['_message'] ) ) {
368 $msg = self::decodeMessage( $data['_message'] )
369 ->setInterfaceMessageFlag( true );
370 }
371 if ( !empty( $data['_null'] ) ) {
372 $data = null;
373 } else {
374 foreach ( $data as $k => $v ) {
375 if ( substr( $k, 0, 1 ) === '_' ) {
376 unset( $data[$k] );
377 }
378 }
379 }
380 }
381 }
382
383 return new CommentStoreComment( $cid, $text, $msg, $data );
384 }
385
402 public function getComment( $key, $row = null, $fallback = false ) {
403 // Compat for method sig change in 1.31 (introduction of $key)
404 if ( $this->key !== null ) {
405 $fallback = $row;
406 $row = $key;
407 $key = $this->getKey();
408 }
409 if ( $row === null ) {
410 // @codeCoverageIgnoreStart
411 throw new InvalidArgumentException( '$row must not be null' );
412 // @codeCoverageIgnoreEnd
413 }
414 return $this->getCommentInternal( null, $key, $row, $fallback );
415 }
416
436 public function getCommentLegacy( IDatabase $db, $key, $row = null, $fallback = false ) {
437 // Compat for method sig change in 1.31 (introduction of $key)
438 if ( $this->key !== null ) {
439 $fallback = $row;
440 $row = $key;
441 $key = $this->getKey();
442 }
443 if ( $row === null ) {
444 // @codeCoverageIgnoreStart
445 throw new InvalidArgumentException( '$row must not be null' );
446 // @codeCoverageIgnoreEnd
447 }
448 return $this->getCommentInternal( $db, $key, $row, $fallback );
449 }
450
471 public function createComment( IDatabase $dbw, $comment, array $data = null ) {
472 $comment = CommentStoreComment::newUnsavedComment( $comment, $data );
473
474 # Truncate comment in a Unicode-sensitive manner
475 $comment->text = $this->lang->truncateForVisual( $comment->text, self::COMMENT_CHARACTER_LIMIT );
476
477 if ( $this->stage > MIGRATION_OLD && !$comment->id ) {
478 $dbData = $comment->data;
479 if ( !$comment->message instanceof RawMessage ) {
480 if ( $dbData === null ) {
481 $dbData = [ '_null' => true ];
482 }
483 $dbData['_message'] = self::encodeMessage( $comment->message );
484 }
485 if ( $dbData !== null ) {
486 $dbData = FormatJson::encode( (object)$dbData, false, FormatJson::ALL_OK );
487 $len = strlen( $dbData );
488 if ( $len > self::MAX_DATA_LENGTH ) {
489 $max = self::MAX_DATA_LENGTH;
490 throw new OverflowException( "Comment data is too long ($len bytes, maximum is $max)" );
491 }
492 }
493
494 $hash = self::hash( $comment->text, $dbData );
495 $comment->id = $dbw->selectField(
496 'comment',
497 'comment_id',
498 [
499 'comment_hash' => $hash,
500 'comment_text' => $comment->text,
501 'comment_data' => $dbData,
502 ],
503 __METHOD__
504 );
505 if ( !$comment->id ) {
506 $dbw->insert(
507 'comment',
508 [
509 'comment_hash' => $hash,
510 'comment_text' => $comment->text,
511 'comment_data' => $dbData,
512 ],
513 __METHOD__
514 );
515 $comment->id = $dbw->insertId();
516 }
517 }
518
519 return $comment;
520 }
521
531 private function insertInternal( IDatabase $dbw, $key, $comment, $data ) {
532 $fields = [];
533 $callback = null;
534
535 $comment = $this->createComment( $dbw, $comment, $data );
536
537 if ( $this->stage <= MIGRATION_WRITE_BOTH ) {
538 $fields[$key] = $this->lang->truncateForDatabase( $comment->text, 255 );
539 }
540
541 if ( $this->stage >= MIGRATION_WRITE_BOTH ) {
542 $tempTableStage = isset( $this->tempTables[$key] )
543 ? $this->tempTables[$key]['stage'] : MIGRATION_NEW;
544 if ( $tempTableStage <= MIGRATION_WRITE_BOTH ) {
545 $t = $this->tempTables[$key];
546 $func = __METHOD__;
547 $commentId = $comment->id;
548 $callback = function ( $id ) use ( $dbw, $commentId, $t, $func ) {
549 $dbw->insert(
550 $t['table'],
551 [
552 $t['pk'] => $id,
553 $t['field'] => $commentId,
554 ],
555 $func
556 );
557 };
558 }
559 if ( $tempTableStage >= MIGRATION_WRITE_BOTH ) {
560 $fields["{$key}_id"] = $comment->id;
561 }
562 }
563
564 return [ $fields, $callback ];
565 }
566
582 public function insert( IDatabase $dbw, $key, $comment = null, $data = null ) {
583 // Compat for method sig change in 1.31 (introduction of $key)
584 if ( $this->key !== null ) {
585 $data = $comment;
586 $comment = $key;
587 $key = $this->key;
588 }
589 if ( $comment === null ) {
590 // @codeCoverageIgnoreStart
591 throw new InvalidArgumentException( '$comment can not be null' );
592 // @codeCoverageIgnoreEnd
593 }
594
595 $tempTableStage = isset( $this->tempTables[$key] )
596 ? $this->tempTables[$key]['stage'] : MIGRATION_NEW;
597 if ( $tempTableStage < MIGRATION_WRITE_NEW ) {
598 throw new InvalidArgumentException( "Must use insertWithTempTable() for $key" );
599 }
600
601 list( $fields ) = $this->insertInternal( $dbw, $key, $comment, $data );
602 return $fields;
603 }
604
626 public function insertWithTempTable( IDatabase $dbw, $key, $comment = null, $data = null ) {
627 // Compat for method sig change in 1.31 (introduction of $key)
628 if ( $this->key !== null ) {
629 $data = $comment;
630 $comment = $key;
631 $key = $this->getKey();
632 }
633 if ( $comment === null ) {
634 // @codeCoverageIgnoreStart
635 throw new InvalidArgumentException( '$comment can not be null' );
636 // @codeCoverageIgnoreEnd
637 }
638
639 if ( !isset( $this->tempTables[$key] ) ) {
640 throw new InvalidArgumentException( "Must use insert() for $key" );
641 } elseif ( isset( $this->tempTables[$key]['deprecatedIn'] ) ) {
642 wfDeprecated( __METHOD__ . " for $key", $this->tempTables[$key]['deprecatedIn'] );
643 }
644
645 list( $fields, $callback ) = $this->insertInternal( $dbw, $key, $comment, $data );
646 if ( !$callback ) {
647 $callback = function () {
648 // Do nothing.
649 };
650 }
651 return [ $fields, $callback ];
652 }
653
659 protected static function encodeMessage( Message $msg ) {
660 $key = count( $msg->getKeysToTry() ) > 1 ? $msg->getKeysToTry() : $msg->getKey();
661 $params = $msg->getParams();
662 foreach ( $params as &$param ) {
663 if ( $param instanceof Message ) {
664 $param = [
665 'message' => self::encodeMessage( $param )
666 ];
667 }
668 }
669 array_unshift( $params, $key );
670 return $params;
671 }
672
678 protected static function decodeMessage( $data ) {
679 $key = array_shift( $data );
680 foreach ( $data as &$param ) {
681 if ( is_object( $param ) ) {
682 $param = (array)$param;
683 }
684 if ( is_array( $param ) && count( $param ) === 1 && isset( $param['message'] ) ) {
685 $param = self::decodeMessage( $param['message'] );
686 }
687 }
688 return new Message( $key, $data );
689 }
690
697 public static function hash( $text, $data ) {
698 $hash = crc32( $text ) ^ crc32( (string)$data );
699
700 // 64-bit PHP returns an unsigned CRC, change it to signed for
701 // insertion into the database.
702 if ( $hash >= 0x80000000 ) {
703 $hash |= -1 << 32;
704 }
705
706 return $hash;
707 }
708
709}
Apache License January AND DISTRIBUTION Definitions License shall mean the terms and conditions for use
wfLogWarning( $msg, $callerOffset=1, $level=E_USER_WARNING)
Send a warning as a PHP error and the debug log.
wfDeprecated( $function, $version=false, $component=false, $callerOffset=2)
Throws a warning that $function is deprecated.
$fallback
CommentStoreComment represents a comment stored by CommentStore.
CommentStore handles storage of comments (edit summaries, log reasons, etc) in the database.
int $stage
One of the MIGRATION_* constants.
static newKey( $key)
Static constructor for easier chaining.
getKey( $methodKey=null)
Compat method allowing use of self::newKey until removed.
getComment( $key, $row=null, $fallback=false)
Extract the comment from a row.
__construct(Language $lang, $migrationStage)
getJoin( $key=null)
Get SELECT fields and joins for the comment key.
createComment(IDatabase $dbw, $comment, array $data=null)
Create a new CommentStoreComment, inserting it into the database if necessary.
insertWithTempTable(IDatabase $dbw, $key, $comment=null, $data=null)
Insert a comment in a temporary table in preparation for a row that references it.
getCommentLegacy(IDatabase $db, $key, $row=null, $fallback=false)
Extract the comment from a row, with legacy lookups.
static hash( $text, $data)
Hashing function for comment storage.
getFields( $key=null)
Get SELECT fields for the comment key.
insertInternal(IDatabase $dbw, $key, $comment, $data)
Implementation for self::insert() and self::insertWithTempTable()
Language $lang
Language to use for comment truncation.
const MAX_COMMENT_LENGTH
Maximum length of a comment in bytes.
static decodeMessage( $data)
Decode a message that was encoded by self::encodeMessage()
string null $key
array[] $joinCache
Cache for self::getJoin()
static encodeMessage(Message $msg)
Encode a Message as a PHP data structure.
insert(IDatabase $dbw, $key, $comment=null, $data=null)
Insert a comment in preparation for a row that references it.
array $tempTables
Define fields that use temporary tables for transitional purposes Keys are '$key',...
const MAX_DATA_LENGTH
Maximum length of serialized data in bytes.
static getStore()
const COMMENT_CHARACTER_LIMIT
Maximum length of a comment in UTF-8 characters.
getCommentInternal(IDatabase $db=null, $key, $row, $fallback=false)
Extract the comment from a row.
Internationalisation code.
Definition Language.php:36
MediaWikiServices is the service locator for the application scope of MediaWiki.
The Message class provides methods which fulfil two basic services:
Definition Message.php:160
getParams()
Returns the message parameters.
Definition Message.php:367
getKeysToTry()
Definition Message.php:341
getKey()
Returns the message key.
Definition Message.php:356
Variant of the Message class.
deferred txt A few of the database updates required by various functions here can be deferred until after the result page is displayed to the user For updating the view updating the linked to tables after a etc PHP does not yet have any way to tell the server to actually return and disconnect while still running these but it might have such a feature in the future We handle these by creating a deferred update object and putting those objects on a global list
Definition deferred.txt:11
This document is intended to provide useful advice for parties seeking to redistribute MediaWiki to end users It s targeted particularly at maintainers for Linux since it s been observed that distribution packages of MediaWiki often break We ve consistently had to recommend that users seeking support use official tarballs instead of their distribution s and this often solves whatever problem the user is having It would be nice if this could such as
$data
Utility to generate mapping file used in mw.Title (phpCharToUpper.json)
globals will be eliminated from MediaWiki replaced by an application object which would be passed to constructors Whether that would be an convenient solution remains to be but certainly PHP makes such object oriented programming models easier than they were in previous versions For the time being MediaWiki programmers will have to work in an environment with some global context At the time of globals were initialised on startup by MediaWiki of these were configuration which are documented in DefaultSettings php There is no comprehensive documentation for the remaining however some of the most important ones are listed below They are typically initialised either in index php or in Setup php $wgTitle Title object created from the request URL $wgOut OutputPage object for HTTP response $wgUser User object for the user associated with the current request $wgLang Language object selected by user preferences $wgContLang Language object associated with the wiki being viewed $wgParser Parser object Parser extensions register their hooks here $wgRequest WebRequest object
Definition globals.txt:62
const MIGRATION_WRITE_NEW
Definition Defines.php:326
const MIGRATION_NEW
Definition Defines.php:327
const MIGRATION_WRITE_BOTH
Definition Defines.php:325
const MIGRATION_OLD
Definition Defines.php:324
either a unescaped string or a HtmlArmor object after in associative array form externallinks including delete and has completed for all link tables whether this was an auto creation use $formDescriptor instead default is conds Array Extra conditions for the No matching items in log is displayed if loglist is empty msgKey Array If you want a nice box with a set this to the key of the message First element is the message key
Definition hooks.txt:2163
this hook is for auditing only RecentChangesLinked and Watchlist Do not use this to implement individual filters if they are compatible with the ChangesListFilter and ChangesListFilterGroup structure use sub classes of those in conjunction with the ChangesListSpecialPageStructuredFilters hook This hook can be used to implement filters that do not implement that or custom behavior that is not an individual filter e g Watchlist & $tables
Definition hooks.txt:996
null means default in associative array with keys and values unescaped Should be merged with default with a value of false meaning to suppress the attribute in associative array with keys and values unescaped noclasses just before the function returns a value If you return true
Definition hooks.txt:2004
injection txt This is an overview of how MediaWiki makes use of dependency injection The design described here grew from the discussion of RFC T384 The term dependency this means that anything an object needs to operate should be injected from the the object itself should only know narrow no concrete implementation of the logic it relies on The requirement to inject everything typically results in an architecture that based on two main types of and essentially stateless service objects that use other service objects to operate on the value objects As of the beginning MediaWiki is only starting to use the DI approach Much of the code still relies on global state or direct resulting in a highly cyclical dependency which acts as the top level factory for services in MediaWiki which can be used to gain access to default instances of various services MediaWikiServices however also allows new services to be defined and default services to be redefined Services are defined or redefined by providing a callback the instantiator that will return a new instance of the service When it will create an instance of MediaWikiServices and populate it with the services defined in the files listed by thereby bootstrapping the DI framework Per $wgServiceWiringFiles lists includes ServiceWiring php
Definition injection.txt:37
Basic database interface for live and lazy-loaded relation database handles.
Definition IDatabase.php:38
selectField( $table, $var, $cond='', $fname=__METHOD__, $options=[], $join_conds=[])
A SELECT wrapper which returns a single field from a single result row.
insert( $table, $a, $fname=__METHOD__, $options=[])
INSERT wrapper, inserts an array into a table.
insertId()
Get the inserted value of an auto-increment row.
The wiki should then use memcached to cache various data To use multiple just add more items to the array To increase the weight of a make its entry a array("192.168.0.1:11211", 2))
$params
if(!isset( $args[0])) $lang