MediaWiki REL1_37
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 return [ 'class' => $this->getRowClass( $row ) ];
239 }
240
244 protected function getCurrentRow() {
245 return $this->mCurrentRow;
246 }
247
259 protected function getCellAttrs( $field, $value ) {
260 return [ 'class' => 'TablePager_col_' . $field ];
261 }
262
267 public function getIndexField() {
268 return $this->mSort;
269 }
270
277 protected function getTableClass() {
278 return 'mw-datatable';
279 }
280
285 protected function getNavClass() {
286 return 'TablePager_nav';
287 }
288
293 protected function getSortHeaderClass() {
294 return 'TablePager_sort';
295 }
296
303 public function getNavigationBar() {
304 if ( !$this->isNavigationBarShown() ) {
305 return '';
306 }
307
308 $this->getOutput()->enableOOUI();
309
310 $types = [ 'first', 'prev', 'next', 'last' ];
311
312 $queries = $this->getPagingQueries();
313
314 $buttons = [];
315
316 $title = $this->getTitle();
317
318 foreach ( $types as $type ) {
319 $buttons[] = new \OOUI\ButtonWidget( [
320 // Messages used here:
321 // * table_pager_first
322 // * table_pager_prev
323 // * table_pager_next
324 // * table_pager_last
325 'classes' => [ 'TablePager-button-' . $type ],
326 'flags' => [ 'progressive' ],
327 'framed' => false,
328 'label' => $this->msg( 'table_pager_' . $type )->text(),
329 'href' => $queries[ $type ] ?
330 $title->getLinkURL( $queries[ $type ] + $this->getDefaultQuery() ) :
331 null,
332 'icon' => $type === 'prev' ? 'previous' : $type,
333 'disabled' => $queries[ $type ] === false
334 ] );
335 }
336 return new \OOUI\ButtonGroupWidget( [
337 'classes' => [ $this->getNavClass() ],
338 'items' => $buttons,
339 ] );
340 }
341
349 public function getModuleStyles() {
350 return [ 'mediawiki.pager.tablePager', 'oojs-ui.styles.icons-movement' ];
351 }
352
359 public function getLimitSelect( $attribs = [] ) {
360 $select = new XmlSelect( 'limit', false, $this->mLimit );
361 $select->addOptions( $this->getLimitSelectList() );
362 foreach ( $attribs as $name => $value ) {
363 $select->setAttribute( $name, $value );
364 }
365 return $select->getHTML();
366 }
367
375 public function getLimitSelectList() {
376 # Add the current limit from the query string
377 # to avoid that the limit is lost after clicking Go next time
378 if ( !in_array( $this->mLimit, $this->mLimitsShown ) ) {
379 $this->mLimitsShown[] = $this->mLimit;
380 sort( $this->mLimitsShown );
381 }
382 $ret = [];
383 foreach ( $this->mLimitsShown as $key => $value ) {
384 # The pair is either $index => $limit, in which case the $value
385 # will be numeric, or $limit => $text, in which case the $value
386 # will be a string.
387 if ( is_int( $value ) ) {
388 $limit = $value;
389 $text = $this->getLanguage()->formatNum( $limit );
390 } else {
391 $limit = $key;
392 $text = $value;
393 }
394 $ret[$text] = $limit;
395 }
396 return $ret;
397 }
398
407 public function getHiddenFields( $noResubmit = [] ) {
408 $noResubmit = (array)$noResubmit;
409 $query = $this->getRequest()->getQueryValues();
410 foreach ( $noResubmit as $name ) {
411 unset( $query[$name] );
412 }
413 $s = '';
414 foreach ( $query as $name => $value ) {
415 $s .= Html::hidden( $name, $value ) . "\n";
416 }
417 return $s;
418 }
419
425 public function getLimitForm() {
426 return Html::rawElement(
427 'form',
428 [
429 'method' => 'get',
430 'action' => wfScript(),
431 ],
432 "\n" . $this->getLimitDropdown()
433 ) . "\n";
434 }
435
441 private function getLimitDropdown() {
442 # Make the select with some explanatory text
443 $msgSubmit = $this->msg( 'table_pager_limit_submit' )->escaped();
444
445 return $this->msg( 'table_pager_limit' )
446 ->rawParams( $this->getLimitSelect() )->escaped() .
447 "\n<input type=\"submit\" value=\"$msgSubmit\"/>\n" .
448 $this->getHiddenFields( [ 'limit' ] );
449 }
450
458 abstract protected function isFieldSortable( $field );
459
471 abstract public function formatValue( $name, $value );
472
482 abstract public function getDefaultSort();
483
491 abstract protected function getFieldNames();
492}
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.
getHiddenFields( $noResubmit=[])
Get <input type="hidden"> elements for use in a method="get" form.
getNavigationBar()
A navigation bar with images.
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
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.
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.
foreach( $mmfl['setupFiles'] as $fileName) if($queue) if(empty( $mmfl['quiet'])) $s