$dummy) { if(is_subclass_of($class, $rootClass)) { $rootClasses[$rootClass][] = $class; break; } } } } return $rootClasses; } /** * Display a simple HTML menu of database admin helpers. */ function index() { echo "

Database Administration Helpers

"; echo "

Add missing database fields (similar to sanity check).

"; echo "

Flush all of the generated images.

"; } /** * Updates the database schema, creating tables & fields as necessary. */ function build() { // The default time limit of 30 seconds is normally not enough increase_time_limit_to(600); // Get all our classes ManifestBuilder::create_manifest_file(); require(MANIFEST_FILE); if(isset($_GET['returnURL'])) { echo "

Setting up the database; you will be returned to your site shortly....

"; $this->doBuild(true); echo "

Done!

"; Director::redirect($_GET['returnURL']); } else { if(!Director::is_cli() && Director::urlParam('Controller') == __CLASS__) { echo '

db/build has been deprecated. Please use dev/build instead.

'; } $this->doBuild(isset($_REQUEST['quiet']) || isset($_REQUEST['from_installer']), !isset($_REQUEST['dont_populate'])); } } /** * Check if database needs to be built, and build it if it does. */ 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 */ function buildDefaults() { $dataClasses = ClassInfo::subclassesFor('DataObject'); array_shift($dataClasses); foreach($dataClasses as $dataClass){ singleton($dataClass)->requireDefaultRecords(); print "Defaults loaded for $dataClass
"; } } /** * Returns the timestamp of the time that the database was last built * * @return string Returns the timestamp of the time that the database was * last built */ static function lastBuilt() { $file = TEMP_FOLDER . '/database-last-generated-' . str_replace(array('\\','/',':'), '.' , Director::baseFolder()); if(file_exists($file)) { return filemtime($file); } } /** * Updates the database schema, creating tables & fields as necessary. * * @param boolean $quiet Don't show messages * @param boolean $populate Populate the database, as well as setting up its schema */ function doBuild($quiet = false, $populate = true, $testMode = false) { if($quiet) { DB::quiet(); } else { $conn = DB::getConn(); $databaseName = (method_exists($conn, 'currentDatabase')) ? $conn->currentDatabase() : ""; if(Director::is_cli()) echo "\n\nBuilding Database $databaseName\n\n"; else echo "

Building Database $databaseName

"; } // Set up the initial database if(!DB::isActive()) { if(!$quiet) { echo '

Creating database

'; } global $databaseConfig; $parameters = $databaseConfig ? $databaseConfig : $_REQUEST['db']; $connect = DB::getConnect($parameters); $username = $parameters['username']; $password = $parameters['password']; $database = $parameters['database']; if(!$database) { user_error("No database name given; please give a value for \$databaseConfig['database']", E_USER_ERROR); } DB::createDatabase($connect, $username, $password, $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 "\n

Creating database tables

\n\n"; } $conn = DB::getConn(); $conn->beginSchemaUpdate(); foreach($dataClasses as $dataClass) { // Check if class exists before trying to instantiate - this sidesteps any manifest weirdness if(class_exists($dataClass)) { $SNG = singleton($dataClass); if($testMode || !($SNG instanceof TestOnly)) { if(!$quiet) { if(Director::is_cli()) echo " * $dataClass\n"; else echo "
  • $dataClass
  • \n"; } $SNG->requireTable(); } } } $conn->endSchemaUpdate(); ClassInfo::reset_db_cache(); if($populate) { if(!$quiet) { if(Director::is_cli()) echo "\nCREATING DATABASE RECORDS\n\n"; else echo "\n

    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 "
  • $dataClass
  • \n"; } singleton($dataClass)->requireDefaultRecords(); } } } touch(TEMP_FOLDER . '/database-last-generated-' . str_replace(array('\\', '/', ':'), '.', Director::baseFolder())); if(isset($_REQUEST['from_installer'])) { echo "OK"; } if(!$quiet) echo "

    Database build completed!

    "; ClassInfo::reset_db_cache(); } /** * Clear all data out of the database * @todo Move this code into SS_Database class, for DB abstraction */ function clearAllData() { $tables = DB::getConn()->tableList(); foreach($tables as $table) { if(method_exists(DB::getConn(), 'clearTable')) DB::getConn()->clearTable($table); else DB::query("TRUNCATE \"$table\""); } } /** * Method used to check mod_rewrite is working correctly in the installer. */ function testinstall() { echo "OK"; } /** * Remove invalid records from tables - that is, records that don't have * corresponding records in their parent class tables. */ 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::database_fields($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 "
  • $sql"; DB::query($sql); } } } } } } } ?>