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