MediaWiki master
ImageHistoryList.php
Go to the documentation of this file.
1<?php
21namespace MediaWiki\Page;
22
25use MediaWiki\HookContainer\ProtectedHookAccessorTrait;
32
39 use ProtectedHookAccessorTrait;
40
41 protected Title $title;
42 protected File $img;
44 protected File $current;
45
46 protected bool $showThumb;
48 protected $preventClickjacking = false;
49
53 public function __construct( $imagePage ) {
54 $context = $imagePage->getContext();
55 $this->current = $imagePage->getPage()->getFile();
56 $this->img = $imagePage->getDisplayedFile();
57 $this->title = $imagePage->getTitle();
58 $this->imagePage = $imagePage;
59 $this->showThumb = $context->getConfig()->get( MainConfigNames::ShowArchiveThumbnails ) &&
60 $this->img->canRender();
61 $this->setContext( $context );
62 }
63
67 public function getImagePage() {
68 return $this->imagePage;
69 }
70
74 public function getFile() {
75 return $this->img;
76 }
77
81 public function beginImageHistoryList() {
82 // Styles for class=history-deleted
83 $this->getOutput()->addModuleStyles( 'mediawiki.interface.helpers.styles' );
84
85 $html = '';
86 $canDelete = $this->current->isLocal() &&
87 $this->getAuthority()->isAllowedAny( 'delete', 'deletedhistory' );
88
89 foreach ( [
90 '',
91 $canDelete ? '' : null,
92 'filehist-datetime',
93 $this->showThumb ? 'filehist-thumb' : null,
94 'filehist-dimensions',
95 'filehist-user',
96 'filehist-comment',
97 ] as $key ) {
98 if ( $key !== null ) {
99 $html .= Html::element( 'th', [], $key ? $this->msg( $key )->text() : '' );
100 }
101 }
102
103 return Html::openElement( 'table', [ 'class' => 'wikitable filehistory' ] ) . "\n"
104 . Html::rawElement( 'tr', [], $html ) . "\n";
105 }
106
110 public function endImageHistoryList() {
111 return Html::closeElement( 'table' ) . "\n";
112 }
113
121 public function imageHistoryLine( $iscur, $file, $formattedComment ) {
122 $user = $this->getUser();
123 $lang = $this->getLanguage();
124 $linkRenderer = MediaWikiServices::getInstance()->getLinkRenderer();
125 $timestamp = wfTimestamp( TS_MW, $file->getTimestamp() );
126 // @phan-suppress-next-line PhanUndeclaredMethod
127 $img = $iscur ? $file->getName() : $file->getArchiveName();
128 $uploader = $file->getUploader( File::FOR_THIS_USER, $user );
129
130 $local = $this->current->isLocal();
131 $row = '';
132
133 // Deletion link
134 if ( $local && ( $this->getAuthority()->isAllowedAny( 'delete', 'deletedhistory' ) ) ) {
135 $row .= Html::openElement( 'td' );
136 # Link to hide content. Don't show useless link to people who cannot hide revisions.
137 if ( !$iscur && $this->getAuthority()->isAllowed( 'deleterevision' ) ) {
138 // If file is top revision, is missing or locked from this user, don't link
139 if ( !$file->userCan( File::DELETED_RESTRICTED, $user ) || !$file->exists() ) {
140 $row .= Html::check( 'deleterevisions', false, [ 'disabled' => 'disabled' ] );
141 } else {
142 $row .= Html::check( 'ids[' . explode( '!', $img, 2 )[0] . ']', false );
143 }
144 if ( $this->getAuthority()->isAllowed( 'delete' ) ) {
145 $row .= ' ';
146 }
147 }
148 # Link to remove from history
149 if ( $this->getAuthority()->isAllowed( 'delete' ) ) {
150 if ( $file->exists() ) {
151 $row .= $linkRenderer->makeKnownLink(
152 $this->title,
153 $this->msg( $iscur ? 'filehist-deleteall' : 'filehist-deleteone' )->text(),
154 [],
155 [ 'action' => 'delete', 'oldimage' => $iscur ? null : $img ]
156 );
157 } else {
158 // T244567: Non-existing file can not be deleted.
159 $row .= $this->msg( 'filehist-missing' )->escaped();
160 }
161
162 }
163 $row .= Html::closeElement( 'td' );
164 }
165
166 // Reversion link/current indicator
167 $row .= Html::openElement( 'td' );
168 if ( $iscur ) {
169 $row .= $this->msg( 'filehist-current' )->escaped();
170 } elseif ( $local && $this->getAuthority()->probablyCan( 'edit', $this->title )
171 && $this->getAuthority()->probablyCan( 'upload', $this->title )
172 ) {
173 if ( $file->isDeleted( File::DELETED_FILE ) ) {
174 $row .= $this->msg( 'filehist-revert' )->escaped();
175 } elseif ( !$file->exists() ) {
176 // T328112: Lost file, in this case there's no version to revert back to.
177 $row .= $this->msg( 'filehist-missing' )->escaped();
178 } else {
179 $row .= $linkRenderer->makeKnownLink(
180 $this->title,
181 $this->msg( 'filehist-revert' )->text(),
182 [],
183 [
184 'action' => 'revert',
185 'oldimage' => $img,
186 ]
187 );
188 }
189 }
190 $row .= Html::closeElement( 'td' );
191
192 // Date/time and image link
193 $selected = $file->getTimestamp() === $this->img->getTimestamp();
194 $row .= Html::openElement( 'td', [
195 'class' => $selected ? 'filehistory-selected' : null,
196 'style' => 'white-space: nowrap;'
197 ] );
198 if ( !$file->userCan( File::DELETED_FILE, $user ) ) {
199 # Don't link to unviewable files
200 $row .= Html::element( 'span', [ 'class' => 'history-deleted' ],
201 $lang->userTimeAndDate( $timestamp, $user )
202 );
203 } elseif ( $file->isDeleted( File::DELETED_FILE ) ) {
204 $timeAndDate = $lang->userTimeAndDate( $timestamp, $user );
205 if ( $local ) {
206 $this->setPreventClickjacking( true );
207 # Make a link to review the image
208 $url = $linkRenderer->makeKnownLink(
209 SpecialPage::getTitleFor( 'Revisiondelete' ),
210 $timeAndDate,
211 [],
212 [
213 'target' => $this->title->getPrefixedText(),
214 'file' => $img,
215 'token' => $user->getEditToken( $img )
216 ]
217 );
218 } else {
219 $url = htmlspecialchars( $timeAndDate );
220 }
221 $row .= Html::rawElement( 'span', [ 'class' => 'history-deleted' ], $url );
222 } elseif ( !$file->exists() ) {
223 $row .= Html::element( 'span', [ 'class' => 'mw-file-missing' ],
224 $lang->userTimeAndDate( $timestamp, $user )
225 );
226 } else {
227 $url = $iscur ? $this->current->getUrl() : $this->current->getArchiveUrl( $img );
228 $row .= Html::element( 'a', [ 'href' => $url ],
229 $lang->userTimeAndDate( $timestamp, $user )
230 );
231 }
232 $row .= Html::closeElement( 'td' );
233
234 // Thumbnail
235 if ( $this->showThumb ) {
236 $row .= Html::rawElement( 'td', [],
237 $this->getThumbForLine( $file, $iscur ) ?? $this->msg( 'filehist-nothumb' )->escaped()
238 );
239 }
240
241 // Image dimensions + size
242 $row .= Html::openElement( 'td' );
243 $row .= htmlspecialchars( $file->getDimensionsString() );
244 $row .= $this->msg( 'word-separator' )->escaped();
245 $row .= Html::element( 'span', [ 'style' => 'white-space: nowrap;' ],
246 $this->msg( 'parentheses' )->sizeParams( $file->getSize() )->text()
247 );
248 $row .= Html::closeElement( 'td' );
249
250 // Uploading user
251 $row .= Html::openElement( 'td' );
252 // Hide deleted usernames
253 if ( $uploader ) {
254 $row .= Linker::userLink( $uploader->getId(), $uploader->getName() );
255 if ( $local ) {
256 $row .= Html::rawElement( 'span', [ 'style' => 'white-space: nowrap;' ],
257 Linker::userToolLinks( $uploader->getId(), $uploader->getName() )
258 );
259 }
260 } else {
261 $row .= Html::element( 'span', [ 'class' => 'history-deleted' ],
262 $this->msg( 'rev-deleted-user' )->text()
263 );
264 }
265 $row .= Html::closeElement( 'td' );
266
267 // Don't show deleted descriptions
268 if ( $file->isDeleted( File::DELETED_COMMENT ) ) {
269 $row .= Html::rawElement( 'td', [],
270 Html::element( 'span', [ 'class' => 'history-deleted' ],
271 $this->msg( 'rev-deleted-comment' )->text()
272 )
273 );
274 } else {
275 $contLang = MediaWikiServices::getInstance()->getContentLanguage();
276 $row .= Html::rawElement( 'td', [ 'dir' => $contLang->getDir() ], $formattedComment );
277 }
278
279 $rowClass = null;
280 $this->getHookRunner()->onImagePageFileHistoryLine( $this, $file, $row, $rowClass );
281
282 return Html::rawElement( 'tr', [ 'class' => $rowClass ], $row ) . "\n";
283 }
284
290 protected function getThumbForLine( $file, $iscur ) {
291 $user = $this->getUser();
292 if ( !$file->allowInlineDisplay() ||
293 $file->isDeleted( File::DELETED_FILE ) ||
294 !$file->userCan( File::DELETED_FILE, $user )
295 ) {
296 return null;
297 }
298
299 $thumbnail = $file->transform(
300 [
301 'width' => '120',
302 'height' => '120',
303 'isFilePageThumb' => $iscur // old revisions are already versioned
304 ]
305 );
306 if ( !$thumbnail ) {
307 return null;
308 }
309
310 $lang = $this->getLanguage();
311 $timestamp = wfTimestamp( TS_MW, $file->getTimestamp() );
312 $alt = $this->msg(
313 'filehist-thumbtext',
314 $lang->userTimeAndDate( $timestamp, $user ),
315 $lang->userDate( $timestamp, $user ),
316 $lang->userTime( $timestamp, $user )
317 )->text();
318 return $thumbnail->toHtml( [ 'alt' => $alt, 'file-link' => true, 'loading' => 'lazy' ] );
319 }
320
325 protected function preventClickjacking( $enable = true ) {
326 $this->preventClickjacking = $enable;
327 }
328
333 protected function setPreventClickjacking( bool $enable ) {
334 $this->preventClickjacking = $enable;
335 }
336
340 public function getPreventClickjacking() {
342 }
343}
344
346class_alias( ImageHistoryList::class, 'ImageHistoryList' );
wfTimestamp( $outputtype=TS_UNIX, $ts=0)
Get a timestamp string in one of various formats.
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()
Implements some public methods and some protected utility functions which are required by multiple ch...
Definition File.php:93
getName()
Return the name of this file.
Definition File.php:361
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:62
A class containing constants representing the names of configuration variables.
const ShowArchiveThumbnails
Name constant for the ShowArchiveThumbnails setting, for use with Config::get()
Service locator for MediaWiki core services.
static getInstance()
Returns the global default instance of the top level service locator.
getPage()
Get the WikiPage object of this instance.
Definition Article.php:263
getTitle()
Get the title object of the article.
Definition Article.php:253
getContext()
Gets the context this Article is executed in.
Definition Article.php:2066
Builds the image revision log shown on image pages.
imageHistoryLine( $iscur, $file, $formattedComment)
Rendering of file description pages.
Definition ImagePage.php:46
Parent class for all special pages.
static getTitleFor( $name, $subpage=false, $fragment='')
Get a localised Title object for a specified special page name If you don't need a full Title object,...
Represents a title within MediaWiki.
Definition Title.php:78
element(SerializerNode $parent, SerializerNode $node, $contents)