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  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 }
ContextSource\$context
IContextSource $context
Definition: ContextSource.php:39
IndexPager\$mLimit
int $mLimit
The maximum number of entries to show.
Definition: IndexPager.php:96
ParserOutput
Definition: ParserOutput.php:31
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:267
MediaWiki\Linker\LinkRenderer
Class that generates HTML links for pages.
Definition: LinkRenderer.php:43
TablePager\getCellAttrs
getCellAttrs( $field, $value)
Get any extra attributes to be applied to the given cell.
Definition: TablePager.php:259
TablePager\formatValue
formatValue( $name, $value)
Format a table cell.
TablePager\getTableClass
getTableClass()
TablePager relies on mw-datatable for styling, see T214208.
Definition: TablePager.php:277
TablePager\getEndBody
getEndBody()
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:747
IndexPager\$linkRenderer
LinkRenderer $linkRenderer
Definition: IndexPager.php:167
TablePager\__construct
__construct(IContextSource $context=null, LinkRenderer $linkRenderer=null)
Definition: TablePager.php:44
IndexPager\makeLink
makeLink( $text, array $query=null, $type=null)
Make a self-link.
Definition: IndexPager.php:634
ContextSource\getRequest
getRequest()
Definition: ContextSource.php:81
ContextSource\getTitle
getTitle()
Definition: ContextSource.php:90
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:153
TablePager\getBodyOutput
getBodyOutput()
Get the formatted result list.
Definition: TablePager.php:93
Html\closeElement
static closeElement( $element)
Returns "</$element>".
Definition: Html.php:316
TablePager\getSortHeaderClass
getSortHeaderClass()
Definition: TablePager.php:293
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:2307
ContextSource\getOutput
getOutput()
Definition: ContextSource.php:126
TablePager
Table-based display with a user-selectable sort order.
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:724
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:63
TablePager\getNavClass
getNavClass()
Definition: TablePager.php:285
ContextSource\msg
msg( $key,... $params)
Get a Message object with context set Parameters are the same as wfMessage()
Definition: ContextSource.php:197
Html\hidden
static hidden( $name, $value, array $attribs=[])
Convenience function to produce an input element with type=hidden.
Definition: Html.php:831
$s
foreach( $mmfl['setupFiles'] as $fileName) if( $queue) if(empty( $mmfl['quiet'])) $s
Definition: mergeMessageFileList.php:206
TablePager\getNavigationBar
getNavigationBar()
A navigation bar with images.
Definition: TablePager.php:303
TablePager\getLimitDropdown
getLimitDropdown()
Gets a limit selection dropdown.
Definition: TablePager.php:441
IContextSource
Interface for objects which can provide a MediaWiki context on request.
Definition: IContextSource.php:58
IndexPager\DIR_DESCENDING
const DIR_DESCENDING
Backwards-compatible constant for $mDefaultDirection field (do not change)
Definition: IndexPager.php:80
TablePager\getStartBody
getStartBody()
Definition: TablePager.php:125
IndexPager\isNavigationBarShown
isNavigationBarShown()
Returns whether to show the "navigation bar" @stable to override.
Definition: IndexPager.php:788
TablePager\formatRow
formatRow( $row)
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:252
Html\rawElement
static rawElement( $element, $attribs=[], $contents='')
Returns an HTML element in a string.
Definition: Html.php:210
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:425
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:359
Html\element
static element( $element, $attribs=[], $contents='')
Identical to rawElement(), but HTML-escapes $contents (like Xml::element()).
Definition: Html.php:232
TablePager\getModuleStyles
getModuleStyles()
ResourceLoader modules that must be loaded to provide correct styling for this pager.
Definition: TablePager.php:349
TablePager\getLimitSelectList
getLimitSelectList()
Get a list of items to show in a "<select>" element of limits.
Definition: TablePager.php:375
TablePager\getCurrentRow
getCurrentRow()
Definition: TablePager.php:244
TablePager\getHiddenFields
getHiddenFields( $noResubmit=[])
Get <input type="hidden"> elements for use in a method="get" form.
Definition: TablePager.php:407
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