MediaWiki  master
TablePager.php
Go to the documentation of this file.
1 <?php
25 
31 abstract 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 }
ContextSource\$context
IContextSource $context
Definition: ContextSource.php:37
IndexPager\$mLimit
int $mLimit
The maximum number of entries to show.
Definition: IndexPager.php:96
ParserOutput
Definition: ParserOutput.php:27
TablePager\getHiddenFields
getHiddenFields( $blacklist=[])
Get <input type="hidden"> elements for use in a method="get" form.
Definition: TablePager.php:413
TablePager\getIndexField
getIndexField()
Returns the name of the index field.If the pager supports multiple orders, it may return an array of ...
Definition: TablePager.php:273
MediaWiki\Linker\LinkRenderer
Class that generates HTML links for pages.
Definition: LinkRenderer.php:41
TablePager\getCellAttrs
getCellAttrs( $field, $value)
Get any extra attributes to be applied to the given cell.
Definition: TablePager.php:265
TablePager\formatValue
formatValue( $name, $value)
Format a table cell.
TablePager\getTableClass
getTableClass()
TablePager relies on mw-datatable for styling, see T214208.
Definition: TablePager.php:283
TablePager\getEndBody
getEndBody()
Stable to override.
Definition: TablePager.php:177
TablePager\getFullOutput
getFullOutput()
Get the formatted result list, with navigation bars.
Definition: TablePager.php:111
IndexPager\getPagingQueries
getPagingQueries()
Get a URL query array for the prev, next, first and last links.
Definition: IndexPager.php:745
$s
$s
Definition: mergeMessageFileList.php:185
IndexPager\$linkRenderer
LinkRenderer $linkRenderer
Definition: IndexPager.php:167
TablePager\__construct
__construct(IContextSource $context=null, LinkRenderer $linkRenderer=null)
Stable to call.
Definition: TablePager.php:44
IndexPager\makeLink
makeLink( $text, array $query=null, $type=null)
Make a self-link.
Definition: IndexPager.php:632
ContextSource\getRequest
getRequest()
Definition: ContextSource.php:79
ContextSource\getTitle
getTitle()
Definition: ContextSource.php:88
IndexPager\DIR_ASCENDING
const DIR_ASCENDING
Backwards-compatible constant for $mDefaultDirection field (do not change)
Definition: IndexPager.php:78
XmlSelect
Class for generating HTML <select> or <datalist> elements.
Definition: XmlSelect.php:26
ContextSource\getLanguage
getLanguage()
Definition: ContextSource.php:143
TablePager\getBodyOutput
getBodyOutput()
Get the formatted result list.
Definition: TablePager.php:93
Html\closeElement
static closeElement( $element)
Returns "</$element>".
Definition: Html.php:315
TablePager\getSortHeaderClass
getSortHeaderClass()
Stable to override.
Definition: TablePager.php:299
TablePager\getRowAttrs
getRowAttrs( $row)
Get attributes to be applied to the given row.
Definition: TablePager.php:237
wfScript
wfScript( $script='index')
Get the path to a specified script file, respecting file extensions; this is a wrapper around $wgScri...
Definition: GlobalFunctions.php:2542
ContextSource\getOutput
getOutput()
Definition: ContextSource.php:124
TablePager
Table-based display with a user-selectable sort order Stable to extend.
Definition: TablePager.php:31
TablePager\$mCurrentRow
stdClass $mCurrentRow
Definition: TablePager.php:36
$title
$title
Definition: testCompression.php:38
ParserOutput\setText
setText( $text)
Definition: ParserOutput.php:718
TablePager\isFieldSortable
isFieldSortable( $field)
Return true if the named field should be sortable by the UI, false otherwise.
ContextSource\setContext
setContext(IContextSource $context)
Definition: ContextSource.php:61
TablePager\getNavClass
getNavClass()
Stable to override.
Definition: TablePager.php:291
ContextSource\msg
msg( $key,... $params)
Get a Message object with context set Parameters are the same as wfMessage()
Definition: ContextSource.php:187
Html\hidden
static hidden( $name, $value, array $attribs=[])
Convenience function to produce an input element with type=hidden.
Definition: Html.php:802
TablePager\getNavigationBar
getNavigationBar()
A navigation bar with images.
Definition: TablePager.php:309
TablePager\getLimitDropdown
getLimitDropdown()
Gets a limit selection dropdown.
Definition: TablePager.php:447
IContextSource
Interface for objects which can provide a MediaWiki context on request.
Definition: IContextSource.php:55
IndexPager\DIR_DESCENDING
const DIR_DESCENDING
Backwards-compatible constant for $mDefaultDirection field (do not change)
Definition: IndexPager.php:80
TablePager\getStartBody
getStartBody()
Stable to override.
Definition: TablePager.php:125
IndexPager\isNavigationBarShown
isNavigationBarShown()
Returns whether to show the "navigation bar" @stable to override.
Definition: IndexPager.php:786
TablePager\formatRow
formatRow( $row)
Stable to override.
Definition: TablePager.php:196
TablePager\$mSort
string $mSort
Definition: TablePager.php:33
TablePager\getEmptyBody
getEmptyBody()
Definition: TablePager.php:184
Html\openElement
static openElement( $element, $attribs=[])
Identical to rawElement(), but has no third parameter and omits the end tag (and the self-closing '/'...
Definition: Html.php:251
Html\rawElement
static rawElement( $element, $attribs=[], $contents='')
Returns an HTML element in a string.
Definition: Html.php:209
IndexPager
IndexPager is an efficient pager which uses a (roughly unique) index in the data set to implement pag...
Definition: IndexPager.php:74
TablePager\getLimitForm
getLimitForm()
Get a form containing a limit selection dropdown.
Definition: TablePager.php:431
TablePager\getBody
getBody()
Get the formatted result list.
Definition: TablePager.php:79
TablePager\getDefaultSort
getDefaultSort()
The database field name used as a default sort order.
TablePager\getRowClass
getRowClass( $row)
Get a class name to be applied to the given row.
Definition: TablePager.php:225
TablePager\getLimitSelect
getLimitSelect( $attribs=[])
Get a "<select>" element which has options for each of the allowed limits.
Definition: TablePager.php:365
Html\element
static element( $element, $attribs=[], $contents='')
Identical to rawElement(), but HTML-escapes $contents (like Xml::element()).
Definition: Html.php:231
TablePager\getModuleStyles
getModuleStyles()
ResourceLoader modules that must be loaded to provide correct styling for this pager.
Definition: TablePager.php:355
TablePager\getLimitSelectList
getLimitSelectList()
Get a list of items to show in a "<select>" element of limits.
Definition: TablePager.php:381
TablePager\getCurrentRow
getCurrentRow()
Definition: TablePager.php:250
TablePager\getFieldNames
getFieldNames()
An array mapping database field names to a textual description of the field name, for use in the tabl...
$type
$type
Definition: testCompression.php:52