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