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