* DatabaseOracle - throw connection exception instead of debug+false
[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 * TODO: document
377 *
378 * @param $installer DatabaseInstaller
379 *
380 * @return Status
381 */
382 public function installMainpage( DatabaseInstaller &$installer ) {
383 return $installer->createMainpage();
384 }
385
386 /**
387 * Exports all wg* variables stored by the installer into global scope.
388 */
389 public function exportVars() {
390 foreach ( $this->settings as $name => $value ) {
391 if ( substr( $name, 0, 2 ) == 'wg' ) {
392 $GLOBALS[$name] = $value;
393 }
394 }
395 }
396
397 /**
398 * Check if we're installing the latest version.
399 */
400 public function envLatestVersion() {
401 global $wgVersion;
402
403 $repository = wfGetRepository();
404 $currentVersion = $repository->getLatestCoreVersion();
405
406 $this->setVar( '_ExternalHTTP', true );
407
408 if ( $currentVersion === false ) {
409 # For when the request is successful but there's e.g. some silly man in
410 # the middle firewall blocking us, e.g. one of those annoying airport ones
411 $this->showMessage( 'config-env-latest-can-not-check', $repository->getLocation() );
412 return;
413 }
414
415 if( version_compare( $wgVersion, $currentVersion, '<' ) ) {
416 $this->showMessage( 'config-env-latest-old' );
417 // FIXME: this only works for the web installer!
418 $this->showHelpBox( 'config-env-latest-help', $wgVersion, $currentVersion );
419 } elseif( version_compare( $wgVersion, $currentVersion, '>' ) ) {
420 $this->showMessage( 'config-env-latest-new' );
421 }
422
423 $this->showMessage( 'config-env-latest-ok' );
424 }
425
426 /**
427 * Environment check for DB types.
428 */
429 public function envCheckDB() {
430 global $wgLang;
431
432 $compiledDBs = array();
433 $goodNames = array();
434 $allNames = array();
435
436 foreach ( self::getDBTypes() as $name ) {
437 $db = $this->getDBInstaller( $name );
438 $readableName = wfMsg( 'config-type-' . $name );
439
440 if ( $db->isCompiled() ) {
441 $compiledDBs[] = $name;
442 $goodNames[] = $readableName;
443 }
444
445 $allNames[] = $readableName;
446 }
447
448 $this->setVar( '_CompiledDBs', $compiledDBs );
449
450 if ( !$compiledDBs ) {
451 $this->showMessage( 'config-no-db' );
452 // FIXME: this only works for the web installer!
453 $this->showHelpBox( 'config-no-db-help', $wgLang->commaList( $allNames ) );
454 return false;
455 }
456
457 $this->showMessage( 'config-have-db', $wgLang->listToText( $goodNames ), count( $goodNames ) );
458
459 // Check for FTS3 full-text search module
460 $sqlite = $this->getDBInstaller( 'sqlite' );
461 if ( $sqlite->isCompiled() ) {
462 $db = new DatabaseSqliteStandalone( ':memory:' );
463 $this->showMessage( $db->getFulltextSearchModule() == 'FTS3'
464 ? 'config-have-fts3'
465 : 'config-no-fts3'
466 );
467 }
468 }
469
470 /**
471 * Environment check for register_globals.
472 */
473 public function envCheckRegisterGlobals() {
474 if( wfIniGetBool( "magic_quotes_runtime" ) ) {
475 $this->showMessage( 'config-register-globals' );
476 }
477 }
478
479 /**
480 * Environment check for magic_quotes_runtime.
481 */
482 public function envCheckMagicQuotes() {
483 if( wfIniGetBool( "magic_quotes_runtime" ) ) {
484 $this->showMessage( 'config-magic-quotes-runtime' );
485 return false;
486 }
487 }
488
489 /**
490 * Environment check for magic_quotes_sybase.
491 */
492 public function envCheckMagicSybase() {
493 if ( wfIniGetBool( 'magic_quotes_sybase' ) ) {
494 $this->showMessage( 'config-magic-quotes-sybase' );
495 return false;
496 }
497 }
498
499 /**
500 * Environment check for mbstring.func_overload.
501 */
502 public function envCheckMbstring() {
503 if ( wfIniGetBool( 'mbstring.func_overload' ) ) {
504 $this->showMessage( 'config-mbstring' );
505 return false;
506 }
507 }
508
509 /**
510 * Environment check for zend.ze1_compatibility_mode.
511 */
512 public function envCheckZE1() {
513 if ( wfIniGetBool( 'zend.ze1_compatibility_mode' ) ) {
514 $this->showMessage( 'config-ze1' );
515 return false;
516 }
517 }
518
519 /**
520 * Environment check for safe_mode.
521 */
522 public function envCheckSafeMode() {
523 if ( wfIniGetBool( 'safe_mode' ) ) {
524 $this->setVar( '_SafeMode', true );
525 $this->showMessage( 'config-safe-mode' );
526 }
527 }
528
529 /**
530 * Environment check for the XML module.
531 */
532 public function envCheckXML() {
533 if ( !function_exists( "utf8_encode" ) ) {
534 $this->showMessage( 'config-xml-bad' );
535 return false;
536 }
537 $this->showMessage( 'config-xml-good' );
538 }
539
540 /**
541 * Environment check for the PCRE module.
542 */
543 public function envCheckPCRE() {
544 if ( !function_exists( 'preg_match' ) ) {
545 $this->showMessage( 'config-pcre' );
546 return false;
547 }
548 }
549
550 /**
551 * Environment check for available memory.
552 */
553 public function envCheckMemory() {
554 $limit = ini_get( 'memory_limit' );
555
556 if ( !$limit || $limit == -1 ) {
557 $this->showMessage( 'config-memory-none' );
558 return true;
559 }
560
561 $n = intval( $limit );
562
563 if( preg_match( '/^([0-9]+)[Mm]$/', trim( $limit ), $m ) ) {
564 $n = intval( $m[1] * ( 1024 * 1024 ) );
565 }
566
567 if( $n < $this->minMemorySize * 1024 * 1024 ) {
568 $newLimit = "{$this->minMemorySize}M";
569
570 if( ini_set( "memory_limit", $newLimit ) === false ) {
571 $this->showMessage( 'config-memory-bad', $limit );
572 } else {
573 $this->showMessage( 'config-memory-raised', $limit, $newLimit );
574 $this->setVar( '_RaiseMemory', true );
575 }
576 } else {
577 $this->showMessage( 'config-memory-ok', $limit );
578 }
579 }
580
581 /**
582 * Environment check for compiled object cache types.
583 */
584 public function envCheckCache() {
585 $caches = array();
586
587 foreach ( $this->objectCaches as $name => $function ) {
588 if ( function_exists( $function ) ) {
589 $caches[$name] = true;
590 $this->showMessage( 'config-' . $name );
591 }
592 }
593
594 if ( !$caches ) {
595 $this->showMessage( 'config-no-cache' );
596 }
597
598 $this->setVar( '_Caches', $caches );
599 }
600
601 /**
602 * Search for GNU diff3.
603 */
604 public function envCheckDiff3() {
605 $names = array( "gdiff3", "diff3", "diff3.exe" );
606 $versionInfo = array( '$1 --version 2>&1', 'diff3 (GNU diffutils)' );
607
608 $diff3 = $this->locateExecutableInDefaultPaths( $names, $versionInfo );
609
610 if ( $diff3 ) {
611 $this->showMessage( 'config-diff3-good', $diff3 );
612 $this->setVar( 'wgDiff3', $diff3 );
613 } else {
614 $this->setVar( 'wgDiff3', false );
615 $this->showMessage( 'config-diff3-bad' );
616 }
617 }
618
619 /**
620 * Environment check for ImageMagick and GD.
621 */
622 public function envCheckGraphics() {
623 $names = array( wfIsWindows() ? 'convert.exe' : 'convert' );
624 $convert = $this->locateExecutableInDefaultPaths( $names, array( '$1 -version', 'ImageMagick' ) );
625
626 if ( $convert ) {
627 $this->setVar( 'wgImageMagickConvertCommand', $convert );
628 $this->showMessage( 'config-imagemagick', $convert );
629 return true;
630 } elseif ( function_exists( 'imagejpeg' ) ) {
631 $this->showMessage( 'config-gd' );
632 return true;
633 } else {
634 $this->showMessage( 'no-scaling' );
635 }
636 }
637
638 /**
639 * Environment check for setting $IP and $wgScriptPath.
640 */
641 public function envCheckPath() {
642 global $IP;
643 $IP = dirname( dirname( dirname( __FILE__ ) ) );
644
645 $this->setVar( 'IP', $IP );
646 $this->showMessage( 'config-dir', $IP );
647
648 // PHP_SELF isn't available sometimes, such as when PHP is CGI but
649 // cgi.fix_pathinfo is disabled. In that case, fall back to SCRIPT_NAME
650 // to get the path to the current script... hopefully it's reliable. SIGH
651 if ( !empty( $_SERVER['PHP_SELF'] ) ) {
652 $path = $_SERVER['PHP_SELF'];
653 } elseif ( !empty( $_SERVER['SCRIPT_NAME'] ) ) {
654 $path = $_SERVER['SCRIPT_NAME'];
655 } elseif ( $this->getVar( 'wgScriptPath' ) ) {
656 // Some kind soul has set it for us already (e.g. debconf)
657 return true;
658 } else {
659 $this->showMessage( 'config-no-uri' );
660 return false;
661 }
662
663 $uri = preg_replace( '{^(.*)/config.*$}', '$1', $path );
664 $this->setVar( 'wgScriptPath', $uri );
665 $this->showMessage( 'config-uri', $uri );
666 }
667
668 /**
669 * Environment check for writable config/ directory.
670 */
671 public function envCheckWriteableDir() {
672 $ipDir = $this->getVar( 'IP' );
673 $configDir = $ipDir . '/config';
674
675 if( !is_writeable( $configDir ) ) {
676 $webserverGroup = self::maybeGetWebserverPrimaryGroup();
677
678 if ( $webserverGroup !== null ) {
679 $this->showMessage( 'config-dir-not-writable-group', $ipDir, $webserverGroup );
680 } else {
681 $this->showMessage( 'config-dir-not-writable-nogroup', $ipDir, $webserverGroup );
682 }
683
684 return false;
685 }
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 $lines = $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 $this->showMessage( 'config-unicode-using-' . $useNormalizer );
858 if( $useNormalizer === 'php' ) {
859 $this->showMessage( 'config-unicode-pure-php-warning' );
860 } elseif( $needsUpdate ) {
861 $this->showMessage( 'config-unicode-update-warning' );
862 }
863 }
864
865 /**
866 * Get an array of likely places we can find executables. Check a bunch
867 * of known Unix-like defaults, as well as the PATH environment variable
868 * (which should maybe make it work for Windows?)
869 *
870 * @return Array
871 */
872 protected function getPossibleBinPaths() {
873 return array_merge(
874 array( '/usr/bin', '/usr/local/bin', '/opt/csw/bin',
875 '/usr/gnu/bin', '/usr/sfw/bin', '/sw/bin', '/opt/local/bin' ),
876 explode( PATH_SEPARATOR, getenv( 'PATH' ) )
877 );
878 }
879
880 /**
881 * Search a path for any of the given executable names. Returns the
882 * executable name if found. Also checks the version string returned
883 * by each executable.
884 *
885 * Used only by environment checks.
886 *
887 * @param $path String: path to search
888 * @param $names Array of executable names
889 * @param $versionInfo Boolean false or array with two members:
890 * 0 => Command to run for version check, with $1 for the path
891 * 1 => String to compare the output with
892 *
893 * If $versionInfo is not false, only executables with a version
894 * matching $versionInfo[1] will be returned.
895 */
896 protected function locateExecutable( $path, $names, $versionInfo = false ) {
897 if ( !is_array( $names ) ) {
898 $names = array( $names );
899 }
900
901 foreach ( $names as $name ) {
902 $command = $path . DIRECTORY_SEPARATOR . $name;
903
904 wfSuppressWarnings();
905 $file_exists = file_exists( $command );
906 wfRestoreWarnings();
907
908 if ( $file_exists ) {
909 if ( !$versionInfo ) {
910 return $command;
911 }
912
913 if ( wfIsWindows() ) {
914 $command = "\"$command\"";
915 }
916 $file = str_replace( '$1', $command, $versionInfo[0] );
917 if ( strstr( wfShellExec( $file ), $versionInfo[1]) !== false ) {
918 return $command;
919 }
920 }
921 }
922 return false;
923 }
924
925 /**
926 * Same as locateExecutable(), but checks in getPossibleBinPaths() by default
927 * @see locateExecutable()
928 */
929 protected function locateExecutableInDefaultPaths( $names, $versionInfo = false ) {
930 foreach( $this->getPossibleBinPaths() as $path ) {
931 $exe = $this->locateExecutable( $path, $names, $versionInfo );
932 if( $exe !== false ) {
933 return $exe;
934 }
935 }
936 return false;
937 }
938
939 /**
940 * Checks if scripts located in the given directory can be executed via the given URL.
941 *
942 * Used only by environment checks.
943 */
944 public function dirIsExecutable( $dir, $url ) {
945 $scriptTypes = array(
946 'php' => array(
947 "<?php echo 'ex' . 'ec';",
948 "#!/var/env php5\n<?php echo 'ex' . 'ec';",
949 ),
950 );
951
952 // it would be good to check other popular languages here, but it'll be slow.
953
954 wfSuppressWarnings();
955
956 foreach ( $scriptTypes as $ext => $contents ) {
957 foreach ( $contents as $source ) {
958 $file = 'exectest.' . $ext;
959
960 if ( !file_put_contents( $dir . $file, $source ) ) {
961 break;
962 }
963
964 $text = Http::get( $url . $file );
965 unlink( $dir . $file );
966
967 if ( $text == 'exec' ) {
968 wfRestoreWarnings();
969 return $ext;
970 }
971 }
972 }
973
974 wfRestoreWarnings();
975
976 return false;
977 }
978
979 }