HTMLNamespacesMultiselectField.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. <?php
  2. use MediaWiki\Widget\NamespacesMultiselectWidget;
  3. use MediaWiki\MediaWikiServices;
  4. /**
  5. * Implements a tag multiselect input field for namespaces.
  6. *
  7. * The result is the array of namespaces
  8. *
  9. * TODO: This widget duplicates a lot from HTMLTitlesMultiselectField,
  10. * which itself duplicates HTMLUsersMultiselectField. These classes
  11. * should be refactored.
  12. *
  13. * @note This widget is not likely to remain functional in non-OOUI forms.
  14. */
  15. class HTMLNamespacesMultiselectField extends HTMLSelectNamespace {
  16. public function loadDataFromRequest( $request ) {
  17. $value = $request->getText( $this->mName, $this->getDefault() );
  18. $namespaces = explode( "\n", $value );
  19. // Remove empty lines
  20. $namespaces = array_values( array_filter( $namespaces, function ( $namespace ) {
  21. return trim( $namespace ) !== '';
  22. } ) );
  23. // This function is expected to return a string
  24. return implode( "\n", $namespaces );
  25. }
  26. public function validate( $value, $alldata ) {
  27. if ( !$this->mParams['exists'] || $value === '' ) {
  28. return true;
  29. }
  30. if ( is_null( $value ) ) {
  31. return false;
  32. }
  33. // $value is a string, because HTMLForm fields store their values as strings
  34. $namespaces = explode( "\n", $value );
  35. if ( isset( $this->mParams['max'] ) && ( count( $namespaces ) > $this->mParams['max'] ) ) {
  36. return $this->msg( 'htmlform-int-toohigh', $this->mParams['max'] );
  37. }
  38. foreach ( $namespaces as $namespace ) {
  39. if (
  40. $namespace < 0 ||
  41. !MediaWikiServices::getInstance()->getNamespaceInfo()->exists( $namespace )
  42. ) {
  43. return $this->msg( 'htmlform-select-badoption' );
  44. }
  45. $result = parent::validate( $namespace, $alldata );
  46. if ( $result !== true ) {
  47. return $result;
  48. }
  49. }
  50. return true;
  51. }
  52. public function getInputHTML( $value ) {
  53. $this->mParent->getOutput()->enableOOUI();
  54. return $this->getInputOOUI( $value );
  55. }
  56. public function getInputOOUI( $value ) {
  57. $params = [
  58. 'id' => $this->mID,
  59. 'name' => $this->mName,
  60. 'dir' => $this->mDir,
  61. ];
  62. if ( isset( $this->mParams['disabled'] ) ) {
  63. $params['disabled'] = $this->mParams['disabled'];
  64. }
  65. if ( isset( $this->mParams['default'] ) ) {
  66. $params['default'] = $this->mParams['default'];
  67. }
  68. if ( isset( $this->mParams['placeholder'] ) ) {
  69. $params['placeholder'] = $this->mParams['placeholder'];
  70. } else {
  71. $params['placeholder'] = $this->msg( 'mw-widgets-titlesmultiselect-placeholder' )->plain();
  72. }
  73. if ( isset( $this->mParams['max'] ) ) {
  74. $params['tagLimit'] = $this->mParams['max'];
  75. }
  76. if ( isset( $this->mParams['input'] ) ) {
  77. $params['input'] = $this->mParams['input'];
  78. }
  79. if ( !is_null( $value ) ) {
  80. // $value is a string, but the widget expects an array
  81. $params['default'] = $value === '' ? [] : explode( "\n", $value );
  82. }
  83. // Make the field auto-infusable when it's used inside a legacy HTMLForm rather than OOUIHTMLForm
  84. $params['infusable'] = true;
  85. $params['classes'] = [ 'mw-htmlform-field-autoinfuse' ];
  86. $widget = new NamespacesMultiselectWidget( $params );
  87. $widget->setAttributes( [ 'data-mw-modules' => implode( ',', $this->getOOUIModules() ) ] );
  88. return $widget;
  89. }
  90. protected function shouldInfuseOOUI() {
  91. return true;
  92. }
  93. protected function getOOUIModules() {
  94. return [ 'mediawiki.widgets.NamespacesMultiselectWidget' ];
  95. }
  96. }