MediaWiki REL1_37
ImageHistoryPseudoPager.php
Go to the documentation of this file.
1<?php
23use Wikimedia\Timestamp\TimestampException;
24
26 protected $preventClickjacking = false;
27
31 protected $mImg;
32
36 protected $mTitle;
37
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 $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 $list = new ImageHistoryList( $this->mImagePage );
131 # Generate prev/next links
132 $navLink = $this->getNavigationBar();
133 $s = $list->beginImageHistoryList( $navLink );
134 // Skip rows there just for paging links
135 for ( $i = $this->mRange[0]; $i <= $this->mRange[1]; $i++ ) {
136 $file = $this->mHist[$i];
137 $s .= $list->imageHistoryLine( !$file->isOld(), $file );
138 }
139 $s .= $list->endImageHistoryList( $navLink );
140
141 if ( $list->getPreventClickjacking() ) {
142 $this->preventClickjacking();
143 }
144 }
145 return $s;
146 }
147
148 public function doQuery() {
149 if ( $this->mQueryDone ) {
150 return;
151 }
152 $this->mImg = $this->mImagePage->getPage()->getFile(); // ensure loading
153 if ( !$this->mImg->exists() ) {
154 return;
155 }
156 // Make sure the date (probably from user input) is valid; if not, drop it.
157 if ( $this->mOffset !== null ) {
158 try {
159 $sadlyWeCannotPassThisTimestampDownTheStack = $this->mDb->timestamp( $this->mOffset );
160 } catch ( TimestampException $e ) {
161 $this->mOffset = null;
162 }
163 }
164 $queryLimit = $this->mLimit + 1; // limit plus extra row
165 if ( $this->mIsBackwards ) {
166 // Fetch the file history
167 $this->mHist = $this->mImg->getHistory( $queryLimit, null, $this->mOffset, false );
168 // The current rev may not meet the offset/limit
169 $numRows = count( $this->mHist );
170 if ( $numRows <= $this->mLimit && $this->mImg->getTimestamp() > $this->mOffset ) {
171 $this->mHist = array_merge( [ $this->mImg ], $this->mHist );
172 }
173 } else {
174 // The current rev may not meet the offset
175 if ( !$this->mOffset || $this->mImg->getTimestamp() < $this->mOffset ) {
176 $this->mHist[] = $this->mImg;
177 }
178 // Old image versions (fetch extra row for nav links)
179 $oiLimit = count( $this->mHist ) ? $this->mLimit : $this->mLimit + 1;
180 // Fetch the file history
181 $this->mHist = array_merge( $this->mHist,
182 $this->mImg->getHistory( $oiLimit, $this->mOffset, null, false ) );
183 }
184 $numRows = count( $this->mHist ); // Total number of query results
185 if ( $numRows ) {
186 # Index value of top item in the list
187 $firstIndex = $this->mIsBackwards ?
188 [ $this->mHist[$numRows - 1]->getTimestamp() ] : [ $this->mHist[0]->getTimestamp() ];
189 # Discard the extra result row if there is one
190 if ( $numRows > $this->mLimit && $numRows > 1 ) {
191 if ( $this->mIsBackwards ) {
192 # Index value of item past the index
193 $this->mPastTheEndIndex = [ $this->mHist[0]->getTimestamp() ];
194 # Index value of bottom item in the list
195 $lastIndex = [ $this->mHist[1]->getTimestamp() ];
196 # Display range
197 $this->mRange = [ 1, $numRows - 1 ];
198 } else {
199 # Index value of item past the index
200 $this->mPastTheEndIndex = [ $this->mHist[$numRows - 1]->getTimestamp() ];
201 # Index value of bottom item in the list
202 $lastIndex = [ $this->mHist[$numRows - 2]->getTimestamp() ];
203 # Display range
204 $this->mRange = [ 0, $numRows - 2 ];
205 }
206 } else {
207 # Setting indexes to an empty array means that they will be
208 # omitted if they would otherwise appear in URLs. It just so
209 # happens that this is the right thing to do in the standard
210 # UI, in all the relevant cases.
211 $this->mPastTheEndIndex = [];
212 # Index value of bottom item in the list
213 $lastIndex = $this->mIsBackwards ?
214 [ $this->mHist[0]->getTimestamp() ] : [ $this->mHist[$numRows - 1]->getTimestamp() ];
215 # Display range
216 $this->mRange = [ 0, $numRows - 1 ];
217 }
218 } else {
219 $firstIndex = [];
220 $lastIndex = [];
221 $this->mPastTheEndIndex = [];
222 }
223 if ( $this->mIsBackwards ) {
224 $this->mIsFirst = ( $numRows < $queryLimit );
225 $this->mIsLast = ( $this->mOffset == '' );
226 $this->mLastShown = $firstIndex;
227 $this->mFirstShown = $lastIndex;
228 } else {
229 $this->mIsFirst = ( $this->mOffset == '' );
230 $this->mIsLast = ( $numRows < $queryLimit );
231 $this->mLastShown = $lastIndex;
232 $this->mFirstShown = $firstIndex;
233 }
234 $this->mQueryDone = true;
235 }
236
240 protected function preventClickjacking( $enable = true ) {
241 $this->preventClickjacking = $enable;
242 }
243
247 public function getPreventClickjacking() {
249 }
250
251}
const NS_USER
Definition Defines.php:66
const NS_USER_TALK
Definition Defines.php:67
Implements some public methods and some protected utility functions which are required by multiple ch...
Definition File.php:66
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.
Class for viewing MediaWiki file description pages.
Definition ImagePage.php:34
MediaWikiServices is the service locator for the application scope of MediaWiki.
Efficient paging for SQL queries.
Represents a title within MediaWiki.
Definition Title.php:48
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