(bug 38110) provide a way to separate out schema changes
[lhc/web/wiklou.git] / maintenance / update.php
1 <?php
2 /**
3 * Run all updaters.
4 *
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.
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
28 if ( !function_exists( 'version_compare' ) || ( version_compare( phpversion(), '5.3.2' ) < 0 ) ) {
29 echo "You are using PHP version " . phpversion() . " but MediaWiki needs PHP 5.3.2 or higher. ABORTING.\n" .
30 "Check if you have a newer php executable with a different name, such as php5.\n";
31 die( 1 );
32 }
33
34 $wgUseMasterForMaintenance = true;
35 require_once( __DIR__ . '/Maintenance.php' );
36
37 /**
38 * Maintenance script to run database schema updates.
39 *
40 * @ingroup Maintenance
41 */
42 class UpdateMediaWiki extends Maintenance {
43 function __construct() {
44 parent::__construct();
45 $this->mDescription = "MediaWiki database updater";
46 $this->addOption( 'skip-compat-checks', 'Skips compatibility checks, mostly for developers' );
47 $this->addOption( 'quick', 'Skip 5 second countdown before starting' );
48 $this->addOption( 'doshared', 'Also update shared tables' );
49 $this->addOption( 'nopurge', 'Do not purge the objectcache table after updates' );
50 $this->addOption( 'noschema', 'Only do the updates that are not done during schema updates' );
51 $this->addOption( 'schema', 'Output SQL to do the schema updates instead of doing them. Works even when $wgAllowSchemaUpdates is false', false, true );
52 $this->addOption( 'force', 'Override when $wgAllowSchemaUpdates disables this script' );
53 }
54
55 function getDbType() {
56 /* If we used the class constant PHP4 would give a parser error here */
57 return 2 /* Maintenance::DB_ADMIN */;
58 }
59
60 function compatChecks() {
61 $test = new PhpXmlBugTester();
62 if ( !$test->ok ) {
63 $this->error(
64 "Your system has a combination of PHP and libxml2 versions which is buggy\n" .
65 "and can cause hidden data corruption in MediaWiki and other web apps.\n" .
66 "Upgrade to PHP 5.2.9 or later and libxml2 2.7.3 or later!\n" .
67 "ABORTING (see http://bugs.php.net/bug.php?id=45996).\n",
68 true );
69 }
70
71 $test = new PhpRefCallBugTester;
72 $test->execute();
73 if ( !$test->ok ) {
74 $ver = phpversion();
75 $this->error(
76 "PHP $ver is not compatible with MediaWiki due to a bug involving\n" .
77 "reference parameters to __call. Upgrade to PHP 5.3.2 or higher, or \n" .
78 "downgrade to PHP 5.3.0 to fix this.\n" .
79 "ABORTING (see http://bugs.php.net/bug.php?id=50394 for details)\n",
80 true );
81 }
82 }
83
84 function execute() {
85 global $wgVersion, $wgTitle, $wgLang, $wgAllowSchemaUpdates;
86
87 if( !$wgAllowSchemaUpdates && !( $this->hasOption( 'force' ) || $this->hasOption( 'schema' ) || $this->hasOption( 'noschema' ) ) ) {
88 $this->error( "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", true );
93 }
94
95 $this->fileHandle = null;
96 if( substr( $this->getOption( 'schema' ), 0, 2 ) === "--" ) {
97 $this->error( "The --schema option requires a file as an argument.\n", true );
98 } else if( $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->error( "Problem opening the schema file for writing: $file\n\t{$err['message']}", true );
104 }
105 }
106
107 $wgLang = Language::factory( 'en' );
108 $wgTitle = Title::newFromText( "MediaWiki database updater" );
109
110 $this->output( "MediaWiki {$wgVersion} Updater\n\n" );
111
112 wfWaitForSlaves( 5 ); // let's not kill databases, shall we? ;) --tor
113
114 if ( !$this->hasOption( 'skip-compat-checks' ) ) {
115 $this->compatChecks();
116 } else {
117 $this->output( "Skipping compatibility checks, proceed at your own risk (Ctrl+C to abort)\n" );
118 wfCountdown( 5 );
119 }
120
121 # Attempt to connect to the database as a privileged user
122 # This will vomit up an error if there are permissions problems
123 $db = wfGetDB( DB_MASTER );
124
125 $this->output( "Going to run database updates for " . wfWikiID() . "\n" );
126 if( $db->getType() === 'sqlite' ) {
127 $this->output( "Using SQLite file: '{$db->mDatabaseFile}'\n" );
128 }
129 $this->output( "Depending on the size of your database this may take a while!\n" );
130
131 if ( !$this->hasOption( 'quick' ) ) {
132 $this->output( "Abort with control-c in the next five seconds (skip this countdown with --quick) ... " );
133 wfCountDown( 5 );
134 }
135
136 $shared = $this->hasOption( 'doshared' );
137
138 $updates = array( 'core', 'extensions' );
139 if( !$this->hasOption('schema') ) {
140 if( $this->hasOption('noschema') ) {
141 $updates[] = 'noschema';
142 }
143 $updates[] = 'stats';
144
145 if( !$this->hasOption('nopurge') ) {
146 $updates[] = 'purge';
147 }
148 }
149
150 $updater = DatabaseUpdater::newForDb( $db, $shared, $this );
151 $updater->doUpdates( $updates );
152
153 foreach( $updater->getPostDatabaseUpdateMaintenance() as $maint ) {
154 $child = $this->runChild( $maint );
155
156 // LoggedUpdateMaintenance is checking the updatelog itself
157 $isLoggedUpdate = ( $child instanceof LoggedUpdateMaintenance );
158
159 if ( !$isLoggedUpdate && $updater->updateRowExists( $maint ) ) {
160 continue;
161 }
162
163 $child = $this->runChild( $maint );
164 $child->execute();
165 if ( !$isLoggedUpdate ) {
166 $updater->insertUpdateRow( $maint );
167 }
168 }
169
170 if( !$this->hasOption('nopurge') ) {
171 $updater->purgeCache();
172 }
173
174 $this->output( "\nDone.\n" );
175 }
176
177 function afterFinalSetup() {
178 global $wgLocalisationCacheConf;
179
180 # Don't try to access the database
181 # This needs to be disabled early since extensions will try to use the l10n
182 # cache from $wgExtensionFunctions (bug 20471)
183 $wgLocalisationCacheConf = array(
184 'class' => 'LocalisationCache',
185 'storeClass' => 'LCStore_Null',
186 'storeDirectory' => false,
187 'manualRecache' => false,
188 );
189 }
190 }
191
192 $maintClass = 'UpdateMediaWiki';
193 require_once( RUN_MAINTENANCE_IF_MAIN );