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.
99 protected function getOldGlobalUpdates() {
100 global $wgUpdates, $wgExtNewFields, $wgExtNewTables,
101 $wgExtModifiedFields, $wgExtNewIndexes, $wgSharedDB, $wgSharedTables;
103 $doUser = $this->shared ?
104 $wgSharedDB && in_array( 'user', $wgSharedTables ) :
105 !$wgSharedDB ||
!in_array( 'user', $wgSharedTables );
109 if( isset( $wgUpdates[ $this->db
->getType() ] ) ) {
110 foreach( $wgUpdates[ $this->db
->getType() ] as $upd ) {
115 foreach ( $wgExtNewTables as $tableRecord ) {
117 'addTable', $tableRecord[0], $tableRecord[1], true
121 foreach ( $wgExtNewFields as $fieldRecord ) {
122 if ( $fieldRecord[0] != 'user' ||
$doUser ) {
124 'addField', $fieldRecord[0], $fieldRecord[1],
125 $fieldRecord[2], true
130 foreach ( $wgExtNewIndexes as $fieldRecord ) {
132 'addIndex', $fieldRecord[0], $fieldRecord[1],
133 $fieldRecord[2], true
137 foreach ( $wgExtModifiedFields as $fieldRecord ) {
139 'modify_field', $fieldRecord[0], $fieldRecord[1],
140 $fieldRecord[2], true
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)
155 protected abstract function getCoreUpdateList();
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
162 protected function applyPatch( $path, $isFullPath = false ) {
164 $this->db
->sourceFile( $path );
166 $this->db
->sourceFile( DatabaseBase
::patchPath( $path ) );
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
176 protected function addTable( $name, $patch, $fullpath = false ) {
177 if ( $this->db
->tableExists( $name ) ) {
178 wfOut( "...$name table already exists.\n" );
180 wfOut( "Creating $name table..." );
181 $this->applyPatch( $patch, $fullpath );
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
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" );
199 wfOut( "Adding $field field to table $table..." );
200 $this->applyPatch( $patch, $fullpath );
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
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" );
216 wfOut( "Adding $index key to table $table... " );
217 $this->applyPatch( $patch, $fullpath );
223 * Drop a field from an existing table
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
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 );
236 wfOut( "...$table table does not contain $field field.\n" );