Merge "Html::closeElement: Don't omit closing tags."
[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 var $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[] = array( $this->file->repo->getVirtualUrl( 'deleted' ) . '/' . $srcRel, 'public', $this->file->getRel() );
79 $this->list->cleanupBatch[] = $key;
80 }
81 } elseif ( $bits & File::DELETED_FILE ) {
82 # Newly deleted
83 $key = $this->file->getStorageKey();
84 $dstRel = $this->file->repo->getDeletedHashPath( $key ) . $key;
85 $this->list->deleteBatch[] = array( $this->file->getRel(), $dstRel );
86 }
87
88 # Do the database operations
89 $dbw = wfGetDB( DB_MASTER );
90 $dbw->update( 'oldimage', array( 'oi_deleted' => $bits ), array( 'oi_name' => $this->row->oi_name, 'oi_timestamp' => $this->row->oi_timestamp, 'oi_deleted' => $this->getBits() ), __METHOD__ );
91
92 return (bool) $dbw->affectedRows();
93 }
94
95 public function isDeleted() {
96 return $this->file->isDeleted( File::DELETED_FILE );
97 }
98
99 /**
100 * Get the link to the file.
101 * Overridden by RevDelArchivedFileItem.
102 * @return string
103 */
104 protected function getLink() {
105 $date = htmlspecialchars( $this->list->getLanguage()->userTimeAndDate( $this->file->getTimestamp(), $this->list->getUser() ) );
106
107 if ( !$this->isDeleted() ) {
108 # Regular files...
109 return Html::rawElement( 'a', array( 'href' => $this->file->getUrl() ), $date );
110 }
111
112 # Hidden files...
113 if ( !$this->canViewContent() ) {
114 $link = $date;
115 } else {
116 $link = Linker::link( SpecialPage::getTitleFor( 'Revisiondelete' ), $date, array(), array( 'target' => $this->list->title->getPrefixedText(), 'file' => $this->file->getArchiveName(), 'token' => $this->list->getUser()->getEditToken( $this->file->getArchiveName() ) ) );
117 }
118
119 return '<span class="history-deleted">' . $link . '</span>';
120 }
121
122 /**
123 * Generate a user tool link cluster if the current user is allowed to view it
124 * @return string HTML
125 */
126 protected function getUserTools() {
127 if ( $this->file->userCan( Revision::DELETED_USER, $this->list->getUser() ) ) {
128 $uid = $this->file->getUser( 'id' );
129 $name = $this->file->getUser( 'text' );
130 $link = Linker::userLink( $uid, $name ) . Linker::userToolLinks( $uid, $name );
131 } else {
132 $link = $this->list->msg( 'rev-deleted-user' )->escaped();
133 }
134 if ( $this->file->isDeleted( Revision::DELETED_USER ) ) {
135 return '<span class="history-deleted">' . $link . '</span>';
136 }
137
138 return $link;
139 }
140
141 /**
142 * Wrap and format the file's comment block, if the current
143 * user is allowed to view it.
144 *
145 * @return string HTML
146 */
147 protected function getComment() {
148 if ( $this->file->userCan( File::DELETED_COMMENT, $this->list->getUser() ) ) {
149 $block = Linker::commentBlock( $this->file->getDescription() );
150 } else {
151 $block = ' ' . $this->list->msg( 'rev-deleted-comment' )->escaped();
152 }
153 if ( $this->file->isDeleted( File::DELETED_COMMENT ) ) {
154 return "<span class=\"history-deleted\">$block</span>";
155 }
156
157 return $block;
158 }
159
160 public function getHTML() {
161 $data = $this->list->msg( 'widthheight' )->numParams( $this->file->getWidth(), $this->file->getHeight() )->text() . ' (' . $this->list->msg( 'nbytes' )->numParams( $this->file->getSize() )->text() . ')';
162
163 return '<li>' . $this->getLink() . ' ' . $this->getUserTools() . ' ' . $data . ' ' . $this->getComment() . '</li>';
164 }
165
166 public function getApiData( ApiResult $result ) {
167 $file = $this->file;
168 $user = $this->list->getUser();
169 $ret = array( 'title' => $this->list->title->getPrefixedText(), 'archivename' => $file->getArchiveName(), 'timestamp' => wfTimestamp( TS_ISO_8601, $file->getTimestamp() ), 'width' => $file->getWidth(), 'height' => $file->getHeight(), 'size' => $file->getSize(), );
170 $ret += $file->isDeleted( Revision::DELETED_USER ) ? array( 'userhidden' => '' ) : array();
171 $ret += $file->isDeleted( Revision::DELETED_COMMENT ) ? array( 'commenthidden' => '' ) : array();
172 $ret += $this->isDeleted() ? array( 'contenthidden' => '' ) : array();
173 if ( !$this->isDeleted() ) {
174 $ret += array( 'url' => $file->getUrl(), );
175 } elseif ( $this->canViewContent() ) {
176 $ret += array( 'url' => SpecialPage::getTitleFor( 'Revisiondelete' )->getLinkURL( array( 'target' => $this->list->title->getPrefixedText(), 'file' => $file->getArchiveName(), 'token' => $user->getEditToken( $file->getArchiveName() ) ), false, PROTO_RELATIVE ), );
177 }
178 if ( $file->userCan( Revision::DELETED_USER, $user ) ) {
179 $ret += array( 'userid' => $file->user, 'user' => $file->user_text, );
180 }
181 if ( $file->userCan( Revision::DELETED_COMMENT, $user ) ) {
182 $ret += array( 'comment' => $file->description, );
183 }
184
185 return $ret;
186 }
187 }