MediaWiki  master
ImageHistoryPseudoPager.php
Go to the documentation of this file.
1 <?php
23 use Wikimedia\Timestamp\TimestampException;
24 
26  protected $preventClickjacking = false;
27 
31  protected $mImg;
32 
36  protected $mTitle;
37 
42  public $mImagePage;
43 
48  public $mHist;
49 
54  public $mRange;
55 
57  private $linkBatchFactory;
58 
63  public function __construct( $imagePage, LinkBatchFactory $linkBatchFactory = null ) {
64  parent::__construct( $imagePage->getContext() );
65  $this->mImagePage = $imagePage;
66  $this->mTitle = $imagePage->getTitle()->createFragmentTarget( 'filehistory' );
67  $this->mImg = null;
68  $this->mHist = [];
69  $this->mRange = [ 0, 0 ]; // display range
70 
71  // Only display 10 revisions at once by default, otherwise the list is overwhelming
72  $this->mLimitsShown = array_merge( [ 10 ], $this->mLimitsShown );
73  $this->mDefaultLimit = 10;
74  [ $this->mLimit, /* $offset */ ] =
75  $this->mRequest->getLimitOffsetForUser(
76  $this->getUser(),
77  $this->mDefaultLimit,
78  ''
79  );
80  $this->linkBatchFactory = $linkBatchFactory ?? MediaWikiServices::getInstance()->getLinkBatchFactory();
81  }
82 
86  public function getTitle() {
87  return $this->mTitle;
88  }
89 
90  public function getQueryInfo() {
91  return [];
92  }
93 
97  public function getIndexField() {
98  return '';
99  }
100 
105  public function formatRow( $row ) {
106  return '';
107  }
108 
112  public function getBody() {
113  $s = '';
114  $this->doQuery();
115  if ( count( $this->mHist ) ) {
116  if ( $this->mImg->isLocal() ) {
117  // Do a batch existence check for user pages and talkpages.
118  $linkBatch = $this->linkBatchFactory->newLinkBatch();
119  for ( $i = $this->mRange[0]; $i <= $this->mRange[1]; $i++ ) {
120  $file = $this->mHist[$i];
121  $uploader = $file->getUploader( File::FOR_THIS_USER, $this->getAuthority() );
122  if ( $uploader ) {
123  $linkBatch->add( NS_USER, $uploader->getName() );
124  $linkBatch->add( NS_USER_TALK, $uploader->getName() );
125  }
126  }
127  $linkBatch->execute();
128  }
129 
130  // Batch-format comments
131  $comments = [];
132  for ( $i = $this->mRange[0]; $i <= $this->mRange[1]; $i++ ) {
133  $file = $this->mHist[$i];
134  $comments[$i] = $file->getDescription(
136  $this->getAuthority()
137  ) ?: '';
138  }
139  $formattedComments = MediaWikiServices::getInstance()
140  ->getCommentFormatter()
141  ->formatStrings( $comments, $this->getTitle() );
142 
143  $list = new ImageHistoryList( $this->mImagePage );
144  # Generate prev/next links
145  $navLink = $this->getNavigationBar();
146 
147  $s = Html::element( 'h2', [ 'id' => 'filehistory' ], $this->msg( 'filehist' )->text() ) . "\n"
148  . Html::openElement( 'div', [ 'id' => 'mw-imagepage-section-filehistory' ] ) . "\n"
149  . $this->msg( 'filehist-help' )->parseAsBlock()
150  . $navLink . "\n";
151 
152  $sList = $list->beginImageHistoryList();
153  $onlyCurrentFile = true;
154  // Skip rows there just for paging links
155  for ( $i = $this->mRange[0]; $i <= $this->mRange[1]; $i++ ) {
156  $file = $this->mHist[$i];
157  $sList .= $list->imageHistoryLine( !$file->isOld(), $file, $formattedComments[$i] );
158  $onlyCurrentFile = !$file->isOld();
159  }
160  $sList .= $list->endImageHistoryList();
161  if ( $onlyCurrentFile || !$this->mImg->isLocal() ) {
162  // It is not possible to revision-delete the current file or foreign files,
163  // if there is only the current file or the file is not local, show no buttons
164  $s .= $sList;
165  } else {
166  $s .= $this->wrapWithActionButtons( $sList );
167  }
168  $s .= $navLink . "\n" . Html::closeElement( 'div' ) . "\n";
169 
170  if ( $list->getPreventClickjacking() ) {
171  $this->setPreventClickjacking( true );
172  }
173  }
174  return $s;
175  }
176 
177  public function doQuery() {
178  if ( $this->mQueryDone ) {
179  return;
180  }
181  $this->mImg = $this->mImagePage->getPage()->getFile(); // ensure loading
182  if ( !$this->mImg->exists() ) {
183  return;
184  }
185  // Make sure the date (probably from user input) is valid; if not, drop it.
186  if ( $this->mOffset !== null ) {
187  try {
188  $this->mDb->timestamp( $this->mOffset );
189  } catch ( TimestampException $e ) {
190  $this->mOffset = null;
191  }
192  }
193  $queryLimit = $this->mLimit + 1; // limit plus extra row
194  if ( $this->mIsBackwards ) {
195  // Fetch the file history
196  $this->mHist = $this->mImg->getHistory( $queryLimit, null, $this->mOffset, false );
197  // The current rev may not meet the offset/limit
198  $numRows = count( $this->mHist );
199  if ( $numRows <= $this->mLimit && $this->mImg->getTimestamp() > $this->mOffset ) {
200  $this->mHist = array_merge( [ $this->mImg ], $this->mHist );
201  }
202  } else {
203  // The current rev may not meet the offset
204  if ( !$this->mOffset || $this->mImg->getTimestamp() < $this->mOffset ) {
205  $this->mHist[] = $this->mImg;
206  }
207  // Old image versions (fetch extra row for nav links)
208  $oiLimit = count( $this->mHist ) ? $this->mLimit : $this->mLimit + 1;
209  // Fetch the file history
210  $this->mHist = array_merge( $this->mHist,
211  $this->mImg->getHistory( $oiLimit, $this->mOffset, null, false ) );
212  }
213  $numRows = count( $this->mHist ); // Total number of query results
214  if ( $numRows ) {
215  # Index value of top item in the list
216  $firstIndex = $this->mIsBackwards ?
217  [ $this->mHist[$numRows - 1]->getTimestamp() ] : [ $this->mHist[0]->getTimestamp() ];
218  # Discard the extra result row if there is one
219  if ( $numRows > $this->mLimit && $numRows > 1 ) {
220  if ( $this->mIsBackwards ) {
221  # Index value of item past the index
222  $this->mPastTheEndIndex = [ $this->mHist[0]->getTimestamp() ];
223  # Index value of bottom item in the list
224  $lastIndex = [ $this->mHist[1]->getTimestamp() ];
225  # Display range
226  $this->mRange = [ 1, $numRows - 1 ];
227  } else {
228  # Index value of item past the index
229  $this->mPastTheEndIndex = [ $this->mHist[$numRows - 1]->getTimestamp() ];
230  # Index value of bottom item in the list
231  $lastIndex = [ $this->mHist[$numRows - 2]->getTimestamp() ];
232  # Display range
233  $this->mRange = [ 0, $numRows - 2 ];
234  }
235  } else {
236  # Setting indexes to an empty array means that they will be
237  # omitted if they would otherwise appear in URLs. It just so
238  # happens that this is the right thing to do in the standard
239  # UI, in all the relevant cases.
240  $this->mPastTheEndIndex = [];
241  # Index value of bottom item in the list
242  $lastIndex = $this->mIsBackwards ?
243  [ $this->mHist[0]->getTimestamp() ] : [ $this->mHist[$numRows - 1]->getTimestamp() ];
244  # Display range
245  $this->mRange = [ 0, $numRows - 1 ];
246  }
247  } else {
248  $firstIndex = [];
249  $lastIndex = [];
250  $this->mPastTheEndIndex = [];
251  }
252  if ( $this->mIsBackwards ) {
253  $this->mIsFirst = ( $numRows < $queryLimit );
254  $this->mIsLast = ( $this->mOffset == '' );
255  $this->mLastShown = $firstIndex;
256  $this->mFirstShown = $lastIndex;
257  } else {
258  $this->mIsFirst = ( $this->mOffset == '' );
259  $this->mIsLast = ( $numRows < $queryLimit );
260  $this->mLastShown = $lastIndex;
261  $this->mFirstShown = $firstIndex;
262  }
263  $this->mQueryDone = true;
264  }
265 
272  private function wrapWithActionButtons( $formcontents ) {
273  if ( !$this->getAuthority()->isAllowed( 'deleterevision' ) ) {
274  return $formcontents;
275  }
276 
277  # Show button to hide log entries
279  'form',
280  [ 'action' => wfScript(), 'id' => 'mw-filehistory-deleterevision-submit' ]
281  ) . "\n";
282  $s .= Html::hidden( 'target', $this->getTitle()->getPrefixedDBkey() ) . "\n";
283  $s .= Html::hidden( 'action', 'historysubmit' ) . "\n";
284  $s .= Html::hidden( 'type', 'oldimage' ) . "\n";
285  $this->setPreventClickjacking( true );
286 
287  $buttons = Html::element(
288  'button',
289  [
290  'type' => 'submit',
291  'name' => 'revisiondelete',
292  'value' => '1',
293  'class' => "deleterevision-filehistory-submit mw-filehistory-deleterevision-button mw-ui-button"
294  ],
295  $this->msg( 'showhideselectedfileversions' )->text()
296  ) . "\n";
297 
298  $s .= $buttons . $formcontents . $buttons;
299  $s .= Html::closeElement( 'form' );
300 
301  return $s;
302  }
303 
308  protected function preventClickjacking( $enable = true ) {
309  $this->preventClickjacking = $enable;
310  }
311 
316  protected function setPreventClickjacking( bool $enable ) {
317  $this->preventClickjacking = $enable;
318  }
319 
323  public function getPreventClickjacking() {
325  }
326 
327 }
const NS_USER
Definition: Defines.php:66
const NS_USER_TALK
Definition: Defines.php:67
wfScript( $script='index')
Get the path to a specified script file, respecting file extensions; this is a wrapper around $wgScri...
msg( $key,... $params)
Get a Message object with context set Parameters are the same as wfMessage()
const FOR_THIS_USER
Definition: File.php:88
static element( $element, $attribs=[], $contents='')
Identical to rawElement(), but HTML-escapes $contents (like Xml::element()).
Definition: Html.php:236
static openElement( $element, $attribs=[])
Identical to rawElement(), but has no third parameter and omits the end tag (and the self-closing '/'...
Definition: Html.php:256
static closeElement( $element)
Returns "</$element>".
Definition: Html.php:320
static hidden( $name, $value, array $attribs=[])
Convenience function to produce an input element with type=hidden.
Definition: Html.php:851
Builds the image revision log shown on image pages.
__construct( $imagePage, LinkBatchFactory $linkBatchFactory=null)
doQuery()
Do the query, using information from the object context.
getQueryInfo()
Provides all parameters needed for the main paged query.
int $mLimit
The maximum number of entries to show.
Definition: IndexPager.php:96
Service locator for MediaWiki core services.
IndexPager with a formatted navigation bar.
foreach( $mmfl['setupFiles'] as $fileName) if( $queue) if(empty( $mmfl['quiet'])) $s
if(PHP_SAPI !='cli-server') if(!isset( $_SERVER['SCRIPT_FILENAME'])) $file
Item class for a filearchive table row.
Definition: router.php:42