Fix for r68645: <doclink> was removed in refactor
[lhc/web/wiklou.git] / includes / installer / Installer.php
1 <?php
2
3 /**
4 * Base installer class
5 * Handles everything that is independent of user interface
6 */
7 abstract class Installer {
8 var $settings, $output;
9
10 /**
11 * MediaWiki configuration globals that will eventually be passed through
12 * to LocalSettings.php. The names only are given here, the defaults
13 * typically come from DefaultSettings.php.
14 * @protected
15 */
16 var $defaultVarNames = array(
17 'wgSitename',
18 'wgPasswordSender',
19 'wgLanguageCode',
20 'wgRightsIcon',
21 'wgRightsText',
22 'wgRightsUrl',
23 'wgMainCacheType',
24 'wgEnableEmail',
25 'wgEnableUserEmail',
26 'wgEnotifUserTalk',
27 'wgEnotifWatchlist',
28 'wgEmailAuthentication',
29 'wgDBtype',
30 'wgDiff3',
31 'wgImageMagickConvertCommand',
32 'IP',
33 'wgScriptPath',
34 'wgScriptExtension',
35 'wgMetaNamespace',
36 // 'wgDeletedDirectory',
37 'wgEnableUploads',
38 'wgLogo',
39 'wgShellLocale',
40 'wgSecretKey',
41 );
42
43 /**
44 * Variables that are stored alongside globals, and are used for any
45 * configuration of the installation process aside from the MediaWiki
46 * configuration. Map of names to defaults.
47 * @protected
48 */
49 var $internalDefaults = array(
50 '_UserLang' => 'en',
51 '_Environment' => false,
52 '_CompiledDBs' => array(),
53 '_SafeMode' => false,
54 '_RaiseMemory' => false,
55 '_UpgradeDone' => false,
56 '_Caches' => array(),
57 '_InstallUser' => 'root',
58 '_InstallPassword' => '',
59 '_SameAccount' => true,
60 '_CreateDBAccount' => false,
61 '_NamespaceType' => 'site-name',
62 '_AdminName' => null, // will be set later, when the user selects language
63 '_AdminPassword' => '',
64 '_AdminPassword2' => '',
65 '_AdminEmail' => '',
66 '_Subscribe' => false,
67 '_SkipOptional' => 'continue',
68 '_RightsProfile' => 'wiki',
69 '_LicenseCode' => 'none',
70 '_CCDone' => false,
71 '_Extensions' => array(),
72 '_MemCachedServers' => '',
73 );
74
75 /**
76 * Known database types. These correspond to the class names <type>Installer,
77 * and are also MediaWiki database types valid for $wgDBtype.
78 *
79 * To add a new type, create a <type>Installer class and a Database<type>
80 * class, and add a config-type-<type> message to MessagesEn.php.
81 * @private
82 */
83 var $dbTypes = array(
84 'mysql',
85 'postgres',
86 'sqlite',
87 'oracle'
88 );
89
90 /**
91 * Minimum memory size in MB
92 */
93 private $minMemorySize = 50;
94
95 /**
96 * Cached DB installer instances, access using getDBInstaller()
97 * @private
98 */
99 var $dbInstallers = array();
100
101 /**
102 * A list of environment check methods called by doEnvironmentChecks().
103 * These may output warnings using showMessage(), and/or abort the
104 * installation process by returning false.
105 * @protected
106 */
107 var $envChecks = array(
108 'envLatestVersion',
109 'envCheckDB',
110 'envCheckRegisterGlobals',
111 'envCheckMagicQuotes',
112 'envCheckMagicSybase',
113 'envCheckMbstring',
114 'envCheckZE1',
115 'envCheckSafeMode',
116 'envCheckXML',
117 'envCheckPCRE',
118 'envCheckMemory',
119 'envCheckCache',
120 'envCheckDiff3',
121 'envCheckGraphics',
122 'envCheckPath',
123 'envCheckWriteableDir',
124 'envCheckExtension',
125 'envCheckShellLocale',
126 'envCheckUploadsDirectory',
127 );
128
129 var $installSteps = array(
130 'database',
131 'tables',
132 'interwiki',
133 'secretkey',
134 'sysop',
135 'localsettings',
136 );
137
138 /**
139 * Known object cache types and the functions used to test for their existence
140 * @protected
141 */
142 var $objectCaches = array(
143 'xcache' => 'xcache_get',
144 'apc' => 'apc_fetch',
145 'eaccel' => 'eaccelerator_get',
146 'wincache' => 'wincache_ucache_get'
147 );
148
149 /**
150 * User rights profiles
151 */
152 var $rightsProfiles = array(
153 'wiki' => array(),
154 'no-anon' => array(
155 '*' => array( 'edit' => false )
156 ),
157 'fishbowl' => array(
158 '*' => array(
159 'createaccount' => false,
160 'edit' => false,
161 ),
162 ),
163 'private' => array(
164 '*' => array(
165 'createaccount' => false,
166 'edit' => false,
167 'read' => false,
168 ),
169 ),
170 );
171
172 /**
173 * License types
174 */
175 var $licenses = array(
176 'none' => array(
177 'url' => '',
178 'icon' => '',
179 'text' => ''
180 ),
181 'cc-by-sa' => array(
182 'url' => 'http://creativecommons.org/licenses/by-sa/3.0/',
183 'icon' => '{$wgStylePath}/common/images/cc-by-sa.png',
184 ),
185 'cc-by-nc-sa' => array(
186 'url' => 'http://creativecommons.org/licenses/by-nc-sa/3.0/',
187 'icon' => '{$wgStylePath}/common/images/cc-by-nc-sa.png',
188 ),
189 'pd' => array(
190 'url' => 'http://creativecommons.org/licenses/publicdomain/',
191 'icon' => '{$wgStylePath}/common/images/public-domain.png',
192 ),
193 'gfdl-old' => array(
194 'url' => 'http://www.gnu.org/licenses/old-licenses/fdl-1.2.html',
195 'icon' => '{$wgStylePath}/common/images/gnu-fdl.png',
196 ),
197 'gfdl-current' => array(
198 'url' => 'http://www.gnu.org/copyleft/fdl.html',
199 'icon' => '{$wgStylePath}/common/images/gnu-fdl.png',
200 ),
201 'cc-choose' => array(
202 // details will be filled in by the selector
203 'url' => '',
204 'icon' => '',
205 'text' => '',
206 ),
207 );
208 /**
209 * Cached Title and ParserOptions used by parse()
210 * @private
211 */
212 var $parserTitle, $parserOptions;
213
214 /**
215 * Constructor, always call this from child classes
216 */
217 function __construct() {
218 // Disable the i18n cache and LoadBalancer
219 Language::getLocalisationCache()->disableBackend();
220 LBFactory::disableBackend();
221
222 // Load the installer's i18n file
223 global $wgExtensionMessagesFiles;
224 $wgExtensionMessagesFiles['MediawikiInstaller'] =
225 './includes/installer/Installer.i18n.php';
226
227 // Set our custom <doclink> hook
228 global $wgHooks;
229 $wgHooks['ParserFirstCallInit'][] = array( $this, 'registerDocLink' );
230
231 $this->settings = $this->internalDefaults;
232 foreach ( $this->defaultVarNames as $var ) {
233 $this->settings[$var] = $GLOBALS[$var];
234 }
235 foreach ( $this->dbTypes as $type ) {
236 $installer = $this->getDBInstaller( $type );
237 if ( !$installer->isCompiled() ) {
238 continue;
239 }
240 $defaults = $installer->getGlobalDefaults();
241 foreach ( $installer->getGlobalNames() as $var ) {
242 if ( isset( $defaults[$var] ) ) {
243 $this->settings[$var] = $defaults[$var];
244 } else {
245 $this->settings[$var] = $GLOBALS[$var];
246 }
247 }
248 }
249
250 $this->parserTitle = Title::newFromText( 'Installer' );
251 $this->parserOptions = new ParserOptions;
252 $this->parserOptions->setEditSection( false );
253 }
254
255 /**
256 * UI interface for displaying a short message
257 * The parameters are like parameters to wfMsg().
258 * The messages will be in wikitext format, which will be converted to an
259 * output format such as HTML or text before being sent to the user.
260 */
261 abstract function showMessage( $msg /*, ... */ );
262
263 abstract function showStatusError( $status );
264
265 /**
266 * Get a list of known DB types
267 */
268 function getDBTypes() {
269 return $this->dbTypes;
270 }
271
272 /**
273 * Get an instance of InstallerDBType for the specified DB type
274 * @param $type Mixed: DB installer for which is needed, false to use default
275 */
276 function getDBInstaller( $type = false ) {
277 if ( !$type ) {
278 $type = $this->getVar( 'wgDBtype' );
279 }
280 if ( !isset( $this->dbInstallers[$type] ) ) {
281 $class = ucfirst( $type ). 'Installer';
282 $this->dbInstallers[$type] = new $class( $this );
283 }
284 return $this->dbInstallers[$type];
285 }
286
287 /**
288 * Do initial checks of the PHP environment. Set variables according to
289 * the observed environment.
290 *
291 * It's possible that this may be called under the CLI SAPI, not the SAPI
292 * that the wiki will primarily run under. In that case, the subclass should
293 * initialise variables such as wgScriptPath, before calling this function.
294 *
295 * Under the web subclass, it can already be assumed that PHP 5+ is in use
296 * and that sessions are working.
297 */
298 function doEnvironmentChecks() {
299 $this->showMessage( 'config-env-php', phpversion() );
300
301 $good = true;
302 foreach ( $this->envChecks as $check ) {
303 $status = $this->$check();
304 if ( $status === false ) {
305 $good = false;
306 }
307 }
308 $this->setVar( '_Environment', $good );
309 if ( $good ) {
310 $this->showMessage( 'config-env-good' );
311 } else {
312 $this->showMessage( 'config-env-bad' );
313 }
314 return $good;
315 }
316
317 /**
318 * Get an MW configuration variable, or internal installer configuration variable.
319 * The defaults come from $GLOBALS (ultimately DefaultSettings.php).
320 * Installer variables are typically prefixed by an underscore.
321 */
322 function getVar( $name, $default = null ) {
323 if ( !isset( $this->settings[$name] ) ) {
324 return $default;
325 } else {
326 return $this->settings[$name];
327 }
328 }
329
330 /**
331 * Set a MW configuration variable, or internal installer configuration variable.
332 */
333 function setVar( $name, $value ) {
334 $this->settings[$name] = $value;
335 }
336
337 /**
338 * Exports all wg* variables stored by the installer into global scope
339 */
340 function exportVars() {
341 foreach ( $this->settings as $name => $value ) {
342 if ( substr( $name, 0, 2 ) == 'wg' ) {
343 $GLOBALS[$name] = $value;
344 }
345 }
346 }
347
348 /**
349 * Get a fake password for sending back to the user in HTML.
350 * This is a security mechanism to avoid compromise of the password in the
351 * event of session ID compromise.
352 */
353 function getFakePassword( $realPassword ) {
354 return str_repeat( '*', strlen( $realPassword ) );
355 }
356
357 /**
358 * Set a variable which stores a password, except if the new value is a
359 * fake password in which case leave it as it is.
360 */
361 function setPassword( $name, $value ) {
362 if ( !preg_match( '/^\*+$/', $value ) ) {
363 $this->setVar( $name, $value );
364 }
365 }
366
367 /** Check if we're installing the latest version */
368 function envLatestVersion() {
369 global $wgVersion;
370 $latestInfoUrl = 'http://www.mediawiki.org/w/api.php?action=mwreleases&format=json';
371 $latestInfo = Http::get( $latestInfoUrl );
372 if( !$latestInfo ) {
373 $this->showMessage( 'config-env-latest-can-not-check', $latestInfoUrl );
374 return;
375 }
376 $latestInfo = FormatJson::decode($latestInfo);
377 if ($latestInfo === false || !isset( $latestInfo->mwreleases ) ) {
378 # For when the request is successful but there's e.g. some silly man in
379 # the middle firewall blocking us, e.g. one of those annoying airport ones
380 $this->showMessage( 'config-env-latest-data-invalid', $latestInfoUrl );
381 return;
382 }
383 foreach( $latestInfo->mwreleases as $rel ) {
384 if( isset( $rel->current ) )
385 $currentVersion = $rel->version;
386 }
387 if( version_compare( $wgVersion, $currentVersion, '<' ) ) {
388 $this->showMessage( 'config-env-latest-old' );
389 $this->showHelpBox( 'config-env-latest-help', $wgVersion, $currentVersion );
390 } elseif( version_compare( $wgVersion, $currentVersion, '>' ) ) {
391 $this->showMessage( 'config-env-latest-new' );
392 }
393 $this->showMessage( 'config-env-latest-ok' );
394 }
395
396 /** Environment check for DB types */
397 function envCheckDB() {
398 $compiledDBs = array();
399 $goodNames = array();
400 $allNames = array();
401 foreach ( $this->dbTypes as $name ) {
402 $db = $this->getDBInstaller( $name );
403 $readableName = wfMsg( 'config-type-' . $name );
404 if ( $db->isCompiled() ) {
405 $compiledDBs[] = $name;
406 $goodNames[] = $readableName;
407 }
408 $allNames[] = $readableName;
409 }
410 $this->setVar( '_CompiledDBs', $compiledDBs );
411
412 global $wgLang;
413 if ( !$compiledDBs ) {
414 $this->showMessage( 'config-no-db' );
415 $this->showHelpBox( 'config-no-db-help', $wgLang->commaList( $allNames ) );
416 return false;
417 }
418 $this->showMessage( 'config-have-db', $wgLang->commaList( $goodNames ) );
419 }
420
421 /** Environment check for register_globals */
422 function envCheckRegisterGlobals() {
423 if( wfIniGetBool( "magic_quotes_runtime" ) ) {
424 $this->showMessage( 'config-register-globals' );
425 }
426 }
427
428 /** Environment check for magic_quotes_runtime */
429 function envCheckMagicQuotes() {
430 if( wfIniGetBool( "magic_quotes_runtime" ) ) {
431 $this->showMessage( 'config-magic-quotes-runtime' );
432 return false;
433 }
434 }
435
436 /** Environment check for magic_quotes_sybase */
437 function envCheckMagicSybase() {
438 if ( wfIniGetBool( 'magic_quotes_sybase' ) ) {
439 $this->showMessage( 'config-magic-quotes-sybase' );
440 return false;
441 }
442 }
443
444 /* Environment check for mbstring.func_overload */
445 function envCheckMbstring() {
446 if ( wfIniGetBool( 'mbstring.func_overload' ) ) {
447 $this->showMessage( 'config-mbstring' );
448 return false;
449 }
450 }
451
452 /** Environment check for zend.ze1_compatibility_mode */
453 function envCheckZE1() {
454 if ( wfIniGetBool( 'zend.ze1_compatibility_mode' ) ) {
455 $this->showMessage( 'config-ze1' );
456 return false;
457 }
458 }
459
460 /** Environment check for safe_mode */
461 function envCheckSafeMode() {
462 if ( wfIniGetBool( 'safe_mode' ) ) {
463 $this->setVar( '_SafeMode', true );
464 $this->showMessage( 'config-safe-mode' );
465 }
466 }
467
468 /** Environment check for the XML module */
469 function envCheckXML() {
470 if ( !function_exists( "utf8_encode" ) ) {
471 $this->showMessage( 'config-xml-bad' );
472 return false;
473 }
474 $this->showMessage( 'config-xml-good' );
475 }
476
477 /** Environment check for the PCRE module */
478 function envCheckPCRE() {
479 if ( !function_exists( 'preg_match' ) ) {
480 $this->showMessage( 'config-pcre' );
481 return false;
482 }
483 }
484
485 /** Environment check for available memory */
486 function envCheckMemory() {
487 $limit = ini_get( 'memory_limit' );
488 if ( !$limit || $limit == -1 ) {
489 $this->showMessage( 'config-memory-none' );
490 return true;
491 }
492 $n = intval( $limit );
493 if( preg_match( '/^([0-9]+)[Mm]$/', trim( $limit ), $m ) ) {
494 $n = intval( $m[1] * (1024*1024) );
495 }
496 if( $n < $this->minMemorySize*1024*1024 ) {
497 $newLimit = "{$this->minMemorySize}M";
498 if( false === ini_set( "memory_limit", $newLimit ) ) {
499 $this->showMessage( 'config-memory-bad', $limit );
500 } else {
501 $this->showMessage( 'config-memory-raised', $limit, $newLimit );
502 $this->setVar( '_RaiseMemory', true );
503 }
504 } else {
505 $this->showMessage( 'config-memory-ok', $limit );
506 }
507 }
508
509 /** Environment check for compiled object cache types */
510 function envCheckCache() {
511 $caches = array();
512 foreach ( $this->objectCaches as $name => $function ) {
513 if ( function_exists( $function ) ) {
514 $caches[$name] = true;
515 $this->showMessage( 'config-' . $name );
516 }
517 }
518 if ( !$caches ) {
519 $this->showMessage( 'config-no-cache' );
520 }
521 $this->setVar( '_Caches', $caches );
522 }
523
524 /** Search for GNU diff3 */
525 function envCheckDiff3() {
526 $paths = array_merge(
527 array(
528 "/usr/bin",
529 "/usr/local/bin",
530 "/opt/csw/bin",
531 "/usr/gnu/bin",
532 "/usr/sfw/bin" ),
533 explode( PATH_SEPARATOR, getenv( "PATH" ) ) );
534 $names = array( "gdiff3", "diff3", "diff3.exe" );
535
536 $versionInfo = array( '$1 --version 2>&1', 'diff3 (GNU diffutils)' );
537 $haveDiff3 = false;
538 foreach ( $paths as $path ) {
539 $exe = $this->locateExecutable( $path, $names, $versionInfo );
540 if ($exe !== false) {
541 $this->setVar( 'wgDiff3', $exe );
542 $haveDiff3 = true;
543 break;
544 }
545 }
546 if ( $haveDiff3 ) {
547 $this->showMessage( 'config-diff3-good', $exe );
548 } else {
549 $this->setVar( 'wgDiff3', false );
550 $this->showMessage( 'config-diff3-bad' );
551 }
552 }
553
554 /**
555 * Search a path for any of the given executable names. Returns the
556 * executable name if found. Also checks the version string returned
557 * by each executable
558 *
559 * @param $path String: path to search
560 * @param $names Array of executable names
561 * @param $versionInfo Boolean false or array with two members:
562 * 0 => Command to run for version check, with $1 for the path
563 * 1 => String to compare the output with
564 *
565 * If $versionInfo is not false, only executables with a version
566 * matching $versionInfo[1] will be returned.
567 */
568 function locateExecutable( $path, $names, $versionInfo = false ) {
569 if (!is_array($names))
570 $names = array($names);
571
572 foreach ($names as $name) {
573 $command = "$path/$name";
574 if ( @file_exists( $command ) ) {
575 if ( !$versionInfo )
576 return $command;
577
578 $file = str_replace( '$1', $command, $versionInfo[0] );
579 if ( strstr( wfShellExec( $file ), $versionInfo[1]) !== false )
580 return $command;
581 }
582 }
583 return false;
584 }
585
586 /** Environment check for ImageMagick and GD */
587 function envCheckGraphics() {
588 $imcheck = array( "/usr/bin", "/opt/csw/bin", "/usr/local/bin", "/sw/bin", "/opt/local/bin" );
589 foreach( $imcheck as $dir ) {
590 $im = "$dir/convert";
591 if( @file_exists( $im ) ) {
592 $this->showMessage( 'config-imagemagick', $im );
593 $this->setVar( 'wgImageMagickConvertCommand', $im );
594 return true;
595 }
596 }
597 if ( function_exists( 'imagejpeg' ) ) {
598 $this->showMessage( 'config-gd' );
599 return true;
600 }
601 $this->showMessage( 'no-scaling' );
602 }
603
604 /** Environment check for setting $IP and $wgScriptPath */
605 function envCheckPath() {
606 $IP = dirname( dirname( dirname( __FILE__ ) ) );
607 $this->setVar( 'IP', $IP );
608 $this->showMessage( 'config-dir', $IP );
609
610 // PHP_SELF isn't available sometimes, such as when PHP is CGI but
611 // cgi.fix_pathinfo is disabled. In that case, fall back to SCRIPT_NAME
612 // to get the path to the current script... hopefully it's reliable. SIGH
613 if ( !empty( $_SERVER['PHP_SELF'] ) ) {
614 $path = $_SERVER['PHP_SELF'];
615 } elseif ( !empty( $_SERVER['SCRIPT_NAME'] ) ) {
616 $path = $_SERVER['SCRIPT_NAME'];
617 } elseif ( $this->getVar( 'wgScriptPath' ) ) {
618 // Some kind soul has set it for us already (e.g. debconf)
619 return true;
620 } else {
621 $this->showMessage( 'config-no-uri' );
622 return false;
623 }
624 $uri = preg_replace( '{^(.*)/config.*$}', '$1', $path );
625 $this->setVar( 'wgScriptPath', $uri );
626 $this->showMessage( 'config-uri', $uri );
627 }
628
629 /** Environment check for writable config/ directory */
630 function envCheckWriteableDir() {
631 $ipDir = $this->getVar( 'IP' );
632 $configDir = $ipDir . '/config';
633 if( !is_writeable( $configDir ) ) {
634 $webserverGroup = self::maybeGetWebserverPrimaryGroup();
635 if ( $webserverGroup !== null ) {
636 $this->showMessage( 'config-dir-not-writable-group', $ipDir, $webserverGroup );
637 } else {
638 $this->showMessage( 'config-dir-not-writable-nogroup', $ipDir, $webserverGroup );
639 }
640 return false;
641 }
642 }
643
644 /** Environment check for setting the preferred PHP file extension */
645 function envCheckExtension() {
646 // FIXME: detect this properly
647 if ( defined( 'MW_INSTALL_PHP5_EXT' ) ) {
648 $ext = 'php5';
649 } else {
650 $ext = 'php';
651 }
652 $this->setVar( 'wgScriptExtension', ".$ext" );
653 $this->showMessage( 'config-file-extension', $ext );
654 }
655
656 function envCheckShellLocale() {
657 # Give up now if we're in safe mode or open_basedir
658 # It's theoretically possible but tricky to work with
659 if ( wfIniGetBool( "safe_mode" ) || ini_get( 'open_basedir' ) || !function_exists( 'exec' ) ) {
660 return true;
661 }
662
663 $os = php_uname( 's' );
664 $supported = array( 'Linux', 'SunOS', 'HP-UX' ); # Tested these
665 if ( !in_array( $os, $supported ) ) {
666 return true;
667 }
668
669 # Get a list of available locales
670 $lines = $ret = false;
671 exec( '/usr/bin/locale -a', $lines, $ret );
672 if ( $ret ) {
673 return true;
674 }
675
676 $lines = wfArrayMap( 'trim', $lines );
677 $candidatesByLocale = array();
678 $candidatesByLang = array();
679 foreach ( $lines as $line ) {
680 if ( $line === '' ) {
681 continue;
682 }
683 if ( !preg_match( '/^([a-zA-Z]+)(_[a-zA-Z]+|)\.(utf8|UTF-8)(@[a-zA-Z_]*|)$/i', $line, $m ) ) {
684 continue;
685 }
686 list( $all, $lang, $territory, $charset, $modifier ) = $m;
687 $candidatesByLocale[$m[0]] = $m;
688 $candidatesByLang[$lang][] = $m;
689 }
690
691 # Try the current value of LANG
692 if ( isset( $candidatesByLocale[ getenv( 'LANG' ) ] ) ) {
693 $this->setVar( 'wgShellLocale', getenv( 'LANG' ) );
694 $this->showMessage( 'config-shell-locale', getenv( 'LANG' ) );
695 return true;
696 }
697
698 # Try the most common ones
699 $commonLocales = array( 'en_US.UTF-8', 'en_US.utf8', 'de_DE.UTF-8', 'de_DE.utf8' );
700 foreach ( $commonLocales as $commonLocale ) {
701 if ( isset( $candidatesByLocale[$commonLocale] ) ) {
702 $this->setVar( 'wgShellLocale', $commonLocale );
703 $this->showMessage( 'config-shell-locale', $commonLocale );
704 return true;
705 }
706 }
707
708 # Is there an available locale in the Wiki's language?
709 $wikiLang = $this->getVar( 'wgLanguageCode' );
710 if ( isset( $candidatesByLang[$wikiLang] ) ) {
711 $m = reset( $candidatesByLang[$wikiLang] );
712 $this->setVar( 'wgShellLocale', $m[0] );
713 $this->showMessage( 'config-shell-locale', $m[0] );
714 return true;
715 }
716
717 # Are there any at all?
718 if ( count( $candidatesByLocale ) ) {
719 $m = reset( $candidatesByLocale );
720 $this->setVar( 'wgShellLocale', $m[0] );
721 $this->showMessage( 'config-shell-locale', $m[0] );
722 return true;
723 }
724
725 # Give up
726 return true;
727 }
728
729 function envCheckUploadsDirectory() {
730 global $IP, $wgServer;
731 $dir = $IP . '/images/';
732 $url = $wgServer . $this->getVar( 'wgScriptPath' ) . '/images/';
733 $safe = !$this->dirIsExecutable( $dir, $url );
734 if ( $safe ) {
735 $this->showMessage( 'config-uploads-safe' );
736 } else {
737 $this->showMessage( 'config-uploads-not-safe', $dir );
738 }
739 }
740
741 /**
742 * Checks if scripts located in the given directory can be executed via the given URL
743 */
744 function dirIsExecutable( $dir, $url ) {
745 $scriptTypes = array(
746 'php' => array(
747 "<?php echo 'ex' . 'ec';",
748 "#!/var/env php5\n<?php echo 'ex' . 'ec';",
749 ),
750 );
751 // it would be good to check other popular languages here, but it'll be slow
752
753 wfSuppressWarnings();
754 foreach ( $scriptTypes as $ext => $contents ) {
755 foreach ( $contents as $source ) {
756 $file = 'exectest.' . $ext;
757 if ( !file_put_contents( $dir . $file, $source ) ) {
758 break;
759 }
760 $text = Http::get( $url . $file );
761 unlink( $dir . $file );
762 if ( $text == 'exec' ) {
763 wfRestoreWarnings();
764 return $ext;
765 }
766 }
767 }
768 wfRestoreWarnings();
769 return false;
770 }
771
772 /**
773 * Convert wikitext $text to HTML.
774 *
775 * This is potentially error prone since many parser features require a complete
776 * installed MW database. The solution is to just not use those features when you
777 * write your messages. This appears to work well enough. Basic formatting and
778 * external links work just fine.
779 *
780 * But in case a translator decides to throw in a #ifexist or internal link or
781 * whatever, this function is guarded to catch attempted DB access and to present
782 * some fallback text.
783 *
784 * @param $text String
785 * @param $lineStart Boolean
786 * @return String
787 */
788 function parse( $text, $lineStart = false ) {
789 global $wgParser;
790 try {
791 $out = $wgParser->parse( $text, $this->parserTitle, $this->parserOptions, $lineStart );
792 $html = $out->getText();
793 } catch ( DBAccessError $e ) {
794 $html = '<!--DB access attempted during parse--> ' . htmlspecialchars( $text );
795 if ( !empty( $this->debug ) ) {
796 $html .= "<!--\n" . $e->getTraceAsString() . "\n-->";
797 }
798 }
799 return $html;
800 }
801
802 /**
803 * Register tag hook below
804 */
805 function registerDocLink( &$parser ) {
806 $parser->setHook( 'doclink', array( $this, 'docLink' ) );
807 return true;
808 }
809
810 /**
811 * Extension tag hook for a documentation link
812 */
813 function docLink( $linkText, $attribs, $parser ) {
814 $url = $this->getDocUrl( $attribs['href'] );
815 return '<a href="' . htmlspecialchars( $url ) . '">' .
816 htmlspecialchars( $linkText ) .
817 '</a>';
818 }
819
820 /**
821 * Overridden by WebInstaller to provide lastPage parameters
822 */
823 protected function getDocUrl( $page ) {
824 return "{$_SERVER['PHP_SELF']}?page=" . urlencode( $attribs['href'] );
825 }
826
827 public function findExtensions() {
828 if( $this->getVar( 'IP' ) === null ) {
829 return false;
830 }
831 $exts = array();
832 $dir = $this->getVar( 'IP' ) . '/extensions';
833 $dh = opendir( $dir );
834 while ( ( $file = readdir( $dh ) ) !== false ) {
835 if( file_exists( "$dir/$file/$file.php" ) ) {
836 $exts[] = $file;
837 }
838 }
839 $this->setVar( '_Extensions', $exts );
840 return $exts;
841 }
842
843 public function getInstallSteps() {
844 if( $this->getVar( '_UpgradeDone' ) ) {
845 $this->installSteps = array( 'localsettings' );
846 }
847 if( count( $this->getVar( '_Extensions' ) ) ) {
848 array_unshift( $this->installSteps, 'extensions' );
849 }
850 return $this->installSteps;
851 }
852
853 public function installExtensions() {
854 global $wgHooks, $wgAutoloadClasses;
855 $exts = $this->getVar( '_Extensions' );
856 $path = $this->getVar( 'IP' ) . '/extensions';
857 foreach( $exts as $e ) {
858 require( "$path/$e/$e.php" );
859 }
860 return Status::newGood();
861 }
862
863 public function installDatabase() {
864 $installer = $this->getDBInstaller( $this->getVar( 'wgDBtype' ) );
865 $status = $installer->setupDatabase();
866 return $status;
867 }
868
869 public function installTables() {
870 $installer = $this->getDBInstaller();
871 $status = $installer->createTables();
872 if( $status->isGood() ) {
873 LBFactory::enableBackend();
874 }
875 return $status;
876 }
877
878 public function installInterwiki() {
879 $installer = $this->getDBInstaller();
880 return $installer->populateInterwikiTable();
881 }
882
883 public function installSecretKey() {
884 $file = wfIsWindows() ? null : @fopen( "/dev/urandom", "r" );
885 if ( $file ) {
886 $secretKey = bin2hex( fread( $file, 32 ) );
887 fclose( $file );
888 } else {
889 $secretKey = "";
890 for ( $i=0; $i<8; $i++ ) {
891 $secretKey .= dechex(mt_rand(0, 0x7fffffff));
892 }
893 $this->output->addWarningMsg( 'config-insecure-secretkey' );
894 }
895 $this->setVar( 'wgSecretKey', $secretKey );
896 return Status::newGood();
897 }
898
899 public function installSysop() {
900 $name = $this->getVar( '_AdminName' );
901 $user = User::newFromName( $name );
902 if ( !$user ) {
903 // we should've validated this earlier anyway!
904 return Status::newFatal( 'config-admin-error-user', $name );
905 }
906 if ( $user->idForName() == 0 ) {
907 $user->addToDatabase();
908 try {
909 $user->setPassword( $this->getVar( '_AdminPassword' ) );
910 } catch( PasswordError $pwe ) {
911 return Status::newFatal( 'config-admin-error-password', $name, $pwe->getMessage() );
912 }
913 $user->addGroup( 'sysop' );
914 $user->addGroup( 'bureaucrat' );
915 $user->saveSettings();
916 }
917 return Status::newGood();
918 }
919
920 public function installLocalsettings() {
921 $localSettings = new LocalSettingsGenerator( $this );
922 return $localSettings->writeLocalSettings();
923 }
924
925 /**
926 * On POSIX systems return the primary group of the webserver we're running under.
927 * On other systems just returns null.
928 *
929 * This is used to advice the user that he should chgrp his config/data/images directory as the
930 * webserver user before he can install.
931 *
932 * Public because SqliteInstaller needs it, and doesn't subclass Installer.
933 *
934 * @return String
935 */
936 public static function maybeGetWebserverPrimaryGroup() {
937 if ( ! function_exists('posix_getegid') || ! function_exists('posix_getpwuid') ) {
938 # I don't know this, this isn't UNIX
939 return null;
940 }
941
942 # posix_getegid() *not* getmygid() because we want the group of the webserver,
943 # not whoever owns the current script
944 $gid = posix_getegid();
945 $getpwuid = posix_getpwuid( $gid );
946 $group = $getpwuid["name"];
947
948 return $group;
949 }
950
951 /**
952 * Override the necessary bits of the config to run an installation
953 */
954 public static function overrideConfig() {
955 define( 'MW_NO_SESSION', 1 );
956
957 // Don't access the database
958 $GLOBALS['wgUseDatabaseMessages'] = false;
959 // Debug-friendly
960 $GLOBALS['wgShowExceptionDetails'] = true;
961 // Don't break forms
962 $GLOBALS['wgExternalLinkTarget'] = '_blank';
963
964 // Extended debugging. Maybe disable before release?
965 $GLOBALS['wgShowSQLErrors'] = true;
966 $GLOBALS['wgShowDBErrorBacktrace'] = true;
967 }
968 }