* Use Maintenance::runChild() to get the child script instance
[lhc/web/wiklou.git] / includes / installer / DatabaseUpdater.php
index 04827bd..fcf489b 100644 (file)
@@ -1,5 +1,11 @@
 <?php
-
+/**
+ * DBMS-specific updater helper.
+ *
+ * @file
+ * @ingroup Deployment
+ */
 /*
  * Class for handling database updates. Roughly based off of updaters.inc, with
  * a few improvements :)
@@ -16,6 +22,8 @@ abstract class DatabaseUpdater {
         */
        protected $updates = array();
 
+       protected $extensionUpdates = array();
+
        protected $db;
 
        protected $shared = false;
@@ -24,12 +32,45 @@ abstract class DatabaseUpdater {
                'DeleteDefaultMessages'
        );
 
-       protected function __construct( $db, $shared ) {
+       /**
+        * Constructor
+        *
+        * @param $db DatabaseBase object to perform updates on
+        * @param $shared bool Whether to perform updates on shared tables
+        *
+        * @TODO @FIXME Make $wgDatabase go away.
+        */
+       protected function __construct( DatabaseBase &$db, $shared ) {
+               global $wgDatabase;
+               $wgDatabase = $db;
                $this->db = $db;
                $this->shared = $shared;
+               $this->initOldGlobals();
+               wfRunHooks( 'LoadExtensionSchemaUpdates', array( $this ) );
+       }
+
+       /**
+        * Initialize all of the old globals. One day this should all become
+        * something much nicer
+        */
+       private function initOldGlobals() {
+               global $wgUpdates, $wgExtNewTables, $wgExtNewFields, $wgExtPGNewFields,
+                       $wgExtPGAlteredFields, $wgExtNewIndexes, $wgExtModifiedFields;
+
+               // Deprecated. Do not use, ever.
+               $wgUpdates = array();
+
+               # For extensions only, should be populated via hooks
+               # $wgDBtype should be checked to specifiy the proper file
+               $wgExtNewTables = array(); // table, dir
+               $wgExtNewFields = array(); // table, column, dir
+               $wgExtPGNewFields = array(); // table, column, column attributes; for PostgreSQL
+               $wgExtPGAlteredFields = array(); // table, column, new type, conversion method; for PostgreSQL
+               $wgExtNewIndexes = array(); // table, index, dir
+               $wgExtModifiedFields = array(); // table, index, dir
        }
 
-       public static function newForDB( $db, $shared ) {
+       public static function newForDB( &$db, $shared = false ) {
                $type = $db->getType();
                if( in_array( $type, Installer::getDBTypes() ) ) {
                        $class = ucfirst( $type ) . 'Updater';
@@ -39,26 +80,83 @@ abstract class DatabaseUpdater {
                }
        }
 
-       public function getDB() { return $this->db; }
+       /**
+        * Get a database connection to run updates
+        *
+        * @return DatabasBase object
+        */
+       public function getDB() {
+               return $this->db;
+       }
+
+       /**
+        * Add a new update coming from an extension. This should be called by
+        * extensions while executing the LoadExtensionSchemaUpdates hook.
+        *
+        * @param $update Array: the update to run. Format is the following:
+        *                first item is the callback function, it also can be a
+        *                simple string with the name of a function in this class,
+        *                following elements are parameters to the function.
+        *                Note that callback functions will recieve this object as
+        *                first parameter.
+        */
+       public function addExtensionUpdate( $update ) {
+               $this->extensionUpdates[] = $update;
+       }
+
+       /**
+        * Get the list of extension-defined updates
+        *
+        * @return Array
+        */
+       protected function getExtensionUpdates() {
+               return $this->extensionUpdates;
+       }
 
        public function getPostDatabaseUpdateMaintenance() {
                return $this->postDatabaseUpdateMaintenance;
        }
 
-       public function doUpdates() {
+       /**
+        * Do all the updates
+        *
+        * @param $purge Boolean: whether to clear the objectcache table after updates
+        */
+       public function doUpdates( $purge = true ) {
                global $IP, $wgVersion;
                require_once( "$IP/maintenance/updaters.inc" );
-               $this->updates = array_merge( $this->getCoreUpdateList(),
-                       $this->getOldGlobalUpdates() );
-               foreach ( $this->updates as $params ) {
+
+               $this->runUpdates( $this->getCoreUpdateList(), false );
+               $this->runUpdates( $this->getOldGlobalUpdates(), false );
+               $this->runUpdates( $this->getExtensionUpdates(), true );
+
+               $this->setAppliedUpdates( $wgVersion, $this->updates );
+
+               if( $purge ) {
+                       $this->purgeCache();
+               }
+               $this->checkStats();
+       }
+
+       /**
+        * Helper function for doUpdates()
+        *
+        * @param $updates Array of updates to run
+        * @param $passSelf Boolean: whether to pass this object we calling external
+        *                  functions
+        */
+       private function runUpdates( array $updates, $passSelf ) {
+               foreach ( $updates as $params ) {
                        $func = array_shift( $params );
-                       if( method_exists( $this, $func ) ) {
+                       if( !is_array( $func ) && method_exists( $this, $func ) ) {
                                $func = array( $this, $func );
+                       } elseif ( $passSelf ) {
+                               array_unshift( $params, $this );
                        }
                        call_user_func_array( $func, $params );
                        flush();
                }
-               $this->setAppliedUpdates( $wgVersion, $this->updates );
+               $this->updates = array_merge( $this->updates, $updates );
        }
 
        protected function setAppliedUpdates( $version, $updates = array() ) {
@@ -88,11 +186,9 @@ abstract class DatabaseUpdater {
         * Before 1.17, we used to handle updates via stuff like $wgUpdates,
         * $wgExtNewTables/Fields/Indexes. This is nasty :) We refactored a lot
         * of this in 1.17 but we want to remain back-compatible for awhile. So
-        * load up these old global-based things into our update list. We can't
-        * version these like we do with our core updates, so they have to go
-        * in 'always'
+        * load up these old global-based things into our update list.
         */
-       private function getOldGlobalUpdates() {
+       protected function getOldGlobalUpdates() {
                global $wgUpdates, $wgExtNewFields, $wgExtNewTables,
                        $wgExtModifiedFields, $wgExtNewIndexes, $wgSharedDB, $wgSharedTables;
 
@@ -117,7 +213,7 @@ abstract class DatabaseUpdater {
                foreach ( $wgExtNewFields as $fieldRecord ) {
                        if ( $fieldRecord[0] != 'user' || $doUser ) {
                                $updates[] = array(
-                                       'add_field', $fieldRecord[0], $fieldRecord[1],
+                                       'addField', $fieldRecord[0], $fieldRecord[1],
                                                $fieldRecord[2], true
                                );
                        }
@@ -125,14 +221,14 @@ abstract class DatabaseUpdater {
 
                foreach ( $wgExtNewIndexes as $fieldRecord ) {
                        $updates[] = array(
-                               'add_index', $fieldRecord[0], $fieldRecord[1],
+                               'addIndex', $fieldRecord[0], $fieldRecord[1],
                                        $fieldRecord[2], true
                        );
                }
 
                foreach ( $wgExtModifiedFields as $fieldRecord ) {
                        $updates[] = array(
-                               'modify_field', $fieldRecord[0], $fieldRecord[1],
+                               'modifyField', $fieldRecord[0], $fieldRecord[1],
                                        $fieldRecord[2], true
                        );
                }
@@ -142,7 +238,7 @@ abstract class DatabaseUpdater {
 
        /**
         * Get an array of updates to perform on the database. Should return a
-        * mutli-dimensional array. The main key is the MediaWiki version (1.12,
+        * multi-dimensional array. The main key is the MediaWiki version (1.12,
         * 1.13...) with the values being arrays of updates, identical to how
         * updaters.inc did it (for now)
         *
@@ -150,29 +246,153 @@ abstract class DatabaseUpdater {
         */
        protected abstract function getCoreUpdateList();
 
+       /**
+        * Applies a SQL patch
+        * @param $path String Path to the patch file
+        * @param $isFullPath Boolean Whether to treat $path as a relative or not
+        */
+       protected function applyPatch( $path, $isFullPath = false ) {
+               if ( $isFullPath ) {
+                       $this->db->sourceFile( $path );
+               } else {
+                       $this->db->sourceFile( DatabaseBase::patchPath( $path ) );
+               }
+       }
+
        /**
         * Add a new table to the database
         * @param $name String Name of the new table
         * @param $patch String Path to the patch file
-        * @param $fullpath Boolean Whether to treat $fullPath as a relative or not
+        * @param $fullpath Boolean Whether to treat $patch path as a relative or not
         */
        protected function addTable( $name, $patch, $fullpath = false ) {
                if ( $this->db->tableExists( $name ) ) {
                        wfOut( "...$name table already exists.\n" );
                } else {
                        wfOut( "Creating $name table..." );
-                       if ( $fullpath ) {
-                               $this->db->sourceFile( $patch );
-                       } else {
-                               $this->db->sourceFile( archive( $patch ) );
-                       }
+                       $this->applyPatch( $patch, $fullpath );
                        wfOut( "ok\n" );
                }
        }
-}
 
-class OracleUpdater extends DatabaseUpdater {
-       protected function getCoreUpdateList() {
-               return array();
+       /**
+        * Add a new field to an existing table
+        * @param $table String Name of the table to modify
+        * @param $field String Name of the new field
+        * @param $patch String Path to the patch file
+        * @param $fullpath Boolean Whether to treat $patch path as a relative or not
+        */
+       protected function addField( $table, $field, $patch, $fullpath = false ) {
+               if ( !$this->db->tableExists( $table ) ) {
+                       wfOut( "...$table table does not exist, skipping new field patch\n" );
+               } elseif ( $this->db->fieldExists( $table, $field ) ) {
+                       wfOut( "...have $field field in $table table.\n" );
+               } else {
+                       wfOut( "Adding $field field to table $table..." );
+                       $this->applyPatch( $patch, $fullpath );
+                       wfOut( "ok\n" );
+               }
+       }
+
+       /**
+        * Add a new index to an existing table
+        * @param $table String Name of the table to modify
+        * @param $index String Name of the new index
+        * @param $patch String Path to the patch file
+        * @param $fullpath Boolean Whether to treat $patch path as a relative or not
+        */
+       function addIndex( $table, $index, $patch, $fullpath = false ) {
+               if ( $this->db->indexExists( $table, $index ) ) {
+                       wfOut( "...$index key already set on $table table.\n" );
+               } else {
+                       wfOut( "Adding $index key to table $table... " );
+                       $this->applyPatch( $patch, $fullpath );
+                       wfOut( "ok\n" );
+               }
        }
+
+       /**
+        * Drop a field from an existing table
+        *
+        * @param $table String Name of the table to modify
+        * @param $field String Name of the old field
+        * @param $patch String Path to the patch file
+        * @param $fullpath Boolean Whether to treat $patch path as a relative or not
+        */
+       function dropField( $table, $field, $patch, $fullpath = false ) {
+               if ( $this->db->fieldExists( $table, $field ) ) {
+                       wfOut( "Table $table contains $field field. Dropping... " );
+                       $this->applyPatch( $patch, $fullpath );
+                       wfOut( "ok\n" );
+               } else {
+                       wfOut( "...$table table does not contain $field field.\n" );
+               }
+       }
+
+       /**
+        * Drop an index from an existing table
+        *
+        * @param $table String: Name of the table to modify
+        * @param $index String: Name of the old index
+        * @param $patch String: Path to the patch file
+        * @param $fullpath Boolean: Whether to treat $patch path as a relative or not
+        */
+       function dropIndex( $table, $index, $patch, $fullpath = false ) {
+               if ( $this->db->indexExists( $table, $index ) ) {
+                       wfOut( "Dropping $index from table $table... " );
+                       $this->applyPatch( $patch, $fullpath );
+                       wfOut( "ok\n" );
+               } else {
+                       wfOut( "...$index key doesn't exist.\n" );
+               }
+       }
+
+       /**
+        * Modify an existing field
+        *
+        * @param $table String: name of the table to which the field belongs
+        * @param $field String: name of the field to modify
+        * @param $patch String: path to the patch file
+        * @param $fullpath Boolean: whether to treat $patch path as a relative or not
+        */
+       public function modifyField( $table, $field, $patch, $fullpath = false ) {
+               if ( !$this->db->tableExists( $table ) ) {
+                       wfOut( "...$table table does not exist, skipping modify field patch\n" );
+               } elseif ( !$this->db->fieldExists( $table, $field ) ) {
+                       wfOut( "...$field field does not exist in $table table, skipping modify field patch\n" );
+               } else {
+                       wfOut( "Modifying $field field of table $table..." );
+                       $this->applyPatch( $patch, $fullpath );
+                       wfOut( "ok\n" );
+               }
+       }
+
+       /**
+        * Purge the objectcache table
+        */
+       protected function purgeCache() {
+               # We can't guarantee that the user will be able to use TRUNCATE,
+               # but we know that DELETE is available to us
+               wfOut( "Purging caches..." );
+               $this->db->delete( 'objectcache', '*', __METHOD__ );
+               wfOut( "done.\n" );
+       }
+
+       /**
+        * Check the site_stats table is not properly populated.
+        */
+       protected function checkStats() {
+               wfOut( "Checking site_stats row..." );
+               $row = $this->db->selectRow( 'site_stats', '*', array( 'ss_row_id' => 1 ), __METHOD__ );
+               if ( $row === false ) {
+                       wfOut( "data is missing! rebuilding...\n" );
+               } elseif ( isset( $row->site_stats ) && $row->ss_total_pages == -1 ) {
+                       wfOut( "missing ss_total_pages, rebuilding...\n" );
+               } else {
+                       wfOut( "done.\n" );
+                       return;
+               }
+               SiteStatsInit::doAllAndCommit( false );
+       }
+
 }