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