MediaWiki  master
EditResultCache.php
Go to the documentation of this file.
1 <?php
23 namespace MediaWiki\Storage;
24 
25 use BagOStuff;
26 use FormatJson;
29 
43 
44  public const CONSTRUCTOR_OPTIONS = [
45  'RCMaxAge'
46  ];
47 
48  private const CACHE_KEY_PREFIX = 'EditResult';
49 
52 
54  private $loadBalancer;
55 
57  private $options;
58 
65  public function __construct(
69  ) {
70  $options->assertRequiredOptions( self::CONSTRUCTOR_OPTIONS );
71 
72  $this->mainObjectStash = $mainObjectStash;
73  $this->loadBalancer = $loadBalancer;
74  $this->options = $options;
75  }
76 
85  public function set( int $revisionId, EditResult $editResult ): bool {
86  return $this->mainObjectStash->set(
87  $this->makeKey( $revisionId ),
88  FormatJson::encode( $editResult ),
89  // Patrol flags are not stored for longer than $wgRCMaxAge
90  $this->options->get( 'RCMaxAge' )
91  );
92  }
93 
104  public function get( int $revisionId ): ?EditResult {
105  $result = $this->mainObjectStash->get( $this->makeKey( $revisionId ) );
106 
107  // not found in stash, try change tags
108  if ( !$result ) {
109  $dbr = $this->loadBalancer->getConnection( DB_REPLICA );
110  $result = $dbr->selectField(
111  [ 'change_tag', 'change_tag_def' ],
112  'ct_params',
113  [
114  'ct_rev_id' => $revisionId,
115  'ctd_id = ct_tag_id',
116  'ctd_name' => [
117  'mw-rollback',
118  'mw-undo',
119  'mw-manual-revert'
120  ]
121  ],
122  __METHOD__
123  );
124  }
125 
126  if ( !$result ) {
127  return null;
128  }
129 
130  $decoded = FormatJson::decode( $result, true );
131  return $decoded ? EditResult::newFromArray( $decoded ) : null;
132  }
133 
141  private function makeKey( int $revisionId ): string {
142  return $this->mainObjectStash->makeKey( self::CACHE_KEY_PREFIX, $revisionId );
143  }
144 }
BagOStuff
Class representing a cache/ephemeral data store.
Definition: BagOStuff.php:86
$dbr
$dbr
Definition: testCompression.php:54
FormatJson\decode
static decode( $value, $assoc=false)
Decodes a JSON string.
Definition: FormatJson.php:146
FormatJson\encode
static encode( $value, $pretty=false, $escaping=0)
Returns the JSON representation of a value.
Definition: FormatJson.php:96
FormatJson
JSON formatter wrapper class.
Definition: FormatJson.php:26
MediaWiki\Config\ServiceOptions
A class for passing options to services.
Definition: ServiceOptions.php:27
MediaWiki\Storage\EditResultCache\$mainObjectStash
BagOStuff $mainObjectStash
Definition: EditResultCache.php:51
MediaWiki\Storage\EditResultCache\$loadBalancer
ILoadBalancer $loadBalancer
Definition: EditResultCache.php:54
DB_REPLICA
const DB_REPLICA
Definition: defines.php:25
MediaWiki\Storage\EditResultCache\__construct
__construct(BagOStuff $mainObjectStash, ILoadBalancer $loadBalancer, ServiceOptions $options)
Definition: EditResultCache.php:65
MediaWiki\Storage\EditResult\newFromArray
static newFromArray(array $a)
Recreate the EditResult object from its array representation.
Definition: EditResult.php:125
MediaWiki\Storage\EditResult
Object for storing information about the effects of an edit.
Definition: EditResult.php:38
MediaWiki\Storage\EditResultCache
Class allowing easy storage and retrieval of EditResults associated with revisions.
Definition: EditResultCache.php:42
MediaWiki\Storage\EditResultCache\CACHE_KEY_PREFIX
const CACHE_KEY_PREFIX
Definition: EditResultCache.php:48
MediaWiki\Storage
Definition: BlobAccessException.php:23
MediaWiki\Storage\EditResultCache\makeKey
makeKey(int $revisionId)
Generates a cache key for the given revision ID.
Definition: EditResultCache.php:141
MediaWiki\Storage\EditResultCache\CONSTRUCTOR_OPTIONS
const CONSTRUCTOR_OPTIONS
Definition: EditResultCache.php:44
MediaWiki\Storage\EditResultCache\$options
ServiceOptions $options
Definition: EditResultCache.php:57
Wikimedia\Rdbms\ILoadBalancer
Database cluster connection, tracking, load balancing, and transaction manager interface.
Definition: ILoadBalancer.php:81
MediaWiki\Config\ServiceOptions\assertRequiredOptions
assertRequiredOptions(array $expectedKeys)
Assert that the list of options provided in this instance exactly match $expectedKeys,...
Definition: ServiceOptions.php:71