MediaWiki REL1_35
TablePager.php
Go to the documentation of this file.
1<?php
25
31abstract class TablePager extends IndexPager {
33 protected $mSort;
34
36 protected $mCurrentRow;
37
44 public function __construct( IContextSource $context = null, LinkRenderer $linkRenderer = null ) {
45 if ( $context ) {
46 $this->setContext( $context );
47 }
48
49 $this->mSort = $this->getRequest()->getText( 'sort' );
50 if ( !array_key_exists( $this->mSort, $this->getFieldNames() )
51 || !$this->isFieldSortable( $this->mSort )
52 ) {
53 $this->mSort = $this->getDefaultSort();
54 }
55 if ( $this->getRequest()->getBool( 'asc' ) ) {
56 $this->mDefaultDirection = IndexPager::DIR_ASCENDING;
57 } elseif ( $this->getRequest()->getBool( 'desc' ) ) {
58 $this->mDefaultDirection = IndexPager::DIR_DESCENDING;
59 } /* Else leave it at whatever the class default is */
60
61 // Parent constructor needs mSort set, so we call it last
62 parent::__construct( null, $linkRenderer );
63 }
64
79 final public function getBody() {
80 $this->getOutput()->addModuleStyles( $this->getModuleStyles() );
81 return parent::getBody();
82 }
83
93 public function getBodyOutput() {
94 $body = parent::getBody();
95
96 $pout = new ParserOutput;
97 $pout->setText( $body );
98 $pout->addModuleStyles( $this->getModuleStyles() );
99 return $pout;
100 }
101
111 public function getFullOutput() {
112 $navigation = $this->getNavigationBar();
113 $body = parent::getBody();
114
115 $pout = new ParserOutput;
116 $pout->setText( $navigation . $body . $navigation );
117 $pout->addModuleStyles( $this->getModuleStyles() );
118 return $pout;
119 }
120
125 protected function getStartBody() {
126 $sortClass = $this->getSortHeaderClass();
127
128 $s = '';
129 $fields = $this->getFieldNames();
130
131 // Make table header
132 foreach ( $fields as $field => $name ) {
133 if ( strval( $name ) == '' ) {
134 $s .= Html::rawElement( 'th', [], "\u{00A0}" ) . "\n";
135 } elseif ( $this->isFieldSortable( $field ) ) {
136 $query = [ 'sort' => $field, 'limit' => $this->mLimit ];
137 $linkType = null;
138 $class = null;
139
140 if ( $this->mSort == $field ) {
141 // The table is sorted by this field already, make a link to sort in the other direction
142 // We don't actually know in which direction other fields will be sorted by default…
143 if ( $this->mDefaultDirection == IndexPager::DIR_DESCENDING ) {
144 $linkType = 'asc';
145 $class = "$sortClass mw-datatable-is-sorted mw-datatable-is-descending";
146 $query['asc'] = '1';
147 $query['desc'] = '';
148 } else {
149 $linkType = 'desc';
150 $class = "$sortClass mw-datatable-is-sorted mw-datatable-is-ascending";
151 $query['asc'] = '';
152 $query['desc'] = '1';
153 }
154 }
155
156 $link = $this->makeLink( htmlspecialchars( $name ), $query, $linkType );
157 $s .= Html::rawElement( 'th', [ 'class' => $class ], $link ) . "\n";
158 } else {
159 $s .= Html::element( 'th', [], $name ) . "\n";
160 }
161 }
162
163 $tableClass = $this->getTableClass();
164 $ret = Html::openElement( 'table', [
165 'class' => " $tableClass" ]
166 );
167 $ret .= Html::rawElement( 'thead', [], Html::rawElement( 'tr', [], "\n" . $s . "\n" ) );
168 $ret .= Html::openElement( 'tbody' ) . "\n";
169
170 return $ret;
171 }
172
177 protected function getEndBody() {
178 return "</tbody></table>\n";
179 }
180
184 protected function getEmptyBody() {
185 $colspan = count( $this->getFieldNames() );
186 $msgEmpty = $this->msg( 'table_pager_empty' )->text();
187 return Html::rawElement( 'tr', [],
188 Html::element( 'td', [ 'colspan' => $colspan ], $msgEmpty ) );
189 }
190
196 public function formatRow( $row ) {
197 $this->mCurrentRow = $row; // In case formatValue etc need to know
198 $s = Html::openElement( 'tr', $this->getRowAttrs( $row ) ) . "\n";
199 $fieldNames = $this->getFieldNames();
200
201 foreach ( $fieldNames as $field => $name ) {
202 $value = $row->$field ?? null;
203 $formatted = strval( $this->formatValue( $field, $value ) );
204
205 if ( $formatted == '' ) {
206 $formatted = "\u{00A0}";
207 }
208
209 $s .= Html::rawElement( 'td', $this->getCellAttrs( $field, $value ), $formatted ) . "\n";
210 }
211
212 $s .= Html::closeElement( 'tr' ) . "\n";
213
214 return $s;
215 }
216
225 protected function getRowClass( $row ) {
226 return '';
227 }
228
237 protected function getRowAttrs( $row ) {
238 $class = $this->getRowClass( $row );
239 if ( $class === '' ) {
240 // Return an empty array to avoid clutter in HTML like class=""
241 return [];
242 } else {
243 return [ 'class' => $this->getRowClass( $row ) ];
244 }
245 }
246
250 protected function getCurrentRow() {
251 return $this->mCurrentRow;
252 }
253
265 protected function getCellAttrs( $field, $value ) {
266 return [ 'class' => 'TablePager_col_' . $field ];
267 }
268
273 public function getIndexField() {
274 return $this->mSort;
275 }
276
283 protected function getTableClass() {
284 return 'mw-datatable';
285 }
286
291 protected function getNavClass() {
292 return 'TablePager_nav';
293 }
294
299 protected function getSortHeaderClass() {
300 return 'TablePager_sort';
301 }
302
309 public function getNavigationBar() {
310 if ( !$this->isNavigationBarShown() ) {
311 return '';
312 }
313
314 $this->getOutput()->enableOOUI();
315
316 $types = [ 'first', 'prev', 'next', 'last' ];
317
318 $queries = $this->getPagingQueries();
319
320 $buttons = [];
321
322 $title = $this->getTitle();
323
324 foreach ( $types as $type ) {
325 $buttons[] = new \OOUI\ButtonWidget( [
326 // Messages used here:
327 // * table_pager_first
328 // * table_pager_prev
329 // * table_pager_next
330 // * table_pager_last
331 'classes' => [ 'TablePager-button-' . $type ],
332 'flags' => [ 'progressive' ],
333 'framed' => false,
334 'label' => $this->msg( 'table_pager_' . $type )->text(),
335 'href' => $queries[ $type ] ?
336 $title->getLinkURL( $queries[ $type ] + $this->getDefaultQuery() ) :
337 null,
338 'icon' => $type === 'prev' ? 'previous' : $type,
339 'disabled' => $queries[ $type ] === false
340 ] );
341 }
342 return new \OOUI\ButtonGroupWidget( [
343 'classes' => [ $this->getNavClass() ],
344 'items' => $buttons,
345 ] );
346 }
347
355 public function getModuleStyles() {
356 return [ 'mediawiki.pager.tablePager', 'oojs-ui.styles.icons-movement' ];
357 }
358
365 public function getLimitSelect( $attribs = [] ) {
366 $select = new XmlSelect( 'limit', false, $this->mLimit );
367 $select->addOptions( $this->getLimitSelectList() );
368 foreach ( $attribs as $name => $value ) {
369 $select->setAttribute( $name, $value );
370 }
371 return $select->getHTML();
372 }
373
381 public function getLimitSelectList() {
382 # Add the current limit from the query string
383 # to avoid that the limit is lost after clicking Go next time
384 if ( !in_array( $this->mLimit, $this->mLimitsShown ) ) {
385 $this->mLimitsShown[] = $this->mLimit;
386 sort( $this->mLimitsShown );
387 }
388 $ret = [];
389 foreach ( $this->mLimitsShown as $key => $value ) {
390 # The pair is either $index => $limit, in which case the $value
391 # will be numeric, or $limit => $text, in which case the $value
392 # will be a string.
393 if ( is_int( $value ) ) {
394 $limit = $value;
395 $text = $this->getLanguage()->formatNum( $limit );
396 } else {
397 $limit = $key;
398 $text = $value;
399 }
400 $ret[$text] = $limit;
401 }
402 return $ret;
403 }
404
413 public function getHiddenFields( $blacklist = [] ) {
414 $blacklist = (array)$blacklist;
415 $query = $this->getRequest()->getQueryValues();
416 foreach ( $blacklist as $name ) {
417 unset( $query[$name] );
418 }
419 $s = '';
420 foreach ( $query as $name => $value ) {
421 $s .= Html::hidden( $name, $value ) . "\n";
422 }
423 return $s;
424 }
425
431 public function getLimitForm() {
432 return Html::rawElement(
433 'form',
434 [
435 'method' => 'get',
436 'action' => wfScript(),
437 ],
438 "\n" . $this->getLimitDropdown()
439 ) . "\n";
440 }
441
447 private function getLimitDropdown() {
448 # Make the select with some explanatory text
449 $msgSubmit = $this->msg( 'table_pager_limit_submit' )->escaped();
450
451 return $this->msg( 'table_pager_limit' )
452 ->rawParams( $this->getLimitSelect() )->escaped() .
453 "\n<input type=\"submit\" value=\"$msgSubmit\"/>\n" .
454 $this->getHiddenFields( [ 'limit' ] );
455 }
456
463 abstract protected function isFieldSortable( $field );
464
475 abstract public function formatValue( $name, $value );
476
486 abstract public function getDefaultSort();
487
495 abstract protected function getFieldNames();
496}
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" Stable to override.
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 Stable to extend.
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.
getSortHeaderClass()
Stable to override.
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.
getEndBody()
Stable to override.
getNavClass()
Stable to override.
formatRow( $row)
Stable to override.
stdClass $mCurrentRow
getIndexField()
Returns the name of the index field.If the pager supports multiple orders, it may return an array of ...
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.
getStartBody()
Stable to override.
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)
Stable to call.
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.