MediaWiki  master
update.php
Go to the documentation of this file.
1 #!/usr/bin/env php
2 <?php
28 require_once __DIR__ . '/Maintenance.php';
29 
32 
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() {
81 
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 
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' ) {
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() {
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)
217  'class' => LocalisationCache::class,
218  'storeClass' => LCStoreNull::class,
219  'storeDirectory' => false,
220  'manualRecache' => false,
221  ];
222  }
223 
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;
253 require_once RUN_MAINTENANCE_IF_MAIN;
RUN_MAINTENANCE_IF_MAIN
const RUN_MAINTENANCE_IF_MAIN
Definition: Maintenance.php:38
UpdateMediaWiki\getDbType
getDbType()
Does the script need different DB access? By default, we give Maintenance scripts normal rights to th...
Definition: update.php:61
DatabaseUpdater\newForDB
static newForDB(IMaintainableDatabase $db, $shared=false, Maintenance $maintenance=null)
Definition: DatabaseUpdater.php:194
WikiMap\getCurrentWikiDbDomain
static getCurrentWikiDbDomain()
Definition: WikiMap.php:293
Wikimedia\Rdbms\DatabaseSqlite
Definition: DatabaseSqlite.php:37
UpdateMediaWiki
Maintenance script to run database schema updates.
Definition: update.php:38
MediaWiki\MediaWikiServices
MediaWikiServices is the service locator for the application scope of MediaWiki.
Definition: MediaWikiServices.php:154
$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:487
Maintenance\addDescription
addDescription( $text)
Set the description text.
Definition: Maintenance.php:327
Maintenance\runChild
runChild( $maintClass, $classFile=null)
Run a child maintenance script.
Definition: Maintenance.php:710
MW_VERSION
const MW_VERSION
The running version of MediaWiki.
Definition: Defines.php:39
$wgMessagesDirs
$wgMessagesDirs
Extension messages directories.
Definition: DefaultSettings.php:7756
$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:2402
Maintenance
Abstract maintenance class for quickly writing and churning out maintenance scripts with minimal effo...
Definition: Maintenance.php:55
UpdateMediaWiki\__construct
__construct()
Default constructor.
Definition: update.php:39
LoggedUpdateMaintenance
Class for scripts that perform database maintenance and want to log the update in updatelog so we can...
Definition: LoggedUpdateMaintenance.php:26
$wgLang
$wgLang
Definition: Setup.php:776
Maintenance\addOption
addOption( $name, $description, $required=false, $withArg=false, $shortName=false, $multiOccurrence=false)
Add a parameter to the script.
Definition: Maintenance.php:245
$wgLocalisationCacheConf
$wgLocalisationCacheConf
Localisation cache configuration.
Definition: DefaultSettings.php:2754
DB_MASTER
const DB_MASTER
Definition: defines.php:26
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:1544
Maintenance\DB_ADMIN
const DB_ADMIN
Definition: Maintenance.php:62
LoggedUpdateMaintenance\execute
execute()
Do the actual work.
Definition: LoggedUpdateMaintenance.php:33
UpdateMediaWiki\afterFinalSetup
afterFinalSetup()
Execute a callback function at the end of initialisation @stable for overriding.
Definition: update.php:210
UpdateMediaWiki\compatChecks
compatChecks()
Definition: update.php:65
Maintenance\getHookRunner
getHookRunner()
Get a HookRunner for running core hooks.
Definition: Maintenance.php:1706
Maintenance\getDB
getDB( $db, $groups=[], $dbDomain=false)
Returns a database to be used by current maintenance script.
Definition: Maintenance.php:1366
RequestContext\getMain
static getMain()
Get the RequestContext object associated with the main request.
Definition: RequestContext.php:453
$maintClass
$maintClass
Definition: update.php:252
Maintenance\getOption
getOption( $name, $default=null)
Get an option, or return the default.
Definition: Maintenance.php:281
UpdateMediaWiki\execute
execute()
Do the actual work.
Definition: update.php:79
Maintenance\output
output( $out, $channel=null)
Throw some output to the user.
Definition: Maintenance.php:434
Installer\MINIMUM_PCRE_VERSION
const MINIMUM_PCRE_VERSION
The oldest version of PCRE we can support.
Definition: Installer.php:60
Maintenance\hasOption
hasOption( $name)
Checks to see if a particular option exists.
Definition: Maintenance.php:266
UpdateMediaWiki\validateParamsAndArgs
validateParamsAndArgs()
Definition: update.php:229
Installer\getDBInstallerClass
static getDBInstallerClass( $type)
Get the DatabaseInstaller class name for this type.
Definition: Installer.php:567