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