| 1 | <?php |
| 2 | /** |
| 3 | * Copyright 2016 |
| 4 | * |
| 5 | * This program is free software; you can redistribute it and/or modify |
| 6 | * it under the terms of the GNU General Public License as published by |
| 7 | * the Free Software Foundation; either version 2 of the License, or |
| 8 | * (at your option) any later version. |
| 9 | * |
| 10 | * This program is distributed in the hope that it will be useful, |
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | * GNU General Public License for more details. |
| 14 | * |
| 15 | * You should have received a copy of the GNU General Public License along |
| 16 | * with this program; if not, write to the Free Software Foundation, Inc., |
| 17 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
| 18 | * http://www.gnu.org/copyleft/gpl.html |
| 19 | * |
| 20 | * @file |
| 21 | */ |
| 22 | |
| 23 | namespace MediaWiki\Config; |
| 24 | |
| 25 | use Wikimedia\Assert\Assert; |
| 26 | use Wikimedia\Services\SalvageableService; |
| 27 | |
| 28 | /** |
| 29 | * Object which holds currently registered configuration options. |
| 30 | * |
| 31 | * @deprecated since 1.42; Introduced but seems unused since inception. |
| 32 | * @since 1.32 |
| 33 | */ |
| 34 | class ConfigRepository implements SalvageableService { |
| 35 | /** @var ConfigFactory */ |
| 36 | private $configFactory; |
| 37 | |
| 38 | /** @var array */ |
| 39 | private $configItems = [ |
| 40 | 'private' => [], |
| 41 | 'public' => [], |
| 42 | ]; |
| 43 | |
| 44 | /** |
| 45 | * @param ConfigFactory $configFactory |
| 46 | */ |
| 47 | public function __construct( ConfigFactory $configFactory ) { |
| 48 | $this->configFactory = $configFactory; |
| 49 | } |
| 50 | |
| 51 | /** |
| 52 | * Returns true, if this repository contains a configuration with a specific name. |
| 53 | * |
| 54 | * @param string $name The name of the config to check the existence of |
| 55 | * @param bool $alsoPrivate If set to true, will check the private config options, too |
| 56 | * @return bool |
| 57 | */ |
| 58 | public function has( $name, $alsoPrivate = false ) { |
| 59 | return isset( $this->configItems['public'][$name] ) || |
| 60 | ( $alsoPrivate && isset( $this->configItems['private'][$name] ) ); |
| 61 | } |
| 62 | |
| 63 | /** |
| 64 | * Returns the ConfigItem with the given name, if there's one. Throws a ConfigException |
| 65 | * otherwise. |
| 66 | * |
| 67 | * @param string $name The name of the configuration option to get |
| 68 | * @return array |
| 69 | * @throws ConfigException |
| 70 | */ |
| 71 | public function get( $name ) { |
| 72 | if ( !$this->has( $name, true ) ) { |
| 73 | throw new ConfigException( 'The configuration option ' . $name . ' does not exist.' ); |
| 74 | } |
| 75 | |
| 76 | return $this->configItems['public'][$name] ?? $this->configItems['private'][$name]; |
| 77 | } |
| 78 | |
| 79 | /** |
| 80 | * Returns an array of all configuration items saved in this ConfigRepository. This includes |
| 81 | * all configuration options, including the ones marked as private and public. |
| 82 | * |
| 83 | * Note: This function does not do any permission checks or something similar. You should not |
| 84 | * use this function, if the output will be available to the public audience! Use |
| 85 | * ConfigRepository::getPublic() instead. |
| 86 | * |
| 87 | * @return array |
| 88 | */ |
| 89 | public function getAll() { |
| 90 | return array_merge( $this->configItems['private'], $this->configItems['public'] ); |
| 91 | } |
| 92 | |
| 93 | /** |
| 94 | * Returns an array of all public configuration options saved in this ConfigRepository. |
| 95 | * |
| 96 | * @return array |
| 97 | */ |
| 98 | public function getPublic() { |
| 99 | return $this->configItems['public']; |
| 100 | } |
| 101 | |
| 102 | /** |
| 103 | * Returns the description of the given config option, This can be either a localized |
| 104 | * description, if one such, or the (maybe english only) description provided in the |
| 105 | * definition of the configuration. If both is not provided an empty string is returned. |
| 106 | * |
| 107 | * @param string $name The name of the configuration option to get the description of |
| 108 | * @return string HTML-escaped string |
| 109 | */ |
| 110 | public function getDescriptionOf( $name ) { |
| 111 | $config = $this->get( $name ); |
| 112 | if ( isset( $config['descriptionmsg'] ) ) { |
| 113 | return wfMessage( $config['descriptionmsg'] )->escaped(); |
| 114 | } |
| 115 | if ( isset( $config['description'] ) ) { |
| 116 | return htmlspecialchars( $config['description'] ); |
| 117 | } |
| 118 | return ''; |
| 119 | } |
| 120 | |
| 121 | /** |
| 122 | * Adds the definition of a configuration to this repository. |
| 123 | * |
| 124 | * @param string $name the name of the config |
| 125 | * @param array $config Options of this config. Values are: |
| 126 | * - value: The default value of this configuration, required |
| 127 | * - providedby: The name of the provider of this config (an extension, core, ...), required |
| 128 | * - configregistry: The name of the config to retrieve the value with, required |
| 129 | * - public: whether this option is public or not, if not set, the option is considered as |
| 130 | * "private", optional |
| 131 | * - description: the not localized description of this config option, optional |
| 132 | * - descriptionmsg: The message key of the localized description of this configuration |
| 133 | * option, optional |
| 134 | * @throws ConfigException |
| 135 | */ |
| 136 | public function add( $name, array $config ) { |
| 137 | if ( $this->has( $name ) ) { |
| 138 | throw new ConfigException( 'A configuration with the name ' . $name . |
| 139 | 'does already exist. It is provided by: ' . |
| 140 | $this->get( $name )['providedby'] ); |
| 141 | } |
| 142 | if ( isset( $config['public'] ) && $config['public'] ) { |
| 143 | $this->configItems['public'][$name] = $config; |
| 144 | } else { |
| 145 | $this->configItems['private'][$name] = $config; |
| 146 | } |
| 147 | } |
| 148 | |
| 149 | /** |
| 150 | * Returns true, if there're no elements in this instance, otherwise false. |
| 151 | * |
| 152 | * @param bool $includePrivate Whether configuration options, that are marked as private |
| 153 | * should be included in this check. |
| 154 | * @return bool |
| 155 | */ |
| 156 | public function isEmpty( $includePrivate = false ) { |
| 157 | if ( $includePrivate ) { |
| 158 | return empty( $this->configItems['private'] ) && |
| 159 | empty( $this->configItems[ 'public'] ); |
| 160 | } |
| 161 | return empty( $this->configItems['public'] ); |
| 162 | } |
| 163 | |
| 164 | /** |
| 165 | * Re-uses existing Cache objects from $other. Cache objects are only re-used if the |
| 166 | * registered factory function for both is the same. |
| 167 | * |
| 168 | * @see SalvageableService::salvage() |
| 169 | * |
| 170 | * @param SalvageableService $other The object to salvage state from. $other must have the |
| 171 | * exact same type as $this. |
| 172 | */ |
| 173 | public function salvage( SalvageableService $other ) { |
| 174 | Assert::parameterType( self::class, $other, '$other' ); |
| 175 | /** @var self $other */ |
| 176 | '@phan-var self $other'; |
| 177 | |
| 178 | foreach ( $other->configItems['public'] as $name => $otherConfig ) { |
| 179 | if ( isset( $this->configItems['public'][$name] ) ) { |
| 180 | continue; |
| 181 | } |
| 182 | |
| 183 | $this->add( $name, $otherConfig ); |
| 184 | } |
| 185 | foreach ( $other->configItems['private'] as $name => $otherConfig ) { |
| 186 | if ( isset( $this->configItems['private'][$name] ) ) { |
| 187 | continue; |
| 188 | } |
| 189 | |
| 190 | $this->add( $name, $otherConfig ); |
| 191 | } |
| 192 | |
| 193 | // disable $other |
| 194 | $other->configItems = []; |
| 195 | } |
| 196 | } |