$dummy) { if(is_subclass_of($class, $rootClass)) { $rootClasses[$rootClass][] = $class; break; } } } } return $rootClasses; } /** * When we're called as /dev/build, that's actually the index. Do the same * as /dev/build/build. */ public function index() { return $this->build(); } /** * Updates the database schema, creating tables & fields as necessary. */ public function build() { // The default time limit of 30 seconds is normally not enough increase_time_limit_to(600); // Get all our classes SS_ClassLoader::instance()->getManifest()->regenerate(); $url = $this->getReturnURL(); if($url) { echo "
Setting up the database; you will be returned to your site shortly....
"; $this->doBuild(true); echo "Done!
"; $this->redirect($url); } else { $quiet = $this->request->requestVar('quiet') !== null; $fromInstaller = $this->request->requestVar('from_installer') !== null; $populate = $this->request->requestVar('dont_populate') === null; $this->doBuild($quiet || $fromInstaller, $populate); } } /** * Gets the url to return to after build * * @return string|null */ protected function getReturnURL() { $url = $this->request->getVar('returnURL'); // Check that this url is a site url if(empty($url) || !Director::is_site_url($url)) { return null; } // Convert to absolute URL return Director::absoluteURL($url, true); } /** * Check if database needs to be built, and build it if it does. */ public static function autoBuild() { $dataClasses = ClassInfo::subclassesFor('DataObject'); $lastBuilt = self::lastBuilt(); foreach($dataClasses as $class) { if(filemtime(getClassFile($class)) > $lastBuilt) { $da = new DatabaseAdmin(); $da->doBuild(true); return; } } } /** * Build the default data, calling requireDefaultRecords on all * DataObject classes */ public function buildDefaults() { $dataClasses = ClassInfo::subclassesFor('DataObject'); array_shift($dataClasses); foreach($dataClasses as $dataClass){ singleton($dataClass)->requireDefaultRecords(); print "Defaults loaded for $dataClassCreating database
'; } // Load parameters from existing configuration global $databaseConfig; if(empty($databaseConfig) && empty($_REQUEST['db'])) { user_error("No database configuration available", E_USER_ERROR); } $parameters = (!empty($databaseConfig)) ? $databaseConfig : $_REQUEST['db']; // Check database name is given if(empty($parameters['database'])) { user_error("No database name given; please give a value for \$databaseConfig['database']", E_USER_ERROR); } $database = $parameters['database']; // Establish connection and create database in two steps unset($parameters['database']); DB::connect($parameters); DB::create_database($database); } // Build the database. Most of the hard work is handled by DataObject $dataClasses = ClassInfo::subclassesFor('DataObject'); array_shift($dataClasses); if(!$quiet) { if(Director::is_cli()) echo "\nCREATING DATABASE TABLES\n\n"; else echo "\nCreating database tables
\n\n"; } // Initiate schema update $dbSchema = DB::get_schema(); $dbSchema->schemaUpdate(function() use($dataClasses, $testMode, $quiet){ foreach($dataClasses as $dataClass) { // Check if class exists before trying to instantiate - this sidesteps any manifest weirdness if(!class_exists($dataClass)) continue; // Check if this class should be excluded as per testing conventions $SNG = singleton($dataClass); if(!$testMode && $SNG instanceof TestOnly) continue; // Log data if(!$quiet) { if(Director::is_cli()) echo " * $dataClass\n"; else echo "Creating database records
\n\n"; } foreach($dataClasses as $dataClass) { // Check if class exists before trying to instantiate - this sidesteps any manifest weirdness // Test_ indicates that it's the data class is part of testing system if(strpos($dataClass,'Test_') === false && class_exists($dataClass)) { if(!$quiet) { if(Director::is_cli()) echo " * $dataClass\n"; else echo "Database build completed!
"; } ClassInfo::reset_db_cache(); } /** * Clear all data out of the database * * @deprecated since version 4.0 */ public function clearAllData() { Deprecation::notice('4.0', 'Use DB::get_conn()->clearAllData() instead'); DB::get_conn()->clearAllData(); } /** * Remove invalid records from tables - that is, records that don't have * corresponding records in their parent class tables. */ public function cleanup() { $allClasses = get_declared_classes(); foreach($allClasses as $class) { if(get_parent_class($class) == 'DataObject') { $baseClasses[] = $class; } } foreach($baseClasses as $baseClass) { // Get data classes $subclasses = ClassInfo::subclassesFor($baseClass); unset($subclasses[0]); foreach($subclasses as $k => $subclass) { if(DataObject::has_own_table($subclass)) { unset($subclasses[$k]); } } if($subclasses) { $records = DB::query("SELECT * FROM \"$baseClass\""); foreach($subclasses as $subclass) { $recordExists[$subclass] = DB::query("SELECT \"ID\" FROM \"$subclass\"")->keyedColumn(); } foreach($records as $record) { foreach($subclasses as $subclass) { $id = $record['ID']; if(($record['ClassName'] != $subclass) && (!is_subclass_of($record['ClassName'], $subclass)) && (isset($recordExists[$subclass][$id]))) { $sql = "DELETE FROM \"$subclass\" WHERE \"ID\" = $record[ID]"; echo "