MediaWiki master
ImageHistoryPseudoPager.php
Go to the documentation of this file.
1<?php
27use Wikimedia\Timestamp\TimestampException;
28
30 protected $preventClickjacking = false;
31
35 protected $mImg;
36
40 protected $mTitle;
41
47
52 public $mHist;
53
58 public $mRange;
59
61 private $linkBatchFactory;
62
67 public function __construct( $imagePage, LinkBatchFactory $linkBatchFactory = null ) {
68 parent::__construct( $imagePage->getContext() );
69 $this->mImagePage = $imagePage;
70 $this->mTitle = $imagePage->getTitle()->createFragmentTarget( 'filehistory' );
71 $this->mImg = null;
72 $this->mHist = [];
73 $this->mRange = [ 0, 0 ]; // display range
74
75 // Only display 10 revisions at once by default, otherwise the list is overwhelming
76 $this->mLimitsShown = array_merge( [ 10 ], $this->mLimitsShown );
77 $this->mDefaultLimit = 10;
78 [ $this->mLimit, /* $offset */ ] =
79 $this->mRequest->getLimitOffsetForUser(
80 $this->getUser(),
81 $this->mDefaultLimit,
82 ''
83 );
84 $this->linkBatchFactory = $linkBatchFactory ?? MediaWikiServices::getInstance()->getLinkBatchFactory();
85 }
86
90 public function getTitle() {
91 return $this->mTitle;
92 }
93
94 public function getQueryInfo() {
95 return [];
96 }
97
101 public function getIndexField() {
102 return '';
103 }
104
109 public function formatRow( $row ) {
110 return '';
111 }
112
116 public function getBody() {
117 $s = '';
118 $this->doQuery();
119 if ( count( $this->mHist ) ) {
120 if ( $this->mImg->isLocal() ) {
121 // Do a batch existence check for user pages and talkpages.
122 $linkBatch = $this->linkBatchFactory->newLinkBatch();
123 for ( $i = $this->mRange[0]; $i <= $this->mRange[1]; $i++ ) {
124 $file = $this->mHist[$i];
125 $uploader = $file->getUploader( File::FOR_THIS_USER, $this->getAuthority() );
126 if ( $uploader ) {
127 $linkBatch->add( NS_USER, $uploader->getName() );
128 $linkBatch->add( NS_USER_TALK, $uploader->getName() );
129 }
130 }
131 $linkBatch->execute();
132 }
133
134 // Batch-format comments
135 $comments = [];
136 for ( $i = $this->mRange[0]; $i <= $this->mRange[1]; $i++ ) {
137 $file = $this->mHist[$i];
138 $comments[$i] = $file->getDescription(
139 File::FOR_THIS_USER,
140 $this->getAuthority()
141 ) ?: '';
142 }
143 $formattedComments = MediaWikiServices::getInstance()
144 ->getCommentFormatter()
145 ->formatStrings( $comments, $this->getTitle() );
146
147 $list = new ImageHistoryList( $this->mImagePage );
148 # Generate prev/next links
149 $navLink = $this->getNavigationBar();
150
151 $s = Html::element( 'h2', [ 'id' => 'filehistory' ], $this->msg( 'filehist' )->text() ) . "\n"
152 . Html::openElement( 'div', [ 'id' => 'mw-imagepage-section-filehistory' ] ) . "\n"
153 . $this->msg( 'filehist-help' )->parseAsBlock()
154 . $navLink . "\n";
155
156 $sList = $list->beginImageHistoryList();
157 $onlyCurrentFile = true;
158 // Skip rows there just for paging links
159 for ( $i = $this->mRange[0]; $i <= $this->mRange[1]; $i++ ) {
160 $file = $this->mHist[$i];
161 $sList .= $list->imageHistoryLine( !$file->isOld(), $file, $formattedComments[$i] );
162 $onlyCurrentFile = !$file->isOld();
163 }
164 $sList .= $list->endImageHistoryList();
165 if ( $onlyCurrentFile || !$this->mImg->isLocal() ) {
166 // It is not possible to revision-delete the current file or foreign files,
167 // if there is only the current file or the file is not local, show no buttons
168 $s .= $sList;
169 } else {
170 $s .= $this->wrapWithActionButtons( $sList );
171 }
172 $s .= $navLink . "\n" . Html::closeElement( 'div' ) . "\n";
173
174 if ( $list->getPreventClickjacking() ) {
175 $this->setPreventClickjacking( true );
176 }
177 }
178 return $s;
179 }
180
181 public function doQuery() {
182 if ( $this->mQueryDone ) {
183 return;
184 }
185 $this->mImg = $this->mImagePage->getPage()->getFile(); // ensure loading
186 if ( !$this->mImg->exists() ) {
187 return;
188 }
189 // Make sure the date (probably from user input) is valid; if not, drop it.
190 if ( $this->mOffset !== null ) {
191 try {
192 $this->mDb->timestamp( $this->mOffset );
193 } catch ( TimestampException $e ) {
194 $this->mOffset = null;
195 }
196 }
197 $queryLimit = $this->mLimit + 1; // limit plus extra row
198 if ( $this->mIsBackwards ) {
199 // Fetch the file history
200 $this->mHist = $this->mImg->getHistory( $queryLimit, null, $this->mOffset, false );
201 // The current rev may not meet the offset/limit
202 $numRows = count( $this->mHist );
203 if ( $numRows <= $this->mLimit && $this->mImg->getTimestamp() > $this->mOffset ) {
204 $this->mHist = array_merge( [ $this->mImg ], $this->mHist );
205 }
206 } else {
207 // The current rev may not meet the offset
208 if ( !$this->mOffset || $this->mImg->getTimestamp() < $this->mOffset ) {
209 $this->mHist[] = $this->mImg;
210 }
211 // Old image versions (fetch extra row for nav links)
212 $oiLimit = count( $this->mHist ) ? $this->mLimit : $this->mLimit + 1;
213 // Fetch the file history
214 $this->mHist = array_merge( $this->mHist,
215 $this->mImg->getHistory( $oiLimit, $this->mOffset, null, false ) );
216 }
217 $numRows = count( $this->mHist ); // Total number of query results
218 if ( $numRows ) {
219 # Index value of top item in the list
220 $firstIndex = $this->mIsBackwards ?
221 [ $this->mHist[$numRows - 1]->getTimestamp() ] : [ $this->mHist[0]->getTimestamp() ];
222 # Discard the extra result row if there is one
223 if ( $numRows > $this->mLimit && $numRows > 1 ) {
224 if ( $this->mIsBackwards ) {
225 # Index value of item past the index
226 $this->mPastTheEndIndex = [ $this->mHist[0]->getTimestamp() ];
227 # Index value of bottom item in the list
228 $lastIndex = [ $this->mHist[1]->getTimestamp() ];
229 # Display range
230 $this->mRange = [ 1, $numRows - 1 ];
231 } else {
232 # Index value of item past the index
233 $this->mPastTheEndIndex = [ $this->mHist[$numRows - 1]->getTimestamp() ];
234 # Index value of bottom item in the list
235 $lastIndex = [ $this->mHist[$numRows - 2]->getTimestamp() ];
236 # Display range
237 $this->mRange = [ 0, $numRows - 2 ];
238 }
239 } else {
240 # Setting indexes to an empty array means that they will be
241 # omitted if they would otherwise appear in URLs. It just so
242 # happens that this is the right thing to do in the standard
243 # UI, in all the relevant cases.
244 $this->mPastTheEndIndex = [];
245 # Index value of bottom item in the list
246 $lastIndex = $this->mIsBackwards ?
247 [ $this->mHist[0]->getTimestamp() ] : [ $this->mHist[$numRows - 1]->getTimestamp() ];
248 # Display range
249 $this->mRange = [ 0, $numRows - 1 ];
250 }
251 } else {
252 $firstIndex = [];
253 $lastIndex = [];
254 $this->mPastTheEndIndex = [];
255 }
256 if ( $this->mIsBackwards ) {
257 $this->mIsFirst = ( $numRows < $queryLimit );
258 $this->mIsLast = ( $this->mOffset == '' );
259 $this->mLastShown = $firstIndex;
260 $this->mFirstShown = $lastIndex;
261 } else {
262 $this->mIsFirst = ( $this->mOffset == '' );
263 $this->mIsLast = ( $numRows < $queryLimit );
264 $this->mLastShown = $lastIndex;
265 $this->mFirstShown = $firstIndex;
266 }
267 $this->mQueryDone = true;
268 }
269
276 private function wrapWithActionButtons( $formcontents ) {
277 if ( !$this->getAuthority()->isAllowed( 'deleterevision' ) ) {
278 return $formcontents;
279 }
280
281 # Show button to hide log entries
282 $s = Html::openElement(
283 'form',
284 [ 'action' => wfScript(), 'id' => 'mw-filehistory-deleterevision-submit' ]
285 ) . "\n";
286 $s .= Html::hidden( 'target', $this->getTitle()->getPrefixedDBkey() ) . "\n";
287 $s .= Html::hidden( 'type', 'oldimage' ) . "\n";
288 $this->setPreventClickjacking( true );
289
290 $buttons = Html::element(
291 'button',
292 [
293 'type' => 'submit',
294 'name' => 'title',
295 'value' => SpecialPage::getTitleFor( 'Revisiondelete' )->getPrefixedDBkey(),
296 'class' => "deleterevision-filehistory-submit mw-filehistory-deleterevision-button mw-ui-button"
297 ],
298 $this->msg( 'showhideselectedfileversions' )->text()
299 ) . "\n";
300
301 $s .= $buttons . $formcontents . $buttons;
302 $s .= Html::closeElement( 'form' );
303
304 return $s;
305 }
306
311 protected function preventClickjacking( $enable = true ) {
312 $this->preventClickjacking = $enable;
313 }
314
319 protected function setPreventClickjacking( bool $enable ) {
320 $this->preventClickjacking = $enable;
321 }
322
326 public function getPreventClickjacking() {
328 }
329
330}
const NS_USER
Definition Defines.php:66
const NS_USER_TALK
Definition Defines.php:67
wfScript( $script='index')
Get the URL path to a MediaWiki entry point.
Implements some public methods and some protected utility functions which are required by multiple ch...
Definition File.php:73
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.
Rendering of file description pages.
Definition ImagePage.php:37
msg( $key,... $params)
Get a Message object with context set Parameters are the same as wfMessage()
This class is a collection of static functions that serve two purposes:
Definition Html.php:56
Service locator for MediaWiki core services.
int $mLimit
The maximum number of entries to show.
IndexPager with a formatted navigation bar.
Parent class for all special pages.
Represents a title within MediaWiki.
Definition Title.php:78