* do_all_updates() is dead
[lhc/web/wiklou.git] / includes / installer / DatabaseUpdater.php
1 <?php
2 /**
3 * DBMS-specific updater helper.
4 *
5 * @file
6 * @ingroup Deployment
7 */
8
9 /*
10 * Class for handling database updates. Roughly based off of updaters.inc, with
11 * a few improvements :)
12 *
13 * @ingroup Deployment
14 * @since 1.17
15 */
16 abstract class DatabaseUpdater {
17
18 /**
19 * Array of updates to perform on the database
20 *
21 * @var array
22 */
23 protected $updates = array();
24
25 protected $db;
26
27 protected $shared = false;
28
29 protected $postDatabaseUpdateMaintenance = array(
30 'DeleteDefaultMessages'
31 );
32
33 /**
34 * Constructor
35 *
36 * @param $db DatabaseBase object to perform updates on
37 * @param $shared bool Whether to perform updates on shared tables
38 *
39 * @TODO @FIXME Make $wgDatabase go away.
40 */
41 protected function __construct( DatabaseBase &$db, $shared ) {
42 global $wgDatabase;
43 $wgDatabase = $db;
44 $this->db = $db;
45 $this->shared = $shared;
46 $this->initOldGlobals();
47 wfRunHooks( 'LoadExtensionSchemaUpdates', array( $this ) );
48 }
49
50 /**
51 * Initialize all of the old globals. One day this should all become
52 * something much nicer
53 */
54 private function initOldGlobals() {
55 global $wgUpdates, $wgExtNewTables, $wgExtNewFields, $wgExtPGNewFields,
56 $wgExtPGAlteredFields, $wgExtNewIndexes, $wgExtModifiedFields;
57
58 // Deprecated. Do not use, ever.
59 $wgUpdates = array();
60
61 # For extensions only, should be populated via hooks
62 # $wgDBtype should be checked to specifiy the proper file
63 $wgExtNewTables = array(); // table, dir
64 $wgExtNewFields = array(); // table, column, dir
65 $wgExtPGNewFields = array(); // table, column, column attributes; for PostgreSQL
66 $wgExtPGAlteredFields = array(); // table, column, new type, conversion method; for PostgreSQL
67 $wgExtNewIndexes = array(); // table, index, dir
68 $wgExtModifiedFields = array(); // table, index, dir
69 }
70
71 public static function newForDB( &$db, $shared = false ) {
72 $type = $db->getType();
73 if( in_array( $type, Installer::getDBTypes() ) ) {
74 $class = ucfirst( $type ) . 'Updater';
75 return new $class( $db, $shared );
76 } else {
77 throw new MWException( __METHOD__ . ' called for unsupported $wgDBtype' );
78 }
79 }
80
81 public function getDB() { return $this->db; }
82
83 public function getPostDatabaseUpdateMaintenance() {
84 return $this->postDatabaseUpdateMaintenance;
85 }
86
87 /**
88 * Do all the updates
89 * @param $purge bool Whether to clear the objectcache table after updates
90 */
91 public function doUpdates( $purge = true ) {
92 global $IP, $wgVersion;
93 require_once( "$IP/maintenance/updaters.inc" );
94 $this->updates = array_merge( $this->getCoreUpdateList(),
95 $this->getOldGlobalUpdates() );
96 foreach ( $this->updates as $params ) {
97 $func = array_shift( $params );
98 if( !is_array( $func ) && method_exists( $this, $func ) ) {
99 $func = array( $this, $func );
100 }
101 call_user_func_array( $func, $params );
102 flush();
103 }
104 $this->setAppliedUpdates( $wgVersion, $this->updates );
105
106 if( $purge ) {
107 $this->purgeCache();
108 }
109 }
110
111 protected function setAppliedUpdates( $version, $updates = array() ) {
112 if( !$this->canUseNewUpdatelog() ) {
113 return;
114 }
115 $key = "updatelist-$version-" . time();
116 $this->db->insert( 'updatelog',
117 array( 'ul_key' => $key, 'ul_value' => serialize( $updates ) ),
118 __METHOD__ );
119 }
120
121 /**
122 * Updatelog was changed in 1.17 to have a ul_value column so we can record
123 * more information about what kind of updates we've done (that's what this
124 * class does). Pre-1.17 wikis won't have this column, and really old wikis
125 * might not even have updatelog at all
126 *
127 * @return boolean
128 */
129 protected function canUseNewUpdatelog() {
130 return $this->db->tableExists( 'updatelog' ) &&
131 $this->db->fieldExists( 'updatelog', 'ul_value' );
132 }
133
134 /**
135 * Before 1.17, we used to handle updates via stuff like $wgUpdates,
136 * $wgExtNewTables/Fields/Indexes. This is nasty :) We refactored a lot
137 * of this in 1.17 but we want to remain back-compatible for awhile. So
138 * load up these old global-based things into our update list.
139 */
140 protected function getOldGlobalUpdates() {
141 global $wgUpdates, $wgExtNewFields, $wgExtNewTables,
142 $wgExtModifiedFields, $wgExtNewIndexes, $wgSharedDB, $wgSharedTables;
143
144 $doUser = $this->shared ?
145 $wgSharedDB && in_array( 'user', $wgSharedTables ) :
146 !$wgSharedDB || !in_array( 'user', $wgSharedTables );
147
148 $updates = array();
149
150 if( isset( $wgUpdates[ $this->db->getType() ] ) ) {
151 foreach( $wgUpdates[ $this->db->getType() ] as $upd ) {
152 $updates[] = $upd;
153 }
154 }
155
156 foreach ( $wgExtNewTables as $tableRecord ) {
157 $updates[] = array(
158 'addTable', $tableRecord[0], $tableRecord[1], true
159 );
160 }
161
162 foreach ( $wgExtNewFields as $fieldRecord ) {
163 if ( $fieldRecord[0] != 'user' || $doUser ) {
164 $updates[] = array(
165 'addField', $fieldRecord[0], $fieldRecord[1],
166 $fieldRecord[2], true
167 );
168 }
169 }
170
171 foreach ( $wgExtNewIndexes as $fieldRecord ) {
172 $updates[] = array(
173 'addIndex', $fieldRecord[0], $fieldRecord[1],
174 $fieldRecord[2], true
175 );
176 }
177
178 foreach ( $wgExtModifiedFields as $fieldRecord ) {
179 $updates[] = array(
180 'modify_field', $fieldRecord[0], $fieldRecord[1],
181 $fieldRecord[2], true
182 );
183 }
184
185 return $updates;
186 }
187
188 /**
189 * Get an array of updates to perform on the database. Should return a
190 * mutli-dimensional array. The main key is the MediaWiki version (1.12,
191 * 1.13...) with the values being arrays of updates, identical to how
192 * updaters.inc did it (for now)
193 *
194 * @return Array
195 */
196 protected abstract function getCoreUpdateList();
197
198 /**
199 * Applies a SQL patch
200 * @param $path String Path to the patch file
201 * @param $isFullPath Boolean Whether to treat $path as a relative or not
202 */
203 protected function applyPatch( $path, $isFullPath = false ) {
204 if ( $isFullPath ) {
205 $this->db->sourceFile( $path );
206 } else {
207 $this->db->sourceFile( DatabaseBase::patchPath( $path ) );
208 }
209 }
210
211 /**
212 * Add a new table to the database
213 * @param $name String Name of the new table
214 * @param $patch String Path to the patch file
215 * @param $fullpath Boolean Whether to treat $patch path as a relative or not
216 */
217 protected function addTable( $name, $patch, $fullpath = false ) {
218 if ( $this->db->tableExists( $name ) ) {
219 wfOut( "...$name table already exists.\n" );
220 } else {
221 wfOut( "Creating $name table..." );
222 $this->applyPatch( $patch, $fullpath );
223 wfOut( "ok\n" );
224 }
225 }
226
227 /**
228 * Add a new field to an existing table
229 * @param $table String Name of the table to modify
230 * @param $field String Name of the new field
231 * @param $patch String Path to the patch file
232 * @param $fullpath Boolean Whether to treat $patch path as a relative or not
233 */
234 protected function addField( $table, $field, $patch, $fullpath = false ) {
235 if ( !$this->db->tableExists( $table ) ) {
236 wfOut( "...$table table does not exist, skipping new field patch\n" );
237 } elseif ( $this->db->fieldExists( $table, $field ) ) {
238 wfOut( "...have $field field in $table table.\n" );
239 } else {
240 wfOut( "Adding $field field to table $table..." );
241 $this->applyPatch( $patch, $fullpath );
242 wfOut( "ok\n" );
243 }
244 }
245
246 /**
247 * Add a new index to an existing table
248 * @param $table String Name of the table to modify
249 * @param $index String Name of the new index
250 * @param $patch String Path to the patch file
251 * @param $fullpath Boolean Whether to treat $patch path as a relative or not
252 */
253 function addIndex( $table, $index, $patch, $fullpath = false ) {
254 if ( $this->db->indexExists( $table, $index ) ) {
255 wfOut( "...$index key already set on $table table.\n" );
256 } else {
257 wfOut( "Adding $index key to table $table... " );
258 $this->applyPatch( $patch, $fullpath );
259 wfOut( "ok\n" );
260 }
261 }
262
263 /**
264 * Drop a field from an existing table
265 *
266 * @param $table String Name of the table to modify
267 * @param $field String Name of the old field
268 * @param $patch String Path to the patch file
269 * @param $fullpath Boolean Whether to treat $patch path as a relative or not
270 */
271 function dropField( $table, $field, $patch, $fullpath = false ) {
272 if ( $this->db->fieldExists( $table, $field ) ) {
273 wfOut( "Table $table contains $field field. Dropping... " );
274 $this->applyPatch( $patch, $fullpath );
275 wfOut( "ok\n" );
276 } else {
277 wfOut( "...$table table does not contain $field field.\n" );
278 }
279 }
280
281 /**
282 * Purge the objectcache table
283 */
284 protected function purgeCache() {
285 # We can't guarantee that the user will be able to use TRUNCATE,
286 # but we know that DELETE is available to us
287 wfOut( "Purging caches..." );
288 $this->db->delete( 'objectcache', '*', __METHOD__ );
289 wfOut( "done.\n" );
290 }
291 }