2007-07-19 12:40:28 +02:00
|
|
|
<?php
|
2008-11-02 01:55:33 +01:00
|
|
|
// Include the DB class
|
2011-03-30 22:56:21 +02:00
|
|
|
require_once("model/DB.php");
|
2007-07-19 12:40:28 +02:00
|
|
|
|
|
|
|
/**
|
2007-08-06 02:47:31 +02:00
|
|
|
* DatabaseAdmin class
|
|
|
|
*
|
|
|
|
* Utility functions for administrating the database. These can be accessed
|
|
|
|
* via URL, e.g. http://www.yourdomain.com/db/build.
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2012-04-12 08:02:46 +02:00
|
|
|
* @package framework
|
2008-02-25 03:10:37 +01:00
|
|
|
* @subpackage model
|
2007-07-19 12:40:28 +02:00
|
|
|
*/
|
2007-08-06 02:47:31 +02:00
|
|
|
class DatabaseAdmin extends Controller {
|
|
|
|
|
2008-02-25 03:10:37 +01:00
|
|
|
/// SECURITY ///
|
2013-03-21 19:48:54 +01:00
|
|
|
private static $allowed_actions = array(
|
2009-09-10 03:37:44 +02:00
|
|
|
'index',
|
2008-02-25 03:10:37 +01:00
|
|
|
'build',
|
|
|
|
'cleanup',
|
|
|
|
'import'
|
|
|
|
);
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-09-19 12:07:39 +02:00
|
|
|
public function init() {
|
2009-03-17 23:23:52 +01:00
|
|
|
parent::init();
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2009-03-17 23:23:52 +01:00
|
|
|
// 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.
|
2010-11-30 06:13:09 +01:00
|
|
|
$isRunningTests = (class_exists('SapphireTest', false) && SapphireTest::is_running_test());
|
2009-03-17 23:23:52 +01:00
|
|
|
$canAccess = (
|
2014-08-15 08:53:05 +02:00
|
|
|
Director::isDev()
|
|
|
|
|| !Security::database_is_ready()
|
2010-10-15 05:43:30 +02:00
|
|
|
// We need to ensure that DevelopmentAdminTest can simulate permission failures when running
|
2014-08-15 08:53:05 +02:00
|
|
|
// "dev/tests" from CLI.
|
2010-11-30 06:13:09 +01:00
|
|
|
|| (Director::is_cli() && !$isRunningTests)
|
2009-03-17 23:23:52 +01:00
|
|
|
|| Permission::check("ADMIN")
|
|
|
|
);
|
|
|
|
if(!$canAccess) {
|
|
|
|
return 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.");
|
|
|
|
}
|
|
|
|
}
|
2008-02-25 03:10:37 +01:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
/**
|
|
|
|
* Get the data classes, grouped by their root class
|
2007-08-06 02:47:31 +02:00
|
|
|
*
|
|
|
|
* @return array Array of data classes, grouped by their root class
|
2007-07-19 12:40:28 +02:00
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function groupedDataClasses() {
|
2007-08-06 02:47:31 +02:00
|
|
|
// Get all root data objects
|
2007-07-19 12:40:28 +02:00
|
|
|
$allClasses = get_declared_classes();
|
|
|
|
foreach($allClasses as $class) {
|
2007-08-06 02:47:31 +02:00
|
|
|
if(get_parent_class($class) == "DataObject")
|
|
|
|
$rootClasses[$class] = array();
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
2007-08-06 02:47:31 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
// Assign every other data object one of those
|
|
|
|
foreach($allClasses as $class) {
|
|
|
|
if(!isset($rootClasses[$class]) && is_subclass_of($class, "DataObject")) {
|
|
|
|
foreach($rootClasses as $rootClass => $dummy) {
|
|
|
|
if(is_subclass_of($class, $rootClass)) {
|
|
|
|
$rootClasses[$rootClass][] = $class;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return $rootClasses;
|
|
|
|
}
|
2007-08-06 02:47:31 +02:00
|
|
|
|
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
/**
|
2013-01-24 23:43:10 +01:00
|
|
|
* When we're called as /dev/build, that's actually the index. Do the same
|
|
|
|
* as /dev/build/build.
|
2007-07-19 12:40:28 +02:00
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function index() {
|
2013-01-24 23:43:10 +01:00
|
|
|
return $this->build();
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
2007-08-06 02:47:31 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
/**
|
|
|
|
* Updates the database schema, creating tables & fields as necessary.
|
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function build() {
|
2007-08-06 02:47:31 +02:00
|
|
|
// The default time limit of 30 seconds is normally not enough
|
2009-06-28 04:36:46 +02:00
|
|
|
increase_time_limit_to(600);
|
2007-08-06 02:47:31 +02:00
|
|
|
|
2008-09-25 02:03:09 +02:00
|
|
|
// Get all our classes
|
2011-03-22 10:47:55 +01:00
|
|
|
SS_ClassLoader::instance()->getManifest()->regenerate();
|
|
|
|
|
2015-09-07 23:46:57 +02:00
|
|
|
$url = $this->getReturnURL();
|
|
|
|
if($url) {
|
2009-03-10 23:08:52 +01:00
|
|
|
echo "<p>Setting up the database; you will be returned to your site shortly....</p>";
|
|
|
|
$this->doBuild(true);
|
|
|
|
echo "<p>Done!</p>";
|
2015-09-07 23:46:57 +02:00
|
|
|
$this->redirect($url);
|
2009-03-10 23:08:52 +01:00
|
|
|
} else {
|
2015-09-07 23:46:57 +02:00
|
|
|
$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);
|
2009-03-10 23:08:52 +01:00
|
|
|
}
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
|
|
|
|
2015-09-07 23:46:57 +02:00
|
|
|
/**
|
|
|
|
* 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);
|
|
|
|
}
|
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
/**
|
|
|
|
* Check if database needs to be built, and build it if it does.
|
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public static function autoBuild() {
|
2007-07-19 12:40:28 +02:00
|
|
|
$dataClasses = ClassInfo::subclassesFor('DataObject');
|
|
|
|
$lastBuilt = self::lastBuilt();
|
|
|
|
foreach($dataClasses as $class) {
|
|
|
|
if(filemtime(getClassFile($class)) > $lastBuilt) {
|
|
|
|
$da = new DatabaseAdmin();
|
|
|
|
$da->doBuild(true);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2007-08-06 02:47:31 +02:00
|
|
|
|
2010-10-15 01:57:02 +02:00
|
|
|
/**
|
|
|
|
* Build the default data, calling requireDefaultRecords on all
|
|
|
|
* DataObject classes
|
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function buildDefaults() {
|
2010-10-15 01:57:02 +02:00
|
|
|
$dataClasses = ClassInfo::subclassesFor('DataObject');
|
|
|
|
array_shift($dataClasses);
|
|
|
|
foreach($dataClasses as $dataClass){
|
|
|
|
singleton($dataClass)->requireDefaultRecords();
|
|
|
|
print "Defaults loaded for $dataClass<br/>";
|
|
|
|
}
|
|
|
|
}
|
2007-08-06 02:47:31 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
/**
|
|
|
|
* Returns the timestamp of the time that the database was last built
|
2007-08-06 02:47:31 +02:00
|
|
|
*
|
|
|
|
* @return string Returns the timestamp of the time that the database was
|
|
|
|
* last built
|
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public static function lastBuilt() {
|
2007-08-06 02:47:31 +02:00
|
|
|
$file = TEMP_FOLDER . '/database-last-generated-' .
|
|
|
|
str_replace(array('\\','/',':'), '.' , Director::baseFolder());
|
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
if(file_exists($file)) {
|
|
|
|
return filemtime($file);
|
|
|
|
}
|
|
|
|
}
|
2007-08-06 02:47:31 +02:00
|
|
|
|
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
/**
|
|
|
|
* Updates the database schema, creating tables & fields as necessary.
|
2007-08-06 02:47:31 +02:00
|
|
|
*
|
|
|
|
* @param boolean $quiet Don't show messages
|
2007-08-15 08:38:41 +02:00
|
|
|
* @param boolean $populate Populate the database, as well as setting up its schema
|
2007-07-19 12:40:28 +02:00
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function doBuild($quiet = false, $populate = true, $testMode = false) {
|
2007-07-19 12:40:28 +02:00
|
|
|
if($quiet) {
|
|
|
|
DB::quiet();
|
|
|
|
} else {
|
2013-06-21 00:32:08 +02:00
|
|
|
$conn = DB::get_conn();
|
2010-10-19 07:02:02 +02:00
|
|
|
// Assumes database class is like "MySQLDatabase" or "MSSQLDatabase" (suffixed with "Database")
|
|
|
|
$dbType = substr(get_class($conn), 0, -8);
|
|
|
|
$dbVersion = $conn->getVersion();
|
2013-06-21 00:32:08 +02:00
|
|
|
$databaseName = (method_exists($conn, 'currentDatabase')) ? $conn->getSelectedDatabase() : "";
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-09-26 23:34:00 +02:00
|
|
|
if(Director::is_cli()) {
|
|
|
|
echo sprintf("\n\nBuilding database %s using %s %s\n\n", $databaseName, $dbType, $dbVersion);
|
|
|
|
} else {
|
|
|
|
echo sprintf("<h2>Building database %s using %s %s</h2>", $databaseName, $dbType, $dbVersion);
|
|
|
|
}
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Set up the initial database
|
2013-06-21 00:32:08 +02:00
|
|
|
if(!DB::is_active()) {
|
2007-07-19 12:40:28 +02:00
|
|
|
if(!$quiet) {
|
|
|
|
echo '<p><b>Creating database</b></p>';
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2013-06-21 00:32:08 +02:00
|
|
|
// Load parameters from existing configuration
|
2007-11-09 03:50:35 +01:00
|
|
|
global $databaseConfig;
|
2013-06-21 00:32:08 +02:00
|
|
|
if(empty($databaseConfig) && empty($_REQUEST['db'])) {
|
|
|
|
user_error("No database configuration available", E_USER_ERROR);
|
|
|
|
}
|
|
|
|
$parameters = (!empty($databaseConfig)) ? $databaseConfig : $_REQUEST['db'];
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2013-06-21 00:32:08 +02:00
|
|
|
// Check database name is given
|
|
|
|
if(empty($parameters['database'])) {
|
2012-09-26 23:34:00 +02:00
|
|
|
user_error("No database name given; please give a value for \$databaseConfig['database']",
|
2013-06-21 00:32:08 +02:00
|
|
|
E_USER_ERROR);
|
2008-11-09 23:11:25 +01:00
|
|
|
}
|
2013-06-21 00:32:08 +02:00
|
|
|
$database = $parameters['database'];
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2013-06-21 00:32:08 +02:00
|
|
|
// Establish connection and create database in two steps
|
|
|
|
unset($parameters['database']);
|
|
|
|
DB::connect($parameters);
|
|
|
|
DB::create_database($database);
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
|
|
|
|
2007-08-06 02:47:31 +02:00
|
|
|
// Build the database. Most of the hard work is handled by DataObject
|
2007-07-19 12:40:28 +02:00
|
|
|
$dataClasses = ClassInfo::subclassesFor('DataObject');
|
|
|
|
array_shift($dataClasses);
|
2007-08-06 02:47:31 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
if(!$quiet) {
|
2008-08-13 05:41:21 +02:00
|
|
|
if(Director::is_cli()) echo "\nCREATING DATABASE TABLES\n\n";
|
|
|
|
else echo "\n<p><b>Creating database tables</b></p>\n\n";
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
2007-08-20 01:47:49 +02:00
|
|
|
|
2013-06-21 00:32:08 +02:00
|
|
|
// 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;
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2013-06-21 00:32:08 +02:00
|
|
|
// Check if this class should be excluded as per testing conventions
|
2009-03-04 04:44:11 +01:00
|
|
|
$SNG = singleton($dataClass);
|
2013-06-21 00:32:08 +02:00
|
|
|
if(!$testMode && $SNG instanceof TestOnly) continue;
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2013-06-21 00:32:08 +02:00
|
|
|
// Log data
|
|
|
|
if(!$quiet) {
|
|
|
|
if(Director::is_cli()) echo " * $dataClass\n";
|
|
|
|
else echo "<li>$dataClass</li>\n";
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2013-06-21 00:32:08 +02:00
|
|
|
// Instruct the class to apply its schema to the database
|
|
|
|
$SNG->requireTable();
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
2013-06-21 00:32:08 +02:00
|
|
|
});
|
2010-10-13 05:44:14 +02:00
|
|
|
ClassInfo::reset_db_cache();
|
2007-09-16 17:28:08 +02:00
|
|
|
|
2007-08-15 08:38:41 +02:00
|
|
|
if($populate) {
|
|
|
|
if(!$quiet) {
|
2008-08-13 05:41:21 +02:00
|
|
|
if(Director::is_cli()) echo "\nCREATING DATABASE RECORDS\n\n";
|
|
|
|
else echo "\n<p><b>Creating database records</b></p>\n\n";
|
2007-08-15 08:38:41 +02:00
|
|
|
}
|
2007-08-06 02:47:31 +02:00
|
|
|
|
2007-08-15 08:38:41 +02:00
|
|
|
foreach($dataClasses as $dataClass) {
|
2009-03-04 04:44:11 +01:00
|
|
|
// Check if class exists before trying to instantiate - this sidesteps any manifest weirdness
|
2007-08-15 08:38:41 +02:00
|
|
|
// Test_ indicates that it's the data class is part of testing system
|
2009-03-04 04:44:11 +01:00
|
|
|
if(strpos($dataClass,'Test_') === false && class_exists($dataClass)) {
|
2007-08-15 08:38:41 +02:00
|
|
|
if(!$quiet) {
|
2008-08-13 05:41:21 +02:00
|
|
|
if(Director::is_cli()) echo " * $dataClass\n";
|
|
|
|
else echo "<li>$dataClass</li>\n";
|
2007-08-15 08:38:41 +02:00
|
|
|
}
|
2007-07-19 12:40:28 +02:00
|
|
|
|
2007-08-15 08:38:41 +02:00
|
|
|
singleton($dataClass)->requireDefaultRecords();
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2007-08-06 02:47:31 +02:00
|
|
|
|
2013-06-21 00:32:08 +02:00
|
|
|
touch(TEMP_FOLDER
|
|
|
|
. '/database-last-generated-'
|
|
|
|
. str_replace(array('\\', '/', ':'), '.', Director::baseFolder())
|
|
|
|
);
|
2007-08-06 02:47:31 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
if(isset($_REQUEST['from_installer'])) {
|
|
|
|
echo "OK";
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2010-10-15 04:58:21 +02:00
|
|
|
if(!$quiet) {
|
|
|
|
echo (Director::is_cli()) ? "\n Database build completed!\n\n" :"<p>Database build completed!</p>";
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2010-04-12 04:03:16 +02:00
|
|
|
ClassInfo::reset_db_cache();
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2008-08-13 04:47:14 +02:00
|
|
|
/**
|
|
|
|
* Clear all data out of the database
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2015-06-19 01:59:27 +02:00
|
|
|
* @deprecated since version 4.0
|
2008-08-13 04:47:14 +02:00
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function clearAllData() {
|
2015-06-19 01:59:27 +02:00
|
|
|
Deprecation::notice('4.0', 'Use DB::get_conn()->clearAllData() instead');
|
2013-06-21 00:32:08 +02:00
|
|
|
DB::get_conn()->clearAllData();
|
2008-08-13 04:47:14 +02:00
|
|
|
}
|
2007-08-06 02:47:31 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
/**
|
2007-08-06 02:47:31 +02:00
|
|
|
* Remove invalid records from tables - that is, records that don't have
|
|
|
|
* corresponding records in their parent class tables.
|
2007-07-19 12:40:28 +02:00
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function cleanup() {
|
2007-07-19 12:40:28 +02:00
|
|
|
$allClasses = get_declared_classes();
|
|
|
|
foreach($allClasses as $class) {
|
|
|
|
if(get_parent_class($class) == 'DataObject') {
|
|
|
|
$baseClasses[] = $class;
|
|
|
|
}
|
|
|
|
}
|
2007-08-06 02:47:31 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
foreach($baseClasses as $baseClass) {
|
|
|
|
// Get data classes
|
|
|
|
$subclasses = ClassInfo::subclassesFor($baseClass);
|
|
|
|
unset($subclasses[0]);
|
|
|
|
foreach($subclasses as $k => $subclass) {
|
2013-06-21 00:32:08 +02:00
|
|
|
if(DataObject::has_own_table($subclass)) {
|
2007-07-19 12:40:28 +02:00
|
|
|
unset($subclasses[$k]);
|
|
|
|
}
|
|
|
|
}
|
2007-08-06 02:47:31 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
if($subclasses) {
|
2008-11-23 01:31:06 +01:00
|
|
|
$records = DB::query("SELECT * FROM \"$baseClass\"");
|
2007-08-06 02:47:31 +02:00
|
|
|
|
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
foreach($subclasses as $subclass) {
|
2007-08-06 02:47:31 +02:00
|
|
|
$recordExists[$subclass] =
|
2008-11-24 10:31:14 +01:00
|
|
|
DB::query("SELECT \"ID\" FROM \"$subclass\"")->keyedColumn();
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
2007-08-06 02:47:31 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
foreach($records as $record) {
|
|
|
|
foreach($subclasses as $subclass) {
|
|
|
|
$id = $record['ID'];
|
2007-08-06 02:47:31 +02:00
|
|
|
if(($record['ClassName'] != $subclass) &&
|
2012-12-08 12:20:20 +01:00
|
|
|
(!is_subclass_of($record['ClassName'], $subclass)) &&
|
|
|
|
(isset($recordExists[$subclass][$id]))) {
|
2008-11-24 00:28:16 +01:00
|
|
|
$sql = "DELETE FROM \"$subclass\" WHERE \"ID\" = $record[ID]";
|
2007-07-19 12:40:28 +02:00
|
|
|
echo "<li>$sql";
|
|
|
|
DB::query($sql);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2007-08-06 02:47:31 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|