Merge "Changes list legend modules cleanup"
[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 require dirname( __FILE__ ) . '/../includes/PHPVersionError.php';
30 wfPHPVersionError( 'cli' );
31 }
32
33 $wgUseMasterForMaintenance = true;
34 require_once __DIR__ . '/Maintenance.php';
35
36 /**
37 * Maintenance script to run database schema updates.
38 *
39 * @ingroup Maintenance
40 */
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( 'schema', 'Output SQL to do the schema updates instead of doing them. Works even when $wgAllowSchemaUpdates is false', false, true );
51 $this->addOption( 'force', 'Override when $wgAllowSchemaUpdates disables this script' );
52 }
53
54 function getDbType() {
55 /* If we used the class constant PHP4 would give a parser error here */
56 return 2 /* Maintenance::DB_ADMIN */;
57 }
58
59 function compatChecks() {
60 // Avoid syntax error in PHP4
61 $minimumPcreVersion = constant( 'Installer::MINIMUM_PCRE_VERSION' );
62
63 list( $pcreVersion ) = explode( ' ', PCRE_VERSION, 2 );
64 if ( version_compare( $pcreVersion, $minimumPcreVersion, '<' ) ) {
65 $this->error(
66 "PCRE $minimumPcreVersion or later is required.\n" .
67 "Your PHP binary is linked with PCRE $pcreVersion.\n\n" .
68 "More information:\n" .
69 "https://www.mediawiki.org/wiki/Manual:Errors_and_symptoms/PCRE\n\n" .
70 "ABORTING.\n",
71 true );
72 }
73
74 $test = new PhpXmlBugTester();
75 if ( !$test->ok ) {
76 $this->error(
77 "Your system has a combination of PHP and libxml2 versions that is buggy\n" .
78 "and can cause hidden data corruption in MediaWiki and other web apps.\n" .
79 "Upgrade to libxml2 2.7.3 or later.\n" .
80 "ABORTING (see https://bugs.php.net/bug.php?id=45996).\n",
81 true );
82 }
83 }
84
85 function execute() {
86 global $wgVersion, $wgTitle, $wgLang, $wgAllowSchemaUpdates;
87
88 if ( !$wgAllowSchemaUpdates && !( $this->hasOption( 'force' ) || $this->hasOption( 'schema' ) || $this->hasOption( 'noschema' ) ) ) {
89 $this->error( "Do not run update.php on this wiki. If you're seeing this you should\n"
90 . "probably ask for some help in performing your schema updates or use\n"
91 . "the --noschema and --schema options to get an SQL file for someone\n"
92 . "else to inspect and run.\n\n"
93 . "If you know what you are doing, you can continue with --force\n", true );
94 }
95
96 $this->fileHandle = null;
97 if ( substr( $this->getOption( 'schema' ), 0, 2 ) === "--" ) {
98 $this->error( "The --schema option requires a file as an argument.\n", true );
99 } elseif ( $this->hasOption( 'schema' ) ) {
100 $file = $this->getOption( 'schema' );
101 $this->fileHandle = fopen( $file, "w" );
102 if ( $this->fileHandle === false ) {
103 $err = error_get_last();
104 $this->error( "Problem opening the schema file for writing: $file\n\t{$err['message']}", true );
105 }
106 }
107
108 $wgLang = Language::factory( 'en' );
109 $wgTitle = Title::newFromText( "MediaWiki database updater" );
110
111 define( 'MW_UPDATER', true );
112
113 $this->output( "MediaWiki {$wgVersion} Updater\n\n" );
114
115 wfWaitForSlaves( 5 ); // let's not kill databases, shall we? ;) --tor
116
117 if ( !$this->hasOption( 'skip-compat-checks' ) ) {
118 $this->compatChecks();
119 } else {
120 $this->output( "Skipping compatibility checks, proceed at your own risk (Ctrl+C to abort)\n" );
121 wfCountdown( 5 );
122 }
123
124 # Attempt to connect to the database as a privileged user
125 # This will vomit up an error if there are permissions problems
126 $db = wfGetDB( DB_MASTER );
127
128 $this->output( "Going to run database updates for " . wfWikiID() . "\n" );
129 if ( $db->getType() === 'sqlite' ) {
130 $this->output( "Using SQLite file: '{$db->mDatabaseFile}'\n" );
131 }
132 $this->output( "Depending on the size of your database this may take a while!\n" );
133
134 if ( !$this->hasOption( 'quick' ) ) {
135 $this->output( "Abort with control-c in the next five seconds (skip this countdown with --quick) ... " );
136 wfCountDown( 5 );
137 }
138
139 $time1 = new MWTimestamp();
140
141 $shared = $this->hasOption( 'doshared' );
142
143 $updates = array( 'core', 'extensions' );
144 if ( !$this->hasOption( 'schema' ) ) {
145 if ( $this->hasOption( 'noschema' ) ) {
146 $updates[] = 'noschema';
147 }
148 $updates[] = 'stats';
149 }
150
151 $updater = DatabaseUpdater::newForDb( $db, $shared, $this );
152 $updater->doUpdates( $updates );
153
154 foreach ( $updater->getPostDatabaseUpdateMaintenance() as $maint ) {
155 $child = $this->runChild( $maint );
156
157 // LoggedUpdateMaintenance is checking the updatelog itself
158 $isLoggedUpdate = is_a( $child, 'LoggedUpdateMaintenance' );
159
160 if ( !$isLoggedUpdate && $updater->updateRowExists( $maint ) ) {
161 continue;
162 }
163
164 $child->execute();
165 if ( !$isLoggedUpdate ) {
166 $updater->insertUpdateRow( $maint );
167 }
168 }
169
170 if ( !$this->hasOption( 'nopurge' ) ) {
171 $updater->purgeCache();
172 }
173 $time2 = new MWTimestamp();
174
175 $timeDiff = $time2->diff( $time1 );
176 $this->output( "\nDone in " . $timeDiff->format( "%i:%S" ) . ".\n" );
177 }
178
179 function afterFinalSetup() {
180 global $wgLocalisationCacheConf;
181
182 # Don't try to access the database
183 # This needs to be disabled early since extensions will try to use the l10n
184 # cache from $wgExtensionFunctions (bug 20471)
185 $wgLocalisationCacheConf = array(
186 'class' => 'LocalisationCache',
187 'storeClass' => 'LCStoreNull',
188 'storeDirectory' => false,
189 'manualRecache' => false,
190 );
191 }
192 }
193
194 $maintClass = 'UpdateMediaWiki';
195 require_once RUN_MAINTENANCE_IF_MAIN;