Merge "Fix call to non-existing TempFSFileFactory::getTempFSFile()"
[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 *
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 require_once __DIR__ . '/Maintenance.php';
29
30 use Wikimedia\Rdbms\IMaintainableDatabase;
31 use Wikimedia\Rdbms\DatabaseSqlite;
32
33 /**
34 * Maintenance script to run database schema updates.
35 *
36 * @ingroup Maintenance
37 */
38 class UpdateMediaWiki extends Maintenance {
39 function __construct() {
40 parent::__construct();
41 $this->addDescription( 'MediaWiki database updater' );
42 $this->addOption( 'skip-compat-checks', 'Skips compatibility checks, mostly for developers' );
43 $this->addOption( 'quick', 'Skip 5 second countdown before starting' );
44 $this->addOption( 'doshared', 'Also update shared tables' );
45 $this->addOption( 'nopurge', 'Do not purge the objectcache table after updates' );
46 $this->addOption( 'noschema', 'Only do the updates that are not done during schema updates' );
47 $this->addOption(
48 'schema',
49 'Output SQL to do the schema updates instead of doing them. Works '
50 . 'even when $wgAllowSchemaUpdates is false',
51 false,
52 true
53 );
54 $this->addOption( 'force', 'Override when $wgAllowSchemaUpdates disables this script' );
55 $this->addOption(
56 'skip-external-dependencies',
57 'Skips checking whether external dependencies are up to date, mostly for developers'
58 );
59 }
60
61 function getDbType() {
62 return Maintenance::DB_ADMIN;
63 }
64
65 function compatChecks() {
66 $minimumPcreVersion = Installer::MINIMUM_PCRE_VERSION;
67
68 $pcreVersion = explode( ' ', PCRE_VERSION, 2 )[0];
69 if ( version_compare( $pcreVersion, $minimumPcreVersion, '<' ) ) {
70 $this->fatalError(
71 "PCRE $minimumPcreVersion or later is required.\n" .
72 "Your PHP binary is linked with PCRE $pcreVersion.\n\n" .
73 "More information:\n" .
74 "https://www.mediawiki.org/wiki/Manual:Errors_and_symptoms/PCRE\n\n" .
75 "ABORTING.\n" );
76 }
77
78 $test = new PhpXmlBugTester();
79 if ( !$test->ok ) {
80 $this->fatalError(
81 "Your system has a combination of PHP and libxml2 versions that is buggy\n" .
82 "and can cause hidden data corruption in MediaWiki and other web apps.\n" .
83 "Upgrade to libxml2 2.7.3 or later.\n" .
84 "ABORTING (see https://bugs.php.net/bug.php?id=45996).\n" );
85 }
86 }
87
88 function execute() {
89 global $wgVersion, $wgLang, $wgAllowSchemaUpdates, $wgMessagesDirs;
90
91 if ( !$wgAllowSchemaUpdates
92 && !( $this->hasOption( 'force' )
93 || $this->hasOption( 'schema' )
94 || $this->hasOption( 'noschema' ) )
95 ) {
96 $this->fatalError( "Do not run update.php on this wiki. If you're seeing this you should\n"
97 . "probably ask for some help in performing your schema updates or use\n"
98 . "the --noschema and --schema options to get an SQL file for someone\n"
99 . "else to inspect and run.\n\n"
100 . "If you know what you are doing, you can continue with --force\n" );
101 }
102
103 $this->fileHandle = null;
104 if ( substr( $this->getOption( 'schema' ), 0, 2 ) === "--" ) {
105 $this->fatalError( "The --schema option requires a file as an argument.\n" );
106 } elseif ( $this->hasOption( 'schema' ) ) {
107 $file = $this->getOption( 'schema' );
108 $this->fileHandle = fopen( $file, "w" );
109 if ( $this->fileHandle === false ) {
110 $err = error_get_last();
111 $this->fatalError( "Problem opening the schema file for writing: $file\n\t{$err['message']}" );
112 }
113 }
114
115 // T206765: We need to load the installer i18n files as some of errors come installer/updater code
116 $wgMessagesDirs['MediawikiInstaller'] = dirname( __DIR__ ) . '/includes/installer/i18n';
117
118 $lang = Language::factory( 'en' );
119 // Set global language to ensure localised errors are in English (T22633)
120 RequestContext::getMain()->setLanguage( $lang );
121 $wgLang = $lang; // BackCompat
122
123 define( 'MW_UPDATER', true );
124
125 $this->output( "MediaWiki {$wgVersion} Updater\n\n" );
126
127 wfWaitForSlaves();
128
129 if ( !$this->hasOption( 'skip-compat-checks' ) ) {
130 $this->compatChecks();
131 } else {
132 $this->output( "Skipping compatibility checks, proceed at your own risk (Ctrl+C to abort)\n" );
133 $this->countDown( 5 );
134 }
135
136 // Check external dependencies are up to date
137 if ( !$this->hasOption( 'skip-external-dependencies' ) ) {
138 $composerLockUpToDate = $this->runChild( CheckComposerLockUpToDate::class );
139 $composerLockUpToDate->execute();
140 } else {
141 $this->output(
142 "Skipping checking whether external dependencies are up to date, proceed at your own risk\n"
143 );
144 }
145
146 # Attempt to connect to the database as a privileged user
147 # This will vomit up an error if there are permissions problems
148 $db = $this->getDB( DB_MASTER );
149
150 # Check to see whether the database server meets the minimum requirements
151 /** @var DatabaseInstaller $dbInstallerClass */
152 $dbInstallerClass = Installer::getDBInstallerClass( $db->getType() );
153 $status = $dbInstallerClass::meetsMinimumRequirement( $db->getServerVersion() );
154 if ( !$status->isOK() ) {
155 // This might output some wikitext like <strong> but it should be comprehensible
156 $text = $status->getWikiText();
157 $this->fatalError( $text );
158 }
159
160 $dbDomain = WikiMap::getCurrentWikiDbDomain()->getId();
161 $this->output( "Going to run database updates for $dbDomain\n" );
162 if ( $db->getType() === 'sqlite' ) {
163 /** @var IMaintainableDatabase|DatabaseSqlite $db */
164 $this->output( "Using SQLite file: '{$db->getDbFilePath()}'\n" );
165 }
166 $this->output( "Depending on the size of your database this may take a while!\n" );
167
168 if ( !$this->hasOption( 'quick' ) ) {
169 $this->output( "Abort with control-c in the next five seconds "
170 . "(skip this countdown with --quick) ... " );
171 $this->countDown( 5 );
172 }
173
174 $time1 = microtime( true );
175
176 $badPhpUnit = dirname( __DIR__ ) . '/vendor/phpunit/phpunit/src/Util/PHP/eval-stdin.php';
177 if ( file_exists( $badPhpUnit ) ) {
178 // Bad versions of the file are:
179 // https://raw.githubusercontent.com/sebastianbergmann/phpunit/c820f915bfae34e5a836f94967a2a5ea5ef34f21/src/Util/PHP/eval-stdin.php
180 // https://raw.githubusercontent.com/sebastianbergmann/phpunit/3aaddb1c5bd9b9b8d070b4cf120e71c36fd08412/src/Util/PHP/eval-stdin.php
181 $md5 = md5_file( $badPhpUnit );
182 if ( $md5 === '120ac49800671dc383b6f3709c25c099'
183 || $md5 === '28af792cb38fc9a1b236b91c1aad2876'
184 ) {
185 $success = unlink( $badPhpUnit );
186 if ( $success ) {
187 $this->output( "Removed PHPUnit eval-stdin.php to protect against CVE-2017-9841\n" );
188 } else {
189 $this->error( "Unable to remove $badPhpUnit, you should manually. See CVE-2017-9841" );
190 }
191 }
192 }
193
194 $shared = $this->hasOption( 'doshared' );
195
196 $updates = [ 'core', 'extensions' ];
197 if ( !$this->hasOption( 'schema' ) ) {
198 if ( $this->hasOption( 'noschema' ) ) {
199 $updates[] = 'noschema';
200 }
201 $updates[] = 'stats';
202 }
203
204 $updater = DatabaseUpdater::newForDB( $db, $shared, $this );
205 $updater->doUpdates( $updates );
206
207 foreach ( $updater->getPostDatabaseUpdateMaintenance() as $maint ) {
208 $child = $this->runChild( $maint );
209
210 // LoggedUpdateMaintenance is checking the updatelog itself
211 $isLoggedUpdate = $child instanceof LoggedUpdateMaintenance;
212
213 if ( !$isLoggedUpdate && $updater->updateRowExists( $maint ) ) {
214 continue;
215 }
216
217 $child->execute();
218 if ( !$isLoggedUpdate ) {
219 $updater->insertUpdateRow( $maint );
220 }
221 }
222
223 $updater->setFileAccess();
224 if ( !$this->hasOption( 'nopurge' ) ) {
225 $updater->purgeCache();
226 }
227
228 $time2 = microtime( true );
229
230 $timeDiff = $lang->formatTimePeriod( $time2 - $time1 );
231 $this->output( "\nDone in $timeDiff.\n" );
232 }
233
234 function afterFinalSetup() {
235 global $wgLocalisationCacheConf;
236
237 # Don't try to access the database
238 # This needs to be disabled early since extensions will try to use the l10n
239 # cache from $wgExtensionFunctions (T22471)
240 $wgLocalisationCacheConf = [
241 'class' => LocalisationCache::class,
242 'storeClass' => LCStoreNull::class,
243 'storeDirectory' => false,
244 'manualRecache' => false,
245 ];
246 }
247
248 /**
249 * @throws FatalError
250 * @throws MWException
251 * @suppress PhanPluginDuplicateConditionalNullCoalescing
252 */
253 public function validateParamsAndArgs() {
254 // Allow extensions to add additional params.
255 $params = [];
256 Hooks::run( 'MaintenanceUpdateAddParams', [ &$params ] );
257
258 // This executes before the PHP version check, so don't use null coalesce (??).
259 // Keeping this compatible with older PHP versions lets us reach the code that
260 // displays a more helpful error.
261 foreach ( $params as $name => $param ) {
262 $this->addOption(
263 $name,
264 $param['desc'],
265 isset( $param['require'] ) ? $param['require'] : false,
266 isset( $param['withArg'] ) ? $param['withArg'] : false,
267 isset( $param['shortName'] ) ? $param['shortName'] : false,
268 isset( $param['multiOccurrence'] ) ? $param['multiOccurrence'] : false
269 );
270 }
271
272 parent::validateParamsAndArgs();
273 }
274 }
275
276 $maintClass = UpdateMediaWiki::class;
277 require_once RUN_MAINTENANCE_IF_MAIN;