3 * A RevisionStoreRecord loaded from the cache.
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 namespace MediaWiki\Revision
;
25 use MediaWiki\User\UserIdentity
;
26 use MediaWiki\User\UserIdentityValue
;
27 use CommentStoreComment
;
28 use InvalidArgumentException
;
33 * A cached RevisionStoreRecord. Ensures that changes performed "behind the back"
34 * of the cache do not cause the revision record to deliver stale data.
38 class RevisionStoreCacheRecord
extends RevisionStoreRecord
{
46 * @note Avoid calling this constructor directly. Use the appropriate methods
47 * in RevisionStore instead.
49 * @param callable $callback Callback for loading data. Signature: function ( $id ): object
50 * @param Title $title The title of the page this Revision is associated with.
51 * @param UserIdentity $user
52 * @param CommentStoreComment $comment
53 * @param object $row A row from the revision table. Use RevisionStore::getQueryInfo() to build
54 * a query that yields the required fields.
55 * @param RevisionSlots $slots The slots of this revision.
56 * @param bool|string $wikiId the wiki ID of the site this Revision belongs to,
57 * or false for the local site.
63 CommentStoreComment
$comment,
68 parent
::__construct( $title, $user, $comment, $row, $slots, $wikiId );
69 $this->mCallback
= $callback;
73 * Overridden to ensure that we return a fresh value and not a cached one.
77 public function getVisibility() {
78 if ( $this->mCallback
) {
79 $this->loadFreshRow();
81 return parent
::getVisibility();
85 * Overridden to ensure that we return a fresh value and not a cached one.
87 * @param int $audience
88 * @param User|null $user
90 * @return UserIdentity The identity of the revision author, null if access is forbidden.
92 public function getUser( $audience = self
::FOR_PUBLIC
, User
$user = null ) {
93 if ( $this->mCallback
) {
94 $this->loadFreshRow();
96 return parent
::getUser( $audience, $user );
100 * Load a fresh row from the database to ensure we return updated information
102 * @throws RevisionAccessException if the row could not be loaded
104 private function loadFreshRow() {
105 $freshRow = call_user_func( $this->mCallback
, $this->mId
);
107 // Set to null to ensure we do not make unnecessary queries for subsequent getter calls,
108 // and to allow the closure to be freed.
109 $this->mCallback
= null;
112 $this->mDeleted
= intval( $freshRow->rev_deleted
);
115 $this->mUser
= User
::newFromAnyId(
116 $freshRow->rev_user ??
null,
117 $freshRow->rev_user_text ??
null,
118 $freshRow->rev_actor ??
null
120 } catch ( InvalidArgumentException
$ex ) {
124 . $this->mTitle
->getPrefixedDBkey()
128 $this->mUser
= new UserIdentityValue( 0, 'Unknown user', 0 );
131 throw new RevisionAccessException(
132 'Unable to load fresh row for rev_id: ' . $this->mId