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