MediaWiki  1.34.0
ImageHistoryPseudoPager.php
Go to the documentation of this file.
1 <?php
21 use Wikimedia\Timestamp\TimestampException;
22 
24  protected $preventClickjacking = false;
25 
29  protected $mImg;
30 
34  protected $mTitle;
35 
40  public $mImagePage;
41 
46  public $mHist;
47 
52  public $mRange;
53 
57  public function __construct( $imagePage ) {
58  parent::__construct( $imagePage->getContext() );
59  $this->mImagePage = $imagePage;
60  $this->mTitle = $imagePage->getTitle()->createFragmentTarget( 'filehistory' );
61  $this->mImg = null;
62  $this->mHist = [];
63  $this->mRange = [ 0, 0 ]; // display range
64 
65  // Only display 10 revisions at once by default, otherwise the list is overwhelming
66  $this->mLimitsShown = array_merge( [ 10 ], $this->mLimitsShown );
67  $this->mDefaultLimit = 10;
68  list( $this->mLimit, /* $offset */ ) =
69  $this->mRequest->getLimitOffset( $this->mDefaultLimit, '' );
70  }
71 
75  public function getTitle() {
76  return $this->mTitle;
77  }
78 
79  public function getQueryInfo() {
80  return [];
81  }
82 
86  public function getIndexField() {
87  return '';
88  }
89 
94  public function formatRow( $row ) {
95  return '';
96  }
97 
101  public function getBody() {
102  $s = '';
103  $this->doQuery();
104  if ( count( $this->mHist ) ) {
105  if ( $this->mImg->isLocal() ) {
106  // Do a batch existence check for user pages and talkpages
107  $linkBatch = new LinkBatch();
108  for ( $i = $this->mRange[0]; $i <= $this->mRange[1]; $i++ ) {
109  $file = $this->mHist[$i];
110  $user = $file->getUser( 'text' );
111  $linkBatch->add( NS_USER, $user );
112  $linkBatch->add( NS_USER_TALK, $user );
113  }
114  $linkBatch->execute();
115  }
116 
117  $list = new ImageHistoryList( $this->mImagePage );
118  # Generate prev/next links
119  $navLink = $this->getNavigationBar();
120  $s = $list->beginImageHistoryList( $navLink );
121  // Skip rows there just for paging links
122  for ( $i = $this->mRange[0]; $i <= $this->mRange[1]; $i++ ) {
123  $file = $this->mHist[$i];
124  $s .= $list->imageHistoryLine( !$file->isOld(), $file );
125  }
126  $s .= $list->endImageHistoryList( $navLink );
127 
128  if ( $list->getPreventClickjacking() ) {
129  $this->preventClickjacking();
130  }
131  }
132  return $s;
133  }
134 
135  public function doQuery() {
136  if ( $this->mQueryDone ) {
137  return;
138  }
139  $this->mImg = $this->mImagePage->getPage()->getFile(); // ensure loading
140  if ( !$this->mImg->exists() ) {
141  return;
142  }
143  // Make sure the date (probably from user input) is valid; if not, drop it.
144  if ( $this->mOffset !== null ) {
145  try {
146  $sadlyWeCannotPassThisTimestampDownTheStack = $this->mDb->timestamp( $this->mOffset );
147  } catch ( TimestampException $e ) {
148  $this->mOffset = null;
149  }
150  }
151  $queryLimit = $this->mLimit + 1; // limit plus extra row
152  if ( $this->mIsBackwards ) {
153  // Fetch the file history
154  $this->mHist = $this->mImg->getHistory( $queryLimit, null, $this->mOffset, false );
155  // The current rev may not meet the offset/limit
156  $numRows = count( $this->mHist );
157  if ( $numRows <= $this->mLimit && $this->mImg->getTimestamp() > $this->mOffset ) {
158  $this->mHist = array_merge( [ $this->mImg ], $this->mHist );
159  }
160  } else {
161  // The current rev may not meet the offset
162  if ( !$this->mOffset || $this->mImg->getTimestamp() < $this->mOffset ) {
163  $this->mHist[] = $this->mImg;
164  }
165  // Old image versions (fetch extra row for nav links)
166  $oiLimit = count( $this->mHist ) ? $this->mLimit : $this->mLimit + 1;
167  // Fetch the file history
168  $this->mHist = array_merge( $this->mHist,
169  $this->mImg->getHistory( $oiLimit, $this->mOffset, null, false ) );
170  }
171  $numRows = count( $this->mHist ); // Total number of query results
172  if ( $numRows ) {
173  # Index value of top item in the list
174  $firstIndex = $this->mIsBackwards ?
175  $this->mHist[$numRows - 1]->getTimestamp() : $this->mHist[0]->getTimestamp();
176  # Discard the extra result row if there is one
177  if ( $numRows > $this->mLimit && $numRows > 1 ) {
178  if ( $this->mIsBackwards ) {
179  # Index value of item past the index
180  $this->mPastTheEndIndex = $this->mHist[0]->getTimestamp();
181  # Index value of bottom item in the list
182  $lastIndex = $this->mHist[1]->getTimestamp();
183  # Display range
184  $this->mRange = [ 1, $numRows - 1 ];
185  } else {
186  # Index value of item past the index
187  $this->mPastTheEndIndex = $this->mHist[$numRows - 1]->getTimestamp();
188  # Index value of bottom item in the list
189  $lastIndex = $this->mHist[$numRows - 2]->getTimestamp();
190  # Display range
191  $this->mRange = [ 0, $numRows - 2 ];
192  }
193  } else {
194  # Setting indexes to an empty string means that they will be
195  # omitted if they would otherwise appear in URLs. It just so
196  # happens that this is the right thing to do in the standard
197  # UI, in all the relevant cases.
198  $this->mPastTheEndIndex = '';
199  # Index value of bottom item in the list
200  $lastIndex = $this->mIsBackwards ?
201  $this->mHist[0]->getTimestamp() : $this->mHist[$numRows - 1]->getTimestamp();
202  # Display range
203  $this->mRange = [ 0, $numRows - 1 ];
204  }
205  } else {
206  $firstIndex = '';
207  $lastIndex = '';
208  $this->mPastTheEndIndex = '';
209  }
210  if ( $this->mIsBackwards ) {
211  $this->mIsFirst = ( $numRows < $queryLimit );
212  $this->mIsLast = ( $this->mOffset == '' );
213  $this->mLastShown = $firstIndex;
214  $this->mFirstShown = $lastIndex;
215  } else {
216  $this->mIsFirst = ( $this->mOffset == '' );
217  $this->mIsLast = ( $numRows < $queryLimit );
218  $this->mLastShown = $lastIndex;
219  $this->mFirstShown = $firstIndex;
220  }
221  $this->mQueryDone = true;
222  }
223 
227  protected function preventClickjacking( $enable = true ) {
228  $this->preventClickjacking = $enable;
229  }
230 
234  public function getPreventClickjacking() {
236  }
237 
238 }
ImageHistoryPseudoPager\$mImg
File $mImg
Definition: ImageHistoryPseudoPager.php:29
ImageHistoryPseudoPager\$mRange
int[] $mRange
Definition: ImageHistoryPseudoPager.php:52
ImageHistoryPseudoPager\$mImagePage
ImagePage $mImagePage
Definition: ImageHistoryPseudoPager.php:40
LinkBatch
Class representing a list of titles The execute() method checks them all for existence and adds them ...
Definition: LinkBatch.php:34
ImageHistoryList
Builds the image revision log shown on image pages.
Definition: ImageHistoryList.php:28
ImageHistoryPseudoPager\__construct
__construct( $imagePage)
Definition: ImageHistoryPseudoPager.php:57
$file
if(PHP_SAPI !='cli-server') if(!isset( $_SERVER['SCRIPT_FILENAME'])) $file
Item class for a filearchive table row.
Definition: router.php:42
$s
$s
Definition: mergeMessageFileList.php:185
ImagePage
Definition: ImagePage.php:34
ImageHistoryPseudoPager\preventClickjacking
preventClickjacking( $enable=true)
Definition: ImageHistoryPseudoPager.php:227
ImageHistoryPseudoPager\formatRow
formatRow( $row)
Definition: ImageHistoryPseudoPager.php:94
ImageHistoryPseudoPager\getBody
getBody()
Definition: ImageHistoryPseudoPager.php:101
ImageHistoryPseudoPager\doQuery
doQuery()
Do the query, using information from the object context.
Definition: ImageHistoryPseudoPager.php:135
File
Implements some public methods and some protected utility functions which are required by multiple ch...
Definition: File.php:61
ImageHistoryPseudoPager\$preventClickjacking
$preventClickjacking
Definition: ImageHistoryPseudoPager.php:24
ImageHistoryPseudoPager\getIndexField
getIndexField()
Definition: ImageHistoryPseudoPager.php:86
ImageHistoryPseudoPager\getPreventClickjacking
getPreventClickjacking()
Definition: ImageHistoryPseudoPager.php:234
ImageHistoryPseudoPager\getQueryInfo
getQueryInfo()
This function should be overridden to provide all parameters needed for the main paged query.
Definition: ImageHistoryPseudoPager.php:79
ImageHistoryPseudoPager\getTitle
getTitle()
Definition: ImageHistoryPseudoPager.php:75
NS_USER_TALK
const NS_USER_TALK
Definition: Defines.php:63
ImageHistoryPseudoPager\$mTitle
Title $mTitle
Definition: ImageHistoryPseudoPager.php:34
Title
Represents a title within MediaWiki.
Definition: Title.php:42
ImageHistoryPseudoPager\$mHist
File[] $mHist
Definition: ImageHistoryPseudoPager.php:46
NS_USER
const NS_USER
Definition: Defines.php:62
ReverseChronologicalPager
Efficient paging for SQL queries.
Definition: ReverseChronologicalPager.php:28
ImageHistoryPseudoPager
Definition: ImageHistoryPseudoPager.php:23
IndexPager\$mOffset
mixed $mOffset
The starting point to enumerate entries.
Definition: IndexPager.php:90
ReverseChronologicalPager\getNavigationBar
getNavigationBar()
Definition: ReverseChronologicalPager.php:38