Merge "Allow for dynamic TTLs in getWithSetCallback()"
[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 return Maintenance::DB_ADMIN;
67 }
68
69 function compatChecks() {
70 // Avoid syntax error in PHP4
71 $minimumPcreVersion = constant( 'Installer::MINIMUM_PCRE_VERSION' );
72
73 list( $pcreVersion ) = explode( ' ', PCRE_VERSION, 2 );
74 if ( version_compare( $pcreVersion, $minimumPcreVersion, '<' ) ) {
75 $this->error(
76 "PCRE $minimumPcreVersion or later is required.\n" .
77 "Your PHP binary is linked with PCRE $pcreVersion.\n\n" .
78 "More information:\n" .
79 "https://www.mediawiki.org/wiki/Manual:Errors_and_symptoms/PCRE\n\n" .
80 "ABORTING.\n",
81 true );
82 }
83
84 $test = new PhpXmlBugTester();
85 if ( !$test->ok ) {
86 $this->error(
87 "Your system has a combination of PHP and libxml2 versions that is buggy\n" .
88 "and can cause hidden data corruption in MediaWiki and other web apps.\n" .
89 "Upgrade to libxml2 2.7.3 or later.\n" .
90 "ABORTING (see https://bugs.php.net/bug.php?id=45996).\n",
91 true );
92 }
93 }
94
95 function execute() {
96 global $wgVersion, $wgLang, $wgAllowSchemaUpdates;
97
98 if ( !$wgAllowSchemaUpdates
99 && !( $this->hasOption( 'force' )
100 || $this->hasOption( 'schema' )
101 || $this->hasOption( 'noschema' ) )
102 ) {
103 $this->error( "Do not run update.php on this wiki. If you're seeing this you should\n"
104 . "probably ask for some help in performing your schema updates or use\n"
105 . "the --noschema and --schema options to get an SQL file for someone\n"
106 . "else to inspect and run.\n\n"
107 . "If you know what you are doing, you can continue with --force\n", true );
108 }
109
110 $this->fileHandle = null;
111 if ( substr( $this->getOption( 'schema' ), 0, 2 ) === "--" ) {
112 $this->error( "The --schema option requires a file as an argument.\n", true );
113 } elseif ( $this->hasOption( 'schema' ) ) {
114 $file = $this->getOption( 'schema' );
115 $this->fileHandle = fopen( $file, "w" );
116 if ( $this->fileHandle === false ) {
117 $err = error_get_last();
118 $this->error( "Problem opening the schema file for writing: $file\n\t{$err['message']}", true );
119 }
120 }
121
122 $wgLang = Language::factory( 'en' );
123
124 define( 'MW_UPDATER', true );
125
126 $this->output( "MediaWiki {$wgVersion} Updater\n\n" );
127
128 wfWaitForSlaves();
129
130 if ( !$this->hasOption( 'skip-compat-checks' ) ) {
131 $this->compatChecks();
132 } else {
133 $this->output( "Skipping compatibility checks, proceed at your own risk (Ctrl+C to abort)\n" );
134 wfCountdown( 5 );
135 }
136
137 // Check external dependencies are up to date
138 if ( !$this->hasOption( 'skip-external-dependencies' ) ) {
139 $composerLockUpToDate = $this->runChild( 'CheckComposerLockUpToDate' );
140 $composerLockUpToDate->execute();
141 } else {
142 $this->output(
143 "Skipping checking whether external dependencies are up to date, proceed at your own risk\n"
144 );
145 }
146
147 # Attempt to connect to the database as a privileged user
148 # This will vomit up an error if there are permissions problems
149 $db = wfGetDB( DB_MASTER );
150
151 $this->output( "Going to run database updates for " . wfWikiID() . "\n" );
152 if ( $db->getType() === 'sqlite' ) {
153 $this->output( "Using SQLite file: '{$db->getDbFilePath()}'\n" );
154 }
155 $this->output( "Depending on the size of your database this may take a while!\n" );
156
157 if ( !$this->hasOption( 'quick' ) ) {
158 $this->output( "Abort with control-c in the next five seconds "
159 . "(skip this countdown with --quick) ... " );
160 wfCountDown( 5 );
161 }
162
163 $time1 = microtime( true );
164
165 $shared = $this->hasOption( 'doshared' );
166
167 $updates = array( 'core', 'extensions' );
168 if ( !$this->hasOption( 'schema' ) ) {
169 if ( $this->hasOption( 'noschema' ) ) {
170 $updates[] = 'noschema';
171 }
172 $updates[] = 'stats';
173 }
174
175 $updater = DatabaseUpdater::newForDb( $db, $shared, $this );
176 $updater->doUpdates( $updates );
177
178 foreach ( $updater->getPostDatabaseUpdateMaintenance() as $maint ) {
179 $child = $this->runChild( $maint );
180
181 // LoggedUpdateMaintenance is checking the updatelog itself
182 $isLoggedUpdate = is_a( $child, 'LoggedUpdateMaintenance' );
183
184 if ( !$isLoggedUpdate && $updater->updateRowExists( $maint ) ) {
185 continue;
186 }
187
188 $child->execute();
189 if ( !$isLoggedUpdate ) {
190 $updater->insertUpdateRow( $maint );
191 }
192 }
193
194 $updater->setFileAccess();
195 if ( !$this->hasOption( 'nopurge' ) ) {
196 $updater->purgeCache();
197 }
198
199 $time2 = microtime( true );
200
201 $timeDiff = $wgLang->formatTimePeriod( $time2 - $time1 );
202 $this->output( "\nDone in $timeDiff.\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;