Merge "Title: Title::getSubpage should not lose the interwiki prefix"
[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 foreach ( SpecialVersion::getSoftwareInformation() as $name => $version ) {
128 $this->output( "{$name}: {$version}\n" );
129 }
130
131 wfWaitForSlaves();
132
133 if ( !$this->hasOption( 'skip-compat-checks' ) ) {
134 $this->compatChecks();
135 } else {
136 $this->output( "Skipping compatibility checks, proceed at your own risk (Ctrl+C to abort)\n" );
137 $this->countDown( 5 );
138 }
139
140 // Check external dependencies are up to date
141 if ( !$this->hasOption( 'skip-external-dependencies' ) ) {
142 $composerLockUpToDate = $this->runChild( CheckComposerLockUpToDate::class );
143 $composerLockUpToDate->execute();
144 } else {
145 $this->output(
146 "Skipping checking whether external dependencies are up to date, proceed at your own risk\n"
147 );
148 }
149
150 # Attempt to connect to the database as a privileged user
151 # This will vomit up an error if there are permissions problems
152 $db = $this->getDB( DB_MASTER );
153
154 # Check to see whether the database server meets the minimum requirements
155 /** @var DatabaseInstaller $dbInstallerClass */
156 $dbInstallerClass = Installer::getDBInstallerClass( $db->getType() );
157 $status = $dbInstallerClass::meetsMinimumRequirement( $db->getServerVersion() );
158 if ( !$status->isOK() ) {
159 // This might output some wikitext like <strong> but it should be comprehensible
160 $text = $status->getWikiText();
161 $this->fatalError( $text );
162 }
163
164 $dbDomain = WikiMap::getCurrentWikiDbDomain()->getId();
165 $this->output( "Going to run database updates for $dbDomain\n" );
166 if ( $db->getType() === 'sqlite' ) {
167 /** @var IMaintainableDatabase|DatabaseSqlite $db */
168 $this->output( "Using SQLite file: '{$db->getDbFilePath()}'\n" );
169 }
170 $this->output( "Depending on the size of your database this may take a while!\n" );
171
172 if ( !$this->hasOption( 'quick' ) ) {
173 $this->output( "Abort with control-c in the next five seconds "
174 . "(skip this countdown with --quick) ... " );
175 $this->countDown( 5 );
176 }
177
178 $time1 = microtime( true );
179
180 $badPhpUnit = dirname( __DIR__ ) . '/vendor/phpunit/phpunit/src/Util/PHP/eval-stdin.php';
181 if ( file_exists( $badPhpUnit ) ) {
182 // Bad versions of the file are:
183 // https://raw.githubusercontent.com/sebastianbergmann/phpunit/c820f915bfae34e5a836f94967a2a5ea5ef34f21/src/Util/PHP/eval-stdin.php
184 // https://raw.githubusercontent.com/sebastianbergmann/phpunit/3aaddb1c5bd9b9b8d070b4cf120e71c36fd08412/src/Util/PHP/eval-stdin.php
185 $md5 = md5_file( $badPhpUnit );
186 if ( $md5 === '120ac49800671dc383b6f3709c25c099'
187 || $md5 === '28af792cb38fc9a1b236b91c1aad2876'
188 ) {
189 $success = unlink( $badPhpUnit );
190 if ( $success ) {
191 $this->output( "Removed PHPUnit eval-stdin.php to protect against CVE-2017-9841\n" );
192 } else {
193 $this->error( "Unable to remove $badPhpUnit, you should manually. See CVE-2017-9841" );
194 }
195 }
196 }
197
198 $shared = $this->hasOption( 'doshared' );
199
200 $updates = [ 'core', 'extensions' ];
201 if ( !$this->hasOption( 'schema' ) ) {
202 if ( $this->hasOption( 'noschema' ) ) {
203 $updates[] = 'noschema';
204 }
205 $updates[] = 'stats';
206 }
207
208 $updater = DatabaseUpdater::newForDB( $db, $shared, $this );
209 $updater->doUpdates( $updates );
210
211 foreach ( $updater->getPostDatabaseUpdateMaintenance() as $maint ) {
212 $child = $this->runChild( $maint );
213
214 // LoggedUpdateMaintenance is checking the updatelog itself
215 $isLoggedUpdate = $child instanceof LoggedUpdateMaintenance;
216
217 if ( !$isLoggedUpdate && $updater->updateRowExists( $maint ) ) {
218 continue;
219 }
220
221 $child->execute();
222 if ( !$isLoggedUpdate ) {
223 $updater->insertUpdateRow( $maint );
224 }
225 }
226
227 $updater->setFileAccess();
228 if ( !$this->hasOption( 'nopurge' ) ) {
229 $updater->purgeCache();
230 }
231
232 $time2 = microtime( true );
233
234 $timeDiff = $lang->formatTimePeriod( $time2 - $time1 );
235 $this->output( "\nDone in $timeDiff.\n" );
236 }
237
238 function afterFinalSetup() {
239 global $wgLocalisationCacheConf;
240
241 # Don't try to access the database
242 # This needs to be disabled early since extensions will try to use the l10n
243 # cache from $wgExtensionFunctions (T22471)
244 $wgLocalisationCacheConf = [
245 'class' => LocalisationCache::class,
246 'storeClass' => LCStoreNull::class,
247 'storeDirectory' => false,
248 'manualRecache' => false,
249 ];
250 }
251
252 /**
253 * @throws FatalError
254 * @throws MWException
255 * @suppress PhanPluginDuplicateConditionalNullCoalescing
256 */
257 public function validateParamsAndArgs() {
258 // Allow extensions to add additional params.
259 $params = [];
260 Hooks::run( 'MaintenanceUpdateAddParams', [ &$params ] );
261
262 // This executes before the PHP version check, so don't use null coalesce (??).
263 // Keeping this compatible with older PHP versions lets us reach the code that
264 // displays a more helpful error.
265 foreach ( $params as $name => $param ) {
266 $this->addOption(
267 $name,
268 $param['desc'],
269 isset( $param['require'] ) ? $param['require'] : false,
270 isset( $param['withArg'] ) ? $param['withArg'] : false,
271 isset( $param['shortName'] ) ? $param['shortName'] : false,
272 isset( $param['multiOccurrence'] ) ? $param['multiOccurrence'] : false
273 );
274 }
275
276 parent::validateParamsAndArgs();
277 }
278 }
279
280 $maintClass = UpdateMediaWiki::class;
281 require_once RUN_MAINTENANCE_IF_MAIN;