Add stub OracleUpdater
[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 case 'oracle':
25 $this->updater = new OracleUpdater();
26 break;
27 default:
28 throw new MWException( __METHOD__ . ' called for unsupported $wgDBtype' );
29 }
30 }
31
32 public function doUpdates() {
33 global $IP;
34 require_once( "$IP/maintenance/updaters.inc" );
35 $this->loadUpdates();
36 foreach ( $this->updates as $version => $updates ) {
37 foreach( $updates as $params ) {
38 $func = array_shift( $params );
39 call_user_func_array( $func, $params );
40 flush();
41 }
42 $this->setAppliedUpdates( $version, $updates );
43 }
44 }
45
46 protected function loadUpdates() {
47 // If the updatelog table hasn't been upgraded, we can't use the new
48 // style of recording our steps. Run all to be safe
49 if( !$this->canUseNewUpdatelog() ) {
50 $this->updates = $this->updater->getUpdates();
51 return;
52 }
53 foreach( $this->updater->getUpdates() as $version => $updates ) {
54 $appliedUpdates = $this->getAppliedUpdates( $version );
55 if( !$appliedUpdates || $appliedUpdates != $updates ) {
56 $this->updates[ $version ] = $updates;
57 }
58 }
59 }
60
61 protected function getAppliedUpdates( $version ) {
62 $key = "updatelist-$version";
63 $val = $this->db->selectField( 'updatelog', 'ul_value',
64 array( 'ul_key' => $key ), __METHOD__ );
65 if( !$val ) {
66 return null;
67 } else {
68 return unserialize( $val );
69 }
70 }
71
72 private function setAppliedUpdates( $version, $updates = array() ) {
73 if( !$this->canUseNewUpdatelog() ) {
74 return;
75 }
76 $key = "updatelist-$version";
77 $this->db->delete( 'updatelog', array( 'ul_key' => $key ), __METHOD__ );
78 $this->db->insert( 'updatelog',
79 array( 'ul_key' => $key, 'ul_value' => serialize( $updates ) ),
80 __METHOD__ );
81 }
82
83 /**
84 * Updatelog was changed in 1.17 to have a ul_value column so we can record
85 * more information about what kind of updates we've done (that's what this
86 * class does). Pre-1.17 wikis won't have this column, and really old wikis
87 * might not even have updatelog at all
88 *
89 * @return boolean
90 */
91 protected function canUseNewUpdatelog() {
92 return $this->db->tableExists( 'updatelog' ) &&
93 $this->db->fieldExists( 'updatelog', 'ul_value' );
94 }
95 }