Merge "Avoid database access in ImportTitleFactory tests dataProviders"
[lhc/web/wiklou.git] / includes / revisiondelete / RevDelFileItem.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 Wikimedia\Rdbms\IDatabase;
23
24 /**
25 * Item class for an oldimage table row
26 */
27 class RevDelFileItem extends RevDelItem {
28 /** @var RevDelFileList */
29 protected $list;
30 /** @var OldLocalFile */
31 protected $file;
32
33 public function __construct( $list, $row ) {
34 parent::__construct( $list, $row );
35 $this->file = RepoGroup::singleton()->getLocalRepo()->newFileFromRow( $row );
36 }
37
38 public function getIdField() {
39 return 'oi_archive_name';
40 }
41
42 public function getTimestampField() {
43 return 'oi_timestamp';
44 }
45
46 public function getAuthorIdField() {
47 return 'oi_user';
48 }
49
50 public function getAuthorNameField() {
51 return 'oi_user_text';
52 }
53
54 public function getId() {
55 $parts = explode( '!', $this->row->oi_archive_name );
56
57 return $parts[0];
58 }
59
60 public function canView() {
61 return $this->file->userCan( File::DELETED_RESTRICTED, $this->list->getUser() );
62 }
63
64 public function canViewContent() {
65 return $this->file->userCan( File::DELETED_FILE, $this->list->getUser() );
66 }
67
68 public function getBits() {
69 return $this->file->getVisibility();
70 }
71
72 public function setBits( $bits ) {
73 # Queue the file op
74 # @todo FIXME: Move to LocalFile.php
75 if ( $this->isDeleted() ) {
76 if ( $bits & File::DELETED_FILE ) {
77 # Still deleted
78 } else {
79 # Newly undeleted
80 $key = $this->file->getStorageKey();
81 $srcRel = $this->file->repo->getDeletedHashPath( $key ) . $key;
82 $this->list->storeBatch[] = [
83 $this->file->repo->getVirtualUrl( 'deleted' ) . '/' . $srcRel,
84 'public',
85 $this->file->getRel()
86 ];
87 $this->list->cleanupBatch[] = $key;
88 }
89 } elseif ( $bits & File::DELETED_FILE ) {
90 # Newly deleted
91 $key = $this->file->getStorageKey();
92 $dstRel = $this->file->repo->getDeletedHashPath( $key ) . $key;
93 $this->list->deleteBatch[] = [ $this->file->getRel(), $dstRel ];
94 }
95
96 # Do the database operations
97 $dbw = wfGetDB( DB_MASTER );
98 $dbw->update( 'oldimage',
99 [ 'oi_deleted' => $bits ],
100 [
101 'oi_name' => $this->row->oi_name,
102 'oi_timestamp' => $this->row->oi_timestamp,
103 'oi_deleted' => $this->getBits()
104 ],
105 __METHOD__
106 );
107
108 return (bool)$dbw->affectedRows();
109 }
110
111 public function isDeleted() {
112 return $this->file->isDeleted( File::DELETED_FILE );
113 }
114
115 /**
116 * Get the link to the file.
117 * Overridden by RevDelArchivedFileItem.
118 * @return string
119 */
120 protected function getLink() {
121 $date = $this->list->getLanguage()->userTimeAndDate(
122 $this->file->getTimestamp(), $this->list->getUser() );
123
124 if ( !$this->isDeleted() ) {
125 # Regular files...
126 return Html::element( 'a', [ 'href' => $this->file->getUrl() ], $date );
127 }
128
129 # Hidden files...
130 if ( !$this->canViewContent() ) {
131 $link = htmlspecialchars( $date );
132 } else {
133 $link = $this->getLinkRenderer()->makeLink(
134 SpecialPage::getTitleFor( 'Revisiondelete' ),
135 $date,
136 [],
137 [
138 'target' => $this->list->title->getPrefixedText(),
139 'file' => $this->file->getArchiveName(),
140 'token' => $this->list->getUser()->getEditToken(
141 $this->file->getArchiveName() )
142 ]
143 );
144 }
145
146 return '<span class="history-deleted">' . $link . '</span>';
147 }
148
149 /**
150 * Generate a user tool link cluster if the current user is allowed to view it
151 * @return string HTML
152 */
153 protected function getUserTools() {
154 if ( $this->file->userCan( Revision::DELETED_USER, $this->list->getUser() ) ) {
155 $uid = $this->file->getUser( 'id' );
156 $name = $this->file->getUser( 'text' );
157 $link = Linker::userLink( $uid, $name ) . Linker::userToolLinks( $uid, $name );
158 } else {
159 $link = $this->list->msg( 'rev-deleted-user' )->escaped();
160 }
161 if ( $this->file->isDeleted( Revision::DELETED_USER ) ) {
162 return '<span class="history-deleted">' . $link . '</span>';
163 }
164
165 return $link;
166 }
167
168 /**
169 * Wrap and format the file's comment block, if the current
170 * user is allowed to view it.
171 *
172 * @return string HTML
173 */
174 protected function getComment() {
175 if ( $this->file->userCan( File::DELETED_COMMENT, $this->list->getUser() ) ) {
176 $block = Linker::commentBlock( $this->file->getDescription() );
177 } else {
178 $block = ' ' . $this->list->msg( 'rev-deleted-comment' )->escaped();
179 }
180 if ( $this->file->isDeleted( File::DELETED_COMMENT ) ) {
181 return "<span class=\"history-deleted\">$block</span>";
182 }
183
184 return $block;
185 }
186
187 public function getHTML() {
188 $data =
189 $this->list->msg( 'widthheight' )->numParams(
190 $this->file->getWidth(), $this->file->getHeight() )->text() .
191 ' (' . $this->list->msg( 'nbytes' )->numParams( $this->file->getSize() )->text() . ')';
192
193 return '<li>' . $this->getLink() . ' ' . $this->getUserTools() . ' ' .
194 $data . ' ' . $this->getComment() . '</li>';
195 }
196
197 public function getApiData( ApiResult $result ) {
198 $file = $this->file;
199 $user = $this->list->getUser();
200 $ret = [
201 'title' => $this->list->title->getPrefixedText(),
202 'archivename' => $file->getArchiveName(),
203 'timestamp' => wfTimestamp( TS_ISO_8601, $file->getTimestamp() ),
204 'width' => $file->getWidth(),
205 'height' => $file->getHeight(),
206 'size' => $file->getSize(),
207 'userhidden' => (bool)$file->isDeleted( Revision::DELETED_USER ),
208 'commenthidden' => (bool)$file->isDeleted( Revision::DELETED_COMMENT ),
209 'contenthidden' => (bool)$this->isDeleted(),
210 ];
211 if ( !$this->isDeleted() ) {
212 $ret += [
213 'url' => $file->getUrl(),
214 ];
215 } elseif ( $this->canViewContent() ) {
216 $ret += [
217 'url' => SpecialPage::getTitleFor( 'Revisiondelete' )->getLinkURL(
218 [
219 'target' => $this->list->title->getPrefixedText(),
220 'file' => $file->getArchiveName(),
221 'token' => $user->getEditToken( $file->getArchiveName() )
222 ]
223 ),
224 ];
225 }
226 if ( $file->userCan( Revision::DELETED_USER, $user ) ) {
227 $ret += [
228 'userid' => $file->user,
229 'user' => $file->user_text,
230 ];
231 }
232 if ( $file->userCan( Revision::DELETED_COMMENT, $user ) ) {
233 $ret += [
234 'comment' => $file->description,
235 ];
236 }
237
238 return $ret;
239 }
240
241 public function lock() {
242 return $this->file->acquireFileLock();
243 }
244
245 public function unlock() {
246 return $this->file->releaseFileLock();
247 }
248 }