Merge "Special:Upload should not crash on failing previews"
[mediawiki.git] / includes / page / ImageHistoryPseudoPager.php
blob58f1666842eb21e618d341e571752fb951e436a5
1 <?php
2 /**
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
18 * @file
21 class ImageHistoryPseudoPager extends ReverseChronologicalPager {
22 protected $preventClickjacking = false;
24 /**
25 * @var File
27 protected $mImg;
29 /**
30 * @var Title
32 protected $mTitle;
34 /**
35 * @param ImagePage $imagePage
37 function __construct( $imagePage ) {
38 parent::__construct( $imagePage->getContext() );
39 $this->mImagePage = $imagePage;
40 $this->mTitle = clone $imagePage->getTitle();
41 $this->mTitle->setFragment( '#filehistory' );
42 $this->mImg = null;
43 $this->mHist = [];
44 $this->mRange = [ 0, 0 ]; // display range
46 // Only display 10 revisions at once by default, otherwise the list is overwhelming
47 $this->mLimitsShown = array_merge( [ 10 ], $this->mLimitsShown );
48 $this->mDefaultLimit = 10;
49 list( $this->mLimit, /* $offset */ ) =
50 $this->mRequest->getLimitOffset( $this->mDefaultLimit, '' );
53 /**
54 * @return Title
56 function getTitle() {
57 return $this->mTitle;
60 function getQueryInfo() {
61 return false;
64 /**
65 * @return string
67 function getIndexField() {
68 return '';
71 /**
72 * @param object $row
73 * @return string
75 function formatRow( $row ) {
76 return '';
79 /**
80 * @return string
82 function getBody() {
83 $s = '';
84 $this->doQuery();
85 if ( count( $this->mHist ) ) {
86 if ( $this->mImg->isLocal() ) {
87 // Do a batch existence check for user pages and talkpages
88 $linkBatch = new LinkBatch();
89 for ( $i = $this->mRange[0]; $i <= $this->mRange[1]; $i++ ) {
90 $file = $this->mHist[$i];
91 $user = $file->getUser( 'text' );
92 $linkBatch->add( NS_USER, $user );
93 $linkBatch->add( NS_USER_TALK, $user );
95 $linkBatch->execute();
98 $list = new ImageHistoryList( $this->mImagePage );
99 # Generate prev/next links
100 $navLink = $this->getNavigationBar();
101 $s = $list->beginImageHistoryList( $navLink );
102 // Skip rows there just for paging links
103 for ( $i = $this->mRange[0]; $i <= $this->mRange[1]; $i++ ) {
104 $file = $this->mHist[$i];
105 $s .= $list->imageHistoryLine( !$file->isOld(), $file );
107 $s .= $list->endImageHistoryList( $navLink );
109 if ( $list->getPreventClickjacking() ) {
110 $this->preventClickjacking();
113 return $s;
116 function doQuery() {
117 if ( $this->mQueryDone ) {
118 return;
120 $this->mImg = $this->mImagePage->getPage()->getFile(); // ensure loading
121 if ( !$this->mImg->exists() ) {
122 return;
124 $queryLimit = $this->mLimit + 1; // limit plus extra row
125 if ( $this->mIsBackwards ) {
126 // Fetch the file history
127 $this->mHist = $this->mImg->getHistory( $queryLimit, null, $this->mOffset, false );
128 // The current rev may not meet the offset/limit
129 $numRows = count( $this->mHist );
130 if ( $numRows <= $this->mLimit && $this->mImg->getTimestamp() > $this->mOffset ) {
131 $this->mHist = array_merge( [ $this->mImg ], $this->mHist );
133 } else {
134 // The current rev may not meet the offset
135 if ( !$this->mOffset || $this->mImg->getTimestamp() < $this->mOffset ) {
136 $this->mHist[] = $this->mImg;
138 // Old image versions (fetch extra row for nav links)
139 $oiLimit = count( $this->mHist ) ? $this->mLimit : $this->mLimit + 1;
140 // Fetch the file history
141 $this->mHist = array_merge( $this->mHist,
142 $this->mImg->getHistory( $oiLimit, $this->mOffset, null, false ) );
144 $numRows = count( $this->mHist ); // Total number of query results
145 if ( $numRows ) {
146 # Index value of top item in the list
147 $firstIndex = $this->mIsBackwards ?
148 $this->mHist[$numRows - 1]->getTimestamp() : $this->mHist[0]->getTimestamp();
149 # Discard the extra result row if there is one
150 if ( $numRows > $this->mLimit && $numRows > 1 ) {
151 if ( $this->mIsBackwards ) {
152 # Index value of item past the index
153 $this->mPastTheEndIndex = $this->mHist[0]->getTimestamp();
154 # Index value of bottom item in the list
155 $lastIndex = $this->mHist[1]->getTimestamp();
156 # Display range
157 $this->mRange = [ 1, $numRows - 1 ];
158 } else {
159 # Index value of item past the index
160 $this->mPastTheEndIndex = $this->mHist[$numRows - 1]->getTimestamp();
161 # Index value of bottom item in the list
162 $lastIndex = $this->mHist[$numRows - 2]->getTimestamp();
163 # Display range
164 $this->mRange = [ 0, $numRows - 2 ];
166 } else {
167 # Setting indexes to an empty string means that they will be
168 # omitted if they would otherwise appear in URLs. It just so
169 # happens that this is the right thing to do in the standard
170 # UI, in all the relevant cases.
171 $this->mPastTheEndIndex = '';
172 # Index value of bottom item in the list
173 $lastIndex = $this->mIsBackwards ?
174 $this->mHist[0]->getTimestamp() : $this->mHist[$numRows - 1]->getTimestamp();
175 # Display range
176 $this->mRange = [ 0, $numRows - 1 ];
178 } else {
179 $firstIndex = '';
180 $lastIndex = '';
181 $this->mPastTheEndIndex = '';
183 if ( $this->mIsBackwards ) {
184 $this->mIsFirst = ( $numRows < $queryLimit );
185 $this->mIsLast = ( $this->mOffset == '' );
186 $this->mLastShown = $firstIndex;
187 $this->mFirstShown = $lastIndex;
188 } else {
189 $this->mIsFirst = ( $this->mOffset == '' );
190 $this->mIsLast = ( $numRows < $queryLimit );
191 $this->mLastShown = $lastIndex;
192 $this->mFirstShown = $firstIndex;
194 $this->mQueryDone = true;
198 * @param bool $enable
200 protected function preventClickjacking( $enable = true ) {
201 $this->preventClickjacking = $enable;
205 * @return bool
207 public function getPreventClickjacking() {
208 return $this->preventClickjacking;