4 * Class for handling database updates. Roughly based off of updaters.inc, with
5 * a few improvements :)
10 abstract class DatabaseUpdater
{
13 * Array of updates to perform on the database
17 protected $updates = array();
21 protected $shared = false;
23 protected function __construct( $db, $shared ) {
25 $this->shared
= $shared;
28 public static function newForDB( $db, $shared ) {
29 switch( $db->getType() ) {
33 $class = ucfirst( $db->getType() ) . 'Updater';
34 return new $class( $db, $shared );
36 throw new MWException( __METHOD__
. ' called for unsupported $wgDBtype' );
40 public function doUpdates() {
41 global $IP, $wgVersion;
42 require_once( "$IP/maintenance/updaters.inc" );
43 $this->updates
= array_merge( $this->getCoreUpdateList(),
44 $this->getOldGlobalUpdates() );
45 foreach ( $this->updates
as $params ) {
46 $func = array_shift( $params );
47 call_user_func_array( $func, $params );
50 $this->setAppliedUpdates( $wgVersion, $this->updates
);
53 protected function setAppliedUpdates( $version, $updates = array() ) {
54 if( !$this->canUseNewUpdatelog() ) {
57 $key = "updatelist-$version-" . time();
58 $this->db
->insert( 'updatelog',
59 array( 'ul_key' => $key, 'ul_value' => serialize( $updates ) ),
64 * Updatelog was changed in 1.17 to have a ul_value column so we can record
65 * more information about what kind of updates we've done (that's what this
66 * class does). Pre-1.17 wikis won't have this column, and really old wikis
67 * might not even have updatelog at all
71 protected function canUseNewUpdatelog() {
72 return $this->db
->tableExists( 'updatelog' ) &&
73 $this->db
->fieldExists( 'updatelog', 'ul_value' );
77 * Before 1.17, we used to handle updates via stuff like $wgUpdates,
78 * $wgExtNewTables/Fields/Indexes. This is nasty :) We refactored a lot
79 * of this in 1.17 but we want to remain back-compatible for awhile. So
80 * load up these old global-based things into our update list. We can't
81 * version these like we do with our core updates, so they have to go
84 private function getOldGlobalUpdates() {
85 global $wgUpdates, $wgExtNewFields, $wgExtNewTables,
86 $wgExtModifiedFields, $wgExtNewIndexes, $wgSharedDB, $wgSharedTables;
88 $doUser = $this->shared ?
89 $wgSharedDB && in_array( 'user', $wgSharedTables ) :
90 !$wgSharedDB ||
!in_array( 'user', $wgSharedTables );
94 if( isset( $wgUpdates[ $this->db
->getType() ] ) ) {
95 foreach( $wgUpdates[ $this->db
->getType() ] as $upd ) {
100 foreach ( $wgExtNewTables as $tableRecord ) {
102 'add_table', $tableRecord[0], $tableRecord[1], true
106 foreach ( $wgExtNewFields as $fieldRecord ) {
107 if ( $fieldRecord[0] != 'user' ||
$doUser ) {
109 'add_field', $fieldRecord[0], $fieldRecord[1],
110 $fieldRecord[2], true
115 foreach ( $wgExtNewIndexes as $fieldRecord ) {
117 'add_index', $fieldRecord[0], $fieldRecord[1],
118 $fieldRecord[2], true
122 foreach ( $wgExtModifiedFields as $fieldRecord ) {
124 'modify_field', $fieldRecord[0], $fieldRecord[1],
125 $fieldRecord[2], true
133 * Get an array of updates to perform on the database. Should return a
134 * mutli-dimensional array. The main key is the MediaWiki version (1.12,
135 * 1.13...) with the values being arrays of updates, identical to how
136 * updaters.inc did it (for now)
140 protected abstract function getCoreUpdateList();
143 class OracleUpdater
extends DatabaseUpdater
{
144 protected function getCoreUpdateList() {