install-utils.inc 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. <?php
  2. /**
  3. * This file contains functions used by the install script (config/index.php)
  4. * and maintenance scripts. It is not loaded in normal web requests.
  5. *
  6. * @file
  7. */
  8. function install_version_checks() {
  9. # We dare not turn output buffer _off_ since this will break completely
  10. # if PHP is globally configured to run through a gzip filter.
  11. @ob_implicit_flush( true );
  12. if( !function_exists( 'version_compare' ) ) {
  13. # version_compare was introduced in 4.1.0
  14. echo "Your PHP version is much too old; 4.0.x will _not_ work. 5.0.0 or higher is required. ABORTING.\n";
  15. die( -1 );
  16. }
  17. if( version_compare( phpversion(), '5.0.0' ) < 0 ) {
  18. echo "PHP 5.0.0 or higher is required. If PHP 5 is available only when \n".
  19. "PHP files have a .php5 extension, please navigate to <a href=\"index.php5\">index.php5</a> \n".
  20. "to continue installation. ABORTING.\n";
  21. die( -1 );
  22. }
  23. // Test for PHP bug which breaks PHP 5.0.x on 64-bit...
  24. // As of 1.8 this breaks lots of common operations instead
  25. // of just some rare ones like export.
  26. $borked = str_replace( 'a', 'b', array( -1 => -1 ) );
  27. if( !isset( $borked[-1] ) ) {
  28. echo "PHP 5.0.x is buggy on your 64-bit system; you must upgrade to PHP 5.1.x\n" .
  29. "or higher. ABORTING. (http://bugs.php.net/bug.php?id=34879 for details)\n";
  30. die( -1 );
  31. }
  32. global $wgCommandLineMode;
  33. $wgCommandLineMode = true;
  34. umask( 000 );
  35. @set_time_limit( 0 );
  36. }
  37. function copyfile( $sdir, $name, $ddir, $perms = 0664 ) {
  38. copyfileto( $sdir, $name, $ddir, $name, $perms );
  39. }
  40. function copyfileto( $sdir, $sname, $ddir, $dname, $perms = 0664 ) {
  41. global $wgInstallOwner, $wgInstallGroup;
  42. $d = "{$ddir}/{$dname}";
  43. if ( copy( "{$sdir}/{$sname}", $d ) ) {
  44. if ( isset( $wgInstallOwner ) ) { chown( $d, $wgInstallOwner ); }
  45. if ( isset( $wgInstallGroup ) ) { chgrp( $d, $wgInstallGroup ); }
  46. chmod( $d, $perms );
  47. # print "Copied \"{$sname}\" to \"{$d}\".\n";
  48. } else {
  49. print "Failed to copy file \"{$sname}\" to \"{$ddir}/{$dname}\".\n";
  50. exit();
  51. }
  52. }
  53. function copydirectory( $source, $dest ) {
  54. $handle = opendir( $source );
  55. while ( false !== ( $f = readdir( $handle ) ) ) {
  56. $fullname = "$source/$f";
  57. if ( $f{0} != '.' && is_file( $fullname ) ) {
  58. copyfile( $source, $f, $dest );
  59. }
  60. }
  61. }
  62. function readconsole( $prompt = '' ) {
  63. static $isatty = null;
  64. if ( is_null( $isatty ) ) {
  65. if ( !function_exists( 'posix_isatty' ) || posix_isatty( 0 /*STDIN*/ ) ) {
  66. $isatty = true;
  67. } else {
  68. $isatty = false;
  69. }
  70. }
  71. if ( $isatty && function_exists( 'readline' ) ) {
  72. return readline( $prompt );
  73. } else {
  74. if ( $isatty ) {
  75. print $prompt;
  76. }
  77. if ( feof( STDIN ) ) {
  78. return false;
  79. }
  80. $st = fgets(STDIN, 1024);
  81. if ($st === false) return false;
  82. $resp = trim( $st );
  83. return $resp;
  84. }
  85. }
  86. #
  87. # Read and execute SQL commands from a file
  88. #
  89. function dbsource( $fname, $db = false ) {
  90. if ( !$db ) {
  91. // Try $wgDatabase, which is used in the install and update scripts
  92. global $wgDatabase;
  93. if ( isset( $wgDatabase ) ) {
  94. $db = $wgDatabase;
  95. } else {
  96. // No? Well, we must be outside of those scripts, so use the standard method
  97. $db = wfGetDB( DB_MASTER );
  98. }
  99. }
  100. $error = $db->sourceFile( $fname );
  101. if ( $error !== true ) {
  102. print $error;
  103. exit(1);
  104. }
  105. }
  106. /**
  107. * Get the value of session.save_path
  108. *
  109. * Per http://www.php.net/manual/en/ref.session.php#ini.session.save-path,
  110. * this might have some additional preceding parts which need to be
  111. * ditched
  112. *
  113. * @return string
  114. */
  115. function mw_get_session_save_path() {
  116. $path = ini_get( 'session.save_path' );
  117. $path = substr( $path, strrpos( $path, ';' ) );
  118. return $path;
  119. }
  120. /**
  121. * Is dl() available to us?
  122. *
  123. * According to http://www.php.net/manual/en/function.dl.php, dl()
  124. * is *not* available when `enable_dl` is off, or under `safe_mode`
  125. *
  126. * @return bool
  127. */
  128. function mw_have_dl() {
  129. return function_exists( 'dl' )
  130. && is_callable( 'dl' )
  131. && wfIniGetBool( 'enable_dl' )
  132. && !wfIniGetBool( 'safe_mode' );
  133. }