Cleanup to r69187: forgot to safeguard against ul_value or updatelog itself not existing.
[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 // Things 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 MysqlUpdater();
20 break;
21 case 'sqlite':
22 $this->updater = new SqliteUpdater();
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 // If the updatelog table hasn't been upgraded, we can't use the new
45 // style of recording our steps. Run all to be safe
46 if( !$this->canUseNewUpdatelog() ) {
47 $this->updates = $this->updater->getUpdates();
48 return;
49 }
50 foreach( $this->updater->getUpdates() as $version => $updates ) {
51 $appliedUpdates = $this->getAppliedUpdates( $version );
52 if( !$appliedUpdates || $appliedUpdates != $updates ) {
53 $this->updates[ $version ] = $updates;
54 }
55 }
56 }
57
58 protected function getAppliedUpdates( $version ) {
59 $key = "updatelist-$version";
60 $val = $this->db->selectField( 'updatelog', 'ul_value',
61 array( 'ul_key' => $key ), __METHOD__ );
62 if( !$val ) {
63 return null;
64 } else {
65 return unserialize( $val );
66 }
67 }
68
69 private function setAppliedUpdates( $version, $updates = array() ) {
70 if( !$this->canUseNewUpdatelog() ) {
71 $this->updates = $this->updater->getUpdates();
72 return;
73 }
74 $key = "updatelist-$version";
75 $this->db->delete( 'updatelog', array( 'ul_key' => $key ), __METHOD__ );
76 $this->db->insert( 'updatelog',
77 array( 'ul_key' => $key, 'ul_value' => serialize( $updates ) ),
78 __METHOD__ );
79 }
80
81 /**
82 * Updatelog was changed in 1.17 to have a ul_value column so we can record
83 * more information about what kind of updates we've done (that's what this
84 * class does). Pre-1.17 wikis won't have this column, and really old wikis
85 * might not even have updatelog at all
86 *
87 * @return boolean
88 */
89 protected function canUseNewUpdatelog() {
90 return $this->db->tableExists( 'updatelog' ) &&
91 $this->db->fieldExists( 'updatelog', 'ul_value' );
92 }
93 }