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',
60  'wgLanguageCode', 'wgLocaltimezone', 'wgEnableEmail', 'wgEnableUserEmail',
61  'wgDiff3', '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 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  $metaNamespace = '';
246  if ( $this->values['wgMetaNamespace'] !== $this->values['wgSitename'] ) {
247  $metaNamespace = "\$wgMetaNamespace = \"{$this->values['wgMetaNamespace']}\";\n";
248  }
249 
250  $groupRights = '';
251  $noFollow = '';
252  if ( $this->groupPermissions ) {
253  $groupRights .= "# The following permissions were set based on your choice in the installer\n";
254  foreach ( $this->groupPermissions as $group => $rightArr ) {
255  $group = self::escapePhpString( $group );
256  foreach ( $rightArr as $right => $perm ) {
257  $right = self::escapePhpString( $right );
258  $groupRights .= "\$wgGroupPermissions['$group']['$right'] = " .
259  wfBoolToStr( $perm ) . ";\n";
260  }
261  }
262  $groupRights .= "\n";
263 
264  if ( ( isset( $this->groupPermissions['*']['edit'] ) &&
265  $this->groupPermissions['*']['edit'] === false )
266  && ( isset( $this->groupPermissions['*']['createaccount'] ) &&
267  $this->groupPermissions['*']['createaccount'] === false )
268  && ( isset( $this->groupPermissions['*']['read'] ) &&
269  $this->groupPermissions['*']['read'] !== false )
270  ) {
271  $noFollow = "# Set \$wgNoFollowLinks to true if you open up your wiki to editing by\n"
272  . "# the general public and wish to apply nofollow to external links as a\n"
273  . "# deterrent to spammers. Nofollow is not a comprehensive anti-spam solution\n"
274  . "# and open wikis will generally require other anti-spam measures; for more\n"
275  . "# information, see https://www.mediawiki.org/wiki/Manual:Combating_spam\n"
276  . "\$wgNoFollowLinks = false;\n\n";
277  }
278  }
279 
280  $serverSetting = "";
281  if ( array_key_exists( 'wgServer', $this->values ) && $this->values['wgServer'] !== null ) {
282  $serverSetting = "\n## The protocol and server name to use in fully-qualified URLs\n";
283  $serverSetting .= "\$wgServer = \"{$this->values['wgServer']}\";";
284  }
285 
286  switch ( $this->values['_MainCacheType'] ) {
287  case 'anything':
288  case 'db':
289  case 'memcached':
290  case 'accel':
291  $cacheType = 'CACHE_' . strtoupper( $this->values['_MainCacheType'] );
292  break;
293  case 'none':
294  default:
295  $cacheType = 'CACHE_NONE';
296  }
297 
298  $mcservers = $this->buildMemcachedServerList();
299  if ( file_exists( dirname( __DIR__ ) . '/PlatformSettings.php' ) ) {
300  $platformSettings = "\n## Include platform/distribution defaults";
301  $platformSettings .= "\nrequire_once \"\$IP/includes/PlatformSettings.php\";";
302  } else {
303  $platformSettings = '';
304  }
305 
306  $version = MW_VERSION;
307  return "<?php
308 # This file was automatically generated by the MediaWiki {$version}
309 # installer. If you make manual changes, please keep track in case you
310 # need to recreate them later.
311 #
312 # See includes/DefaultSettings.php for all configurable settings
313 # and their default values, but don't forget to make changes in _this_
314 # file, not there.
315 #
316 # Further documentation for configuration settings may be found at:
317 # https://www.mediawiki.org/wiki/Manual:Configuration_settings
318 
319 # Protect against web entry
320 if ( !defined( 'MEDIAWIKI' ) ) {
321  exit;
322 }
323 {$platformSettings}
324 
325 ## Uncomment this to disable output compression
326 # \$wgDisableOutputCompression = true;
327 
328 \$wgSitename = \"{$this->values['wgSitename']}\";
329 {$metaNamespace}
330 ## The URL base path to the directory containing the wiki;
331 ## defaults for all runtime URL paths are based off of this.
332 ## For more information on customizing the URLs
333 ## (like /w/index.php/Page_title to /wiki/Page_title) please see:
334 ## https://www.mediawiki.org/wiki/Manual:Short_URL
335 \$wgScriptPath = \"{$this->values['wgScriptPath']}\";
336 ${serverSetting}
337 
338 ## The URL path to static resources (images, scripts, etc.)
339 \$wgResourceBasePath = \$wgScriptPath;
340 
341 ## The URL paths to the logo. Make sure you change this from the default,
342 ## or else you'll overwrite your logo when you upgrade!
343 \$wgLogos = [ '1x' => \"{$this->values['_Logo']}\" ];
344 
345 ## UPO means: this is also a user preference option
346 
347 \$wgEnableEmail = {$this->values['wgEnableEmail']};
348 \$wgEnableUserEmail = {$this->values['wgEnableUserEmail']}; # UPO
349 
350 \$wgEmergencyContact = \"{$this->values['wgEmergencyContact']}\";
351 \$wgPasswordSender = \"{$this->values['wgPasswordSender']}\";
352 
353 \$wgEnotifUserTalk = {$this->values['wgEnotifUserTalk']}; # UPO
354 \$wgEnotifWatchlist = {$this->values['wgEnotifWatchlist']}; # UPO
355 \$wgEmailAuthentication = {$this->values['wgEmailAuthentication']};
356 
357 ## Database settings
358 \$wgDBtype = \"{$this->values['wgDBtype']}\";
359 \$wgDBserver = \"{$this->values['wgDBserver']}\";
360 \$wgDBname = \"{$this->values['wgDBname']}\";
361 \$wgDBuser = \"{$this->values['wgDBuser']}\";
362 \$wgDBpassword = \"{$this->values['wgDBpassword']}\";
363 
364 {$this->dbSettings}
365 
366 # Shared database table
367 # This has no effect unless \$wgSharedDB is also set.
368 \$wgSharedTables[] = \"actor\";
369 
370 ## Shared memory settings
371 \$wgMainCacheType = $cacheType;
372 \$wgMemCachedServers = $mcservers;
373 
374 ## To enable image uploads, make sure the 'images' directory
375 ## is writable, then set this to true:
376 \$wgEnableUploads = {$this->values['wgEnableUploads']};
377 {$magic}\$wgUseImageMagick = true;
378 {$magic}\$wgImageMagickConvertCommand = \"{$this->values['wgImageMagickConvertCommand']}\";
379 
380 # InstantCommons allows wiki to use images from https://commons.wikimedia.org
381 \$wgUseInstantCommons = {$this->values['wgUseInstantCommons']};
382 
383 # Periodically send a pingback to https://www.mediawiki.org/ with basic data
384 # about this MediaWiki instance. The Wikimedia Foundation shares this data
385 # with MediaWiki developers to help guide future development efforts.
386 \$wgPingback = {$this->values['wgPingback']};
387 
388 # Site language code, should be one of the list in ./languages/data/Names.php
389 \$wgLanguageCode = \"{$this->values['wgLanguageCode']}\";
390 
391 # Time zone
392 \$wgLocaltimezone = \"{$this->values['wgLocaltimezone']}\";
393 
394 ## Set \$wgCacheDirectory to a writable directory on the web server
395 ## to make your wiki go slightly faster. The directory should not
396 ## be publicly accessible from the web.
397 #\$wgCacheDirectory = \"\$IP/cache\";
398 
399 \$wgSecretKey = \"{$this->values['wgSecretKey']}\";
400 
401 # Changing this will log out all existing sessions.
402 \$wgAuthenticationTokenVersion = \"{$this->values['wgAuthenticationTokenVersion']}\";
403 
404 # Site upgrade key. Must be set to a string (default provided) to turn on the
405 # web installer while LocalSettings.php is in place
406 \$wgUpgradeKey = \"{$this->values['wgUpgradeKey']}\";
407 
408 ## For attaching licensing metadata to pages, and displaying an
409 ## appropriate copyright notice / icon. GNU Free Documentation
410 ## License and Creative Commons licenses are supported so far.
411 \$wgRightsPage = \"\"; # Set to the title of a wiki page that describes your license/copyright
412 \$wgRightsUrl = \"{$this->values['wgRightsUrl']}\";
413 \$wgRightsText = \"{$this->values['wgRightsText']}\";
414 \$wgRightsIcon = \"{$this->values['wgRightsIcon']}\";
415 
416 # Path to the GNU diff3 utility. Used for conflict resolution.
417 \$wgDiff3 = \"{$this->values['wgDiff3']}\";
418 
419 {$groupRights}{$noFollow}## Default skin: you can change the default skin. Use the internal symbolic
420 ## names, e.g. 'vector' or 'monobook':
421 \$wgDefaultSkin = \"{$this->values['wgDefaultSkin']}\";
422 ";
423  }
424 }
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:36
wfBoolToStr
wfBoolToStr( $value)
Convenience function converts boolean values into "true" or "false" (string) values.
Definition: GlobalFunctions.php:2325
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:578
Installer\getDBInstaller
getDBInstaller( $type=false)
Get an instance of DatabaseInstaller for the specified DB type.
Definition: Installer.php:609
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:53
LocalSettingsGenerator\setGroupRights
setGroupRights( $group, $rightsArr)
For $wgGroupPermissions, set a given ['group']['permission'] value.
Definition: LocalSettingsGenerator.php:105
LocalSettingsGenerator\$skins
$skins
Definition: LocalSettingsGenerator.php:33