Be consistent with filearchive system and use whole key. This is a modified version...
[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) {
277 if ( $query === null ) {
278 return $text;
279 }
280 return $this->getSkin()->makeKnownLinkObj( $this->getTitle(), $text,
281 wfArrayToCGI( $query, $this->getDefaultQuery() ) );
282 }
283
284 /**
285 * Hook into getBody(), allows text to be inserted at the start. This
286 * will be called even if there are no rows in the result set.
287 */
288 function getStartBody() {
289 return '';
290 }
291
292 /**
293 * Hook into getBody() for the end of the list
294 */
295 function getEndBody() {
296 return '';
297 }
298
299 /**
300 * Hook into getBody(), for the bit between the start and the
301 * end when there are no rows
302 */
303 function getEmptyBody() {
304 return '';
305 }
306
307 /**
308 * Title used for self-links. Override this if you want to be able to
309 * use a title other than $wgTitle
310 */
311 function getTitle() {
312 return $GLOBALS['wgTitle'];
313 }
314
315 /**
316 * Get the current skin. This can be overridden if necessary.
317 */
318 function getSkin() {
319 if ( !isset( $this->mSkin ) ) {
320 global $wgUser;
321 $this->mSkin = $wgUser->getSkin();
322 }
323 return $this->mSkin;
324 }
325
326 /**
327 * Get an array of query parameters that should be put into self-links.
328 * By default, all parameters passed in the URL are used, except for a
329 * short blacklist.
330 */
331 function getDefaultQuery() {
332 if ( !isset( $this->mDefaultQuery ) ) {
333 $this->mDefaultQuery = $_GET;
334 unset( $this->mDefaultQuery['title'] );
335 unset( $this->mDefaultQuery['dir'] );
336 unset( $this->mDefaultQuery['offset'] );
337 unset( $this->mDefaultQuery['limit'] );
338 unset( $this->mDefaultQuery['order'] );
339 }
340 return $this->mDefaultQuery;
341 }
342
343 /**
344 * Get the number of rows in the result set
345 */
346 function getNumRows() {
347 if ( !$this->mQueryDone ) {
348 $this->doQuery();
349 }
350 return $this->mResult->numRows();
351 }
352
353 /**
354 * Get a URL query array for the prev, next, first and last links.
355 */
356 function getPagingQueries() {
357 if ( !$this->mQueryDone ) {
358 $this->doQuery();
359 }
360
361 # Don't announce the limit everywhere if it's the default
362 $urlLimit = $this->mLimit == $this->mDefaultLimit ? '' : $this->mLimit;
363
364 if ( $this->mIsFirst ) {
365 $prev = false;
366 $first = false;
367 } else {
368 $prev = array( 'dir' => 'prev', 'offset' => $this->mFirstShown, 'limit' => $urlLimit );
369 $first = array( 'limit' => $urlLimit );
370 }
371 if ( $this->mIsLast ) {
372 $next = false;
373 $last = false;
374 } else {
375 $next = array( 'offset' => $this->mLastShown, 'limit' => $urlLimit );
376 $last = array( 'dir' => 'prev', 'limit' => $urlLimit );
377 }
378 return array( 'prev' => $prev, 'next' => $next, 'first' => $first, 'last' => $last );
379 }
380
381 /**
382 * Get paging links. If a link is disabled, the item from $disabledTexts
383 * will be used. If there is no such item, the unlinked text from
384 * $linkTexts will be used. Both $linkTexts and $disabledTexts are arrays
385 * of HTML.
386 */
387 function getPagingLinks( $linkTexts, $disabledTexts = array() ) {
388 $queries = $this->getPagingQueries();
389 $links = array();
390 foreach ( $queries as $type => $query ) {
391 if ( $query !== false ) {
392 $links[$type] = $this->makeLink( $linkTexts[$type], $queries[$type] );
393 } elseif ( isset( $disabledTexts[$type] ) ) {
394 $links[$type] = $disabledTexts[$type];
395 } else {
396 $links[$type] = $linkTexts[$type];
397 }
398 }
399 return $links;
400 }
401
402 function getLimitLinks() {
403 global $wgLang;
404 $links = array();
405 if ( $this->mIsBackwards ) {
406 $offset = $this->mPastTheEndIndex;
407 } else {
408 $offset = $this->mOffset;
409 }
410 foreach ( $this->mLimitsShown as $limit ) {
411 $links[] = $this->makeLink( $wgLang->formatNum( $limit ),
412 array( 'offset' => $offset, 'limit' => $limit ) );
413 }
414 return $links;
415 }
416
417 /**
418 * Abstract formatting function. This should return an HTML string
419 * representing the result row $row. Rows will be concatenated and
420 * returned by getBody()
421 */
422 abstract function formatRow( $row );
423
424 /**
425 * This function should be overridden to provide all parameters
426 * needed for the main paged query. It returns an associative
427 * array with the following elements:
428 * tables => Table(s) for passing to Database::select()
429 * fields => Field(s) for passing to Database::select(), may be *
430 * conds => WHERE conditions
431 * options => option array
432 */
433 abstract function getQueryInfo();
434
435 /**
436 * This function should be overridden to return the name of the index fi-
437 * eld. If the pager supports multiple orders, it may return an array of
438 * 'querykey' => 'indexfield' pairs, so that a request with &count=querykey
439 * will use indexfield to sort. In this case, the first returned key is
440 * the default.
441 *
442 * Needless to say, it's really not a good idea to use a non-unique index
443 * for this! That won't page right.
444 */
445 abstract function getIndexField();
446
447 /**
448 * Return the default sorting direction: false for ascending, true for de-
449 * scending. You can also have an associative array of ordertype => dir,
450 * if multiple order types are supported. In this case getIndexField()
451 * must return an array, and the keys of that must exactly match the keys
452 * of this.
453 *
454 * For backward compatibility, this method's return value will be ignored
455 * if $this->mDefaultDirection is already set when the constructor is
456 * called, for instance if it's statically initialized. In that case the
457 * value of that variable (which must be a boolean) will be used.
458 *
459 * Note that despite its name, this does not return the value of the
460 * $this->mDefaultDirection member variable. That's the default for this
461 * particular instantiation, which is a single value. This is the set of
462 * all defaults for the class.
463 */
464 protected function getDefaultDirections() { return false; }
465 }
466
467
468 /**
469 * IndexPager with an alphabetic list and a formatted navigation bar
470 * @addtogroup Pager
471 */
472 abstract class AlphabeticPager extends IndexPager {
473 function __construct() {
474 parent::__construct();
475 }
476
477 /**
478 * Shamelessly stolen bits from ReverseChronologicalPager,
479 * didn't want to do class magic as may be still revamped
480 */
481 function getNavigationBar() {
482 global $wgLang;
483
484 if( isset( $this->mNavigationBar ) ) {
485 return $this->mNavigationBar;
486 }
487
488 $linkTexts = array(
489 'prev' => wfMsgHtml( 'prevn', $wgLang->formatNum( $this->mLimit ) ),
490 'next' => wfMsgHtml( 'nextn', $wgLang->formatNum($this->mLimit ) ),
491 'first' => wfMsgHtml( 'page_first' ),
492 'last' => wfMsgHtml( 'page_last' )
493 );
494
495 $pagingLinks = $this->getPagingLinks( $linkTexts );
496 $limitLinks = $this->getLimitLinks();
497 $limits = implode( ' | ', $limitLinks );
498
499 $this->mNavigationBar =
500 "({$pagingLinks['first']} | {$pagingLinks['last']}) " .
501 wfMsgHtml( 'viewprevnext', $pagingLinks['prev'],
502 $pagingLinks['next'], $limits );
503
504 if( !is_array( $this->getIndexField() ) ) {
505 # Early return to avoid undue nesting
506 return $this->mNavigationBar;
507 }
508
509 $extra = '';
510 $first = true;
511 $msgs = $this->getOrderTypeMessages();
512 foreach( array_keys( $msgs ) as $order ) {
513 if( $first ) {
514 $first = false;
515 } else {
516 $extra .= ' | ';
517 }
518
519 if( $order == $this->mOrderType ) {
520 $extra .= wfMsgHTML( $msgs[$order] );
521 } else {
522 $extra .= $this->makeLink(
523 wfMsgHTML( $msgs[$order] ),
524 array( 'order' => $order )
525 );
526 }
527 }
528
529 if( $extra !== '' ) {
530 $this->mNavigationBar .= " ($extra)";
531 }
532
533 return $this->mNavigationBar;
534 }
535
536 /**
537 * If this supports multiple order type messages, give the message key for
538 * enabling each one in getNavigationBar. The return type is an associa-
539 * tive array whose keys must exactly match the keys of the array returned
540 * by getIndexField(), and whose values are message keys.
541 * @return array
542 */
543 protected function getOrderTypeMessages() {
544 return null;
545 }
546 }
547
548 /**
549 * IndexPager with a formatted navigation bar
550 * @addtogroup Pager
551 */
552 abstract class ReverseChronologicalPager extends IndexPager {
553 public $mDefaultDirection = true;
554
555 function __construct() {
556 parent::__construct();
557 }
558
559 function getNavigationBar() {
560 global $wgLang;
561
562 if ( isset( $this->mNavigationBar ) ) {
563 return $this->mNavigationBar;
564 }
565 $nicenumber = $wgLang->formatNum( $this->mLimit );
566 $linkTexts = array(
567 'prev' => wfMsgExt( 'pager-newer-n', array( 'parsemag' ), $nicenumber ),
568 'next' => wfMsgExt( 'pager-older-n', array( 'parsemag' ), $nicenumber ),
569 'first' => wfMsgHtml( 'histlast' ),
570 'last' => wfMsgHtml( 'histfirst' )
571 );
572
573 $pagingLinks = $this->getPagingLinks( $linkTexts );
574 $limitLinks = $this->getLimitLinks();
575 $limits = implode( ' | ', $limitLinks );
576
577 $this->mNavigationBar = "({$pagingLinks['first']} | {$pagingLinks['last']}) " .
578 wfMsgHtml("viewprevnext", $pagingLinks['prev'], $pagingLinks['next'], $limits);
579 return $this->mNavigationBar;
580 }
581 }
582
583 /**
584 * Table-based display with a user-selectable sort order
585 * @addtogroup Pager
586 */
587 abstract class TablePager extends IndexPager {
588 var $mSort;
589 var $mCurrentRow;
590
591 function __construct() {
592 global $wgRequest;
593 $this->mSort = $wgRequest->getText( 'sort' );
594 if ( !array_key_exists( $this->mSort, $this->getFieldNames() ) ) {
595 $this->mSort = $this->getDefaultSort();
596 }
597 if ( $wgRequest->getBool( 'asc' ) ) {
598 $this->mDefaultDirection = false;
599 } elseif ( $wgRequest->getBool( 'desc' ) ) {
600 $this->mDefaultDirection = true;
601 } /* Else leave it at whatever the class default is */
602
603 parent::__construct();
604 }
605
606 function getStartBody() {
607 global $wgStylePath;
608 $tableClass = htmlspecialchars( $this->getTableClass() );
609 $sortClass = htmlspecialchars( $this->getSortHeaderClass() );
610
611 $s = "<table border='1' class=\"$tableClass\"><thead><tr>\n";
612 $fields = $this->getFieldNames();
613
614 # Make table header
615 foreach ( $fields as $field => $name ) {
616 if ( strval( $name ) == '' ) {
617 $s .= "<th>&nbsp;</th>\n";
618 } elseif ( $this->isFieldSortable( $field ) ) {
619 $query = array( 'sort' => $field, 'limit' => $this->mLimit );
620 if ( $field == $this->mSort ) {
621 # This is the sorted column
622 # Prepare a link that goes in the other sort order
623 if ( $this->mDefaultDirection ) {
624 # Descending
625 $image = 'Arr_u.png';
626 $query['asc'] = '1';
627 $query['desc'] = '';
628 $alt = htmlspecialchars( wfMsg( 'descending_abbrev' ) );
629 } else {
630 # Ascending
631 $image = 'Arr_d.png';
632 $query['asc'] = '';
633 $query['desc'] = '1';
634 $alt = htmlspecialchars( wfMsg( 'ascending_abbrev' ) );
635 }
636 $image = htmlspecialchars( "$wgStylePath/common/images/$image" );
637 $link = $this->makeLink(
638 "<img width=\"12\" height=\"12\" alt=\"$alt\" src=\"$image\" />" .
639 htmlspecialchars( $name ), $query );
640 $s .= "<th class=\"$sortClass\">$link</th>\n";
641 } else {
642 $s .= '<th>' . $this->makeLink( htmlspecialchars( $name ), $query ) . "</th>\n";
643 }
644 } else {
645 $s .= '<th>' . htmlspecialchars( $name ) . "</th>\n";
646 }
647 }
648 $s .= "</tr></thead><tbody>\n";
649 return $s;
650 }
651
652 function getEndBody() {
653 return "</tbody></table>\n";
654 }
655
656 function getEmptyBody() {
657 $colspan = count( $this->getFieldNames() );
658 $msgEmpty = wfMsgHtml( 'table_pager_empty' );
659 return "<tr><td colspan=\"$colspan\">$msgEmpty</td></tr>\n";
660 }
661
662 function formatRow( $row ) {
663 $s = "<tr>\n";
664 $fieldNames = $this->getFieldNames();
665 $this->mCurrentRow = $row; # In case formatValue needs to know
666 foreach ( $fieldNames as $field => $name ) {
667 $value = isset( $row->$field ) ? $row->$field : null;
668 $formatted = strval( $this->formatValue( $field, $value ) );
669 if ( $formatted == '' ) {
670 $formatted = '&nbsp;';
671 }
672 $class = 'TablePager_col_' . htmlspecialchars( $field );
673 $s .= "<td class=\"$class\">$formatted</td>\n";
674 }
675 $s .= "</tr>\n";
676 return $s;
677 }
678
679 function getIndexField() {
680 return $this->mSort;
681 }
682
683 function getTableClass() {
684 return 'TablePager';
685 }
686
687 function getNavClass() {
688 return 'TablePager_nav';
689 }
690
691 function getSortHeaderClass() {
692 return 'TablePager_sort';
693 }
694
695 /**
696 * A navigation bar with images
697 */
698 function getNavigationBar() {
699 global $wgStylePath, $wgContLang;
700 $path = "$wgStylePath/common/images";
701 $labels = array(
702 'first' => 'table_pager_first',
703 'prev' => 'table_pager_prev',
704 'next' => 'table_pager_next',
705 'last' => 'table_pager_last',
706 );
707 $images = array(
708 'first' => $wgContLang->isRTL() ? 'arrow_last_25.png' : 'arrow_first_25.png',
709 'prev' => $wgContLang->isRTL() ? 'arrow_right_25.png' : 'arrow_left_25.png',
710 'next' => $wgContLang->isRTL() ? 'arrow_left_25.png' : 'arrow_right_25.png',
711 'last' => $wgContLang->isRTL() ? 'arrow_first_25.png' : 'arrow_last_25.png',
712 );
713 $disabledImages = array(
714 'first' => $wgContLang->isRTL() ? 'arrow_disabled_last_25.png' : 'arrow_disabled_first_25.png',
715 'prev' => $wgContLang->isRTL() ? 'arrow_disabled_right_25.png' : 'arrow_disabled_left_25.png',
716 'next' => $wgContLang->isRTL() ? 'arrow_disabled_left_25.png' : 'arrow_disabled_right_25.png',
717 'last' => $wgContLang->isRTL() ? 'arrow_disabled_first_25.png' : 'arrow_disabled_last_25.png',
718 );
719
720 $linkTexts = array();
721 $disabledTexts = array();
722 foreach ( $labels as $type => $label ) {
723 $msgLabel = wfMsgHtml( $label );
724 $linkTexts[$type] = "<img src=\"$path/{$images[$type]}\" alt=\"$msgLabel\"/><br/>$msgLabel";
725 $disabledTexts[$type] = "<img src=\"$path/{$disabledImages[$type]}\" alt=\"$msgLabel\"/><br/>$msgLabel";
726 }
727 $links = $this->getPagingLinks( $linkTexts, $disabledTexts );
728
729 $navClass = htmlspecialchars( $this->getNavClass() );
730 $s = "<table class=\"$navClass\" align=\"center\" cellpadding=\"3\"><tr>\n";
731 $cellAttrs = 'valign="top" align="center" width="' . 100 / count( $links ) . '%"';
732 foreach ( $labels as $type => $label ) {
733 $s .= "<td $cellAttrs>{$links[$type]}</td>\n";
734 }
735 $s .= "</tr></table>\n";
736 return $s;
737 }
738
739 /**
740 * Get a <select> element which has options for each of the allowed limits
741 */
742 function getLimitSelect() {
743 global $wgLang;
744 $s = "<select name=\"limit\">";
745 foreach ( $this->mLimitsShown as $limit ) {
746 $selected = $limit == $this->mLimit ? 'selected="selected"' : '';
747 $formattedLimit = $wgLang->formatNum( $limit );
748 $s .= "<option value=\"$limit\" $selected>$formattedLimit</option>\n";
749 }
750 $s .= "</select>";
751 return $s;
752 }
753
754 /**
755 * Get <input type="hidden"> elements for use in a method="get" form.
756 * Resubmits all defined elements of the $_GET array, except for a
757 * blacklist, passed in the $blacklist parameter.
758 */
759 function getHiddenFields( $blacklist = array() ) {
760 $blacklist = (array)$blacklist;
761 $query = $_GET;
762 foreach ( $blacklist as $name ) {
763 unset( $query[$name] );
764 }
765 $s = '';
766 foreach ( $query as $name => $value ) {
767 $encName = htmlspecialchars( $name );
768 $encValue = htmlspecialchars( $value );
769 $s .= "<input type=\"hidden\" name=\"$encName\" value=\"$encValue\"/>\n";
770 }
771 return $s;
772 }
773
774 /**
775 * Get a form containing a limit selection dropdown
776 */
777 function getLimitForm() {
778 # Make the select with some explanatory text
779 $url = $this->getTitle()->escapeLocalURL();
780 $msgSubmit = wfMsgHtml( 'table_pager_limit_submit' );
781 return
782 "<form method=\"get\" action=\"$url\">" .
783 wfMsgHtml( 'table_pager_limit', $this->getLimitSelect() ) .
784 "\n<input type=\"submit\" value=\"$msgSubmit\"/>\n" .
785 $this->getHiddenFields( 'limit' ) .
786 "</form>\n";
787 }
788
789 /**
790 * Return true if the named field should be sortable by the UI, false
791 * otherwise
792 *
793 * @param string $field
794 */
795 abstract function isFieldSortable( $field );
796
797 /**
798 * Format a table cell. The return value should be HTML, but use an empty
799 * string not &nbsp; for empty cells. Do not include the <td> and </td>.
800 *
801 * The current result row is available as $this->mCurrentRow, in case you
802 * need more context.
803 *
804 * @param string $name The database field name
805 * @param string $value The value retrieved from the database
806 */
807 abstract function formatValue( $name, $value );
808
809 /**
810 * The database field name used as a default sort order
811 */
812 abstract function getDefaultSort();
813
814 /**
815 * An array mapping database field names to a textual description of the
816 * field name, for use in the table header. The description should be plain
817 * text, it will be HTML-escaped later.
818 */
819 abstract function getFieldNames();
820 }