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