Merge "StringUtils: Add a utility for checking if a string is a valid regex"
[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\Revision\RevisionRecord;
23
24 /**
25 * Item class for a live revision table row
26 *
27 * @property RevDelRevisionList $list
28 */
29 class RevDelRevisionItem extends RevDelItem {
30 /** @var Revision */
31 public $revision;
32
33 public function __construct( $list, $row ) {
34 parent::__construct( $list, $row );
35 $this->revision = static::initRevision( $list, $row );
36 }
37
38 /**
39 * Create revision object from $row sourced from $list
40 *
41 * @param RevisionListBase $list
42 * @param mixed $row
43 * @return Revision
44 */
45 protected static function initRevision( $list, $row ) {
46 return new Revision( $row );
47 }
48
49 public function getIdField() {
50 return 'rev_id';
51 }
52
53 public function getTimestampField() {
54 return 'rev_timestamp';
55 }
56
57 public function getAuthorIdField() {
58 return 'rev_user';
59 }
60
61 public function getAuthorNameField() {
62 return 'rev_user_text';
63 }
64
65 public function getAuthorActorField() {
66 return 'rev_actor';
67 }
68
69 public function canView() {
70 return $this->revision->userCan(
71 RevisionRecord::DELETED_RESTRICTED, $this->list->getUser()
72 );
73 }
74
75 public function canViewContent() {
76 return $this->revision->userCan(
77 RevisionRecord::DELETED_TEXT, $this->list->getUser()
78 );
79 }
80
81 public function getBits() {
82 return $this->revision->getVisibility();
83 }
84
85 public function setBits( $bits ) {
86 $dbw = wfGetDB( DB_MASTER );
87 // Update revision table
88 $dbw->update( 'revision',
89 [ 'rev_deleted' => $bits ],
90 [
91 'rev_id' => $this->revision->getId(),
92 'rev_page' => $this->revision->getPage(),
93 'rev_deleted' => $this->getBits() // cas
94 ],
95 __METHOD__
96 );
97 if ( !$dbw->affectedRows() ) {
98 // Concurrent fail!
99 return false;
100 }
101 // Update recentchanges table
102 $dbw->update( 'recentchanges',
103 [
104 'rc_deleted' => $bits,
105 'rc_patrolled' => RecentChange::PRC_AUTOPATROLLED
106 ],
107 [
108 'rc_this_oldid' => $this->revision->getId(), // condition
109 // non-unique timestamp index
110 'rc_timestamp' => $dbw->timestamp( $this->revision->getTimestamp() ),
111 ],
112 __METHOD__
113 );
114
115 return true;
116 }
117
118 public function isDeleted() {
119 return $this->revision->isDeleted( RevisionRecord::DELETED_TEXT );
120 }
121
122 public function isHideCurrentOp( $newBits ) {
123 return ( $newBits & RevisionRecord::DELETED_TEXT )
124 && $this->list->getCurrent() == $this->getId();
125 }
126
127 /**
128 * Get the HTML link to the revision text.
129 * Overridden by RevDelArchiveItem.
130 * @return string
131 */
132 protected function getRevisionLink() {
133 $date = $this->list->getLanguage()->userTimeAndDate(
134 $this->revision->getTimestamp(), $this->list->getUser() );
135
136 if ( $this->isDeleted() && !$this->canViewContent() ) {
137 return htmlspecialchars( $date );
138 }
139
140 return $this->getLinkRenderer()->makeKnownLink(
141 $this->list->title,
142 $date,
143 [],
144 [
145 'oldid' => $this->revision->getId(),
146 'unhide' => 1
147 ]
148 );
149 }
150
151 /**
152 * Get the HTML link to the diff.
153 * Overridden by RevDelArchiveItem
154 * @return string
155 */
156 protected function getDiffLink() {
157 if ( $this->isDeleted() && !$this->canViewContent() ) {
158 return $this->list->msg( 'diff' )->escaped();
159 } else {
160 return $this->getLinkRenderer()->makeKnownLink(
161 $this->list->title,
162 $this->list->msg( 'diff' )->text(),
163 [],
164 [
165 'diff' => $this->revision->getId(),
166 'oldid' => 'prev',
167 'unhide' => 1
168 ]
169 );
170 }
171 }
172
173 /**
174 * @return string A HTML <li> element representing this revision, showing
175 * change tags and everything
176 */
177 public function getHTML() {
178 $difflink = $this->list->msg( 'parentheses' )
179 ->rawParams( $this->getDiffLink() )->escaped();
180 $revlink = $this->getRevisionLink();
181 $userlink = Linker::revUserLink( $this->revision );
182 $comment = Linker::revComment( $this->revision );
183 if ( $this->isDeleted() ) {
184 $revlink = "<span class=\"history-deleted\">$revlink</span>";
185 }
186 $content = "$difflink $revlink $userlink $comment";
187 $attribs = [];
188 $tags = $this->getTags();
189 if ( $tags ) {
190 list( $tagSummary, $classes ) = ChangeTags::formatSummaryRow(
191 $tags,
192 'revisiondelete',
193 $this->list->getContext()
194 );
195 $content .= " $tagSummary";
196 $attribs['class'] = implode( ' ', $classes );
197 }
198 return Xml::tags( 'li', $attribs, $content );
199 }
200
201 /**
202 * @return string Comma-separated list of tags
203 */
204 public function getTags() {
205 return $this->row->ts_tags;
206 }
207
208 public function getApiData( ApiResult $result ) {
209 $rev = $this->revision;
210 $user = $this->list->getUser();
211 $ret = [
212 'id' => $rev->getId(),
213 'timestamp' => wfTimestamp( TS_ISO_8601, $rev->getTimestamp() ),
214 'userhidden' => (bool)$rev->isDeleted( RevisionRecord::DELETED_USER ),
215 'commenthidden' => (bool)$rev->isDeleted( RevisionRecord::DELETED_COMMENT ),
216 'texthidden' => (bool)$rev->isDeleted( RevisionRecord::DELETED_TEXT ),
217 ];
218 if ( $rev->userCan( RevisionRecord::DELETED_USER, $user ) ) {
219 $ret += [
220 'userid' => $rev->getUser( RevisionRecord::FOR_THIS_USER ),
221 'user' => $rev->getUserText( RevisionRecord::FOR_THIS_USER ),
222 ];
223 }
224 if ( $rev->userCan( RevisionRecord::DELETED_COMMENT, $user ) ) {
225 $ret += [
226 'comment' => $rev->getComment( RevisionRecord::FOR_THIS_USER ),
227 ];
228 }
229
230 return $ret;
231 }
232 }