MediaWiki  master
ImageHistoryList.php
Go to the documentation of this file.
1 <?php
21 use MediaWiki\HookContainer\ProtectedHookAccessorTrait;
23 
30  use ProtectedHookAccessorTrait;
31 
35  protected $title;
36 
40  protected $img;
41 
45  protected $imagePage;
46 
50  protected $current;
51 
52  protected $repo, $showThumb;
53  protected $preventClickjacking = false;
54 
58  public function __construct( $imagePage ) {
60  $this->current = $imagePage->getPage()->getFile();
61  $this->img = $imagePage->getDisplayedFile();
62  $this->title = $imagePage->getTitle();
63  $this->imagePage = $imagePage;
64  $this->showThumb = $context->getConfig()->get( 'ShowArchiveThumbnails' ) &&
65  $this->img->canRender();
66  $this->setContext( $context );
67  }
68 
72  public function getImagePage() {
73  return $this->imagePage;
74  }
75 
79  public function getFile() {
80  return $this->img;
81  }
82 
87  public function beginImageHistoryList( $navLinks = '' ) {
88  return Xml::element( 'h2', [ 'id' => 'filehistory' ], $this->msg( 'filehist' )->text() )
89  . "\n"
90  . "<div id=\"mw-imagepage-section-filehistory\">\n"
91  . $this->msg( 'filehist-help' )->parseAsBlock()
92  . $navLinks . "\n"
93  . Xml::openElement( 'table', [ 'class' => 'wikitable filehistory' ] ) . "\n"
94  . '<tr><th></th>'
95  . ( $this->current->isLocal()
96  && ( $this->getAuthority()->isAllowedAny( '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>'
102  . "</tr>\n";
103  }
104 
109  public function endImageHistoryList( $navLinks = '' ) {
110  return "</table>\n$navLinks\n</div>\n";
111  }
112 
118  public function imageHistoryLine( $iscur, $file ) {
119  $user = $this->getUser();
120  $lang = $this->getLanguage();
121  $linkRenderer = MediaWikiServices::getInstance()->getLinkRenderer();
122  $timestamp = wfTimestamp( TS_MW, $file->getTimestamp() );
123  // @phan-suppress-next-line PhanUndeclaredMethod
124  $img = $iscur ? $file->getName() : $file->getArchiveName();
125  $uploader = $file->getUploader( File::FOR_THIS_USER, $user );
126  $description = $file->getDescription( File::FOR_THIS_USER, $user );
127 
128  $local = $this->current->isLocal();
129  $row = $selected = '';
130 
131  // Deletion link
132  if ( $local && ( $this->getAuthority()->isAllowedAny( 'delete', 'deletedhistory' ) ) ) {
133  $row .= '<td>';
134  # Link to remove from history
135  if ( $this->getAuthority()->isAllowed( 'delete' ) ) {
136  $q = [ 'action' => 'delete' ];
137  if ( !$iscur ) {
138  $q['oldimage'] = $img;
139  }
140  $row .= $linkRenderer->makeKnownLink(
141  $this->title,
142  $this->msg( $iscur ? 'filehist-deleteall' : 'filehist-deleteone' )->text(),
143  [], $q
144  );
145  }
146  # Link to hide content. Don't show useless link to people who cannot hide revisions.
147  $canHide = $this->getAuthority()->isAllowed( 'deleterevision' );
148  if ( $canHide || ( $this->getAuthority()->isAllowed( 'deletedhistory' )
149  && $file->getVisibility() ) ) {
150  if ( $this->getAuthority()->isAllowed( 'delete' ) ) {
151  $row .= '<br />';
152  }
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 );
156  } else {
157  list( $ts, ) = explode( '!', $img, 2 );
158  $query = [
159  'type' => 'oldimage',
160  'target' => $this->title->getPrefixedText(),
161  'ids' => $ts,
162  ];
163  $del = Linker::revDeleteLink( $query,
164  $file->isDeleted( File::DELETED_RESTRICTED ), $canHide );
165  }
166  $row .= $del;
167  }
168  $row .= '</td>';
169  }
170 
171  // Reversion link/current indicator
172  $row .= '<td>';
173  if ( $iscur ) {
174  $row .= $this->msg( 'filehist-current' )->escaped();
175  } elseif ( $local && $this->getAuthority()->probablyCan( 'edit', $this->title )
176  && $this->getAuthority()->probablyCan( 'upload', $this->title )
177  ) {
178  if ( $file->isDeleted( File::DELETED_FILE ) ) {
179  $row .= $this->msg( 'filehist-revert' )->escaped();
180  } else {
181  $row .= $linkRenderer->makeKnownLink(
182  $this->title,
183  $this->msg( 'filehist-revert' )->text(),
184  [],
185  [
186  'action' => 'revert',
187  'oldimage' => $img,
188  ]
189  );
190  }
191  }
192  $row .= '</td>';
193 
194  // Date/time and image link
195  if ( $file->getTimestamp() === $this->img->getTimestamp() ) {
196  $selected = "class='filehistory-selected'";
197  }
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 )
203  );
204  } elseif ( $file->isDeleted( File::DELETED_FILE ) ) {
205  $timeAndDate = $lang->userTimeAndDate( $timestamp, $user );
206  if ( $local ) {
207  $this->preventClickjacking();
208  $revdel = SpecialPage::getTitleFor( 'Revisiondelete' );
209  # Make a link to review the image
210  $url = $linkRenderer->makeKnownLink(
211  $revdel,
212  $timeAndDate,
213  [],
214  [
215  'target' => $this->title->getPrefixedText(),
216  'file' => $img,
217  'token' => $user->getEditToken( $img )
218  ]
219  );
220  } else {
221  $url = htmlspecialchars( $timeAndDate );
222  }
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 )
227  );
228  } else {
229  $url = $iscur ? $this->current->getUrl() : $this->current->getArchiveUrl( $img );
230  $row .= Xml::element(
231  'a',
232  [ 'href' => $url ],
233  $lang->userTimeAndDate( $timestamp, $user )
234  );
235  }
236  $row .= "</td>";
237 
238  // Thumbnail
239  if ( $this->showThumb ) {
240  $row .= '<td>' . $this->getThumbForLine( $file ) . '</td>';
241  }
242 
243  // Image dimensions + size
244  $row .= '<td>';
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();
249  $row .= '</span>';
250  $row .= '</td>';
251 
252  // Uploading user
253  $row .= '<td>';
254  // Hide deleted usernames
255  if ( $uploader && $local ) {
256  $row .= Linker::userLink( $uploader->getId(), $uploader->getName() );
257  $row .= '<span style="white-space: nowrap;">';
258  $row .= Linker::userToolLinks( $uploader->getId(), $uploader->getName() );
259  $row .= '</span>';
260  } elseif ( $uploader ) {
261  $row .= htmlspecialchars( $uploader->getName() );
262  } else {
263  $row .= '<span class="history-deleted">'
264  . $this->msg( 'rev-deleted-user' )->escaped() . '</span>';
265  }
266  $row .= '</td>';
267 
268  // Don't show deleted descriptions
269  if ( $file->isDeleted( File::DELETED_COMMENT ) ) {
270  $row .= '<td><span class="history-deleted">' .
271  $this->msg( 'rev-deleted-comment' )->escaped() . '</span></td>';
272  } else {
273  $contLang = MediaWikiServices::getInstance()->getContentLanguage();
274  $row .= Html::rawElement(
275  'td',
276  [ 'dir' => $contLang->getDir() ],
277  Linker::formatComment( $description, $this->title )
278  );
279  }
280 
281  $rowClass = null;
282  $this->getHookRunner()->onImagePageFileHistoryLine( $this, $file, $row, $rowClass );
283  $classAttr = $rowClass ? " class='$rowClass'" : '';
284 
285  return "<tr{$classAttr}>{$row}</tr>\n";
286  }
287 
292  protected function getThumbForLine( $file ) {
293  $lang = $this->getLanguage();
294  $user = $this->getUser();
295  if ( $file->allowInlineDisplay() && $file->userCan( File::DELETED_FILE, $user )
296  && !$file->isDeleted( File::DELETED_FILE )
297  ) {
298  $params = [
299  'width' => '120',
300  'height' => '120',
301  ];
302  $timestamp = wfTimestamp( TS_MW, $file->getTimestamp() );
303 
304  $thumbnail = $file->transform( $params );
305  $options = [
306  'alt' => $this->msg( 'filehist-thumbtext',
307  $lang->userTimeAndDate( $timestamp, $user ),
308  $lang->userDate( $timestamp, $user ),
309  $lang->userTime( $timestamp, $user ) )->text(),
310  'file-link' => true,
311  ];
312 
313  if ( !$thumbnail ) {
314  return $this->msg( 'filehist-nothumb' )->escaped();
315  }
316 
317  return $thumbnail->toHtml( $options );
318  } else {
319  return $this->msg( 'filehist-nothumb' )->escaped();
320  }
321  }
322 
326  protected function preventClickjacking( $enable = true ) {
327  $this->preventClickjacking = $enable;
328  }
329 
333  public function getPreventClickjacking() {
335  }
336 }
ContextSource\$context
IContextSource $context
Definition: ContextSource.php:39
ImageHistoryList\$img
File $img
Definition: ImageHistoryList.php:40
ImageHistoryList
Builds the image revision log shown on image pages.
Definition: ImageHistoryList.php:29
Linker\userLink
static userLink( $userId, $userName, $altUserName=false)
Make user link (or user contributions for unregistered users)
Definition: Linker.php:1064
File\DELETED_RESTRICTED
const DELETED_RESTRICTED
Definition: File.php:73
MediaWiki\MediaWikiServices
MediaWikiServices is the service locator for the application scope of MediaWiki.
Definition: MediaWikiServices.php:193
$lang
if(!isset( $args[0])) $lang
Definition: testCompression.php:37
ImageHistoryList\$showThumb
$showThumb
Definition: ImageHistoryList.php:52
ImageHistoryList\imageHistoryLine
imageHistoryLine( $iscur, $file)
Definition: ImageHistoryList.php:118
wfTimestamp
wfTimestamp( $outputtype=TS_UNIX, $ts=0)
Get a timestamp string in one of various formats.
Definition: GlobalFunctions.php:1664
Linker\userToolLinks
static userToolLinks( $userId, $userText, $redContribsWhenNoEdits=false, $flags=0, $edits=null, $useParentheses=true)
Generate standard user tool links (talk, contributions, block link, etc.)
Definition: Linker.php:1109
ImageHistoryList\__construct
__construct( $imagePage)
Definition: ImageHistoryList.php:58
$file
if(PHP_SAPI !='cli-server') if(!isset( $_SERVER['SCRIPT_FILENAME'])) $file
Item class for a filearchive table row.
Definition: router.php:42
SpecialPage\getTitleFor
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,...
Definition: SpecialPage.php:107
ImagePage
Class for viewing MediaWiki file description pages.
Definition: ImagePage.php:34
ContextSource\getUser
getUser()
Definition: ContextSource.php:136
Xml\openElement
static openElement( $element, $attribs=null)
This opens an XML element.
Definition: Xml.php:110
ContextSource\getLanguage
getLanguage()
Definition: ContextSource.php:153
ImageHistoryList\getImagePage
getImagePage()
Definition: ImageHistoryList.php:72
File
Implements some public methods and some protected utility functions which are required by multiple ch...
Definition: File.php:66
File\DELETED_COMMENT
const DELETED_COMMENT
Definition: File.php:71
Article\getTitle
getTitle()
Get the title object of the article.
Definition: Article.php:224
ImageHistoryList\$repo
$repo
Definition: ImageHistoryList.php:52
ContextSource
The simplest way of implementing IContextSource is to hold a RequestContext as a member variable and ...
Definition: ContextSource.php:33
Xml\element
static element( $element, $attribs=null, $contents='', $allowShortTag=true)
Format an XML element with given attributes and, optionally, text content.
Definition: Xml.php:41
Article\getPage
getPage()
Get the WikiPage object of this instance.
Definition: Article.php:234
Article\getContext
getContext()
Gets the context this Article is executed in.
Definition: Article.php:1954
Linker\revDeleteLinkDisabled
static revDeleteLinkDisabled( $delete=true)
Creates a dead (show/hide) link for deleting revisions/log entries.
Definition: Linker.php:2436
File\FOR_THIS_USER
const FOR_THIS_USER
Definition: File.php:87
ContextSource\setContext
setContext(IContextSource $context)
Definition: ContextSource.php:63
ImageHistoryList\preventClickjacking
preventClickjacking( $enable=true)
Definition: ImageHistoryList.php:326
ImageHistoryList\$imagePage
ImagePage $imagePage
Definition: ImageHistoryList.php:45
ImageHistoryList\getFile
getFile()
Definition: ImageHistoryList.php:79
ContextSource\msg
msg( $key,... $params)
Get a Message object with context set Parameters are the same as wfMessage()
Definition: ContextSource.php:197
ContextSource\getAuthority
getAuthority()
Definition: ContextSource.php:144
ImagePage\getDisplayedFile
getDisplayedFile()
Definition: ImagePage.php:230
Linker\formatComment
static formatComment( $comment, $title=null, $local=false, $wikiId=null)
This function is called by all recent changes variants, by the page history, and by the user contribu...
Definition: Linker.php:1372
Title
Represents a title within MediaWiki.
Definition: Title.php:48
ImageHistoryList\$title
Title $title
Definition: ImageHistoryList.php:35
ImageHistoryList\getThumbForLine
getThumbForLine( $file)
Definition: ImageHistoryList.php:292
IContextSource\getConfig
getConfig()
Get the site configuration.
Html\rawElement
static rawElement( $element, $attribs=[], $contents='')
Returns an HTML element in a string.
Definition: Html.php:210
Linker\revDeleteLink
static revDeleteLink( $query=[], $restricted=false, $delete=true)
Creates a (show/hide) link for deleting revisions/log entries.
Definition: Linker.php:2412
ImageHistoryList\beginImageHistoryList
beginImageHistoryList( $navLinks='')
Definition: ImageHistoryList.php:87
File\DELETED_FILE
const DELETED_FILE
Definition: File.php:70
Html\element
static element( $element, $attribs=[], $contents='')
Identical to rawElement(), but HTML-escapes $contents (like Xml::element()).
Definition: Html.php:232
ImageHistoryList\$preventClickjacking
$preventClickjacking
Definition: ImageHistoryList.php:53
ImageHistoryList\$current
File $current
Definition: ImageHistoryList.php:50
ImageHistoryList\getPreventClickjacking
getPreventClickjacking()
Definition: ImageHistoryList.php:333
ImageHistoryList\endImageHistoryList
endImageHistoryList( $navLinks='')
Definition: ImageHistoryList.php:109