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