MediaWiki  master
TablePager.php
Go to the documentation of this file.
1 <?php
28 abstract class TablePager extends IndexPager {
30  protected $mSort;
31 
33  protected $mCurrentRow;
34 
35  public function __construct( IContextSource $context = null ) {
36  if ( $context ) {
37  $this->setContext( $context );
38  }
39 
40  $this->mSort = $this->getRequest()->getText( 'sort' );
41  if ( !array_key_exists( $this->mSort, $this->getFieldNames() )
42  || !$this->isFieldSortable( $this->mSort )
43  ) {
44  $this->mSort = $this->getDefaultSort();
45  }
46  if ( $this->getRequest()->getBool( 'asc' ) ) {
47  $this->mDefaultDirection = IndexPager::DIR_ASCENDING;
48  } elseif ( $this->getRequest()->getBool( 'desc' ) ) {
49  $this->mDefaultDirection = IndexPager::DIR_DESCENDING;
50  } /* Else leave it at whatever the class default is */
51 
52  parent::__construct();
53  }
54 
69  final public function getBody() {
70  $this->getOutput()->addModuleStyles( $this->getModuleStyles() );
71  return parent::getBody();
72  }
73 
83  public function getBodyOutput() {
84  $body = parent::getBody();
85 
86  $pout = new ParserOutput;
87  $pout->setText( $body );
88  $pout->addModuleStyles( $this->getModuleStyles() );
89  return $pout;
90  }
91 
101  public function getFullOutput() {
102  $navigation = $this->getNavigationBar();
103  $body = parent::getBody();
104 
105  $pout = new ParserOutput;
106  $pout->setText( $navigation . $body . $navigation );
107  $pout->addModuleStyles( $this->getModuleStyles() );
108  return $pout;
109  }
110 
115  protected function getStartBody() {
116  $sortClass = $this->getSortHeaderClass();
117 
118  $s = '';
119  $fields = $this->getFieldNames();
120 
121  // Make table header
122  foreach ( $fields as $field => $name ) {
123  if ( strval( $name ) == '' ) {
124  $s .= Html::rawElement( 'th', [], "\u{00A0}" ) . "\n";
125  } elseif ( $this->isFieldSortable( $field ) ) {
126  $query = [ 'sort' => $field, 'limit' => $this->mLimit ];
127  $linkType = null;
128  $class = null;
129 
130  if ( $this->mSort == $field ) {
131  // The table is sorted by this field already, make a link to sort in the other direction
132  // We don't actually know in which direction other fields will be sorted by default…
133  if ( $this->mDefaultDirection == IndexPager::DIR_DESCENDING ) {
134  $linkType = 'asc';
135  $class = "$sortClass mw-datatable-is-sorted mw-datatable-is-descending";
136  $query['asc'] = '1';
137  $query['desc'] = '';
138  } else {
139  $linkType = 'desc';
140  $class = "$sortClass mw-datatable-is-sorted mw-datatable-is-ascending";
141  $query['asc'] = '';
142  $query['desc'] = '1';
143  }
144  }
145 
146  $link = $this->makeLink( htmlspecialchars( $name ), $query, $linkType );
147  $s .= Html::rawElement( 'th', [ 'class' => $class ], $link ) . "\n";
148  } else {
149  $s .= Html::element( 'th', [], $name ) . "\n";
150  }
151  }
152 
153  $tableClass = $this->getTableClass();
154  $ret = Html::openElement( 'table', [
155  'class' => " $tableClass" ]
156  );
157  $ret .= Html::rawElement( 'thead', [], Html::rawElement( 'tr', [], "\n" . $s . "\n" ) );
158  $ret .= Html::openElement( 'tbody' ) . "\n";
159 
160  return $ret;
161  }
162 
167  protected function getEndBody() {
168  return "</tbody></table>\n";
169  }
170 
175  function getEmptyBody() {
176  $colspan = count( $this->getFieldNames() );
177  $msgEmpty = $this->msg( 'table_pager_empty' )->text();
178  return Html::rawElement( 'tr', [],
179  Html::element( 'td', [ 'colspan' => $colspan ], $msgEmpty ) );
180  }
181 
187  function formatRow( $row ) {
188  $this->mCurrentRow = $row; // In case formatValue etc need to know
189  $s = Html::openElement( 'tr', $this->getRowAttrs( $row ) ) . "\n";
190  $fieldNames = $this->getFieldNames();
191 
192  foreach ( $fieldNames as $field => $name ) {
193  $value = $row->$field ?? null;
194  $formatted = strval( $this->formatValue( $field, $value ) );
195 
196  if ( $formatted == '' ) {
197  $formatted = "\u{00A0}";
198  }
199 
200  $s .= Html::rawElement( 'td', $this->getCellAttrs( $field, $value ), $formatted ) . "\n";
201  }
202 
203  $s .= Html::closeElement( 'tr' ) . "\n";
204 
205  return $s;
206  }
207 
216  function getRowClass( $row ) {
217  return '';
218  }
219 
228  function getRowAttrs( $row ) {
229  $class = $this->getRowClass( $row );
230  if ( $class === '' ) {
231  // Return an empty array to avoid clutter in HTML like class=""
232  return [];
233  } else {
234  return [ 'class' => $this->getRowClass( $row ) ];
235  }
236  }
237 
241  protected function getCurrentRow() {
242  return $this->mCurrentRow;
243  }
244 
256  function getCellAttrs( $field, $value ) {
257  return [ 'class' => 'TablePager_col_' . $field ];
258  }
259 
264  function getIndexField() {
265  return $this->mSort;
266  }
267 
272  protected function getTableClass() {
273  return 'mw-datatable';
274  }
275 
279  protected function getNavClass() {
280  return 'TablePager_nav';
281  }
282 
286  protected function getSortHeaderClass() {
287  return 'TablePager_sort';
288  }
289 
294  public function getNavigationBar() {
295  if ( !$this->isNavigationBarShown() ) {
296  return '';
297  }
298 
299  $this->getOutput()->enableOOUI();
300 
301  $types = [ 'first', 'prev', 'next', 'last' ];
302 
303  $queries = $this->getPagingQueries();
304 
305  $buttons = [];
306 
307  $title = $this->getTitle();
308 
309  foreach ( $types as $type ) {
310  $buttons[] = new \OOUI\ButtonWidget( [
311  // Messages used here:
312  // * table_pager_first
313  // * table_pager_prev
314  // * table_pager_next
315  // * table_pager_last
316  'classes' => [ 'TablePager-button-' . $type ],
317  'flags' => [ 'progressive' ],
318  'framed' => false,
319  'label' => $this->msg( 'table_pager_' . $type )->text(),
320  'href' => $queries[ $type ] ?
321  $title->getLinkURL( $queries[ $type ] + $this->getDefaultQuery() ) :
322  null,
323  'icon' => $type === 'prev' ? 'previous' : $type,
324  'disabled' => $queries[ $type ] === false
325  ] );
326  }
327  return new \OOUI\ButtonGroupWidget( [
328  'classes' => [ $this->getNavClass() ],
329  'items' => $buttons,
330  ] );
331  }
332 
338  public function getModuleStyles() {
339  return [ 'mediawiki.pager.tablePager', 'oojs-ui.styles.icons-movement' ];
340  }
341 
348  public function getLimitSelect( $attribs = [] ) {
349  $select = new XmlSelect( 'limit', false, $this->mLimit );
350  $select->addOptions( $this->getLimitSelectList() );
351  foreach ( $attribs as $name => $value ) {
352  $select->setAttribute( $name, $value );
353  }
354  return $select->getHTML();
355  }
356 
364  public function getLimitSelectList() {
365  # Add the current limit from the query string
366  # to avoid that the limit is lost after clicking Go next time
367  if ( !in_array( $this->mLimit, $this->mLimitsShown ) ) {
368  $this->mLimitsShown[] = $this->mLimit;
369  sort( $this->mLimitsShown );
370  }
371  $ret = [];
372  foreach ( $this->mLimitsShown as $key => $value ) {
373  # The pair is either $index => $limit, in which case the $value
374  # will be numeric, or $limit => $text, in which case the $value
375  # will be a string.
376  if ( is_int( $value ) ) {
377  $limit = $value;
378  $text = $this->getLanguage()->formatNum( $limit );
379  } else {
380  $limit = $key;
381  $text = $value;
382  }
383  $ret[$text] = $limit;
384  }
385  return $ret;
386  }
387 
396  function getHiddenFields( $blacklist = [] ) {
397  $blacklist = (array)$blacklist;
398  $query = $this->getRequest()->getQueryValues();
399  foreach ( $blacklist as $name ) {
400  unset( $query[$name] );
401  }
402  $s = '';
403  foreach ( $query as $name => $value ) {
404  $s .= Html::hidden( $name, $value ) . "\n";
405  }
406  return $s;
407  }
408 
414  function getLimitForm() {
415  return Html::rawElement(
416  'form',
417  [
418  'method' => 'get',
419  'action' => wfScript(),
420  ],
421  "\n" . $this->getLimitDropdown()
422  ) . "\n";
423  }
424 
430  function getLimitDropdown() {
431  # Make the select with some explanatory text
432  $msgSubmit = $this->msg( 'table_pager_limit_submit' )->escaped();
433 
434  return $this->msg( 'table_pager_limit' )
435  ->rawParams( $this->getLimitSelect() )->escaped() .
436  "\n<input type=\"submit\" value=\"$msgSubmit\"/>\n" .
437  $this->getHiddenFields( [ 'limit' ] );
438  }
439 
446  abstract function isFieldSortable( $field );
447 
460  abstract function formatValue( $name, $value );
461 
469  abstract function getDefaultSort();
470 
478  abstract function getFieldNames();
479 }
const DIR_DESCENDING
Backwards-compatible constant for $mDefaultDirection field (do not change)
Definition: IndexPager.php:73
The wiki should then use memcached to cache various data To use multiple just add more items to the array To increase the weight of a make its entry a array("192.168.0.1:11211", 2))
setContext(IContextSource $context)
stdClass $mCurrentRow
Definition: TablePager.php:33
Table-based display with a user-selectable sort order.
Definition: TablePager.php:28
null for the local wiki Added should default to null in handler for backwards compatibility add a value to it if you want to add a cookie that have to vary cache options can modify $query
Definition: hooks.txt:1585
static element( $element, $attribs=[], $contents='')
Identical to rawElement(), but HTML-escapes $contents (like Xml::element()).
Definition: Html.php:232
formatRow( $row)
Definition: TablePager.php:187
null means default in associative array with keys and values unescaped Should be merged with default with a value of false meaning to suppress the attribute in associative array with keys and values unescaped noclasses & $ret
Definition: hooks.txt:1982
static openElement( $element, $attribs=[])
Identical to rawElement(), but has no third parameter and omits the end tag (and the self-closing &#39;/&#39;...
Definition: Html.php:252
static rawElement( $element, $attribs=[], $contents='')
Returns an HTML element in a string.
Definition: Html.php:210
Class for generating HTML <select> or <datalist> elements.
Definition: XmlSelect.php:26
$value
getLimitDropdown()
Gets a limit selection dropdown.
Definition: TablePager.php:430
string $mSort
Definition: TablePager.php:30
getDefaultSort()
The database field name used as a default sort order.
msg( $key)
Get a Message object with context set Parameters are the same as wfMessage()
IContextSource $context
This list may contain false positives That usually means there is additional text with links below the first Each row contains links to the first and second as well as the first line of the second redirect text
getRowClass( $row)
Get a class name to be applied to the given row.
Definition: TablePager.php:216
usually copyright or history_copyright This message must be in HTML not wikitext & $link
Definition: hooks.txt:3050
wfScript( $script='index')
Get the path to a specified script file, respecting file extensions; this is a wrapper around $wgScri...
getLimitSelectList()
Get a list of items to show in a "<select>" element of limits.
Definition: TablePager.php:364
IndexPager is an efficient pager which uses a (roughly unique) index in the data set to implement pag...
Definition: IndexPager.php:69
getModuleStyles()
ResourceLoader modules that must be loaded to provide correct styling for this pager.
Definition: TablePager.php:338
makeLink( $text, array $query=null, $type=null)
Make a self-link.
Definition: IndexPager.php:509
getLimitForm()
Get a form containing a limit selection dropdown.
Definition: TablePager.php:414
__construct(IContextSource $context=null)
Definition: TablePager.php:35
getLimitSelect( $attribs=[])
Get a "<select>" element which has options for each of the allowed limits.
Definition: TablePager.php:348
isNavigationBarShown()
Returns whether to show the "navigation bar".
Definition: IndexPager.php:651
getBody()
Get the formatted result list.
Definition: TablePager.php:69
getFieldNames()
An array mapping database field names to a textual description of the field name, for use in the tabl...
static closeElement( $element)
Returns "</$element>".
Definition: Html.php:316
null means default in associative array with keys and values unescaped Should be merged with default with a value of false meaning to suppress the attribute in associative array with keys and values unescaped noclasses just before the function returns a value If you return an< a > element with HTML attributes $attribs and contents $html will be returned If you return $ret will be returned and may include noclasses after processing & $attribs
Definition: hooks.txt:1982
this hook is for auditing only or null if authentication failed before getting that far or null if we can t even determine that When $user is not null
Definition: hooks.txt:780
namespace and then decline to actually register it file or subcat img or subcat $title
Definition: hooks.txt:925
This document is intended to provide useful advice for parties seeking to redistribute MediaWiki to end users It s targeted particularly at maintainers for Linux since it s been observed that distribution packages of MediaWiki often break We ve consistently had to recommend that users seeking support use official tarballs instead of their distribution s and this often solves whatever problem the user is having It would be nice if this could such as
Definition: distributors.txt:9
getTableClass()
TablePager relies on mw-datatable for styling, see T214208.
Definition: TablePager.php:272
injection txt This is an overview of how MediaWiki makes use of dependency injection The design described here grew from the discussion of RFC T384 The term dependency this means that anything an object needs to operate should be injected from the the object itself should only know narrow no concrete implementation of the logic it relies on The requirement to inject everything typically results in an architecture that based on two main types of and essentially stateless service objects that use other service objects to operate on the value objects As of the beginning MediaWiki is only starting to use the DI approach Much of the code still relies on global state or direct resulting in a highly cyclical dependency which acts as the top level factory for services in MediaWiki which can be used to gain access to default instances of various services MediaWikiServices however also allows new services to be defined and default services to be redefined Services are defined or redefined by providing a callback the instantiator that will return a new instance of the service When it will create an instance of MediaWikiServices and populate it with the services defined in the files listed by thereby bootstrapping the DI framework Per $wgServiceWiringFiles lists includes ServiceWiring php
Definition: injection.txt:35
getHiddenFields( $blacklist=[])
Get <input type="hidden"> elements for use in a method="get" form.
Definition: TablePager.php:396
static hidden( $name, $value, array $attribs=[])
Convenience function to produce an input element with type=hidden.
Definition: Html.php:797
getSortHeaderClass()
Definition: TablePager.php:286
getCellAttrs( $field, $value)
Get any extra attributes to be applied to the given cell.
Definition: TablePager.php:256
getRowAttrs( $row)
Get attributes to be applied to the given row.
Definition: TablePager.php:228
formatValue( $name, $value)
Format a table cell.
Allows to change the fields on the form that will be generated $name
Definition: hooks.txt:271
getFullOutput()
Get the formatted result list, with navigation bars.
Definition: TablePager.php:101
getPagingQueries()
Get a URL query array for the prev, next, first and last links.
Definition: IndexPager.php:612
$queries
const DIR_ASCENDING
Backwards-compatible constant for $mDefaultDirection field (do not change)
Definition: IndexPager.php:71
getBodyOutput()
Get the formatted result list.
Definition: TablePager.php:83
isFieldSortable( $field)
Return true if the named field should be sortable by the UI, false otherwise.
int $mLimit
The maximum number of entries to show.
Definition: IndexPager.php:89
getNavigationBar()
A navigation bar with images.
Definition: TablePager.php:294