MediaWiki  master
TablePager.php
Go to the documentation of this file.
1 <?php
25 
30 abstract class TablePager extends IndexPager {
32  protected $mSort;
33 
35  protected $mCurrentRow;
36 
37  public function __construct( IContextSource $context = null, LinkRenderer $linkRenderer = null ) {
38  if ( $context ) {
39  $this->setContext( $context );
40  }
41 
42  $this->mSort = $this->getRequest()->getText( 'sort' );
43  if ( !array_key_exists( $this->mSort, $this->getFieldNames() )
44  || !$this->isFieldSortable( $this->mSort )
45  ) {
46  $this->mSort = $this->getDefaultSort();
47  }
48  if ( $this->getRequest()->getBool( 'asc' ) ) {
49  $this->mDefaultDirection = IndexPager::DIR_ASCENDING;
50  } elseif ( $this->getRequest()->getBool( 'desc' ) ) {
51  $this->mDefaultDirection = IndexPager::DIR_DESCENDING;
52  } /* Else leave it at whatever the class default is */
53 
54  // Parent constructor needs mSort set, so we call it last
55  parent::__construct( null, $linkRenderer );
56  }
57 
72  final public function getBody() {
73  $this->getOutput()->addModuleStyles( $this->getModuleStyles() );
74  return parent::getBody();
75  }
76 
86  public function getBodyOutput() {
87  $body = parent::getBody();
88 
89  $pout = new ParserOutput;
90  $pout->setText( $body );
91  $pout->addModuleStyles( $this->getModuleStyles() );
92  return $pout;
93  }
94 
104  public function getFullOutput() {
105  $navigation = $this->getNavigationBar();
106  $body = parent::getBody();
107 
108  $pout = new ParserOutput;
109  $pout->setText( $navigation . $body . $navigation );
110  $pout->addModuleStyles( $this->getModuleStyles() );
111  return $pout;
112  }
113 
118  protected function getStartBody() {
119  $sortClass = $this->getSortHeaderClass();
120 
121  $s = '';
122  $fields = $this->getFieldNames();
123 
124  // Make table header
125  foreach ( $fields as $field => $name ) {
126  if ( strval( $name ) == '' ) {
127  $s .= Html::rawElement( 'th', [], "\u{00A0}" ) . "\n";
128  } elseif ( $this->isFieldSortable( $field ) ) {
129  $query = [ 'sort' => $field, 'limit' => $this->mLimit ];
130  $linkType = null;
131  $class = null;
132 
133  if ( $this->mSort == $field ) {
134  // The table is sorted by this field already, make a link to sort in the other direction
135  // We don't actually know in which direction other fields will be sorted by default…
136  if ( $this->mDefaultDirection == IndexPager::DIR_DESCENDING ) {
137  $linkType = 'asc';
138  $class = "$sortClass mw-datatable-is-sorted mw-datatable-is-descending";
139  $query['asc'] = '1';
140  $query['desc'] = '';
141  } else {
142  $linkType = 'desc';
143  $class = "$sortClass mw-datatable-is-sorted mw-datatable-is-ascending";
144  $query['asc'] = '';
145  $query['desc'] = '1';
146  }
147  }
148 
149  $link = $this->makeLink( htmlspecialchars( $name ), $query, $linkType );
150  $s .= Html::rawElement( 'th', [ 'class' => $class ], $link ) . "\n";
151  } else {
152  $s .= Html::element( 'th', [], $name ) . "\n";
153  }
154  }
155 
156  $tableClass = $this->getTableClass();
157  $ret = Html::openElement( 'table', [
158  'class' => " $tableClass" ]
159  );
160  $ret .= Html::rawElement( 'thead', [], Html::rawElement( 'tr', [], "\n" . $s . "\n" ) );
161  $ret .= Html::openElement( 'tbody' ) . "\n";
162 
163  return $ret;
164  }
165 
170  protected function getEndBody() {
171  return "</tbody></table>\n";
172  }
173 
177  protected function getEmptyBody() {
178  $colspan = count( $this->getFieldNames() );
179  $msgEmpty = $this->msg( 'table_pager_empty' )->text();
180  return Html::rawElement( 'tr', [],
181  Html::element( 'td', [ 'colspan' => $colspan ], $msgEmpty ) );
182  }
183 
188  public function formatRow( $row ) {
189  $this->mCurrentRow = $row; // In case formatValue etc need to know
190  $s = Html::openElement( 'tr', $this->getRowAttrs( $row ) ) . "\n";
191  $fieldNames = $this->getFieldNames();
192 
193  foreach ( $fieldNames as $field => $name ) {
194  $value = $row->$field ?? null;
195  $formatted = strval( $this->formatValue( $field, $value ) );
196 
197  if ( $formatted == '' ) {
198  $formatted = "\u{00A0}";
199  }
200 
201  $s .= Html::rawElement( 'td', $this->getCellAttrs( $field, $value ), $formatted ) . "\n";
202  }
203 
204  $s .= Html::closeElement( 'tr' ) . "\n";
205 
206  return $s;
207  }
208 
215  protected function getRowClass( $row ) {
216  return '';
217  }
218 
225  protected function getRowAttrs( $row ) {
226  $class = $this->getRowClass( $row );
227  if ( $class === '' ) {
228  // Return an empty array to avoid clutter in HTML like class=""
229  return [];
230  } else {
231  return [ 'class' => $this->getRowClass( $row ) ];
232  }
233  }
234 
238  protected function getCurrentRow() {
239  return $this->mCurrentRow;
240  }
241 
251  protected function getCellAttrs( $field, $value ) {
252  return [ 'class' => 'TablePager_col_' . $field ];
253  }
254 
258  public function getIndexField() {
259  return $this->mSort;
260  }
261 
266  protected function getTableClass() {
267  return 'mw-datatable';
268  }
269 
273  protected function getNavClass() {
274  return 'TablePager_nav';
275  }
276 
280  protected function getSortHeaderClass() {
281  return 'TablePager_sort';
282  }
283 
288  public function getNavigationBar() {
289  if ( !$this->isNavigationBarShown() ) {
290  return '';
291  }
292 
293  $this->getOutput()->enableOOUI();
294 
295  $types = [ 'first', 'prev', 'next', 'last' ];
296 
297  $queries = $this->getPagingQueries();
298 
299  $buttons = [];
300 
301  $title = $this->getTitle();
302 
303  foreach ( $types as $type ) {
304  $buttons[] = new \OOUI\ButtonWidget( [
305  // Messages used here:
306  // * table_pager_first
307  // * table_pager_prev
308  // * table_pager_next
309  // * table_pager_last
310  'classes' => [ 'TablePager-button-' . $type ],
311  'flags' => [ 'progressive' ],
312  'framed' => false,
313  'label' => $this->msg( 'table_pager_' . $type )->text(),
314  'href' => $queries[ $type ] ?
315  $title->getLinkURL( $queries[ $type ] + $this->getDefaultQuery() ) :
316  null,
317  'icon' => $type === 'prev' ? 'previous' : $type,
318  'disabled' => $queries[ $type ] === false
319  ] );
320  }
321  return new \OOUI\ButtonGroupWidget( [
322  'classes' => [ $this->getNavClass() ],
323  'items' => $buttons,
324  ] );
325  }
326 
332  public function getModuleStyles() {
333  return [ 'mediawiki.pager.tablePager', 'oojs-ui.styles.icons-movement' ];
334  }
335 
342  public function getLimitSelect( $attribs = [] ) {
343  $select = new XmlSelect( 'limit', false, $this->mLimit );
344  $select->addOptions( $this->getLimitSelectList() );
345  foreach ( $attribs as $name => $value ) {
346  $select->setAttribute( $name, $value );
347  }
348  return $select->getHTML();
349  }
350 
358  public function getLimitSelectList() {
359  # Add the current limit from the query string
360  # to avoid that the limit is lost after clicking Go next time
361  if ( !in_array( $this->mLimit, $this->mLimitsShown ) ) {
362  $this->mLimitsShown[] = $this->mLimit;
363  sort( $this->mLimitsShown );
364  }
365  $ret = [];
366  foreach ( $this->mLimitsShown as $key => $value ) {
367  # The pair is either $index => $limit, in which case the $value
368  # will be numeric, or $limit => $text, in which case the $value
369  # will be a string.
370  if ( is_int( $value ) ) {
371  $limit = $value;
372  $text = $this->getLanguage()->formatNum( $limit );
373  } else {
374  $limit = $key;
375  $text = $value;
376  }
377  $ret[$text] = $limit;
378  }
379  return $ret;
380  }
381 
390  public function getHiddenFields( $blacklist = [] ) {
391  $blacklist = (array)$blacklist;
392  $query = $this->getRequest()->getQueryValues();
393  foreach ( $blacklist as $name ) {
394  unset( $query[$name] );
395  }
396  $s = '';
397  foreach ( $query as $name => $value ) {
398  $s .= Html::hidden( $name, $value ) . "\n";
399  }
400  return $s;
401  }
402 
408  public function getLimitForm() {
409  return Html::rawElement(
410  'form',
411  [
412  'method' => 'get',
413  'action' => wfScript(),
414  ],
415  "\n" . $this->getLimitDropdown()
416  ) . "\n";
417  }
418 
424  private function getLimitDropdown() {
425  # Make the select with some explanatory text
426  $msgSubmit = $this->msg( 'table_pager_limit_submit' )->escaped();
427 
428  return $this->msg( 'table_pager_limit' )
429  ->rawParams( $this->getLimitSelect() )->escaped() .
430  "\n<input type=\"submit\" value=\"$msgSubmit\"/>\n" .
431  $this->getHiddenFields( [ 'limit' ] );
432  }
433 
440  abstract protected function isFieldSortable( $field );
441 
454  abstract public function formatValue( $name, $value );
455 
467  abstract public function getDefaultSort();
468 
476  abstract protected function getFieldNames();
477 }
ContextSource\$context
IContextSource $context
Definition: ContextSource.php:33
IndexPager\$mLimit
int $mLimit
The maximum number of entries to show.
Definition: IndexPager.php:95
ParserOutput
Definition: ParserOutput.php:25
TablePager\getHiddenFields
getHiddenFields( $blacklist=[])
Get <input type="hidden"> elements for use in a method="get" form.
Definition: TablePager.php:390
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:258
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:251
TablePager\formatValue
formatValue( $name, $value)
Format a table cell.
TablePager\getTableClass
getTableClass()
TablePager relies on mw-datatable for styling, see T214208.
Definition: TablePager.php:266
TablePager\getEndBody
getEndBody()
Definition: TablePager.php:170
TablePager\getFullOutput
getFullOutput()
Get the formatted result list, with navigation bars.
Definition: TablePager.php:104
IndexPager\getPagingQueries
getPagingQueries()
Get a URL query array for the prev, next, first and last links.
Definition: IndexPager.php:710
$s
$s
Definition: mergeMessageFileList.php:185
IndexPager\$linkRenderer
LinkRenderer $linkRenderer
Definition: IndexPager.php:166
TablePager\__construct
__construct(IContextSource $context=null, LinkRenderer $linkRenderer=null)
Definition: TablePager.php:37
IndexPager\makeLink
makeLink( $text, array $query=null, $type=null)
Make a self-link.
Definition: IndexPager.php:607
ContextSource\getRequest
getRequest()
Definition: ContextSource.php:71
ContextSource\getTitle
getTitle()
Definition: ContextSource.php:79
IndexPager\DIR_ASCENDING
const DIR_ASCENDING
Backwards-compatible constant for $mDefaultDirection field (do not change)
Definition: IndexPager.php:77
XmlSelect
Class for generating HTML <select> or <datalist> elements.
Definition: XmlSelect.php:26
ContextSource\getLanguage
getLanguage()
Definition: ContextSource.php:128
TablePager\getBodyOutput
getBodyOutput()
Get the formatted result list.
Definition: TablePager.php:86
Html\closeElement
static closeElement( $element)
Returns "</$element>".
Definition: Html.php:315
TablePager\getSortHeaderClass
getSortHeaderClass()
Definition: TablePager.php:280
TablePager\getRowAttrs
getRowAttrs( $row)
Get attributes to be applied to the given row.
Definition: TablePager.php:225
wfScript
wfScript( $script='index')
Get the path to a specified script file, respecting file extensions; this is a wrapper around $wgScri...
Definition: GlobalFunctions.php:2530
ContextSource\getOutput
getOutput()
Definition: ContextSource.php:112
TablePager
Table-based display with a user-selectable sort order.
Definition: TablePager.php:30
TablePager\$mCurrentRow
stdClass $mCurrentRow
Definition: TablePager.php:35
$title
$title
Definition: testCompression.php:38
ParserOutput\setText
setText( $text)
Definition: ParserOutput.php:687
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:55
TablePager\getNavClass
getNavClass()
Definition: TablePager.php:273
ContextSource\msg
msg( $key,... $params)
Get a Message object with context set Parameters are the same as wfMessage()
Definition: ContextSource.php:168
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:288
TablePager\getLimitDropdown
getLimitDropdown()
Gets a limit selection dropdown.
Definition: TablePager.php:424
IContextSource
Interface for objects which can provide a MediaWiki context on request.
Definition: IContextSource.php:53
IndexPager\DIR_DESCENDING
const DIR_DESCENDING
Backwards-compatible constant for $mDefaultDirection field (do not change)
Definition: IndexPager.php:79
TablePager\getStartBody
getStartBody()
Definition: TablePager.php:118
IndexPager\isNavigationBarShown
isNavigationBarShown()
Returns whether to show the "navigation bar".
Definition: IndexPager.php:750
TablePager\formatRow
formatRow( $row)
Definition: TablePager.php:188
TablePager\$mSort
string $mSort
Definition: TablePager.php:32
TablePager\getEmptyBody
getEmptyBody()
Definition: TablePager.php:177
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:73
TablePager\getLimitForm
getLimitForm()
Get a form containing a limit selection dropdown.
Definition: TablePager.php:408
TablePager\getBody
getBody()
Get the formatted result list.
Definition: TablePager.php:72
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:215
TablePager\getLimitSelect
getLimitSelect( $attribs=[])
Get a "<select>" element which has options for each of the allowed limits.
Definition: TablePager.php:342
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:332
TablePager\getLimitSelectList
getLimitSelectList()
Get a list of items to show in a "<select>" element of limits.
Definition: TablePager.php:358
TablePager\getCurrentRow
getCurrentRow()
Definition: TablePager.php:238
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