MediaWiki master
ImageHistoryList.php
Go to the documentation of this file.
1<?php
22use MediaWiki\HookContainer\ProtectedHookAccessorTrait;
29
36 use ProtectedHookAccessorTrait;
37
41 protected $title;
42
46 protected $img;
47
51 protected $imagePage;
52
56 protected $current;
57
58 protected $repo;
59 protected bool $showThumb;
60 protected $preventClickjacking = false;
61
65 public function __construct( $imagePage ) {
66 $context = $imagePage->getContext();
67 $this->current = $imagePage->getPage()->getFile();
68 $this->img = $imagePage->getDisplayedFile();
69 $this->title = $imagePage->getTitle();
70 $this->imagePage = $imagePage;
71 $this->showThumb = $context->getConfig()->get( MainConfigNames::ShowArchiveThumbnails ) &&
72 $this->img->canRender();
73 $this->setContext( $context );
74 }
75
79 public function getImagePage() {
80 return $this->imagePage;
81 }
82
86 public function getFile() {
87 return $this->img;
88 }
89
93 public function beginImageHistoryList() {
94 // Styles for class=history-deleted
95 $this->getOutput()->addModuleStyles( 'mediawiki.interface.helpers.styles' );
96
97 $html = '';
98 $canDelete = $this->current->isLocal() &&
99 $this->getAuthority()->isAllowedAny( 'delete', 'deletedhistory' );
100
101 foreach ( [
102 '',
103 $canDelete ? '' : null,
104 'filehist-datetime',
105 $this->showThumb ? 'filehist-thumb' : null,
106 'filehist-dimensions',
107 'filehist-user',
108 'filehist-comment',
109 ] as $key ) {
110 if ( $key !== null ) {
111 $html .= Html::element( 'th', [], $key ? $this->msg( $key )->text() : '' );
112 }
113 }
114
115 return Html::openElement( 'table', [ 'class' => 'wikitable filehistory' ] ) . "\n"
116 . Html::rawElement( 'tr', [], $html ) . "\n";
117 }
118
122 public function endImageHistoryList() {
123 return Html::closeElement( 'table' ) . "\n";
124 }
125
133 public function imageHistoryLine( $iscur, $file, $formattedComment ) {
134 $user = $this->getUser();
135 $lang = $this->getLanguage();
136 $linkRenderer = MediaWikiServices::getInstance()->getLinkRenderer();
137 $timestamp = wfTimestamp( TS_MW, $file->getTimestamp() );
138 // @phan-suppress-next-line PhanUndeclaredMethod
139 $img = $iscur ? $file->getName() : $file->getArchiveName();
140 $uploader = $file->getUploader( File::FOR_THIS_USER, $user );
141
142 $local = $this->current->isLocal();
143 $row = '';
144
145 // Deletion link
146 if ( $local && ( $this->getAuthority()->isAllowedAny( 'delete', 'deletedhistory' ) ) ) {
147 $row .= Html::openElement( 'td' );
148 # Link to hide content. Don't show useless link to people who cannot hide revisions.
149 if ( !$iscur && $this->getAuthority()->isAllowed( 'deleterevision' ) ) {
150 // If file is top revision, is missing or locked from this user, don't link
151 if ( !$file->userCan( File::DELETED_RESTRICTED, $user ) || !$file->exists() ) {
152 $row .= Html::check( 'deleterevisions', false, [ 'disabled' => 'disabled' ] );
153 } else {
154 $row .= Html::check( 'ids[' . explode( '!', $img, 2 )[0] . ']', false );
155 }
156 if ( $this->getAuthority()->isAllowed( 'delete' ) ) {
157 $row .= ' ';
158 }
159 }
160 # Link to remove from history
161 if ( $this->getAuthority()->isAllowed( 'delete' ) ) {
162 if ( $file->exists() ) {
163 $row .= $linkRenderer->makeKnownLink(
164 $this->title,
165 $this->msg( $iscur ? 'filehist-deleteall' : 'filehist-deleteone' )->text(),
166 [],
167 [ 'action' => 'delete', 'oldimage' => $iscur ? null : $img ]
168 );
169 } else {
170 // T244567: Non-existing file can not be deleted.
171 $row .= $this->msg( 'filehist-missing' )->escaped();
172 }
173
174 }
175 $row .= Html::closeElement( 'td' );
176 }
177
178 // Reversion link/current indicator
179 $row .= Html::openElement( 'td' );
180 if ( $iscur ) {
181 $row .= $this->msg( 'filehist-current' )->escaped();
182 } elseif ( $local && $this->getAuthority()->probablyCan( 'edit', $this->title )
183 && $this->getAuthority()->probablyCan( 'upload', $this->title )
184 ) {
185 if ( $file->isDeleted( File::DELETED_FILE ) ) {
186 $row .= $this->msg( 'filehist-revert' )->escaped();
187 } elseif ( !$file->exists() ) {
188 // T328112: Lost file, in this case there's no version to revert back to.
189 $row .= $this->msg( 'filehist-missing' )->escaped();
190 } else {
191 $row .= $linkRenderer->makeKnownLink(
192 $this->title,
193 $this->msg( 'filehist-revert' )->text(),
194 [],
195 [
196 'action' => 'revert',
197 'oldimage' => $img,
198 ]
199 );
200 }
201 }
202 $row .= Html::closeElement( 'td' );
203
204 // Date/time and image link
205 $selected = $file->getTimestamp() === $this->img->getTimestamp();
206 $row .= Html::openElement( 'td', [
207 'class' => $selected ? 'filehistory-selected' : null,
208 'style' => 'white-space: nowrap;'
209 ] );
210 if ( !$file->userCan( File::DELETED_FILE, $user ) ) {
211 # Don't link to unviewable files
212 $row .= Html::element( 'span', [ 'class' => 'history-deleted' ],
213 $lang->userTimeAndDate( $timestamp, $user )
214 );
215 } elseif ( $file->isDeleted( File::DELETED_FILE ) ) {
216 $timeAndDate = $lang->userTimeAndDate( $timestamp, $user );
217 if ( $local ) {
218 $this->setPreventClickjacking( true );
219 # Make a link to review the image
220 $url = $linkRenderer->makeKnownLink(
221 SpecialPage::getTitleFor( 'Revisiondelete' ),
222 $timeAndDate,
223 [],
224 [
225 'target' => $this->title->getPrefixedText(),
226 'file' => $img,
227 'token' => $user->getEditToken( $img )
228 ]
229 );
230 } else {
231 $url = htmlspecialchars( $timeAndDate );
232 }
233 $row .= Html::rawElement( 'span', [ 'class' => 'history-deleted' ], $url );
234 } elseif ( !$file->exists() ) {
235 $row .= Html::element( 'span', [ 'class' => 'mw-file-missing' ],
236 $lang->userTimeAndDate( $timestamp, $user )
237 );
238 } else {
239 $url = $iscur ? $this->current->getUrl() : $this->current->getArchiveUrl( $img );
240 $row .= Html::element( 'a', [ 'href' => $url ],
241 $lang->userTimeAndDate( $timestamp, $user )
242 );
243 }
244 $row .= Html::closeElement( 'td' );
245
246 // Thumbnail
247 if ( $this->showThumb ) {
248 $row .= Html::rawElement( 'td', [],
249 $this->getThumbForLine( $file, $iscur ) ?? $this->msg( 'filehist-nothumb' )->escaped()
250 );
251 }
252
253 // Image dimensions + size
254 $row .= Html::openElement( 'td' );
255 $row .= htmlspecialchars( $file->getDimensionsString() );
256 $row .= $this->msg( 'word-separator' )->escaped();
257 $row .= Html::element( 'span', [ 'style' => 'white-space: nowrap;' ],
258 $this->msg( 'parentheses' )->sizeParams( $file->getSize() )->text()
259 );
260 $row .= Html::closeElement( 'td' );
261
262 // Uploading user
263 $row .= Html::openElement( 'td' );
264 // Hide deleted usernames
265 if ( $uploader && $local ) {
266 $row .= Linker::userLink( $uploader->getId(), $uploader->getName() );
267 $row .= Html::rawElement( 'span', [ 'style' => 'white-space: nowrap;' ],
268 Linker::userToolLinks( $uploader->getId(), $uploader->getName() )
269 );
270 } elseif ( $uploader ) {
271 $row .= htmlspecialchars( $uploader->getName() );
272 } else {
273 $row .= Html::element( 'span', [ 'class' => 'history-deleted' ],
274 $this->msg( 'rev-deleted-user' )->text()
275 );
276 }
277 $row .= Html::closeElement( 'td' );
278
279 // Don't show deleted descriptions
280 if ( $file->isDeleted( File::DELETED_COMMENT ) ) {
281 $row .= Html::rawElement( 'td', [],
282 Html::element( 'span', [ 'class' => 'history-deleted' ],
283 $this->msg( 'rev-deleted-comment' )->text()
284 )
285 );
286 } else {
287 $contLang = MediaWikiServices::getInstance()->getContentLanguage();
288 $row .= Html::rawElement( 'td', [ 'dir' => $contLang->getDir() ], $formattedComment );
289 }
290
291 $rowClass = null;
292 $this->getHookRunner()->onImagePageFileHistoryLine( $this, $file, $row, $rowClass );
293
294 return Html::rawElement( 'tr', [ 'class' => $rowClass ], $row ) . "\n";
295 }
296
302 protected function getThumbForLine( $file, $iscur ) {
303 $user = $this->getUser();
304 if ( !$file->allowInlineDisplay() ||
305 $file->isDeleted( File::DELETED_FILE ) ||
306 !$file->userCan( File::DELETED_FILE, $user )
307 ) {
308 return null;
309 }
310
311 $thumbnail = $file->transform(
312 [
313 'width' => '120',
314 'height' => '120',
315 'isFilePageThumb' => $iscur // old revisions are already versioned
316 ]
317 );
318 if ( !$thumbnail ) {
319 return null;
320 }
321
322 $lang = $this->getLanguage();
323 $timestamp = wfTimestamp( TS_MW, $file->getTimestamp() );
324 $alt = $this->msg(
325 'filehist-thumbtext',
326 $lang->userTimeAndDate( $timestamp, $user ),
327 $lang->userDate( $timestamp, $user ),
328 $lang->userTime( $timestamp, $user )
329 )->text();
330 return $thumbnail->toHtml( [ 'alt' => $alt, 'file-link' => true, 'loading' => 'lazy' ] );
331 }
332
337 protected function preventClickjacking( $enable = true ) {
338 $this->preventClickjacking = $enable;
339 }
340
345 protected function setPreventClickjacking( bool $enable ) {
346 $this->preventClickjacking = $enable;
347 }
348
352 public function getPreventClickjacking() {
353 return $this->preventClickjacking;
354 }
355}
getUser()
getAuthority()
wfTimestamp( $outputtype=TS_UNIX, $ts=0)
Get a timestamp string in one of various formats.
getContext()
Gets the context this Article is executed in.
Definition Article.php:1990
getTitle()
Get the title object of the article.
Definition Article.php:267
getPage()
Get the WikiPage object of this instance.
Definition Article.php:277
Implements some public methods and some protected utility functions which are required by multiple ch...
Definition File.php:73
getName()
Return the name of this file.
Definition File.php:341
Builds the image revision log shown on image pages.
preventClickjacking( $enable=true)
getThumbForLine( $file, $iscur)
setPreventClickjacking(bool $enable)
imageHistoryLine( $iscur, $file, $formattedComment)
__construct( $imagePage)
Rendering of file description pages.
Definition ImagePage.php:37
getDisplayedFile()
The simplest way of implementing IContextSource is to hold a RequestContext as a member variable and ...
setContext(IContextSource $context)
msg( $key,... $params)
Get a Message object with context set Parameters are the same as wfMessage()
This class is a collection of static functions that serve two purposes:
Definition Html.php:56
Some internal bits split of from Skin.php.
Definition Linker.php:65
A class containing constants representing the names of configuration variables.
Service locator for MediaWiki core services.
Parent class for all special pages.
Represents a title within MediaWiki.
Definition Title.php:78