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