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.
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.
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
21 use MediaWiki\MediaWikiServices
;
24 * Builds the image revision log shown on image pages
28 class ImageHistoryList
extends ContextSource
{
50 protected $repo, $showThumb;
51 protected $preventClickjacking = false;
54 * @param ImagePage $imagePage
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 );
70 public function getImagePage() {
71 return $this->imagePage
;
77 public function getFile() {
82 * @param string $navLinks
85 public function beginImageHistoryList( $navLinks = '' ) {
86 return Xml
::element( 'h2', [ 'id' => 'filehistory' ], $this->msg( 'filehist' )->text() )
88 . "<div id=\"mw-imagepage-section-filehistory\">\n"
89 . $this->msg( 'filehist-help' )->parseAsBlock()
91 . Xml
::openElement( 'table', [ 'class' => 'wikitable filehistory' ] ) . "\n"
93 . ( $this->current
->isLocal()
94 && ( MediaWikiServices
::getInstance()
95 ->getPermissionManager()
96 ->userHasAnyRight( $this->getUser(), 'delete', 'deletedhistory' ) ) ?
'<th></th>' : '' )
97 . '<th>' . $this->msg( 'filehist-datetime' )->escaped() . '</th>'
98 . ( $this->showThumb ?
'<th>' . $this->msg( 'filehist-thumb' )->escaped() . '</th>' : '' )
99 . '<th>' . $this->msg( 'filehist-dimensions' )->escaped() . '</th>'
100 . '<th>' . $this->msg( 'filehist-user' )->escaped() . '</th>'
101 . '<th>' . $this->msg( 'filehist-comment' )->escaped() . '</th>'
106 * @param string $navLinks
109 public function endImageHistoryList( $navLinks = '' ) {
110 return "</table>\n$navLinks\n</div>\n";
118 public function imageHistoryLine( $iscur, $file ) {
119 $user = $this->getUser();
120 $lang = $this->getLanguage();
121 $pm = MediaWikiServices
::getInstance()->getPermissionManager();
122 $timestamp = wfTimestamp( TS_MW
, $file->getTimestamp() );
123 // @phan-suppress-next-line PhanUndeclaredMethod
124 $img = $iscur ?
$file->getName() : $file->getArchiveName();
125 $userId = $file->getUser( 'id' );
126 $userText = $file->getUser( 'text' );
127 $description = $file->getDescription( File
::FOR_THIS_USER
, $user );
129 $local = $this->current
->isLocal();
130 $row = $selected = '';
133 if ( $local && ( $pm->userHasAnyRight( $user, 'delete', 'deletedhistory' ) )
136 # Link to remove from history
137 if ( $user->isAllowed( 'delete' ) ) {
138 $q = [ 'action' => 'delete' ];
140 $q['oldimage'] = $img;
142 $row .= Linker
::linkKnown(
144 $this->msg( $iscur ?
'filehist-deleteall' : 'filehist-deleteone' )->escaped(),
148 # Link to hide content. Don't show useless link to people who cannot hide revisions.
149 $canHide = $user->isAllowed( 'deleterevision' );
150 if ( $canHide ||
( $user->isAllowed( 'deletedhistory' ) && $file->getVisibility() ) ) {
151 if ( $user->isAllowed( 'delete' ) ) {
154 // If file is top revision or locked from this user, don't link
155 if ( $iscur ||
!$file->userCan( File
::DELETED_RESTRICTED
, $user ) ) {
156 $del = Linker
::revDeleteLinkDisabled( $canHide );
158 list( $ts, ) = explode( '!', $img, 2 );
160 'type' => 'oldimage',
161 'target' => $this->title
->getPrefixedText(),
164 $del = Linker
::revDeleteLink( $query,
165 $file->isDeleted( File
::DELETED_RESTRICTED
), $canHide );
172 // Reversion link/current indicator
175 $row .= $this->msg( 'filehist-current' )->escaped();
176 } elseif ( $local && $pm->quickUserCan( 'edit', $user, $this->title
)
177 && $pm->quickUserCan( 'upload', $user, $this->title
)
179 if ( $file->isDeleted( File
::DELETED_FILE
) ) {
180 $row .= $this->msg( 'filehist-revert' )->escaped();
182 $row .= Linker
::linkKnown(
184 $this->msg( 'filehist-revert' )->escaped(),
187 'action' => 'revert',
195 // Date/time and image link
196 if ( $file->getTimestamp() === $this->img
->getTimestamp() ) {
197 $selected = "class='filehistory-selected'";
199 $row .= "<td $selected style='white-space: nowrap;'>";
200 if ( !$file->userCan( File
::DELETED_FILE
, $user ) ) {
201 # Don't link to unviewable files
202 $row .= Html
::element( 'span', [ 'class' => 'history-deleted' ],
203 $lang->userTimeAndDate( $timestamp, $user )
205 } elseif ( $file->isDeleted( File
::DELETED_FILE
) ) {
206 $timeAndDate = htmlspecialchars( $lang->userTimeAndDate( $timestamp, $user ) );
208 $this->preventClickjacking();
209 $revdel = SpecialPage
::getTitleFor( 'Revisiondelete' );
210 # Make a link to review the image
211 $url = Linker
::linkKnown(
216 'target' => $this->title
->getPrefixedText(),
218 'token' => $user->getEditToken( $img )
224 $row .= '<span class="history-deleted">' . $url . '</span>';
225 } elseif ( !$file->exists() ) {
226 $row .= Html
::element( 'span', [ 'class' => 'mw-file-missing' ],
227 $lang->userTimeAndDate( $timestamp, $user )
230 $url = $iscur ?
$this->current
->getUrl() : $this->current
->getArchiveUrl( $img );
231 $row .= Xml
::element(
234 $lang->userTimeAndDate( $timestamp, $user )
240 if ( $this->showThumb
) {
241 $row .= '<td>' . $this->getThumbForLine( $file ) . '</td>';
244 // Image dimensions + size
246 $row .= htmlspecialchars( $file->getDimensionsString() );
247 $row .= $this->msg( 'word-separator' )->escaped();
248 $row .= '<span style="white-space: nowrap;">';
249 $row .= $this->msg( 'parentheses' )->sizeParams( $file->getSize() )->escaped();
255 // Hide deleted usernames
256 if ( $file->isDeleted( File
::DELETED_USER
) ) {
257 $row .= '<span class="history-deleted">'
258 . $this->msg( 'rev-deleted-user' )->escaped() . '</span>';
261 $row .= Linker
::userLink( $userId, $userText );
262 $row .= '<span style="white-space: nowrap;">';
263 $row .= Linker
::userToolLinks( $userId, $userText );
266 $row .= htmlspecialchars( $userText );
271 // Don't show deleted descriptions
272 if ( $file->isDeleted( File
::DELETED_COMMENT
) ) {
273 $row .= '<td><span class="history-deleted">' .
274 $this->msg( 'rev-deleted-comment' )->escaped() . '</span></td>';
276 $contLang = MediaWikiServices
::getInstance()->getContentLanguage();
277 $row .= Html
::rawElement(
279 [ 'dir' => $contLang->getDir() ],
280 Linker
::formatComment( $description, $this->title
)
285 Hooks
::run( 'ImagePageFileHistoryLine', [ $this, $file, &$row, &$rowClass ] );
286 $classAttr = $rowClass ?
" class='$rowClass'" : '';
288 return "<tr{$classAttr}>{$row}</tr>\n";
295 protected function getThumbForLine( $file ) {
296 $lang = $this->getLanguage();
297 $user = $this->getUser();
298 if ( $file->allowInlineDisplay() && $file->userCan( File
::DELETED_FILE
, $user )
299 && !$file->isDeleted( File
::DELETED_FILE
)
305 $timestamp = wfTimestamp( TS_MW
, $file->getTimestamp() );
307 $thumbnail = $file->transform( $params );
309 'alt' => $this->msg( 'filehist-thumbtext',
310 $lang->userTimeAndDate( $timestamp, $user ),
311 $lang->userDate( $timestamp, $user ),
312 $lang->userTime( $timestamp, $user ) )->text(),
317 return $this->msg( 'filehist-nothumb' )->escaped();
320 return $thumbnail->toHtml( $options );
322 return $this->msg( 'filehist-nothumb' )->escaped();
327 * @param bool $enable
329 protected function preventClickjacking( $enable = true ) {
330 $this->preventClickjacking
= $enable;
336 public function getPreventClickjacking() {
337 return $this->preventClickjacking
;