96ad2fd99e4cee237967a137657dd1f7f11ab664
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 $this->updater
= new OracleUpdater();
28 throw new MWException( __METHOD__
. ' called for unsupported $wgDBtype' );
32 public function doUpdates() {
34 require_once( "$IP/maintenance/updaters.inc" );
36 foreach ( $this->updates
as $version => $updates ) {
37 foreach( $updates as $params ) {
38 $func = array_shift( $params );
39 call_user_func_array( $func, $params );
42 $this->setAppliedUpdates( $version, $updates );
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();
53 foreach( $this->updater
->getUpdates() as $version => $updates ) {
54 $appliedUpdates = $this->getAppliedUpdates( $version );
55 if( !$appliedUpdates ||
$appliedUpdates != $updates ) {
56 $this->updates
[ $version ] = $updates;
61 protected function getAppliedUpdates( $version ) {
62 $key = "updatelist-$version";
63 $val = $this->db
->selectField( 'updatelog', 'ul_value',
64 array( 'ul_key' => $key ), __METHOD__
);
68 return unserialize( $val );
72 private function setAppliedUpdates( $version, $updates = array() ) {
73 if( !$this->canUseNewUpdatelog() ) {
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 ) ),
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
91 protected function canUseNewUpdatelog() {
92 return $this->db
->tableExists( 'updatelog' ) &&
93 $this->db
->fieldExists( 'updatelog', 'ul_value' );