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 class ImageHistoryPseudoPager
extends ReverseChronologicalPager
{
22 protected $preventClickjacking = false;
53 * @param ImagePage $imagePage
55 public function __construct( $imagePage ) {
56 parent
::__construct( $imagePage->getContext() );
57 $this->mImagePage
= $imagePage;
58 $this->mTitle
= $imagePage->getTitle()->createFragmentTarget( 'filehistory' );
61 $this->mRange
= [ 0, 0 ]; // display range
63 // Only display 10 revisions at once by default, otherwise the list is overwhelming
64 $this->mLimitsShown
= array_merge( [ 10 ], $this->mLimitsShown
);
65 $this->mDefaultLimit
= 10;
66 list( $this->mLimit
, /* $offset */ ) =
67 $this->mRequest
->getLimitOffset( $this->mDefaultLimit
, '' );
73 public function getTitle() {
77 public function getQueryInfo() {
84 public function getIndexField() {
92 public function formatRow( $row ) {
99 public function getBody() {
102 if ( count( $this->mHist
) ) {
103 if ( $this->mImg
->isLocal() ) {
104 // Do a batch existence check for user pages and talkpages
105 $linkBatch = new LinkBatch();
106 for ( $i = $this->mRange
[0]; $i <= $this->mRange
[1]; $i++
) {
107 $file = $this->mHist
[$i];
108 $user = $file->getUser( 'text' );
109 $linkBatch->add( NS_USER
, $user );
110 $linkBatch->add( NS_USER_TALK
, $user );
112 $linkBatch->execute();
115 $list = new ImageHistoryList( $this->mImagePage
);
116 # Generate prev/next links
117 $navLink = $this->getNavigationBar();
118 $s = $list->beginImageHistoryList( $navLink );
119 // Skip rows there just for paging links
120 for ( $i = $this->mRange
[0]; $i <= $this->mRange
[1]; $i++
) {
121 $file = $this->mHist
[$i];
122 $s .= $list->imageHistoryLine( !$file->isOld(), $file );
124 $s .= $list->endImageHistoryList( $navLink );
126 if ( $list->getPreventClickjacking() ) {
127 $this->preventClickjacking();
133 public function doQuery() {
134 if ( $this->mQueryDone
) {
137 $this->mImg
= $this->mImagePage
->getPage()->getFile(); // ensure loading
138 if ( !$this->mImg
->exists() ) {
141 $queryLimit = $this->mLimit +
1; // limit plus extra row
142 if ( $this->mIsBackwards
) {
143 // Fetch the file history
144 $this->mHist
= $this->mImg
->getHistory( $queryLimit, null, $this->mOffset
, false );
145 // The current rev may not meet the offset/limit
146 $numRows = count( $this->mHist
);
147 if ( $numRows <= $this->mLimit
&& $this->mImg
->getTimestamp() > $this->mOffset
) {
148 $this->mHist
= array_merge( [ $this->mImg
], $this->mHist
);
151 // The current rev may not meet the offset
152 if ( !$this->mOffset ||
$this->mImg
->getTimestamp() < $this->mOffset
) {
153 $this->mHist
[] = $this->mImg
;
155 // Old image versions (fetch extra row for nav links)
156 $oiLimit = count( $this->mHist
) ?
$this->mLimit
: $this->mLimit +
1;
157 // Fetch the file history
158 $this->mHist
= array_merge( $this->mHist
,
159 $this->mImg
->getHistory( $oiLimit, $this->mOffset
, null, false ) );
161 $numRows = count( $this->mHist
); // Total number of query results
163 # Index value of top item in the list
164 $firstIndex = $this->mIsBackwards ?
165 $this->mHist
[$numRows - 1]->getTimestamp() : $this->mHist
[0]->getTimestamp();
166 # Discard the extra result row if there is one
167 if ( $numRows > $this->mLimit
&& $numRows > 1 ) {
168 if ( $this->mIsBackwards
) {
169 # Index value of item past the index
170 $this->mPastTheEndIndex
= $this->mHist
[0]->getTimestamp();
171 # Index value of bottom item in the list
172 $lastIndex = $this->mHist
[1]->getTimestamp();
174 $this->mRange
= [ 1, $numRows - 1 ];
176 # Index value of item past the index
177 $this->mPastTheEndIndex
= $this->mHist
[$numRows - 1]->getTimestamp();
178 # Index value of bottom item in the list
179 $lastIndex = $this->mHist
[$numRows - 2]->getTimestamp();
181 $this->mRange
= [ 0, $numRows - 2 ];
184 # Setting indexes to an empty string means that they will be
185 # omitted if they would otherwise appear in URLs. It just so
186 # happens that this is the right thing to do in the standard
187 # UI, in all the relevant cases.
188 $this->mPastTheEndIndex
= '';
189 # Index value of bottom item in the list
190 $lastIndex = $this->mIsBackwards ?
191 $this->mHist
[0]->getTimestamp() : $this->mHist
[$numRows - 1]->getTimestamp();
193 $this->mRange
= [ 0, $numRows - 1 ];
198 $this->mPastTheEndIndex
= '';
200 if ( $this->mIsBackwards
) {
201 $this->mIsFirst
= ( $numRows < $queryLimit );
202 $this->mIsLast
= ( $this->mOffset
== '' );
203 $this->mLastShown
= $firstIndex;
204 $this->mFirstShown
= $lastIndex;
206 $this->mIsFirst
= ( $this->mOffset
== '' );
207 $this->mIsLast
= ( $numRows < $queryLimit );
208 $this->mLastShown
= $lastIndex;
209 $this->mFirstShown
= $firstIndex;
211 $this->mQueryDone
= true;
215 * @param bool $enable
217 protected function preventClickjacking( $enable = true ) {
218 $this->preventClickjacking
= $enable;
224 public function getPreventClickjacking() {
225 return $this->preventClickjacking
;