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