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 Wikimedia\Timestamp\TimestampException
;
23 class ImageHistoryPseudoPager
extends ReverseChronologicalPager
{
24 protected $preventClickjacking = false;
55 * @param ImagePage $imagePage
57 public function __construct( $imagePage ) {
58 parent
::__construct( $imagePage->getContext() );
59 $this->mImagePage
= $imagePage;
60 $this->mTitle
= $imagePage->getTitle()->createFragmentTarget( 'filehistory' );
63 $this->mRange
= [ 0, 0 ]; // display range
65 // Only display 10 revisions at once by default, otherwise the list is overwhelming
66 $this->mLimitsShown
= array_merge( [ 10 ], $this->mLimitsShown
);
67 $this->mDefaultLimit
= 10;
68 list( $this->mLimit
, /* $offset */ ) =
69 $this->mRequest
->getLimitOffset( $this->mDefaultLimit
, '' );
75 public function getTitle() {
79 public function getQueryInfo() {
86 public function getIndexField() {
94 public function formatRow( $row ) {
101 public function getBody() {
104 if ( count( $this->mHist
) ) {
105 if ( $this->mImg
->isLocal() ) {
106 // Do a batch existence check for user pages and talkpages
107 $linkBatch = new LinkBatch();
108 for ( $i = $this->mRange
[0]; $i <= $this->mRange
[1]; $i++
) {
109 $file = $this->mHist
[$i];
110 $user = $file->getUser( 'text' );
111 $linkBatch->add( NS_USER
, $user );
112 $linkBatch->add( NS_USER_TALK
, $user );
114 $linkBatch->execute();
117 $list = new ImageHistoryList( $this->mImagePage
);
118 # Generate prev/next links
119 $navLink = $this->getNavigationBar();
120 $s = $list->beginImageHistoryList( $navLink );
121 // Skip rows there just for paging links
122 for ( $i = $this->mRange
[0]; $i <= $this->mRange
[1]; $i++
) {
123 $file = $this->mHist
[$i];
124 $s .= $list->imageHistoryLine( !$file->isOld(), $file );
126 $s .= $list->endImageHistoryList( $navLink );
128 if ( $list->getPreventClickjacking() ) {
129 $this->preventClickjacking();
135 public function doQuery() {
136 if ( $this->mQueryDone
) {
139 $this->mImg
= $this->mImagePage
->getPage()->getFile(); // ensure loading
140 if ( !$this->mImg
->exists() ) {
143 // Make sure the date (probably from user input) is valid; if not, drop it.
144 if ( $this->mOffset
!== null ) {
146 $sadlyWeCannotPassThisTimestampDownTheStack = $this->mDb
->timestamp( $this->mOffset
);
147 } catch ( TimestampException
$e ) {
148 $this->mOffset
= null;
151 $queryLimit = $this->mLimit +
1; // limit plus extra row
152 if ( $this->mIsBackwards
) {
153 // Fetch the file history
154 $this->mHist
= $this->mImg
->getHistory( $queryLimit, null, $this->mOffset
, false );
155 // The current rev may not meet the offset/limit
156 $numRows = count( $this->mHist
);
157 if ( $numRows <= $this->mLimit
&& $this->mImg
->getTimestamp() > $this->mOffset
) {
158 $this->mHist
= array_merge( [ $this->mImg
], $this->mHist
);
161 // The current rev may not meet the offset
162 if ( !$this->mOffset ||
$this->mImg
->getTimestamp() < $this->mOffset
) {
163 $this->mHist
[] = $this->mImg
;
165 // Old image versions (fetch extra row for nav links)
166 $oiLimit = count( $this->mHist
) ?
$this->mLimit
: $this->mLimit +
1;
167 // Fetch the file history
168 $this->mHist
= array_merge( $this->mHist
,
169 $this->mImg
->getHistory( $oiLimit, $this->mOffset
, null, false ) );
171 $numRows = count( $this->mHist
); // Total number of query results
173 # Index value of top item in the list
174 $firstIndex = $this->mIsBackwards ?
175 $this->mHist
[$numRows - 1]->getTimestamp() : $this->mHist
[0]->getTimestamp();
176 # Discard the extra result row if there is one
177 if ( $numRows > $this->mLimit
&& $numRows > 1 ) {
178 if ( $this->mIsBackwards
) {
179 # Index value of item past the index
180 $this->mPastTheEndIndex
= $this->mHist
[0]->getTimestamp();
181 # Index value of bottom item in the list
182 $lastIndex = $this->mHist
[1]->getTimestamp();
184 $this->mRange
= [ 1, $numRows - 1 ];
186 # Index value of item past the index
187 $this->mPastTheEndIndex
= $this->mHist
[$numRows - 1]->getTimestamp();
188 # Index value of bottom item in the list
189 $lastIndex = $this->mHist
[$numRows - 2]->getTimestamp();
191 $this->mRange
= [ 0, $numRows - 2 ];
194 # Setting indexes to an empty string means that they will be
195 # omitted if they would otherwise appear in URLs. It just so
196 # happens that this is the right thing to do in the standard
197 # UI, in all the relevant cases.
198 $this->mPastTheEndIndex
= '';
199 # Index value of bottom item in the list
200 $lastIndex = $this->mIsBackwards ?
201 $this->mHist
[0]->getTimestamp() : $this->mHist
[$numRows - 1]->getTimestamp();
203 $this->mRange
= [ 0, $numRows - 1 ];
208 $this->mPastTheEndIndex
= '';
210 if ( $this->mIsBackwards
) {
211 $this->mIsFirst
= ( $numRows < $queryLimit );
212 $this->mIsLast
= ( $this->mOffset
== '' );
213 $this->mLastShown
= $firstIndex;
214 $this->mFirstShown
= $lastIndex;
216 $this->mIsFirst
= ( $this->mOffset
== '' );
217 $this->mIsLast
= ( $numRows < $queryLimit );
218 $this->mLastShown
= $lastIndex;
219 $this->mFirstShown
= $firstIndex;
221 $this->mQueryDone
= true;
225 * @param bool $enable
227 protected function preventClickjacking( $enable = true ) {
228 $this->preventClickjacking
= $enable;
234 public function getPreventClickjacking() {
235 return $this->preventClickjacking
;