MediaWiki  master
CommentStore.php
Go to the documentation of this file.
1 <?php
25 
31 class CommentStore {
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 
86  protected $stage;
87 
89  protected $joinCache = [];
90 
92  protected $lang;
93 
99  public function __construct( Language $lang, $migrationStage ) {
100  $this->stage = $migrationStage;
101  $this->lang = $lang;
102  }
103 
111  public static function newKey( $key ) {
113  wfDeprecated( __METHOD__, '1.31' );
114  $store = new CommentStore( MediaWikiServices::getInstance()->getContentLanguage(),
115  $wgCommentTableSchemaMigrationStage );
116  $store->key = $key;
117  return $store;
118  }
119 
125  public static function getStore() {
126  return MediaWikiServices::getInstance()->getCommentStore();
127  }
128 
135  private function getKey( $methodKey = null ) {
136  $key = $this->key ?? $methodKey;
137  if ( $key === null ) {
138  // @codeCoverageIgnoreStart
139  throw new InvalidArgumentException( '$key should not be null' );
140  // @codeCoverageIgnoreEnd
141  }
142  return $key;
143  }
144 
161  public function getFields( $key = null ) {
162  $key = $this->getKey( $key );
163  $fields = [];
164  if ( $this->stage === MIGRATION_OLD ) {
165  $fields["{$key}_text"] = $key;
166  $fields["{$key}_data"] = 'NULL';
167  $fields["{$key}_cid"] = 'NULL';
168  } else {
169  if ( $this->stage < MIGRATION_NEW ) {
170  $fields["{$key}_old"] = $key;
171  }
172 
173  $tempTableStage = isset( $this->tempTables[$key] )
174  ? $this->tempTables[$key]['stage'] : MIGRATION_NEW;
175  if ( $tempTableStage < MIGRATION_NEW ) {
176  $fields["{$key}_pk"] = $this->tempTables[$key]['joinPK'];
177  }
178  if ( $tempTableStage > MIGRATION_OLD ) {
179  $fields["{$key}_id"] = "{$key}_id";
180  }
181  }
182  return $fields;
183  }
184 
201  public function getJoin( $key = null ) {
202  $key = $this->getKey( $key );
203  if ( !array_key_exists( $key, $this->joinCache ) ) {
204  $tables = [];
205  $fields = [];
206  $joins = [];
207 
208  if ( $this->stage === MIGRATION_OLD ) {
209  $fields["{$key}_text"] = $key;
210  $fields["{$key}_data"] = 'NULL';
211  $fields["{$key}_cid"] = 'NULL';
212  } else {
213  $join = $this->stage === MIGRATION_NEW ? 'JOIN' : 'LEFT JOIN';
214 
215  $tempTableStage = isset( $this->tempTables[$key] )
216  ? $this->tempTables[$key]['stage'] : MIGRATION_NEW;
217  if ( $tempTableStage < MIGRATION_NEW ) {
218  $t = $this->tempTables[$key];
219  $alias = "temp_$key";
220  $tables[$alias] = $t['table'];
221  $joins[$alias] = [ $join, "{$alias}.{$t['pk']} = {$t['joinPK']}" ];
222  if ( $tempTableStage === MIGRATION_OLD ) {
223  $joinField = "{$alias}.{$t['field']}";
224  } else {
225  // Nothing hits this code path for now, but will in the future when we set
226  // $this->tempTables['rev_comment']['stage'] to MIGRATION_WRITE_NEW while
227  // merging revision_comment_temp into revision.
228  // @codeCoverageIgnoreStart
229  $joins[$alias][0] = 'LEFT JOIN';
230  $joinField = "(CASE WHEN {$key}_id != 0 THEN {$key}_id ELSE {$alias}.{$t['field']} END)";
231  throw new LogicException( 'Nothing should reach this code path at this time' );
232  // @codeCoverageIgnoreEnd
233  }
234  } else {
235  $joinField = "{$key}_id";
236  }
237 
238  $alias = "comment_$key";
239  $tables[$alias] = 'comment';
240  $joins[$alias] = [ $join, "{$alias}.comment_id = {$joinField}" ];
241 
242  if ( $this->stage === MIGRATION_NEW ) {
243  $fields["{$key}_text"] = "{$alias}.comment_text";
244  } else {
245  $fields["{$key}_text"] = "COALESCE( {$alias}.comment_text, $key )";
246  }
247  $fields["{$key}_data"] = "{$alias}.comment_data";
248  $fields["{$key}_cid"] = "{$alias}.comment_id";
249  }
250 
251  $this->joinCache[$key] = [
252  'tables' => $tables,
253  'fields' => $fields,
254  'joins' => $joins,
255  ];
256  }
257 
258  return $this->joinCache[$key];
259  }
260 
273  private function getCommentInternal( IDatabase $db = null, $key, $row, $fallback = false ) {
274  $row = (array)$row;
275  if ( array_key_exists( "{$key}_text", $row ) && array_key_exists( "{$key}_data", $row ) ) {
276  $cid = $row["{$key}_cid"] ?? null;
277  $text = $row["{$key}_text"];
278  $data = $row["{$key}_data"];
279  } elseif ( $this->stage === MIGRATION_OLD ) {
280  $cid = null;
281  if ( $fallback && isset( $row[$key] ) ) {
282  wfLogWarning( "Using deprecated fallback handling for comment $key" );
283  $text = $row[$key];
284  } else {
285  wfLogWarning( "Missing {$key}_text and {$key}_data fields in row with MIGRATION_OLD" );
286  $text = '';
287  }
288  $data = null;
289  } else {
290  $tempTableStage = isset( $this->tempTables[$key] )
291  ? $this->tempTables[$key]['stage'] : MIGRATION_NEW;
292  $row2 = null;
293  if ( $tempTableStage > MIGRATION_OLD && array_key_exists( "{$key}_id", $row ) ) {
294  if ( !$db ) {
295  throw new InvalidArgumentException(
296  "\$row does not contain fields needed for comment $key and getComment(), but "
297  . "does have fields for getCommentLegacy()"
298  );
299  }
300  $id = $row["{$key}_id"];
301  $row2 = $db->selectRow(
302  'comment',
303  [ 'comment_id', 'comment_text', 'comment_data' ],
304  [ 'comment_id' => $id ],
305  __METHOD__
306  );
307  }
308  if ( !$row2 && $tempTableStage < MIGRATION_NEW && array_key_exists( "{$key}_pk", $row ) ) {
309  if ( !$db ) {
310  throw new InvalidArgumentException(
311  "\$row does not contain fields needed for comment $key and getComment(), but "
312  . "does have fields for getCommentLegacy()"
313  );
314  }
315  $t = $this->tempTables[$key];
316  $id = $row["{$key}_pk"];
317  $row2 = $db->selectRow(
318  [ $t['table'], 'comment' ],
319  [ 'comment_id', 'comment_text', 'comment_data' ],
320  [ $t['pk'] => $id ],
321  __METHOD__,
322  [],
323  [ 'comment' => [ 'JOIN', [ "comment_id = {$t['field']}" ] ] ]
324  );
325  }
326  if ( $row2 === null && $fallback && isset( $row[$key] ) ) {
327  wfLogWarning( "Using deprecated fallback handling for comment $key" );
328  $row2 = (object)[ 'comment_text' => $row[$key], 'comment_data' => null ];
329  }
330  if ( $row2 === null ) {
331  throw new InvalidArgumentException( "\$row does not contain fields needed for comment $key" );
332  }
333 
334  if ( $row2 ) {
335  $cid = $row2->comment_id;
336  $text = $row2->comment_text;
337  $data = $row2->comment_data;
338  } elseif ( $this->stage < MIGRATION_NEW && array_key_exists( "{$key}_old", $row ) ) {
339  $cid = null;
340  $text = $row["{$key}_old"];
341  $data = null;
342  } else {
343  // @codeCoverageIgnoreStart
344  wfLogWarning( "Missing comment row for $key, id=$id" );
345  $cid = null;
346  $text = '';
347  $data = null;
348  // @codeCoverageIgnoreEnd
349  }
350  }
351 
352  $msg = null;
353  if ( $data !== null ) {
354  $data = FormatJson::decode( $data, true );
355  if ( !is_array( $data ) ) {
356  // @codeCoverageIgnoreStart
357  wfLogWarning( "Invalid JSON object in comment: $data" );
358  $data = null;
359  // @codeCoverageIgnoreEnd
360  } else {
361  if ( isset( $data['_message'] ) ) {
362  $msg = self::decodeMessage( $data['_message'] )
363  ->setInterfaceMessageFlag( true );
364  }
365  if ( !empty( $data['_null'] ) ) {
366  $data = null;
367  } else {
368  foreach ( $data as $k => $v ) {
369  if ( substr( $k, 0, 1 ) === '_' ) {
370  unset( $data[$k] );
371  }
372  }
373  }
374  }
375  }
376 
377  return new CommentStoreComment( $cid, $text, $msg, $data );
378  }
379 
396  public function getComment( $key, $row = null, $fallback = false ) {
397  // Compat for method sig change in 1.31 (introduction of $key)
398  if ( $this->key !== null ) {
399  $fallback = $row;
400  $row = $key;
401  $key = $this->getKey();
402  }
403  if ( $row === null ) {
404  // @codeCoverageIgnoreStart
405  throw new InvalidArgumentException( '$row must not be null' );
406  // @codeCoverageIgnoreEnd
407  }
408  return $this->getCommentInternal( null, $key, $row, $fallback );
409  }
410 
430  public function getCommentLegacy( IDatabase $db, $key, $row = null, $fallback = false ) {
431  // Compat for method sig change in 1.31 (introduction of $key)
432  if ( $this->key !== null ) {
433  $fallback = $row;
434  $row = $key;
435  $key = $this->getKey();
436  }
437  if ( $row === null ) {
438  // @codeCoverageIgnoreStart
439  throw new InvalidArgumentException( '$row must not be null' );
440  // @codeCoverageIgnoreEnd
441  }
442  return $this->getCommentInternal( $db, $key, $row, $fallback );
443  }
444 
465  public function createComment( IDatabase $dbw, $comment, array $data = null ) {
466  $comment = CommentStoreComment::newUnsavedComment( $comment, $data );
467 
468  # Truncate comment in a Unicode-sensitive manner
469  $comment->text = $this->lang->truncateForVisual( $comment->text, self::COMMENT_CHARACTER_LIMIT );
470 
471  if ( $this->stage > MIGRATION_OLD && !$comment->id ) {
472  $dbData = $comment->data;
473  if ( !$comment->message instanceof RawMessage ) {
474  if ( $dbData === null ) {
475  $dbData = [ '_null' => true ];
476  }
477  $dbData['_message'] = self::encodeMessage( $comment->message );
478  }
479  if ( $dbData !== null ) {
480  $dbData = FormatJson::encode( (object)$dbData, false, FormatJson::ALL_OK );
481  $len = strlen( $dbData );
482  if ( $len > self::MAX_DATA_LENGTH ) {
483  $max = self::MAX_DATA_LENGTH;
484  throw new OverflowException( "Comment data is too long ($len bytes, maximum is $max)" );
485  }
486  }
487 
488  $hash = self::hash( $comment->text, $dbData );
489  $comment->id = $dbw->selectField(
490  'comment',
491  'comment_id',
492  [
493  'comment_hash' => $hash,
494  'comment_text' => $comment->text,
495  'comment_data' => $dbData,
496  ],
497  __METHOD__
498  );
499  if ( !$comment->id ) {
500  $dbw->insert(
501  'comment',
502  [
503  'comment_hash' => $hash,
504  'comment_text' => $comment->text,
505  'comment_data' => $dbData,
506  ],
507  __METHOD__
508  );
509  $comment->id = $dbw->insertId();
510  }
511  }
512 
513  return $comment;
514  }
515 
525  private function insertInternal( IDatabase $dbw, $key, $comment, $data ) {
526  $fields = [];
527  $callback = null;
528 
529  $comment = $this->createComment( $dbw, $comment, $data );
530 
531  if ( $this->stage <= MIGRATION_WRITE_BOTH ) {
532  $fields[$key] = $this->lang->truncateForDatabase( $comment->text, 255 );
533  }
534 
535  if ( $this->stage >= MIGRATION_WRITE_BOTH ) {
536  $tempTableStage = isset( $this->tempTables[$key] )
537  ? $this->tempTables[$key]['stage'] : MIGRATION_NEW;
538  if ( $tempTableStage <= MIGRATION_WRITE_BOTH ) {
539  $t = $this->tempTables[$key];
540  $func = __METHOD__;
541  $commentId = $comment->id;
542  $callback = function ( $id ) use ( $dbw, $commentId, $t, $func ) {
543  $dbw->insert(
544  $t['table'],
545  [
546  $t['pk'] => $id,
547  $t['field'] => $commentId,
548  ],
549  $func
550  );
551  };
552  }
553  if ( $tempTableStage >= MIGRATION_WRITE_BOTH ) {
554  $fields["{$key}_id"] = $comment->id;
555  }
556  }
557 
558  return [ $fields, $callback ];
559  }
560 
576  public function insert( IDatabase $dbw, $key, $comment = null, $data = null ) {
577  // Compat for method sig change in 1.31 (introduction of $key)
578  if ( $this->key !== null ) {
579  $data = $comment;
580  $comment = $key;
581  $key = $this->key;
582  }
583  if ( $comment === null ) {
584  // @codeCoverageIgnoreStart
585  throw new InvalidArgumentException( '$comment can not be null' );
586  // @codeCoverageIgnoreEnd
587  }
588 
589  $tempTableStage = isset( $this->tempTables[$key] )
590  ? $this->tempTables[$key]['stage'] : MIGRATION_NEW;
591  if ( $tempTableStage < MIGRATION_WRITE_NEW ) {
592  throw new InvalidArgumentException( "Must use insertWithTempTable() for $key" );
593  }
594 
595  list( $fields ) = $this->insertInternal( $dbw, $key, $comment, $data );
596  return $fields;
597  }
598 
620  public function insertWithTempTable( IDatabase $dbw, $key, $comment = null, $data = null ) {
621  // Compat for method sig change in 1.31 (introduction of $key)
622  if ( $this->key !== null ) {
623  $data = $comment;
624  $comment = $key;
625  $key = $this->getKey();
626  }
627  if ( $comment === null ) {
628  // @codeCoverageIgnoreStart
629  throw new InvalidArgumentException( '$comment can not be null' );
630  // @codeCoverageIgnoreEnd
631  }
632 
633  if ( !isset( $this->tempTables[$key] ) ) {
634  throw new InvalidArgumentException( "Must use insert() for $key" );
635  } elseif ( isset( $this->tempTables[$key]['deprecatedIn'] ) ) {
636  wfDeprecated( __METHOD__ . " for $key", $this->tempTables[$key]['deprecatedIn'] );
637  }
638 
639  list( $fields, $callback ) = $this->insertInternal( $dbw, $key, $comment, $data );
640  if ( !$callback ) {
641  $callback = function () {
642  // Do nothing.
643  };
644  }
645  return [ $fields, $callback ];
646  }
647 
653  protected static function encodeMessage( Message $msg ) {
654  $key = count( $msg->getKeysToTry() ) > 1 ? $msg->getKeysToTry() : $msg->getKey();
655  $params = $msg->getParams();
656  foreach ( $params as &$param ) {
657  if ( $param instanceof Message ) {
658  $param = [
659  'message' => self::encodeMessage( $param )
660  ];
661  }
662  }
663  array_unshift( $params, $key );
664  return $params;
665  }
666 
672  protected static function decodeMessage( $data ) {
673  $key = array_shift( $data );
674  foreach ( $data as &$param ) {
675  if ( is_object( $param ) ) {
676  $param = (array)$param;
677  }
678  if ( is_array( $param ) && count( $param ) === 1 && isset( $param['message'] ) ) {
679  $param = self::decodeMessage( $param['message'] );
680  }
681  }
682  return new Message( $key, $data );
683  }
684 
691  public static function hash( $text, $data ) {
692  $hash = crc32( $text ) ^ crc32( (string)$data );
693 
694  // 64-bit PHP returns an unsigned CRC, change it to signed for
695  // insertion into the database.
696  if ( $hash >= 0x80000000 ) {
697  $hash |= -1 << 32;
698  }
699 
700  return $hash;
701  }
702 
703 }
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))
getKeysToTry()
Definition: Message.php:329
getKey()
Returns the message key.
Definition: Message.php:344
int $wgCommentTableSchemaMigrationStage
Comment table schema migration stage.
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
static encodeMessage(Message $msg)
Encode a Message as a PHP data structure.
static newKey( $key)
Static constructor for easier chaining.
insertWithTempTable(IDatabase $dbw, $key, $comment=null, $data=null)
Insert a comment in a temporary table in preparation for a row that references it.
array string null $key
selectField( $table, $var, $cond='', $fname=__METHOD__, $options=[], $join_conds=[])
A SELECT wrapper which returns a single field from a single result row.
const MIGRATION_WRITE_NEW
Definition: Defines.php:317
Apache License January AND DISTRIBUTION Definitions License shall mean the terms and conditions for use
insert( $table, $a, $fname=__METHOD__, $options=[])
INSERT wrapper, inserts an array into a table.
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:25
getCommentInternal(IDatabase $db=null, $key, $row, $fallback=false)
Extract the comment from a row.
const ALL_OK
Skip escaping as many characters as reasonably possible.
Definition: FormatJson.php:55
static hash( $text, $data)
Hashing function for comment storage.
const MAX_DATA_LENGTH
Maximum length of serialized data in bytes.
const MIGRATION_WRITE_BOTH
Definition: Defines.php:316
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:2166
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 MediaWikiServices
Definition: injection.txt:23
static encode( $value, $pretty=false, $escaping=0)
Returns the JSON representation of a value.
Definition: FormatJson.php:115
wfLogWarning( $msg, $callerOffset=1, $level=E_USER_WARNING)
Send a warning as a PHP error and the debug log.
insertId()
Get the inserted value of an auto-increment row.
const COMMENT_CHARACTER_LIMIT
Maximum length of a comment in UTF-8 characters.
createComment(IDatabase $dbw, $comment, array $data=null)
Create a new CommentStoreComment, inserting it into the database if necessary.
static newUnsavedComment( $comment, array $data=null)
Create a new, unsaved CommentStoreComment.
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:990
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:1997
getParams()
Returns the message parameters.
Definition: Message.php:355
getFields( $key=null)
Get SELECT fields for the comment key.
getJoin( $key=null)
Get SELECT fields and joins for the comment key.
const MIGRATION_OLD
Definition: Defines.php:315
static decode( $value, $assoc=false)
Decodes a JSON string.
Definition: FormatJson.php:174
Language $lang
Language to use for comment truncation.
$params
const MAX_COMMENT_LENGTH
Maximum length of a comment in bytes.
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
Definition: distributors.txt:9
insert(IDatabase $dbw, $key, $comment=null, $data=null)
Insert a comment in preparation for a row that references it.
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:35
getCommentLegacy(IDatabase $db, $key, $row=null, $fallback=false)
Extract the comment from a row, with legacy lookups.
$fallback
Definition: MessagesAb.php:11
static getStore()
Basic database interface for live and lazy-loaded relation database handles.
Definition: IDatabase.php:38
Variant of the Message class.
Definition: RawMessage.php:34
wfDeprecated( $function, $version=false, $component=false, $callerOffset=2)
Throws a warning that $function is deprecated.
getComment( $key, $row=null, $fallback=false)
Extract the comment from a row.
array [] $joinCache
Cache for self::getJoin()
int $stage
One of the MIGRATION_* constants.
array $tempTables
Define fields that use temporary tables for transitional purposes Keys are &#39;$key&#39;, values are arrays with these possible fields:
getKey( $methodKey=null)
Compat method allowing use of self::newKey until removed.
__construct(Language $lang, $migrationStage)
const MIGRATION_NEW
Definition: Defines.php:318
static decodeMessage( $data)
Decode a message that was encoded by self::encodeMessage()
insertInternal(IDatabase $dbw, $key, $comment, $data)
Implementation for self::insert() and self::insertWithTempTable()