Merge "Add .pipeline/ with dev image variant"
[lhc/web/wiklou.git] / includes / page / ImageHistoryPseudoPager.php
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.
7 *
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.
12 *
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
17 *
18 * @file
19 */
20
21 use Wikimedia\Timestamp\TimestampException;
22
23 class ImageHistoryPseudoPager extends ReverseChronologicalPager {
24 protected $preventClickjacking = false;
25
26 /**
27 * @var File
28 */
29 protected $mImg;
30
31 /**
32 * @var Title
33 */
34 protected $mTitle;
35
36 /**
37 * @since 1.14
38 * @var ImagePage
39 */
40 public $mImagePage;
41
42 /**
43 * @since 1.14
44 * @var File[]
45 */
46 public $mHist;
47
48 /**
49 * @since 1.14
50 * @var int[]
51 */
52 public $mRange;
53
54 /**
55 * @param ImagePage $imagePage
56 */
57 public function __construct( $imagePage ) {
58 parent::__construct( $imagePage->getContext() );
59 $this->mImagePage = $imagePage;
60 $this->mTitle = $imagePage->getTitle()->createFragmentTarget( 'filehistory' );
61 $this->mImg = null;
62 $this->mHist = [];
63 $this->mRange = [ 0, 0 ]; // display range
64
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, '' );
70 }
71
72 /**
73 * @return Title
74 */
75 public function getTitle() {
76 return $this->mTitle;
77 }
78
79 public function getQueryInfo() {
80 return [];
81 }
82
83 /**
84 * @return string
85 */
86 public function getIndexField() {
87 return '';
88 }
89
90 /**
91 * @param object $row
92 * @return string
93 */
94 public function formatRow( $row ) {
95 return '';
96 }
97
98 /**
99 * @return string
100 */
101 public function getBody() {
102 $s = '';
103 $this->doQuery();
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 );
113 }
114 $linkBatch->execute();
115 }
116
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 );
125 }
126 $s .= $list->endImageHistoryList( $navLink );
127
128 if ( $list->getPreventClickjacking() ) {
129 $this->preventClickjacking();
130 }
131 }
132 return $s;
133 }
134
135 public function doQuery() {
136 if ( $this->mQueryDone ) {
137 return;
138 }
139 $this->mImg = $this->mImagePage->getPage()->getFile(); // ensure loading
140 if ( !$this->mImg->exists() ) {
141 return;
142 }
143 // Make sure the date (probably from user input) is valid; if not, drop it.
144 if ( $this->mOffset !== null ) {
145 try {
146 $sadlyWeCannotPassThisTimestampDownTheStack = $this->mDb->timestamp( $this->mOffset );
147 } catch ( TimestampException $e ) {
148 $this->mOffset = null;
149 }
150 }
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 );
159 }
160 } else {
161 // The current rev may not meet the offset
162 if ( !$this->mOffset || $this->mImg->getTimestamp() < $this->mOffset ) {
163 $this->mHist[] = $this->mImg;
164 }
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 ) );
170 }
171 $numRows = count( $this->mHist ); // Total number of query results
172 if ( $numRows ) {
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();
183 # Display range
184 $this->mRange = [ 1, $numRows - 1 ];
185 } else {
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();
190 # Display range
191 $this->mRange = [ 0, $numRows - 2 ];
192 }
193 } else {
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();
202 # Display range
203 $this->mRange = [ 0, $numRows - 1 ];
204 }
205 } else {
206 $firstIndex = '';
207 $lastIndex = '';
208 $this->mPastTheEndIndex = '';
209 }
210 if ( $this->mIsBackwards ) {
211 $this->mIsFirst = ( $numRows < $queryLimit );
212 $this->mIsLast = ( $this->mOffset == '' );
213 $this->mLastShown = $firstIndex;
214 $this->mFirstShown = $lastIndex;
215 } else {
216 $this->mIsFirst = ( $this->mOffset == '' );
217 $this->mIsLast = ( $numRows < $queryLimit );
218 $this->mLastShown = $lastIndex;
219 $this->mFirstShown = $firstIndex;
220 }
221 $this->mQueryDone = true;
222 }
223
224 /**
225 * @param bool $enable
226 */
227 protected function preventClickjacking( $enable = true ) {
228 $this->preventClickjacking = $enable;
229 }
230
231 /**
232 * @return bool
233 */
234 public function getPreventClickjacking() {
235 return $this->preventClickjacking;
236 }
237
238 }