Merge "Simplify code to avoid interpreting "$" characters in string replacement"
[mediawiki.git] / includes / revisiondelete / RevDelRevisionItem.php
blobf60e0bcb2e7fd97ff89403a4f53ee467e947907f
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.
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.
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
18 * @file
19 * @ingroup RevisionDelete
22 use MediaWiki\Linker\Linker;
23 use MediaWiki\MediaWikiServices;
24 use MediaWiki\Revision\RevisionRecord;
25 use MediaWiki\RevisionList\RevisionListBase;
26 use MediaWiki\Xml\Xml;
28 /**
29 * Item class for a live revision table row
31 * @property RevDelRevisionList $list
33 class RevDelRevisionItem extends RevDelItem {
34 /** @var RevisionRecord */
35 public $revisionRecord;
37 public function __construct( RevisionListBase $list, $row ) {
38 parent::__construct( $list, $row );
39 $this->revisionRecord = static::initRevisionRecord( $list, $row );
42 /**
43 * Create RevisionRecord object from $row sourced from $list
45 * @param RevisionListBase $list
46 * @param mixed $row
47 * @return RevisionRecord
49 protected static function initRevisionRecord( $list, $row ) {
50 return MediaWikiServices::getInstance()
51 ->getRevisionFactory()
52 ->newRevisionFromRow( $row );
55 /**
56 * Get the RevisionRecord for the item
58 * @return RevisionRecord
60 protected function getRevisionRecord(): RevisionRecord {
61 return $this->revisionRecord;
64 public function getIdField() {
65 return 'rev_id';
68 public function getTimestampField() {
69 return 'rev_timestamp';
72 public function getAuthorIdField() {
73 return 'rev_user';
76 public function getAuthorNameField() {
77 return 'rev_user_text';
80 public function getAuthorActorField() {
81 return 'rev_actor';
84 public function canView() {
85 return $this->getRevisionRecord()->userCan(
86 RevisionRecord::DELETED_RESTRICTED,
87 $this->list->getAuthority()
91 public function canViewContent() {
92 return $this->getRevisionRecord()->userCan(
93 RevisionRecord::DELETED_TEXT,
94 $this->list->getAuthority()
98 public function getBits() {
99 return $this->getRevisionRecord()->getVisibility();
102 public function setBits( $bits ) {
103 $revRecord = $this->getRevisionRecord();
105 $dbw = MediaWikiServices::getInstance()->getConnectionProvider()->getPrimaryDatabase();
106 // Update revision table
107 $dbw->newUpdateQueryBuilder()
108 ->update( 'revision' )
109 ->set( [ 'rev_deleted' => $bits ] )
110 ->where( [
111 'rev_id' => $revRecord->getId(),
112 'rev_page' => $revRecord->getPageId(),
113 'rev_deleted' => $this->getBits() // cas
115 ->caller( __METHOD__ )->execute();
117 if ( !$dbw->affectedRows() ) {
118 // Concurrent fail!
119 return false;
121 // Update recentchanges table
122 $dbw->newUpdateQueryBuilder()
123 ->update( 'recentchanges' )
124 ->set( [
125 'rc_deleted' => $bits,
126 'rc_patrolled' => RecentChange::PRC_AUTOPATROLLED
128 ->where( [ 'rc_this_oldid' => $revRecord->getId() ] )
129 ->caller( __METHOD__ )->execute();
131 return true;
134 public function isDeleted() {
135 return $this->getRevisionRecord()->isDeleted( RevisionRecord::DELETED_TEXT );
138 public function isHideCurrentOp( $newBits ) {
139 return ( $newBits & RevisionRecord::DELETED_TEXT )
140 && $this->list->getCurrent() == $this->getId();
144 * Get the HTML link to the revision text.
145 * Overridden by RevDelArchiveItem.
146 * @return string
148 protected function getRevisionLink() {
149 $date = $this->list->getLanguage()->userTimeAndDate(
150 $this->getRevisionRecord()->getTimestamp(),
151 $this->list->getUser()
154 if ( $this->isDeleted() && !$this->canViewContent() ) {
155 return htmlspecialchars( $date );
158 return $this->getLinkRenderer()->makeKnownLink(
159 $this->list->getPage(),
160 $date,
163 'oldid' => $this->getRevisionRecord()->getId(),
164 'unhide' => 1
170 * Get the HTML link to the diff.
171 * Overridden by RevDelArchiveItem
172 * @return string
174 protected function getDiffLink() {
175 if ( $this->isDeleted() && !$this->canViewContent() ) {
176 return $this->list->msg( 'diff' )->escaped();
177 } else {
178 return $this->getLinkRenderer()->makeKnownLink(
179 $this->list->getPage(),
180 $this->list->msg( 'diff' )->text(),
183 'diff' => $this->getRevisionRecord()->getId(),
184 'oldid' => 'prev',
185 'unhide' => 1
192 * @return string A HTML <li> element representing this revision, showing
193 * change tags and everything
195 public function getHTML() {
196 $revRecord = $this->getRevisionRecord();
198 $difflink = $this->list->msg( 'parentheses' )
199 ->rawParams( $this->getDiffLink() )->escaped();
200 $revlink = $this->getRevisionLink();
201 $userlink = Linker::revUserLink( $revRecord );
202 $comment = MediaWikiServices::getInstance()->getCommentFormatter()
203 ->formatRevision( $revRecord, $this->list->getAuthority() );
204 if ( $this->isDeleted() ) {
205 $class = Linker::getRevisionDeletedClass( $revRecord );
206 $revlink = "<span class=\"$class\">$revlink</span>";
208 $content = "$difflink $revlink $userlink $comment";
209 $attribs = [];
210 $tags = $this->getTags();
211 if ( $tags ) {
212 [ $tagSummary, $classes ] = ChangeTags::formatSummaryRow(
213 $tags,
214 'revisiondelete',
215 $this->list->getContext()
217 $content .= " $tagSummary";
218 $attribs['class'] = implode( ' ', $classes );
220 return Xml::tags( 'li', $attribs, $content );
224 * @return string Comma-separated list of tags
226 public function getTags() {
227 return $this->row->ts_tags;
230 public function getApiData( ApiResult $result ) {
231 $revRecord = $this->getRevisionRecord();
232 $authority = $this->list->getAuthority();
233 $ret = [
234 'id' => $revRecord->getId(),
235 'timestamp' => wfTimestamp( TS_ISO_8601, $revRecord->getTimestamp() ),
236 'userhidden' => (bool)$revRecord->isDeleted( RevisionRecord::DELETED_USER ),
237 'commenthidden' => (bool)$revRecord->isDeleted( RevisionRecord::DELETED_COMMENT ),
238 'texthidden' => (bool)$revRecord->isDeleted( RevisionRecord::DELETED_TEXT ),
240 if ( $revRecord->userCan( RevisionRecord::DELETED_USER, $authority ) ) {
241 $revUser = $revRecord->getUser( RevisionRecord::FOR_THIS_USER, $authority );
242 $ret += [
243 'userid' => $revUser ? $revUser->getId() : 0,
244 'user' => $revUser ? $revUser->getName() : '',
247 if ( $revRecord->userCan( RevisionRecord::DELETED_COMMENT, $authority ) ) {
248 $revComment = $revRecord->getComment( RevisionRecord::FOR_THIS_USER, $authority );
249 $ret += [
250 'comment' => $revComment ? $revComment->text : ''
254 return $ret;