3 * DBMS-specific updater helper.
10 * Class for handling database updates. Roughly based off of updaters.inc, with
11 * a few improvements :)
16 abstract class DatabaseUpdater
{
19 * Array of updates to perform on the database
23 protected $updates = array();
27 protected $shared = false;
29 protected $postDatabaseUpdateMaintenance = array(
30 'DeleteDefaultMessages'
33 protected function __construct( $db, $shared ) {
35 $this->shared
= $shared;
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 );
44 throw new MWException( __METHOD__
. ' called for unsupported $wgDBtype' );
48 public function getDB() { return $this->db
; }
50 public function getPostDatabaseUpdateMaintenance() {
51 return $this->postDatabaseUpdateMaintenance
;
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 );
64 call_user_func_array( $func, $params );
67 $this->setAppliedUpdates( $wgVersion, $this->updates
);
70 protected function setAppliedUpdates( $version, $updates = array() ) {
71 if( !$this->canUseNewUpdatelog() ) {
74 $key = "updatelist-$version-" . time();
75 $this->db
->insert( 'updatelog',
76 array( 'ul_key' => $key, 'ul_value' => serialize( $updates ) ),
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
88 protected function canUseNewUpdatelog() {
89 return $this->db
->tableExists( 'updatelog' ) &&
90 $this->db
->fieldExists( 'updatelog', 'ul_value' );
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
101 protected function getOldGlobalUpdates() {
102 global $wgUpdates, $wgExtNewFields, $wgExtNewTables,
103 $wgExtModifiedFields, $wgExtNewIndexes, $wgSharedDB, $wgSharedTables;
105 $doUser = $this->shared ?
106 $wgSharedDB && in_array( 'user', $wgSharedTables ) :
107 !$wgSharedDB ||
!in_array( 'user', $wgSharedTables );
111 if( isset( $wgUpdates[ $this->db
->getType() ] ) ) {
112 foreach( $wgUpdates[ $this->db
->getType() ] as $upd ) {
117 foreach ( $wgExtNewTables as $tableRecord ) {
119 'addTable', $tableRecord[0], $tableRecord[1], true
123 foreach ( $wgExtNewFields as $fieldRecord ) {
124 if ( $fieldRecord[0] != 'user' ||
$doUser ) {
126 'addField', $fieldRecord[0], $fieldRecord[1],
127 $fieldRecord[2], true
132 foreach ( $wgExtNewIndexes as $fieldRecord ) {
134 'addIndex', $fieldRecord[0], $fieldRecord[1],
135 $fieldRecord[2], true
139 foreach ( $wgExtModifiedFields as $fieldRecord ) {
141 'modify_field', $fieldRecord[0], $fieldRecord[1],
142 $fieldRecord[2], true
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)
157 protected abstract function getCoreUpdateList();
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
164 protected function applyPatch( $path, $isFullPath = false ) {
166 $this->db
->sourceFile( $path );
168 $this->db
->sourceFile( archive( $path ) );
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
178 protected function addTable( $name, $patch, $fullpath = false ) {
179 if ( $this->db
->tableExists( $name ) ) {
180 wfOut( "...$name table already exists.\n" );
182 wfOut( "Creating $name table..." );
183 $this->applyPatch( $patch, $fullpath );
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
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" );
201 wfOut( "Adding $field field to table $table..." );
202 $this->applyPatch( $patch, $fullpath );
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
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" );
218 wfOut( "Adding $index key to table $table... " );
219 $this->applyPatch( $patch, $fullpath );
225 * Drop a field from an existing table
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
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 );
238 wfOut( "...$table table does not contain $field field.\n" );
243 class OracleUpdater
extends DatabaseUpdater
{
244 protected function getCoreUpdateList() {