Merge "Use {{int:}} on MediaWiki:Blockedtext and MediaWiki:Autoblockedtext"
[lhc/web/wiklou.git] / includes / Storage / 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\Storage;
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 */
39 class RevisionArchiveRecord extends RevisionRecord {
40
41 /**
42 * @var int
43 */
44 protected $mArchiveId;
45
46 /**
47 * @note Avoid calling this constructor directly. Use the appropriate methods
48 * in RevisionStore instead.
49 *
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 An archive table row. Use RevisionStore::getArchiveQueryInfo() 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 Title $title,
61 UserIdentity $user,
62 CommentStoreComment $comment,
63 $row,
64 RevisionSlots $slots,
65 $wikiId = false
66 ) {
67 parent::__construct( $title, $slots, $wikiId );
68 Assert::parameterType( 'object', $row, '$row' );
69
70 $timestamp = wfTimestamp( TS_MW, $row->ar_timestamp );
71 Assert::parameter( is_string( $timestamp ), '$row->rev_timestamp', 'must be a valid timestamp' );
72
73 $this->mArchiveId = intval( $row->ar_id );
74
75 // NOTE: ar_page_id may be different from $this->mTitle->getArticleID() in some cases,
76 // notably when a partially restored page has been moved, and a new page has been created
77 // with the same title. Archive rows for that title will then have the wrong page id.
78 $this->mPageId = isset( $row->ar_page_id ) ? intval( $row->ar_page_id ) : $title->getArticleID();
79
80 // NOTE: ar_parent_id = 0 indicates that there is no parent revision, while null
81 // indicates that the parent revision is unknown. As per MW 1.31, the database schema
82 // allows ar_parent_id to be NULL.
83 $this->mParentId = isset( $row->ar_parent_id ) ? intval( $row->ar_parent_id ) : null;
84 $this->mId = isset( $row->ar_rev_id ) ? intval( $row->ar_rev_id ) : null;
85 $this->mComment = $comment;
86 $this->mUser = $user;
87 $this->mTimestamp = $timestamp;
88 $this->mMinorEdit = boolval( $row->ar_minor_edit );
89 $this->mDeleted = intval( $row->ar_deleted );
90 $this->mSize = isset( $row->ar_len ) ? intval( $row->ar_len ) : null;
91 $this->mSha1 = !empty( $row->ar_sha1 ) ? $row->ar_sha1 : null;
92 }
93
94 /**
95 * Get archive row ID
96 *
97 * @return int
98 */
99 public function getArchiveId() {
100 return $this->mArchiveId;
101 }
102
103 /**
104 * @return int|null The revision id, or null if the original revision ID
105 * was not recorded in the archive table.
106 */
107 public function getId() {
108 // overwritten just to refine the contract specification.
109 return parent::getId();
110 }
111
112 /**
113 * @throws RevisionAccessException if the size was unknown and could not be calculated.
114 * @return int The nominal revision size, never null. May be computed on the fly.
115 */
116 public function getSize() {
117 // If length is null, calculate and remember it (potentially SLOW!).
118 // This is for compatibility with old database rows that don't have the field set.
119 if ( $this->mSize === null ) {
120 $this->mSize = $this->mSlots->computeSize();
121 }
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 if ( $this->mSha1 === null ) {
134 $this->mSha1 = $this->mSlots->computeSha1();
135 }
136
137 return $this->mSha1;
138 }
139
140 /**
141 * @param int $audience
142 * @param User|null $user
143 *
144 * @return UserIdentity The identity of the revision author, null if access is forbidden.
145 */
146 public function getUser( $audience = self::FOR_PUBLIC, User $user = null ) {
147 // overwritten just to add a guarantee to the contract
148 return parent::getUser( $audience, $user );
149 }
150
151 /**
152 * @param int $audience
153 * @param User|null $user
154 *
155 * @return CommentStoreComment The revision comment, null if access is forbidden.
156 */
157 public function getComment( $audience = self::FOR_PUBLIC, User $user = null ) {
158 // overwritten just to add a guarantee to the contract
159 return parent::getComment( $audience, $user );
160 }
161
162 /**
163 * @return string never null
164 */
165 public function getTimestamp() {
166 // overwritten just to add a guarantee to the contract
167 return parent::getTimestamp();
168 }
169
170 }