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.
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.
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
23 namespace MediaWiki\Config
;
25 use Wikimedia\Assert\Assert
;
26 use Wikimedia\Services\SalvageableService
;
29 * Object which holds currently registered configuration options.
33 class ConfigRepository
implements SalvageableService
{
34 /** @var \ConfigFactory */
35 private $configFactory;
38 private $configItems = [
44 * @param \ConfigFactory $configFactory
46 public function __construct( \ConfigFactory
$configFactory ) {
47 $this->configFactory
= $configFactory;
51 * Returns true, if this repository contains a configuration with a specific name.
53 * @param string $name The name of the config to check the existence of
54 * @param bool $alsoPrivate If set to true, will check the private config options, too
57 public function has( $name, $alsoPrivate = false ) {
58 return isset( $this->configItems
['public'][$name] ) ||
59 ( $alsoPrivate && isset( $this->configItems
['private'][$name] ) );
63 * Returns the ConfigItem with the given name, if there's one. Throws a ConfigException
66 * @param string $name The name of the configuration option to get
68 * @throws \ConfigException
70 public function get( $name ) {
71 if ( !$this->has( $name, true ) ) {
72 throw new \
ConfigException( 'The configuration option ' . $name . ' does not exist.' );
75 return $this->configItems
['public'][$name] ??
$this->configItems
['private'][$name];
79 * Returns an array of all configuration items saved in this ConfigRepository. This includes
80 * all configuration options, including the ones marked as private and public.
82 * Note: This function does not do any permission checks or something similar. You should not
83 * use this function, if the output will be available to the public audience! Use
84 * ConfigRepository::getPublic() instead.
88 public function getAll() {
89 return array_merge( $this->configItems
['private'], $this->configItems
['public'] );
93 * Returns an array of all public configuration options saved in this ConfigRepository.
97 public function getPublic() {
98 return $this->configItems
['public'];
102 * Returns the current value of the configuration option. If no ConfigRegistry was provided
103 * when the config was added to the repository, the default value will be returned.
105 * @param string $name The name of the configuration option to get the value of
107 * @throws \ConfigException
109 public function getValueOf( $name ) {
110 $config = $this->get( $name );
111 if ( !isset( $config['configregistry'] ) ) {
112 return $config['value'];
115 return $this->configFactory
->makeConfig( $config['configregistry'] )->get( $name );
119 * Returns the description of the given config option, This can be either a localized
120 * description, if one such, or the (maybe english only) description provided in the
121 * definition of the configuration. If both is not provided an empty string is returned.
123 * @param string $name The name of the configuration option to get the description of
124 * @return string HTML-escaped string
126 public function getDescriptionOf( $name ) {
127 $config = $this->get( $name );
128 if ( isset( $config['descriptionmsg'] ) ) {
129 return wfMessage( $config['descriptionmsg'] )->escaped();
131 if ( isset( $config['description'] ) ) {
132 return htmlspecialchars( $config['description'] );
138 * Adds the definition of a configuration to this repository.
140 * @param string $name the name of the config
141 * @param array $config Options of this config. Values are:
142 * - value: The default value of this configuration, required
143 * - providedby: The name of the provider of this config (an extension, core, ...), required
144 * - configregistry: The name of the config to retrieve the value with, required
145 * - public: whether this option is public or not, if not set, the option is considered as
146 * "private", optional
147 * - description: the not localized description of this config option, optional
148 * - descriptionmsg: The message key of the localized description of this configuration
150 * @throws \ConfigException
152 public function add( $name, array $config ) {
153 if ( $this->has( $name ) ) {
154 throw new \
ConfigException( 'A configuration with the name ' . $name .
155 'does already exist. It is provided by: ' .
156 $this->get( $name )['providedby'] );
158 if ( isset( $config['public'] ) && $config['public'] ) {
159 $this->configItems
['public'][$name] = $config;
161 $this->configItems
['private'][$name] = $config;
166 * Returns true, if there're no elements in this instance, otherwise false.
168 * @param bool $includePrivate Whether configuration options, that are marked as private
169 * should be included in this check.
172 public function isEmpty( $includePrivate = false ) {
173 if ( $includePrivate ) {
174 return empty( $this->configItems
['private'] ) &&
175 empty( $this->configItems
[ 'public'] );
177 return empty( $this->configItems
['public'] );
181 * Re-uses existing Cache objects from $other. Cache objects are only re-used if the
182 * registered factory function for both is the same.
184 * @see SalvageableService::salvage()
186 * @param SalvageableService $other The object to salvage state from. $other must have the
187 * exact same type as $this.
189 public function salvage( SalvageableService
$other ) {
190 Assert
::parameterType( self
::class, $other, '$other' );
192 /** @var ConfigRepository $other */
193 $otherCurrentObj = $other->current();
194 foreach ( $other->configItems
['public'] as $name => $otherConfig ) {
195 if ( isset( $this->configItems
['public'][$name] ) ) {
199 $this->add( $name, $otherConfig );
201 // recover the pointer of the other config repository
202 if ( $otherCurrentObj === $otherConfig ) {
203 end( $this->configItems
['public'] );
206 foreach ( $other->configItems
['private'] as $name => $otherConfig ) {
207 if ( isset( $this->configItems
['private'][$name] ) ) {
211 $this->add( $name, $otherConfig );
213 // recover the pointer of the other config repository
214 if ( $otherCurrentObj === $otherConfig ) {
215 end( $this->configItems
['private'] );
220 $other->configItems
= [];