Merge "Improve docs for Title::getInternalURL/getCanonicalURL"
[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
75 return $this->configItems['public'][$name] ?? $this->configItems['private'][$name];
76 }
77
78 /**
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.
81 *
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.
85 *
86 * @return array
87 */
88 public function getAll() {
89 return array_merge( $this->configItems['private'], $this->configItems['public'] );
90 }
91
92 /**
93 * Returns an array of all public configuration options saved in this ConfigRepository.
94 *
95 * @return array
96 */
97 public function getPublic() {
98 return $this->configItems['public'];
99 }
100
101 /**
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.
104 *
105 * @param string $name The name of the configuration option to get the value of
106 * @return mixed
107 * @throws \ConfigException
108 */
109 public function getValueOf( $name ) {
110 $config = $this->get( $name );
111 if ( !isset( $config['configregistry'] ) ) {
112 return $config['value'];
113 }
114
115 return $this->configFactory->makeConfig( $config['configregistry'] )->get( $name );
116 }
117
118 /**
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.
122 *
123 * @param string $name The name of the configuration option to get the description of
124 * @return string HTML-escaped string
125 */
126 public function getDescriptionOf( $name ) {
127 $config = $this->get( $name );
128 if ( isset( $config['descriptionmsg'] ) ) {
129 return wfMessage( $config['descriptionmsg'] )->escaped();
130 }
131 if ( isset( $config['description'] ) ) {
132 return htmlspecialchars( $config['description'] );
133 }
134 return '';
135 }
136
137 /**
138 * Adds the definition of a configuration to this repository.
139 *
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
149 * option, optional
150 * @throws \ConfigException
151 */
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'] );
157 }
158 if ( isset( $config['public'] ) && $config['public'] ) {
159 $this->configItems['public'][$name] = $config;
160 } else {
161 $this->configItems['private'][$name] = $config;
162 }
163 }
164
165 /**
166 * Returns true, if there're no elements in this instance, otherwise false.
167 *
168 * @param bool $includePrivate Whether configuration options, that are marked as private
169 * should be included in this check.
170 * @return bool
171 */
172 public function isEmpty( $includePrivate = false ) {
173 if ( $includePrivate ) {
174 return empty( $this->configItems['private'] ) &&
175 empty( $this->configItems[ 'public'] );
176 }
177 return empty( $this->configItems['public'] );
178 }
179
180 /**
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.
183 *
184 * @see SalvageableService::salvage()
185 *
186 * @param SalvageableService $other The object to salvage state from. $other must have the
187 * exact same type as $this.
188 */
189 public function salvage( SalvageableService $other ) {
190 Assert::parameterType( self::class, $other, '$other' );
191
192 /** @var ConfigRepository $other */
193 $otherCurrentObj = $other->current();
194 foreach ( $other->configItems['public'] as $name => $otherConfig ) {
195 if ( isset( $this->configItems['public'][$name] ) ) {
196 continue;
197 }
198
199 $this->add( $name, $otherConfig );
200
201 // recover the pointer of the other config repository
202 if ( $otherCurrentObj === $otherConfig ) {
203 end( $this->configItems['public'] );
204 }
205 }
206 foreach ( $other->configItems['private'] as $name => $otherConfig ) {
207 if ( isset( $this->configItems['private'][$name] ) ) {
208 continue;
209 }
210
211 $this->add( $name, $otherConfig );
212
213 // recover the pointer of the other config repository
214 if ( $otherCurrentObj === $otherConfig ) {
215 end( $this->configItems['private'] );
216 }
217 }
218
219 // disable $other
220 $other->configItems = [];
221 }
222 }