Migrate remaining usages of Title::userCan() to PermissionManager
[lhc/web/wiklou.git] / includes / Revision / RevisionStoreCacheRecord.php
1 <?php
2 /**
3 * A RevisionStoreRecord loaded from the cache.
4 *
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.
9 *
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.
14 *
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
19 *
20 * @file
21 */
22
23 namespace MediaWiki\Revision;
24
25 use MediaWiki\User\UserIdentity;
26 use MediaWiki\User\UserIdentityValue;
27 use CommentStoreComment;
28 use InvalidArgumentException;
29 use Title;
30 use User;
31
32 /**
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.
35 *
36 * @since 1.33
37 */
38 class RevisionStoreCacheRecord extends RevisionStoreRecord {
39
40 /**
41 * @var callable
42 */
43 private $mCallback;
44
45 /**
46 * @note Avoid calling this constructor directly. Use the appropriate methods
47 * in RevisionStore instead.
48 *
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.
58 */
59 function __construct(
60 $callback,
61 Title $title,
62 UserIdentity $user,
63 CommentStoreComment $comment,
64 $row,
65 RevisionSlots $slots,
66 $wikiId = false
67 ) {
68 parent::__construct( $title, $user, $comment, $row, $slots, $wikiId );
69 $this->mCallback = $callback;
70 }
71
72 /**
73 * Overridden to ensure that we return a fresh value and not a cached one.
74 *
75 * @return int
76 */
77 public function getVisibility() {
78 if ( $this->mCallback ) {
79 $this->loadFreshRow();
80 }
81 return parent::getVisibility();
82 }
83
84 /**
85 * Overridden to ensure that we return a fresh value and not a cached one.
86 *
87 * @param int $audience
88 * @param User|null $user
89 *
90 * @return UserIdentity The identity of the revision author, null if access is forbidden.
91 */
92 public function getUser( $audience = self::FOR_PUBLIC, User $user = null ) {
93 if ( $this->mCallback ) {
94 $this->loadFreshRow();
95 }
96 return parent::getUser( $audience, $user );
97 }
98
99 /**
100 * Load a fresh row from the database to ensure we return updated information
101
102 * @throws RevisionAccessException if the row could not be loaded
103 */
104 private function loadFreshRow() {
105 $freshRow = call_user_func( $this->mCallback, $this->mId );
106
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;
110
111 if ( $freshRow ) {
112 $this->mDeleted = intval( $freshRow->rev_deleted );
113
114 try {
115 $this->mUser = User::newFromAnyId(
116 $freshRow->rev_user ?? null,
117 $freshRow->rev_user_text ?? null,
118 $freshRow->rev_actor ?? null
119 );
120 } catch ( InvalidArgumentException $ex ) {
121 wfWarn(
122 __METHOD__
123 . ': '
124 . $this->mTitle->getPrefixedDBkey()
125 . ': '
126 . $ex->getMessage()
127 );
128 $this->mUser = new UserIdentityValue( 0, 'Unknown user', 0 );
129 }
130 } else {
131 throw new RevisionAccessException(
132 'Unable to load fresh row for rev_id: ' . $this->mId
133 );
134 }
135 }
136
137 }