6 * This is used when the database schema is modified and we need to apply patches.
7 * It is kept compatible with php 4 parsing so that it can give out a meaningful error.
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License along
20 * with this program; if not, write to the Free Software Foundation, Inc.,
21 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
22 * http://www.gnu.org/copyleft/gpl.html
26 * @ingroup Maintenance
29 if ( !function_exists( 'version_compare' ) ||
( version_compare( PHP_VERSION
, '5.3.3' ) < 0 ) ) {
30 require dirname( __FILE__
) . '/../includes/PHPVersionError.php';
31 wfPHPVersionError( 'cli' );
34 $wgUseMasterForMaintenance = true;
35 require_once __DIR__
. '/Maintenance.php';
38 * Maintenance script to run database schema updates.
40 * @ingroup Maintenance
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' );
53 'Output SQL to do the schema updates instead of doing them. Works '
54 . 'even when $wgAllowSchemaUpdates is false',
58 $this->addOption( 'force', 'Override when $wgAllowSchemaUpdates disables this script' );
61 function getDbType() {
62 /* If we used the class constant PHP4 would give a parser error here */
63 return 2; /* Maintenance::DB_ADMIN */
66 function compatChecks() {
67 // Avoid syntax error in PHP4
68 $minimumPcreVersion = constant( 'Installer::MINIMUM_PCRE_VERSION' );
70 list( $pcreVersion ) = explode( ' ', PCRE_VERSION
, 2 );
71 if ( version_compare( $pcreVersion, $minimumPcreVersion, '<' ) ) {
73 "PCRE $minimumPcreVersion or later is required.\n" .
74 "Your PHP binary is linked with PCRE $pcreVersion.\n\n" .
75 "More information:\n" .
76 "https://www.mediawiki.org/wiki/Manual:Errors_and_symptoms/PCRE\n\n" .
81 $test = new PhpXmlBugTester();
84 "Your system has a combination of PHP and libxml2 versions that is buggy\n" .
85 "and can cause hidden data corruption in MediaWiki and other web apps.\n" .
86 "Upgrade to libxml2 2.7.3 or later.\n" .
87 "ABORTING (see https://bugs.php.net/bug.php?id=45996).\n",
93 global $wgVersion, $wgLang, $wgAllowSchemaUpdates;
95 if ( !$wgAllowSchemaUpdates
96 && !( $this->hasOption( 'force' )
97 ||
$this->hasOption( 'schema' )
98 ||
$this->hasOption( 'noschema' ) )
100 $this->error( "Do not run update.php on this wiki. If you're seeing this you should\n"
101 . "probably ask for some help in performing your schema updates or use\n"
102 . "the --noschema and --schema options to get an SQL file for someone\n"
103 . "else to inspect and run.\n\n"
104 . "If you know what you are doing, you can continue with --force\n", true );
107 $this->fileHandle
= null;
108 if ( substr( $this->getOption( 'schema' ), 0, 2 ) === "--" ) {
109 $this->error( "The --schema option requires a file as an argument.\n", true );
110 } elseif ( $this->hasOption( 'schema' ) ) {
111 $file = $this->getOption( 'schema' );
112 $this->fileHandle
= fopen( $file, "w" );
113 if ( $this->fileHandle
=== false ) {
114 $err = error_get_last();
115 $this->error( "Problem opening the schema file for writing: $file\n\t{$err['message']}", true );
119 $wgLang = Language
::factory( 'en' );
121 define( 'MW_UPDATER', true );
123 $this->output( "MediaWiki {$wgVersion} Updater\n\n" );
125 wfWaitForSlaves( 5 ); // let's not kill databases, shall we? ;) --tor
127 if ( !$this->hasOption( 'skip-compat-checks' ) ) {
128 $this->compatChecks();
130 $this->output( "Skipping compatibility checks, proceed at your own risk (Ctrl+C to abort)\n" );
134 # Attempt to connect to the database as a privileged user
135 # This will vomit up an error if there are permissions problems
136 $db = wfGetDB( DB_MASTER
);
138 $this->output( "Going to run database updates for " . wfWikiID() . "\n" );
139 if ( $db->getType() === 'sqlite' ) {
140 $this->output( "Using SQLite file: '{$db->mDatabaseFile}'\n" );
142 $this->output( "Depending on the size of your database this may take a while!\n" );
144 if ( !$this->hasOption( 'quick' ) ) {
145 $this->output( "Abort with control-c in the next five seconds "
146 . "(skip this countdown with --quick) ... " );
150 $time1 = new MWTimestamp();
152 $shared = $this->hasOption( 'doshared' );
154 $updates = array( 'core', 'extensions' );
155 if ( !$this->hasOption( 'schema' ) ) {
156 if ( $this->hasOption( 'noschema' ) ) {
157 $updates[] = 'noschema';
159 $updates[] = 'stats';
162 $updater = DatabaseUpdater
::newForDb( $db, $shared, $this );
163 $updater->doUpdates( $updates );
165 foreach ( $updater->getPostDatabaseUpdateMaintenance() as $maint ) {
166 $child = $this->runChild( $maint );
168 // LoggedUpdateMaintenance is checking the updatelog itself
169 $isLoggedUpdate = is_a( $child, 'LoggedUpdateMaintenance' );
171 if ( !$isLoggedUpdate && $updater->updateRowExists( $maint ) ) {
176 if ( !$isLoggedUpdate ) {
177 $updater->insertUpdateRow( $maint );
181 $updater->setFileAccess();
182 if ( !$this->hasOption( 'nopurge' ) ) {
183 $updater->purgeCache();
186 $time2 = new MWTimestamp();
187 $timeDiff = $time2->diff( $time1 );
188 $this->output( "\nDone in " . $timeDiff->format( "%i:%S" ) . ".\n" );
191 function afterFinalSetup() {
192 global $wgLocalisationCacheConf;
194 # Don't try to access the database
195 # This needs to be disabled early since extensions will try to use the l10n
196 # cache from $wgExtensionFunctions (bug 20471)
197 $wgLocalisationCacheConf = array(
198 'class' => 'LocalisationCache',
199 'storeClass' => 'LCStoreNull',
200 'storeDirectory' => false,
201 'manualRecache' => false,
206 $maintClass = 'UpdateMediaWiki';
207 require_once RUN_MAINTENANCE_IF_MAIN
;