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 $dbDomain DB domain of the relevant wiki or false for the current one.
62 CommentStoreComment
$comment,
67 parent
::__construct( $title, $user, $comment, $row, $slots, $dbDomain );
68 $this->mCallback
= $callback;
72 * Overridden to ensure that we return a fresh value and not a cached one.
76 public function getVisibility() {
77 if ( $this->mCallback
) {
78 $this->loadFreshRow();
80 return parent
::getVisibility();
84 * Overridden to ensure that we return a fresh value and not a cached one.
86 * @param int $audience
87 * @param User|null $user
89 * @return UserIdentity The identity of the revision author, null if access is forbidden.
91 public function getUser( $audience = self
::FOR_PUBLIC
, User
$user = null ) {
92 if ( $this->mCallback
) {
93 $this->loadFreshRow();
95 return parent
::getUser( $audience, $user );
99 * Load a fresh row from the database to ensure we return updated information
101 * @throws RevisionAccessException if the row could not be loaded
103 private function loadFreshRow() {
104 $freshRow = call_user_func( $this->mCallback
, $this->mId
);
106 // Set to null to ensure we do not make unnecessary queries for subsequent getter calls,
107 // and to allow the closure to be freed.
108 $this->mCallback
= null;
111 $this->mDeleted
= intval( $freshRow->rev_deleted
);
114 $this->mUser
= User
::newFromAnyId(
115 $freshRow->rev_user ??
null,
116 $freshRow->rev_user_text ??
null,
117 $freshRow->rev_actor ??
null
119 } catch ( InvalidArgumentException
$ex ) {
123 . $this->mTitle
->getPrefixedDBkey()
127 $this->mUser
= new UserIdentityValue( 0, 'Unknown user', 0 );
130 throw new RevisionAccessException(
131 'Unable to load fresh row for rev_id: ' . $this->mId