Fix indentation of => added in r77366 to be in step with the rest
[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 upgrading is enabled 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 the 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 installTables( DatabaseInstaller &$installer ) {
341 $status = $installer->createTables();
342
343 if( $status->isOK() ) {
344 LBFactory::enableBackend();
345 }
346
347 return $status;
348 }
349
350 /**
351 * Exports all wg* variables stored by the installer into global scope.
352 */
353 public function exportVars() {
354 foreach ( $this->settings as $name => $value ) {
355 if ( substr( $name, 0, 2 ) == 'wg' ) {
356 $GLOBALS[$name] = $value;
357 }
358 }
359 }
360
361 /**
362 * Check if we're installing the latest version.
363 */
364 protected function envCheckMediaWikiVersion() {
365 global $wgVersion;
366
367 if( !$this->getVar( '_ExternalHTTP' ) ) {
368 $this->showMessage( 'config-env-latest-disabled' );
369 return;
370 }
371
372 $repository = wfGetRepository();
373 $currentVersion = $repository->getLatestCoreVersion();
374
375 if ( $currentVersion === false ) {
376 # For when the request is successful but there's e.g. some silly man in
377 # the middle firewall blocking us, e.g. one of those annoying airport ones
378 $this->showMessage( 'config-env-latest-can-not-check', $repository->getLocation() );
379 return;
380 }
381
382 if( version_compare( $wgVersion, $currentVersion, '<' ) ) {
383 $this->showMessage( 'config-env-latest-old' );
384 // FIXME: this only works for the web installer!
385 $this->showHelpBox( 'config-env-latest-help', $wgVersion, $currentVersion );
386 } elseif( version_compare( $wgVersion, $currentVersion, '>' ) ) {
387 $this->showMessage( 'config-env-latest-new' );
388 } else {
389 $this->showMessage( 'config-env-latest-ok' );
390 }
391 }
392
393 /**
394 * Environment check for DB types.
395 */
396 protected function envCheckDB() {
397 global $wgLang;
398
399 $compiledDBs = array();
400 $goodNames = array();
401 $allNames = array();
402
403 foreach ( self::getDBTypes() as $name ) {
404 $db = $this->getDBInstaller( $name );
405 $readableName = wfMsg( 'config-type-' . $name );
406
407 if ( $db->isCompiled() ) {
408 $compiledDBs[] = $name;
409 $goodNames[] = $readableName;
410 }
411
412 $allNames[] = $readableName;
413 }
414
415 $this->setVar( '_CompiledDBs', $compiledDBs );
416
417 if ( !$compiledDBs ) {
418 $this->showMessage( 'config-no-db' );
419 // FIXME: this only works for the web installer!
420 $this->showHelpBox( 'config-no-db-help', $wgLang->commaList( $allNames ) );
421 return false;
422 }
423
424 $this->showMessage( 'config-have-db', $wgLang->listToText( $goodNames ), count( $goodNames ) );
425
426 // Check for FTS3 full-text search module
427 $sqlite = $this->getDBInstaller( 'sqlite' );
428 if ( $sqlite->isCompiled() ) {
429 $db = new DatabaseSqliteStandalone( ':memory:' );
430 $this->showMessage( $db->getFulltextSearchModule() == 'FTS3'
431 ? 'config-have-fts3'
432 : 'config-no-fts3'
433 );
434 }
435 }
436
437 /**
438 * Environment check for register_globals.
439 */
440 protected function envCheckRegisterGlobals() {
441 if( wfIniGetBool( "magic_quotes_runtime" ) ) {
442 $this->showMessage( 'config-register-globals' );
443 }
444 }
445
446 /**
447 * Some versions of libxml+PHP break < and > encoding horribly
448 */
449 protected function envCheckBrokenXML() {
450 $test = new PhpXmlBugTester();
451 if ( !$test->ok ) {
452 $this->showMessage( 'config-brokenlibxml' );
453 return false;
454 }
455 }
456
457 /**
458 * Test PHP (probably 5.3.1, but it could regress again) to make sure that
459 * reference parameters to __call() are not converted to null
460 */
461 protected function envCheckPHP531() {
462 $test = new PhpRefCallBugTester;
463 $test->execute();
464 if ( !$test->ok ) {
465 $this->showMessage( 'config-using531' );
466 return false;
467 }
468 }
469
470 /**
471 * Environment check for magic_quotes_runtime.
472 */
473 protected function envCheckMagicQuotes() {
474 if( wfIniGetBool( "magic_quotes_runtime" ) ) {
475 $this->showMessage( 'config-magic-quotes-runtime' );
476 return false;
477 }
478 }
479
480 /**
481 * Environment check for magic_quotes_sybase.
482 */
483 protected function envCheckMagicSybase() {
484 if ( wfIniGetBool( 'magic_quotes_sybase' ) ) {
485 $this->showMessage( 'config-magic-quotes-sybase' );
486 return false;
487 }
488 }
489
490 /**
491 * Environment check for mbstring.func_overload.
492 */
493 protected function envCheckMbstring() {
494 if ( wfIniGetBool( 'mbstring.func_overload' ) ) {
495 $this->showMessage( 'config-mbstring' );
496 return false;
497 }
498 }
499
500 /**
501 * Environment check for zend.ze1_compatibility_mode.
502 */
503 protected function envCheckZE1() {
504 if ( wfIniGetBool( 'zend.ze1_compatibility_mode' ) ) {
505 $this->showMessage( 'config-ze1' );
506 return false;
507 }
508 }
509
510 /**
511 * Environment check for safe_mode.
512 */
513 protected function envCheckSafeMode() {
514 if ( wfIniGetBool( 'safe_mode' ) ) {
515 $this->setVar( '_SafeMode', true );
516 $this->showMessage( 'config-safe-mode' );
517 }
518 }
519
520 /**
521 * Environment check for the XML module.
522 */
523 protected function envCheckXML() {
524 if ( !function_exists( "utf8_encode" ) ) {
525 $this->showMessage( 'config-xml-bad' );
526 return false;
527 }
528 $this->showMessage( 'config-xml-good' );
529 }
530
531 /**
532 * Environment check for the PCRE module.
533 */
534 protected function envCheckPCRE() {
535 if ( !function_exists( 'preg_match' ) ) {
536 $this->showMessage( 'config-pcre' );
537 return false;
538 }
539 wfSuppressWarnings();
540 $regexd = preg_replace( '/[\x{0400}-\x{04FF}]/u', '', '-АБВГД-' );
541 wfRestoreWarnings();
542 if ( $regexd != '--' ) {
543 $this->showMessage( 'config-pcre-no-utf8' );
544 return false;
545 }
546 }
547
548 /**
549 * Environment check for available memory.
550 */
551 protected function envCheckMemory() {
552 $limit = ini_get( 'memory_limit' );
553
554 if ( !$limit || $limit == -1 ) {
555 $this->showMessage( 'config-memory-none' );
556 return true;
557 }
558
559 $n = intval( $limit );
560
561 if( preg_match( '/^([0-9]+)[Mm]$/', trim( $limit ), $m ) ) {
562 $n = intval( $m[1] * ( 1024 * 1024 ) );
563 }
564
565 if( $n < $this->minMemorySize * 1024 * 1024 ) {
566 $newLimit = "{$this->minMemorySize}M";
567
568 if( ini_set( "memory_limit", $newLimit ) === false ) {
569 $this->showMessage( 'config-memory-bad', $limit );
570 } else {
571 $this->showMessage( 'config-memory-raised', $limit, $newLimit );
572 $this->setVar( '_RaiseMemory', true );
573 }
574 } else {
575 $this->showMessage( 'config-memory-ok', $limit );
576 }
577 }
578
579 /**
580 * Environment check for compiled object cache types.
581 */
582 protected function envCheckCache() {
583 $caches = array();
584
585 foreach ( $this->objectCaches as $name => $function ) {
586 if ( function_exists( $function ) ) {
587 $caches[$name] = true;
588 $this->showMessage( 'config-' . $name );
589 }
590 }
591
592 if ( !$caches ) {
593 $this->showMessage( 'config-no-cache' );
594 }
595
596 $this->setVar( '_Caches', $caches );
597 }
598
599 /**
600 * Search for GNU diff3.
601 */
602 protected function envCheckDiff3() {
603 $names = array( "gdiff3", "diff3", "diff3.exe" );
604 $versionInfo = array( '$1 --version 2>&1', 'GNU diffutils' );
605
606 $diff3 = $this->locateExecutableInDefaultPaths( $names, $versionInfo );
607
608 if ( $diff3 ) {
609 $this->showMessage( 'config-diff3-good', $diff3 );
610 $this->setVar( 'wgDiff3', $diff3 );
611 } else {
612 $this->setVar( 'wgDiff3', false );
613 $this->showMessage( 'config-diff3-bad' );
614 }
615 }
616
617 /**
618 * Environment check for ImageMagick and GD.
619 */
620 protected function envCheckGraphics() {
621 $names = array( wfIsWindows() ? 'convert.exe' : 'convert' );
622 $convert = $this->locateExecutableInDefaultPaths( $names, array( '$1 -version', 'ImageMagick' ) );
623
624 if ( $convert ) {
625 $this->setVar( 'wgImageMagickConvertCommand', $convert );
626 $this->showMessage( 'config-imagemagick', $convert );
627 return true;
628 } elseif ( function_exists( 'imagejpeg' ) ) {
629 $this->showMessage( 'config-gd' );
630 return true;
631 } else {
632 $this->showMessage( 'no-scaling' );
633 }
634 }
635
636 /**
637 * Environment check for setting $IP and $wgScriptPath.
638 */
639 protected function envCheckPath() {
640 global $IP;
641 $IP = dirname( dirname( dirname( __FILE__ ) ) );
642
643 $this->setVar( 'IP', $IP );
644 $this->showMessage( 'config-dir', $IP );
645
646 // PHP_SELF isn't available sometimes, such as when PHP is CGI but
647 // cgi.fix_pathinfo is disabled. In that case, fall back to SCRIPT_NAME
648 // to get the path to the current script... hopefully it's reliable. SIGH
649 if ( !empty( $_SERVER['PHP_SELF'] ) ) {
650 $path = $_SERVER['PHP_SELF'];
651 } elseif ( !empty( $_SERVER['SCRIPT_NAME'] ) ) {
652 $path = $_SERVER['SCRIPT_NAME'];
653 } elseif ( $this->getVar( 'wgScriptPath' ) ) {
654 // Some kind soul has set it for us already (e.g. debconf)
655 return true;
656 } else {
657 $this->showMessage( 'config-no-uri' );
658 return false;
659 }
660
661 $uri = preg_replace( '{^(.*)/config.*$}', '$1', $path );
662 $this->setVar( 'wgScriptPath', $uri );
663 $this->showMessage( 'config-uri', $uri );
664 }
665
666 /**
667 * Environment check for setting the preferred PHP file extension.
668 */
669 protected function envCheckExtension() {
670 // FIXME: detect this properly
671 if ( defined( 'MW_INSTALL_PHP5_EXT' ) ) {
672 $ext = 'php5';
673 } else {
674 $ext = 'php';
675 }
676
677 $this->setVar( 'wgScriptExtension', ".$ext" );
678 $this->showMessage( 'config-file-extension', $ext );
679 }
680
681 /**
682 * TODO: document
683 */
684 protected function envCheckShellLocale() {
685 $os = php_uname( 's' );
686 $supported = array( 'Linux', 'SunOS', 'HP-UX', 'Darwin' ); # Tested these
687
688 if ( !in_array( $os, $supported ) ) {
689 return true;
690 }
691
692 # Get a list of available locales.
693 $ret = false;
694 $lines = wfShellExec( '/usr/bin/locale -a', $ret );
695
696 if ( $ret ) {
697 return true;
698 }
699
700 $lines = wfArrayMap( 'trim', explode( "\n", $lines ) );
701 $candidatesByLocale = array();
702 $candidatesByLang = array();
703
704 foreach ( $lines as $line ) {
705 if ( $line === '' ) {
706 continue;
707 }
708
709 if ( !preg_match( '/^([a-zA-Z]+)(_[a-zA-Z]+|)\.(utf8|UTF-8)(@[a-zA-Z_]*|)$/i', $line, $m ) ) {
710 continue;
711 }
712
713 list( $all, $lang, $territory, $charset, $modifier ) = $m;
714
715 $candidatesByLocale[$m[0]] = $m;
716 $candidatesByLang[$lang][] = $m;
717 }
718
719 # Try the current value of LANG.
720 if ( isset( $candidatesByLocale[ getenv( 'LANG' ) ] ) ) {
721 $this->setVar( 'wgShellLocale', getenv( 'LANG' ) );
722 $this->showMessage( 'config-shell-locale', getenv( 'LANG' ) );
723 return true;
724 }
725
726 # Try the most common ones.
727 $commonLocales = array( 'en_US.UTF-8', 'en_US.utf8', 'de_DE.UTF-8', 'de_DE.utf8' );
728 foreach ( $commonLocales as $commonLocale ) {
729 if ( isset( $candidatesByLocale[$commonLocale] ) ) {
730 $this->setVar( 'wgShellLocale', $commonLocale );
731 $this->showMessage( 'config-shell-locale', $commonLocale );
732 return true;
733 }
734 }
735
736 # Is there an available locale in the Wiki's language?
737 $wikiLang = $this->getVar( 'wgLanguageCode' );
738
739 if ( isset( $candidatesByLang[$wikiLang] ) ) {
740 $m = reset( $candidatesByLang[$wikiLang] );
741 $this->setVar( 'wgShellLocale', $m[0] );
742 $this->showMessage( 'config-shell-locale', $m[0] );
743 return true;
744 }
745
746 # Are there any at all?
747 if ( count( $candidatesByLocale ) ) {
748 $m = reset( $candidatesByLocale );
749 $this->setVar( 'wgShellLocale', $m[0] );
750 $this->showMessage( 'config-shell-locale', $m[0] );
751 return true;
752 }
753
754 # Give up.
755 return true;
756 }
757
758 /**
759 * TODO: document
760 */
761 protected function envCheckUploadsDirectory() {
762 global $IP, $wgServer;
763
764 $dir = $IP . '/images/';
765 $url = $wgServer . $this->getVar( 'wgScriptPath' ) . '/images/';
766 $safe = !$this->dirIsExecutable( $dir, $url );
767
768 if ( $safe ) {
769 $this->showMessage( 'config-uploads-safe' );
770 } else {
771 $this->showMessage( 'config-uploads-not-safe', $dir );
772 }
773 }
774
775 /**
776 * Convert a hex string representing a Unicode code point to that code point.
777 * @param $c String
778 * @return string
779 */
780 protected function unicodeChar( $c ) {
781 $c = hexdec($c);
782 if ($c <= 0x7F) {
783 return chr($c);
784 } else if ($c <= 0x7FF) {
785 return chr(0xC0 | $c >> 6) . chr(0x80 | $c & 0x3F);
786 } else if ($c <= 0xFFFF) {
787 return chr(0xE0 | $c >> 12) . chr(0x80 | $c >> 6 & 0x3F)
788 . chr(0x80 | $c & 0x3F);
789 } else if ($c <= 0x10FFFF) {
790 return chr(0xF0 | $c >> 18) . chr(0x80 | $c >> 12 & 0x3F)
791 . chr(0x80 | $c >> 6 & 0x3F)
792 . chr(0x80 | $c & 0x3F);
793 } else {
794 return false;
795 }
796 }
797
798
799 /**
800 * Check the libicu version
801 */
802 protected function envCheckLibicu() {
803 $utf8 = function_exists( 'utf8_normalize' );
804 $intl = function_exists( 'normalizer_normalize' );
805
806 /**
807 * This needs to be updated something that the latest libicu
808 * will properly normalize. This normalization was found at
809 * http://www.unicode.org/versions/Unicode5.2.0/#Character_Additions
810 * Note that we use the hex representation to create the code
811 * points in order to avoid any Unicode-destroying during transit.
812 */
813 $not_normal_c = $this->unicodeChar("FA6C");
814 $normal_c = $this->unicodeChar("242EE");
815
816 $useNormalizer = 'php';
817 $needsUpdate = false;
818
819 /**
820 * We're going to prefer the pecl extension here unless
821 * utf8_normalize is more up to date.
822 */
823 if( $utf8 ) {
824 $useNormalizer = 'utf8';
825 $utf8 = utf8_normalize( $not_normal_c, UNORM_NFC );
826 if ( $utf8 !== $normal_c ) $needsUpdate = true;
827 }
828 if( $intl ) {
829 $useNormalizer = 'intl';
830 $intl = normalizer_normalize( $not_normal_c, Normalizer::FORM_C );
831 if ( $intl !== $normal_c ) $needsUpdate = true;
832 }
833
834 // Uses messages 'config-unicode-using-php', 'config-unicode-using-utf8', 'config-unicode-using-intl'
835 if( $useNormalizer === 'php' ) {
836 $this->showMessage( 'config-unicode-pure-php-warning' );
837 } else {
838 $this->showMessage( 'config-unicode-using-' . $useNormalizer );
839 if( $needsUpdate ) {
840 $this->showMessage( 'config-unicode-update-warning' );
841 }
842 }
843 }
844
845 /**
846 * Get an array of likely places we can find executables. Check a bunch
847 * of known Unix-like defaults, as well as the PATH environment variable
848 * (which should maybe make it work for Windows?)
849 *
850 * @return Array
851 */
852 protected static function getPossibleBinPaths() {
853 return array_merge(
854 array( '/usr/bin', '/usr/local/bin', '/opt/csw/bin',
855 '/usr/gnu/bin', '/usr/sfw/bin', '/sw/bin', '/opt/local/bin' ),
856 explode( PATH_SEPARATOR, getenv( 'PATH' ) )
857 );
858 }
859
860 /**
861 * Search a path for any of the given executable names. Returns the
862 * executable name if found. Also checks the version string returned
863 * by each executable.
864 *
865 * Used only by environment checks.
866 *
867 * @param $path String: path to search
868 * @param $names Array of executable names
869 * @param $versionInfo Boolean false or array with two members:
870 * 0 => Command to run for version check, with $1 for the full executable name
871 * 1 => String to compare the output with
872 *
873 * If $versionInfo is not false, only executables with a version
874 * matching $versionInfo[1] will be returned.
875 */
876 public static function locateExecutable( $path, $names, $versionInfo = false ) {
877 if ( !is_array( $names ) ) {
878 $names = array( $names );
879 }
880
881 foreach ( $names as $name ) {
882 $command = $path . DIRECTORY_SEPARATOR . $name;
883
884 wfSuppressWarnings();
885 $file_exists = file_exists( $command );
886 wfRestoreWarnings();
887
888 if ( $file_exists ) {
889 if ( !$versionInfo ) {
890 return $command;
891 }
892
893 $file = str_replace( '$1', wfEscapeShellArg( $command ), $versionInfo[0] );
894 if ( strstr( wfShellExec( $file ), $versionInfo[1] ) !== false ) {
895 return $command;
896 }
897 }
898 }
899 return false;
900 }
901
902 /**
903 * Same as locateExecutable(), but checks in getPossibleBinPaths() by default
904 * @see locateExecutable()
905 */
906 public static function locateExecutableInDefaultPaths( $names, $versionInfo = false ) {
907 foreach( self::getPossibleBinPaths() as $path ) {
908 $exe = self::locateExecutable( $path, $names, $versionInfo );
909 if( $exe !== false ) {
910 return $exe;
911 }
912 }
913 return false;
914 }
915
916 /**
917 * Checks if scripts located in the given directory can be executed via the given URL.
918 *
919 * Used only by environment checks.
920 */
921 public function dirIsExecutable( $dir, $url ) {
922 $scriptTypes = array(
923 'php' => array(
924 "<?php echo 'ex' . 'ec';",
925 "#!/var/env php5\n<?php echo 'ex' . 'ec';",
926 ),
927 );
928
929 // it would be good to check other popular languages here, but it'll be slow.
930
931 wfSuppressWarnings();
932
933 foreach ( $scriptTypes as $ext => $contents ) {
934 foreach ( $contents as $source ) {
935 $file = 'exectest.' . $ext;
936
937 if ( !file_put_contents( $dir . $file, $source ) ) {
938 break;
939 }
940
941 $text = Http::get( $url . $file );
942 unlink( $dir . $file );
943
944 if ( $text == 'exec' ) {
945 wfRestoreWarnings();
946 return $ext;
947 }
948 }
949 }
950
951 wfRestoreWarnings();
952
953 return false;
954 }
955
956 }