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.
31 * @deprecated since 1.42; Introduced but seems unused since inception.
34 class ConfigRepository
implements SalvageableService
{
35 /** @var ConfigFactory */
36 private $configFactory;
39 private $configItems = [
44 public function __construct( ConfigFactory
$configFactory ) {
45 $this->configFactory
= $configFactory;
49 * Returns true, if this repository contains a configuration with a specific name.
51 * @param string $name The name of the config to check the existence of
52 * @param bool $alsoPrivate If set to true, will check the private config options, too
55 public function has( $name, $alsoPrivate = false ) {
56 return isset( $this->configItems
['public'][$name] ) ||
57 ( $alsoPrivate && isset( $this->configItems
['private'][$name] ) );
61 * Returns the ConfigItem with the given name, if there's one. Throws a ConfigException
64 * @param string $name The name of the configuration option to get
66 * @throws ConfigException
68 public function get( $name ) {
69 if ( !$this->has( $name, true ) ) {
70 throw new ConfigException( 'The configuration option ' . $name . ' does not exist.' );
73 return $this->configItems
['public'][$name] ??
$this->configItems
['private'][$name];
77 * Returns an array of all configuration items saved in this ConfigRepository. This includes
78 * all configuration options, including the ones marked as private and public.
80 * Note: This function does not do any permission checks or something similar. You should not
81 * use this function, if the output will be available to the public audience! Use
82 * ConfigRepository::getPublic() instead.
86 public function getAll() {
87 return array_merge( $this->configItems
['private'], $this->configItems
['public'] );
91 * Returns an array of all public configuration options saved in this ConfigRepository.
95 public function getPublic() {
96 return $this->configItems
['public'];
100 * Returns the description of the given config option, This can be either a localized
101 * description, if one such, or the (maybe english only) description provided in the
102 * definition of the configuration. If both is not provided an empty string is returned.
104 * @param string $name The name of the configuration option to get the description of
105 * @return string HTML-escaped string
107 public function getDescriptionOf( $name ) {
108 $config = $this->get( $name );
109 if ( isset( $config['descriptionmsg'] ) ) {
110 return wfMessage( $config['descriptionmsg'] )->escaped();
112 if ( isset( $config['description'] ) ) {
113 return htmlspecialchars( $config['description'] );
119 * Adds the definition of a configuration to this repository.
121 * @param string $name the name of the config
122 * @param array $config Options of this config. Values are:
123 * - value: The default value of this configuration, required
124 * - providedby: The name of the provider of this config (an extension, core, ...), required
125 * - configregistry: The name of the config to retrieve the value with, required
126 * - public: whether this option is public or not, if not set, the option is considered as
127 * "private", optional
128 * - description: the not localized description of this config option, optional
129 * - descriptionmsg: The message key of the localized description of this configuration
131 * @throws ConfigException
133 public function add( $name, array $config ) {
134 if ( $this->has( $name ) ) {
135 throw new ConfigException( 'A configuration with the name ' . $name .
136 'does already exist. It is provided by: ' .
137 $this->get( $name )['providedby'] );
139 if ( isset( $config['public'] ) && $config['public'] ) {
140 $this->configItems
['public'][$name] = $config;
142 $this->configItems
['private'][$name] = $config;
147 * Returns true, if there're no elements in this instance, otherwise false.
149 * @param bool $includePrivate Whether configuration options, that are marked as private
150 * should be included in this check.
153 public function isEmpty( $includePrivate = false ) {
154 if ( $includePrivate ) {
155 return empty( $this->configItems
['private'] ) &&
156 empty( $this->configItems
[ 'public'] );
158 return empty( $this->configItems
['public'] );
162 * Re-uses existing Cache objects from $other. Cache objects are only re-used if the
163 * registered factory function for both is the same.
165 * @see SalvageableService::salvage()
167 * @param SalvageableService $other The object to salvage state from. $other must have the
168 * exact same type as $this.
170 public function salvage( SalvageableService
$other ) {
171 Assert
::parameterType( self
::class, $other, '$other' );
172 /** @var self $other */
173 '@phan-var self $other';
175 foreach ( $other->configItems
['public'] as $name => $otherConfig ) {
176 if ( isset( $this->configItems
['public'][$name] ) ) {
180 $this->add( $name, $otherConfig );
182 foreach ( $other->configItems
['private'] as $name => $otherConfig ) {
183 if ( isset( $this->configItems
['private'][$name] ) ) {
187 $this->add( $name, $otherConfig );
191 $other->configItems
= [];