Equialization of Serbian Latin translations with Serbian Cyrillic translations
[lhc/web/wiklou.git] / includes / page / ImageHistoryList.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 */
20
21 use MediaWiki\MediaWikiServices;
22
23 /**
24 * Builds the image revision log shown on image pages
25 *
26 * @ingroup Media
27 */
28 class ImageHistoryList extends ContextSource {
29
30 /**
31 * @var Title
32 */
33 protected $title;
34
35 /**
36 * @var File
37 */
38 protected $img;
39
40 /**
41 * @var ImagePage
42 */
43 protected $imagePage;
44
45 /**
46 * @var File
47 */
48 protected $current;
49
50 protected $repo, $showThumb;
51 protected $preventClickjacking = false;
52
53 /**
54 * @param ImagePage $imagePage
55 */
56 public function __construct( $imagePage ) {
57 $context = $imagePage->getContext();
58 $this->current = $imagePage->getPage()->getFile();
59 $this->img = $imagePage->getDisplayedFile();
60 $this->title = $imagePage->getTitle();
61 $this->imagePage = $imagePage;
62 $this->showThumb = $context->getConfig()->get( 'ShowArchiveThumbnails' ) &&
63 $this->img->canRender();
64 $this->setContext( $context );
65 }
66
67 /**
68 * @return ImagePage
69 */
70 public function getImagePage() {
71 return $this->imagePage;
72 }
73
74 /**
75 * @return File
76 */
77 public function getFile() {
78 return $this->img;
79 }
80
81 /**
82 * @param string $navLinks
83 * @return string
84 */
85 public function beginImageHistoryList( $navLinks = '' ) {
86 return Xml::element( 'h2', [ 'id' => 'filehistory' ], $this->msg( 'filehist' )->text() )
87 . "\n"
88 . "<div id=\"mw-imagepage-section-filehistory\">\n"
89 . $this->msg( 'filehist-help' )->parseAsBlock()
90 . $navLinks . "\n"
91 . Xml::openElement( 'table', [ 'class' => 'wikitable filehistory' ] ) . "\n"
92 . '<tr><th></th>'
93 . ( $this->current->isLocal()
94 && ( $this->getUser()->isAllowedAny( 'delete', 'deletedhistory' ) ) ? '<th></th>' : '' )
95 . '<th>' . $this->msg( 'filehist-datetime' )->escaped() . '</th>'
96 . ( $this->showThumb ? '<th>' . $this->msg( 'filehist-thumb' )->escaped() . '</th>' : '' )
97 . '<th>' . $this->msg( 'filehist-dimensions' )->escaped() . '</th>'
98 . '<th>' . $this->msg( 'filehist-user' )->escaped() . '</th>'
99 . '<th>' . $this->msg( 'filehist-comment' )->escaped() . '</th>'
100 . "</tr>\n";
101 }
102
103 /**
104 * @param string $navLinks
105 * @return string
106 */
107 public function endImageHistoryList( $navLinks = '' ) {
108 return "</table>\n$navLinks\n</div>\n";
109 }
110
111 /**
112 * @param bool $iscur
113 * @param File $file
114 * @return string
115 */
116 public function imageHistoryLine( $iscur, $file ) {
117 $user = $this->getUser();
118 $lang = $this->getLanguage();
119 $timestamp = wfTimestamp( TS_MW, $file->getTimestamp() );
120 $img = $iscur ? $file->getName() : $file->getArchiveName();
121 $userId = $file->getUser( 'id' );
122 $userText = $file->getUser( 'text' );
123 $description = $file->getDescription( File::FOR_THIS_USER, $user );
124
125 $local = $this->current->isLocal();
126 $row = $selected = '';
127
128 // Deletion link
129 if ( $local && ( $user->isAllowedAny( 'delete', 'deletedhistory' ) ) ) {
130 $row .= '<td>';
131 # Link to remove from history
132 if ( $user->isAllowed( 'delete' ) ) {
133 $q = [ 'action' => 'delete' ];
134 if ( !$iscur ) {
135 $q['oldimage'] = $img;
136 }
137 $row .= Linker::linkKnown(
138 $this->title,
139 $this->msg( $iscur ? 'filehist-deleteall' : 'filehist-deleteone' )->escaped(),
140 [], $q
141 );
142 }
143 # Link to hide content. Don't show useless link to people who cannot hide revisions.
144 $canHide = $user->isAllowed( 'deleterevision' );
145 if ( $canHide || ( $user->isAllowed( 'deletedhistory' ) && $file->getVisibility() ) ) {
146 if ( $user->isAllowed( 'delete' ) ) {
147 $row .= '<br />';
148 }
149 // If file is top revision or locked from this user, don't link
150 if ( $iscur || !$file->userCan( File::DELETED_RESTRICTED, $user ) ) {
151 $del = Linker::revDeleteLinkDisabled( $canHide );
152 } else {
153 list( $ts, ) = explode( '!', $img, 2 );
154 $query = [
155 'type' => 'oldimage',
156 'target' => $this->title->getPrefixedText(),
157 'ids' => $ts,
158 ];
159 $del = Linker::revDeleteLink( $query,
160 $file->isDeleted( File::DELETED_RESTRICTED ), $canHide );
161 }
162 $row .= $del;
163 }
164 $row .= '</td>';
165 }
166
167 // Reversion link/current indicator
168 $row .= '<td>';
169 if ( $iscur ) {
170 $row .= $this->msg( 'filehist-current' )->escaped();
171 } elseif ( $local && $this->title->quickUserCan( 'edit', $user )
172 && $this->title->quickUserCan( 'upload', $user )
173 ) {
174 if ( $file->isDeleted( File::DELETED_FILE ) ) {
175 $row .= $this->msg( 'filehist-revert' )->escaped();
176 } else {
177 $row .= Linker::linkKnown(
178 $this->title,
179 $this->msg( 'filehist-revert' )->escaped(),
180 [],
181 [
182 'action' => 'revert',
183 'oldimage' => $img,
184 ]
185 );
186 }
187 }
188 $row .= '</td>';
189
190 // Date/time and image link
191 if ( $file->getTimestamp() === $this->img->getTimestamp() ) {
192 $selected = "class='filehistory-selected'";
193 }
194 $row .= "<td $selected style='white-space: nowrap;'>";
195 if ( !$file->userCan( File::DELETED_FILE, $user ) ) {
196 # Don't link to unviewable files
197 $row .= '<span class="history-deleted">'
198 . $lang->userTimeAndDate( $timestamp, $user ) . '</span>';
199 } elseif ( $file->isDeleted( File::DELETED_FILE ) ) {
200 if ( $local ) {
201 $this->preventClickjacking();
202 $revdel = SpecialPage::getTitleFor( 'Revisiondelete' );
203 # Make a link to review the image
204 $url = Linker::linkKnown(
205 $revdel,
206 $lang->userTimeAndDate( $timestamp, $user ),
207 [],
208 [
209 'target' => $this->title->getPrefixedText(),
210 'file' => $img,
211 'token' => $user->getEditToken( $img )
212 ]
213 );
214 } else {
215 $url = $lang->userTimeAndDate( $timestamp, $user );
216 }
217 $row .= '<span class="history-deleted">' . $url . '</span>';
218 } elseif ( !$file->exists() ) {
219 $row .= '<span class="mw-file-missing">'
220 . $lang->userTimeAndDate( $timestamp, $user ) . '</span>';
221 } else {
222 $url = $iscur ? $this->current->getUrl() : $this->current->getArchiveUrl( $img );
223 $row .= Xml::element(
224 'a',
225 [ 'href' => $url ],
226 $lang->userTimeAndDate( $timestamp, $user )
227 );
228 }
229 $row .= "</td>";
230
231 // Thumbnail
232 if ( $this->showThumb ) {
233 $row .= '<td>' . $this->getThumbForLine( $file ) . '</td>';
234 }
235
236 // Image dimensions + size
237 $row .= '<td>';
238 $row .= htmlspecialchars( $file->getDimensionsString() );
239 $row .= $this->msg( 'word-separator' )->escaped();
240 $row .= '<span style="white-space: nowrap;">';
241 $row .= $this->msg( 'parentheses' )->sizeParams( $file->getSize() )->escaped();
242 $row .= '</span>';
243 $row .= '</td>';
244
245 // Uploading user
246 $row .= '<td>';
247 // Hide deleted usernames
248 if ( $file->isDeleted( File::DELETED_USER ) ) {
249 $row .= '<span class="history-deleted">'
250 . $this->msg( 'rev-deleted-user' )->escaped() . '</span>';
251 } else {
252 if ( $local ) {
253 $row .= Linker::userLink( $userId, $userText );
254 $row .= '<span style="white-space: nowrap;">';
255 $row .= Linker::userToolLinks( $userId, $userText );
256 $row .= '</span>';
257 } else {
258 $row .= htmlspecialchars( $userText );
259 }
260 }
261 $row .= '</td>';
262
263 // Don't show deleted descriptions
264 if ( $file->isDeleted( File::DELETED_COMMENT ) ) {
265 $row .= '<td><span class="history-deleted">' .
266 $this->msg( 'rev-deleted-comment' )->escaped() . '</span></td>';
267 } else {
268 $row .=
269 '<td dir="' . MediaWikiServices::getInstance()->getContentLanguage()->getDir() .
270 '">' . Linker::formatComment( $description, $this->title ) . '</td>';
271 }
272
273 $rowClass = null;
274 Hooks::run( 'ImagePageFileHistoryLine', [ $this, $file, &$row, &$rowClass ] );
275 $classAttr = $rowClass ? " class='$rowClass'" : '';
276
277 return "<tr{$classAttr}>{$row}</tr>\n";
278 }
279
280 /**
281 * @param File $file
282 * @return string
283 */
284 protected function getThumbForLine( $file ) {
285 $lang = $this->getLanguage();
286 $user = $this->getUser();
287 if ( $file->allowInlineDisplay() && $file->userCan( File::DELETED_FILE, $user )
288 && !$file->isDeleted( File::DELETED_FILE )
289 ) {
290 $params = [
291 'width' => '120',
292 'height' => '120',
293 ];
294 $timestamp = wfTimestamp( TS_MW, $file->getTimestamp() );
295
296 $thumbnail = $file->transform( $params );
297 $options = [
298 'alt' => $this->msg( 'filehist-thumbtext',
299 $lang->userTimeAndDate( $timestamp, $user ),
300 $lang->userDate( $timestamp, $user ),
301 $lang->userTime( $timestamp, $user ) )->text(),
302 'file-link' => true,
303 ];
304
305 if ( !$thumbnail ) {
306 return $this->msg( 'filehist-nothumb' )->escaped();
307 }
308
309 return $thumbnail->toHtml( $options );
310 } else {
311 return $this->msg( 'filehist-nothumb' )->escaped();
312 }
313 }
314
315 /**
316 * @param bool $enable
317 */
318 protected function preventClickjacking( $enable = true ) {
319 $this->preventClickjacking = $enable;
320 }
321
322 /**
323 * @return bool
324 */
325 public function getPreventClickjacking() {
326 return $this->preventClickjacking;
327 }
328 }