Merge "Add type hints for class properties in SpecialVersion"
[lhc/web/wiklou.git] / includes / revisiondelete / RevDelRevisionItem.php
1 <?php
2 /**
3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation; either version 2 of the License, or
6 * (at your option) any later version.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License along
14 * with this program; if not, write to the Free Software Foundation, Inc.,
15 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
16 * http://www.gnu.org/copyleft/gpl.html
17 *
18 * @file
19 * @ingroup RevisionDelete
20 */
21
22 use MediaWiki\Storage\RevisionRecord;
23
24 /**
25 * Item class for a live revision table row
26 */
27 class RevDelRevisionItem extends RevDelItem {
28 /** @var Revision */
29 public $revision;
30
31 public function __construct( $list, $row ) {
32 parent::__construct( $list, $row );
33 $this->revision = static::initRevision( $list, $row );
34 }
35
36 /**
37 * Create revision object from $row sourced from $list
38 *
39 * @param RevisionListBase $list
40 * @param mixed $row
41 * @return Revision
42 */
43 protected static function initRevision( $list, $row ) {
44 return new Revision( $row );
45 }
46
47 public function getIdField() {
48 return 'rev_id';
49 }
50
51 public function getTimestampField() {
52 return 'rev_timestamp';
53 }
54
55 public function getAuthorIdField() {
56 return 'rev_user';
57 }
58
59 public function getAuthorNameField() {
60 return 'rev_user_text';
61 }
62
63 public function getAuthorActorField() {
64 return 'rev_actor';
65 }
66
67 public function canView() {
68 return $this->revision->userCan(
69 RevisionRecord::DELETED_RESTRICTED, $this->list->getUser()
70 );
71 }
72
73 public function canViewContent() {
74 return $this->revision->userCan(
75 RevisionRecord::DELETED_TEXT, $this->list->getUser()
76 );
77 }
78
79 public function getBits() {
80 return $this->revision->getVisibility();
81 }
82
83 public function setBits( $bits ) {
84 $dbw = wfGetDB( DB_MASTER );
85 // Update revision table
86 $dbw->update( 'revision',
87 [ 'rev_deleted' => $bits ],
88 [
89 'rev_id' => $this->revision->getId(),
90 'rev_page' => $this->revision->getPage(),
91 'rev_deleted' => $this->getBits() // cas
92 ],
93 __METHOD__
94 );
95 if ( !$dbw->affectedRows() ) {
96 // Concurrent fail!
97 return false;
98 }
99 // Update recentchanges table
100 $dbw->update( 'recentchanges',
101 [
102 'rc_deleted' => $bits,
103 'rc_patrolled' => RecentChange::PRC_AUTOPATROLLED
104 ],
105 [
106 'rc_this_oldid' => $this->revision->getId(), // condition
107 // non-unique timestamp index
108 'rc_timestamp' => $dbw->timestamp( $this->revision->getTimestamp() ),
109 ],
110 __METHOD__
111 );
112
113 return true;
114 }
115
116 public function isDeleted() {
117 return $this->revision->isDeleted( RevisionRecord::DELETED_TEXT );
118 }
119
120 public function isHideCurrentOp( $newBits ) {
121 return ( $newBits & RevisionRecord::DELETED_TEXT )
122 && $this->list->getCurrent() == $this->getId();
123 }
124
125 /**
126 * Get the HTML link to the revision text.
127 * Overridden by RevDelArchiveItem.
128 * @return string
129 */
130 protected function getRevisionLink() {
131 $date = $this->list->getLanguage()->userTimeAndDate(
132 $this->revision->getTimestamp(), $this->list->getUser() );
133
134 if ( $this->isDeleted() && !$this->canViewContent() ) {
135 return htmlspecialchars( $date );
136 }
137
138 return $this->getLinkRenderer()->makeKnownLink(
139 $this->list->title,
140 $date,
141 [],
142 [
143 'oldid' => $this->revision->getId(),
144 'unhide' => 1
145 ]
146 );
147 }
148
149 /**
150 * Get the HTML link to the diff.
151 * Overridden by RevDelArchiveItem
152 * @return string
153 */
154 protected function getDiffLink() {
155 if ( $this->isDeleted() && !$this->canViewContent() ) {
156 return $this->list->msg( 'diff' )->escaped();
157 } else {
158 return $this->getLinkRenderer()->makeKnownLink(
159 $this->list->title,
160 $this->list->msg( 'diff' )->text(),
161 [],
162 [
163 'diff' => $this->revision->getId(),
164 'oldid' => 'prev',
165 'unhide' => 1
166 ]
167 );
168 }
169 }
170
171 /**
172 * @return string A HTML <li> element representing this revision, showing
173 * change tags and everything
174 */
175 public function getHTML() {
176 $difflink = $this->list->msg( 'parentheses' )
177 ->rawParams( $this->getDiffLink() )->escaped();
178 $revlink = $this->getRevisionLink();
179 $userlink = Linker::revUserLink( $this->revision );
180 $comment = Linker::revComment( $this->revision );
181 if ( $this->isDeleted() ) {
182 $revlink = "<span class=\"history-deleted\">$revlink</span>";
183 }
184 $content = "$difflink $revlink $userlink $comment";
185 $attribs = [];
186 $tags = $this->getTags();
187 if ( $tags ) {
188 list( $tagSummary, $classes ) = ChangeTags::formatSummaryRow(
189 $tags,
190 'revisiondelete',
191 $this->list->getContext()
192 );
193 $content .= " $tagSummary";
194 $attribs['class'] = implode( ' ', $classes );
195 }
196 return Xml::tags( 'li', $attribs, $content );
197 }
198
199 /**
200 * @return string Comma-separated list of tags
201 */
202 public function getTags() {
203 return $this->row->ts_tags;
204 }
205
206 public function getApiData( ApiResult $result ) {
207 $rev = $this->revision;
208 $user = $this->list->getUser();
209 $ret = [
210 'id' => $rev->getId(),
211 'timestamp' => wfTimestamp( TS_ISO_8601, $rev->getTimestamp() ),
212 'userhidden' => (bool)$rev->isDeleted( RevisionRecord::DELETED_USER ),
213 'commenthidden' => (bool)$rev->isDeleted( RevisionRecord::DELETED_COMMENT ),
214 'texthidden' => (bool)$rev->isDeleted( RevisionRecord::DELETED_TEXT ),
215 ];
216 if ( $rev->userCan( RevisionRecord::DELETED_USER, $user ) ) {
217 $ret += [
218 'userid' => $rev->getUser( RevisionRecord::FOR_THIS_USER ),
219 'user' => $rev->getUserText( RevisionRecord::FOR_THIS_USER ),
220 ];
221 }
222 if ( $rev->userCan( RevisionRecord::DELETED_COMMENT, $user ) ) {
223 $ret += [
224 'comment' => $rev->getComment( RevisionRecord::FOR_THIS_USER ),
225 ];
226 }
227
228 return $ret;
229 }
230 }