Some general update refactoring:
[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 function __construct( $db, $shared ) {
24 $this->db = $db;
25 $this->shared = $shared;
26 }
27
28 public static function newForDB( $db, $shared ) {
29 switch( $db->getType() ) {
30 case 'mysql':
31 return new MysqlUpdater( $db, $shared );
32 case 'sqlite':
33 return new SqliteUpdater( $db, $shared );
34 case 'oracle':
35 return new OracleUpdater( $db, $shared );
36 default:
37 throw new MWException( __METHOD__ . ' called for unsupported $wgDBtype' );
38 }
39 }
40
41 public function doUpdates( $doUser = false ) {
42 global $IP, $wgVersion;
43 require_once( "$IP/maintenance/updaters.inc" );
44 $this->updates = array_merge( $this->getCoreUpdateList(),
45 $this->getOldGlobalUpdates( $doUser ) );
46 foreach ( $this->updates as $params ) {
47 $func = array_shift( $params );
48 call_user_func_array( $func, $params );
49 flush();
50 }
51 $this->setAppliedUpdates( $wgVersion, $this->updates );
52 }
53
54 protected function setAppliedUpdates( $version, $updates = array() ) {
55 if( !$this->canUseNewUpdatelog() ) {
56 return;
57 }
58 $key = "updatelist-$version-" . time();
59 $this->db->insert( 'updatelog',
60 array( 'ul_key' => $key, 'ul_value' => serialize( $updates ) ),
61 __METHOD__ );
62 }
63
64 /**
65 * Updatelog was changed in 1.17 to have a ul_value column so we can record
66 * more information about what kind of updates we've done (that's what this
67 * class does). Pre-1.17 wikis won't have this column, and really old wikis
68 * might not even have updatelog at all
69 *
70 * @return boolean
71 */
72 protected function canUseNewUpdatelog() {
73 return $this->db->tableExists( 'updatelog' ) &&
74 $this->db->fieldExists( 'updatelog', 'ul_value' );
75 }
76
77 /**
78 * Before 1.17, we used to handle updates via stuff like $wgUpdates,
79 * $wgExtNewTables/Fields/Indexes. This is nasty :) We refactored a lot
80 * of this in 1.17 but we want to remain back-compatible for awhile. So
81 * load up these old global-based things into our update list. We can't
82 * version these like we do with our core updates, so they have to go
83 * in 'always'
84 */
85 private function getOldGlobalUpdates( $douser ) {
86 global $wgUpdates, $wgExtNewFields, $wgExtNewTables,
87 $wgExtModifiedFields, $wgExtNewIndexes, $wgSharedDB, $wgSharedTables;
88
89 $doUser = $this->shared ?
90 $wgSharedDB && in_array( 'user', $wgSharedTables ) :
91 !$wgSharedDB || !in_array( 'user', $wgSharedTables );
92
93 $updates = array();
94
95 if( isset( $wgUpdates[ $this->db->getType() ] ) ) {
96 foreach( $wgUpdates[ $this->db->getType() ] as $upd ) {
97 $updates[] = $upd;
98 }
99 }
100
101 foreach ( $wgExtNewTables as $tableRecord ) {
102 $updates[] = array(
103 'add_table', $tableRecord[0], $tableRecord[1], true
104 );
105 }
106
107 foreach ( $wgExtNewFields as $fieldRecord ) {
108 if ( $fieldRecord[0] != 'user' || $doUser ) {
109 $updates[] = array(
110 'add_field', $fieldRecord[0], $fieldRecord[1],
111 $fieldRecord[2], true
112 );
113 }
114 }
115
116 foreach ( $wgExtNewIndexes as $fieldRecord ) {
117 $updates[] = array(
118 'add_index', $fieldRecord[0], $fieldRecord[1],
119 $fieldRecord[2], true
120 );
121 }
122
123 foreach ( $wgExtModifiedFields as $fieldRecord ) {
124 $updates[] = array(
125 'modify_field', $fieldRecord[0], $fieldRecord[1],
126 $fieldRecord[2], true
127 );
128 }
129
130 return $updates;
131 }
132
133 /**
134 * Get an array of updates to perform on the database. Should return a
135 * mutli-dimensional array. The main key is the MediaWiki version (1.12,
136 * 1.13...) with the values being arrays of updates, identical to how
137 * updaters.inc did it (for now)
138 *
139 * @return Array
140 */
141 protected abstract function getCoreUpdateList();
142 }
143
144 class OracleUpdater extends DatabaseUpdater {
145 protected function getCoreUpdateList() {
146 return array();
147 }
148 }