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 28require_once __DIR__ . '/Maintenance.php'; 29 30use MediaWiki\MediaWikiServices; 31use Wikimedia\Rdbms\DatabaseSqlite; 32 33/** 34 * Maintenance script to run database schema updates. 35 * 36 * @ingroup Maintenance 37 */ 38class UpdateMediaWiki extends Maintenance { 39 public function __construct() { 40 parent::__construct(); 41 $this->addDescription( 'MediaWiki database updater' ); 42 $this->addOption( 'skip-compat-checks', 'Skips compatibility checks, mostly for developers' ); 43 $this->addOption( 'quick', 'Skip 5 second countdown before starting' ); 44 $this->addOption( 'doshared', 'Also update shared tables' ); 45 $this->addOption( 'nopurge', 'Do not purge the objectcache table after updates' ); 46 $this->addOption( 'noschema', 'Only do the updates that are not done during schema updates' ); 47 $this->addOption( 48 'schema', 49 'Output SQL to do the schema updates instead of doing them. Works ' 50 . 'even when $wgAllowSchemaUpdates is false', 51 false, 52 true 53 ); 54 $this->addOption( 'force', 'Override when $wgAllowSchemaUpdates disables this script' ); 55 $this->addOption( 56 'skip-external-dependencies', 57 'Skips checking whether external dependencies are up to date, mostly for developers' 58 ); 59 } 60 61 public function getDbType() { 62 return Maintenance::DB_ADMIN; 63 } 64 65 private function compatChecks() { 66 $minimumPcreVersion = Installer::MINIMUM_PCRE_VERSION; 67 68 $pcreVersion = explode( ' ', PCRE_VERSION, 2 )[0]; 69 if ( version_compare( $pcreVersion, $minimumPcreVersion, '<' ) ) { 70 $this->fatalError( 71 "PCRE $minimumPcreVersion or later is required.\n" . 72 "Your PHP binary is linked with PCRE $pcreVersion.\n\n" . 73 "More information:\n" . 74 "https://www.mediawiki.org/wiki/Manual:Errors_and_symptoms/PCRE\n\n" . 75 "ABORTING.\n" ); 76 } 77 } 78 79 public function execute() { 80 global $wgLang, $wgAllowSchemaUpdates, $wgMessagesDirs; 81 82 if ( !$wgAllowSchemaUpdates 83 && !( $this->hasOption( 'force' ) 84 || $this->hasOption( 'schema' ) 85 || $this->hasOption( 'noschema' ) ) 86 ) { 87 $this->fatalError( "Do not run update.php on this wiki. If you're seeing this you should\n" 88 . "probably ask for some help in performing your schema updates or use\n" 89 . "the --noschema and --schema options to get an SQL file for someone\n" 90 . "else to inspect and run.\n\n" 91 . "If you know what you are doing, you can continue with --force\n" ); 92 } 93 94 $this->fileHandle = null; 95 if ( substr( $this->getOption( 'schema' ), 0, 2 ) === "--" ) { 96 $this->fatalError( "The --schema option requires a file as an argument.\n" ); 97 } elseif ( $this->hasOption( 'schema' ) ) { 98 $file = $this->getOption( 'schema' ); 99 $this->fileHandle = fopen( $file, "w" ); 100 if ( $this->fileHandle === false ) { 101 $err = error_get_last(); 102 $this->fatalError( "Problem opening the schema file for writing: $file\n\t{$err['message']}" ); 103 } 104 } 105 106 // T206765: We need to load the installer i18n files as some of errors come installer/updater code 107 $wgMessagesDirs['MediawikiInstaller'] = dirname( __DIR__ ) . '/includes/installer/i18n'; 108 109 $lang = MediaWikiServices::getInstance()->getLanguageFactory()->getLanguage( 'en' ); 110 // Set global language to ensure localised errors are in English (T22633) 111 RequestContext::getMain()->setLanguage( $lang ); 112 113 // BackCompat 114 $wgLang = $lang; 115 116 define( 'MW_UPDATER', true ); 117 118 $this->output( 'MediaWiki ' . MW_VERSION . " Updater\n\n" ); 119 120 MediaWikiServices::getInstance()->getDBLoadBalancerFactory()->waitForReplication(); 121 122 if ( !$this->hasOption( 'skip-compat-checks' ) ) { 123 $this->compatChecks(); 124 } else { 125 $this->output( "Skipping compatibility checks, proceed at your own risk (Ctrl+C to abort)\n" ); 126 $this->countDown( 5 ); 127 } 128 129 // Check external dependencies are up to date 130 if ( !$this->hasOption( 'skip-external-dependencies' ) ) { 131 $composerLockUpToDate = $this->runChild( CheckComposerLockUpToDate::class ); 132 $composerLockUpToDate->execute(); 133 } else { 134 $this->output( 135 "Skipping checking whether external dependencies are up to date, proceed at your own risk\n" 136 ); 137 } 138 139 # Attempt to connect to the database as a privileged user 140 # This will vomit up an error if there are permissions problems 141 $db = $this->getDB( DB_MASTER ); 142 143 # Check to see whether the database server meets the minimum requirements 144 /** @var DatabaseInstaller $dbInstallerClass */ 145 $dbInstallerClass = Installer::getDBInstallerClass( $db->getType() ); 146 $status = $dbInstallerClass::meetsMinimumRequirement( $db->getServerVersion() ); 147 if ( !$status->isOK() ) { 148 // This might output some wikitext like <strong> but it should be comprehensible 149 $text = $status->getWikiText(); 150 $this->fatalError( $text ); 151 } 152 153 $dbDomain = WikiMap::getCurrentWikiDbDomain()->getId(); 154 $this->output( "Going to run database updates for $dbDomain\n" ); 155 if ( $db->getType() === 'sqlite' ) { 156 /** @var DatabaseSqlite $db */ 157 '@phan-var DatabaseSqlite $db'; 158 $this->output( "Using SQLite file: '{$db->getDbFilePath()}'\n" ); 159 } 160 $this->output( "Depending on the size of your database this may take a while!\n" ); 161 162 if ( !$this->hasOption( 'quick' ) ) { 163 $this->output( "Abort with control-c in the next five seconds " 164 . "(skip this countdown with --quick) ... " ); 165 $this->countDown( 5 ); 166 } 167 168 $time1 = microtime( true ); 169 170 $shared = $this->hasOption( 'doshared' ); 171 172 $updates = [ 'core', 'extensions' ]; 173 if ( !$this->hasOption( 'schema' ) ) { 174 if ( $this->hasOption( 'noschema' ) ) { 175 $updates[] = 'noschema'; 176 } 177 $updates[] = 'stats'; 178 } 179 180 $updater = DatabaseUpdater::newForDB( $db, $shared, $this ); 181 $updater->doUpdates( $updates ); 182 183 foreach ( $updater->getPostDatabaseUpdateMaintenance() as $maint ) { 184 $child = $this->runChild( $maint ); 185 186 // LoggedUpdateMaintenance is checking the updatelog itself 187 $isLoggedUpdate = $child instanceof LoggedUpdateMaintenance; 188 189 if ( !$isLoggedUpdate && $updater->updateRowExists( $maint ) ) { 190 continue; 191 } 192 193 $child->execute(); 194 if ( !$isLoggedUpdate ) { 195 $updater->insertUpdateRow( $maint ); 196 } 197 } 198 199 $updater->setFileAccess(); 200 if ( !$this->hasOption( 'nopurge' ) ) { 201 $updater->purgeCache(); 202 } 203 204 $time2 = microtime( true ); 205 206 $timeDiff = $lang->formatTimePeriod( $time2 - $time1 ); 207 $this->output( "\nDone in $timeDiff.\n" ); 208 } 209 210 protected function afterFinalSetup() { 211 global $wgLocalisationCacheConf; 212 213 # Don't try to access the database 214 # This needs to be disabled early since extensions will try to use the l10n 215 # cache from $wgExtensionFunctions (T22471) 216 $wgLocalisationCacheConf = [ 217 'class' => LocalisationCache::class, 218 'storeClass' => LCStoreNull::class, 219 'storeDirectory' => false, 220 'manualRecache' => false, 221 ]; 222 } 223 224 /** 225 * @throws FatalError 226 * @throws MWException 227 * @suppress PhanPluginDuplicateConditionalNullCoalescing 228 */ 229 public function validateParamsAndArgs() { 230 // Allow extensions to add additional params. 231 $params = []; 232 $this->getHookRunner()->onMaintenanceUpdateAddParams( $params ); 233 234 // This executes before the PHP version check, so don't use null coalesce (??). 235 // Keeping this compatible with older PHP versions lets us reach the code that 236 // displays a more helpful error. 237 foreach ( $params as $name => $param ) { 238 $this->addOption( 239 $name, 240 $param['desc'], 241 isset( $param['require'] ) ? $param['require'] : false, 242 isset( $param['withArg'] ) ? $param['withArg'] : false, 243 isset( $param['shortName'] ) ? $param['shortName'] : false, 244 isset( $param['multiOccurrence'] ) ? $param['multiOccurrence'] : false 245 ); 246 } 247 248 parent::validateParamsAndArgs(); 249 } 250} 251 252$maintClass = UpdateMediaWiki::class; 253require_once RUN_MAINTENANCE_IF_MAIN; 254