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 
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  list( $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  $user = $file->getUser( 'text' );
122  $linkBatch->add( NS_USER, $user );
123  $linkBatch->add( NS_USER_TALK, $user );
124  }
125  $linkBatch->execute();
126  }
127 
128  $list = new ImageHistoryList( $this->mImagePage );
129  # Generate prev/next links
130  $navLink = $this->getNavigationBar();
131  $s = $list->beginImageHistoryList( $navLink );
132  // Skip rows there just for paging links
133  for ( $i = $this->mRange[0]; $i <= $this->mRange[1]; $i++ ) {
134  $file = $this->mHist[$i];
135  $s .= $list->imageHistoryLine( !$file->isOld(), $file );
136  }
137  $s .= $list->endImageHistoryList( $navLink );
138 
139  if ( $list->getPreventClickjacking() ) {
140  $this->preventClickjacking();
141  }
142  }
143  return $s;
144  }
145 
146  public function doQuery() {
147  if ( $this->mQueryDone ) {
148  return;
149  }
150  $this->mImg = $this->mImagePage->getPage()->getFile(); // ensure loading
151  if ( !$this->mImg->exists() ) {
152  return;
153  }
154  // Make sure the date (probably from user input) is valid; if not, drop it.
155  if ( $this->mOffset !== null ) {
156  try {
157  $sadlyWeCannotPassThisTimestampDownTheStack = $this->mDb->timestamp( $this->mOffset );
158  } catch ( TimestampException $e ) {
159  $this->mOffset = null;
160  }
161  }
162  $queryLimit = $this->mLimit + 1; // limit plus extra row
163  if ( $this->mIsBackwards ) {
164  // Fetch the file history
165  $this->mHist = $this->mImg->getHistory( $queryLimit, null, $this->mOffset, false );
166  // The current rev may not meet the offset/limit
167  $numRows = count( $this->mHist );
168  if ( $numRows <= $this->mLimit && $this->mImg->getTimestamp() > $this->mOffset ) {
169  $this->mHist = array_merge( [ $this->mImg ], $this->mHist );
170  }
171  } else {
172  // The current rev may not meet the offset
173  if ( !$this->mOffset || $this->mImg->getTimestamp() < $this->mOffset ) {
174  $this->mHist[] = $this->mImg;
175  }
176  // Old image versions (fetch extra row for nav links)
177  $oiLimit = count( $this->mHist ) ? $this->mLimit : $this->mLimit + 1;
178  // Fetch the file history
179  $this->mHist = array_merge( $this->mHist,
180  $this->mImg->getHistory( $oiLimit, $this->mOffset, null, false ) );
181  }
182  $numRows = count( $this->mHist ); // Total number of query results
183  if ( $numRows ) {
184  # Index value of top item in the list
185  $firstIndex = $this->mIsBackwards ?
186  [ $this->mHist[$numRows - 1]->getTimestamp() ] : [ $this->mHist[0]->getTimestamp() ];
187  # Discard the extra result row if there is one
188  if ( $numRows > $this->mLimit && $numRows > 1 ) {
189  if ( $this->mIsBackwards ) {
190  # Index value of item past the index
191  $this->mPastTheEndIndex = [ $this->mHist[0]->getTimestamp() ];
192  # Index value of bottom item in the list
193  $lastIndex = [ $this->mHist[1]->getTimestamp() ];
194  # Display range
195  $this->mRange = [ 1, $numRows - 1 ];
196  } else {
197  # Index value of item past the index
198  $this->mPastTheEndIndex = [ $this->mHist[$numRows - 1]->getTimestamp() ];
199  # Index value of bottom item in the list
200  $lastIndex = [ $this->mHist[$numRows - 2]->getTimestamp() ];
201  # Display range
202  $this->mRange = [ 0, $numRows - 2 ];
203  }
204  } else {
205  # Setting indexes to an empty array means that they will be
206  # omitted if they would otherwise appear in URLs. It just so
207  # happens that this is the right thing to do in the standard
208  # UI, in all the relevant cases.
209  $this->mPastTheEndIndex = [];
210  # Index value of bottom item in the list
211  $lastIndex = $this->mIsBackwards ?
212  [ $this->mHist[0]->getTimestamp() ] : [ $this->mHist[$numRows - 1]->getTimestamp() ];
213  # Display range
214  $this->mRange = [ 0, $numRows - 1 ];
215  }
216  } else {
217  $firstIndex = [];
218  $lastIndex = [];
219  $this->mPastTheEndIndex = [];
220  }
221  if ( $this->mIsBackwards ) {
222  $this->mIsFirst = ( $numRows < $queryLimit );
223  $this->mIsLast = ( $this->mOffset == '' );
224  $this->mLastShown = $firstIndex;
225  $this->mFirstShown = $lastIndex;
226  } else {
227  $this->mIsFirst = ( $this->mOffset == '' );
228  $this->mIsLast = ( $numRows < $queryLimit );
229  $this->mLastShown = $lastIndex;
230  $this->mFirstShown = $firstIndex;
231  }
232  $this->mQueryDone = true;
233  }
234 
238  protected function preventClickjacking( $enable = true ) {
239  $this->preventClickjacking = $enable;
240  }
241 
245  public function getPreventClickjacking() {
247  }
248 
249 }
ImageHistoryPseudoPager\$mImg
File $mImg
Definition: ImageHistoryPseudoPager.php:31
ImageHistoryPseudoPager\$mRange
int[] $mRange
Definition: ImageHistoryPseudoPager.php:54
ImageHistoryPseudoPager\$mImagePage
ImagePage $mImagePage
Definition: ImageHistoryPseudoPager.php:42
ImageHistoryList
Builds the image revision log shown on image pages.
Definition: ImageHistoryList.php:29
MediaWiki\MediaWikiServices
MediaWikiServices is the service locator for the application scope of MediaWiki.
Definition: MediaWikiServices.php:163
$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:184
ImagePage
Class for viewing MediaWiki file description pages.
Definition: ImagePage.php:33
ImageHistoryPseudoPager\preventClickjacking
preventClickjacking( $enable=true)
Definition: ImageHistoryPseudoPager.php:238
ContextSource\getUser
getUser()
Stable to override.
Definition: ContextSource.php:134
ImageHistoryPseudoPager\formatRow
formatRow( $row)
Definition: ImageHistoryPseudoPager.php:105
ImageHistoryPseudoPager\getBody
getBody()
Definition: ImageHistoryPseudoPager.php:112
ImageHistoryPseudoPager\doQuery
doQuery()
Do the query, using information from the object context.
Definition: ImageHistoryPseudoPager.php:146
File
Implements some public methods and some protected utility functions which are required by multiple ch...
Definition: File.php:63
ImageHistoryPseudoPager\$linkBatchFactory
LinkBatchFactory $linkBatchFactory
Definition: ImageHistoryPseudoPager.php:57
ImageHistoryPseudoPager\$preventClickjacking
$preventClickjacking
Definition: ImageHistoryPseudoPager.php:26
ImageHistoryPseudoPager\getIndexField
getIndexField()
Definition: ImageHistoryPseudoPager.php:97
MediaWiki\Cache\LinkBatchFactory
Definition: LinkBatchFactory.php:38
ImageHistoryPseudoPager\__construct
__construct( $imagePage, LinkBatchFactory $linkBatchFactory=null)
Definition: ImageHistoryPseudoPager.php:63
ImageHistoryPseudoPager\getPreventClickjacking
getPreventClickjacking()
Definition: ImageHistoryPseudoPager.php:245
ImageHistoryPseudoPager\getQueryInfo
getQueryInfo()
Provides all parameters needed for the main paged query.
Definition: ImageHistoryPseudoPager.php:90
ImageHistoryPseudoPager\getTitle
getTitle()
Definition: ImageHistoryPseudoPager.php:86
NS_USER_TALK
const NS_USER_TALK
Definition: Defines.php:72
ImageHistoryPseudoPager\$mTitle
Title $mTitle
Definition: ImageHistoryPseudoPager.php:36
Title
Represents a title within MediaWiki.
Definition: Title.php:41
ImageHistoryPseudoPager\$mHist
File[] $mHist
Definition: ImageHistoryPseudoPager.php:48
NS_USER
const NS_USER
Definition: Defines.php:71
ReverseChronologicalPager
Efficient paging for SQL queries.
Definition: ReverseChronologicalPager.php:29
ImageHistoryPseudoPager
Definition: ImageHistoryPseudoPager.php:25
ReverseChronologicalPager\getNavigationBar
getNavigationBar()
Stable to override.
Definition: ReverseChronologicalPager.php:43