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 $img = $iscur ?
$file->getName() : $file->getArchiveName();
124 $userId = $file->getUser( 'id' );
125 $userText = $file->getUser( 'text' );
126 $description = $file->getDescription( File
::FOR_THIS_USER
, $user );
128 $local = $this->current
->isLocal();
129 $row = $selected = '';
132 if ( $local && ( $pm->userHasAnyRight( $user, 'delete', 'deletedhistory' ) )
135 # Link to remove from history
136 if ( $user->isAllowed( 'delete' ) ) {
137 $q = [ 'action' => 'delete' ];
139 $q['oldimage'] = $img;
141 $row .= Linker
::linkKnown(
143 $this->msg( $iscur ?
'filehist-deleteall' : 'filehist-deleteone' )->escaped(),
147 # Link to hide content. Don't show useless link to people who cannot hide revisions.
148 $canHide = $user->isAllowed( 'deleterevision' );
149 if ( $canHide ||
( $user->isAllowed( 'deletedhistory' ) && $file->getVisibility() ) ) {
150 if ( $user->isAllowed( 'delete' ) ) {
153 // If file is top revision or locked from this user, don't link
154 if ( $iscur ||
!$file->userCan( File
::DELETED_RESTRICTED
, $user ) ) {
155 $del = Linker
::revDeleteLinkDisabled( $canHide );
157 list( $ts, ) = explode( '!', $img, 2 );
159 'type' => 'oldimage',
160 'target' => $this->title
->getPrefixedText(),
163 $del = Linker
::revDeleteLink( $query,
164 $file->isDeleted( File
::DELETED_RESTRICTED
), $canHide );
171 // Reversion link/current indicator
174 $row .= $this->msg( 'filehist-current' )->escaped();
175 } elseif ( $local && $pm->quickUserCan( 'edit', $user, $this->title
)
176 && $pm->quickUserCan( 'upload', $user, $this->title
)
178 if ( $file->isDeleted( File
::DELETED_FILE
) ) {
179 $row .= $this->msg( 'filehist-revert' )->escaped();
181 $row .= Linker
::linkKnown(
183 $this->msg( 'filehist-revert' )->escaped(),
186 'action' => 'revert',
194 // Date/time and image link
195 if ( $file->getTimestamp() === $this->img
->getTimestamp() ) {
196 $selected = "class='filehistory-selected'";
198 $row .= "<td $selected style='white-space: nowrap;'>";
199 if ( !$file->userCan( File
::DELETED_FILE
, $user ) ) {
200 # Don't link to unviewable files
201 $row .= Html
::element( 'span', [ 'class' => 'history-deleted' ],
202 $lang->userTimeAndDate( $timestamp, $user )
204 } elseif ( $file->isDeleted( File
::DELETED_FILE
) ) {
205 $timeAndDate = htmlspecialchars( $lang->userTimeAndDate( $timestamp, $user ) );
207 $this->preventClickjacking();
208 $revdel = SpecialPage
::getTitleFor( 'Revisiondelete' );
209 # Make a link to review the image
210 $url = Linker
::linkKnown(
215 'target' => $this->title
->getPrefixedText(),
217 'token' => $user->getEditToken( $img )
223 $row .= '<span class="history-deleted">' . $url . '</span>';
224 } elseif ( !$file->exists() ) {
225 $row .= Html
::element( 'span', [ 'class' => 'mw-file-missing' ],
226 $lang->userTimeAndDate( $timestamp, $user )
229 $url = $iscur ?
$this->current
->getUrl() : $this->current
->getArchiveUrl( $img );
230 $row .= Xml
::element(
233 $lang->userTimeAndDate( $timestamp, $user )
239 if ( $this->showThumb
) {
240 $row .= '<td>' . $this->getThumbForLine( $file ) . '</td>';
243 // Image dimensions + size
245 $row .= htmlspecialchars( $file->getDimensionsString() );
246 $row .= $this->msg( 'word-separator' )->escaped();
247 $row .= '<span style="white-space: nowrap;">';
248 $row .= $this->msg( 'parentheses' )->sizeParams( $file->getSize() )->escaped();
254 // Hide deleted usernames
255 if ( $file->isDeleted( File
::DELETED_USER
) ) {
256 $row .= '<span class="history-deleted">'
257 . $this->msg( 'rev-deleted-user' )->escaped() . '</span>';
260 $row .= Linker
::userLink( $userId, $userText );
261 $row .= '<span style="white-space: nowrap;">';
262 $row .= Linker
::userToolLinks( $userId, $userText );
265 $row .= htmlspecialchars( $userText );
270 // Don't show deleted descriptions
271 if ( $file->isDeleted( File
::DELETED_COMMENT
) ) {
272 $row .= '<td><span class="history-deleted">' .
273 $this->msg( 'rev-deleted-comment' )->escaped() . '</span></td>';
275 $contLang = MediaWikiServices
::getInstance()->getContentLanguage();
276 $row .= Html
::rawElement(
278 [ 'dir' => $contLang->getDir() ],
279 Linker
::formatComment( $description, $this->title
)
284 Hooks
::run( 'ImagePageFileHistoryLine', [ $this, $file, &$row, &$rowClass ] );
285 $classAttr = $rowClass ?
" class='$rowClass'" : '';
287 return "<tr{$classAttr}>{$row}</tr>\n";
294 protected function getThumbForLine( $file ) {
295 $lang = $this->getLanguage();
296 $user = $this->getUser();
297 if ( $file->allowInlineDisplay() && $file->userCan( File
::DELETED_FILE
, $user )
298 && !$file->isDeleted( File
::DELETED_FILE
)
304 $timestamp = wfTimestamp( TS_MW
, $file->getTimestamp() );
306 $thumbnail = $file->transform( $params );
308 'alt' => $this->msg( 'filehist-thumbtext',
309 $lang->userTimeAndDate( $timestamp, $user ),
310 $lang->userDate( $timestamp, $user ),
311 $lang->userTime( $timestamp, $user ) )->text(),
316 return $this->msg( 'filehist-nothumb' )->escaped();
319 return $thumbnail->toHtml( $options );
321 return $this->msg( 'filehist-nothumb' )->escaped();
326 * @param bool $enable
328 protected function preventClickjacking( $enable = true ) {
329 $this->preventClickjacking
= $enable;
335 public function getPreventClickjacking() {
336 return $this->preventClickjacking
;