MediaWiki fundraising/REL1_35
ImageHistoryPseudoPager.php
Go to the documentation of this file.
1<?php
21use Wikimedia\Timestamp\TimestampException;
22
24 protected $preventClickjacking = false;
25
29 protected $mImg;
30
34 protected $mTitle;
35
41
46 public $mHist;
47
52 public $mRange;
53
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->getLimitOffsetForUser(
70 $this->getUser(),
71 $this->mDefaultLimit,
72 ''
73 );
74 }
75
79 public function getTitle() {
80 return $this->mTitle;
81 }
82
83 public function getQueryInfo() {
84 return [];
85 }
86
90 public function getIndexField() {
91 return '';
92 }
93
98 public function formatRow( $row ) {
99 return '';
100 }
101
105 public function getBody() {
106 $s = '';
107 $this->doQuery();
108 if ( count( $this->mHist ) ) {
109 if ( $this->mImg->isLocal() ) {
110 // Do a batch existence check for user pages and talkpages
111 $linkBatch = new LinkBatch();
112 for ( $i = $this->mRange[0]; $i <= $this->mRange[1]; $i++ ) {
113 $file = $this->mHist[$i];
114 $user = $file->getUser( 'text' );
115 $linkBatch->add( NS_USER, $user );
116 $linkBatch->add( NS_USER_TALK, $user );
117 }
118 $linkBatch->execute();
119 }
120
121 $list = new ImageHistoryList( $this->mImagePage );
122 # Generate prev/next links
123 $navLink = $this->getNavigationBar();
124 $s = $list->beginImageHistoryList( $navLink );
125 // Skip rows there just for paging links
126 for ( $i = $this->mRange[0]; $i <= $this->mRange[1]; $i++ ) {
127 $file = $this->mHist[$i];
128 $s .= $list->imageHistoryLine( !$file->isOld(), $file );
129 }
130 $s .= $list->endImageHistoryList( $navLink );
131
132 if ( $list->getPreventClickjacking() ) {
133 $this->preventClickjacking();
134 }
135 }
136 return $s;
137 }
138
139 public function doQuery() {
140 if ( $this->mQueryDone ) {
141 return;
142 }
143 $this->mImg = $this->mImagePage->getPage()->getFile(); // ensure loading
144 if ( !$this->mImg->exists() ) {
145 return;
146 }
147 // Make sure the date (probably from user input) is valid; if not, drop it.
148 if ( $this->mOffset !== null ) {
149 try {
150 $sadlyWeCannotPassThisTimestampDownTheStack = $this->mDb->timestamp( $this->mOffset );
151 } catch ( TimestampException $e ) {
152 $this->mOffset = null;
153 }
154 }
155 $queryLimit = $this->mLimit + 1; // limit plus extra row
156 if ( $this->mIsBackwards ) {
157 // Fetch the file history
158 $this->mHist = $this->mImg->getHistory( $queryLimit, null, $this->mOffset, false );
159 // The current rev may not meet the offset/limit
160 $numRows = count( $this->mHist );
161 if ( $numRows <= $this->mLimit && $this->mImg->getTimestamp() > $this->mOffset ) {
162 $this->mHist = array_merge( [ $this->mImg ], $this->mHist );
163 }
164 } else {
165 // The current rev may not meet the offset
166 if ( !$this->mOffset || $this->mImg->getTimestamp() < $this->mOffset ) {
167 $this->mHist[] = $this->mImg;
168 }
169 // Old image versions (fetch extra row for nav links)
170 $oiLimit = count( $this->mHist ) ? $this->mLimit : $this->mLimit + 1;
171 // Fetch the file history
172 $this->mHist = array_merge( $this->mHist,
173 $this->mImg->getHistory( $oiLimit, $this->mOffset, null, false ) );
174 }
175 $numRows = count( $this->mHist ); // Total number of query results
176 if ( $numRows ) {
177 # Index value of top item in the list
178 $firstIndex = $this->mIsBackwards ?
179 [ $this->mHist[$numRows - 1]->getTimestamp() ] : [ $this->mHist[0]->getTimestamp() ];
180 # Discard the extra result row if there is one
181 if ( $numRows > $this->mLimit && $numRows > 1 ) {
182 if ( $this->mIsBackwards ) {
183 # Index value of item past the index
184 $this->mPastTheEndIndex = [ $this->mHist[0]->getTimestamp() ];
185 # Index value of bottom item in the list
186 $lastIndex = [ $this->mHist[1]->getTimestamp() ];
187 # Display range
188 $this->mRange = [ 1, $numRows - 1 ];
189 } else {
190 # Index value of item past the index
191 $this->mPastTheEndIndex = [ $this->mHist[$numRows - 1]->getTimestamp() ];
192 # Index value of bottom item in the list
193 $lastIndex = [ $this->mHist[$numRows - 2]->getTimestamp() ];
194 # Display range
195 $this->mRange = [ 0, $numRows - 2 ];
196 }
197 } else {
198 # Setting indexes to an empty array means that they will be
199 # omitted if they would otherwise appear in URLs. It just so
200 # happens that this is the right thing to do in the standard
201 # UI, in all the relevant cases.
202 $this->mPastTheEndIndex = [];
203 # Index value of bottom item in the list
204 $lastIndex = $this->mIsBackwards ?
205 [ $this->mHist[0]->getTimestamp() ] : [ $this->mHist[$numRows - 1]->getTimestamp() ];
206 # Display range
207 $this->mRange = [ 0, $numRows - 1 ];
208 }
209 } else {
210 $firstIndex = [];
211 $lastIndex = [];
212 $this->mPastTheEndIndex = [];
213 }
214 if ( $this->mIsBackwards ) {
215 $this->mIsFirst = ( $numRows < $queryLimit );
216 $this->mIsLast = ( $this->mOffset == '' );
217 $this->mLastShown = $firstIndex;
218 $this->mFirstShown = $lastIndex;
219 } else {
220 $this->mIsFirst = ( $this->mOffset == '' );
221 $this->mIsLast = ( $numRows < $queryLimit );
222 $this->mLastShown = $lastIndex;
223 $this->mFirstShown = $firstIndex;
224 }
225 $this->mQueryDone = true;
226 }
227
231 protected function preventClickjacking( $enable = true ) {
232 $this->preventClickjacking = $enable;
233 }
234
238 public function getPreventClickjacking() {
240 }
241
242}
getUser()
Stable to override.
Implements some public methods and some protected utility functions which are required by multiple ch...
Definition File.php:63
Builds the image revision log shown on image pages.
doQuery()
Do the query, using information from the object context.
getQueryInfo()
Provides all parameters needed for the main paged query.
Class for viewing MediaWiki file description pages.
Definition ImagePage.php:33
Class representing a list of titles The execute() method checks them all for existence and adds them ...
Definition LinkBatch.php:35
Efficient paging for SQL queries.
Represents a title within MediaWiki.
Definition Title.php:42
const NS_USER
Definition Defines.php:72
const NS_USER_TALK
Definition Defines.php:73
if(PHP_SAPI !='cli-server') if(!isset( $_SERVER['SCRIPT_FILENAME'])) $file
Item class for a filearchive table row.
Definition router.php:42