3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation; either version 2 of the License, or
6 * (at your option) any later version.
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
13 * You should have received a copy of the GNU General Public License along
14 * with this program; if not, write to the Free Software Foundation, Inc.,
15 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
16 * http://www.gnu.org/copyleft/gpl.html
21 use MediaWiki\Cache\LinkBatchFactory
;
22 use MediaWiki\Html\Html
;
23 use MediaWiki\MediaWikiServices
;
24 use MediaWiki\Pager\ReverseChronologicalPager
;
25 use MediaWiki\SpecialPage\SpecialPage
;
26 use MediaWiki\Title\Title
;
27 use Wikimedia\Timestamp\TimestampException
;
29 class ImageHistoryPseudoPager
extends ReverseChronologicalPager
{
31 protected $preventClickjacking = false;
61 /** @var LinkBatchFactory */
62 private $linkBatchFactory;
65 * @param ImagePage $imagePage
66 * @param LinkBatchFactory|null $linkBatchFactory
68 public function __construct( $imagePage, ?LinkBatchFactory
$linkBatchFactory = null ) {
69 parent
::__construct( $imagePage->getContext() );
70 $this->mImagePage
= $imagePage;
71 $this->mTitle
= $imagePage->getTitle()->createFragmentTarget( 'filehistory' );
74 $this->mRange
= [ 0, 0 ]; // display range
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(
85 $this->linkBatchFactory
= $linkBatchFactory ?? MediaWikiServices
::getInstance()->getLinkBatchFactory();
91 public function getTitle() {
95 public function getQueryInfo() {
102 public function getIndexField() {
107 * @param stdClass $row
110 public function formatRow( $row ) {
117 public function getBody() {
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() );
128 $linkBatch->add( NS_USER
, $uploader->getName() );
129 $linkBatch->add( NS_USER_TALK
, $uploader->getName() );
132 $linkBatch->execute();
135 // Batch-format comments
137 for ( $i = $this->mRange
[0]; $i <= $this->mRange
[1]; $i++
) {
138 $file = $this->mHist
[$i];
139 $comments[$i] = $file->getDescription(
141 $this->getAuthority()
144 $formattedComments = MediaWikiServices
::getInstance()
145 ->getCommentFormatter()
146 ->formatStrings( $comments, $this->getTitle() );
148 $list = new ImageHistoryList( $this->mImagePage
);
149 # Generate prev/next links
150 $navLink = $this->getNavigationBar();
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()
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();
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
171 $s .= $this->wrapWithActionButtons( $sList );
173 $s .= $navLink . "\n" . Html
::closeElement( 'div' ) . "\n";
175 if ( $list->getPreventClickjacking() ) {
176 $this->setPreventClickjacking( true );
182 public function doQuery() {
183 if ( $this->mQueryDone
) {
186 $this->mImg
= $this->mImagePage
->getPage()->getFile(); // ensure loading
187 if ( !$this->mImg
->exists() ) {
190 // Make sure the date (probably from user input) is valid; if not, drop it.
191 if ( $this->mOffset
!== null ) {
193 $this->mDb
->timestamp( $this->mOffset
);
194 } catch ( TimestampException
$e ) {
195 $this->mOffset
= null;
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
);
208 // The current rev may not meet the offset
209 if ( !$this->mOffset ||
$this->mImg
->getTimestamp() < $this->mOffset
) {
210 $this->mHist
[] = $this->mImg
;
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 ) );
218 $numRows = count( $this->mHist
); // Total number of query results
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() ];
231 $this->mRange
= [ 1, $numRows - 1 ];
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() ];
238 $this->mRange
= [ 0, $numRows - 2 ];
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() ];
250 $this->mRange
= [ 0, $numRows - 1 ];
255 $this->mPastTheEndIndex
= [];
257 if ( $this->mIsBackwards
) {
258 $this->mIsFirst
= ( $numRows < $queryLimit );
259 $this->mIsLast
= ( $this->mOffset
== '' );
260 $this->mLastShown
= $firstIndex;
261 $this->mFirstShown
= $lastIndex;
263 $this->mIsFirst
= ( $this->mOffset
== '' );
264 $this->mIsLast
= ( $numRows < $queryLimit );
265 $this->mLastShown
= $lastIndex;
266 $this->mFirstShown
= $firstIndex;
268 $this->mQueryDone
= true;
272 * Wrap the content with action buttons at begin and end if the user
273 * is allow to use the action buttons.
274 * @param string $formcontents
277 private function wrapWithActionButtons( $formcontents ) {
278 if ( !$this->getAuthority()->isAllowed( 'deleterevision' ) ) {
279 return $formcontents;
282 # Show button to hide log entries
283 $s = Html
::openElement(
285 [ 'action' => wfScript(), 'id' => 'mw-filehistory-deleterevision-submit' ]
287 $s .= Html
::hidden( 'target', $this->getTitle()->getPrefixedDBkey() ) . "\n";
288 $s .= Html
::hidden( 'type', 'oldimage' ) . "\n";
289 $this->setPreventClickjacking( true );
291 $buttons = Html
::element(
296 'value' => SpecialPage
::getTitleFor( 'Revisiondelete' )->getPrefixedDBkey(),
297 'class' => "deleterevision-filehistory-submit mw-filehistory-deleterevision-button mw-ui-button"
299 $this->msg( 'showhideselectedfileversions' )->text()
302 $s .= $buttons . $formcontents . $buttons;
303 $s .= Html
::closeElement( 'form' );
309 * @param bool $enable
310 * @deprecated since 1.38, use ::setPreventClickjacking()
312 protected function preventClickjacking( $enable = true ) {
313 $this->preventClickjacking
= $enable;
317 * @param bool $enable
320 protected function setPreventClickjacking( bool $enable ) {
321 $this->preventClickjacking
= $enable;
327 public function getPreventClickjacking() {
328 return $this->preventClickjacking
;