AuthManager: Break AuthPlugin::addUser more explicitly
[mediawiki.git] / includes / page / ImageHistoryPseudoPager.php
blob6ab3ffc9308af158d6e28c8855143feeb16b4d3d
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
47 /**
48 * @return Title
50 function getTitle() {
51 return $this->mTitle;
54 function getQueryInfo() {
55 return false;
58 /**
59 * @return string
61 function getIndexField() {
62 return '';
65 /**
66 * @param object $row
67 * @return string
69 function formatRow( $row ) {
70 return '';
73 /**
74 * @return string
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 );
89 $linkBatch->execute();
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 );
101 $s .= $list->endImageHistoryList( $navLink );
103 if ( $list->getPreventClickjacking() ) {
104 $this->preventClickjacking();
107 return $s;
110 function doQuery() {
111 if ( $this->mQueryDone ) {
112 return;
114 $this->mImg = $this->mImagePage->getPage()->getFile(); // ensure loading
115 if ( !$this->mImg->exists() ) {
116 return;
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 );
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;
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 ) );
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 ];
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 ];
172 } else {
173 $firstIndex = '';
174 $lastIndex = '';
175 $this->mPastTheEndIndex = '';
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;
188 $this->mQueryDone = true;
192 * @param bool $enable
194 protected function preventClickjacking( $enable = true ) {
195 $this->preventClickjacking = $enable;
199 * @return bool
201 public function getPreventClickjacking() {
202 return $this->preventClickjacking;