| 1 |
<?php |
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
namespace MediaWiki\Pager; |
| 22 |
|
| 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; |
| 30 |
use stdClass; |
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
abstract class TablePager extends IndexPager { |
| 39 |
|
| 40 |
protected $mSort; |
| 41 |
|
| 42 |
|
| 43 |
protected $mCurrentRow; |
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
public function __construct( ?IContextSource $context = null, ?LinkRenderer $linkRenderer = null ) { |
| 52 |
if ( $context ) { |
| 53 |
$this->setContext( $context ); |
| 54 |
} |
| 55 |
|
| 56 |
$this->mSort = $this->getRequest()->getText( 'sort' ); |
| 57 |
if ( !array_key_exists( $this->mSort, $this->getFieldNames() ) |
| 58 |
|| !$this->isFieldSortable( $this->mSort ) |
| 59 |
) { |
| 60 |
$this->mSort = $this->getDefaultSort(); |
| 61 |
} |
| 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 |
} |
| 67 |
|
| 68 |
|
| 69 |
parent::__construct( null, $linkRenderer ); |
| 70 |
} |
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
public function getBodyOutput() { |
| 82 |
$body = parent::getBody(); |
| 83 |
|
| 84 |
$pout = new ParserOutput; |
| 85 |
$pout->setRawText( $body ); |
| 86 |
return $pout; |
| 87 |
} |
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
public function getFullOutput() { |
| 99 |
$navigation = $this->getNavigationBar(); |
| 100 |
$body = parent::getBody(); |
| 101 |
|
| 102 |
$pout = new ParserOutput; |
| 103 |
$pout->setRawText( $navigation . $body . $navigation ); |
| 104 |
$pout->addModuleStyles( $this->getModuleStyles() ); |
| 105 |
return $pout; |
| 106 |
} |
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
protected function getStartBody() { |
| 113 |
$sortClass = $this->getSortHeaderClass(); |
| 114 |
|
| 115 |
$s = ''; |
| 116 |
$fields = $this->getFieldNames(); |
| 117 |
|
| 118 |
|
| 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 ]; |
| 124 |
$linkType = null; |
| 125 |
$class = null; |
| 126 |
|
| 127 |
if ( $this->mSort == $field ) { |
| 128 |
|
| 129 |
… |
| 130 |
if ( $this->mDefaultDirection == IndexPager::DIR_DESCENDING ) { |
| 131 |
$linkType = 'asc'; |
| 132 |
$class = "$sortClass mw-datatable-is-sorted mw-datatable-is-descending"; |
| 133 |
$query['asc'] = '1'; |
| 134 |
$query['desc'] = ''; |
| 135 |
} else { |
| 136 |
$linkType = 'desc'; |
| 137 |
$class = "$sortClass mw-datatable-is-sorted mw-datatable-is-ascending"; |
| 138 |
$query['asc'] = ''; |
| 139 |
$query['desc'] = '1'; |
| 140 |
} |
| 141 |
} |
| 142 |
|
| 143 |
$link = $this->makeLink( htmlspecialchars( $name ), $query, $linkType ); |
| 144 |
$s .= Html::rawElement( 'th', [ 'class' => $class ], $link ) . "\n"; |
| 145 |
} else { |
| 146 |
$s .= Html::element( 'th', [], $name ) . "\n"; |
| 147 |
} |
| 148 |
} |
| 149 |
|
| 150 |
$ret = Html::openElement( 'table', [ |
| 151 |
'class' => $this->getTableClass() ] |
| 152 |
); |
| 153 |
$ret .= Html::rawElement( 'thead', [], Html::rawElement( 'tr', [], "\n" . $s . "\n" ) ); |
| 154 |
$ret .= Html::openElement( 'tbody' ) . "\n"; |
| 155 |
|
| 156 |
return $ret; |
| 157 |
} |
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
protected function getEndBody() { |
| 164 |
return "</tbody></table>\n"; |
| 165 |
} |
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
|
| 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 ) ); |
| 175 |
} |
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
|
| 180 |
|
| 181 |
|
| 182 |
public function formatRow( $row ) { |
| 183 |
$this->mCurrentRow = $row; |
| 184 |
$s = Html::openElement( 'tr', $this->getRowAttrs( $row ) ) . "\n"; |
| 185 |
$fieldNames = $this->getFieldNames(); |
| 186 |
|
| 187 |
foreach ( $fieldNames as $field => $name ) { |
| 188 |
$value = $row->$field ?? null; |
| 189 |
$formatted = strval( $this->formatValue( $field, $value ) ); |
| 190 |
|
| 191 |
if ( $formatted == '' ) { |
| 192 |
$formatted = "\u{00A0}"; |
| 193 |
} |
| 194 |
|
| 195 |
$s .= Html::rawElement( 'td', $this->getCellAttrs( $field, $value ), $formatted ) . "\n"; |
| 196 |
} |
| 197 |
|
| 198 |
$s .= Html::closeElement( 'tr' ) . "\n"; |
| 199 |
|
| 200 |
return $s; |
| 201 |
} |
| 202 |
|
| 203 |
|
| 204 |
|
| 205 |
|
| 206 |
|
| 207 |
|
| 208 |
|
| 209 |
|
| 210 |
|
| 211 |
protected function getRowClass( $row ) { |
| 212 |
return ''; |
| 213 |
} |
| 214 |
|
| 215 |
|
| 216 |
|
| 217 |
|
| 218 |
|
| 219 |
|
| 220 |
|
| 221 |
|
| 222 |
|
| 223 |
protected function getRowAttrs( $row ) { |
| 224 |
return [ 'class' => $this->getRowClass( $row ) ]; |
| 225 |
} |
| 226 |
|
| 227 |
|
| 228 |
|
| 229 |
|
| 230 |
protected function getCurrentRow() { |
| 231 |
return $this->mCurrentRow; |
| 232 |
} |
| 233 |
|
| 234 |
|
| 235 |
|
| 236 |
|
| 237 |
|
| 238 |
|
| 239 |
|
| 240 |
|
| 241 |
|
| 242 |
|
| 243 |
|
| 244 |
|
| 245 |
protected function getCellAttrs( $field, $value ) { |
| 246 |
return [ 'class' => 'TablePager_col_' . $field ]; |
| 247 |
} |
| 248 |
|
| 249 |
|
| 250 |
|
| 251 |
|
| 252 |
|
| 253 |
public function getIndexField() { |
| 254 |
return $this->mSort; |
| 255 |
} |
| 256 |
|
| 257 |
|
| 258 |
|
| 259 |
|
| 260 |
|
| 261 |
|
| 262 |
|
| 263 |
protected function getTableClass() { |
| 264 |
return 'mw-datatable'; |
| 265 |
} |
| 266 |
|
| 267 |
|
| 268 |
|
| 269 |
|
| 270 |
|
| 271 |
protected function getNavClass() { |
| 272 |
return 'TablePager_nav'; |
| 273 |
} |
| 274 |
|
| 275 |
|
| 276 |
|
| 277 |
|
| 278 |
|
| 279 |
protected function getSortHeaderClass() { |
| 280 |
return 'TablePager_sort'; |
| 281 |
} |
| 282 |
|
| 283 |
|
| 284 |
|
| 285 |
|
| 286 |
|
| 287 |
|
| 288 |
|
| 289 |
public function getNavigationBar() { |
| 290 |
if ( !$this->isNavigationBarShown() ) { |
| 291 |
return ''; |
| 292 |
} |
| 293 |
|
| 294 |
$this->getOutput()->enableOOUI(); |
| 295 |
|
| 296 |
$types = [ 'first', 'prev', 'next', 'last' ]; |
| 297 |
|
| 298 |
$queries = $this->getPagingQueries(); |
| 299 |
|
| 300 |
$buttons = []; |
| 301 |
|
| 302 |
$title = $this->getTitle(); |
| 303 |
|
| 304 |
foreach ( $types as $type ) { |
| 305 |
$buttons[] = new ButtonWidget( [ |
| 306 |
|
| 307 |
|
| 308 |
|
| 309 |
|
| 310 |
|
| 311 |
'classes' => [ 'TablePager-button-' . $type ], |
| 312 |
'flags' => [ 'progressive' ], |
| 313 |
'framed' => false, |
| 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 ButtonGroupWidget( [ |
| 323 |
'classes' => [ $this->getNavClass() ], |
| 324 |
'items' => $buttons, |
| 325 |
] ); |
| 326 |
} |
| 327 |
|
| 328 |
|
| 329 |
|
| 330 |
|
| 331 |
public function getModuleStyles() { |
| 332 |
return array_merge( |
| 333 |
parent::getModuleStyles(), [ 'oojs-ui.styles.icons-movement' ] |
| 334 |
); |
| 335 |
} |
| 336 |
|
| 337 |
|
| 338 |
|
| 339 |
|
| 340 |
|
| 341 |
|
| 342 |
|
| 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 |
|
| 352 |
|
| 353 |
|
| 354 |
|
| 355 |
|
| 356 |
|
| 357 |
|
| 358 |
|
| 359 |
public function getLimitSelectList() { |
| 360 |
|
| 361 |
|
| 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 |
|
| 369 |
|
| 370 |
|
| 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 |
|
| 383 |
|
| 384 |
|
| 385 |
|
| 386 |
|
| 387 |
|
| 388 |
|
| 389 |
|
| 390 |
|
| 391 |
|
| 392 |
public function getHiddenFields( $noResubmit = [] ) { |
| 393 |
$noResubmit = (array)$noResubmit; |
| 394 |
$query = $this->getRequest()->getQueryValues(); |
| 395 |
foreach ( $noResubmit as $name ) { |
| 396 |
unset( $query[$name] ); |
| 397 |
} |
| 398 |
$s = ''; |
| 399 |
foreach ( $query as $name => $value ) { |
| 400 |
if ( is_array( $value ) ) { |
| 401 |
|
| 402 |
continue; |
| 403 |
} |
| 404 |
$s .= Html::hidden( $name, $value ) . "\n"; |
| 405 |
} |
| 406 |
return $s; |
| 407 |
} |
| 408 |
|
| 409 |
|
| 410 |
|
| 411 |
|
| 412 |
|
| 413 |
|
| 414 |
public function getLimitForm() { |
| 415 |
return Html::rawElement( |
| 416 |
'form', |
| 417 |
[ |
| 418 |
'method' => 'get', |
| 419 |
'action' => wfScript(), |
| 420 |
], |
| 421 |
"\n" . $this->getLimitDropdown() |
| 422 |
) . "\n"; |
| 423 |
} |
| 424 |
|
| 425 |
|
| 426 |
|
| 427 |
|
| 428 |
|
| 429 |
|
| 430 |
private function getLimitDropdown() { |
| 431 |
|
| 432 |
$msgSubmit = $this->msg( 'table_pager_limit_submit' )->escaped(); |
| 433 |
|
| 434 |
return $this->msg( 'table_pager_limit' ) |
| 435 |
->rawParams( $this->getLimitSelect() )->escaped() . |
| 436 |
"\n<input type=\"submit\" value=\"$msgSubmit\"/>\n" . |
| 437 |
$this->getHiddenFields( [ 'limit' ] ); |
| 438 |
} |
| 439 |
|
| 440 |
|
| 441 |
|
| 442 |
|
| 443 |
|
| 444 |
|
| 445 |
|
| 446 |
|
| 447 |
abstract protected function isFieldSortable( $field ); |
| 448 |
|
| 449 |
|
| 450 |
|
| 451 |
|
| 452 |
|
| 453 |
|
| 454 |
|
| 455 |
|
| 456 |
|
| 457 |
|
| 458 |
|
| 459 |
|
| 460 |
abstract public function formatValue( $name, $value ); |
| 461 |
|
| 462 |
|
| 463 |
|
| 464 |
|
| 465 |
|
| 466 |
|
| 467 |
|
| 468 |
|
| 469 |
|
| 470 |
|
| 471 |
abstract public function getDefaultSort(); |
| 472 |
|
| 473 |
|
| 474 |
|
| 475 |
|
| 476 |
|
| 477 |
|
| 478 |
|
| 479 |
|
| 480 |
abstract protected function getFieldNames(); |
| 481 |
} |
| 482 |
|
| 483 |
|
| 484 |
class_alias( TablePager::class, 'TablePager' ); |