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