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