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