MediaWiki  master
update.php
Go to the documentation of this file.
1 #!/usr/bin/env php
2 <?php
28 // NO_AUTOLOAD -- due to hashbang above
29 
30 require_once __DIR__ . '/Maintenance.php';
31 
34 
41  public function __construct() {
42  parent::__construct();
43  $this->addDescription( 'MediaWiki database updater' );
44  $this->addOption( 'skip-compat-checks', 'Skips compatibility checks, mostly for developers' );
45  $this->addOption( 'quick', 'Skip 5 second countdown before starting' );
46  $this->addOption( 'doshared', 'Also update shared tables' );
47  $this->addOption( 'noschema', 'Only do the updates that are not done during schema updates' );
48  $this->addOption(
49  'schema',
50  'Output SQL to do the schema updates instead of doing them. Works '
51  . 'even when $wgAllowSchemaUpdates is false',
52  false,
53  true
54  );
55  $this->addOption( 'force', 'Override when $wgAllowSchemaUpdates disables this script' );
56  $this->addOption(
57  'skip-external-dependencies',
58  'Skips checking whether external dependencies are up to date, mostly for developers'
59  );
60  }
61 
62  public function getDbType() {
63  return Maintenance::DB_ADMIN;
64  }
65 
66  private function compatChecks() {
67  $minimumPcreVersion = Installer::MINIMUM_PCRE_VERSION;
68 
69  $pcreVersion = explode( ' ', PCRE_VERSION, 2 )[0];
70  if ( version_compare( $pcreVersion, $minimumPcreVersion, '<' ) ) {
71  $this->fatalError(
72  "PCRE $minimumPcreVersion or later is required.\n" .
73  "Your PHP binary is linked with PCRE $pcreVersion.\n\n" .
74  "More information:\n" .
75  "https://www.mediawiki.org/wiki/Manual:Errors_and_symptoms/PCRE\n\n" .
76  "ABORTING.\n" );
77  }
78  }
79 
80  public function execute() {
82 
84  && !( $this->hasOption( 'force' )
85  || $this->hasOption( 'schema' )
86  || $this->hasOption( 'noschema' ) )
87  ) {
88  $this->fatalError( "Do not run update.php on this wiki. If you're seeing this you should\n"
89  . "probably ask for some help in performing your schema updates or use\n"
90  . "the --noschema and --schema options to get an SQL file for someone\n"
91  . "else to inspect and run.\n\n"
92  . "If you know what you are doing, you can continue with --force\n" );
93  }
94 
95  $this->fileHandle = null;
96  if ( substr( $this->getOption( 'schema' ), 0, 2 ) === "--" ) {
97  $this->fatalError( "The --schema option requires a file as an argument.\n" );
98  } elseif ( $this->hasOption( 'schema' ) ) {
99  $file = $this->getOption( 'schema' );
100  $this->fileHandle = fopen( $file, "w" );
101  if ( $this->fileHandle === false ) {
102  $err = error_get_last();
103  $this->fatalError( "Problem opening the schema file for writing: $file\n\t{$err['message']}" );
104  }
105  }
106 
107  // T206765: We need to load the installer i18n files as some of errors come installer/updater code
108  $wgMessagesDirs['MediawikiInstaller'] = dirname( __DIR__ ) . '/includes/installer/i18n';
109 
110  $lang = MediaWikiServices::getInstance()->getLanguageFactory()->getLanguage( 'en' );
111  // Set global language to ensure localised errors are in English (T22633)
112  RequestContext::getMain()->setLanguage( $lang );
113 
114  // BackCompat
115  $wgLang = $lang;
116 
117  define( 'MW_UPDATER', true );
118 
119  $this->output( 'MediaWiki ' . MW_VERSION . " Updater\n\n" );
120 
121  MediaWikiServices::getInstance()->getDBLoadBalancerFactory()->waitForReplication();
122 
123  if ( !$this->hasOption( 'skip-compat-checks' ) ) {
124  $this->compatChecks();
125  } else {
126  $this->output( "Skipping compatibility checks, proceed at your own risk (Ctrl+C to abort)\n" );
127  $this->countDown( 5 );
128  }
129 
130  // Check external dependencies are up to date
131  if ( !$this->hasOption( 'skip-external-dependencies' ) ) {
132  $composerLockUpToDate = $this->runChild( CheckComposerLockUpToDate::class );
133  $composerLockUpToDate->execute();
134  } else {
135  $this->output(
136  "Skipping checking whether external dependencies are up to date, proceed at your own risk\n"
137  );
138  }
139 
140  # Attempt to connect to the database as a privileged user
141  # This will vomit up an error if there are permissions problems
142  $db = $this->getDB( DB_PRIMARY );
143 
144  # Check to see whether the database server meets the minimum requirements
145 
146  $dbInstallerClass = Installer::getDBInstallerClass( $db->getType() );
147  $status = $dbInstallerClass::meetsMinimumRequirement( $db->getServerVersion() );
148  if ( !$status->isOK() ) {
149  // This might output some wikitext like <strong> but it should be comprehensible
150  $text = $status->getWikiText();
151  $this->fatalError( $text );
152  }
153 
154  $dbDomain = WikiMap::getCurrentWikiDbDomain()->getId();
155  $this->output( "Going to run database updates for $dbDomain\n" );
156  if ( $db->getType() === 'sqlite' ) {
158  '@phan-var DatabaseSqlite $db';
159  $this->output( "Using SQLite file: '{$db->getDbFilePath()}'\n" );
160  }
161  $this->output( "Depending on the size of your database this may take a while!\n" );
162 
163  if ( !$this->hasOption( 'quick' ) ) {
164  $this->output( "Abort with control-c in the next five seconds "
165  . "(skip this countdown with --quick) ..." );
166  $this->countDown( 5 );
167  }
168 
169  $time1 = microtime( true );
170 
171  $shared = $this->hasOption( 'doshared' );
172 
173  $updates = [ 'core', 'extensions' ];
174  if ( !$this->hasOption( 'schema' ) ) {
175  if ( $this->hasOption( 'noschema' ) ) {
176  $updates[] = 'noschema';
177  }
178  $updates[] = 'stats';
179  }
180 
181  $updater = DatabaseUpdater::newForDB( $db, $shared, $this );
182 
183  // Avoid upgrading from versions older than 1.27
184  // Using an implicit marker (bot_passwords table didn't exist until 1.27)
185  // TODO: Use an explicit marker
186  // See T259771
187  if ( !$updater->tableExists( 'bot_passwords' ) ) {
188  $this->fatalError(
189  "Can not upgrade from versions older than 1.27, please upgrade to that version or later first."
190  );
191  }
192 
193  $updater->doUpdates( $updates );
194 
195  foreach ( $updater->getPostDatabaseUpdateMaintenance() as $maint ) {
196  $child = $this->runChild( $maint );
197 
198  // LoggedUpdateMaintenance is checking the updatelog itself
199  $isLoggedUpdate = $child instanceof LoggedUpdateMaintenance;
200 
201  if ( !$isLoggedUpdate && $updater->updateRowExists( $maint ) ) {
202  continue;
203  }
204 
205  $child->execute();
206  if ( !$isLoggedUpdate ) {
207  $updater->insertUpdateRow( $maint );
208  }
209  }
210 
211  $updater->setFileAccess();
212 
213  $updater->purgeCache();
214 
215  $time2 = microtime( true );
216 
217  $timeDiff = $lang->formatTimePeriod( $time2 - $time1 );
218  $this->output( "\nDone in $timeDiff.\n" );
219  }
220 
221  protected function afterFinalSetup() {
223 
224  # Don't try to access the database
225  # This needs to be disabled early since extensions will try to use the l10n
226  # cache from $wgExtensionFunctions (T22471)
228  'class' => LocalisationCache::class,
229  'storeClass' => LCStoreNull::class,
230  'storeDirectory' => false,
231  'manualRecache' => false,
232  ];
233  }
234 
240  public function validateParamsAndArgs() {
241  // Allow extensions to add additional params.
242  $params = [];
243  $this->getHookRunner()->onMaintenanceUpdateAddParams( $params );
244 
245  // This executes before the PHP version check, so don't use null coalesce (??).
246  // Keeping this compatible with older PHP versions lets us reach the code that
247  // displays a more helpful error.
248  foreach ( $params as $name => $param ) {
249  $this->addOption(
250  $name,
251  $param['desc'],
252  isset( $param['require'] ) ? $param['require'] : false,
253  isset( $param['withArg'] ) ? $param['withArg'] : false,
254  isset( $param['shortName'] ) ? $param['shortName'] : false,
255  isset( $param['multiOccurrence'] ) ? $param['multiOccurrence'] : false
256  );
257  }
258 
259  parent::validateParamsAndArgs();
260  }
261 }
262 
263 $maintClass = UpdateMediaWiki::class;
264 require_once RUN_MAINTENANCE_IF_MAIN;
UpdateMediaWiki\getDbType
getDbType()
Does the script need different DB access? By default, we give Maintenance scripts normal rights to th...
Definition: update.php:62
DatabaseUpdater\newForDB
static newForDB(IMaintainableDatabase $db, $shared=false, Maintenance $maintenance=null)
Definition: DatabaseUpdater.php:205
WikiMap\getCurrentWikiDbDomain
static getCurrentWikiDbDomain()
Definition: WikiMap.php:293
Wikimedia\Rdbms\DatabaseSqlite
Definition: DatabaseSqlite.php:36
UpdateMediaWiki
Maintenance script to run database schema updates.
Definition: update.php:40
MediaWiki\MediaWikiServices
MediaWikiServices is the service locator for the application scope of MediaWiki.
Definition: MediaWikiServices.php:193
$lang
if(!isset( $args[0])) $lang
Definition: testCompression.php:37
Maintenance\fatalError
fatalError( $msg, $exitCode=1)
Output a message and terminate the current script.
Definition: Maintenance.php:489
Maintenance\addDescription
addDescription( $text)
Set the description text.
Definition: Maintenance.php:329
Maintenance\runChild
runChild( $maintClass, $classFile=null)
Run a child maintenance script.
Definition: Maintenance.php:673
MW_VERSION
const MW_VERSION
The running version of MediaWiki.
Definition: Defines.php:36
$wgMessagesDirs
$wgMessagesDirs
Extension messages directories.
Definition: DefaultSettings.php:8510
$file
if(PHP_SAPI !='cli-server') if(!isset( $_SERVER['SCRIPT_FILENAME'])) $file
Item class for a filearchive table row.
Definition: router.php:42
$wgAllowSchemaUpdates
$wgAllowSchemaUpdates
Allow schema updates.
Definition: DefaultSettings.php:2649
Maintenance
Abstract maintenance class for quickly writing and churning out maintenance scripts with minimal effo...
Definition: Maintenance.php:59
$wgLang
$wgLang
Definition: Setup.php:834
UpdateMediaWiki\__construct
__construct()
Default constructor.
Definition: update.php:41
LoggedUpdateMaintenance
Class for scripts that perform database maintenance and want to log the update in updatelog so we can...
Definition: LoggedUpdateMaintenance.php:26
Maintenance\addOption
addOption( $name, $description, $required=false, $withArg=false, $shortName=false, $multiOccurrence=false)
Add a parameter to the script.
Definition: Maintenance.php:249
$wgLocalisationCacheConf
$wgLocalisationCacheConf
Localisation cache configuration.
Definition: DefaultSettings.php:3081
Maintenance\countDown
countDown( $seconds)
Count down from $seconds to zero on the terminal, with a one-second pause between showing each number...
Definition: Maintenance.php:1452
Maintenance\DB_ADMIN
const DB_ADMIN
Definition: Maintenance.php:66
LoggedUpdateMaintenance\execute
execute()
Do the actual work.
Definition: LoggedUpdateMaintenance.php:33
DB_PRIMARY
const DB_PRIMARY
Definition: defines.php:27
UpdateMediaWiki\afterFinalSetup
afterFinalSetup()
Execute a callback function at the end of initialisation @stable to override.
Definition: update.php:221
UpdateMediaWiki\compatChecks
compatChecks()
Definition: update.php:66
Maintenance\getHookRunner
getHookRunner()
Get a HookRunner for running core hooks.
Definition: Maintenance.php:1624
Maintenance\getDB
getDB( $db, $groups=[], $dbDomain=false)
Returns a database to be used by current maintenance script.
Definition: Maintenance.php:1362
RequestContext\getMain
static getMain()
Get the RequestContext object associated with the main request.
Definition: RequestContext.php:484
$maintClass
$maintClass
Definition: update.php:263
Maintenance\getOption
getOption( $name, $default=null)
Get an option, or return the default.
Definition: Maintenance.php:286
UpdateMediaWiki\execute
execute()
Do the actual work.
Definition: update.php:80
Maintenance\output
output( $out, $channel=null)
Throw some output to the user.
Definition: Maintenance.php:435
Installer\MINIMUM_PCRE_VERSION
const MINIMUM_PCRE_VERSION
The oldest version of PCRE we can support.
Definition: Installer.php:61
Maintenance\hasOption
hasOption( $name)
Checks to see if a particular option was set.
Definition: Maintenance.php:271
UpdateMediaWiki\validateParamsAndArgs
validateParamsAndArgs()
Definition: update.php:240
Installer\getDBInstallerClass
static getDBInstallerClass( $type)
Get the DatabaseInstaller class name for this type.
Definition: Installer.php:598