2007-07-19 12:40:28 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
/**
|
2007-08-06 02:47:31 +02:00
|
|
|
* Database Administration
|
|
|
|
*
|
2007-07-19 12:40:28 +02:00
|
|
|
* @package sapphire
|
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
|
|
|
|
|
|
|
/**
|
|
|
|
* Include the DB class
|
|
|
|
*/
|
2007-07-19 12:40:28 +02:00
|
|
|
require_once("core/model/DB.php");
|
|
|
|
|
2007-08-06 02:47:31 +02:00
|
|
|
|
|
|
|
|
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.
|
2008-02-25 03:10:37 +01:00
|
|
|
* @package sapphire
|
|
|
|
* @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 ///
|
|
|
|
static $allowed_actions = array(
|
|
|
|
'build',
|
|
|
|
'cleanup',
|
|
|
|
'testinstall',
|
|
|
|
'import'
|
|
|
|
);
|
|
|
|
|
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
|
|
|
*/
|
|
|
|
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
|
|
|
/**
|
|
|
|
* Display a simple HTML menu of database admin helpers.
|
|
|
|
*/
|
|
|
|
function index() {
|
|
|
|
echo "<h2>Database Administration Helpers</h2>";
|
|
|
|
echo "<p><a href=\"build\">Add missing database fields (similar to sanity check).</a></p>";
|
|
|
|
echo "<p><a href=\"../images/flush\">Flush <b>all</b> of the generated images.</a></p>";
|
|
|
|
}
|
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.
|
|
|
|
*/
|
|
|
|
function build() {
|
2007-11-15 23:29:10 +01:00
|
|
|
if(Director::isLive() && Security::database_is_ready() && (!Member::currentUser() || !Member::currentUser()->isAdmin())) {
|
2007-08-06 02:47:31 +02:00
|
|
|
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.");
|
|
|
|
return;
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
2007-08-06 02:47:31 +02:00
|
|
|
|
|
|
|
// The default time limit of 30 seconds is normally not enough
|
2007-08-13 01:59:02 +02:00
|
|
|
if(ini_get("safe_mode") != "1") {
|
|
|
|
set_time_limit(600);
|
|
|
|
}
|
2007-08-06 02:47:31 +02:00
|
|
|
|
2007-11-15 23:29:10 +01:00
|
|
|
$this->doBuild(isset($_REQUEST['quiet']) || isset($_REQUEST['from_installer']));
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
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
|
|
|
|
*/
|
2007-07-19 12:40:28 +02:00
|
|
|
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
|
|
|
*/
|
Merged revisions 53150,53681,53700,53820,54200,54459 via svnmerge from
svn://svn.silverstripe.com/silverstripe/open/modules/sapphire/branches/roa
........
r53150 | ischommer | 2008-04-22 11:12:43 +1200 (Tue, 22 Apr 2008) | 1 line
FEATURE Added a "test mode" for /db/build which allows mock-DataObject-subclasses which are just built in a test run
........
r53681 | mrickerby | 2008-04-29 15:26:52 +1200 (Tue, 29 Apr 2008) | 1 line
adding default wrapping header and footer methods, and configurable reporting to the TestRunner
........
r53700 | mrickerby | 2008-04-29 16:41:57 +1200 (Tue, 29 Apr 2008) | 1 line
FEATURE: adding support for /dev/tests --> DevelopmentAdmin-->tests() --> TestRunner, /dev/tasks --> DevelopmentAdmin-->tasks() --> TaskRunner
........
r53820 | mrickerby | 2008-04-30 19:27:52 +1200 (Wed, 30 Apr 2008) | 1 line
BUGFIX fixing up BuildTask interface and task runner action
........
r54200 | sminnee | 2008-05-09 00:28:44 +1200 (Fri, 09 May 2008) | 1 line
Added TestSession object to help with the testing of forms
........
r54459 | sminnee | 2008-05-13 17:28:25 +1200 (Tue, 13 May 2008) | 1 line
Added a basic menu of options to /dev
........
git-svn-id: svn://svn.silverstripe.com/silverstripe/open/modules/sapphire/trunk@54456 467b73ca-7a2a-4603-9d3b-597d59a354a9
2008-05-13 07:57:09 +02:00
|
|
|
function doBuild($quiet = false, $populate = true, $testMode = false) {
|
2007-08-15 04:50:39 +02:00
|
|
|
$conn = DB::getConn();
|
2007-09-16 17:28:08 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
if($quiet) {
|
|
|
|
DB::quiet();
|
|
|
|
} else {
|
|
|
|
echo "<h2>Building Database</h2>";
|
|
|
|
}
|
|
|
|
|
|
|
|
// Set up the initial database
|
|
|
|
if(!DB::isActive()) {
|
|
|
|
if(!$quiet) {
|
|
|
|
echo '<p><b>Creating database</b></p>';
|
|
|
|
}
|
2007-11-09 03:50:35 +01:00
|
|
|
global $databaseConfig;
|
|
|
|
$parameters = $databaseConfig ? $databaseConfig : $_REQUEST['db'];
|
2007-09-14 03:28:16 +02:00
|
|
|
$connect = DB::getConnect($parameters);
|
2007-11-11 22:03:33 +01:00
|
|
|
$username = $parameters['username'];
|
|
|
|
$password = $parameters['password'];
|
|
|
|
$database = $parameters['database'];
|
2007-09-14 03:28:16 +02:00
|
|
|
DB::createDatabase($connect, $username, $password, $database);
|
2007-08-20 01:47:49 +02:00
|
|
|
// ManifestBuilder::compileManifest();
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Get all our classes
|
2007-08-15 04:50:39 +02:00
|
|
|
// ManifestBuilder::compileManifest();
|
|
|
|
// ManifestBuilder::includeEverything();
|
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) {
|
|
|
|
echo '<p><b>Creating database tables</b></p>';
|
|
|
|
}
|
2007-08-20 01:47:49 +02:00
|
|
|
|
2007-08-15 04:50:39 +02:00
|
|
|
$conn->beginSchemaUpdate();
|
2007-07-19 12:40:28 +02:00
|
|
|
foreach($dataClasses as $dataClass) {
|
Merged revisions 53150,53681,53700,53820,54200,54459 via svnmerge from
svn://svn.silverstripe.com/silverstripe/open/modules/sapphire/branches/roa
........
r53150 | ischommer | 2008-04-22 11:12:43 +1200 (Tue, 22 Apr 2008) | 1 line
FEATURE Added a "test mode" for /db/build which allows mock-DataObject-subclasses which are just built in a test run
........
r53681 | mrickerby | 2008-04-29 15:26:52 +1200 (Tue, 29 Apr 2008) | 1 line
adding default wrapping header and footer methods, and configurable reporting to the TestRunner
........
r53700 | mrickerby | 2008-04-29 16:41:57 +1200 (Tue, 29 Apr 2008) | 1 line
FEATURE: adding support for /dev/tests --> DevelopmentAdmin-->tests() --> TestRunner, /dev/tasks --> DevelopmentAdmin-->tasks() --> TaskRunner
........
r53820 | mrickerby | 2008-04-30 19:27:52 +1200 (Wed, 30 Apr 2008) | 1 line
BUGFIX fixing up BuildTask interface and task runner action
........
r54200 | sminnee | 2008-05-09 00:28:44 +1200 (Fri, 09 May 2008) | 1 line
Added TestSession object to help with the testing of forms
........
r54459 | sminnee | 2008-05-13 17:28:25 +1200 (Tue, 13 May 2008) | 1 line
Added a basic menu of options to /dev
........
git-svn-id: svn://svn.silverstripe.com/silverstripe/open/modules/sapphire/trunk@54456 467b73ca-7a2a-4603-9d3b-597d59a354a9
2008-05-13 07:57:09 +02:00
|
|
|
$SNG = singleton($dataClass);
|
|
|
|
if($testMode || !($SNG instanceof TestOnly)) {
|
2007-07-19 12:40:28 +02:00
|
|
|
if(!$quiet) {
|
2007-09-09 07:49:21 +02:00
|
|
|
echo "<li>$dataClass</li>";
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
Merged revisions 53150,53681,53700,53820,54200,54459 via svnmerge from
svn://svn.silverstripe.com/silverstripe/open/modules/sapphire/branches/roa
........
r53150 | ischommer | 2008-04-22 11:12:43 +1200 (Tue, 22 Apr 2008) | 1 line
FEATURE Added a "test mode" for /db/build which allows mock-DataObject-subclasses which are just built in a test run
........
r53681 | mrickerby | 2008-04-29 15:26:52 +1200 (Tue, 29 Apr 2008) | 1 line
adding default wrapping header and footer methods, and configurable reporting to the TestRunner
........
r53700 | mrickerby | 2008-04-29 16:41:57 +1200 (Tue, 29 Apr 2008) | 1 line
FEATURE: adding support for /dev/tests --> DevelopmentAdmin-->tests() --> TestRunner, /dev/tasks --> DevelopmentAdmin-->tasks() --> TaskRunner
........
r53820 | mrickerby | 2008-04-30 19:27:52 +1200 (Wed, 30 Apr 2008) | 1 line
BUGFIX fixing up BuildTask interface and task runner action
........
r54200 | sminnee | 2008-05-09 00:28:44 +1200 (Fri, 09 May 2008) | 1 line
Added TestSession object to help with the testing of forms
........
r54459 | sminnee | 2008-05-13 17:28:25 +1200 (Tue, 13 May 2008) | 1 line
Added a basic menu of options to /dev
........
git-svn-id: svn://svn.silverstripe.com/silverstripe/open/modules/sapphire/trunk@54456 467b73ca-7a2a-4603-9d3b-597d59a354a9
2008-05-13 07:57:09 +02:00
|
|
|
$SNG->requireTable();
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
|
|
|
}
|
2007-08-15 04:50:39 +02:00
|
|
|
$conn->endSchemaUpdate();
|
2007-08-06 02:47:31 +02:00
|
|
|
|
2007-08-15 04:50:39 +02:00
|
|
|
ManifestBuilder::update_db_tables();
|
2007-09-16 17:28:08 +02:00
|
|
|
|
2007-08-15 08:38:41 +02:00
|
|
|
if($populate) {
|
|
|
|
if(!$quiet) {
|
|
|
|
echo '<p><b>Creating database records</b></p>';
|
|
|
|
}
|
2007-08-06 02:47:31 +02:00
|
|
|
|
2007-08-15 08:38:41 +02:00
|
|
|
foreach($dataClasses as $dataClass) {
|
|
|
|
// Test_ indicates that it's the data class is part of testing system
|
2007-08-06 02:47:31 +02:00
|
|
|
|
2007-08-15 08:38:41 +02:00
|
|
|
if(strpos($dataClass,'Test_') === false) {
|
|
|
|
if(!$quiet) {
|
2007-09-09 07:49:21 +02:00
|
|
|
echo "<li>$dataClass</li>";
|
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
|
|
|
|
2007-09-16 17:28: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";
|
|
|
|
}
|
|
|
|
}
|
2007-08-06 02:47:31 +02:00
|
|
|
|
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
/**
|
|
|
|
* Method used to check mod_rewrite is working correctly in the installer.
|
|
|
|
*/
|
|
|
|
function testinstall() {
|
|
|
|
echo "OK";
|
|
|
|
}
|
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
|
|
|
*/
|
|
|
|
function cleanup() {
|
|
|
|
$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) {
|
|
|
|
if(!singleton($subclass)->databaseFields()) {
|
|
|
|
unset($subclasses[$k]);
|
|
|
|
}
|
|
|
|
}
|
2007-08-06 02:47:31 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
if($subclasses) {
|
|
|
|
$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] =
|
|
|
|
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) &&
|
|
|
|
(!is_subclass_of($record['ClassName'], $subclass)) &&
|
|
|
|
($recordExists[$subclass][$id])) {
|
2007-07-19 12:40:28 +02:00
|
|
|
$sql = "DELETE FROM `$subclass` WHERE ID = $record[ID]";
|
|
|
|
echo "<li>$sql";
|
|
|
|
DB::query($sql);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2007-08-06 02:47:31 +02:00
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* This method does nothing at the moment...
|
|
|
|
*/
|
|
|
|
function makeURL() {}
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
2007-08-06 02:47:31 +02:00
|
|
|
|
|
|
|
|
2008-04-28 04:24:40 +02:00
|
|
|
?>
|