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