* Preserve entered reason when reporting unconfirmed lock on Special:Lockdb
[lhc/web/wiklou.git] / includes / Pager.php
1 <?php
2
3 /**
4 * Basic pager interface.
5 */
6 interface Pager {
7 function getNavigationBar();
8 function getBody();
9 }
10
11 /**
12 * IndexPager is an efficient pager which uses a (roughly unique) index in the
13 * data set to implement paging, rather than a "LIMIT offset,limit" clause.
14 * In MySQL, such a limit/offset clause requires counting through the specified number
15 * of offset rows to find the desired data, which can be expensive for large offsets.
16 *
17 * ReverseChronologicalPager is a child class of the abstract IndexPager, and contains
18 * some formatting and display code which is specific to the use of timestamps as
19 * indexes. It is currently the only such child class. Here is a synopsis of the operation
20 * of IndexPager and its primary subclass:
21 *
22 * * The query is specified by the offset, limit and direction (dir) parameters, in
23 * addition to any subclass-specific parameters.
24 *
25 * * The offset is the non-inclusive start of the DB query. A row with an index value
26 * equal to the offset will never be shown.
27 *
28 * * The query may either be done backwards, where the rows are returned by the database
29 * in the opposite order to which they are displayed to the user, or forwards. This is
30 * specified by the "dir" parameter, dir=prev means backwards, anything else means
31 * forwards. The offset value specifies the start of the database result set, which
32 * may be either the start or end of the displayed data set. This allows "previous"
33 * links to be implemented without knowledge of the index value at the start of the
34 * previous page.
35 *
36 * * An additional row beyond the user-specified limit is always requested. This allows
37 * us to tell whether we should display a "next" link in the case of forwards mode,
38 * or a "previous" link in the case of backwards mode. Determining whether to
39 * display the other link (the one for the page before the start of the database
40 * result set) can be done heuristically by examining the offset.
41 *
42 * * An empty offset indicates that the offset condition should be omitted from the query.
43 * This naturally produces either the first page or the last page depending on the
44 * dir parameter.
45 *
46 * Subclassing the pager to implement concrete functionality should be fairly simple,
47 * please see the examples in PageHistory.php and SpecialIpblocklist.php. You just need
48 * to override formatRow(), getQueryInfo() and getIndexField(). Don't forget to call the
49 * parent constructor if you override it.
50 */
51 abstract class IndexPager implements Pager {
52 public $mRequest;
53 public $mLimitsShown = array( 20, 50, 100, 250, 500 );
54 public $mDefaultLimit = 50;
55 public $mOffset, $mLimit;
56 public $mQueryDone = false;
57 public $mDb;
58 public $mPastTheEndRow;
59
60 protected $mIndexField;
61
62 /**
63 * Default query direction. false for ascending, true for descending
64 */
65 public $mDefaultDirection = false;
66
67 /**
68 * Result object for the query. Warning: seek before use.
69 */
70 public $mResult;
71
72 function __construct() {
73 global $wgRequest;
74 $this->mRequest = $wgRequest;
75
76 # NB: the offset is quoted, not validated. It is treated as an arbitrary string
77 # to support the widest variety of index types. Be careful outputting it into
78 # HTML!
79 $this->mOffset = $this->mRequest->getText( 'offset' );
80 $this->mLimit = $this->mRequest->getInt( 'limit', $this->mDefaultLimit );
81 if ( $this->mLimit <= 0 ) {
82 $this->mLimit = $this->mDefaultLimit;
83 }
84 $this->mIsBackwards = ( $this->mRequest->getVal( 'dir' ) == 'prev' );
85 $this->mIndexField = $this->getIndexField();
86 $this->mDb = wfGetDB( DB_SLAVE );
87 }
88
89 /**
90 * Do the query, using information from the object context. This function
91 * has been kept minimal to make it overridable if necessary, to allow for
92 * result sets formed from multiple DB queries.
93 */
94 function doQuery() {
95 # Use the child class name for profiling
96 $fname = __METHOD__ . ' (' . get_class( $this ) . ')';
97 wfProfileIn( $fname );
98
99 $descending = ( $this->mIsBackwards == $this->mDefaultDirection );
100 # Plus an extra row so that we can tell the "next" link should be shown
101 $queryLimit = $this->mLimit + 1;
102
103 $this->mResult = $this->reallyDoQuery( $this->mOffset, $queryLimit, $descending );
104 $this->extractResultInfo( $this->mOffset, $queryLimit, $this->mResult );
105 $this->mQueryDone = true;
106
107 wfProfileOut( $fname );
108 }
109
110 /**
111 * Extract some useful data from the result object for use by
112 * the navigation bar, put it into $this
113 */
114 function extractResultInfo( $offset, $limit, ResultWrapper $res ) {
115 $numRows = $res->numRows();
116 if ( $numRows ) {
117 $row = $res->fetchRow();
118 $firstIndex = $row[$this->mIndexField];
119
120 # Discard the extra result row if there is one
121 if ( $numRows > $this->mLimit && $numRows > 1 ) {
122 $res->seek( $numRows - 1 );
123 $this->mPastTheEndRow = $res->fetchObject();
124 $indexField = $this->mIndexField;
125 $this->mPastTheEndIndex = $this->mPastTheEndRow->$indexField;
126 $res->seek( $numRows - 2 );
127 $row = $res->fetchRow();
128 $lastIndex = $row[$this->mIndexField];
129 } else {
130 $this->mPastTheEndRow = null;
131 # Setting indexes to an empty string means that they will be omitted
132 # if they would otherwise appear in URLs. It just so happens that this
133 # is the right thing to do in the standard UI, in all the relevant cases.
134 $this->mPastTheEndIndex = '';
135 $res->seek( $numRows - 1 );
136 $row = $res->fetchRow();
137 $lastIndex = $row[$this->mIndexField];
138 }
139 } else {
140 $firstIndex = '';
141 $lastIndex = '';
142 $this->mPastTheEndRow = null;
143 $this->mPastTheEndIndex = '';
144 }
145
146 if ( $this->mIsBackwards ) {
147 $this->mIsFirst = ( $numRows < $limit );
148 $this->mIsLast = ( $offset == '' );
149 $this->mLastShown = $firstIndex;
150 $this->mFirstShown = $lastIndex;
151 } else {
152 $this->mIsFirst = ( $offset == '' );
153 $this->mIsLast = ( $numRows < $limit );
154 $this->mLastShown = $lastIndex;
155 $this->mFirstShown = $firstIndex;
156 }
157 }
158
159 /**
160 * Do a query with specified parameters, rather than using the object context
161 *
162 * @param string $offset Index offset, inclusive
163 * @param integer $limit Exact query limit
164 * @param boolean $descending Query direction, false for ascending, true for descending
165 * @return ResultWrapper
166 */
167 function reallyDoQuery( $offset, $limit, $ascending ) {
168 $fname = __METHOD__ . ' (' . get_class( $this ) . ')';
169 $info = $this->getQueryInfo();
170 $tables = $info['tables'];
171 $fields = $info['fields'];
172 $conds = isset( $info['conds'] ) ? $info['conds'] : array();
173 $options = isset( $info['options'] ) ? $info['options'] : array();
174 if ( $ascending ) {
175 $options['ORDER BY'] = $this->mIndexField;
176 $operator = '>';
177 } else {
178 $options['ORDER BY'] = $this->mIndexField . ' DESC';
179 $operator = '<';
180 }
181 if ( $offset != '' ) {
182 $conds[] = $this->mIndexField . $operator . $this->mDb->addQuotes( $offset );
183 }
184 $options['LIMIT'] = intval( $limit );
185 $res = $this->mDb->select( $tables, $fields, $conds, $fname, $options );
186 return new ResultWrapper( $this->mDb, $res );
187 }
188
189 /**
190 * Get the formatted result list. Calls getStartBody(), formatRow() and
191 * getEndBody(), concatenates the results and returns them.
192 */
193 function getBody() {
194 if ( !$this->mQueryDone ) {
195 $this->doQuery();
196 }
197 # Don't use any extra rows returned by the query
198 $numRows = min( $this->mResult->numRows(), $this->mLimit );
199
200 $s = $this->getStartBody();
201 if ( $numRows ) {
202 if ( $this->mIsBackwards ) {
203 for ( $i = $numRows - 1; $i >= 0; $i-- ) {
204 $this->mResult->seek( $i );
205 $row = $this->mResult->fetchObject();
206 $s .= $this->formatRow( $row );
207 }
208 } else {
209 $this->mResult->seek( 0 );
210 for ( $i = 0; $i < $numRows; $i++ ) {
211 $row = $this->mResult->fetchObject();
212 $s .= $this->formatRow( $row );
213 }
214 }
215 }
216 $s .= $this->getEndBody();
217 return $s;
218 }
219
220 /**
221 * Make a self-link
222 */
223 function makeLink($text, $query = NULL) {
224 if ( $query === null ) {
225 return $text;
226 } else {
227 return $this->getSkin()->makeKnownLinkObj( $this->getTitle(), $text,
228 wfArrayToCGI( $query, $this->getDefaultQuery() ) );
229 }
230 }
231
232 /**
233 * Hook into getBody(), allows text to be inserted at the start. This
234 * will be called even if there are no rows in the result set.
235 */
236 function getStartBody() {
237 return '';
238 }
239
240 /**
241 * Hook into getBody() for the end of the list
242 */
243 function getEndBody() {
244 return '';
245 }
246
247 /**
248 * Title used for self-links. Override this if you want to be able to
249 * use a title other than $wgTitle
250 */
251 function getTitle() {
252 return $GLOBALS['wgTitle'];
253 }
254
255 /**
256 * Get the current skin. This can be overridden if necessary.
257 */
258 function getSkin() {
259 if ( !isset( $this->mSkin ) ) {
260 global $wgUser;
261 $this->mSkin = $wgUser->getSkin();
262 }
263 return $this->mSkin;
264 }
265
266 /**
267 * Get an array of query parameters that should be put into self-links.
268 * By default, all parameters passed in the URL are used, except for a
269 * short blacklist.
270 */
271 function getDefaultQuery() {
272 if ( !isset( $this->mDefaultQuery ) ) {
273 $this->mDefaultQuery = $_GET;
274 unset( $this->mDefaultQuery['title'] );
275 unset( $this->mDefaultQuery['dir'] );
276 unset( $this->mDefaultQuery['offset'] );
277 unset( $this->mDefaultQuery['limit'] );
278 }
279 return $this->mDefaultQuery;
280 }
281
282 /**
283 * Get the number of rows in the result set
284 */
285 function getNumRows() {
286 if ( !$this->mQueryDone ) {
287 $this->doQuery();
288 }
289 return $this->mResult->numRows();
290 }
291
292 /**
293 * Abstract formatting function. This should return an HTML string
294 * representing the result row $row. Rows will be concatenated and
295 * returned by getBody()
296 */
297 abstract function formatRow( $row );
298
299 /**
300 * This function should be overridden to provide all parameters
301 * needed for the main paged query. It returns an associative
302 * array with the following elements:
303 * tables => Table(s) for passing to Database::select()
304 * fields => Field(s) for passing to Database::select(), may be *
305 * conds => WHERE conditions
306 * options => option array
307 */
308 abstract function getQueryInfo();
309
310 /**
311 * This function should be overridden to return the name of the
312 * index field.
313 */
314 abstract function getIndexField();
315 }
316
317 /**
318 * IndexPager with a formatted navigation bar
319 */
320 abstract class ReverseChronologicalPager extends IndexPager {
321 public $mDefaultDirection = true;
322
323 function __construct() {
324 parent::__construct();
325 }
326
327 function getNavigationBar() {
328 global $wgLang;
329
330 if ( isset( $this->mNavigationBar ) ) {
331 return $this->mNavigationBar;
332 }
333 if ( !$this->mQueryDone ) {
334 $this->doQuery();
335 }
336
337 # Don't announce the limit everywhere if it's the default
338 $urlLimit = $this->mLimit == $this->mDefaultLimit ? '' : $this->mLimit;
339
340 if ( $this->mIsFirst ) {
341 $prevText = wfMsgHtml("prevn", $this->mLimit);
342 $latestText = wfMsgHtml('histlast');
343 } else {
344 $prevText = $this->makeLink( wfMsgHtml("prevn", $this->mLimit),
345 array( 'dir' => 'prev', 'offset' => $this->mFirstShown, 'limit' => $urlLimit ) );
346 $latestText = $this->makeLink( wfMsgHtml('histlast'),
347 array( 'limit' => $urlLimit ) );
348 }
349 if ( $this->mIsLast ) {
350 $nextText = wfMsgHtml( 'nextn', $this->mLimit);
351 $earliestText = wfMsgHtml( 'histfirst' );
352 } else {
353 $nextText = $this->makeLink( wfMsgHtml( 'nextn', $this->mLimit ),
354 array( 'offset' => $this->mLastShown, 'limit' => $urlLimit ) );
355 $earliestText = $this->makeLink( wfMsgHtml( 'histfirst' ),
356 array( 'dir' => 'prev', 'limit' => $urlLimit ) );
357 }
358 $limits = '';
359 foreach ( $this->mLimitsShown as $limit ) {
360 if ( $limits !== '' ) {
361 $limits .= ' | ';
362 }
363 if ( $this->mIsBackwards ) {
364 $offset = $this->mPastTheEndIndex;
365 } else {
366 $offset = $this->mOffset;
367 }
368 $limits .= $this->makeLink( $wgLang->formatNum($limit),
369 array('offset' => $offset, 'limit' => $limit ) );
370
371 }
372
373 $this->mNavigationBar = "($latestText | $earliestText) " . wfMsgHtml("viewprevnext", $prevText, $nextText, $limits);
374 return $this->mNavigationBar;
375 }
376 }
377
378 ?>