MediaWiki  master
ImageHistoryPseudoPager.php
Go to the documentation of this file.
1 <?php
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 }
mixed $mOffset
The starting point to enumerate entries.
Definition: IndexPager.php:90
if(PHP_SAPI !='cli-server') if(!isset( $_SERVER['SCRIPT_FILENAME'])) $file
Item class for a filearchive table row.
Definition: router.php:42
Efficient paging for SQL queries.
Builds the image revision log shown on image pages.
Class representing a list of titles The execute() method checks them all for existence and adds them ...
Definition: LinkBatch.php:34
const NS_USER_TALK
Definition: Defines.php:63