| 1 |
<?php
|
| 2 |
/**
|
| 3 |
* @file: Script to parse a Drupal script template. Examples are in druplaroot/templates/
|
| 4 |
*/
|
| 5 |
function print_help() {
|
| 6 |
global $argv;
|
| 7 |
print "usage: $argv[0] name=value name2=value2 ... nameN=valueN\n";
|
| 8 |
print "required variables are: \n";
|
| 9 |
foreach (get_required() as $variable) {
|
| 10 |
print "\t$variable->name (example: $variable->name=$variable->example)\n";
|
| 11 |
}
|
| 12 |
print "optional variables are: \n";
|
| 13 |
foreach (get_optional() as $variable) {
|
| 14 |
print "\t$variable->name (example: $variable->name=$variable->example)\n";
|
| 15 |
}
|
| 16 |
exit;
|
| 17 |
}
|
| 18 |
|
| 19 |
/**
|
| 20 |
* Parses a template
|
| 21 |
* @args: $filename: file name of the file to parse. Relative to drupal root
|
| 22 |
* $variables: array of variables. array('var1_name' => $var1_value, etc)
|
| 23 |
* @return: the contents of a file that is parsed.
|
| 24 |
*/
|
| 25 |
function parse_file($file_name, $variables) {
|
| 26 |
$content = file_get_contents($file_name);
|
| 27 |
foreach ($variables as $variable) {
|
| 28 |
$search[$variable->name] = '@<token>\s?'. $variable->name .'\s?</token>@si';
|
| 29 |
$replacement[$variable->name] = $variable->value;
|
| 30 |
}
|
| 31 |
$content = preg_replace($search, $replacement, $content);
|
| 32 |
return $content;
|
| 33 |
}
|
| 34 |
|
| 35 |
/**
|
| 36 |
* Report status messages to the console.
|
| 37 |
* TODO Do some strlength and so to make the output a lot nicer
|
| 38 |
*/
|
| 39 |
function report_to_console($message, $type, $status) {
|
| 40 |
if ($status) {
|
| 41 |
$status = "[OK]";
|
| 42 |
}
|
| 43 |
else {
|
| 44 |
$status = "[FALSE]";
|
| 45 |
}
|
| 46 |
$msg = "$type:\t\t$message\t\t$status\n";
|
| 47 |
print $msg;
|
| 48 |
}
|
| 49 |
|
| 50 |
/**
|
| 51 |
* Parses the argument list --name value pairs
|
| 52 |
* @return an array containing all the variable names as key, and values as value.
|
| 53 |
*/
|
| 54 |
function parse_dash_arguments($name = '') {
|
| 55 |
global $argv;
|
| 56 |
$variables = array();
|
| 57 |
foreach ($argv as $entry) {
|
| 58 |
if (strpos($entry, '--') === 0) {
|
| 59 |
$key = substr($entry, 2);
|
| 60 |
$next = key($argv);
|
| 61 |
$value = '';
|
| 62 |
if ($argv[$next] && (strpos($argv[$next], '--') === false)) {
|
| 63 |
$value = $argv[$next];
|
| 64 |
next($argv);
|
| 65 |
}
|
| 66 |
$variables[$key] = $value;
|
| 67 |
}
|
| 68 |
}
|
| 69 |
|
| 70 |
if ($name) {
|
| 71 |
return $variables[$name];
|
| 72 |
}
|
| 73 |
|
| 74 |
return $variables;
|
| 75 |
}
|
| 76 |
|
| 77 |
/**
|
| 78 |
* Parses the argument list name=value pairs
|
| 79 |
* @return an array containing all the variable names as key, and values as value.
|
| 80 |
*/
|
| 81 |
function parse_arguments() {
|
| 82 |
global $argv;
|
| 83 |
$variables = implode('&',$argv);
|
| 84 |
parse_str($variables, $output);
|
| 85 |
unset($output[0]);
|
| 86 |
return $output;
|
| 87 |
}
|
| 88 |
|
| 89 |
/**
|
| 90 |
* Filters out all non registered arguments.
|
| 91 |
* @return an array containing all the variable objects that got a value.
|
| 92 |
*/
|
| 93 |
function parse_and_filter_arguments() {
|
| 94 |
$registered_arr = get_registered();
|
| 95 |
$arguments = parse_arguments();
|
| 96 |
$variables = array();
|
| 97 |
foreach ($registered_arr as $registered) {
|
| 98 |
if (in_array($registered->name, array_keys($arguments))) {
|
| 99 |
$variables[$registered->name] = $registered;
|
| 100 |
$variables[$registered->name]->value = $arguments[$registered->name];
|
| 101 |
}
|
| 102 |
}
|
| 103 |
return $variables;
|
| 104 |
}
|
| 105 |
|
| 106 |
/**
|
| 107 |
* Find out if all the required variables were filled.
|
| 108 |
* @return an array containing all the variable objects that got a value.
|
| 109 |
*/
|
| 110 |
function are_all_required_filled() {
|
| 111 |
$required = get_required();
|
| 112 |
$arguments = parse_and_filter_arguments();
|
| 113 |
foreach ($required as $variable) {
|
| 114 |
if (!in_array($variable->name, array_keys($arguments))) {
|
| 115 |
return FALSE;
|
| 116 |
}
|
| 117 |
}
|
| 118 |
return TRUE;
|
| 119 |
}
|
| 120 |
|
| 121 |
/**
|
| 122 |
* register a variable;
|
| 123 |
*/
|
| 124 |
function register_var($name, $example, $required = TRUE) {
|
| 125 |
global $registered;
|
| 126 |
$registered[$name]->example = $example;
|
| 127 |
$registered[$name]->name = $name;
|
| 128 |
if ($required) {
|
| 129 |
$registered[$name]->required = TRUE;
|
| 130 |
}
|
| 131 |
}
|
| 132 |
|
| 133 |
/**
|
| 134 |
* returns a list of registered variables
|
| 135 |
*/
|
| 136 |
function get_registered() {
|
| 137 |
global $registered;
|
| 138 |
return $registered;
|
| 139 |
}
|
| 140 |
|
| 141 |
/**
|
| 142 |
* Give a list of all required variables
|
| 143 |
*/
|
| 144 |
function get_required() {
|
| 145 |
global $registered;
|
| 146 |
$required = array();
|
| 147 |
//prefill the required ones
|
| 148 |
//$required = TemplateVariables::get_required();
|
| 149 |
foreach ($registered as $name => $variable) {
|
| 150 |
if ($variable->required) {
|
| 151 |
$required[$name] = $variable;
|
| 152 |
}
|
| 153 |
}
|
| 154 |
return $required;
|
| 155 |
}
|
| 156 |
|
| 157 |
/**
|
| 158 |
* Give a list of all optional variables
|
| 159 |
*/
|
| 160 |
function get_optional() {
|
| 161 |
global $registered;
|
| 162 |
$optional = array();
|
| 163 |
//prefill the required ones
|
| 164 |
//$required = TemplateVariables::get_required();
|
| 165 |
foreach ($registered as $name => $variable) {
|
| 166 |
if (!$variable->required) {
|
| 167 |
$optional[$name] = $variable;
|
| 168 |
}
|
| 169 |
}
|
| 170 |
return $optional;
|
| 171 |
}
|
| 172 |
/**
|
| 173 |
* Register the core required variables
|
| 174 |
*/
|
| 175 |
function init_core() {
|
| 176 |
register_var('domain', 'example.com', TRUE);
|
| 177 |
register_var('mysql_username', 'root', TRUE);
|
| 178 |
register_var('mysql_password', 'r007', TRUE);
|
| 179 |
register_var('database_name', 'examplecom', TRUE);
|
| 180 |
register_var('main_email', 'main_email@othersite.com', TRUE);
|
| 181 |
}
|
| 182 |
|
| 183 |
/**
|
| 184 |
* Find all installed domains in the drupal installation at $drupal_dir
|
| 185 |
**/
|
| 186 |
function list_all_domains($drupal_dir) {
|
| 187 |
$files = glob($drupal_dir .'/sites/*', GLOB_ONLYDIR);
|
| 188 |
|
| 189 |
$domains = array();
|
| 190 |
|
| 191 |
foreach ($files as $file) {
|
| 192 |
$domain = array_pop(explode('/', $file));
|
| 193 |
if ($domain && ($domain != 'CVS') && ($domain != 'default') &&
|
| 194 |
file_exists($file .'/settings.php')) {
|
| 195 |
$domains[] = $domain;
|
| 196 |
}
|
| 197 |
}
|
| 198 |
|
| 199 |
return $domains;
|
| 200 |
}
|
| 201 |
/**
|
| 202 |
* Helper functions
|
| 203 |
*/
|
| 204 |
/**
|
| 205 |
* PHP5 file function
|
| 206 |
* TODO Do not create this function when running PHP5
|
| 207 |
*/
|
| 208 |
function file_put_contents($file_name, $contents) {
|
| 209 |
if (!$handle = fopen($file_name, 'w')) {
|
| 210 |
echo "Cannot open file for writing ($file_name)\n";
|
| 211 |
exit;
|
| 212 |
}
|
| 213 |
// Write $somecontent to our opened file.
|
| 214 |
if (fwrite($handle, $contents) === FALSE) {
|
| 215 |
echo "Cannot write to file ($file_name)\n";
|
| 216 |
exit;
|
| 217 |
}
|
| 218 |
fclose($handle);
|
| 219 |
}
|
| 220 |
?>
|