From 0447d8ed501825efdc832f2d8d480b19cd27afd7 Mon Sep 17 00:00:00 2001
From: Tim Steiner <tsteiner2@unl.edu>
Date: Tue, 20 Jul 2010 19:23:41 +0000
Subject: [PATCH] Cron based automated site creation tools.  Only does
 sub-sites right now, not subdomains.

git-svn-id: file:///tmp/wdn_thm_drupal/branches/drupal-7.x@143 20a16fea-79d4-4915-8869-1ea9d5ebf173
---
 sites/all/modules/unl/cron.php              | 90 +++++++++++++++++++++
 sites/all/modules/unl/unl.info              |  4 +-
 sites/all/modules/unl/unl.install           | 89 ++++++++++++++++++++
 sites/all/modules/unl/unl_site_creation.php | 45 ++---------
 4 files changed, 187 insertions(+), 41 deletions(-)
 create mode 100644 sites/all/modules/unl/cron.php
 create mode 100644 sites/all/modules/unl/unl.install

diff --git a/sites/all/modules/unl/cron.php b/sites/all/modules/unl/cron.php
new file mode 100644
index 00000000..31fad76e
--- /dev/null
+++ b/sites/all/modules/unl/cron.php
@@ -0,0 +1,90 @@
+<?php
+
+if (PHP_SAPI != 'cli') {
+    echo 'This script must be run from the shell!';
+    exit;
+}
+
+chdir(dirname(__FILE__) . '/../../../..');
+define('DRUPAL_ROOT', getcwd());
+
+require_once DRUPAL_ROOT . '/includes/bootstrap.inc';
+drupal_override_server_variables();
+drupal_bootstrap(DRUPAL_BOOTSTRAP_FULL);
+
+$query = db_query('SELECT * FROM {unl_sites} WHERE installed=0');
+while ($row = $query->fetchAssoc()) {
+    db_update('unl_sites')
+        ->fields(array('installed' => 1))
+        ->condition('site_id', $row['site_id'])
+        ->execute();
+    unl_add_site($row['site_path_prefix'], $row['site_path'], $row['uri']);
+    db_update('unl_sites')
+        ->fields(array('installed' => 2))
+        ->condition('site_id', $row['site_id'])
+        ->execute();
+}
+
+
+function unl_add_site($site_path_prefix, $site_path, $uri)
+{
+    
+    if (substr($site_path, 0, 1) == '/') {
+        $site_path = substr($site_path, 1);
+    }
+    if (substr($site_path, -1) == '/') {
+        $site_path = substr($site_path, 0, -1);
+    }
+    if (substr($site_path_prefix, 0, 1) == '/') {
+        $site_path_prefix = substr($site_path_prefix, 1);
+    }
+    if (substr($site_path_prefix, -1) == '/') {
+        $site_path_prefix = substr($site_path_prefix, 0, -1);
+    }
+    
+    $full_path = $site_path;
+    if ($site_path_prefix) {
+        $full_path = $site_path_prefix . '/' . $full_path;
+    }
+    
+    $path_parts = parse_url($uri);
+    $sites_subdir = $path_parts['host'] . $path_parts['path'];
+    $sites_subdir = strtr($sites_subdir, array('/' => '.')); 
+    
+    
+    $database = $GLOBALS['databases']['default']['default'];
+    $db_url = $database['driver']
+            . '://' . $database['username']
+            . ':'   . $database['password']
+            . '@'   . $database['host']
+            . ($database['port'] ? ':' . $database['port'] : '') 
+            . '/'   . $database['database']
+            ;
+    $db_prefix = explode('/', $site_path);
+    $db_prefix = array_reverse($db_prefix);
+    $db_prefix = implode('_', $db_prefix) . '_' . $database['prefix'];
+    
+    $php_path = escapeshellarg($_SERVER['_']);
+    $drupal_root = escapeshellarg(DRUPAL_ROOT);
+    $uri = escapeshellarg($uri);
+    $sites_subdir = escapeshellarg($sites_subdir);
+    $db_url = escapeshellarg($db_url);
+    $db_prefix = escapeshellarg($db_prefix);
+    
+    $subdir = explode('/', $full_path);
+    $symlink_name = array_pop($subdir);
+    $subdir_levels = count($subdir);
+    $subdir = implode('/', $subdir);
+    
+    $symlink_target = array();
+    for ($i = 0; $i < $subdir_levels; $i++) {
+        $symlink_target[] = '..';
+    }
+    $symlink_target = implode('/', $symlink_target);
+    
+    $command = "$php_path sites/all/modules/drush/drush.php -y --uri=$uri site-install unl_profile --sites-subdir=$sites_subdir --db-url=$db_url --db-prefix=$db_prefix --clean-url=0";
+    
+    mkdir($subdir, 0755, TRUE);
+    symlink($symlink_target, $subdir . '/' . $symlink_name);
+    shell_exec($command);
+}
\ No newline at end of file
diff --git a/sites/all/modules/unl/unl.info b/sites/all/modules/unl/unl.info
index a242ff2a..1924b4ea 100644
--- a/sites/all/modules/unl/unl.info
+++ b/sites/all/modules/unl/unl.info
@@ -1,6 +1,8 @@
-; $Id$
 name = UNL WDN
 description = Adds features to allow wysiwyg editing of WDN Templated sites.
 core = 7.x
+version = "7.x-1.0-20100720"
+
 dependencies[] = wysiwyg
+files[] = unl.install
 files[] = unl.module
diff --git a/sites/all/modules/unl/unl.install b/sites/all/modules/unl/unl.install
new file mode 100644
index 00000000..04d71e68
--- /dev/null
+++ b/sites/all/modules/unl/unl.install
@@ -0,0 +1,89 @@
+<?php
+
+function unl_schema()
+{
+	$schema = array();
+    $schema['unl_sites'] = array(
+        'description' => 'Table of tables to be programatically created',
+        'fields' => array(
+            'site_id' => array(
+                'type'     => 'serial',
+                'unsigned' => TRUE,
+                'not null' => TRUE
+            ),
+            'site_path_prefix' => array(
+                'type'     => 'varchar',
+                'length'   => 255,
+                'not null' => TRUE,
+                'default'  => ''
+            ),
+            'site_path' => array(
+                'type'     => 'varchar',
+                'length'   => 255,
+                'not null' => TRUE,
+                'default'  => ''
+            ),
+            'uri' => array(
+                'type'     => 'varchar',
+                'length'   => 255,
+                'not null' => TRUE,
+                'default'  => ''
+            ),
+           	'installed' => array(
+           		'type'	   => 'int',
+           		'not null' => TRUE,
+           		'default'  => 0
+           	)
+        ),
+        'primary key' => array('site_id'),
+        'unique keys' => array(
+            'sub_site' => array('site_path_prefix', 'site_path') 
+        )
+    );
+    
+    
+    return $schema;
+}
+
+function unl_update_7100()
+{
+	$table = array(
+        'description' => 'Table of tables to be programatically created',
+        'fields' => array(
+            'site_id' => array(
+                'type'     => 'serial',
+                'unsigned' => TRUE,
+                'not null' => TRUE
+            ),
+            'site_path_prefix' => array(
+                'type'     => 'varchar',
+                'length'   => 255,
+                'not null' => TRUE,
+                'default'  => ''
+            ),
+            'site_path' => array(
+                'type'     => 'varchar',
+                'length'   => 255,
+                'not null' => TRUE,
+                'default'  => ''
+            ),
+            'uri' => array(
+                'type'     => 'varchar',
+                'length'   => 255,
+                'not null' => TRUE,
+                'default'  => ''
+            ),
+           	'installed' => array(
+           		'type'	   => 'int',
+           		'not null' => TRUE,
+           		'default'  => 0
+           	)
+        ),
+        'primary key' => array('site_id'),
+        'unique keys' => array(
+            'sub_site' => array('site_path_prefix', 'site_path') 
+        )
+    );
+    
+    db_create_table('unl_sites', $table);
+}
diff --git a/sites/all/modules/unl/unl_site_creation.php b/sites/all/modules/unl/unl_site_creation.php
index 0cf8992e..749a69c4 100644
--- a/sites/all/modules/unl/unl_site_creation.php
+++ b/sites/all/modules/unl/unl_site_creation.php
@@ -15,14 +15,6 @@ function unl_site_creation($form, &$form_state)
         '#title' => 'Site Creation Tool'
     );
     
-    $form['root']['php_path'] = array(
-        '#type' => 'textfield',
-        '#title' => t('PHP Path'),
-        '#description' => t('Full Path to the server\'s PHP binary'),
-        '#default_value' => t('/usr/bin/php'),
-        '#required' => TRUE
-    );
-    
     $form['root']['site_path_prefix'] = array(
         '#type' => 'textfield',
         '#title' => t('Site path prefix'),
@@ -69,40 +61,13 @@ function unl_site_creation_submit($form, &$form_state)
         $full_path = $site_path_prefix . '/' . $full_path;
     }
     
-            
     $uri = url($full_path, array('absolute' => TRUE));
-    $path_parts = parse_url($uri);
-    $sites_subdir = $path_parts['host'] . $path_parts['path'];
-    $sites_subdir = strtr($sites_subdir, array('/' => '.')); 
-    
-    
-    $database = $GLOBALS['databases']['default']['default'];
-    $db_url = $database['driver']
-            . '://' . $database['username']
-            . ':'   . $database['password']
-            . '@'   . $database['host']
-            . ($database['port'] ? ':' . $database['port'] : '') 
-            . '/'   . $database['database']
-            ;
-    $db_prefix = explode('/', $site_path);
-    $db_prefix = array_reverse($db_prefix);
-    $db_prefix = implode('_', $db_prefix) . '_' . $database['prefix'];
-    
-    
-    $php_path = escapeshellarg($php_path);
-    $drupal_root = escapeshellarg(DRUPAL_ROOT);
-    $uri = escapeshellarg($uri);
-    $sites_subdir = escapeshellarg($sites_subdir);
-    $db_url = escapeshellarg($db_url);
-    $db_prefix = escapeshellarg($db_prefix);
-   
-    $command = "$php_path sites/all/modules/drush/drush.php --uri=$uri site-install unl_profile --sites-subdir=$sites_subdir --db-url=$db_url --db-prefix=$db_prefix";
-    
-    header('Content-Type: text/plain');
-    echo 'Run the following commands on the drupal server:' . PHP_EOL;
-    echo 'cd ' . $drupal_root . PHP_EOL;
-    echo $command . PHP_EOL;
     
+    db_insert('unl_sites')->fields(array(
+    	'site_path_prefix' => $site_path_prefix,
+        'site_path'        => $site_path,
+        'uri'              => $uri
+    ))->execute();
     
     exit;
 }
\ No newline at end of file
-- 
GitLab