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