Refactor a lot of updaters.inc into some classes in the installer code. Still need...
[lhc/web/wiklou.git] / includes / installer / Update.php
1 <?php
2 /*
3 * Class for handling database updates. Roughly based off of updaters.inc, with
4 * a few improvements :)
5 */
6
7 class Update {
8
9 // Array of updates to perform on the database
10 protected $updates = array();
11
12 // Thing we'll need
13 protected $db, $updater;
14
15 public function __construct( $db ) {
16 $this->db = $db;
17 switch( $this->db->getType() ) {
18 case 'mysql':
19 $this->updater = new MysqlUpdaters();
20 break;
21 case 'sqlite':
22 $this->updater = new SqliteUpdaters();
23 break;
24 default:
25 throw new MWException( __METHOD__ . ' called for unsupported $wgDBtype' );
26 }
27 }
28
29 public function doUpdates() {
30 global $IP;
31 require_once( "$IP/maintenance/updaters.inc" );
32 $this->loadUpdates();
33 foreach ( $this->updates as $version => $updates ) {
34 foreach( $updates as $params ) {
35 $func = array_shift( $params );
36 call_user_func_array( $func, $params );
37 flush();
38 }
39 $this->setAppliedUpdates( $version, $updates );
40 }
41 }
42
43 protected function loadUpdates() {
44 foreach( $this->updater->getUpdates() as $version => $updates ) {
45 $appliedUpdates = $this->getAppliedUpdates( $version );
46 if( !$appliedUpdates || count( $appliedUpdates ) != count( $updates ) ) {
47 $this->updates[ $version ] = $updates;
48 }
49 }
50 }
51
52 private function getAppliedUpdates( $version ) {
53 $key = "updatelist-$version";
54 $val = $this->db->selectField( 'updatelog', 'ul_value',
55 array( 'ul_key' => $key ), __METHOD__ );
56 if( !$val ) {
57 return null;
58 } else {
59 return unserialize( $val );
60 }
61 }
62
63 private function setAppliedUpdates( $version, $updates = array() ) {
64 $key = "updatelist-$version";
65 $this->db->delete( 'updatelog', array( 'ul_key' => $key ), __METHOD__ );
66 $this->db->insert( 'updatelog',
67 array( 'ul_key' => $key, 'ul_value' => serialize( $updates ) ),
68 __METHOD__ );
69 }
70 }