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