3 * Class for handling database updates. Roughly based off of updaters.inc, with
4 * a few improvements :)
9 // Array of updates to perform on the database
10 protected $updates = array();
13 protected $db, $updater;
15 public function __construct( $db ) {
17 switch( $this->db
->getType() ) {
19 $this->updater
= new MysqlUpdater();
22 $this->updater
= new SqliteUpdater();
25 throw new MWException( __METHOD__
. ' called for unsupported $wgDBtype' );
29 public function doUpdates() {
31 require_once( "$IP/maintenance/updaters.inc" );
33 foreach ( $this->updates
as $version => $updates ) {
34 foreach( $updates as $params ) {
35 $func = array_shift( $params );
36 call_user_func_array( $func, $params );
39 $this->setAppliedUpdates( $version, $updates );
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();
50 foreach( $this->updater
->getUpdates() as $version => $updates ) {
51 $appliedUpdates = $this->getAppliedUpdates( $version );
52 if( !$appliedUpdates ||
$appliedUpdates != $updates ) {
53 $this->updates
[ $version ] = $updates;
58 protected function getAppliedUpdates( $version ) {
59 $key = "updatelist-$version";
60 $val = $this->db
->selectField( 'updatelog', 'ul_value',
61 array( 'ul_key' => $key ), __METHOD__
);
65 return unserialize( $val );
69 private function setAppliedUpdates( $version, $updates = array() ) {
70 if( !$this->canUseNewUpdatelog() ) {
71 $this->updates
= $this->updater
->getUpdates();
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 ) ),
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
89 protected function canUseNewUpdatelog() {
90 return $this->db
->tableExists( 'updatelog' ) &&
91 $this->db
->fieldExists( 'updatelog', 'ul_value' );