MediaWiki master
ImageHistoryList.php
Go to the documentation of this file.
1<?php
7namespace MediaWiki\Page;
8
11use MediaWiki\HookContainer\ProtectedHookAccessorTrait;
18
25 use ProtectedHookAccessorTrait;
26
27 protected Title $title;
28 protected File $img;
30 protected File $current;
31
32 protected bool $showThumb;
34 protected $preventClickjacking = false;
35
39 public function __construct( $imagePage ) {
40 $context = $imagePage->getContext();
41 $this->current = $imagePage->getPage()->getFile();
42 $this->img = $imagePage->getDisplayedFile();
43 $this->title = $imagePage->getTitle();
44 $this->imagePage = $imagePage;
45 $this->showThumb = $context->getConfig()->get( MainConfigNames::ShowArchiveThumbnails ) &&
46 $this->img->canRender();
47 $this->setContext( $context );
48 }
49
53 public function getImagePage() {
54 return $this->imagePage;
55 }
56
60 public function getFile() {
61 return $this->img;
62 }
63
67 public function beginImageHistoryList() {
68 // Styles for class=history-deleted
69 $this->getOutput()->addModuleStyles( 'mediawiki.interface.helpers.styles' );
70
71 $html = '';
72 $canDelete = $this->current->isLocal() &&
73 $this->getAuthority()->isAllowedAny( 'delete', 'deletedhistory' );
74
75 foreach ( [
76 '',
77 $canDelete ? '' : null,
78 'filehist-datetime',
79 $this->showThumb ? 'filehist-thumb' : null,
80 'filehist-dimensions',
81 'filehist-user',
82 'filehist-comment',
83 ] as $key ) {
84 if ( $key !== null ) {
85 $html .= Html::element( 'th', [], $key ? $this->msg( $key )->text() : '' );
86 }
87 }
88
89 return Html::openElement( 'table', [ 'class' => 'wikitable filehistory' ] ) . "\n"
90 . Html::rawElement( 'tr', [], $html ) . "\n";
91 }
92
96 public function endImageHistoryList() {
97 return Html::closeElement( 'table' ) . "\n";
98 }
99
107 public function imageHistoryLine( $iscur, $file, $formattedComment ) {
108 $user = $this->getUser();
109 $lang = $this->getLanguage();
110 $linkRenderer = MediaWikiServices::getInstance()->getLinkRenderer();
111 $timestamp = wfTimestamp( TS_MW, $file->getTimestamp() );
112 // @phan-suppress-next-line PhanUndeclaredMethod
113 $img = $iscur ? $file->getName() : $file->getArchiveName();
114 $uploader = $file->getUploader( File::FOR_THIS_USER, $user );
115
116 $local = $this->current->isLocal();
117 $row = '';
118
119 // Deletion link
120 if ( $local && ( $this->getAuthority()->isAllowedAny( 'delete', 'deletedhistory' ) ) ) {
121 $row .= Html::openElement( 'td' );
122 # Link to hide content. Don't show useless link to people who cannot hide revisions.
123 if ( !$iscur && $this->getAuthority()->isAllowed( 'deleterevision' ) ) {
124 // If file is top revision, is missing or locked from this user, don't link
125 if ( !$file->userCan( File::DELETED_RESTRICTED, $user ) || !$file->exists() ) {
126 $row .= Html::check( 'deleterevisions', false, [ 'disabled' => 'disabled' ] );
127 } else {
128 $row .= Html::check( 'ids[' . explode( '!', $img, 2 )[0] . ']', false );
129 }
130 if ( $this->getAuthority()->isAllowed( 'delete' ) ) {
131 $row .= ' ';
132 }
133 }
134 # Link to remove from history
135 if ( $this->getAuthority()->isAllowed( 'delete' ) ) {
136 if ( $file->exists() ) {
137 $row .= $linkRenderer->makeKnownLink(
138 $this->title,
139 $this->msg( $iscur ? 'filehist-deleteall' : 'filehist-deleteone' )->text(),
140 [],
141 [ 'action' => 'delete', 'oldimage' => $iscur ? null : $img ]
142 );
143 } else {
144 // T244567: Non-existing file can not be deleted.
145 $row .= $this->msg( 'filehist-missing' )->escaped();
146 }
147
148 }
149 $row .= Html::closeElement( 'td' );
150 }
151
152 // Reversion link/current indicator
153 $row .= Html::openElement( 'td' );
154 if ( $iscur ) {
155 $row .= $this->msg( 'filehist-current' )->escaped();
156 } elseif ( $local && $this->getAuthority()->probablyCan( 'edit', $this->title )
157 && $this->getAuthority()->probablyCan( 'upload', $this->title )
158 ) {
159 if ( $file->isDeleted( File::DELETED_FILE ) ) {
160 $row .= $this->msg( 'filehist-revert' )->escaped();
161 } elseif ( !$file->exists() ) {
162 // T328112: Lost file, in this case there's no version to revert back to.
163 $row .= $this->msg( 'filehist-missing' )->escaped();
164 } else {
165 $row .= $linkRenderer->makeKnownLink(
166 $this->title,
167 $this->msg( 'filehist-revert' )->text(),
168 [],
169 [
170 'action' => 'revert',
171 'oldimage' => $img,
172 ]
173 );
174 }
175 }
176 $row .= Html::closeElement( 'td' );
177
178 // Date/time and image link
179 $selected = $file->getTimestamp() === $this->img->getTimestamp();
180 $row .= Html::openElement( 'td', [
181 'class' => $selected ? 'filehistory-selected' : null,
182 'style' => 'white-space: nowrap;'
183 ] );
184 if ( !$file->userCan( File::DELETED_FILE, $user ) ) {
185 # Don't link to unviewable files
186 $row .= Html::element( 'span', [ 'class' => 'history-deleted' ],
187 $lang->userTimeAndDate( $timestamp, $user )
188 );
189 } elseif ( $file->isDeleted( File::DELETED_FILE ) ) {
190 $timeAndDate = $lang->userTimeAndDate( $timestamp, $user );
191 if ( $local ) {
192 $this->setPreventClickjacking( true );
193 # Make a link to review the image
194 $url = $linkRenderer->makeKnownLink(
195 SpecialPage::getTitleFor( 'Revisiondelete' ),
196 $timeAndDate,
197 [],
198 [
199 'target' => $this->title->getPrefixedText(),
200 'file' => $img,
201 'token' => $user->getEditToken( $img )
202 ]
203 );
204 } else {
205 $url = htmlspecialchars( $timeAndDate );
206 }
207 $row .= Html::rawElement( 'span', [ 'class' => 'history-deleted' ], $url );
208 } elseif ( !$file->exists() ) {
209 $row .= Html::element( 'span', [ 'class' => 'mw-file-missing' ],
210 $lang->userTimeAndDate( $timestamp, $user )
211 );
212 } else {
213 $url = $iscur ? $this->current->getUrl() : $this->current->getArchiveUrl( $img );
214 $row .= Html::element( 'a', [ 'href' => $url ],
215 $lang->userTimeAndDate( $timestamp, $user )
216 );
217 }
218 $row .= Html::closeElement( 'td' );
219
220 // Thumbnail
221 if ( $this->showThumb ) {
222 $row .= Html::rawElement( 'td', [],
223 $this->getThumbForLine( $file, $iscur ) ?? $this->msg( 'filehist-nothumb' )->escaped()
224 );
225 }
226
227 // Image dimensions + size
228 $row .= Html::openElement( 'td' );
229 $row .= htmlspecialchars( $file->getDimensionsString() );
230 $row .= $this->msg( 'word-separator' )->escaped();
231 $row .= Html::element( 'span', [ 'style' => 'white-space: nowrap;' ],
232 $this->msg( 'parentheses' )->sizeParams( $file->getSize() )->text()
233 );
234 $row .= Html::closeElement( 'td' );
235
236 // Uploading user
237 $row .= Html::openElement( 'td' );
238 // Hide deleted usernames
239 if ( $uploader ) {
240 $row .= Linker::userLink( $uploader->getId(), $uploader->getName() );
241 if ( $local ) {
242 $row .= Html::rawElement( 'span', [ 'style' => 'white-space: nowrap;' ],
243 Linker::userToolLinks( $uploader->getId(), $uploader->getName() )
244 );
245 }
246 } else {
247 $row .= Html::element( 'span', [ 'class' => 'history-deleted' ],
248 $this->msg( 'rev-deleted-user' )->text()
249 );
250 }
251 $row .= Html::closeElement( 'td' );
252
253 // Don't show deleted descriptions
254 if ( $file->isDeleted( File::DELETED_COMMENT ) ) {
255 $row .= Html::rawElement( 'td', [],
256 Html::element( 'span', [ 'class' => 'history-deleted' ],
257 $this->msg( 'rev-deleted-comment' )->text()
258 )
259 );
260 } else {
261 $contLang = MediaWikiServices::getInstance()->getContentLanguage();
262 $row .= Html::rawElement( 'td', [ 'dir' => $contLang->getDir() ], $formattedComment );
263 }
264
265 $rowClass = null;
266 $this->getHookRunner()->onImagePageFileHistoryLine( $this, $file, $row, $rowClass );
267
268 return Html::rawElement( 'tr', [ 'class' => $rowClass ], $row ) . "\n";
269 }
270
276 protected function getThumbForLine( $file, $iscur ) {
277 $user = $this->getUser();
278 if ( !$file->allowInlineDisplay() ||
279 $file->isDeleted( File::DELETED_FILE ) ||
280 !$file->userCan( File::DELETED_FILE, $user )
281 ) {
282 return null;
283 }
284
285 $thumbnail = $file->transform(
286 [
287 'width' => '120',
288 'height' => '120',
289 'isFilePageThumb' => $iscur // old revisions are already versioned
290 ]
291 );
292 if ( !$thumbnail ) {
293 return null;
294 }
295
296 $lang = $this->getLanguage();
297 $timestamp = wfTimestamp( TS_MW, $file->getTimestamp() );
298 $alt = $this->msg(
299 'filehist-thumbtext',
300 $lang->userTimeAndDate( $timestamp, $user ),
301 $lang->userDate( $timestamp, $user ),
302 $lang->userTime( $timestamp, $user )
303 )->text();
304 return $thumbnail->toHtml( [ 'alt' => $alt, 'file-link' => true, 'loading' => 'lazy' ] );
305 }
306
311 protected function preventClickjacking( $enable = true ) {
312 $this->preventClickjacking = $enable;
313 }
314
319 protected function setPreventClickjacking( bool $enable ) {
320 $this->preventClickjacking = $enable;
321 }
322
326 public function getPreventClickjacking() {
328 }
329}
330
332class_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:79
getName()
Return the name of this file.
Definition File.php:347
This class is a collection of static functions that serve two purposes:
Definition Html.php:43
Some internal bits split of from Skin.php.
Definition Linker.php:47
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:245
getTitle()
Get the title object of the article.
Definition Article.php:235
getContext()
Gets the context this Article is executed in.
Definition Article.php:2123
Builds the image revision log shown on image pages.
imageHistoryLine( $iscur, $file, $formattedComment)
Rendering of file description pages.
Definition ImagePage.php:34
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:69
element(SerializerNode $parent, SerializerNode $node, $contents)