MediaWiki  master
TablePager.php
Go to the documentation of this file.
1 <?php
22 
29 abstract class TablePager extends IndexPager {
31  protected $mSort;
32 
34  protected $mCurrentRow;
35 
42  public function __construct( IContextSource $context = null, LinkRenderer $linkRenderer = null ) {
43  if ( $context ) {
44  $this->setContext( $context );
45  }
46 
47  $this->mSort = $this->getRequest()->getText( 'sort' );
48  if ( !array_key_exists( $this->mSort, $this->getFieldNames() )
49  || !$this->isFieldSortable( $this->mSort )
50  ) {
51  $this->mSort = $this->getDefaultSort();
52  }
53  if ( $this->getRequest()->getBool( 'asc' ) ) {
54  $this->mDefaultDirection = IndexPager::DIR_ASCENDING;
55  } elseif ( $this->getRequest()->getBool( 'desc' ) ) {
56  $this->mDefaultDirection = IndexPager::DIR_DESCENDING;
57  } /* Else leave it at whatever the class default is */
58 
59  // Parent constructor needs mSort set, so we call it last
60  parent::__construct( null, $linkRenderer );
61  }
62 
77  final public function getBody() {
78  return parent::getBody();
79  }
80 
90  public function getBodyOutput() {
91  $body = parent::getBody();
92 
93  $pout = new ParserOutput;
94  $pout->setText( $body );
95  return $pout;
96  }
97 
107  public function getFullOutput() {
108  $navigation = $this->getNavigationBar();
109  $body = parent::getBody();
110 
111  $pout = new ParserOutput;
112  $pout->setText( $navigation . $body . $navigation );
113  $pout->addModuleStyles( $this->getModuleStyles() );
114  return $pout;
115  }
116 
121  protected function getStartBody() {
122  $sortClass = $this->getSortHeaderClass();
123 
124  $s = '';
125  $fields = $this->getFieldNames();
126 
127  // Make table header
128  foreach ( $fields as $field => $name ) {
129  if ( strval( $name ) == '' ) {
130  $s .= Html::rawElement( 'th', [], "\u{00A0}" ) . "\n";
131  } elseif ( $this->isFieldSortable( $field ) ) {
132  $query = [ 'sort' => $field, 'limit' => $this->mLimit ];
133  $linkType = null;
134  $class = null;
135 
136  if ( $this->mSort == $field ) {
137  // The table is sorted by this field already, make a link to sort in the other direction
138  // We don't actually know in which direction other fields will be sorted by default…
139  if ( $this->mDefaultDirection == IndexPager::DIR_DESCENDING ) {
140  $linkType = 'asc';
141  $class = "$sortClass mw-datatable-is-sorted mw-datatable-is-descending";
142  $query['asc'] = '1';
143  $query['desc'] = '';
144  } else {
145  $linkType = 'desc';
146  $class = "$sortClass mw-datatable-is-sorted mw-datatable-is-ascending";
147  $query['asc'] = '';
148  $query['desc'] = '1';
149  }
150  }
151 
152  $link = $this->makeLink( htmlspecialchars( $name ), $query, $linkType );
153  $s .= Html::rawElement( 'th', [ 'class' => $class ], $link ) . "\n";
154  } else {
155  $s .= Html::element( 'th', [], $name ) . "\n";
156  }
157  }
158 
159  $ret = Html::openElement( 'table', [
160  'class' => $this->getTableClass() ]
161  );
162  $ret .= Html::rawElement( 'thead', [], Html::rawElement( 'tr', [], "\n" . $s . "\n" ) );
163  $ret .= Html::openElement( 'tbody' ) . "\n";
164 
165  return $ret;
166  }
167 
172  protected function getEndBody() {
173  return "</tbody></table>\n";
174  }
175 
179  protected function getEmptyBody() {
180  $colspan = count( $this->getFieldNames() );
181  $msgEmpty = $this->msg( 'table_pager_empty' )->text();
182  return Html::rawElement( 'tr', [],
183  Html::element( 'td', [ 'colspan' => $colspan ], $msgEmpty ) );
184  }
185 
191  public function formatRow( $row ) {
192  $this->mCurrentRow = $row; // In case formatValue etc need to know
193  $s = Html::openElement( 'tr', $this->getRowAttrs( $row ) ) . "\n";
194  $fieldNames = $this->getFieldNames();
195 
196  foreach ( $fieldNames as $field => $name ) {
197  $value = $row->$field ?? null;
198  $formatted = strval( $this->formatValue( $field, $value ) );
199 
200  if ( $formatted == '' ) {
201  $formatted = "\u{00A0}";
202  }
203 
204  $s .= Html::rawElement( 'td', $this->getCellAttrs( $field, $value ), $formatted ) . "\n";
205  }
206 
207  $s .= Html::closeElement( 'tr' ) . "\n";
208 
209  return $s;
210  }
211 
220  protected function getRowClass( $row ) {
221  return '';
222  }
223 
232  protected function getRowAttrs( $row ) {
233  return [ 'class' => $this->getRowClass( $row ) ];
234  }
235 
239  protected function getCurrentRow() {
240  return $this->mCurrentRow;
241  }
242 
254  protected function getCellAttrs( $field, $value ) {
255  return [ 'class' => 'TablePager_col_' . $field ];
256  }
257 
262  public function getIndexField() {
263  return $this->mSort;
264  }
265 
272  protected function getTableClass() {
273  return 'mw-datatable';
274  }
275 
280  protected function getNavClass() {
281  return 'TablePager_nav';
282  }
283 
288  protected function getSortHeaderClass() {
289  return 'TablePager_sort';
290  }
291 
298  public function getNavigationBar() {
299  if ( !$this->isNavigationBarShown() ) {
300  return '';
301  }
302 
303  $this->getOutput()->enableOOUI();
304 
305  $types = [ 'first', 'prev', 'next', 'last' ];
306 
307  $queries = $this->getPagingQueries();
308 
309  $buttons = [];
310 
311  $title = $this->getTitle();
312 
313  foreach ( $types as $type ) {
314  $buttons[] = new \OOUI\ButtonWidget( [
315  // Messages used here:
316  // * table_pager_first
317  // * table_pager_prev
318  // * table_pager_next
319  // * table_pager_last
320  'classes' => [ 'TablePager-button-' . $type ],
321  'flags' => [ 'progressive' ],
322  'framed' => false,
323  'label' => $this->msg( 'table_pager_' . $type )->text(),
324  'href' => $queries[ $type ] ?
325  $title->getLinkURL( $queries[ $type ] + $this->getDefaultQuery() ) :
326  null,
327  'icon' => $type === 'prev' ? 'previous' : $type,
328  'disabled' => $queries[ $type ] === false
329  ] );
330  }
331  return new \OOUI\ButtonGroupWidget( [
332  'classes' => [ $this->getNavClass() ],
333  'items' => $buttons,
334  ] );
335  }
336 
340  public function getModuleStyles() {
341  return array_merge(
342  parent::getModuleStyles(), [ 'oojs-ui.styles.icons-movement' ]
343  );
344  }
345 
352  public function getLimitSelect( $attribs = [] ) {
353  $select = new XmlSelect( 'limit', false, $this->mLimit );
354  $select->addOptions( $this->getLimitSelectList() );
355  foreach ( $attribs as $name => $value ) {
356  $select->setAttribute( $name, $value );
357  }
358  return $select->getHTML();
359  }
360 
368  public function getLimitSelectList() {
369  # Add the current limit from the query string
370  # to avoid that the limit is lost after clicking Go next time
371  if ( !in_array( $this->mLimit, $this->mLimitsShown ) ) {
372  $this->mLimitsShown[] = $this->mLimit;
373  sort( $this->mLimitsShown );
374  }
375  $ret = [];
376  foreach ( $this->mLimitsShown as $key => $value ) {
377  # The pair is either $index => $limit, in which case the $value
378  # will be numeric, or $limit => $text, in which case the $value
379  # will be a string.
380  if ( is_int( $value ) ) {
381  $limit = $value;
382  $text = $this->getLanguage()->formatNum( $limit );
383  } else {
384  $limit = $key;
385  $text = $value;
386  }
387  $ret[$text] = $limit;
388  }
389  return $ret;
390  }
391 
400  public function getHiddenFields( $noResubmit = [] ) {
401  $noResubmit = (array)$noResubmit;
402  $query = $this->getRequest()->getQueryValues();
403  foreach ( $noResubmit as $name ) {
404  unset( $query[$name] );
405  }
406  $s = '';
407  foreach ( $query as $name => $value ) {
408  $s .= Html::hidden( $name, $value ) . "\n";
409  }
410  return $s;
411  }
412 
418  public function getLimitForm() {
419  return Html::rawElement(
420  'form',
421  [
422  'method' => 'get',
423  'action' => wfScript(),
424  ],
425  "\n" . $this->getLimitDropdown()
426  ) . "\n";
427  }
428 
434  private function getLimitDropdown() {
435  # Make the select with some explanatory text
436  $msgSubmit = $this->msg( 'table_pager_limit_submit' )->escaped();
437 
438  return $this->msg( 'table_pager_limit' )
439  ->rawParams( $this->getLimitSelect() )->escaped() .
440  "\n<input type=\"submit\" value=\"$msgSubmit\"/>\n" .
441  $this->getHiddenFields( [ 'limit' ] );
442  }
443 
451  abstract protected function isFieldSortable( $field );
452 
464  abstract public function formatValue( $name, $value );
465 
475  abstract public function getDefaultSort();
476 
484  abstract protected function getFieldNames();
485 }
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()
setContext(IContextSource $context)
static element( $element, $attribs=[], $contents='')
Identical to rawElement(), but HTML-escapes $contents (like Xml::element()).
Definition: Html.php:236
static rawElement( $element, $attribs=[], $contents='')
Returns an HTML element in a string.
Definition: Html.php:214
static openElement( $element, $attribs=[])
Identical to rawElement(), but has no third parameter and omits the end tag (and the self-closing '/'...
Definition: Html.php:256
static closeElement( $element)
Returns "</$element>".
Definition: Html.php:320
static hidden( $name, $value, array $attribs=[])
Convenience function to produce an input element with type=hidden.
Definition: Html.php:851
Efficient paging for SQL queries that use a (roughly unique) index.
Definition: IndexPager.php:75
int $mLimit
The maximum number of entries to show.
Definition: IndexPager.php:97
makeLink( $text, array $query=null, $type=null)
Make a self-link.
Definition: IndexPager.php:636
const DIR_ASCENDING
Backwards-compatible constant for $mDefaultDirection field (do not change)
Definition: IndexPager.php:79
getPagingQueries()
Get a URL query array for the prev, next, first and last links.
Definition: IndexPager.php:749
isNavigationBarShown()
Returns whether to show the "navigation bar".
Definition: IndexPager.php:847
const DIR_DESCENDING
Backwards-compatible constant for $mDefaultDirection field (do not change)
Definition: IndexPager.php:81
Class that generates HTML anchor link elements for pages.
setText( $text)
Set the text of the ParserOutput.
Table-based display with a user-selectable sort order.
Definition: TablePager.php:29
getTableClass()
TablePager relies on mw-datatable for styling, see T214208.
Definition: TablePager.php:272
getBody()
Get the formatted result list.
Definition: TablePager.php:77
getRowAttrs( $row)
Get attributes to be applied to the given row.
Definition: TablePager.php:232
getHiddenFields( $noResubmit=[])
Get <input type="hidden"> elements for use in a method="get" form.
Definition: TablePager.php:400
getNavigationBar()
A navigation bar with images.
Definition: TablePager.php:298
getSortHeaderClass()
Definition: TablePager.php:288
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.
Definition: TablePager.php:352
getLimitSelectList()
Get a list of items to show in a "<select>" element of limits.
Definition: TablePager.php:368
formatRow( $row)
Definition: TablePager.php:191
stdClass $mCurrentRow
Definition: TablePager.php:34
getIndexField()
Returns the name of the index field.If the pager supports multiple orders, it may return an array of ...
Definition: TablePager.php:262
string $mSort
Definition: TablePager.php:31
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.
Definition: TablePager.php:254
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.
Definition: TablePager.php:107
__construct(IContextSource $context=null, LinkRenderer $linkRenderer=null)
Definition: TablePager.php:42
formatValue( $name, $value)
Format a table cell.
getLimitForm()
Get a form containing a limit selection dropdown.
Definition: TablePager.php:418
getBodyOutput()
Get the formatted result list.
Definition: TablePager.php:90
getModuleStyles()
ResourceLoader modules that must be loaded to provide correct styling for this pager....
Definition: TablePager.php:340
getRowClass( $row)
Get a class name to be applied to the given row.
Definition: TablePager.php:220
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