Forgot to indent this correctly...
[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 protected function __construct( $db, $shared ) {
34 $this->db = $db;
35 $this->shared = $shared;
36 }
37
38 public static function newForDB( $db, $shared ) {
39 $type = $db->getType();
40 if( in_array( $type, Installer::getDBTypes() ) ) {
41 $class = ucfirst( $type ) . 'Updater';
42 return new $class( $db, $shared );
43 } else {
44 throw new MWException( __METHOD__ . ' called for unsupported $wgDBtype' );
45 }
46 }
47
48 public function getDB() { return $this->db; }
49
50 public function getPostDatabaseUpdateMaintenance() {
51 return $this->postDatabaseUpdateMaintenance;
52 }
53
54 public function doUpdates() {
55 global $IP, $wgVersion;
56 require_once( "$IP/maintenance/updaters.inc" );
57 $this->updates = array_merge( $this->getCoreUpdateList(),
58 $this->getOldGlobalUpdates() );
59 foreach ( $this->updates as $params ) {
60 $func = array_shift( $params );
61 if( method_exists( $this, $func ) ) {
62 $func = array( $this, $func );
63 }
64 call_user_func_array( $func, $params );
65 flush();
66 }
67 $this->setAppliedUpdates( $wgVersion, $this->updates );
68 }
69
70 protected function setAppliedUpdates( $version, $updates = array() ) {
71 if( !$this->canUseNewUpdatelog() ) {
72 return;
73 }
74 $key = "updatelist-$version-" . time();
75 $this->db->insert( 'updatelog',
76 array( 'ul_key' => $key, 'ul_value' => serialize( $updates ) ),
77 __METHOD__ );
78 }
79
80 /**
81 * Updatelog was changed in 1.17 to have a ul_value column so we can record
82 * more information about what kind of updates we've done (that's what this
83 * class does). Pre-1.17 wikis won't have this column, and really old wikis
84 * might not even have updatelog at all
85 *
86 * @return boolean
87 */
88 protected function canUseNewUpdatelog() {
89 return $this->db->tableExists( 'updatelog' ) &&
90 $this->db->fieldExists( 'updatelog', 'ul_value' );
91 }
92
93 /**
94 * Before 1.17, we used to handle updates via stuff like $wgUpdates,
95 * $wgExtNewTables/Fields/Indexes. This is nasty :) We refactored a lot
96 * of this in 1.17 but we want to remain back-compatible for awhile. So
97 * load up these old global-based things into our update list. We can't
98 * version these like we do with our core updates, so they have to go
99 * in 'always'
100 */
101 protected function getOldGlobalUpdates() {
102 global $wgUpdates, $wgExtNewFields, $wgExtNewTables,
103 $wgExtModifiedFields, $wgExtNewIndexes, $wgSharedDB, $wgSharedTables;
104
105 $doUser = $this->shared ?
106 $wgSharedDB && in_array( 'user', $wgSharedTables ) :
107 !$wgSharedDB || !in_array( 'user', $wgSharedTables );
108
109 $updates = array();
110
111 if( isset( $wgUpdates[ $this->db->getType() ] ) ) {
112 foreach( $wgUpdates[ $this->db->getType() ] as $upd ) {
113 $updates[] = $upd;
114 }
115 }
116
117 foreach ( $wgExtNewTables as $tableRecord ) {
118 $updates[] = array(
119 'addTable', $tableRecord[0], $tableRecord[1], true
120 );
121 }
122
123 foreach ( $wgExtNewFields as $fieldRecord ) {
124 if ( $fieldRecord[0] != 'user' || $doUser ) {
125 $updates[] = array(
126 'addField', $fieldRecord[0], $fieldRecord[1],
127 $fieldRecord[2], true
128 );
129 }
130 }
131
132 foreach ( $wgExtNewIndexes as $fieldRecord ) {
133 $updates[] = array(
134 'addIndex', $fieldRecord[0], $fieldRecord[1],
135 $fieldRecord[2], true
136 );
137 }
138
139 foreach ( $wgExtModifiedFields as $fieldRecord ) {
140 $updates[] = array(
141 'modify_field', $fieldRecord[0], $fieldRecord[1],
142 $fieldRecord[2], true
143 );
144 }
145
146 return $updates;
147 }
148
149 /**
150 * Get an array of updates to perform on the database. Should return a
151 * mutli-dimensional array. The main key is the MediaWiki version (1.12,
152 * 1.13...) with the values being arrays of updates, identical to how
153 * updaters.inc did it (for now)
154 *
155 * @return Array
156 */
157 protected abstract function getCoreUpdateList();
158
159 /**
160 * Applies a SQL patch
161 * @param $path String Path to the patch file
162 * @param $isFullPath Boolean Whether to treat $path as a relative or not
163 */
164 protected function applyPatch( $path, $isFullPath = false ) {
165 if ( $isFullPath ) {
166 $this->db->sourceFile( $path );
167 } else {
168 $this->db->sourceFile( archive( $path ) );
169 }
170 }
171
172 /**
173 * Add a new table to the database
174 * @param $name String Name of the new table
175 * @param $patch String Path to the patch file
176 * @param $fullpath Boolean Whether to treat $patch path as a relative or not
177 */
178 protected function addTable( $name, $patch, $fullpath = false ) {
179 if ( $this->db->tableExists( $name ) ) {
180 wfOut( "...$name table already exists.\n" );
181 } else {
182 wfOut( "Creating $name table..." );
183 $this->applyPatch( $patch, $fullpath );
184 wfOut( "ok\n" );
185 }
186 }
187
188 /**
189 * Add a new field to an existing table
190 * @param $table String Name of the table to modify
191 * @param $field String Name of the new field
192 * @param $patch String Path to the patch file
193 * @param $fullpath Boolean Whether to treat $patch path as a relative or not
194 */
195 protected function addField( $table, $field, $patch, $fullpath = false ) {
196 if ( !$this->db->tableExists( $table ) ) {
197 wfOut( "...$table table does not exist, skipping new field patch\n" );
198 } elseif ( $this->db->fieldExists( $table, $field ) ) {
199 wfOut( "...have $field field in $table table.\n" );
200 } else {
201 wfOut( "Adding $field field to table $table..." );
202 $this->applyPatch( $patch, $fullpath );
203 wfOut( "ok\n" );
204 }
205 }
206
207 /**
208 * Add a new index to an existing table
209 * @param $table String Name of the table to modify
210 * @param $index String Name of the new index
211 * @param $patch String Path to the patch file
212 * @param $fullpath Boolean Whether to treat $patch path as a relative or not
213 */
214 function addIndex( $table, $index, $patch, $fullpath = false ) {
215 if ( $this->db->indexExists( $table, $index ) ) {
216 wfOut( "...$index key already set on $table table.\n" );
217 } else {
218 wfOut( "Adding $index key to table $table... " );
219 $this->applyPatch( $patch, $fullpath );
220 wfOut( "ok\n" );
221 }
222 }
223
224 /**
225 * Drop a field from an existing table
226 *
227 * @param $table String Name of the table to modify
228 * @param $field String Name of the old field
229 * @param $patch String Path to the patch file
230 * @param $fullpath Boolean Whether to treat $patch path as a relative or not
231 */
232 function dropField( $table, $field, $patch, $fullpath = false ) {
233 if ( $this->db->fieldExists( $table, $field ) ) {
234 wfOut( "Table $table contains $field field. Dropping... " );
235 $this->applyPatch( $patch, $fullpath );
236 wfOut( "ok\n" );
237 } else {
238 wfOut( "...$table table does not contain $field field.\n" );
239 }
240 }
241 }
242
243 class OracleUpdater extends DatabaseUpdater {
244 protected function getCoreUpdateList() {
245 return array();
246 }
247 }