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
19 * @ingroup RevisionDelete
22 use MediaWiki\Api\ApiResult
;
23 use MediaWiki\Html\Html
;
24 use MediaWiki\Linker\Linker
;
25 use MediaWiki\MediaWikiServices
;
26 use MediaWiki\RevisionList\RevisionListBase
;
27 use MediaWiki\SpecialPage\SpecialPage
;
28 use Wikimedia\Rdbms\IConnectionProvider
;
31 * Item class for an oldimage table row
33 class RevDelFileItem
extends RevDelItem
{
34 /** @var RevDelFileList */
36 /** @var OldLocalFile */
38 protected IConnectionProvider
$dbProvider;
40 public function __construct( RevisionListBase
$list, $row ) {
41 parent
::__construct( $list, $row );
42 $this->file
= static::initFile( $list, $row );
43 $this->dbProvider
= MediaWikiServices
::getInstance()->getConnectionProvider();
47 * Create file object from $row sourced from $list
49 * @param RevisionListBase $list
53 protected static function initFile( $list, $row ) {
54 return MediaWikiServices
::getInstance()->getRepoGroup()->getLocalRepo()
55 ->newFileFromRow( $row );
58 public function getIdField() {
59 return 'oi_archive_name';
62 public function getTimestampField() {
63 return 'oi_timestamp';
66 public function getAuthorIdField() {
70 public function getAuthorNameField() {
71 return 'oi_user_text';
74 public function getAuthorActorField() {
78 public function getId() {
79 $parts = explode( '!', $this->row
->oi_archive_name
);
84 public function canView() {
85 return $this->file
->userCan( File
::DELETED_RESTRICTED
, $this->list->getAuthority() );
88 public function canViewContent() {
89 return $this->file
->userCan( File
::DELETED_FILE
, $this->list->getAuthority() );
92 public function getBits() {
93 return $this->file
->getVisibility();
96 public function setBits( $bits ) {
98 # @todo FIXME: Move to LocalFile.php
99 if ( $this->isDeleted() ) {
100 if ( $bits & File
::DELETED_FILE
) {
104 $key = $this->file
->getStorageKey();
105 $srcRel = $this->file
->repo
->getDeletedHashPath( $key ) . $key;
106 $this->list->storeBatch
[] = [
107 $this->file
->repo
->getVirtualUrl( 'deleted' ) . '/' . $srcRel,
109 $this->file
->getRel()
111 $this->list->cleanupBatch
[] = $key;
113 } elseif ( $bits & File
::DELETED_FILE
) {
115 $key = $this->file
->getStorageKey();
116 $dstRel = $this->file
->repo
->getDeletedHashPath( $key ) . $key;
117 $this->list->deleteBatch
[] = [ $this->file
->getRel(), $dstRel ];
120 # Do the database operations
121 $dbw = $this->dbProvider
->getPrimaryDatabase();
122 $dbw->newUpdateQueryBuilder()
123 ->update( 'oldimage' )
124 ->set( [ 'oi_deleted' => $bits ] )
126 'oi_name' => $this->row
->oi_name
,
127 'oi_timestamp' => $this->row
->oi_timestamp
,
128 'oi_deleted' => $this->getBits()
130 ->caller( __METHOD__
)->execute();
132 return (bool)$dbw->affectedRows();
135 public function isDeleted() {
136 return $this->file
->isDeleted( File
::DELETED_FILE
);
140 * Get the link to the file.
141 * Overridden by RevDelArchivedFileItem.
144 protected function getLink() {
145 $date = $this->list->getLanguage()->userTimeAndDate(
146 $this->file
->getTimestamp(), $this->list->getUser() );
148 if ( !$this->isDeleted() ) {
150 return Html
::element( 'a', [ 'href' => $this->file
->getUrl() ], $date );
154 if ( !$this->canViewContent() ) {
155 $link = htmlspecialchars( $date );
157 $link = $this->getLinkRenderer()->makeLink(
158 SpecialPage
::getTitleFor( 'Revisiondelete' ),
162 'target' => $this->list->getPageName(),
163 'file' => $this->file
->getArchiveName(),
164 'token' => $this->list->getUser()->getEditToken(
165 $this->file
->getArchiveName() )
170 return '<span class="history-deleted">' . $link . '</span>';
174 * Generate a user tool link cluster if the current user is allowed to view it
175 * @return string HTML
177 protected function getUserTools() {
178 $uploader = $this->file
->getUploader( File
::FOR_THIS_USER
, $this->list->getAuthority() );
180 $link = Linker
::userLink( $uploader->getId(), $uploader->getName() ) .
181 Linker
::userToolLinks( $uploader->getId(), $uploader->getName() );
184 $link = $this->list->msg( 'rev-deleted-user' )->escaped();
186 if ( $this->file
->isDeleted( File
::DELETED_USER
) ) {
187 return '<span class="history-deleted">' . $link . '</span>';
193 * Wrap and format the file's comment block, if the current
194 * user is allowed to view it.
196 * @return string HTML
198 protected function getComment() {
199 if ( $this->file
->userCan( File
::DELETED_COMMENT
, $this->list->getAuthority() ) ) {
200 $block = MediaWikiServices
::getInstance()->getCommentFormatter()
201 ->formatBlock( $this->file
->getDescription() );
203 $block = ' ' . $this->list->msg( 'rev-deleted-comment' )->escaped();
205 if ( $this->file
->isDeleted( File
::DELETED_COMMENT
) ) {
206 return "<span class=\"history-deleted\">$block</span>";
212 public function getHTML() {
214 $this->list->msg( 'widthheight' )->numParams(
215 $this->file
->getWidth(),
216 $this->file
->getHeight() )->escaped() .
217 ' (' . $this->list->msg( 'nbytes' )->numParams(
218 $this->file
->getSize() )->escaped() . ')';
220 return '<li>' . $this->getLink() . ' ' . $this->getUserTools() . ' ' .
221 $data . ' ' . $this->getComment() . '</li>';
224 public function getApiData( ApiResult
$result ) {
226 $user = $this->list->getUser();
228 'title' => $this->list->getPageName(),
229 'archivename' => $file->getArchiveName(),
230 'timestamp' => wfTimestamp( TS_ISO_8601
, $file->getTimestamp() ),
231 'width' => $file->getWidth(),
232 'height' => $file->getHeight(),
233 'size' => $file->getSize(),
234 'userhidden' => (bool)$file->isDeleted( File
::DELETED_USER
),
235 'commenthidden' => (bool)$file->isDeleted( File
::DELETED_COMMENT
),
236 'contenthidden' => (bool)$this->isDeleted(),
238 if ( !$this->isDeleted() ) {
240 'url' => $file->getUrl(),
242 } elseif ( $this->canViewContent() ) {
244 'url' => SpecialPage
::getTitleFor( 'Revisiondelete' )->getLinkURL(
246 'target' => $this->list->getPageName(),
247 'file' => $file->getArchiveName(),
248 'token' => $user->getEditToken( $file->getArchiveName() )
253 $uploader = $file->getUploader( File
::FOR_THIS_USER
, $user );
256 'userid' => $uploader->getId(),
257 'user' => $uploader->getName(),
260 $comment = $file->getDescription( File
::FOR_THIS_USER
, $user );
261 if ( ( $comment ??
'' ) !== '' ) {
263 'comment' => $comment,
270 public function lock() {
271 return $this->file
->acquireFileLock();
274 public function unlock() {
275 return $this->file
->releaseFileLock();