Merge "TextContent: Document parameters to convert()"
[mediawiki.git] / maintenance / update.php
blobf96ee39a3ba184ec238323e43fe9c5c642418e83
1 <?php
2 /**
3 * Run all updaters.
5 * This is used when the database schema is modified and we need to apply patches.
6 * It is kept compatible with php 4 parsing so that it can give out a meaningful error.
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.
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.
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
23 * @file
24 * @todo document
25 * @ingroup Maintenance
28 if ( !function_exists( 'version_compare' ) || ( version_compare( phpversion(), '5.3.2' ) < 0 ) ) {
29 require dirname( __FILE__ ) . '/../includes/PHPVersionError.php';
30 wfPHPVersionError( 'cli' );
33 $wgUseMasterForMaintenance = true;
34 require_once __DIR__ . '/Maintenance.php';
36 /**
37 * Maintenance script to run database schema updates.
39 * @ingroup Maintenance
41 class UpdateMediaWiki extends Maintenance {
42 function __construct() {
43 parent::__construct();
44 $this->mDescription = "MediaWiki database updater";
45 $this->addOption( 'skip-compat-checks', 'Skips compatibility checks, mostly for developers' );
46 $this->addOption( 'quick', 'Skip 5 second countdown before starting' );
47 $this->addOption( 'doshared', 'Also update shared tables' );
48 $this->addOption( 'nopurge', 'Do not purge the objectcache table after updates' );
49 $this->addOption( 'noschema', 'Only do the updates that are not done during schema updates' );
50 $this->addOption(
51 'schema',
52 'Output SQL to do the schema updates instead of doing them. Works '
53 . 'even when $wgAllowSchemaUpdates is false',
54 false,
55 true
57 $this->addOption( 'force', 'Override when $wgAllowSchemaUpdates disables this script' );
60 function getDbType() {
61 /* If we used the class constant PHP4 would give a parser error here */
62 return 2; /* Maintenance::DB_ADMIN */
65 function compatChecks() {
66 // Avoid syntax error in PHP4
67 $minimumPcreVersion = constant( 'Installer::MINIMUM_PCRE_VERSION' );
69 list( $pcreVersion ) = explode( ' ', PCRE_VERSION, 2 );
70 if ( version_compare( $pcreVersion, $minimumPcreVersion, '<' ) ) {
71 $this->error(
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 true );
80 $test = new PhpXmlBugTester();
81 if ( !$test->ok ) {
82 $this->error(
83 "Your system has a combination of PHP and libxml2 versions that is buggy\n" .
84 "and can cause hidden data corruption in MediaWiki and other web apps.\n" .
85 "Upgrade to libxml2 2.7.3 or later.\n" .
86 "ABORTING (see https://bugs.php.net/bug.php?id=45996).\n",
87 true );
91 function execute() {
92 global $wgVersion, $wgLang, $wgAllowSchemaUpdates;
94 if ( !$wgAllowSchemaUpdates
95 && !( $this->hasOption( 'force' )
96 || $this->hasOption( 'schema' )
97 || $this->hasOption( 'noschema' ) )
98 ) {
99 $this->error( "Do not run update.php on this wiki. If you're seeing this you should\n"
100 . "probably ask for some help in performing your schema updates or use\n"
101 . "the --noschema and --schema options to get an SQL file for someone\n"
102 . "else to inspect and run.\n\n"
103 . "If you know what you are doing, you can continue with --force\n", true );
106 $this->fileHandle = null;
107 if ( substr( $this->getOption( 'schema' ), 0, 2 ) === "--" ) {
108 $this->error( "The --schema option requires a file as an argument.\n", true );
109 } elseif ( $this->hasOption( 'schema' ) ) {
110 $file = $this->getOption( 'schema' );
111 $this->fileHandle = fopen( $file, "w" );
112 if ( $this->fileHandle === false ) {
113 $err = error_get_last();
114 $this->error( "Problem opening the schema file for writing: $file\n\t{$err['message']}", true );
118 $wgLang = Language::factory( 'en' );
120 define( 'MW_UPDATER', true );
122 $this->output( "MediaWiki {$wgVersion} Updater\n\n" );
124 wfWaitForSlaves( 5 ); // let's not kill databases, shall we? ;) --tor
126 if ( !$this->hasOption( 'skip-compat-checks' ) ) {
127 $this->compatChecks();
128 } else {
129 $this->output( "Skipping compatibility checks, proceed at your own risk (Ctrl+C to abort)\n" );
130 wfCountdown( 5 );
133 # Attempt to connect to the database as a privileged user
134 # This will vomit up an error if there are permissions problems
135 $db = wfGetDB( DB_MASTER );
137 $this->output( "Going to run database updates for " . wfWikiID() . "\n" );
138 if ( $db->getType() === 'sqlite' ) {
139 $this->output( "Using SQLite file: '{$db->mDatabaseFile}'\n" );
141 $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 wfCountDown( 5 );
149 $time1 = new MWTimestamp();
151 $shared = $this->hasOption( 'doshared' );
153 $updates = array( 'core', 'extensions' );
154 if ( !$this->hasOption( 'schema' ) ) {
155 if ( $this->hasOption( 'noschema' ) ) {
156 $updates[] = 'noschema';
158 $updates[] = 'stats';
161 $updater = DatabaseUpdater::newForDb( $db, $shared, $this );
162 $updater->doUpdates( $updates );
164 foreach ( $updater->getPostDatabaseUpdateMaintenance() as $maint ) {
165 $child = $this->runChild( $maint );
167 // LoggedUpdateMaintenance is checking the updatelog itself
168 $isLoggedUpdate = is_a( $child, 'LoggedUpdateMaintenance' );
170 if ( !$isLoggedUpdate && $updater->updateRowExists( $maint ) ) {
171 continue;
174 $child->execute();
175 if ( !$isLoggedUpdate ) {
176 $updater->insertUpdateRow( $maint );
180 if ( !$this->hasOption( 'nopurge' ) ) {
181 $updater->purgeCache();
183 $time2 = new MWTimestamp();
185 $timeDiff = $time2->diff( $time1 );
186 $this->output( "\nDone in " . $timeDiff->format( "%i:%S" ) . ".\n" );
189 function afterFinalSetup() {
190 global $wgLocalisationCacheConf;
192 # Don't try to access the database
193 # This needs to be disabled early since extensions will try to use the l10n
194 # cache from $wgExtensionFunctions (bug 20471)
195 $wgLocalisationCacheConf = array(
196 'class' => 'LocalisationCache',
197 'storeClass' => 'LCStoreNull',
198 'storeDirectory' => false,
199 'manualRecache' => false,
204 $maintClass = 'UpdateMediaWiki';
205 require_once RUN_MAINTENANCE_IF_MAIN;