MediaWiki  master
LocalSettingsGenerator.php
Go to the documentation of this file.
1 <?php
31 
32  protected $extensions = [];
33  protected $skins = [];
34  protected $values = [];
35  protected $groupPermissions = [];
36  protected $dbSettings = '';
37  protected $IP;
38 
42  protected $installer;
43 
47  public function __construct( Installer $installer ) {
48  $this->installer = $installer;
49 
50  $this->extensions = $installer->getVar( '_Extensions' );
51  $this->skins = $installer->getVar( '_Skins' );
52  $this->IP = $installer->getVar( 'IP' );
53 
54  $db = $installer->getDBInstaller( $installer->getVar( 'wgDBtype' ) );
55 
56  $confItems = array_merge(
57  [
58  'wgServer', 'wgScriptPath',
59  'wgPasswordSender', 'wgImageMagickConvertCommand', 'wgShellLocale',
60  'wgLanguageCode', 'wgEnableEmail', 'wgEnableUserEmail', 'wgDiff3',
61  'wgEnotifUserTalk', 'wgEnotifWatchlist', 'wgEmailAuthentication',
62  'wgDBtype', 'wgSecretKey', 'wgRightsUrl', 'wgSitename', 'wgRightsIcon',
63  'wgRightsText', '_MainCacheType', 'wgEnableUploads',
64  '_MemCachedServers', 'wgDBserver', 'wgDBuser',
65  'wgDBpassword', 'wgUseInstantCommons', 'wgUpgradeKey', 'wgDefaultSkin',
66  'wgMetaNamespace', '_Logo', 'wgAuthenticationTokenVersion', 'wgPingback',
67  ],
68  $db->getGlobalNames()
69  );
70 
71  // The WebInstaller form field for "Logo" contains a literal "$wgResourceBasePath",
72  // and site admins are told in the help text that they can use $wgStylePath and $wgScriptPath
73  // within their input, such such treat this as raw PHP for now.
74  $unescaped = [ 'wgRightsIcon', '_Logo', '_Caches' ];
75  $boolItems = [
76  'wgEnableEmail', 'wgEnableUserEmail', 'wgEnotifUserTalk',
77  'wgEnotifWatchlist', 'wgEmailAuthentication', 'wgEnableUploads', 'wgUseInstantCommons',
78  'wgPingback',
79  ];
80 
81  foreach ( $confItems as $c ) {
82  $val = $installer->getVar( $c );
83 
84  if ( in_array( $c, $boolItems ) ) {
85  $val = wfBoolToStr( $val );
86  }
87 
88  if ( !in_array( $c, $unescaped ) && $val !== null ) {
89  $val = self::escapePhpString( $val );
90  }
91 
92  $this->values[$c] = $val;
93  }
94 
95  $this->dbSettings = $db->getLocalSettings();
96  $this->values['wgEmergencyContact'] = $this->values['wgPasswordSender'];
97  }
98 
105  public function setGroupRights( $group, $rightsArr ) {
106  $this->groupPermissions[$group] = $rightsArr;
107  }
108 
116  public static function escapePhpString( $string ) {
117  if ( is_array( $string ) || is_object( $string ) ) {
118  return false;
119  }
120 
121  return strtr(
122  $string,
123  [
124  "\n" => "\\n",
125  "\r" => "\\r",
126  "\t" => "\\t",
127  "\\" => "\\\\",
128  "\$" => "\\\$",
129  "\"" => "\\\""
130  ]
131  );
132  }
133 
140  public function getText() {
141  $localSettings = $this->getDefaultText();
142 
143  if ( count( $this->skins ) ) {
144  $localSettings .= "
145 # Enabled skins.
146 # The following skins were automatically enabled:\n";
147 
148  foreach ( $this->skins as $skinName ) {
149  $localSettings .= $this->generateExtEnableLine( 'skins', $skinName );
150  }
151 
152  $localSettings .= "\n";
153  }
154 
155  if ( count( $this->extensions ) ) {
156  $localSettings .= "
157 # Enabled extensions. Most of the extensions are enabled by adding
158 # wfLoadExtension( 'ExtensionName' );
159 # to LocalSettings.php. Check specific extension documentation for more details.
160 # The following extensions were automatically enabled:\n";
161 
162  foreach ( $this->extensions as $extName ) {
163  $localSettings .= $this->generateExtEnableLine( 'extensions', $extName );
164  }
165 
166  $localSettings .= "\n";
167  }
168 
169  $localSettings .= "
170 # End of automatically generated settings.
171 # Add more configuration options below.\n\n";
172 
173  return $localSettings;
174  }
175 
184  private function generateExtEnableLine( $dir, $name ) {
185  if ( $dir === 'extensions' ) {
186  $jsonFile = 'extension.json';
187  $function = 'wfLoadExtension';
188  } elseif ( $dir === 'skins' ) {
189  $jsonFile = 'skin.json';
190  $function = 'wfLoadSkin';
191  } else {
192  throw new InvalidArgumentException( '$dir was not "extensions" or "skins"' );
193  }
194 
195  $encName = self::escapePhpString( $name );
196 
197  if ( file_exists( "{$this->IP}/$dir/$encName/$jsonFile" ) ) {
198  return "$function( '$encName' );\n";
199  } else {
200  return "require_once \"\$IP/$dir/$encName/$encName.php\";\n";
201  }
202  }
203 
209  public function writeFile( $fileName ) {
210  file_put_contents( $fileName, $this->getText() );
211  }
212 
216  protected function buildMemcachedServerList() {
217  $servers = $this->values['_MemCachedServers'];
218 
219  if ( !$servers ) {
220  return '[]';
221  } else {
222  $ret = '[ ';
223  $servers = explode( ',', $servers );
224 
225  foreach ( $servers as $srv ) {
226  $srv = trim( $srv );
227  $ret .= "'$srv', ";
228  }
229 
230  return rtrim( $ret, ', ' ) . ' ]';
231  }
232  }
233 
237  protected function getDefaultText() {
238  if ( !$this->values['wgImageMagickConvertCommand'] ) {
239  $this->values['wgImageMagickConvertCommand'] = '/usr/bin/convert';
240  $magic = '#';
241  } else {
242  $magic = '';
243  }
244 
245  if ( !$this->values['wgShellLocale'] ) {
246  $this->values['wgShellLocale'] = 'C.UTF-8';
247  $locale = '#';
248  } else {
249  $locale = '';
250  }
251 
252  $metaNamespace = '';
253  if ( $this->values['wgMetaNamespace'] !== $this->values['wgSitename'] ) {
254  $metaNamespace = "\$wgMetaNamespace = \"{$this->values['wgMetaNamespace']}\";\n";
255  }
256 
257  $groupRights = '';
258  $noFollow = '';
259  if ( $this->groupPermissions ) {
260  $groupRights .= "# The following permissions were set based on your choice in the installer\n";
261  foreach ( $this->groupPermissions as $group => $rightArr ) {
262  $group = self::escapePhpString( $group );
263  foreach ( $rightArr as $right => $perm ) {
264  $right = self::escapePhpString( $right );
265  $groupRights .= "\$wgGroupPermissions['$group']['$right'] = " .
266  wfBoolToStr( $perm ) . ";\n";
267  }
268  }
269  $groupRights .= "\n";
270 
271  if ( ( isset( $this->groupPermissions['*']['edit'] ) &&
272  $this->groupPermissions['*']['edit'] === false )
273  && ( isset( $this->groupPermissions['*']['createaccount'] ) &&
274  $this->groupPermissions['*']['createaccount'] === false )
275  && ( isset( $this->groupPermissions['*']['read'] ) &&
276  $this->groupPermissions['*']['read'] !== false )
277  ) {
278  $noFollow = "# Set \$wgNoFollowLinks to true if you open up your wiki to editing by\n"
279  . "# the general public and wish to apply nofollow to external links as a\n"
280  . "# deterrent to spammers. Nofollow is not a comprehensive anti-spam solution\n"
281  . "# and open wikis will generally require other anti-spam measures; for more\n"
282  . "# information, see https://www.mediawiki.org/wiki/Manual:Combating_spam\n"
283  . "\$wgNoFollowLinks = false;\n\n";
284  }
285  }
286 
287  $serverSetting = "";
288  if ( array_key_exists( 'wgServer', $this->values ) && $this->values['wgServer'] !== null ) {
289  $serverSetting = "\n## The protocol and server name to use in fully-qualified URLs\n";
290  $serverSetting .= "\$wgServer = \"{$this->values['wgServer']}\";";
291  }
292 
293  switch ( $this->values['_MainCacheType'] ) {
294  case 'anything':
295  case 'db':
296  case 'memcached':
297  case 'accel':
298  $cacheType = 'CACHE_' . strtoupper( $this->values['_MainCacheType'] );
299  break;
300  case 'none':
301  default:
302  $cacheType = 'CACHE_NONE';
303  }
304 
305  $mcservers = $this->buildMemcachedServerList();
306  if ( file_exists( dirname( __DIR__ ) . '/PlatformSettings.php' ) ) {
307  $platformSettings = "\n## Include platform/distribution defaults";
308  $platformSettings .= "\nrequire_once \"\$IP/includes/PlatformSettings.php\";";
309  } else {
310  $platformSettings = '';
311  }
312 
313  $version = MW_VERSION;
314  return "<?php
315 # This file was automatically generated by the MediaWiki {$version}
316 # installer. If you make manual changes, please keep track in case you
317 # need to recreate them later.
318 #
319 # See includes/DefaultSettings.php for all configurable settings
320 # and their default values, but don't forget to make changes in _this_
321 # file, not there.
322 #
323 # Further documentation for configuration settings may be found at:
324 # https://www.mediawiki.org/wiki/Manual:Configuration_settings
325 
326 # Protect against web entry
327 if ( !defined( 'MEDIAWIKI' ) ) {
328  exit;
329 }
330 {$platformSettings}
331 
332 ## Uncomment this to disable output compression
333 # \$wgDisableOutputCompression = true;
334 
335 \$wgSitename = \"{$this->values['wgSitename']}\";
336 {$metaNamespace}
337 ## The URL base path to the directory containing the wiki;
338 ## defaults for all runtime URL paths are based off of this.
339 ## For more information on customizing the URLs
340 ## (like /w/index.php/Page_title to /wiki/Page_title) please see:
341 ## https://www.mediawiki.org/wiki/Manual:Short_URL
342 \$wgScriptPath = \"{$this->values['wgScriptPath']}\";
343 ${serverSetting}
344 
345 ## The URL path to static resources (images, scripts, etc.)
346 \$wgResourceBasePath = \$wgScriptPath;
347 
348 ## The URL paths to the logo. Make sure you change this from the default,
349 ## or else you'll overwrite your logo when you upgrade!
350 \$wgLogos = [ '1x' => \"{$this->values['_Logo']}\" ];
351 
352 ## UPO means: this is also a user preference option
353 
354 \$wgEnableEmail = {$this->values['wgEnableEmail']};
355 \$wgEnableUserEmail = {$this->values['wgEnableUserEmail']}; # UPO
356 
357 \$wgEmergencyContact = \"{$this->values['wgEmergencyContact']}\";
358 \$wgPasswordSender = \"{$this->values['wgPasswordSender']}\";
359 
360 \$wgEnotifUserTalk = {$this->values['wgEnotifUserTalk']}; # UPO
361 \$wgEnotifWatchlist = {$this->values['wgEnotifWatchlist']}; # UPO
362 \$wgEmailAuthentication = {$this->values['wgEmailAuthentication']};
363 
364 ## Database settings
365 \$wgDBtype = \"{$this->values['wgDBtype']}\";
366 \$wgDBserver = \"{$this->values['wgDBserver']}\";
367 \$wgDBname = \"{$this->values['wgDBname']}\";
368 \$wgDBuser = \"{$this->values['wgDBuser']}\";
369 \$wgDBpassword = \"{$this->values['wgDBpassword']}\";
370 
371 {$this->dbSettings}
372 
373 ## Shared memory settings
374 \$wgMainCacheType = $cacheType;
375 \$wgMemCachedServers = $mcservers;
376 
377 ## To enable image uploads, make sure the 'images' directory
378 ## is writable, then set this to true:
379 \$wgEnableUploads = {$this->values['wgEnableUploads']};
380 {$magic}\$wgUseImageMagick = true;
381 {$magic}\$wgImageMagickConvertCommand = \"{$this->values['wgImageMagickConvertCommand']}\";
382 
383 # InstantCommons allows wiki to use images from https://commons.wikimedia.org
384 \$wgUseInstantCommons = {$this->values['wgUseInstantCommons']};
385 
386 # Periodically send a pingback to https://www.mediawiki.org/ with basic data
387 # about this MediaWiki instance. The Wikimedia Foundation shares this data
388 # with MediaWiki developers to help guide future development efforts.
389 \$wgPingback = {$this->values['wgPingback']};
390 
391 ## If you use ImageMagick (or any other shell command) on a
392 ## Linux server, this will need to be set to the name of an
393 ## available UTF-8 locale
394 {$locale}\$wgShellLocale = \"{$this->values['wgShellLocale']}\";
395 
396 ## Set \$wgCacheDirectory to a writable directory on the web server
397 ## to make your wiki go slightly faster. The directory should not
398 ## be publicly accessible from the web.
399 #\$wgCacheDirectory = \"\$IP/cache\";
400 
401 # Site language code, should be one of the list in ./languages/data/Names.php
402 \$wgLanguageCode = \"{$this->values['wgLanguageCode']}\";
403 
404 \$wgSecretKey = \"{$this->values['wgSecretKey']}\";
405 
406 # Changing this will log out all existing sessions.
407 \$wgAuthenticationTokenVersion = \"{$this->values['wgAuthenticationTokenVersion']}\";
408 
409 # Site upgrade key. Must be set to a string (default provided) to turn on the
410 # web installer while LocalSettings.php is in place
411 \$wgUpgradeKey = \"{$this->values['wgUpgradeKey']}\";
412 
413 ## For attaching licensing metadata to pages, and displaying an
414 ## appropriate copyright notice / icon. GNU Free Documentation
415 ## License and Creative Commons licenses are supported so far.
416 \$wgRightsPage = \"\"; # Set to the title of a wiki page that describes your license/copyright
417 \$wgRightsUrl = \"{$this->values['wgRightsUrl']}\";
418 \$wgRightsText = \"{$this->values['wgRightsText']}\";
419 \$wgRightsIcon = \"{$this->values['wgRightsIcon']}\";
420 
421 # Path to the GNU diff3 utility. Used for conflict resolution.
422 \$wgDiff3 = \"{$this->values['wgDiff3']}\";
423 
424 {$groupRights}{$noFollow}## Default skin: you can change the default skin. Use the internal symbolic
425 ## names, ie 'vector', 'monobook':
426 \$wgDefaultSkin = \"{$this->values['wgDefaultSkin']}\";
427 ";
428  }
429 }
LocalSettingsGenerator\generateExtEnableLine
generateExtEnableLine( $dir, $name)
Generate the appropriate line to enable the given extension or skin.
Definition: LocalSettingsGenerator.php:184
LocalSettingsGenerator\$dbSettings
$dbSettings
Definition: LocalSettingsGenerator.php:36
LocalSettingsGenerator\writeFile
writeFile( $fileName)
Write the generated LocalSettings to a file.
Definition: LocalSettingsGenerator.php:209
MW_VERSION
const MW_VERSION
The running version of MediaWiki.
Definition: Defines.php:39
IP
Pre-librarized class name for IPUtils.
Definition: IP.php:80
wfBoolToStr
wfBoolToStr( $value)
Convenience function converts boolean values into "true" or "false" (string) values.
Definition: GlobalFunctions.php:2579
LocalSettingsGenerator\buildMemcachedServerList
buildMemcachedServerList()
Definition: LocalSettingsGenerator.php:216
LocalSettingsGenerator\$installer
Installer $installer
Definition: LocalSettingsGenerator.php:42
LocalSettingsGenerator\$IP
$IP
Definition: LocalSettingsGenerator.php:37
LocalSettingsGenerator\__construct
__construct(Installer $installer)
Definition: LocalSettingsGenerator.php:47
LocalSettingsGenerator\getText
getText()
Return the full text of the generated LocalSettings.php file, including the extensions and skins.
Definition: LocalSettingsGenerator.php:140
Installer\getVar
getVar( $name, $default=null)
Get an MW configuration variable, or internal installer configuration variable.
Definition: Installer.php:547
Installer\getDBInstaller
getDBInstaller( $type=false)
Get an instance of DatabaseInstaller for the specified DB type.
Definition: Installer.php:578
LocalSettingsGenerator\$extensions
$extensions
Definition: LocalSettingsGenerator.php:32
LocalSettingsGenerator
Class for generating LocalSettings.php file.
Definition: LocalSettingsGenerator.php:30
LocalSettingsGenerator\$groupPermissions
$groupPermissions
Definition: LocalSettingsGenerator.php:35
LocalSettingsGenerator\getDefaultText
getDefaultText()
Definition: LocalSettingsGenerator.php:237
LocalSettingsGenerator\$values
$values
Definition: LocalSettingsGenerator.php:34
LocalSettingsGenerator\escapePhpString
static escapePhpString( $string)
Returns the escaped version of a string of php code.
Definition: LocalSettingsGenerator.php:116
Installer
Base installer class.
Definition: Installer.php:52
LocalSettingsGenerator\setGroupRights
setGroupRights( $group, $rightsArr)
For $wgGroupPermissions, set a given ['group']['permission'] value.
Definition: LocalSettingsGenerator.php:105
LocalSettingsGenerator\$skins
$skins
Definition: LocalSettingsGenerator.php:33