| 1 |
#!/usr/bin/php
|
| 2 |
<?php
|
| 3 |
# Script to install a new Drupal site from commandline in a multisite
|
| 4 |
/**
|
| 5 |
* include the configuration settings.
|
| 6 |
*/
|
| 7 |
include_once('script_settings.php');
|
| 8 |
|
| 9 |
/**
|
| 10 |
* include the parser
|
| 11 |
*/
|
| 12 |
include_once('common.php');
|
| 13 |
|
| 14 |
/**
|
| 15 |
* Include all the .php files found at preinstall.php
|
| 16 |
*/
|
| 17 |
include_all('preinstall.d');
|
| 18 |
|
| 19 |
/**
|
| 20 |
* Creates all the required directories for a new domain
|
| 21 |
* TODO some form of feedback on success or failure would be nice.
|
| 22 |
* TODO check for existing dirs, skip them.
|
| 23 |
*/
|
| 24 |
function create_directories($variables) {
|
| 25 |
global $drupal_dir, $file_user, $file_group;
|
| 26 |
$domain = $variables['domain']->value;
|
| 27 |
$main_dir = $drupal_dir .'/sites/'. $domain;
|
| 28 |
$dirs = array(
|
| 29 |
$main_dir,
|
| 30 |
$main_dir .'/themes',
|
| 31 |
$main_dir .'/modules',
|
| 32 |
$main_dir .'/files',
|
| 33 |
);
|
| 34 |
|
| 35 |
foreach ($dirs as $dir) {
|
| 36 |
if(!file_exists($dir)) {
|
| 37 |
$status = mkdir($dir, 0770);
|
| 38 |
report_to_console($dir, 'Create', $status);
|
| 39 |
$status = chown($dir, $file_user);
|
| 40 |
report_to_console($dir, 'Set user', $status);
|
| 41 |
$status = chgrp($dir, $file_group);
|
| 42 |
report_to_console($dir, 'Set group', $status);
|
| 43 |
}
|
| 44 |
else {
|
| 45 |
report_to_console($dir, 'Skip', 'Files exist');
|
| 46 |
}
|
| 47 |
}
|
| 48 |
}
|
| 49 |
|
| 50 |
/**
|
| 51 |
* Creates all the required databases for a new domain
|
| 52 |
*/
|
| 53 |
function insert_data($variables) {
|
| 54 |
global $template_dir, $sql_binary, $argv;
|
| 55 |
|
| 56 |
//Find all database files
|
| 57 |
$files = glob($template_dir .'/*.mysql');
|
| 58 |
if (count($files) > 0) { //glob always returns an array
|
| 59 |
foreach ($files as $file) {
|
| 60 |
$contents[$file] = parse_file($file, $variables);
|
| 61 |
}
|
| 62 |
$content = implode("\n\n#################### $file ####################\n", $contents);
|
| 63 |
//We now write a temporary database file to the temp dir.
|
| 64 |
//file_put_contents cannot be used, because fclose destroys the tempfile.
|
| 65 |
$tmpfname = tempnam("/tmp", "MYSQL_");
|
| 66 |
file_put_contents($tmpfname, $content);
|
| 67 |
|
| 68 |
//We now pipe this into mysql. Since we are writing a LOT of data, we use the client mysql for this, and not the database connection with PHP.
|
| 69 |
//TODO: rewrite this part to make it use drupals internal database abstraction to write the data to,
|
| 70 |
$cmd = escapeshellcmd($sql_binary .' -u '. $variables['mysql_username']->value .' -p'. $variables['mysql_password']->value .' '. $variables['database_name']->value) .' < '. escapeshellcmd($tmpfname);
|
| 71 |
report_to_console($cmd, "Executing", TRUE);
|
| 72 |
if (exec($cmd)) {
|
| 73 |
report_to_console('Inserted database file', "Database", TRUE);
|
| 74 |
}
|
| 75 |
unlink($tmpfname); // this removes the file
|
| 76 |
}
|
| 77 |
}
|
| 78 |
|
| 79 |
/**
|
| 80 |
* Creates a new settings.php
|
| 81 |
*/
|
| 82 |
function create_settings($variables) {
|
| 83 |
global $template_dir, $drupal_dir;
|
| 84 |
$file_name = $template_dir .'/settings.php';
|
| 85 |
|
| 86 |
$contents = parse_file($file_name, $variables);
|
| 87 |
$file_name = $drupal_dir .'/sites/'. $variables['domain']->value .'/settings.php';
|
| 88 |
$status = file_put_contents($file_name, $contents);
|
| 89 |
report_to_console($file_name, 'Create', $status);
|
| 90 |
}
|
| 91 |
|
| 92 |
/**
|
| 93 |
* Helper function to include the post and preinstall.d pluggable files
|
| 94 |
*/
|
| 95 |
function include_all($dir) {
|
| 96 |
$files = glob($dir .'/*.php');
|
| 97 |
if (count($files) > 0) { //glob always returns an array
|
| 98 |
foreach ($files as $file) {
|
| 99 |
include_once($file);
|
| 100 |
}
|
| 101 |
}
|
| 102 |
}
|
| 103 |
|
| 104 |
/**
|
| 105 |
* This is where the action happens.
|
| 106 |
* Three functions are always ran: to create the directories, create the settings file and insert the data. The rest is pluggable.
|
| 107 |
*/
|
| 108 |
init_core();
|
| 109 |
if (are_all_required_filled()) {
|
| 110 |
$variables = parse_and_filter_arguments();
|
| 111 |
create_directories($variables);
|
| 112 |
create_settings($variables);
|
| 113 |
insert_data($variables);
|
| 114 |
}
|
| 115 |
else {
|
| 116 |
print_help();
|
| 117 |
}
|
| 118 |
|
| 119 |
/**
|
| 120 |
* Include Drupal
|
| 121 |
*/
|
| 122 |
include_once('drupal.php');
|
| 123 |
installer_include_drupal();
|
| 124 |
|
| 125 |
/**
|
| 126 |
* Include all the .php files found at preinstall.php
|
| 127 |
*/
|
| 128 |
include_all('postinstall.d');
|
| 129 |
?>
|