Merge "Added Id to the input box"
[lhc/web/wiklou.git] / includes / config / ConfigRepository.php
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 * @since 1.32
32 */
33 class ConfigRepository implements SalvageableService {
34 /** @var \ConfigFactory */
35 private $configFactory;
36
37 /** @var array */
38 private $configItems = [
39 'private' => [],
40 'public' => [],
41 ];
42
43 /**
44 * @param \ConfigFactory $configFactory
45 */
46 public function __construct( \ConfigFactory $configFactory ) {
47 $this->configFactory = $configFactory;
48 }
49
50 /**
51 * Returns true, if this repository contains a configuration with a specific name.
52 *
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
55 * @return bool
56 */
57 public function has( $name, $alsoPrivate = false ) {
58 return isset( $this->configItems['public'][$name] ) ||
59 ( $alsoPrivate && isset( $this->configItems['private'][$name] ) );
60 }
61
62 /**
63 * Returns the ConfigItem with the given name, if there's one. Throws a ConfigException
64 * otherwise.
65 *
66 * @param string $name The name of the configuration option to get
67 * @return array
68 * @throws \ConfigException
69 */
70 public function get( $name ) {
71 if ( !$this->has( $name, true ) ) {
72 throw new \ConfigException( 'The configuration option ' . $name . ' does not exist.' );
73 }
74 if ( isset( $this->configItems['public'][$name] ) ) {
75 return $this->configItems['public'][$name];
76 }
77 return $this->configItems['private'][$name];
78 }
79
80 /**
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.
83 *
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.
87 *
88 * @return array
89 */
90 public function getAll() {
91 return array_merge( $this->configItems['private'], $this->configItems['public'] );
92 }
93
94 /**
95 * Returns an array of all public configuration options saved in this ConfigRepository.
96 *
97 * @return array
98 */
99 public function getPublic() {
100 return $this->configItems['public'];
101 }
102
103 /**
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.
106 *
107 * @param string $name The name of the configuration option to get the value of
108 * @return mixed
109 * @throws \ConfigException
110 */
111 public function getValueOf( $name ) {
112 $config = $this->get( $name );
113 if ( !isset( $config['configregistry'] ) ) {
114 return $config['value'];
115 }
116
117 return $this->configFactory->makeConfig( $config['configregistry'] )->get( $name );
118 }
119
120 /**
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.
124 *
125 * @param string $name The name of the configuration option to get the description of
126 * @return string HTML-escaped string
127 */
128 public function getDescriptionOf( $name ) {
129 $config = $this->get( $name );
130 if ( isset( $config['descriptionmsg'] ) ) {
131 return wfMessage( $config['descriptionmsg'] )->escaped();
132 }
133 if ( isset( $config['description'] ) ) {
134 return htmlspecialchars( $config['description'] );
135 }
136 return '';
137 }
138
139 /**
140 * Adds the definition of a configuration to this repository.
141 *
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
151 * option, optional
152 * @throws \ConfigException
153 */
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'] );
159 }
160 if ( isset( $config['public'] ) && $config['public'] ) {
161 $this->configItems['public'][$name] = $config;
162 } else {
163 $this->configItems['private'][$name] = $config;
164 }
165 }
166
167 /**
168 * Returns true, if there're no elements in this instance, otherwise false.
169 *
170 * @param bool $includePrivate Whether configuration options, that are marked as private
171 * should be included in this check.
172 * @return bool
173 */
174 public function isEmpty( $includePrivate = false ) {
175 if ( $includePrivate ) {
176 return empty( $this->configItems['private'] ) &&
177 empty( $this->configItems[ 'public'] );
178 }
179 return empty( $this->configItems['public'] );
180 }
181
182 /**
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.
185 *
186 * @see SalvageableService::salvage()
187 *
188 * @param SalvageableService $other The object to salvage state from. $other must have the
189 * exact same type as $this.
190 */
191 public function salvage( SalvageableService $other ) {
192 Assert::parameterType( self::class, $other, '$other' );
193
194 /** @var ConfigRepository $other */
195 $otherCurrentObj = $other->current();
196 foreach ( $other->configItems['public'] as $name => $otherConfig ) {
197 if ( isset( $this->configItems['public'][$name] ) ) {
198 continue;
199 }
200
201 $this->add( $name, $otherConfig );
202
203 // recover the pointer of the other config repository
204 if ( $otherCurrentObj === $otherConfig ) {
205 end( $this->configItems['public'] );
206 }
207 }
208 foreach ( $other->configItems['private'] as $name => $otherConfig ) {
209 if ( isset( $this->configItems['private'][$name] ) ) {
210 continue;
211 }
212
213 $this->add( $name, $otherConfig );
214
215 // recover the pointer of the other config repository
216 if ( $otherCurrentObj === $otherConfig ) {
217 end( $this->configItems['private'] );
218 }
219 }
220
221 // disable $other
222 $other->configItems = [];
223 }
224 }