'SilverStripe\\Assets\\File', 'Image' => 'SilverStripe\\Assets\\Image', 'Folder' => 'SilverStripe\\Assets\\Folder', 'Group' => 'SilverStripe\\Security\\Group', 'LoginAttempt' => 'SilverStripe\\Security\\LoginAttempt', 'Member' => 'SilverStripe\\Security\\Member', 'MemberPassword' => 'SilverStripe\\Security\\MemberPassword', 'Permission' => 'SilverStripe\\Security\\Permission', 'PermissionRole' => 'SilverStripe\\Security\\PermissionRole', 'PermissionRoleCode' => 'SilverStripe\\Security\\PermissionRoleCode', 'RememberLoginHash' => 'SilverStripe\\Security\\RememberLoginHash', ]; /** * Config setting to enabled/disable the display of record counts on the dev/build output */ private static $show_record_counts = true; protected function init() { parent::init(); // We allow access to this controller regardless of live-status or ADMIN permission only // if on CLI or with the database not ready. The latter makes it less errorprone to do an // initial schema build without requiring a default-admin login. // Access to this controller is always allowed in "dev-mode", or of the user is ADMIN. $allowAllCLI = DevelopmentAdmin::config()->get('allow_all_cli'); $canAccess = ( Director::isDev() || !Security::database_is_ready() // We need to ensure that DevelopmentAdminTest can simulate permission failures when running // "dev/tests" from CLI. || (Director::is_cli() && $allowAllCLI) || Permission::check("ADMIN") ); if (!$canAccess) { Security::permissionFailure( $this, "This page is secured and you need administrator rights to access it. " . "Enter your credentials below and we will send you right along." ); } } /** * Get the data classes, grouped by their root class * * @return array Array of data classes, grouped by their root class */ public function groupedDataClasses() { // Get all root data objects $allClasses = get_declared_classes(); $rootClasses = []; foreach ($allClasses as $class) { if (get_parent_class($class) == DataObject::class) { $rootClasses[$class] = []; } } // Assign every other data object one of those foreach ($allClasses as $class) { if (!isset($rootClasses[$class]) && is_subclass_of($class, DataObject::class)) { foreach ($rootClasses as $rootClass => $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 Environment::increaseTimeLimitTo(600); // If this code is being run outside of a dev/build or without a ?flush query string param, // the class manifest hasn't been flushed, so do it here $request = $this->getRequest(); if (!array_key_exists('flush', $request->getVars()) && strpos($request->getURL(), 'dev/build') !== 0) { ClassLoader::inst()->getManifest()->regenerate(false); } $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); } /** * Build the default data, calling requireDefaultRecords on all * DataObject classes */ public function buildDefaults() { $dataClasses = ClassInfo::subclassesFor(DataObject::class); array_shift($dataClasses); if (!Director::is_cli()) { echo "Creating database
'; } // Load parameters from existing configuration $databaseConfig = DB::getConfig(); if (empty($databaseConfig) && empty($_REQUEST['db'])) { throw new BadMethodCallException("No database configuration available"); } $parameters = (!empty($databaseConfig)) ? $databaseConfig : $_REQUEST['db']; // Check database name is given if (empty($parameters['database'])) { throw new BadMethodCallException( "No database name given; please give a value for SS_DATABASE_NAME or set SS_DATABASE_CHOOSE_NAME" ); } $database = $parameters['database']; // Establish connection unset($parameters['database']); DB::connect($parameters); // Check to ensure that the re-instated SS_DATABASE_SUFFIX functionality won't unexpectedly // rename the database. To be removed for SS5 if ($suffix = Environment::getEnv('SS_DATABASE_SUFFIX')) { $previousName = preg_replace("/{$suffix}$/", '', $database); if (!isset($_GET['force_suffix_rename']) && DB::get_conn()->databaseExists($previousName)) { throw new DatabaseException( "SS_DATABASE_SUFFIX was previously broken, but has now been fixed. This will result in your " . "database being named \"{$database}\" instead of \"{$previousName}\" from now on. If this " . "change is intentional, please visit dev/build?force_suffix_rename=1 to continue" ); } } // Create database DB::create_database($database); } // Build the database. Most of the hard work is handled by DataObject $dataClasses = ClassInfo::subclassesFor(DataObject::class); array_shift($dataClasses); if (!$quiet) { if (Director::is_cli()) { echo "\nCREATING DATABASE TABLES\n\n"; } else { echo "\nCreating database tables
Creating database records
Database build completed!
"; } ClassInfo::reset_db_cache(); } /** * Given a base data class, a field name and an old and new class name (value), look for obsolete ($oldClassName) * values in the $dataClass's $fieldName column and replace it with $newClassName. * * @param string $dataClass The data class to look up * @param string $fieldName The field name to look in for obsolete class names * @param string $oldClassName The old class name * @param string $newClassName The new class name */ protected function updateLegacyClassNames($dataClass, $fieldName, $oldClassName, $newClassName) { $schema = DataObject::getSchema(); // Check first to ensure that the class has the specified field to update if (!$schema->databaseField($dataClass, $fieldName, false)) { return; } // Load a list of any records that have obsolete class names $badRecordCount = DataObject::get($dataClass) ->filter([$fieldName => $oldClassName]) ->count(); if (!$badRecordCount) { return; } if (Director::is_cli()) { echo " * Correcting {$badRecordCount} obsolete {$fieldName} values for {$newClassName}\n"; } else { echo "