MediaWiki REL1_34
TablePager.php
Go to the documentation of this file.
1<?php
25
30abstract class TablePager extends IndexPager {
32 protected $mSort;
33
35 protected $mCurrentRow;
36
37 public function __construct( IContextSource $context = null, LinkRenderer $linkRenderer = null ) {
38 if ( $context ) {
39 $this->setContext( $context );
40 }
41
42 $this->mSort = $this->getRequest()->getText( 'sort' );
43 if ( !array_key_exists( $this->mSort, $this->getFieldNames() )
44 || !$this->isFieldSortable( $this->mSort )
45 ) {
46 $this->mSort = $this->getDefaultSort();
47 }
48 if ( $this->getRequest()->getBool( 'asc' ) ) {
49 $this->mDefaultDirection = IndexPager::DIR_ASCENDING;
50 } elseif ( $this->getRequest()->getBool( 'desc' ) ) {
51 $this->mDefaultDirection = IndexPager::DIR_DESCENDING;
52 } /* Else leave it at whatever the class default is */
53
54 // Parent constructor needs mSort set, so we call it last
55 parent::__construct( null, $linkRenderer );
56 }
57
72 final public function getBody() {
73 $this->getOutput()->addModuleStyles( $this->getModuleStyles() );
74 return parent::getBody();
75 }
76
86 public function getBodyOutput() {
87 $body = parent::getBody();
88
89 $pout = new ParserOutput;
90 $pout->setText( $body );
91 $pout->addModuleStyles( $this->getModuleStyles() );
92 return $pout;
93 }
94
104 public function getFullOutput() {
105 $navigation = $this->getNavigationBar();
106 $body = parent::getBody();
107
108 $pout = new ParserOutput;
109 $pout->setText( $navigation . $body . $navigation );
110 $pout->addModuleStyles( $this->getModuleStyles() );
111 return $pout;
112 }
113
118 protected function getStartBody() {
119 $sortClass = $this->getSortHeaderClass();
120
121 $s = '';
122 $fields = $this->getFieldNames();
123
124 // Make table header
125 foreach ( $fields as $field => $name ) {
126 if ( strval( $name ) == '' ) {
127 $s .= Html::rawElement( 'th', [], "\u{00A0}" ) . "\n";
128 } elseif ( $this->isFieldSortable( $field ) ) {
129 $query = [ 'sort' => $field, 'limit' => $this->mLimit ];
130 $linkType = null;
131 $class = null;
132
133 if ( $this->mSort == $field ) {
134 // The table is sorted by this field already, make a link to sort in the other direction
135 // We don't actually know in which direction other fields will be sorted by default…
136 if ( $this->mDefaultDirection == IndexPager::DIR_DESCENDING ) {
137 $linkType = 'asc';
138 $class = "$sortClass mw-datatable-is-sorted mw-datatable-is-descending";
139 $query['asc'] = '1';
140 $query['desc'] = '';
141 } else {
142 $linkType = 'desc';
143 $class = "$sortClass mw-datatable-is-sorted mw-datatable-is-ascending";
144 $query['asc'] = '';
145 $query['desc'] = '1';
146 }
147 }
148
149 $link = $this->makeLink( htmlspecialchars( $name ), $query, $linkType );
150 $s .= Html::rawElement( 'th', [ 'class' => $class ], $link ) . "\n";
151 } else {
152 $s .= Html::element( 'th', [], $name ) . "\n";
153 }
154 }
155
156 $tableClass = $this->getTableClass();
157 $ret = Html::openElement( 'table', [
158 'class' => " $tableClass" ]
159 );
160 $ret .= Html::rawElement( 'thead', [], Html::rawElement( 'tr', [], "\n" . $s . "\n" ) );
161 $ret .= Html::openElement( 'tbody' ) . "\n";
162
163 return $ret;
164 }
165
170 protected function getEndBody() {
171 return "</tbody></table>\n";
172 }
173
178 function getEmptyBody() {
179 $colspan = count( $this->getFieldNames() );
180 $msgEmpty = $this->msg( 'table_pager_empty' )->text();
181 return Html::rawElement( 'tr', [],
182 Html::element( 'td', [ 'colspan' => $colspan ], $msgEmpty ) );
183 }
184
190 function formatRow( $row ) {
191 $this->mCurrentRow = $row; // In case formatValue etc need to know
192 $s = Html::openElement( 'tr', $this->getRowAttrs( $row ) ) . "\n";
193 $fieldNames = $this->getFieldNames();
194
195 foreach ( $fieldNames as $field => $name ) {
196 $value = $row->$field ?? null;
197 $formatted = strval( $this->formatValue( $field, $value ) );
198
199 if ( $formatted == '' ) {
200 $formatted = "\u{00A0}";
201 }
202
203 $s .= Html::rawElement( 'td', $this->getCellAttrs( $field, $value ), $formatted ) . "\n";
204 }
205
206 $s .= Html::closeElement( 'tr' ) . "\n";
207
208 return $s;
209 }
210
219 function getRowClass( $row ) {
220 return '';
221 }
222
231 function getRowAttrs( $row ) {
232 $class = $this->getRowClass( $row );
233 if ( $class === '' ) {
234 // Return an empty array to avoid clutter in HTML like class=""
235 return [];
236 } else {
237 return [ 'class' => $this->getRowClass( $row ) ];
238 }
239 }
240
244 protected function getCurrentRow() {
245 return $this->mCurrentRow;
246 }
247
259 function getCellAttrs( $field, $value ) {
260 return [ 'class' => 'TablePager_col_' . $field ];
261 }
262
267 function getIndexField() {
268 return $this->mSort;
269 }
270
275 protected function getTableClass() {
276 return 'mw-datatable';
277 }
278
282 protected function getNavClass() {
283 return 'TablePager_nav';
284 }
285
289 protected function getSortHeaderClass() {
290 return 'TablePager_sort';
291 }
292
297 public function getNavigationBar() {
298 if ( !$this->isNavigationBarShown() ) {
299 return '';
300 }
301
302 $this->getOutput()->enableOOUI();
303
304 $types = [ 'first', 'prev', 'next', 'last' ];
305
306 $queries = $this->getPagingQueries();
307
308 $buttons = [];
309
310 $title = $this->getTitle();
311
312 foreach ( $types as $type ) {
313 $buttons[] = new \OOUI\ButtonWidget( [
314 // Messages used here:
315 // * table_pager_first
316 // * table_pager_prev
317 // * table_pager_next
318 // * table_pager_last
319 'classes' => [ 'TablePager-button-' . $type ],
320 'flags' => [ 'progressive' ],
321 'framed' => false,
322 'label' => $this->msg( 'table_pager_' . $type )->text(),
323 'href' => $queries[ $type ] ?
324 $title->getLinkURL( $queries[ $type ] + $this->getDefaultQuery() ) :
325 null,
326 'icon' => $type === 'prev' ? 'previous' : $type,
327 'disabled' => $queries[ $type ] === false
328 ] );
329 }
330 return new \OOUI\ButtonGroupWidget( [
331 'classes' => [ $this->getNavClass() ],
332 'items' => $buttons,
333 ] );
334 }
335
341 public function getModuleStyles() {
342 return [ 'mediawiki.pager.tablePager', 'oojs-ui.styles.icons-movement' ];
343 }
344
351 public function getLimitSelect( $attribs = [] ) {
352 $select = new XmlSelect( 'limit', false, $this->mLimit );
353 $select->addOptions( $this->getLimitSelectList() );
354 foreach ( $attribs as $name => $value ) {
355 $select->setAttribute( $name, $value );
356 }
357 return $select->getHTML();
358 }
359
367 public function getLimitSelectList() {
368 # Add the current limit from the query string
369 # to avoid that the limit is lost after clicking Go next time
370 if ( !in_array( $this->mLimit, $this->mLimitsShown ) ) {
371 $this->mLimitsShown[] = $this->mLimit;
372 sort( $this->mLimitsShown );
373 }
374 $ret = [];
375 foreach ( $this->mLimitsShown as $key => $value ) {
376 # The pair is either $index => $limit, in which case the $value
377 # will be numeric, or $limit => $text, in which case the $value
378 # will be a string.
379 if ( is_int( $value ) ) {
380 $limit = $value;
381 $text = $this->getLanguage()->formatNum( $limit );
382 } else {
383 $limit = $key;
384 $text = $value;
385 }
386 $ret[$text] = $limit;
387 }
388 return $ret;
389 }
390
399 function getHiddenFields( $blacklist = [] ) {
400 $blacklist = (array)$blacklist;
401 $query = $this->getRequest()->getQueryValues();
402 foreach ( $blacklist as $name ) {
403 unset( $query[$name] );
404 }
405 $s = '';
406 foreach ( $query as $name => $value ) {
407 $s .= Html::hidden( $name, $value ) . "\n";
408 }
409 return $s;
410 }
411
417 function getLimitForm() {
418 return Html::rawElement(
419 'form',
420 [
421 'method' => 'get',
422 'action' => wfScript(),
423 ],
424 "\n" . $this->getLimitDropdown()
425 ) . "\n";
426 }
427
433 function getLimitDropdown() {
434 # Make the select with some explanatory text
435 $msgSubmit = $this->msg( 'table_pager_limit_submit' )->escaped();
436
437 return $this->msg( 'table_pager_limit' )
438 ->rawParams( $this->getLimitSelect() )->escaped() .
439 "\n<input type=\"submit\" value=\"$msgSubmit\"/>\n" .
440 $this->getHiddenFields( [ 'limit' ] );
441 }
442
449 abstract function isFieldSortable( $field );
450
463 abstract function formatValue( $name, $value );
464
472 abstract function getDefaultSort();
473
481 abstract function getFieldNames();
482}
wfScript( $script='index')
Get the path to a specified script file, respecting file extensions; this is a wrapper around $wgScri...
msg( $key,... $params)
Get a Message object with context set Parameters are the same as wfMessage()
IContextSource $context
setContext(IContextSource $context)
IndexPager is an efficient pager which uses a (roughly unique) index in the data set to implement pag...
int $mLimit
The maximum number of entries to show.
makeLink( $text, array $query=null, $type=null)
Make a self-link.
const DIR_ASCENDING
Backwards-compatible constant for $mDefaultDirection field (do not change)
getPagingQueries()
Get a URL query array for the prev, next, first and last links.
LinkRenderer $linkRenderer
isNavigationBarShown()
Returns whether to show the "navigation bar".
const DIR_DESCENDING
Backwards-compatible constant for $mDefaultDirection field (do not change)
Class that generates HTML links for pages.
Table-based display with a user-selectable sort order.
getTableClass()
TablePager relies on mw-datatable for styling, see T214208.
getBody()
Get the formatted result list.
getRowAttrs( $row)
Get attributes to be applied to the given row.
getNavigationBar()
A navigation bar with images.
getHiddenFields( $blacklist=[])
Get <input type="hidden"> elements for use in a method="get" form.
isFieldSortable( $field)
Return true if the named field should be sortable by the UI, false otherwise.
getLimitSelect( $attribs=[])
Get a "<select>" element which has options for each of the allowed limits.
getLimitSelectList()
Get a list of items to show in a "<select>" element of limits.
formatRow( $row)
stdClass $mCurrentRow
string $mSort
getDefaultSort()
The database field name used as a default sort order.
getCellAttrs( $field, $value)
Get any extra attributes to be applied to the given cell.
getFieldNames()
An array mapping database field names to a textual description of the field name, for use in the tabl...
getFullOutput()
Get the formatted result list, with navigation bars.
__construct(IContextSource $context=null, LinkRenderer $linkRenderer=null)
formatValue( $name, $value)
Format a table cell.
getLimitForm()
Get a form containing a limit selection dropdown.
getLimitDropdown()
Gets a limit selection dropdown.
getBodyOutput()
Get the formatted result list.
getModuleStyles()
ResourceLoader modules that must be loaded to provide correct styling for this pager.
getRowClass( $row)
Get a class name to be applied to the given row.
Class for generating HTML <select> or <datalist> elements.
Definition XmlSelect.php:26
Interface for objects which can provide a MediaWiki context on request.
$queries