update.php 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  1. #!/usr/bin/env php
  2. <?php
  3. /**
  4. * Run all updaters.
  5. *
  6. * This is used when the database schema is modified and we need to apply patches.
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation; either version 2 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License along
  19. * with this program; if not, write to the Free Software Foundation, Inc.,
  20. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  21. * http://www.gnu.org/copyleft/gpl.html
  22. *
  23. * @file
  24. * @todo document
  25. * @ingroup Maintenance
  26. */
  27. require_once __DIR__ . '/Maintenance.php';
  28. use Wikimedia\Rdbms\IMaintainableDatabase;
  29. /**
  30. * Maintenance script to run database schema updates.
  31. *
  32. * @ingroup Maintenance
  33. */
  34. class UpdateMediaWiki extends Maintenance {
  35. function __construct() {
  36. parent::__construct();
  37. $this->addDescription( 'MediaWiki database updater' );
  38. $this->addOption( 'skip-compat-checks', 'Skips compatibility checks, mostly for developers' );
  39. $this->addOption( 'quick', 'Skip 5 second countdown before starting' );
  40. $this->addOption( 'doshared', 'Also update shared tables' );
  41. $this->addOption( 'nopurge', 'Do not purge the objectcache table after updates' );
  42. $this->addOption( 'noschema', 'Only do the updates that are not done during schema updates' );
  43. $this->addOption(
  44. 'schema',
  45. 'Output SQL to do the schema updates instead of doing them. Works '
  46. . 'even when $wgAllowSchemaUpdates is false',
  47. false,
  48. true
  49. );
  50. $this->addOption( 'force', 'Override when $wgAllowSchemaUpdates disables this script' );
  51. $this->addOption(
  52. 'skip-external-dependencies',
  53. 'Skips checking whether external dependencies are up to date, mostly for developers'
  54. );
  55. }
  56. function getDbType() {
  57. return Maintenance::DB_ADMIN;
  58. }
  59. function compatChecks() {
  60. $minimumPcreVersion = Installer::MINIMUM_PCRE_VERSION;
  61. list( $pcreVersion ) = explode( ' ', PCRE_VERSION, 2 );
  62. if ( version_compare( $pcreVersion, $minimumPcreVersion, '<' ) ) {
  63. $this->fatalError(
  64. "PCRE $minimumPcreVersion or later is required.\n" .
  65. "Your PHP binary is linked with PCRE $pcreVersion.\n\n" .
  66. "More information:\n" .
  67. "https://www.mediawiki.org/wiki/Manual:Errors_and_symptoms/PCRE\n\n" .
  68. "ABORTING.\n" );
  69. }
  70. $test = new PhpXmlBugTester();
  71. if ( !$test->ok ) {
  72. $this->fatalError(
  73. "Your system has a combination of PHP and libxml2 versions that is buggy\n" .
  74. "and can cause hidden data corruption in MediaWiki and other web apps.\n" .
  75. "Upgrade to libxml2 2.7.3 or later.\n" .
  76. "ABORTING (see https://bugs.php.net/bug.php?id=45996).\n" );
  77. }
  78. }
  79. function execute() {
  80. global $wgVersion, $wgLang, $wgAllowSchemaUpdates;
  81. if ( !$wgAllowSchemaUpdates
  82. && !( $this->hasOption( 'force' )
  83. || $this->hasOption( 'schema' )
  84. || $this->hasOption( 'noschema' ) )
  85. ) {
  86. $this->fatalError( "Do not run update.php on this wiki. If you're seeing this you should\n"
  87. . "probably ask for some help in performing your schema updates or use\n"
  88. . "the --noschema and --schema options to get an SQL file for someone\n"
  89. . "else to inspect and run.\n\n"
  90. . "If you know what you are doing, you can continue with --force\n" );
  91. }
  92. $this->fileHandle = null;
  93. if ( substr( $this->getOption( 'schema' ), 0, 2 ) === "--" ) {
  94. $this->fatalError( "The --schema option requires a file as an argument.\n" );
  95. } elseif ( $this->hasOption( 'schema' ) ) {
  96. $file = $this->getOption( 'schema' );
  97. $this->fileHandle = fopen( $file, "w" );
  98. if ( $this->fileHandle === false ) {
  99. $err = error_get_last();
  100. $this->fatalError( "Problem opening the schema file for writing: $file\n\t{$err['message']}" );
  101. }
  102. }
  103. $lang = Language::factory( 'en' );
  104. // Set global language to ensure localised errors are in English (T22633)
  105. RequestContext::getMain()->setLanguage( $lang );
  106. $wgLang = $lang; // BackCompat
  107. define( 'MW_UPDATER', true );
  108. $this->output( "MediaWiki {$wgVersion} Updater\n\n" );
  109. wfWaitForSlaves();
  110. if ( !$this->hasOption( 'skip-compat-checks' ) ) {
  111. $this->compatChecks();
  112. } else {
  113. $this->output( "Skipping compatibility checks, proceed at your own risk (Ctrl+C to abort)\n" );
  114. $this->countDown( 5 );
  115. }
  116. // Check external dependencies are up to date
  117. if ( !$this->hasOption( 'skip-external-dependencies' ) ) {
  118. $composerLockUpToDate = $this->runChild( CheckComposerLockUpToDate::class );
  119. $composerLockUpToDate->execute();
  120. } else {
  121. $this->output(
  122. "Skipping checking whether external dependencies are up to date, proceed at your own risk\n"
  123. );
  124. }
  125. # Attempt to connect to the database as a privileged user
  126. # This will vomit up an error if there are permissions problems
  127. $db = $this->getDB( DB_MASTER );
  128. # Check to see whether the database server meets the minimum requirements
  129. /** @var DatabaseInstaller $dbInstallerClass */
  130. $dbInstallerClass = Installer::getDBInstallerClass( $db->getType() );
  131. $status = $dbInstallerClass::meetsMinimumRequirement( $db->getServerVersion() );
  132. if ( !$status->isOK() ) {
  133. // This might output some wikitext like <strong> but it should be comprehensible
  134. $text = $status->getWikiText();
  135. $this->fatalError( $text );
  136. }
  137. $this->output( "Going to run database updates for " . wfWikiID() . "\n" );
  138. if ( $db->getType() === 'sqlite' ) {
  139. /** @var IMaintainableDatabase|DatabaseSqlite $db */
  140. $this->output( "Using SQLite file: '{$db->getDbFilePath()}'\n" );
  141. }
  142. $this->output( "Depending on the size of your database this may take a while!\n" );
  143. if ( !$this->hasOption( 'quick' ) ) {
  144. $this->output( "Abort with control-c in the next five seconds "
  145. . "(skip this countdown with --quick) ... " );
  146. $this->countDown( 5 );
  147. }
  148. $time1 = microtime( true );
  149. $badPhpUnit = dirname( __DIR__ ) . '/vendor/phpunit/phpunit/src/Util/PHP/eval-stdin.php';
  150. if ( file_exists( $badPhpUnit ) ) {
  151. // Bad versions of the file are:
  152. // https://raw.githubusercontent.com/sebastianbergmann/phpunit/c820f915bfae34e5a836f94967a2a5ea5ef34f21/src/Util/PHP/eval-stdin.php
  153. // https://raw.githubusercontent.com/sebastianbergmann/phpunit/3aaddb1c5bd9b9b8d070b4cf120e71c36fd08412/src/Util/PHP/eval-stdin.php
  154. $md5 = md5_file( $badPhpUnit );
  155. if ( $md5 === '120ac49800671dc383b6f3709c25c099'
  156. || $md5 === '28af792cb38fc9a1b236b91c1aad2876'
  157. ) {
  158. $success = unlink( $badPhpUnit );
  159. if ( $success ) {
  160. $this->output( "Removed PHPUnit eval-stdin.php to protect against CVE-2017-9841\n" );
  161. } else {
  162. $this->error( "Unable to remove $badPhpUnit, you should manually. See CVE-2017-9841" );
  163. }
  164. }
  165. }
  166. $shared = $this->hasOption( 'doshared' );
  167. $updates = [ 'core', 'extensions' ];
  168. if ( !$this->hasOption( 'schema' ) ) {
  169. if ( $this->hasOption( 'noschema' ) ) {
  170. $updates[] = 'noschema';
  171. }
  172. $updates[] = 'stats';
  173. }
  174. $updater = DatabaseUpdater::newForDB( $db, $shared, $this );
  175. $updater->doUpdates( $updates );
  176. foreach ( $updater->getPostDatabaseUpdateMaintenance() as $maint ) {
  177. $child = $this->runChild( $maint );
  178. // LoggedUpdateMaintenance is checking the updatelog itself
  179. $isLoggedUpdate = $child instanceof LoggedUpdateMaintenance;
  180. if ( !$isLoggedUpdate && $updater->updateRowExists( $maint ) ) {
  181. continue;
  182. }
  183. $child->execute();
  184. if ( !$isLoggedUpdate ) {
  185. $updater->insertUpdateRow( $maint );
  186. }
  187. }
  188. $updater->setFileAccess();
  189. if ( !$this->hasOption( 'nopurge' ) ) {
  190. $updater->purgeCache();
  191. }
  192. $time2 = microtime( true );
  193. $timeDiff = $lang->formatTimePeriod( $time2 - $time1 );
  194. $this->output( "\nDone in $timeDiff.\n" );
  195. }
  196. function afterFinalSetup() {
  197. global $wgLocalisationCacheConf;
  198. # Don't try to access the database
  199. # This needs to be disabled early since extensions will try to use the l10n
  200. # cache from $wgExtensionFunctions (T22471)
  201. $wgLocalisationCacheConf = [
  202. 'class' => LocalisationCache::class,
  203. 'storeClass' => LCStoreNull::class,
  204. 'storeDirectory' => false,
  205. 'manualRecache' => false,
  206. ];
  207. }
  208. }
  209. $maintClass = UpdateMediaWiki::class;
  210. require_once RUN_MAINTENANCE_IF_MAIN;