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