MediaWiki REL1_32
TablePager.php
Go to the documentation of this file.
1<?php
28abstract 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', [], "\u{00A0}" ) . "\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 = $row->$field ?? null;
192 $formatted = strval( $this->formatValue( $field, $value ) );
193
194 if ( $formatted == '' ) {
195 $formatted = "\u{00A0}";
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
302 $buttons = [];
303
304 $title = $this->getTitle();
305
306 foreach ( $types as $type ) {
307 $buttons[] = new \OOUI\ButtonWidget( [
308 // Messages used here:
309 // * table_pager_first
310 // * table_pager_prev
311 // * table_pager_next
312 // * table_pager_last
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 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 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 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 function isFieldSortable( $field );
441
454 abstract function formatValue( $name, $value );
455
463 abstract function getDefaultSort();
464
472 abstract function getFieldNames();
473}
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
wfScript( $script='index')
Get the path to a specified script file, respecting file extensions; this is a wrapper around $wgScri...
msg( $key)
Get a Message object with context set Parameters are the same as wfMessage()
IContextSource $context
setContext(IContextSource $context)
IndexPager is an efficient pager which uses a (roughly unique) index in the data set to implement pag...
makeLink( $text, array $query=null, $type=null)
Make a self-link.
const DIR_ASCENDING
Constants for the $mDefaultDirection field.
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
Table-based display with a user-selectable sort order.
getBody()
Get the formatted result list.
getRowAttrs( $row)
Get attributes to be applied to the given row.
getNavigationBar()
A navigation bar with images.
getHiddenFields( $blacklist=[])
Get <input type="hidden"> elements for use in a method="get" form.
__construct(IContextSource $context=null)
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)
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.
formatValue( $name, $value)
Format a table cell.
getLimitForm()
Get a form containing a limit selection dropdown.
getLimitDropdown()
Gets 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
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:2054
usually copyright or history_copyright This message must be in HTML not wikitext & $link
Definition hooks.txt:3106
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:2063
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:1656
processing should stop and the error should be shown to the user * false
Definition hooks.txt:187
Interface for objects which can provide a MediaWiki context on request.
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))
$queries