Fully deprecate $wgProxyKey. Has been marked as deprecated since 1.4, but never seems...
[lhc/web/wiklou.git] / includes / Pager.php
1 <?php
2 /**
3 * @defgroup Pager Pager
4 *
5 * @file
6 * @ingroup Pager
7 */
8
9 /**
10 * Basic pager interface.
11 * @ingroup Pager
12 */
13 interface Pager {
14 function getNavigationBar();
15 function getBody();
16 }
17
18 /**
19 * IndexPager is an efficient pager which uses a (roughly unique) index in the
20 * data set to implement paging, rather than a "LIMIT offset,limit" clause.
21 * In MySQL, such a limit/offset clause requires counting through the
22 * specified number of offset rows to find the desired data, which can be
23 * expensive for large offsets.
24 *
25 * ReverseChronologicalPager is a child class of the abstract IndexPager, and
26 * contains some formatting and display code which is specific to the use of
27 * timestamps as indexes. Here is a synopsis of its operation:
28 *
29 * * The query is specified by the offset, limit and direction (dir)
30 * parameters, in addition to any subclass-specific parameters.
31 * * The offset is the non-inclusive start of the DB query. A row with an
32 * index value equal to the offset will never be shown.
33 * * The query may either be done backwards, where the rows are returned by
34 * the database in the opposite order to which they are displayed to the
35 * user, or forwards. This is specified by the "dir" parameter, dir=prev
36 * means backwards, anything else means forwards. The offset value
37 * specifies the start of the database result set, which may be either
38 * the start or end of the displayed data set. This allows "previous"
39 * links to be implemented without knowledge of the index value at the
40 * start of the previous page.
41 * * An additional row beyond the user-specified limit is always requested.
42 * This allows us to tell whether we should display a "next" link in the
43 * case of forwards mode, or a "previous" link in the case of backwards
44 * mode. Determining whether to display the other link (the one for the
45 * page before the start of the database result set) can be done
46 * heuristically by examining the offset.
47 *
48 * * An empty offset indicates that the offset condition should be omitted
49 * from the query. This naturally produces either the first page or the
50 * last page depending on the dir parameter.
51 *
52 * Subclassing the pager to implement concrete functionality should be fairly
53 * simple, please see the examples in PageHistory.php and
54 * SpecialIpblocklist.php. You just need to override formatRow(),
55 * getQueryInfo() and getIndexField(). Don't forget to call the parent
56 * constructor if you override it.
57 *
58 * @ingroup Pager
59 */
60 abstract class IndexPager implements Pager {
61 public $mRequest;
62 public $mLimitsShown = array( 20, 50, 100, 250, 500 );
63 public $mDefaultLimit = 50;
64 public $mOffset, $mLimit;
65 public $mQueryDone = false;
66 public $mDb;
67 public $mPastTheEndRow;
68
69 /**
70 * The index to actually be used for ordering. This is a single string e-
71 * ven if multiple orderings are supported.
72 */
73 protected $mIndexField;
74 /** For pages that support multiple types of ordering, which one to use. */
75 protected $mOrderType;
76 /**
77 * $mDefaultDirection gives the direction to use when sorting results:
78 * false for ascending, true for descending. If $mIsBackwards is set, we
79 * start from the opposite end, but we still sort the page itself according
80 * to $mDefaultDirection. E.g., if $mDefaultDirection is false but we're
81 * going backwards, we'll display the last page of results, but the last
82 * result will be at the bottom, not the top.
83 *
84 * Like $mIndexField, $mDefaultDirection will be a single value even if the
85 * class supports multiple default directions for different order types.
86 */
87 public $mDefaultDirection;
88 public $mIsBackwards;
89
90 /**
91 * Result object for the query. Warning: seek before use.
92 */
93 public $mResult;
94
95 public function __construct() {
96 global $wgRequest, $wgUser;
97 $this->mRequest = $wgRequest;
98
99 # NB: the offset is quoted, not validated. It is treated as an
100 # arbitrary string to support the widest variety of index types. Be
101 # careful outputting it into HTML!
102 $this->mOffset = $this->mRequest->getText( 'offset' );
103
104 # Use consistent behavior for the limit options
105 $this->mDefaultLimit = intval( $wgUser->getOption( 'rclimit' ) );
106 list( $this->mLimit, /* $offset */ ) = $this->mRequest->getLimitOffset();
107
108 $this->mIsBackwards = ( $this->mRequest->getVal( 'dir' ) == 'prev' );
109 $this->mDb = wfGetDB( DB_SLAVE );
110
111 $index = $this->getIndexField();
112 $order = $this->mRequest->getVal( 'order' );
113 if( is_array( $index ) && isset( $index[$order] ) ) {
114 $this->mOrderType = $order;
115 $this->mIndexField = $index[$order];
116 } elseif( is_array( $index ) ) {
117 # First element is the default
118 reset( $index );
119 list( $this->mOrderType, $this->mIndexField ) = each( $index );
120 } else {
121 # $index is not an array
122 $this->mOrderType = null;
123 $this->mIndexField = $index;
124 }
125
126 if( !isset( $this->mDefaultDirection ) ) {
127 $dir = $this->getDefaultDirections();
128 $this->mDefaultDirection = is_array( $dir )
129 ? $dir[$this->mOrderType]
130 : $dir;
131 }
132 }
133
134 /**
135 * Do the query, using information from the object context. This function
136 * has been kept minimal to make it overridable if necessary, to allow for
137 * result sets formed from multiple DB queries.
138 */
139 function doQuery() {
140 # Use the child class name for profiling
141 $fname = __METHOD__ . ' (' . get_class( $this ) . ')';
142 wfProfileIn( $fname );
143
144 $descending = ( $this->mIsBackwards == $this->mDefaultDirection );
145 # Plus an extra row so that we can tell the "next" link should be shown
146 $queryLimit = $this->mLimit + 1;
147
148 $this->mResult = $this->reallyDoQuery( $this->mOffset, $queryLimit, $descending );
149 $this->extractResultInfo( $this->mOffset, $queryLimit, $this->mResult );
150 $this->mQueryDone = true;
151
152 $this->preprocessResults( $this->mResult );
153 $this->mResult->rewind(); // Paranoia
154
155 wfProfileOut( $fname );
156 }
157
158 /**
159 * Extract some useful data from the result object for use by
160 * the navigation bar, put it into $this
161 */
162 function extractResultInfo( $offset, $limit, ResultWrapper $res ) {
163 $numRows = $res->numRows();
164 if ( $numRows ) {
165 $row = $res->fetchRow();
166 $firstIndex = $row[$this->mIndexField];
167
168 # Discard the extra result row if there is one
169 if ( $numRows > $this->mLimit && $numRows > 1 ) {
170 $res->seek( $numRows - 1 );
171 $this->mPastTheEndRow = $res->fetchObject();
172 $indexField = $this->mIndexField;
173 $this->mPastTheEndIndex = $this->mPastTheEndRow->$indexField;
174 $res->seek( $numRows - 2 );
175 $row = $res->fetchRow();
176 $lastIndex = $row[$this->mIndexField];
177 } else {
178 $this->mPastTheEndRow = null;
179 # Setting indexes to an empty string means that they will be
180 # omitted if they would otherwise appear in URLs. It just so
181 # happens that this is the right thing to do in the standard
182 # UI, in all the relevant cases.
183 $this->mPastTheEndIndex = '';
184 $res->seek( $numRows - 1 );
185 $row = $res->fetchRow();
186 $lastIndex = $row[$this->mIndexField];
187 }
188 } else {
189 $firstIndex = '';
190 $lastIndex = '';
191 $this->mPastTheEndRow = null;
192 $this->mPastTheEndIndex = '';
193 }
194
195 if ( $this->mIsBackwards ) {
196 $this->mIsFirst = ( $numRows < $limit );
197 $this->mIsLast = ( $offset == '' );
198 $this->mLastShown = $firstIndex;
199 $this->mFirstShown = $lastIndex;
200 } else {
201 $this->mIsFirst = ( $offset == '' );
202 $this->mIsLast = ( $numRows < $limit );
203 $this->mLastShown = $lastIndex;
204 $this->mFirstShown = $firstIndex;
205 }
206 }
207
208 /**
209 * Do a query with specified parameters, rather than using the object
210 * context
211 *
212 * @param string $offset Index offset, inclusive
213 * @param integer $limit Exact query limit
214 * @param boolean $descending Query direction, false for ascending, true for descending
215 * @return ResultWrapper
216 */
217 function reallyDoQuery( $offset, $limit, $descending ) {
218 $fname = __METHOD__ . ' (' . get_class( $this ) . ')';
219 $info = $this->getQueryInfo();
220 $tables = $info['tables'];
221 $fields = $info['fields'];
222 $conds = isset( $info['conds'] ) ? $info['conds'] : array();
223 $options = isset( $info['options'] ) ? $info['options'] : array();
224 $join_conds = isset( $info['join_conds'] ) ? $info['join_conds'] : array();
225 if ( $descending ) {
226 $options['ORDER BY'] = $this->mIndexField;
227 $operator = '>';
228 } else {
229 $options['ORDER BY'] = $this->mIndexField . ' DESC';
230 $operator = '<';
231 }
232 if ( $offset != '' ) {
233 $conds[] = $this->mIndexField . $operator . $this->mDb->addQuotes( $offset );
234 }
235 $options['LIMIT'] = intval( $limit );
236 $res = $this->mDb->select( $tables, $fields, $conds, $fname, $options, $join_conds );
237 return new ResultWrapper( $this->mDb, $res );
238 }
239
240 /**
241 * Pre-process results; useful for performing batch existence checks, etc.
242 *
243 * @param ResultWrapper $result Result wrapper
244 */
245 protected function preprocessResults( $result ) {}
246
247 /**
248 * Get the formatted result list. Calls getStartBody(), formatRow() and
249 * getEndBody(), concatenates the results and returns them.
250 */
251 function getBody() {
252 if ( !$this->mQueryDone ) {
253 $this->doQuery();
254 }
255 # Don't use any extra rows returned by the query
256 $numRows = min( $this->mResult->numRows(), $this->mLimit );
257
258 $s = $this->getStartBody();
259 if ( $numRows ) {
260 if ( $this->mIsBackwards ) {
261 for ( $i = $numRows - 1; $i >= 0; $i-- ) {
262 $this->mResult->seek( $i );
263 $row = $this->mResult->fetchObject();
264 $s .= $this->formatRow( $row );
265 }
266 } else {
267 $this->mResult->seek( 0 );
268 for ( $i = 0; $i < $numRows; $i++ ) {
269 $row = $this->mResult->fetchObject();
270 $s .= $this->formatRow( $row );
271 }
272 }
273 } else {
274 $s .= $this->getEmptyBody();
275 }
276 $s .= $this->getEndBody();
277 return $s;
278 }
279
280 /**
281 * Make a self-link
282 */
283 function makeLink($text, $query = null, $type=null) {
284 if ( $query === null ) {
285 return $text;
286 }
287 if( $type == 'prev' || $type == 'next' ) {
288 $attrs = "rel=\"$type\"";
289 } elseif( $type == 'first' ) {
290 $attrs = "rel=\"start\"";
291 } else {
292 # HTML 4 has no rel="end" . . .
293 $attrs = '';
294 }
295
296 if( $type ) {
297 $attrs .= " class=\"mw-{$type}link\"" ;
298 }
299 return $this->getSkin()->makeKnownLinkObj( $this->getTitle(), $text,
300 wfArrayToCGI( $query, $this->getDefaultQuery() ), '', '',
301 $attrs );
302 }
303
304 /**
305 * Hook into getBody(), allows text to be inserted at the start. This
306 * will be called even if there are no rows in the result set.
307 */
308 function getStartBody() {
309 return '';
310 }
311
312 /**
313 * Hook into getBody() for the end of the list
314 */
315 function getEndBody() {
316 return '';
317 }
318
319 /**
320 * Hook into getBody(), for the bit between the start and the
321 * end when there are no rows
322 */
323 function getEmptyBody() {
324 return '';
325 }
326
327 /**
328 * Title used for self-links. Override this if you want to be able to
329 * use a title other than $wgTitle
330 */
331 function getTitle() {
332 return $GLOBALS['wgTitle'];
333 }
334
335 /**
336 * Get the current skin. This can be overridden if necessary.
337 */
338 function getSkin() {
339 if ( !isset( $this->mSkin ) ) {
340 global $wgUser;
341 $this->mSkin = $wgUser->getSkin();
342 }
343 return $this->mSkin;
344 }
345
346 /**
347 * Get an array of query parameters that should be put into self-links.
348 * By default, all parameters passed in the URL are used, except for a
349 * short blacklist.
350 */
351 function getDefaultQuery() {
352 if ( !isset( $this->mDefaultQuery ) ) {
353 $this->mDefaultQuery = $_GET;
354 unset( $this->mDefaultQuery['title'] );
355 unset( $this->mDefaultQuery['dir'] );
356 unset( $this->mDefaultQuery['offset'] );
357 unset( $this->mDefaultQuery['limit'] );
358 unset( $this->mDefaultQuery['order'] );
359 unset( $this->mDefaultQuery['month'] );
360 unset( $this->mDefaultQuery['year'] );
361 }
362 return $this->mDefaultQuery;
363 }
364
365 /**
366 * Get the number of rows in the result set
367 */
368 function getNumRows() {
369 if ( !$this->mQueryDone ) {
370 $this->doQuery();
371 }
372 return $this->mResult->numRows();
373 }
374
375 /**
376 * Get a URL query array for the prev, next, first and last links.
377 */
378 function getPagingQueries() {
379 if ( !$this->mQueryDone ) {
380 $this->doQuery();
381 }
382
383 # Don't announce the limit everywhere if it's the default
384 $urlLimit = $this->mLimit == $this->mDefaultLimit ? '' : $this->mLimit;
385
386 if ( $this->mIsFirst ) {
387 $prev = false;
388 $first = false;
389 } else {
390 $prev = array( 'dir' => 'prev', 'offset' => $this->mFirstShown, 'limit' => $urlLimit );
391 $first = array( 'limit' => $urlLimit );
392 }
393 if ( $this->mIsLast ) {
394 $next = false;
395 $last = false;
396 } else {
397 $next = array( 'offset' => $this->mLastShown, 'limit' => $urlLimit );
398 $last = array( 'dir' => 'prev', 'limit' => $urlLimit );
399 }
400 return array( 'prev' => $prev, 'next' => $next, 'first' => $first, 'last' => $last );
401 }
402
403 /**
404 * Get paging links. If a link is disabled, the item from $disabledTexts
405 * will be used. If there is no such item, the unlinked text from
406 * $linkTexts will be used. Both $linkTexts and $disabledTexts are arrays
407 * of HTML.
408 */
409 function getPagingLinks( $linkTexts, $disabledTexts = array() ) {
410 $queries = $this->getPagingQueries();
411 $links = array();
412 foreach ( $queries as $type => $query ) {
413 if ( $query !== false ) {
414 $links[$type] = $this->makeLink( $linkTexts[$type], $queries[$type], $type );
415 } elseif ( isset( $disabledTexts[$type] ) ) {
416 $links[$type] = $disabledTexts[$type];
417 } else {
418 $links[$type] = $linkTexts[$type];
419 }
420 }
421 return $links;
422 }
423
424 function getLimitLinks() {
425 global $wgLang;
426 $links = array();
427 if ( $this->mIsBackwards ) {
428 $offset = $this->mPastTheEndIndex;
429 } else {
430 $offset = $this->mOffset;
431 }
432 foreach ( $this->mLimitsShown as $limit ) {
433 $links[] = $this->makeLink( $wgLang->formatNum( $limit ),
434 array( 'offset' => $offset, 'limit' => $limit ), 'num' );
435 }
436 return $links;
437 }
438
439 /**
440 * Abstract formatting function. This should return an HTML string
441 * representing the result row $row. Rows will be concatenated and
442 * returned by getBody()
443 */
444 abstract function formatRow( $row );
445
446 /**
447 * This function should be overridden to provide all parameters
448 * needed for the main paged query. It returns an associative
449 * array with the following elements:
450 * tables => Table(s) for passing to Database::select()
451 * fields => Field(s) for passing to Database::select(), may be *
452 * conds => WHERE conditions
453 * options => option array
454 */
455 abstract function getQueryInfo();
456
457 /**
458 * This function should be overridden to return the name of the index fi-
459 * eld. If the pager supports multiple orders, it may return an array of
460 * 'querykey' => 'indexfield' pairs, so that a request with &count=querykey
461 * will use indexfield to sort. In this case, the first returned key is
462 * the default.
463 *
464 * Needless to say, it's really not a good idea to use a non-unique index
465 * for this! That won't page right.
466 */
467 abstract function getIndexField();
468
469 /**
470 * Return the default sorting direction: false for ascending, true for de-
471 * scending. You can also have an associative array of ordertype => dir,
472 * if multiple order types are supported. In this case getIndexField()
473 * must return an array, and the keys of that must exactly match the keys
474 * of this.
475 *
476 * For backward compatibility, this method's return value will be ignored
477 * if $this->mDefaultDirection is already set when the constructor is
478 * called, for instance if it's statically initialized. In that case the
479 * value of that variable (which must be a boolean) will be used.
480 *
481 * Note that despite its name, this does not return the value of the
482 * $this->mDefaultDirection member variable. That's the default for this
483 * particular instantiation, which is a single value. This is the set of
484 * all defaults for the class.
485 */
486 protected function getDefaultDirections() { return false; }
487 }
488
489
490 /**
491 * IndexPager with an alphabetic list and a formatted navigation bar
492 * @ingroup Pager
493 */
494 abstract class AlphabeticPager extends IndexPager {
495 /**
496 * Shamelessly stolen bits from ReverseChronologicalPager,
497 * didn't want to do class magic as may be still revamped
498 */
499 function getNavigationBar() {
500 global $wgLang;
501
502 if( isset( $this->mNavigationBar ) ) {
503 return $this->mNavigationBar;
504 }
505
506 $opts = array( 'parsemag', 'escapenoentities' );
507 $linkTexts = array(
508 'prev' => wfMsgExt( 'prevn', $opts, $wgLang->formatNum( $this->mLimit ) ),
509 'next' => wfMsgExt( 'nextn', $opts, $wgLang->formatNum($this->mLimit ) ),
510 'first' => wfMsgExt( 'page_first', $opts ),
511 'last' => wfMsgExt( 'page_last', $opts )
512 );
513
514 $pagingLinks = $this->getPagingLinks( $linkTexts );
515 $limitLinks = $this->getLimitLinks();
516 $limits = implode( ' | ', $limitLinks );
517
518 $this->mNavigationBar =
519 "({$pagingLinks['first']} | {$pagingLinks['last']}) " .
520 wfMsgHtml( 'viewprevnext', $pagingLinks['prev'],
521 $pagingLinks['next'], $limits );
522
523 if( !is_array( $this->getIndexField() ) ) {
524 # Early return to avoid undue nesting
525 return $this->mNavigationBar;
526 }
527
528 $extra = '';
529 $first = true;
530 $msgs = $this->getOrderTypeMessages();
531 foreach( array_keys( $msgs ) as $order ) {
532 if( $first ) {
533 $first = false;
534 } else {
535 $extra .= ' | ';
536 }
537
538 if( $order == $this->mOrderType ) {
539 $extra .= wfMsgHTML( $msgs[$order] );
540 } else {
541 $extra .= $this->makeLink(
542 wfMsgHTML( $msgs[$order] ),
543 array( 'order' => $order )
544 );
545 }
546 }
547
548 if( $extra !== '' ) {
549 $this->mNavigationBar .= " ($extra)";
550 }
551
552 return $this->mNavigationBar;
553 }
554
555 /**
556 * If this supports multiple order type messages, give the message key for
557 * enabling each one in getNavigationBar. The return type is an associa-
558 * tive array whose keys must exactly match the keys of the array returned
559 * by getIndexField(), and whose values are message keys.
560 * @return array
561 */
562 protected function getOrderTypeMessages() {
563 return null;
564 }
565 }
566
567 /**
568 * IndexPager with a formatted navigation bar
569 * @ingroup Pager
570 */
571 abstract class ReverseChronologicalPager extends IndexPager {
572 public $mDefaultDirection = true;
573 public $mYear;
574 public $mMonth;
575
576 function __construct() {
577 parent::__construct();
578 }
579
580 function getNavigationBar() {
581 global $wgLang;
582
583 if ( isset( $this->mNavigationBar ) ) {
584 return $this->mNavigationBar;
585 }
586 $nicenumber = $wgLang->formatNum( $this->mLimit );
587 $linkTexts = array(
588 'prev' => wfMsgExt( 'pager-newer-n', array( 'parsemag' ), $nicenumber ),
589 'next' => wfMsgExt( 'pager-older-n', array( 'parsemag' ), $nicenumber ),
590 'first' => wfMsgHtml( 'histlast' ),
591 'last' => wfMsgHtml( 'histfirst' )
592 );
593
594 $pagingLinks = $this->getPagingLinks( $linkTexts );
595 $limitLinks = $this->getLimitLinks();
596 $limits = implode( ' | ', $limitLinks );
597
598 $this->mNavigationBar = "({$pagingLinks['first']} | {$pagingLinks['last']}) " .
599 wfMsgHtml("viewprevnext", $pagingLinks['prev'], $pagingLinks['next'], $limits);
600 return $this->mNavigationBar;
601 }
602
603 function getDateCond( $year, $month ) {
604 $year = intval($year);
605 $month = intval($month);
606 // Basic validity checks
607 $this->mYear = $year > 0 ? $year : false;
608 $this->mMonth = ($month > 0 && $month < 13) ? $month : false;
609 // Given an optional year and month, we need to generate a timestamp
610 // to use as "WHERE rev_timestamp <= result"
611 // Examples: year = 2006 equals < 20070101 (+000000)
612 // year=2005, month=1 equals < 20050201
613 // year=2005, month=12 equals < 20060101
614 if ( !$this->mYear && !$this->mMonth ) {
615 return;
616 }
617 if ( $this->mYear ) {
618 $year = $this->mYear;
619 } else {
620 // If no year given, assume the current one
621 $year = gmdate( 'Y' );
622 // If this month hasn't happened yet this year, go back to last year's month
623 if( $this->mMonth > gmdate( 'n' ) ) {
624 $year--;
625 }
626 }
627 if ( $this->mMonth ) {
628 $month = $this->mMonth + 1;
629 // For December, we want January 1 of the next year
630 if ($month > 12) {
631 $month = 1;
632 $year++;
633 }
634 } else {
635 // No month implies we want up to the end of the year in question
636 $month = 1;
637 $year++;
638 }
639 // Y2K38 bug
640 if ( $year > 2032 ) {
641 $year = 2032;
642 }
643 $ymd = (int)sprintf( "%04d%02d01", $year, $month );
644 if ( $ymd > 20320101 ) {
645 $ymd = 20320101;
646 }
647 $this->mOffset = $this->mDb->timestamp( "${ymd}000000" );
648 }
649 }
650
651 /**
652 * Table-based display with a user-selectable sort order
653 * @ingroup Pager
654 */
655 abstract class TablePager extends IndexPager {
656 var $mSort;
657 var $mCurrentRow;
658
659 function __construct() {
660 global $wgRequest;
661 $this->mSort = $wgRequest->getText( 'sort' );
662 if ( !array_key_exists( $this->mSort, $this->getFieldNames() ) ) {
663 $this->mSort = $this->getDefaultSort();
664 }
665 if ( $wgRequest->getBool( 'asc' ) ) {
666 $this->mDefaultDirection = false;
667 } elseif ( $wgRequest->getBool( 'desc' ) ) {
668 $this->mDefaultDirection = true;
669 } /* Else leave it at whatever the class default is */
670
671 parent::__construct();
672 }
673
674 function getStartBody() {
675 global $wgStylePath;
676 $tableClass = htmlspecialchars( $this->getTableClass() );
677 $sortClass = htmlspecialchars( $this->getSortHeaderClass() );
678
679 $s = "<table border='1' class=\"$tableClass\"><thead><tr>\n";
680 $fields = $this->getFieldNames();
681
682 # Make table header
683 foreach ( $fields as $field => $name ) {
684 if ( strval( $name ) == '' ) {
685 $s .= "<th>&nbsp;</th>\n";
686 } elseif ( $this->isFieldSortable( $field ) ) {
687 $query = array( 'sort' => $field, 'limit' => $this->mLimit );
688 if ( $field == $this->mSort ) {
689 # This is the sorted column
690 # Prepare a link that goes in the other sort order
691 if ( $this->mDefaultDirection ) {
692 # Descending
693 $image = 'Arr_u.png';
694 $query['asc'] = '1';
695 $query['desc'] = '';
696 $alt = htmlspecialchars( wfMsg( 'descending_abbrev' ) );
697 } else {
698 # Ascending
699 $image = 'Arr_d.png';
700 $query['asc'] = '';
701 $query['desc'] = '1';
702 $alt = htmlspecialchars( wfMsg( 'ascending_abbrev' ) );
703 }
704 $image = htmlspecialchars( "$wgStylePath/common/images/$image" );
705 $link = $this->makeLink(
706 "<img width=\"12\" height=\"12\" alt=\"$alt\" src=\"$image\" />" .
707 htmlspecialchars( $name ), $query );
708 $s .= "<th class=\"$sortClass\">$link</th>\n";
709 } else {
710 $s .= '<th>' . $this->makeLink( htmlspecialchars( $name ), $query ) . "</th>\n";
711 }
712 } else {
713 $s .= '<th>' . htmlspecialchars( $name ) . "</th>\n";
714 }
715 }
716 $s .= "</tr></thead><tbody>\n";
717 return $s;
718 }
719
720 function getEndBody() {
721 return "</tbody></table>\n";
722 }
723
724 function getEmptyBody() {
725 $colspan = count( $this->getFieldNames() );
726 $msgEmpty = wfMsgHtml( 'table_pager_empty' );
727 return "<tr><td colspan=\"$colspan\">$msgEmpty</td></tr>\n";
728 }
729
730 function formatRow( $row ) {
731 $s = "<tr>\n";
732 $fieldNames = $this->getFieldNames();
733 $this->mCurrentRow = $row; # In case formatValue needs to know
734 foreach ( $fieldNames as $field => $name ) {
735 $value = isset( $row->$field ) ? $row->$field : null;
736 $formatted = strval( $this->formatValue( $field, $value ) );
737 if ( $formatted == '' ) {
738 $formatted = '&nbsp;';
739 }
740 $class = 'TablePager_col_' . htmlspecialchars( $field );
741 $s .= "<td class=\"$class\">$formatted</td>\n";
742 }
743 $s .= "</tr>\n";
744 return $s;
745 }
746
747 function getIndexField() {
748 return $this->mSort;
749 }
750
751 function getTableClass() {
752 return 'TablePager';
753 }
754
755 function getNavClass() {
756 return 'TablePager_nav';
757 }
758
759 function getSortHeaderClass() {
760 return 'TablePager_sort';
761 }
762
763 /**
764 * A navigation bar with images
765 */
766 function getNavigationBar() {
767 global $wgStylePath, $wgContLang;
768 $path = "$wgStylePath/common/images";
769 $labels = array(
770 'first' => 'table_pager_first',
771 'prev' => 'table_pager_prev',
772 'next' => 'table_pager_next',
773 'last' => 'table_pager_last',
774 );
775 $images = array(
776 'first' => $wgContLang->isRTL() ? 'arrow_last_25.png' : 'arrow_first_25.png',
777 'prev' => $wgContLang->isRTL() ? 'arrow_right_25.png' : 'arrow_left_25.png',
778 'next' => $wgContLang->isRTL() ? 'arrow_left_25.png' : 'arrow_right_25.png',
779 'last' => $wgContLang->isRTL() ? 'arrow_first_25.png' : 'arrow_last_25.png',
780 );
781 $disabledImages = array(
782 'first' => $wgContLang->isRTL() ? 'arrow_disabled_last_25.png' : 'arrow_disabled_first_25.png',
783 'prev' => $wgContLang->isRTL() ? 'arrow_disabled_right_25.png' : 'arrow_disabled_left_25.png',
784 'next' => $wgContLang->isRTL() ? 'arrow_disabled_left_25.png' : 'arrow_disabled_right_25.png',
785 'last' => $wgContLang->isRTL() ? 'arrow_disabled_first_25.png' : 'arrow_disabled_last_25.png',
786 );
787
788 $linkTexts = array();
789 $disabledTexts = array();
790 foreach ( $labels as $type => $label ) {
791 $msgLabel = wfMsgHtml( $label );
792 $linkTexts[$type] = "<img src=\"$path/{$images[$type]}\" alt=\"$msgLabel\"/><br/>$msgLabel";
793 $disabledTexts[$type] = "<img src=\"$path/{$disabledImages[$type]}\" alt=\"$msgLabel\"/><br/>$msgLabel";
794 }
795 $links = $this->getPagingLinks( $linkTexts, $disabledTexts );
796
797 $navClass = htmlspecialchars( $this->getNavClass() );
798 $s = "<table class=\"$navClass\" align=\"center\" cellpadding=\"3\"><tr>\n";
799 $cellAttrs = 'valign="top" align="center" width="' . 100 / count( $links ) . '%"';
800 foreach ( $labels as $type => $label ) {
801 $s .= "<td $cellAttrs>{$links[$type]}</td>\n";
802 }
803 $s .= "</tr></table>\n";
804 return $s;
805 }
806
807 /**
808 * Get a <select> element which has options for each of the allowed limits
809 */
810 function getLimitSelect() {
811 global $wgLang;
812 $s = "<select name=\"limit\">";
813 foreach ( $this->mLimitsShown as $limit ) {
814 $selected = $limit == $this->mLimit ? 'selected="selected"' : '';
815 $formattedLimit = $wgLang->formatNum( $limit );
816 $s .= "<option value=\"$limit\" $selected>$formattedLimit</option>\n";
817 }
818 $s .= "</select>";
819 return $s;
820 }
821
822 /**
823 * Get <input type="hidden"> elements for use in a method="get" form.
824 * Resubmits all defined elements of the $_GET array, except for a
825 * blacklist, passed in the $blacklist parameter.
826 */
827 function getHiddenFields( $blacklist = array() ) {
828 $blacklist = (array)$blacklist;
829 $query = $_GET;
830 foreach ( $blacklist as $name ) {
831 unset( $query[$name] );
832 }
833 $s = '';
834 foreach ( $query as $name => $value ) {
835 $encName = htmlspecialchars( $name );
836 $encValue = htmlspecialchars( $value );
837 $s .= "<input type=\"hidden\" name=\"$encName\" value=\"$encValue\"/>\n";
838 }
839 return $s;
840 }
841
842 /**
843 * Get a form containing a limit selection dropdown
844 */
845 function getLimitForm() {
846 # Make the select with some explanatory text
847 $url = $this->getTitle()->escapeLocalURL();
848 $msgSubmit = wfMsgHtml( 'table_pager_limit_submit' );
849 return
850 "<form method=\"get\" action=\"$url\">" .
851 wfMsgHtml( 'table_pager_limit', $this->getLimitSelect() ) .
852 "\n<input type=\"submit\" value=\"$msgSubmit\"/>\n" .
853 $this->getHiddenFields( 'limit' ) .
854 "</form>\n";
855 }
856
857 /**
858 * Return true if the named field should be sortable by the UI, false
859 * otherwise
860 *
861 * @param string $field
862 */
863 abstract function isFieldSortable( $field );
864
865 /**
866 * Format a table cell. The return value should be HTML, but use an empty
867 * string not &nbsp; for empty cells. Do not include the <td> and </td>.
868 *
869 * The current result row is available as $this->mCurrentRow, in case you
870 * need more context.
871 *
872 * @param string $name The database field name
873 * @param string $value The value retrieved from the database
874 */
875 abstract function formatValue( $name, $value );
876
877 /**
878 * The database field name used as a default sort order
879 */
880 abstract function getDefaultSort();
881
882 /**
883 * An array mapping database field names to a textual description of the
884 * field name, for use in the table header. The description should be plain
885 * text, it will be HTML-escaped later.
886 */
887 abstract function getFieldNames();
888 }