MediaWiki REL1_39
TablePager.php
Go to the documentation of this file.
1<?php
25
31abstract class TablePager extends IndexPager {
33 protected $mSort;
34
36 protected $mCurrentRow;
37
44 public function __construct( IContextSource $context = null, LinkRenderer $linkRenderer = null ) {
45 if ( $context ) {
46 $this->setContext( $context );
47 }
48
49 $this->mSort = $this->getRequest()->getText( 'sort' );
50 if ( !array_key_exists( $this->mSort, $this->getFieldNames() )
51 || !$this->isFieldSortable( $this->mSort )
52 ) {
53 $this->mSort = $this->getDefaultSort();
54 }
55 if ( $this->getRequest()->getBool( 'asc' ) ) {
56 $this->mDefaultDirection = IndexPager::DIR_ASCENDING;
57 } elseif ( $this->getRequest()->getBool( 'desc' ) ) {
58 $this->mDefaultDirection = IndexPager::DIR_DESCENDING;
59 } /* Else leave it at whatever the class default is */
60
61 // Parent constructor needs mSort set, so we call it last
62 parent::__construct( null, $linkRenderer );
63 }
64
79 final public function getBody() {
80 return parent::getBody();
81 }
82
92 public function getBodyOutput() {
93 $body = parent::getBody();
94
95 $pout = new ParserOutput;
96 $pout->setText( $body );
97 return $pout;
98 }
99
109 public function getFullOutput() {
110 $navigation = $this->getNavigationBar();
111 $body = parent::getBody();
112
113 $pout = new ParserOutput;
114 $pout->setText( $navigation . $body . $navigation );
115 $pout->addModuleStyles( $this->getModuleStyles() );
116 return $pout;
117 }
118
123 protected function getStartBody() {
124 $sortClass = $this->getSortHeaderClass();
125
126 $s = '';
127 $fields = $this->getFieldNames();
128
129 // Make table header
130 foreach ( $fields as $field => $name ) {
131 if ( strval( $name ) == '' ) {
132 $s .= Html::rawElement( 'th', [], "\u{00A0}" ) . "\n";
133 } elseif ( $this->isFieldSortable( $field ) ) {
134 $query = [ 'sort' => $field, 'limit' => $this->mLimit ];
135 $linkType = null;
136 $class = null;
137
138 if ( $this->mSort == $field ) {
139 // The table is sorted by this field already, make a link to sort in the other direction
140 // We don't actually know in which direction other fields will be sorted by default…
141 if ( $this->mDefaultDirection == IndexPager::DIR_DESCENDING ) {
142 $linkType = 'asc';
143 $class = "$sortClass mw-datatable-is-sorted mw-datatable-is-descending";
144 $query['asc'] = '1';
145 $query['desc'] = '';
146 } else {
147 $linkType = 'desc';
148 $class = "$sortClass mw-datatable-is-sorted mw-datatable-is-ascending";
149 $query['asc'] = '';
150 $query['desc'] = '1';
151 }
152 }
153
154 $link = $this->makeLink( htmlspecialchars( $name ), $query, $linkType );
155 $s .= Html::rawElement( 'th', [ 'class' => $class ], $link ) . "\n";
156 } else {
157 $s .= Html::element( 'th', [], $name ) . "\n";
158 }
159 }
160
161 $ret = Html::openElement( 'table', [
162 'class' => $this->getTableClass() ]
163 );
164 $ret .= Html::rawElement( 'thead', [], Html::rawElement( 'tr', [], "\n" . $s . "\n" ) );
165 $ret .= Html::openElement( 'tbody' ) . "\n";
166
167 return $ret;
168 }
169
174 protected function getEndBody() {
175 return "</tbody></table>\n";
176 }
177
181 protected function getEmptyBody() {
182 $colspan = count( $this->getFieldNames() );
183 $msgEmpty = $this->msg( 'table_pager_empty' )->text();
184 return Html::rawElement( 'tr', [],
185 Html::element( 'td', [ 'colspan' => $colspan ], $msgEmpty ) );
186 }
187
193 public function formatRow( $row ) {
194 $this->mCurrentRow = $row; // In case formatValue etc need to know
195 $s = Html::openElement( 'tr', $this->getRowAttrs( $row ) ) . "\n";
196 $fieldNames = $this->getFieldNames();
197
198 foreach ( $fieldNames as $field => $name ) {
199 $value = $row->$field ?? null;
200 $formatted = strval( $this->formatValue( $field, $value ) );
201
202 if ( $formatted == '' ) {
203 $formatted = "\u{00A0}";
204 }
205
206 $s .= Html::rawElement( 'td', $this->getCellAttrs( $field, $value ), $formatted ) . "\n";
207 }
208
209 $s .= Html::closeElement( 'tr' ) . "\n";
210
211 return $s;
212 }
213
222 protected function getRowClass( $row ) {
223 return '';
224 }
225
234 protected function getRowAttrs( $row ) {
235 return [ 'class' => $this->getRowClass( $row ) ];
236 }
237
241 protected function getCurrentRow() {
242 return $this->mCurrentRow;
243 }
244
256 protected function getCellAttrs( $field, $value ) {
257 return [ 'class' => 'TablePager_col_' . $field ];
258 }
259
264 public function getIndexField() {
265 return $this->mSort;
266 }
267
274 protected function getTableClass() {
275 return 'mw-datatable';
276 }
277
282 protected function getNavClass() {
283 return 'TablePager_nav';
284 }
285
290 protected function getSortHeaderClass() {
291 return 'TablePager_sort';
292 }
293
300 public function getNavigationBar() {
301 if ( !$this->isNavigationBarShown() ) {
302 return '';
303 }
304
305 $this->getOutput()->enableOOUI();
306
307 $types = [ 'first', 'prev', 'next', 'last' ];
308
309 $queries = $this->getPagingQueries();
310
311 $buttons = [];
312
313 $title = $this->getTitle();
314
315 foreach ( $types as $type ) {
316 $buttons[] = new \OOUI\ButtonWidget( [
317 // Messages used here:
318 // * table_pager_first
319 // * table_pager_prev
320 // * table_pager_next
321 // * table_pager_last
322 'classes' => [ 'TablePager-button-' . $type ],
323 'flags' => [ 'progressive' ],
324 'framed' => false,
325 'label' => $this->msg( 'table_pager_' . $type )->text(),
326 'href' => $queries[ $type ] ?
327 $title->getLinkURL( $queries[ $type ] + $this->getDefaultQuery() ) :
328 null,
329 'icon' => $type === 'prev' ? 'previous' : $type,
330 'disabled' => $queries[ $type ] === false
331 ] );
332 }
333 return new \OOUI\ButtonGroupWidget( [
334 'classes' => [ $this->getNavClass() ],
335 'items' => $buttons,
336 ] );
337 }
338
342 public function getModuleStyles() {
343 return array_merge(
344 parent::getModuleStyles(), [ 'oojs-ui.styles.icons-movement' ]
345 );
346 }
347
354 public function getLimitSelect( $attribs = [] ) {
355 $select = new XmlSelect( 'limit', false, $this->mLimit );
356 $select->addOptions( $this->getLimitSelectList() );
357 foreach ( $attribs as $name => $value ) {
358 $select->setAttribute( $name, $value );
359 }
360 return $select->getHTML();
361 }
362
370 public function getLimitSelectList() {
371 # Add the current limit from the query string
372 # to avoid that the limit is lost after clicking Go next time
373 if ( !in_array( $this->mLimit, $this->mLimitsShown ) ) {
374 $this->mLimitsShown[] = $this->mLimit;
375 sort( $this->mLimitsShown );
376 }
377 $ret = [];
378 foreach ( $this->mLimitsShown as $key => $value ) {
379 # The pair is either $index => $limit, in which case the $value
380 # will be numeric, or $limit => $text, in which case the $value
381 # will be a string.
382 if ( is_int( $value ) ) {
383 $limit = $value;
384 $text = $this->getLanguage()->formatNum( $limit );
385 } else {
386 $limit = $key;
387 $text = $value;
388 }
389 $ret[$text] = $limit;
390 }
391 return $ret;
392 }
393
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 $s .= Html::hidden( $name, $value ) . "\n";
411 }
412 return $s;
413 }
414
420 public function getLimitForm() {
421 return Html::rawElement(
422 'form',
423 [
424 'method' => 'get',
425 'action' => wfScript(),
426 ],
427 "\n" . $this->getLimitDropdown()
428 ) . "\n";
429 }
430
436 private function getLimitDropdown() {
437 # Make the select with some explanatory text
438 $msgSubmit = $this->msg( 'table_pager_limit_submit' )->escaped();
439
440 return $this->msg( 'table_pager_limit' )
441 ->rawParams( $this->getLimitSelect() )->escaped() .
442 "\n<input type=\"submit\" value=\"$msgSubmit\"/>\n" .
443 $this->getHiddenFields( [ 'limit' ] );
444 }
445
453 abstract protected function isFieldSortable( $field );
454
466 abstract public function formatValue( $name, $value );
467
477 abstract public function getDefaultSort();
478
486 abstract protected function getFieldNames();
487}
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)
IndexPager is an efficient pager which uses a (roughly unique) index in the data set to implement pag...
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)
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.
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:26
Interface for objects which can provide a MediaWiki context on request.
foreach( $mmfl['setupFiles'] as $fileName) if($queue) if(empty( $mmfl['quiet'])) $s