Minor code style tweak
[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 case 'sqlite':
32 case 'oracle':
33 $class = ucfirst( $db->getType() ) . 'Updater';
34 return new $class( $db, $shared );
35 default:
36 throw new MWException( __METHOD__ . ' called for unsupported $wgDBtype' );
37 }
38 }
39
40 public function doUpdates( $doUser = false ) {
41 global $IP, $wgVersion;
42 require_once( "$IP/maintenance/updaters.inc" );
43 $this->updates = array_merge( $this->getCoreUpdateList(),
44 $this->getOldGlobalUpdates( $doUser ) );
45 foreach ( $this->updates as $params ) {
46 $func = array_shift( $params );
47 call_user_func_array( $func, $params );
48 flush();
49 }
50 $this->setAppliedUpdates( $wgVersion, $this->updates );
51 }
52
53 protected function setAppliedUpdates( $version, $updates = array() ) {
54 if( !$this->canUseNewUpdatelog() ) {
55 return;
56 }
57 $key = "updatelist-$version-" . time();
58 $this->db->insert( 'updatelog',
59 array( 'ul_key' => $key, 'ul_value' => serialize( $updates ) ),
60 __METHOD__ );
61 }
62
63 /**
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
68 *
69 * @return boolean
70 */
71 protected function canUseNewUpdatelog() {
72 return $this->db->tableExists( 'updatelog' ) &&
73 $this->db->fieldExists( 'updatelog', 'ul_value' );
74 }
75
76 /**
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
82 * in 'always'
83 */
84 private function getOldGlobalUpdates( $douser ) {
85 global $wgUpdates, $wgExtNewFields, $wgExtNewTables,
86 $wgExtModifiedFields, $wgExtNewIndexes, $wgSharedDB, $wgSharedTables;
87
88 $doUser = $this->shared ?
89 $wgSharedDB && in_array( 'user', $wgSharedTables ) :
90 !$wgSharedDB || !in_array( 'user', $wgSharedTables );
91
92 $updates = array();
93
94 if( isset( $wgUpdates[ $this->db->getType() ] ) ) {
95 foreach( $wgUpdates[ $this->db->getType() ] as $upd ) {
96 $updates[] = $upd;
97 }
98 }
99
100 foreach ( $wgExtNewTables as $tableRecord ) {
101 $updates[] = array(
102 'add_table', $tableRecord[0], $tableRecord[1], true
103 );
104 }
105
106 foreach ( $wgExtNewFields as $fieldRecord ) {
107 if ( $fieldRecord[0] != 'user' || $doUser ) {
108 $updates[] = array(
109 'add_field', $fieldRecord[0], $fieldRecord[1],
110 $fieldRecord[2], true
111 );
112 }
113 }
114
115 foreach ( $wgExtNewIndexes as $fieldRecord ) {
116 $updates[] = array(
117 'add_index', $fieldRecord[0], $fieldRecord[1],
118 $fieldRecord[2], true
119 );
120 }
121
122 foreach ( $wgExtModifiedFields as $fieldRecord ) {
123 $updates[] = array(
124 'modify_field', $fieldRecord[0], $fieldRecord[1],
125 $fieldRecord[2], true
126 );
127 }
128
129 return $updates;
130 }
131
132 /**
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)
137 *
138 * @return Array
139 */
140 protected abstract function getCoreUpdateList();
141 }
142
143 class OracleUpdater extends DatabaseUpdater {
144 protected function getCoreUpdateList() {
145 return array();
146 }
147 }