Move remaining LoadBalancer classes to Rdbms
[mediawiki.git] / includes / revisiondelete / RevDelFileItem.php
blob9beafc9893b4cbd385e404c1e2e4ce6e2d761ea7
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 /**
23 * Item class for an oldimage table row
25 class RevDelFileItem extends RevDelItem {
26 /** @var RevDelFileList */
27 protected $list;
28 /** @var OldLocalFile */
29 protected $file;
31 public function __construct( $list, $row ) {
32 parent::__construct( $list, $row );
33 $this->file = RepoGroup::singleton()->getLocalRepo()->newFileFromRow( $row );
36 public function getIdField() {
37 return 'oi_archive_name';
40 public function getTimestampField() {
41 return 'oi_timestamp';
44 public function getAuthorIdField() {
45 return 'oi_user';
48 public function getAuthorNameField() {
49 return 'oi_user_text';
52 public function getId() {
53 $parts = explode( '!', $this->row->oi_archive_name );
55 return $parts[0];
58 public function canView() {
59 return $this->file->userCan( File::DELETED_RESTRICTED, $this->list->getUser() );
62 public function canViewContent() {
63 return $this->file->userCan( File::DELETED_FILE, $this->list->getUser() );
66 public function getBits() {
67 return $this->file->getVisibility();
70 public function setBits( $bits ) {
71 # Queue the file op
72 # @todo FIXME: Move to LocalFile.php
73 if ( $this->isDeleted() ) {
74 if ( $bits & File::DELETED_FILE ) {
75 # Still deleted
76 } else {
77 # Newly undeleted
78 $key = $this->file->getStorageKey();
79 $srcRel = $this->file->repo->getDeletedHashPath( $key ) . $key;
80 $this->list->storeBatch[] = [
81 $this->file->repo->getVirtualUrl( 'deleted' ) . '/' . $srcRel,
82 'public',
83 $this->file->getRel()
85 $this->list->cleanupBatch[] = $key;
87 } elseif ( $bits & File::DELETED_FILE ) {
88 # Newly deleted
89 $key = $this->file->getStorageKey();
90 $dstRel = $this->file->repo->getDeletedHashPath( $key ) . $key;
91 $this->list->deleteBatch[] = [ $this->file->getRel(), $dstRel ];
94 # Do the database operations
95 $dbw = wfGetDB( DB_MASTER );
96 $dbw->update( 'oldimage',
97 [ 'oi_deleted' => $bits ],
99 'oi_name' => $this->row->oi_name,
100 'oi_timestamp' => $this->row->oi_timestamp,
101 'oi_deleted' => $this->getBits()
103 __METHOD__
106 return (bool)$dbw->affectedRows();
109 public function isDeleted() {
110 return $this->file->isDeleted( File::DELETED_FILE );
114 * Get the link to the file.
115 * Overridden by RevDelArchivedFileItem.
116 * @return string
118 protected function getLink() {
119 $date = $this->list->getLanguage()->userTimeAndDate(
120 $this->file->getTimestamp(), $this->list->getUser() );
122 if ( !$this->isDeleted() ) {
123 # Regular files...
124 return Html::element( 'a', [ 'href' => $this->file->getUrl() ], $date );
127 # Hidden files...
128 if ( !$this->canViewContent() ) {
129 $link = htmlspecialchars( $date );
130 } else {
131 $link = $this->getLinkRenderer()->makeLink(
132 SpecialPage::getTitleFor( 'Revisiondelete' ),
133 $date,
136 'target' => $this->list->title->getPrefixedText(),
137 'file' => $this->file->getArchiveName(),
138 'token' => $this->list->getUser()->getEditToken(
139 $this->file->getArchiveName() )
144 return '<span class="history-deleted">' . $link . '</span>';
148 * Generate a user tool link cluster if the current user is allowed to view it
149 * @return string HTML
151 protected function getUserTools() {
152 if ( $this->file->userCan( Revision::DELETED_USER, $this->list->getUser() ) ) {
153 $uid = $this->file->getUser( 'id' );
154 $name = $this->file->getUser( 'text' );
155 $link = Linker::userLink( $uid, $name ) . Linker::userToolLinks( $uid, $name );
156 } else {
157 $link = $this->list->msg( 'rev-deleted-user' )->escaped();
159 if ( $this->file->isDeleted( Revision::DELETED_USER ) ) {
160 return '<span class="history-deleted">' . $link . '</span>';
163 return $link;
167 * Wrap and format the file's comment block, if the current
168 * user is allowed to view it.
170 * @return string HTML
172 protected function getComment() {
173 if ( $this->file->userCan( File::DELETED_COMMENT, $this->list->getUser() ) ) {
174 $block = Linker::commentBlock( $this->file->getDescription() );
175 } else {
176 $block = ' ' . $this->list->msg( 'rev-deleted-comment' )->escaped();
178 if ( $this->file->isDeleted( File::DELETED_COMMENT ) ) {
179 return "<span class=\"history-deleted\">$block</span>";
182 return $block;
185 public function getHTML() {
186 $data =
187 $this->list->msg( 'widthheight' )->numParams(
188 $this->file->getWidth(), $this->file->getHeight() )->text() .
189 ' (' . $this->list->msg( 'nbytes' )->numParams( $this->file->getSize() )->text() . ')';
191 return '<li>' . $this->getLink() . ' ' . $this->getUserTools() . ' ' .
192 $data . ' ' . $this->getComment() . '</li>';
195 public function getApiData( ApiResult $result ) {
196 $file = $this->file;
197 $user = $this->list->getUser();
198 $ret = [
199 'title' => $this->list->title->getPrefixedText(),
200 'archivename' => $file->getArchiveName(),
201 'timestamp' => wfTimestamp( TS_ISO_8601, $file->getTimestamp() ),
202 'width' => $file->getWidth(),
203 'height' => $file->getHeight(),
204 'size' => $file->getSize(),
205 'userhidden' => (bool)$file->isDeleted( Revision::DELETED_USER ),
206 'commenthidden' => (bool)$file->isDeleted( Revision::DELETED_COMMENT ),
207 'contenthidden' => (bool)$this->isDeleted(),
209 if ( !$this->isDeleted() ) {
210 $ret += [
211 'url' => $file->getUrl(),
213 } elseif ( $this->canViewContent() ) {
214 $ret += [
215 'url' => SpecialPage::getTitleFor( 'Revisiondelete' )->getLinkURL(
217 'target' => $this->list->title->getPrefixedText(),
218 'file' => $file->getArchiveName(),
219 'token' => $user->getEditToken( $file->getArchiveName() )
224 if ( $file->userCan( Revision::DELETED_USER, $user ) ) {
225 $ret += [
226 'userid' => $file->user,
227 'user' => $file->user_text,
230 if ( $file->userCan( Revision::DELETED_COMMENT, $user ) ) {
231 $ret += [
232 'comment' => $file->description,
236 return $ret;
239 public function lock() {
240 return $this->file->acquireFileLock();
243 public function unlock() {
244 return $this->file->releaseFileLock();