Comment fix
[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( !is_array( $func ) && 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.
98 */
99 protected function getOldGlobalUpdates() {
100 global $wgUpdates, $wgExtNewFields, $wgExtNewTables,
101 $wgExtModifiedFields, $wgExtNewIndexes, $wgSharedDB, $wgSharedTables;
102
103 $doUser = $this->shared ?
104 $wgSharedDB && in_array( 'user', $wgSharedTables ) :
105 !$wgSharedDB || !in_array( 'user', $wgSharedTables );
106
107 $updates = array();
108
109 if( isset( $wgUpdates[ $this->db->getType() ] ) ) {
110 foreach( $wgUpdates[ $this->db->getType() ] as $upd ) {
111 $updates[] = $upd;
112 }
113 }
114
115 foreach ( $wgExtNewTables as $tableRecord ) {
116 $updates[] = array(
117 'addTable', $tableRecord[0], $tableRecord[1], true
118 );
119 }
120
121 foreach ( $wgExtNewFields as $fieldRecord ) {
122 if ( $fieldRecord[0] != 'user' || $doUser ) {
123 $updates[] = array(
124 'addField', $fieldRecord[0], $fieldRecord[1],
125 $fieldRecord[2], true
126 );
127 }
128 }
129
130 foreach ( $wgExtNewIndexes as $fieldRecord ) {
131 $updates[] = array(
132 'addIndex', $fieldRecord[0], $fieldRecord[1],
133 $fieldRecord[2], true
134 );
135 }
136
137 foreach ( $wgExtModifiedFields as $fieldRecord ) {
138 $updates[] = array(
139 'modify_field', $fieldRecord[0], $fieldRecord[1],
140 $fieldRecord[2], true
141 );
142 }
143
144 return $updates;
145 }
146
147 /**
148 * Get an array of updates to perform on the database. Should return a
149 * mutli-dimensional array. The main key is the MediaWiki version (1.12,
150 * 1.13...) with the values being arrays of updates, identical to how
151 * updaters.inc did it (for now)
152 *
153 * @return Array
154 */
155 protected abstract function getCoreUpdateList();
156
157 /**
158 * Applies a SQL patch
159 * @param $path String Path to the patch file
160 * @param $isFullPath Boolean Whether to treat $path as a relative or not
161 */
162 protected function applyPatch( $path, $isFullPath = false ) {
163 if ( $isFullPath ) {
164 $this->db->sourceFile( $path );
165 } else {
166 $this->db->sourceFile( DatabaseBase::patchPath( $path ) );
167 }
168 }
169
170 /**
171 * Add a new table to the database
172 * @param $name String Name of the new table
173 * @param $patch String Path to the patch file
174 * @param $fullpath Boolean Whether to treat $patch path as a relative or not
175 */
176 protected function addTable( $name, $patch, $fullpath = false ) {
177 if ( $this->db->tableExists( $name ) ) {
178 wfOut( "...$name table already exists.\n" );
179 } else {
180 wfOut( "Creating $name table..." );
181 $this->applyPatch( $patch, $fullpath );
182 wfOut( "ok\n" );
183 }
184 }
185
186 /**
187 * Add a new field to an existing table
188 * @param $table String Name of the table to modify
189 * @param $field String Name of the new field
190 * @param $patch String Path to the patch file
191 * @param $fullpath Boolean Whether to treat $patch path as a relative or not
192 */
193 protected function addField( $table, $field, $patch, $fullpath = false ) {
194 if ( !$this->db->tableExists( $table ) ) {
195 wfOut( "...$table table does not exist, skipping new field patch\n" );
196 } elseif ( $this->db->fieldExists( $table, $field ) ) {
197 wfOut( "...have $field field in $table table.\n" );
198 } else {
199 wfOut( "Adding $field field to table $table..." );
200 $this->applyPatch( $patch, $fullpath );
201 wfOut( "ok\n" );
202 }
203 }
204
205 /**
206 * Add a new index to an existing table
207 * @param $table String Name of the table to modify
208 * @param $index String Name of the new index
209 * @param $patch String Path to the patch file
210 * @param $fullpath Boolean Whether to treat $patch path as a relative or not
211 */
212 function addIndex( $table, $index, $patch, $fullpath = false ) {
213 if ( $this->db->indexExists( $table, $index ) ) {
214 wfOut( "...$index key already set on $table table.\n" );
215 } else {
216 wfOut( "Adding $index key to table $table... " );
217 $this->applyPatch( $patch, $fullpath );
218 wfOut( "ok\n" );
219 }
220 }
221
222 /**
223 * Drop a field from an existing table
224 *
225 * @param $table String Name of the table to modify
226 * @param $field String Name of the old field
227 * @param $patch String Path to the patch file
228 * @param $fullpath Boolean Whether to treat $patch path as a relative or not
229 */
230 function dropField( $table, $field, $patch, $fullpath = false ) {
231 if ( $this->db->fieldExists( $table, $field ) ) {
232 wfOut( "Table $table contains $field field. Dropping... " );
233 $this->applyPatch( $patch, $fullpath );
234 wfOut( "ok\n" );
235 } else {
236 wfOut( "...$table table does not contain $field field.\n" );
237 }
238 }
239 }