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 && ( $this->getUser()->isAllowedAny( 'delete', 'deletedhistory' ) ) ?
'<th></th>' : '' )
95 . '<th>' . $this->msg( 'filehist-datetime' )->escaped() . '</th>'
96 . ( $this->showThumb ?
'<th>' . $this->msg( 'filehist-thumb' )->escaped() . '</th>' : '' )
97 . '<th>' . $this->msg( 'filehist-dimensions' )->escaped() . '</th>'
98 . '<th>' . $this->msg( 'filehist-user' )->escaped() . '</th>'
99 . '<th>' . $this->msg( 'filehist-comment' )->escaped() . '</th>'
104 * @param string $navLinks
107 public function endImageHistoryList( $navLinks = '' ) {
108 return "</table>\n$navLinks\n</div>\n";
116 public function imageHistoryLine( $iscur, $file ) {
117 $user = $this->getUser();
118 $lang = $this->getLanguage();
119 $timestamp = wfTimestamp( TS_MW
, $file->getTimestamp() );
120 $img = $iscur ?
$file->getName() : $file->getArchiveName();
121 $userId = $file->getUser( 'id' );
122 $userText = $file->getUser( 'text' );
123 $description = $file->getDescription( File
::FOR_THIS_USER
, $user );
125 $local = $this->current
->isLocal();
126 $row = $selected = '';
129 if ( $local && ( $user->isAllowedAny( 'delete', 'deletedhistory' ) ) ) {
131 # Link to remove from history
132 if ( $user->isAllowed( 'delete' ) ) {
133 $q = [ 'action' => 'delete' ];
135 $q['oldimage'] = $img;
137 $row .= Linker
::linkKnown(
139 $this->msg( $iscur ?
'filehist-deleteall' : 'filehist-deleteone' )->escaped(),
143 # Link to hide content. Don't show useless link to people who cannot hide revisions.
144 $canHide = $user->isAllowed( 'deleterevision' );
145 if ( $canHide ||
( $user->isAllowed( 'deletedhistory' ) && $file->getVisibility() ) ) {
146 if ( $user->isAllowed( 'delete' ) ) {
149 // If file is top revision or locked from this user, don't link
150 if ( $iscur ||
!$file->userCan( File
::DELETED_RESTRICTED
, $user ) ) {
151 $del = Linker
::revDeleteLinkDisabled( $canHide );
153 list( $ts, ) = explode( '!', $img, 2 );
155 'type' => 'oldimage',
156 'target' => $this->title
->getPrefixedText(),
159 $del = Linker
::revDeleteLink( $query,
160 $file->isDeleted( File
::DELETED_RESTRICTED
), $canHide );
167 // Reversion link/current indicator
170 $row .= $this->msg( 'filehist-current' )->escaped();
171 } elseif ( $local && $this->title
->quickUserCan( 'edit', $user )
172 && $this->title
->quickUserCan( 'upload', $user )
174 if ( $file->isDeleted( File
::DELETED_FILE
) ) {
175 $row .= $this->msg( 'filehist-revert' )->escaped();
177 $row .= Linker
::linkKnown(
179 $this->msg( 'filehist-revert' )->escaped(),
182 'action' => 'revert',
190 // Date/time and image link
191 if ( $file->getTimestamp() === $this->img
->getTimestamp() ) {
192 $selected = "class='filehistory-selected'";
194 $row .= "<td $selected style='white-space: nowrap;'>";
195 if ( !$file->userCan( File
::DELETED_FILE
, $user ) ) {
196 # Don't link to unviewable files
197 $row .= Html
::element( 'span', [ 'class' => 'history-deleted' ],
198 $lang->userTimeAndDate( $timestamp, $user )
200 } elseif ( $file->isDeleted( File
::DELETED_FILE
) ) {
201 $timeAndDate = htmlspecialchars( $lang->userTimeAndDate( $timestamp, $user ) );
203 $this->preventClickjacking();
204 $revdel = SpecialPage
::getTitleFor( 'Revisiondelete' );
205 # Make a link to review the image
206 $url = Linker
::linkKnown(
211 'target' => $this->title
->getPrefixedText(),
213 'token' => $user->getEditToken( $img )
219 $row .= '<span class="history-deleted">' . $url . '</span>';
220 } elseif ( !$file->exists() ) {
221 $row .= Html
::element( 'span', [ 'class' => 'mw-file-missing' ],
222 $lang->userTimeAndDate( $timestamp, $user )
225 $url = $iscur ?
$this->current
->getUrl() : $this->current
->getArchiveUrl( $img );
226 $row .= Xml
::element(
229 $lang->userTimeAndDate( $timestamp, $user )
235 if ( $this->showThumb
) {
236 $row .= '<td>' . $this->getThumbForLine( $file ) . '</td>';
239 // Image dimensions + size
241 $row .= htmlspecialchars( $file->getDimensionsString() );
242 $row .= $this->msg( 'word-separator' )->escaped();
243 $row .= '<span style="white-space: nowrap;">';
244 $row .= $this->msg( 'parentheses' )->sizeParams( $file->getSize() )->escaped();
250 // Hide deleted usernames
251 if ( $file->isDeleted( File
::DELETED_USER
) ) {
252 $row .= '<span class="history-deleted">'
253 . $this->msg( 'rev-deleted-user' )->escaped() . '</span>';
256 $row .= Linker
::userLink( $userId, $userText );
257 $row .= '<span style="white-space: nowrap;">';
258 $row .= Linker
::userToolLinks( $userId, $userText );
261 $row .= htmlspecialchars( $userText );
266 // Don't show deleted descriptions
267 if ( $file->isDeleted( File
::DELETED_COMMENT
) ) {
268 $row .= '<td><span class="history-deleted">' .
269 $this->msg( 'rev-deleted-comment' )->escaped() . '</span></td>';
271 $contLang = MediaWikiServices
::getInstance()->getContentLanguage();
272 $row .= Html
::rawElement(
274 [ 'dir' => $contLang->getDir() ],
275 Linker
::formatComment( $description, $this->title
)
280 Hooks
::run( 'ImagePageFileHistoryLine', [ $this, $file, &$row, &$rowClass ] );
281 $classAttr = $rowClass ?
" class='$rowClass'" : '';
283 return "<tr{$classAttr}>{$row}</tr>\n";
290 protected function getThumbForLine( $file ) {
291 $lang = $this->getLanguage();
292 $user = $this->getUser();
293 if ( $file->allowInlineDisplay() && $file->userCan( File
::DELETED_FILE
, $user )
294 && !$file->isDeleted( File
::DELETED_FILE
)
300 $timestamp = wfTimestamp( TS_MW
, $file->getTimestamp() );
302 $thumbnail = $file->transform( $params );
304 'alt' => $this->msg( 'filehist-thumbtext',
305 $lang->userTimeAndDate( $timestamp, $user ),
306 $lang->userDate( $timestamp, $user ),
307 $lang->userTime( $timestamp, $user ) )->text(),
312 return $this->msg( 'filehist-nothumb' )->escaped();
315 return $thumbnail->toHtml( $options );
317 return $this->msg( 'filehist-nothumb' )->escaped();
322 * @param bool $enable
324 protected function preventClickjacking( $enable = true ) {
325 $this->preventClickjacking
= $enable;
331 public function getPreventClickjacking() {
332 return $this->preventClickjacking
;