Merge "Improve ApiLogin test coverage"
[lhc/web/wiklou.git] / includes / Revision / RevisionArchiveRecord.php
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
23 namespace MediaWiki\Revision;
24
25 use CommentStoreComment;
26 use MediaWiki\User\UserIdentity;
27 use Title;
28 use User;
29 use Wikimedia\Assert\Assert;
30
31 /**
32 * A RevisionRecord representing a revision of a deleted page persisted in the archive table.
33 * Most getters on RevisionArchiveRecord will never return null. However, getId() and
34 * getParentId() may indeed return null if this information was not stored when the archive entry
35 * was created.
36 *
37 * @since 1.31
38 * @since 1.32 Renamed from MediaWiki\Storage\RevisionArchiveRecord
39 */
40 class RevisionArchiveRecord extends RevisionRecord {
41
42 /**
43 * @var int
44 */
45 protected $mArchiveId;
46
47 /**
48 * @note Avoid calling this constructor directly. Use the appropriate methods
49 * in RevisionStore instead.
50 *
51 * @param Title $title The title of the page this Revision is associated with.
52 * @param UserIdentity $user
53 * @param CommentStoreComment $comment
54 * @param object $row An archive table row. Use RevisionStore::getArchiveQueryInfo() to build
55 * a query that yields the required fields.
56 * @param RevisionSlots $slots The slots of this revision.
57 * @param bool|string $wikiId the wiki ID of the site this Revision belongs to,
58 * or false for the local site.
59 */
60 function __construct(
61 Title $title,
62 UserIdentity $user,
63 CommentStoreComment $comment,
64 $row,
65 RevisionSlots $slots,
66 $wikiId = false
67 ) {
68 parent::__construct( $title, $slots, $wikiId );
69 Assert::parameterType( 'object', $row, '$row' );
70
71 $timestamp = wfTimestamp( 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->mTitle->getArticleID() 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 ) : $title->getArticleID();
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 * @return int|null The revision id, or null if the original revision ID
106 * was not recorded in the archive table.
107 */
108 public function getId() {
109 // overwritten just to refine the contract specification.
110 return parent::getId();
111 }
112
113 /**
114 * @throws RevisionAccessException if the size was unknown and could not be calculated.
115 * @return int The nominal revision size, never null. May be computed on the fly.
116 */
117 public function getSize() {
118 // If length is null, calculate and remember it (potentially SLOW!).
119 // This is for compatibility with old database rows that don't have the field set.
120 if ( $this->mSize === null ) {
121 $this->mSize = $this->mSlots->computeSize();
122 }
123
124 return $this->mSize;
125 }
126
127 /**
128 * @throws RevisionAccessException if the hash was unknown and could not be calculated.
129 * @return string The revision hash, never null. May be computed on the fly.
130 */
131 public function getSha1() {
132 // If hash is null, calculate it and remember (potentially SLOW!)
133 // This is for compatibility with old database rows that don't have the field set.
134 if ( $this->mSha1 === null ) {
135 $this->mSha1 = $this->mSlots->computeSha1();
136 }
137
138 return $this->mSha1;
139 }
140
141 /**
142 * @param int $audience
143 * @param User|null $user
144 *
145 * @return UserIdentity The identity of the revision author, null if access is forbidden.
146 */
147 public function getUser( $audience = self::FOR_PUBLIC, User $user = null ) {
148 // overwritten just to add a guarantee to the contract
149 return parent::getUser( $audience, $user );
150 }
151
152 /**
153 * @param int $audience
154 * @param User|null $user
155 *
156 * @return CommentStoreComment The revision comment, null if access is forbidden.
157 */
158 public function getComment( $audience = self::FOR_PUBLIC, User $user = null ) {
159 // overwritten just to add a guarantee to the contract
160 return parent::getComment( $audience, $user );
161 }
162
163 /**
164 * @return string never null
165 */
166 public function getTimestamp() {
167 // overwritten just to add a guarantee to the contract
168 return parent::getTimestamp();
169 }
170
171 /**
172 * @see RevisionStore::isComplete
173 *
174 * @return bool always true.
175 */
176 public function isReadyForInsertion() {
177 return true;
178 }
179
180 }
181
182 /**
183 * Retain the old class name for backwards compatibility.
184 * @deprecated since 1.32
185 */
186 class_alias( RevisionArchiveRecord::class, 'MediaWiki\Storage\RevisionArchiveRecord' );