3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation; either version 2 of the License, or
6 * (at your option) any later version.
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
13 * You should have received a copy of the GNU General Public License along
14 * with this program; if not, write to the Free Software Foundation, Inc.,
15 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
16 * http://www.gnu.org/copyleft/gpl.html
21 namespace MediaWiki\Pager
;
23 use MediaWiki\Context\IContextSource
;
24 use MediaWiki\Html\Html
;
25 use MediaWiki\Linker\LinkRenderer
;
26 use MediaWiki\Parser\ParserOutput
;
27 use MediaWiki\Xml\XmlSelect
;
28 use OOUI\ButtonGroupWidget
;
29 use OOUI\ButtonWidget
;
33 * Table-based display with a user-selectable sort order
38 abstract class TablePager
extends IndexPager
{
43 protected $mCurrentRow;
48 * @param IContextSource|null $context
49 * @param LinkRenderer|null $linkRenderer
51 public function __construct( ?IContextSource
$context = null, ?LinkRenderer
$linkRenderer = null ) {
53 $this->setContext( $context );
56 $this->mSort
= $this->getRequest()->getText( 'sort' );
57 if ( !array_key_exists( $this->mSort
, $this->getFieldNames() )
58 ||
!$this->isFieldSortable( $this->mSort
)
60 $this->mSort
= $this->getDefaultSort();
62 if ( $this->getRequest()->getBool( 'asc' ) ) {
63 $this->mDefaultDirection
= IndexPager
::DIR_ASCENDING
;
64 } elseif ( $this->getRequest()->getBool( 'desc' ) ) {
65 $this->mDefaultDirection
= IndexPager
::DIR_DESCENDING
;
66 } /* Else leave it at whatever the class default is */
68 // Parent constructor needs mSort set, so we call it last
69 parent
::__construct( null, $linkRenderer );
73 * Get the formatted result list.
75 * Calls getBody() and getModuleStyles() and builds a ParserOutput object. (This is a bit hacky
79 * @return ParserOutput
81 public function getBodyOutput() {
82 $body = parent
::getBody();
84 $pout = new ParserOutput
;
85 $pout->setRawText( $body );
90 * Get the formatted result list, with navigation bars.
92 * Calls getBody(), getNavigationBar() and getModuleStyles() and
93 * builds a ParserOutput object. (This is a bit hacky but works well.)
96 * @return ParserOutput
98 public function getFullOutput() {
99 $navigation = $this->getNavigationBar();
100 $body = parent
::getBody();
102 $pout = new ParserOutput
;
103 $pout->setRawText( $navigation . $body . $navigation );
104 $pout->addModuleStyles( $this->getModuleStyles() );
109 * @stable to override
112 protected function getStartBody() {
113 $sortClass = $this->getSortHeaderClass();
116 $fields = $this->getFieldNames();
119 foreach ( $fields as $field => $name ) {
120 if ( strval( $name ) == '' ) {
121 $s .= Html
::rawElement( 'th', [], "\u{00A0}" ) . "\n";
122 } elseif ( $this->isFieldSortable( $field ) ) {
123 $query = [ 'sort' => $field, 'limit' => $this->mLimit
];
127 if ( $this->mSort
== $field ) {
128 // The table is sorted by this field already, make a link to sort in the other direction
129 // We don't actually know in which direction other fields will be sorted by default…
130 if ( $this->mDefaultDirection
== IndexPager
::DIR_DESCENDING
) {
132 $class = "$sortClass mw-datatable-is-sorted mw-datatable-is-descending";
137 $class = "$sortClass mw-datatable-is-sorted mw-datatable-is-ascending";
139 $query['desc'] = '1';
143 $link = $this->makeLink( htmlspecialchars( $name ), $query, $linkType );
144 $s .= Html
::rawElement( 'th', [ 'class' => $class ], $link ) . "\n";
146 $s .= Html
::element( 'th', [], $name ) . "\n";
150 $ret = Html
::openElement( 'table', [
151 'class' => $this->getTableClass() ]
153 $ret .= Html
::rawElement( 'thead', [], Html
::rawElement( 'tr', [], "\n" . $s . "\n" ) );
154 $ret .= Html
::openElement( 'tbody' ) . "\n";
160 * @stable to override
163 protected function getEndBody() {
164 return "</tbody></table>\n";
170 protected function getEmptyBody() {
171 $colspan = count( $this->getFieldNames() );
172 $msgEmpty = $this->msg( 'table_pager_empty' )->text();
173 return Html
::rawElement( 'tr', [],
174 Html
::element( 'td', [ 'colspan' => $colspan ], $msgEmpty ) );
178 * @stable to override
179 * @param stdClass $row
180 * @return string HTML
182 public function formatRow( $row ) {
183 $this->mCurrentRow
= $row; // In case formatValue etc need to know
184 $s = Html
::openElement( 'tr', $this->getRowAttrs( $row ) ) . "\n";
185 $fieldNames = $this->getFieldNames();
187 foreach ( $fieldNames as $field => $name ) {
188 $value = $row->$field ??
null;
189 $formatted = strval( $this->formatValue( $field, $value ) );
191 if ( $formatted == '' ) {
192 $formatted = "\u{00A0}";
195 $s .= Html
::rawElement( 'td', $this->getCellAttrs( $field, $value ), $formatted ) . "\n";
198 $s .= Html
::closeElement( 'tr' ) . "\n";
204 * Get a class name to be applied to the given row.
206 * @stable to override
208 * @param stdClass $row The database result row
211 protected function getRowClass( $row ) {
216 * Get attributes to be applied to the given row.
218 * @stable to override
220 * @param stdClass $row The database result row
221 * @return array Array of attribute => value
223 protected function getRowAttrs( $row ) {
224 return [ 'class' => $this->getRowClass( $row ) ];
230 protected function getCurrentRow() {
231 return $this->mCurrentRow
;
235 * Get any extra attributes to be applied to the given cell. Don't
236 * take this as an excuse to hardcode styles; use classes and
237 * CSS instead. Row context is available in $this->mCurrentRow
239 * @stable to override
241 * @param string $field The column
242 * @param string $value The cell contents
243 * @return array Array of attr => value
245 protected function getCellAttrs( $field, $value ) {
246 return [ 'class' => 'TablePager_col_' . $field ];
251 * @stable to override
253 public function getIndexField() {
258 * TablePager relies on `mw-datatable` for styling, see T214208
260 * @stable to override
263 protected function getTableClass() {
264 return 'mw-datatable';
268 * @stable to override
271 protected function getNavClass() {
272 return 'TablePager_nav';
276 * @stable to override
279 protected function getSortHeaderClass() {
280 return 'TablePager_sort';
284 * A navigation bar with images
286 * @stable to override
287 * @return string HTML
289 public function getNavigationBar() {
290 if ( !$this->isNavigationBarShown() ) {
294 $this->getOutput()->enableOOUI();
296 $types = [ 'first', 'prev', 'next', 'last' ];
298 $queries = $this->getPagingQueries();
302 $title = $this->getTitle();
304 foreach ( $types as $type ) {
305 $buttons[] = new ButtonWidget( [
306 // Messages used here:
307 // * table_pager_first
308 // * table_pager_prev
309 // * table_pager_next
310 // * table_pager_last
311 'classes' => [ 'TablePager-button-' . $type ],
312 'flags' => [ 'progressive' ],
314 'label' => $this->msg( 'table_pager_' . $type )->text(),
315 'href' => $queries[ $type ] ?
316 $title->getLinkURL( $queries[ $type ] +
$this->getDefaultQuery() ) :
318 'icon' => $type === 'prev' ?
'previous' : $type,
319 'disabled' => $queries[ $type ] === false
322 return new ButtonGroupWidget( [
323 'classes' => [ $this->getNavClass() ],
331 public function getModuleStyles() {
333 parent
::getModuleStyles(), [ 'oojs-ui.styles.icons-movement' ]
338 * Get a "<select>" element which has options for each of the allowed limits
340 * @param string[] $attribs Extra attributes to set
341 * @return string HTML fragment
343 public function getLimitSelect( array $attribs = [] ): string {
344 $select = new XmlSelect( 'limit', false, $this->mLimit
);
345 $select->addOptions( $this->getLimitSelectList() );
346 foreach ( $attribs as $name => $value ) {
347 $select->setAttribute( $name, $value );
349 return $select->getHTML();
353 * Get a list of items to show in a "<select>" element of limits.
354 * This can be passed directly to XmlSelect::addOptions().
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
);
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
371 if ( is_int( $value ) ) {
373 $text = $this->getLanguage()->formatNum( $limit );
378 $ret[$text] = $limit;
384 * Get \<input type="hidden"\> elements for use in a method="get" form.
385 * Resubmits all defined elements of the query string, except for a
386 * exclusion list, passed in the $noResubmit parameter.
387 * Also array values are discarded for security reasons (per WebRequest::getVal)
389 * @param array $noResubmit Parameters from the request query which should not be resubmitted
390 * @return string HTML fragment
392 public function getHiddenFields( $noResubmit = [] ) {
393 $noResubmit = (array)$noResubmit;
394 $query = $this->getRequest()->getQueryValues();
395 foreach ( $noResubmit as $name ) {
396 unset( $query[$name] );
399 foreach ( $query as $name => $value ) {
400 if ( is_array( $value ) ) {
401 // Per WebRequest::getVal: Array values are discarded for security reasons.
404 $s .= Html
::hidden( $name, $value ) . "\n";
410 * Get a form containing a limit selection dropdown
412 * @return string HTML fragment
414 public function getLimitForm() {
415 return Html
::rawElement(
419 'action' => wfScript(),
421 "\n" . $this->getLimitDropdown()
426 * Gets a limit selection dropdown
430 private function getLimitDropdown() {
431 # Make the select with some explanatory text
432 $msgSubmit = $this->msg( 'table_pager_limit_submit' )->escaped();
434 return $this->msg( 'table_pager_limit' )
435 ->rawParams( $this->getLimitSelect() )->escaped() .
436 "\n<input type=\"submit\" value=\"$msgSubmit\"/>\n" .
437 $this->getHiddenFields( [ 'limit' ] );
441 * Return true if the named field should be sortable by the UI, false
444 * @param string $field
447 abstract protected function isFieldSortable( $field );
450 * Format a table cell. The return value should be HTML, but use an empty
451 * string not   for empty cells. Do not include the <td> and </td>.
453 * The current result row is available as $this->mCurrentRow, in case you
456 * @param string $name The database field name
457 * @param string|null $value The value retrieved from the database, or null if
458 * the row doesn't contain this field
460 abstract public function formatValue( $name, $value );
463 * The database field name used as a default sort order.
465 * Note that this field will only be sorted on if isFieldSortable returns
466 * true for this field. If not (e.g. paginating on multiple columns), this
467 * should return empty string, and getIndexField should be overridden.
471 abstract public function getDefaultSort();
474 * An array mapping database field names to a textual description of the
475 * field name, for use in the table header. The description should be plain
476 * text, it will be HTML-escaped later.
480 abstract protected function getFieldNames();
483 /** @deprecated class alias since 1.41 */
484 class_alias( TablePager
::class, 'TablePager' );