MediaWiki  REL1_31
TablePager.php
Go to the documentation of this file.
1 <?php
28 abstract class TablePager extends IndexPager {
29  protected $mSort;
30 
31  protected $mCurrentRow;
32 
33  public function __construct( IContextSource $context = null ) {
34  if ( $context ) {
35  $this->setContext( $context );
36  }
37 
38  $this->mSort = $this->getRequest()->getText( 'sort' );
39  if ( !array_key_exists( $this->mSort, $this->getFieldNames() )
40  || !$this->isFieldSortable( $this->mSort )
41  ) {
42  $this->mSort = $this->getDefaultSort();
43  }
44  if ( $this->getRequest()->getBool( 'asc' ) ) {
45  $this->mDefaultDirection = IndexPager::DIR_ASCENDING;
46  } elseif ( $this->getRequest()->getBool( 'desc' ) ) {
47  $this->mDefaultDirection = IndexPager::DIR_DESCENDING;
48  } /* Else leave it at whatever the class default is */
49 
50  parent::__construct();
51  }
52 
67  final public function getBody() {
68  $this->getOutput()->addModuleStyles( $this->getModuleStyles() );
69  return parent::getBody();
70  }
71 
81  public function getBodyOutput() {
82  $body = parent::getBody();
83 
84  $pout = new ParserOutput;
85  $pout->setText( $body );
86  $pout->addModuleStyles( $this->getModuleStyles() );
87  return $pout;
88  }
89 
99  public function getFullOutput() {
100  $navigation = $this->getNavigationBar();
101  $body = parent::getBody();
102 
103  $pout = new ParserOutput;
104  $pout->setText( $navigation . $body . $navigation );
105  $pout->addModuleStyles( $this->getModuleStyles() );
106  return $pout;
107  }
108 
113  function getStartBody() {
114  $sortClass = $this->getSortHeaderClass();
115 
116  $s = '';
117  $fields = $this->getFieldNames();
118 
119  // Make table header
120  foreach ( $fields as $field => $name ) {
121  if ( strval( $name ) == '' ) {
122  $s .= Html::rawElement( 'th', [], '&#160;' ) . "\n";
123  } elseif ( $this->isFieldSortable( $field ) ) {
124  $query = [ 'sort' => $field, 'limit' => $this->mLimit ];
125  $linkType = null;
126  $class = null;
127 
128  if ( $this->mSort == $field ) {
129  // The table is sorted by this field already, make a link to sort in the other direction
130  // We don't actually know in which direction other fields will be sorted by default…
131  if ( $this->mDefaultDirection == IndexPager::DIR_DESCENDING ) {
132  $linkType = 'asc';
133  $class = "$sortClass TablePager_sort-descending";
134  $query['asc'] = '1';
135  $query['desc'] = '';
136  } else {
137  $linkType = 'desc';
138  $class = "$sortClass TablePager_sort-ascending";
139  $query['asc'] = '';
140  $query['desc'] = '1';
141  }
142  }
143 
144  $link = $this->makeLink( htmlspecialchars( $name ), $query, $linkType );
145  $s .= Html::rawElement( 'th', [ 'class' => $class ], $link ) . "\n";
146  } else {
147  $s .= Html::element( 'th', [], $name ) . "\n";
148  }
149  }
150 
151  $tableClass = $this->getTableClass();
152  $ret = Html::openElement( 'table', [
153  'class' => "mw-datatable $tableClass" ]
154  );
155  $ret .= Html::rawElement( 'thead', [], Html::rawElement( 'tr', [], "\n" . $s . "\n" ) );
156  $ret .= Html::openElement( 'tbody' ) . "\n";
157 
158  return $ret;
159  }
160 
165  function getEndBody() {
166  return "</tbody></table>\n";
167  }
168 
173  function getEmptyBody() {
174  $colspan = count( $this->getFieldNames() );
175  $msgEmpty = $this->msg( 'table_pager_empty' )->text();
176  return Html::rawElement( 'tr', [],
177  Html::element( 'td', [ 'colspan' => $colspan ], $msgEmpty ) );
178  }
179 
185  function formatRow( $row ) {
186  $this->mCurrentRow = $row; // In case formatValue etc need to know
187  $s = Html::openElement( 'tr', $this->getRowAttrs( $row ) ) . "\n";
188  $fieldNames = $this->getFieldNames();
189 
190  foreach ( $fieldNames as $field => $name ) {
191  $value = isset( $row->$field ) ? $row->$field : null;
192  $formatted = strval( $this->formatValue( $field, $value ) );
193 
194  if ( $formatted == '' ) {
195  $formatted = '&#160;';
196  }
197 
198  $s .= Html::rawElement( 'td', $this->getCellAttrs( $field, $value ), $formatted ) . "\n";
199  }
200 
201  $s .= Html::closeElement( 'tr' ) . "\n";
202 
203  return $s;
204  }
205 
214  function getRowClass( $row ) {
215  return '';
216  }
217 
226  function getRowAttrs( $row ) {
227  $class = $this->getRowClass( $row );
228  if ( $class === '' ) {
229  // Return an empty array to avoid clutter in HTML like class=""
230  return [];
231  } else {
232  return [ 'class' => $this->getRowClass( $row ) ];
233  }
234  }
235 
239  protected function getCurrentRow() {
240  return $this->mCurrentRow;
241  }
242 
254  function getCellAttrs( $field, $value ) {
255  return [ 'class' => 'TablePager_col_' . $field ];
256  }
257 
262  function getIndexField() {
263  return $this->mSort;
264  }
265 
269  protected function getTableClass() {
270  return 'TablePager';
271  }
272 
276  protected function getNavClass() {
277  return 'TablePager_nav';
278  }
279 
283  protected function getSortHeaderClass() {
284  return 'TablePager_sort';
285  }
286 
291  public function getNavigationBar() {
292  if ( !$this->isNavigationBarShown() ) {
293  return '';
294  }
295 
296  $this->getOutput()->enableOOUI();
297 
298  $types = [ 'first', 'prev', 'next', 'last' ];
299 
300  $queries = $this->getPagingQueries();
301  $links = [];
302 
303  $buttons = [];
304 
305  $title = $this->getTitle();
306 
307  foreach ( $types as $type ) {
308  $buttons[] = new \OOUI\ButtonWidget( [
309  // Messages used here:
310  // * table_pager_first
311  // * table_pager_prev
312  // * table_pager_next
313  // * table_pager_last
314  'label' => $this->msg( 'table_pager_' . $type )->text(),
315  'href' => $queries[ $type ] ?
316  $title->getLinkURL( $queries[ $type ] + $this->getDefaultQuery() ) :
317  null,
318  'icon' => $type === 'prev' ? 'previous' : $type,
319  'disabled' => $queries[ $type ] === false
320  ] );
321  }
322  return new \OOUI\ButtonGroupWidget( [
323  'classes' => [ $this->getNavClass() ],
324  'items' => $buttons,
325  ] );
326  }
327 
333  public function getModuleStyles() {
334  return [ 'mediawiki.pager.tablePager', 'oojs-ui.styles.icons-movement' ];
335  }
336 
343  public function getLimitSelect( $attribs = [] ) {
344  $select = new XmlSelect( 'limit', false, $this->mLimit );
345  $select->addOptions( $this->getLimitSelectList() );
346  foreach ( $attribs as $name => $value ) {
347  $select->setAttribute( $name, $value );
348  }
349  return $select->getHTML();
350  }
351 
359  public function getLimitSelectList() {
360  # Add the current limit from the query string
361  # to avoid that the limit is lost after clicking Go next time
362  if ( !in_array( $this->mLimit, $this->mLimitsShown ) ) {
363  $this->mLimitsShown[] = $this->mLimit;
364  sort( $this->mLimitsShown );
365  }
366  $ret = [];
367  foreach ( $this->mLimitsShown as $key => $value ) {
368  # The pair is either $index => $limit, in which case the $value
369  # will be numeric, or $limit => $text, in which case the $value
370  # will be a string.
371  if ( is_int( $value ) ) {
372  $limit = $value;
373  $text = $this->getLanguage()->formatNum( $limit );
374  } else {
375  $limit = $key;
376  $text = $value;
377  }
378  $ret[$text] = $limit;
379  }
380  return $ret;
381  }
382 
391  function getHiddenFields( $blacklist = [] ) {
392  $blacklist = (array)$blacklist;
393  $query = $this->getRequest()->getQueryValues();
394  foreach ( $blacklist as $name ) {
395  unset( $query[$name] );
396  }
397  $s = '';
398  foreach ( $query as $name => $value ) {
399  $s .= Html::hidden( $name, $value ) . "\n";
400  }
401  return $s;
402  }
403 
409  function getLimitForm() {
410  return Html::rawElement(
411  'form',
412  [
413  'method' => 'get',
414  'action' => wfScript(),
415  ],
416  "\n" . $this->getLimitDropdown()
417  ) . "\n";
418  }
419 
425  function getLimitDropdown() {
426  # Make the select with some explanatory text
427  $msgSubmit = $this->msg( 'table_pager_limit_submit' )->escaped();
428 
429  return $this->msg( 'table_pager_limit' )
430  ->rawParams( $this->getLimitSelect() )->escaped() .
431  "\n<input type=\"submit\" value=\"$msgSubmit\"/>\n" .
432  $this->getHiddenFields( [ 'limit' ] );
433  }
434 
441  abstract function isFieldSortable( $field );
442 
455  abstract function formatValue( $name, $value );
456 
464  abstract function getDefaultSort();
465 
473  abstract function getFieldNames();
474 }
ContextSource\$context
IContextSource $context
Definition: ContextSource.php:33
ParserOutput
Definition: ParserOutput.php:25
TablePager\getHiddenFields
getHiddenFields( $blacklist=[])
Get <input type="hidden"> elements for use in a method="get" form.
Definition: TablePager.php:391
array
the array() calling protocol came about after MediaWiki 1.4rc1.
TablePager\$mCurrentRow
$mCurrentRow
Definition: TablePager.php:31
TablePager\getIndexField
getIndexField()
Definition: TablePager.php:262
text
design txt This is a brief overview of the new design More thorough and up to date information is available on the documentation wiki at etc Handles the details of getting and saving to the user table of the and dealing with sessions and cookies OutputPage Encapsulates the entire HTML page that will be sent in response to any server request It is used by calling its functions to add text
Definition: design.txt:18
ContextSource\msg
msg( $key)
Get a Message object with context set Parameters are the same as wfMessage()
Definition: ContextSource.php:168
$ret
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:2005
TablePager\getCellAttrs
getCellAttrs( $field, $value)
Get any extra attributes to be applied to the given cell.
Definition: TablePager.php:254
TablePager\formatValue
formatValue( $name, $value)
Format a table cell.
TablePager\getTableClass
getTableClass()
Definition: TablePager.php:269
TablePager\getEndBody
getEndBody()
Definition: TablePager.php:165
TablePager\getFullOutput
getFullOutput()
Get the formatted result list, with navigation bars.
Definition: TablePager.php:99
IndexPager\getPagingQueries
getPagingQueries()
Get a URL query array for the prev, next, first and last links.
Definition: IndexPager.php:567
$s
$s
Definition: mergeMessageFileList.php:187
IndexPager\makeLink
makeLink( $text, array $query=null, $type=null)
Make a self-link.
Definition: IndexPager.php:464
ContextSource\getRequest
getRequest()
Definition: ContextSource.php:71
ContextSource\getTitle
getTitle()
Definition: ContextSource.php:79
IndexPager\DIR_ASCENDING
const DIR_ASCENDING
Constants for the $mDefaultDirection field.
Definition: IndexPager.php:75
php
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:37
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:81
Html\closeElement
static closeElement( $element)
Returns "</$element>".
Definition: Html.php:309
TablePager\getSortHeaderClass
getSortHeaderClass()
Definition: TablePager.php:283
TablePager\getRowAttrs
getRowAttrs( $row)
Get attributes to be applied to the given row.
Definition: TablePager.php:226
wfScript
wfScript( $script='index')
Get the path to a specified script file, respecting file extensions; this is a wrapper around $wgScri...
Definition: GlobalFunctions.php:2890
ContextSource\getOutput
getOutput()
Definition: ContextSource.php:112
TablePager
Table-based display with a user-selectable sort order.
Definition: TablePager.php:28
IndexPager\$mLimit
$mLimit
Definition: IndexPager.php:81
$link
usually copyright or history_copyright This message must be in HTML not wikitext & $link
Definition: hooks.txt:3021
$title
namespace and then decline to actually register it file or subcat img or subcat $title
Definition: hooks.txt:964
ParserOutput\setText
setText( $text)
Definition: ParserOutput.php:519
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:276
Html\hidden
static hidden( $name, $value, array $attribs=[])
Convenience function to produce an input element with type=hidden.
Definition: Html.php:774
$attribs
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:2014
$value
$value
Definition: styleTest.css.php:45
TablePager\getNavigationBar
getNavigationBar()
A navigation bar with images.
Definition: TablePager.php:291
TablePager\getLimitDropdown
getLimitDropdown()
Gets a limit selection dropdown.
Definition: TablePager.php:425
TablePager\$mSort
$mSort
Definition: TablePager.php:29
IContextSource
Interface for objects which can provide a MediaWiki context on request.
Definition: IContextSource.php:53
IndexPager\DIR_DESCENDING
const DIR_DESCENDING
Definition: IndexPager.php:76
TablePager\getStartBody
getStartBody()
Definition: TablePager.php:113
IndexPager\isNavigationBarShown
isNavigationBarShown()
Returns whether to show the "navigation bar".
Definition: IndexPager.php:606
TablePager\formatRow
formatRow( $row)
Definition: TablePager.php:185
$name
Allows to change the fields on the form that will be generated $name
Definition: hooks.txt:302
TablePager\__construct
__construct(IContextSource $context=null)
Definition: TablePager.php:33
TablePager\getEmptyBody
getEmptyBody()
Definition: TablePager.php:173
as
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:22
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:69
TablePager\getLimitForm
getLimitForm()
Get a form containing a limit selection dropdown.
Definition: TablePager.php:409
TablePager\getBody
getBody()
Get the formatted result list.
Definition: TablePager.php:67
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:214
TablePager\getLimitSelect
getLimitSelect( $attribs=[])
Get a "<select>" element which has options for each of the allowed limits.
Definition: TablePager.php:343
Html\element
static element( $element, $attribs=[], $contents='')
Identical to rawElement(), but HTML-escapes $contents (like Xml::element()).
Definition: Html.php:231
$query
null for the 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:1620
TablePager\getModuleStyles
getModuleStyles()
ResourceLoader modules that must be loaded to provide correct styling for this pager.
Definition: TablePager.php:333
TablePager\getLimitSelectList
getLimitSelectList()
Get a list of items to show in a "<select>" element of limits.
Definition: TablePager.php:359
TablePager\getCurrentRow
getCurrentRow()
Definition: TablePager.php:239
$queries
$queries
Definition: profileinfo.php:405
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:48