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.' );
74 if ( isset( $this->configItems
['public'][$name] ) ) {
75 return $this->configItems
['public'][$name];
77 return $this->configItems
['private'][$name];
81 * Returns an array of all configuration items saved in this ConfigRepository. This includes
82 * all configuration options, including the ones marked as private and public.
84 * Note: This function does not do any permission checks or something similar. You should not
85 * use this function, if the output will be available to the public audience! Use
86 * ConfigRepository::getPublic() instead.
90 public function getAll() {
91 return array_merge( $this->configItems
['private'], $this->configItems
['public'] );
95 * Returns an array of all public configuration options saved in this ConfigRepository.
99 public function getPublic() {
100 return $this->configItems
['public'];
104 * Returns the current value of the configuration option. If no ConfigRegistry was provided
105 * when the config was added to the repository, the default value will be returned.
107 * @param string $name The name of the configuration option to get the value of
109 * @throws \ConfigException
111 public function getValueOf( $name ) {
112 $config = $this->get( $name );
113 if ( !isset( $config['configregistry'] ) ) {
114 return $config['value'];
117 return $this->configFactory
->makeConfig( $config['configregistry'] )->get( $name );
121 * Returns the description of the given config option, This can be either a localized
122 * description, if one such, or the (maybe english only) description provided in the
123 * definition of the configuration. If both is not provided an empty string is returned.
125 * @param string $name The name of the configuration option to get the description of
126 * @return string HTML-escaped string
128 public function getDescriptionOf( $name ) {
129 $config = $this->get( $name );
130 if ( isset( $config['descriptionmsg'] ) ) {
131 return wfMessage( $config['descriptionmsg'] )->escaped();
133 if ( isset( $config['description'] ) ) {
134 return htmlspecialchars( $config['description'] );
140 * Adds the definition of a configuration to this repository.
142 * @param string $name the name of the config
143 * @param array $config Options of this config. Values are:
144 * - value: The default value of this configuration, required
145 * - providedby: The name of the provider of this config (an extension, core, ...), required
146 * - configregistry: The name of the config to retrieve the value with, required
147 * - public: whether this option is public or not, if not set, the option is considered as
148 * "private", optional
149 * - description: the not localized description of this config option, optional
150 * - descriptionmsg: The message key of the localized description of this configuration
152 * @throws \ConfigException
154 public function add( $name, array $config ) {
155 if ( $this->has( $name ) ) {
156 throw new \
ConfigException( 'A configuration with the name ' . $name .
157 'does already exist. It is provided by: ' .
158 $this->get( $name )['providedby'] );
160 if ( isset( $config['public'] ) && $config['public'] ) {
161 $this->configItems
['public'][$name] = $config;
163 $this->configItems
['private'][$name] = $config;
168 * Returns true, if there're no elements in this instance, otherwise false.
170 * @param bool $includePrivate Whether configuration options, that are marked as private
171 * should be included in this check.
174 public function isEmpty( $includePrivate = false ) {
175 if ( $includePrivate ) {
176 return empty( $this->configItems
['private'] ) &&
177 empty( $this->configItems
[ 'public'] );
179 return empty( $this->configItems
['public'] );
183 * Re-uses existing Cache objects from $other. Cache objects are only re-used if the
184 * registered factory function for both is the same.
186 * @see SalvageableService::salvage()
188 * @param SalvageableService $other The object to salvage state from. $other must have the
189 * exact same type as $this.
191 public function salvage( SalvageableService
$other ) {
192 Assert
::parameterType( self
::class, $other, '$other' );
194 /** @var ConfigRepository $other */
195 $otherCurrentObj = $other->current();
196 foreach ( $other->configItems
['public'] as $name => $otherConfig ) {
197 if ( isset( $this->configItems
['public'][$name] ) ) {
201 $this->add( $name, $otherConfig );
203 // recover the pointer of the other config repository
204 if ( $otherCurrentObj === $otherConfig ) {
205 end( $this->configItems
['public'] );
208 foreach ( $other->configItems
['private'] as $name => $otherConfig ) {
209 if ( isset( $this->configItems
['private'][$name] ) ) {
213 $this->add( $name, $otherConfig );
215 // recover the pointer of the other config repository
216 if ( $otherCurrentObj === $otherConfig ) {
217 end( $this->configItems
['private'] );
222 $other->configItems
= [];