/** * A method to initialise the class by parsing a database XML schema file, so that * the class will be ready to create/drop tables for the supplied schema. * * @todo Better handling of cache files * * @param string $file The name of the database XML schema file to parse for * the table definitions. * @param bool $useCache If true definitions are loaded from the cache file * @return boolean True if the class was initialised correctly, false otherwise. */ function init($file, $useCache = true) { // Ensure that the schema XML file can be read if (!is_readable($file)) { OA::debug('Unable to read the database XML schema file: ' . $file, PEAR_LOG_ERR); return false; } // Create an instance of MDB2_Schema to parse the schema file $options = array('force_defaults' => false); $this->oSchema =& MDB2_Schema::factory($this->oDbh, $options); if ($useCache) { $oCache = new OA_DB_XmlCache(); $this->aDefinition = $oCache->get($file); $this->cached_definition = true; } else { $this->aDefinition = false; } if (!$this->aDefinition) { $this->cached_definition = false; // Parse the schema file $this->aDefinition = $this->oSchema->parseDatabaseDefinitionFile($file); if (PEAR::isError($this->aDefinition)) { OA::debug('Error parsing the database XML schema file: ' . $file, PEAR_LOG_ERR); return false; } // On-the fly cache writing disabled //if ($useCache) { // $oCache->save($this->aDefinition, $file); //} } return true; }
/** * remove all tables defined in a database structure xml file * @param string $file the xml file describing the tables */ public static function removeDBStructure($file) { $CONFIG_DBNAME = OC_Config::getValue("dbname", "owncloud"); $CONFIG_DBTABLEPREFIX = OC_Config::getValue("dbtableprefix", "oc_"); self::connectScheme(); // read file $content = file_get_contents($file); // Make changes and save them to a temporary file $file2 = tempnam(get_temp_dir(), 'oc_db_scheme_'); $content = str_replace('*dbname*', $CONFIG_DBNAME, $content); $content = str_replace('*dbprefix*', $CONFIG_DBTABLEPREFIX, $content); file_put_contents($file2, $content); // get the tables $definition = self::$schema->parseDatabaseDefinitionFile($file2); // Delete our temporary file unlink($file2); $tables = array_keys($definition['tables']); foreach ($tables as $table) { self::dropTable($table); } }