NamespaceInputWidget.php 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. <?php
  2. namespace MediaWiki\Widget;
  3. /**
  4. * Namespace input widget. Displays a dropdown box with the choice of available namespaces.
  5. *
  6. * @copyright 2011-2015 MediaWiki Widgets Team and others; see AUTHORS.txt
  7. * @license MIT
  8. */
  9. class NamespaceInputWidget extends \OOUI\DropdownInputWidget {
  10. /** @var string */
  11. protected $includeAllValue;
  12. /** @var int[] */
  13. protected $exclude;
  14. /**
  15. * @param array $config Configuration options
  16. * - string $config['includeAllValue'] If specified, add a "all namespaces" option to the
  17. * namespace dropdown, and use this as the input value for it
  18. * - int[] $config['exclude'] List of namespace numbers to exclude from the selector
  19. */
  20. public function __construct( array $config = [] ) {
  21. // Configuration initialization
  22. $config['options'] = $this->getNamespaceDropdownOptions( $config );
  23. parent::__construct( $config );
  24. // Properties
  25. $this->includeAllValue = $config['includeAllValue'] ?? null;
  26. $this->exclude = $config['exclude'] ?? [];
  27. // Initialization
  28. $this->addClasses( [ 'mw-widget-namespaceInputWidget' ] );
  29. }
  30. protected function getNamespaceDropdownOptions( array $config ) {
  31. $namespaceOptionsParams = [
  32. 'all' => $config['includeAllValue'] ?? null,
  33. 'exclude' => $config['exclude'] ?? null
  34. ];
  35. $namespaceOptions = \Html::namespaceSelectorOptions( $namespaceOptionsParams );
  36. $options = [];
  37. foreach ( $namespaceOptions as $id => $name ) {
  38. $options[] = [
  39. 'data' => (string)$id,
  40. 'label' => $name,
  41. ];
  42. }
  43. return $options;
  44. }
  45. protected function getJavaScriptClassName() {
  46. return 'mw.widgets.NamespaceInputWidget';
  47. }
  48. public function getConfig( &$config ) {
  49. $config['includeAllValue'] = $this->includeAllValue;
  50. $config['exclude'] = $this->exclude;
  51. // Skip DropdownInputWidget's getConfig(), we don't need 'options' config
  52. $config['dropdown']['$overlay'] = true;
  53. return \OOUI\InputWidget::getConfig( $config );
  54. }
  55. }