Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
36 / 36
100.00% covered (success)
100.00%
11 / 11
CRAP
100.00% covered (success)
100.00%
1 / 1
RevisionArchiveRecord
100.00% covered (success)
100.00%
36 / 36
100.00% covered (success)
100.00%
11 / 11
24
100.00% covered (success)
100.00%
1 / 1
 __construct
100.00% covered (success)
100.00%
14 / 14
100.00% covered (success)
100.00%
1 / 1
6
 getArchiveId
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getId
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getSize
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 getSha1
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 getUser
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getComment
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getTimestamp
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 userCan
100.00% covered (success)
100.00%
8 / 8
100.00% covered (success)
100.00%
1 / 1
6
 audienceCan
100.00% covered (success)
100.00%
4 / 4
100.00% covered (success)
100.00%
1 / 1
4
 isReadyForInsertion
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
1<?php
2/**
3 * A RevisionRecord representing a revision of a deleted page persisted in the archive table.
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
23namespace MediaWiki\Revision;
24
25use MediaWiki\CommentStore\CommentStoreComment;
26use MediaWiki\Page\PageIdentity;
27use MediaWiki\Permissions\Authority;
28use MediaWiki\User\UserIdentity;
29use MediaWiki\Utils\MWTimestamp;
30use stdClass;
31use Wikimedia\Assert\Assert;
32
33/**
34 * A RevisionRecord representing a revision of a deleted page persisted in the archive table.
35 * Most getters on RevisionArchiveRecord will never return null. However, getId() and
36 * getParentId() may indeed return null if this information was not stored when the archive entry
37 * was created.
38 *
39 * @since 1.31
40 * @since 1.32 Renamed from MediaWiki\Storage\RevisionArchiveRecord
41 */
42class RevisionArchiveRecord extends RevisionRecord {
43
44    /**
45     * @var int
46     */
47    protected $mArchiveId;
48
49    /**
50     * @note Avoid calling this constructor directly. Use the appropriate methods
51     * in RevisionStore instead.
52     *
53     * @param PageIdentity $page The page this RevisionRecord is associated with.
54     * @param UserIdentity $user
55     * @param CommentStoreComment $comment
56     * @param stdClass $row An archive table row. Use RevisionStore::getArchiveQueryInfo() to build
57     *        a query that yields the required fields.
58     * @param RevisionSlots $slots The slots of this revision.
59     * @param false|string $wikiId Relevant wiki or self::LOCAL for the current one.
60     */
61    public function __construct(
62        PageIdentity $page,
63        UserIdentity $user,
64        CommentStoreComment $comment,
65        stdClass $row,
66        RevisionSlots $slots,
67        $wikiId = self::LOCAL
68    ) {
69        parent::__construct( $page, $slots, $wikiId );
70
71        $timestamp = MWTimestamp::convert( TS_MW, $row->ar_timestamp );
72        Assert::parameter( is_string( $timestamp ), '$row->rev_timestamp', 'must be a valid timestamp' );
73
74        $this->mArchiveId = intval( $row->ar_id );
75
76        // NOTE: ar_page_id may be different from $this->mPage->getId() in some cases,
77        // notably when a partially restored page has been moved, and a new page has been created
78        // with the same title. Archive rows for that title will then have the wrong page id.
79        $this->mPageId = isset( $row->ar_page_id ) ? intval( $row->ar_page_id ) : $this->getArticleId( $this->mPage );
80
81        // NOTE: ar_parent_id = 0 indicates that there is no parent revision, while null
82        // indicates that the parent revision is unknown. As per MW 1.31, the database schema
83        // allows ar_parent_id to be NULL.
84        $this->mParentId = isset( $row->ar_parent_id ) ? intval( $row->ar_parent_id ) : null;
85        $this->mId = isset( $row->ar_rev_id ) ? intval( $row->ar_rev_id ) : null;
86        $this->mComment = $comment;
87        $this->mUser = $user;
88        $this->mTimestamp = $timestamp;
89        $this->mMinorEdit = boolval( $row->ar_minor_edit );
90        $this->mDeleted = intval( $row->ar_deleted );
91        $this->mSize = isset( $row->ar_len ) ? intval( $row->ar_len ) : null;
92        $this->mSha1 = !empty( $row->ar_sha1 ) ? $row->ar_sha1 : null;
93    }
94
95    /**
96     * Get archive row ID
97     *
98     * @return int
99     */
100    public function getArchiveId() {
101        return $this->mArchiveId;
102    }
103
104    /**
105     * @param string|false $wikiId The wiki ID expected by the caller.
106     * @return int|null The revision id, or null if the original revision ID
107     *         was not recorded in the archive table.
108     */
109    public function getId( $wikiId = self::LOCAL ) {
110        // overwritten just to refine the contract specification.
111        return parent::getId( $wikiId );
112    }
113
114    /**
115     * @throws RevisionAccessException if the size was unknown and could not be calculated.
116     * @return int The nominal revision size, never null. May be computed on the fly.
117     */
118    public function getSize() {
119        // If length is null, calculate and remember it (potentially SLOW!).
120        // This is for compatibility with old database rows that don't have the field set.
121        $this->mSize ??= $this->mSlots->computeSize();
122
123        return $this->mSize;
124    }
125
126    /**
127     * @throws RevisionAccessException if the hash was unknown and could not be calculated.
128     * @return string The revision hash, never null. May be computed on the fly.
129     */
130    public function getSha1() {
131        // If hash is null, calculate it and remember (potentially SLOW!)
132        // This is for compatibility with old database rows that don't have the field set.
133        $this->mSha1 ??= $this->mSlots->computeSha1();
134
135        return $this->mSha1;
136    }
137
138    /**
139     * @param int $audience
140     * @param Authority|null $performer
141     *
142     * @return UserIdentity The identity of the revision author, null if access is forbidden.
143     */
144    public function getUser( $audience = self::FOR_PUBLIC, Authority $performer = null ) {
145        // overwritten just to add a guarantee to the contract
146        return parent::getUser( $audience, $performer );
147    }
148
149    /**
150     * @param int $audience
151     * @param Authority|null $performer
152     *
153     * @return CommentStoreComment The revision comment, null if access is forbidden.
154     */
155    public function getComment( $audience = self::FOR_PUBLIC, Authority $performer = null ) {
156        // overwritten just to add a guarantee to the contract
157        return parent::getComment( $audience, $performer );
158    }
159
160    /**
161     * @return string never null
162     */
163    public function getTimestamp() {
164        // overwritten just to add a guarantee to the contract
165        return parent::getTimestamp();
166    }
167
168    public function userCan( $field, Authority $performer ) {
169        // This revision belongs to a deleted page, so check the relevant permissions as well. (T345777)
170
171        // Viewing the content requires either 'deletedtext' or 'undelete' (for legacy reasons)
172        if (
173            $field === self::DELETED_TEXT &&
174            !$performer->authorizeRead( 'deletedtext', $this->getPage() ) &&
175            !$performer->authorizeRead( 'undelete', $this->getPage() )
176        ) {
177            return false;
178        }
179
180        // Viewing the edit summary requires 'deletedhistory'
181        if (
182            $field === self::DELETED_COMMENT &&
183            !$performer->authorizeRead( 'deletedhistory', $this->getPage() )
184        ) {
185            return false;
186        }
187
188        // Other fields of revisions of deleted pages are public, per T232389 (unless revision-deleted)
189
190        return parent::userCan( $field, $performer );
191    }
192
193    public function audienceCan( $field, $audience, Authority $performer = null ) {
194        // This revision belongs to a deleted page, so check the relevant permissions as well. (T345777)
195        // See userCan().
196        if (
197            $audience == self::FOR_PUBLIC &&
198            ( $field === self::DELETED_TEXT || $field === self::DELETED_COMMENT )
199        ) {
200            // TODO: Should this use PermissionManager::isEveryoneAllowed() or something?
201            // But RevisionRecord::audienceCan() doesn't do that either…
202            return false;
203        }
204
205        // This calls userCan(), which checks the user's permissions
206        return parent::audienceCan( $field, $audience, $performer );
207    }
208
209    /**
210     * @see RevisionStore::isComplete
211     *
212     * @return bool always true.
213     */
214    public function isReadyForInsertion() {
215        return true;
216    }
217
218}