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