wCMF  3.6
 All Classes Namespaces Files Functions Variables Groups Pages
class.CreateInstanceController.php
Go to the documentation of this file.
1 <?php
2 /**
3  * wCMF - wemove Content Management Framework
4  * Copyright (C) 2005-2014 wemove digital solutions GmbH
5  *
6  * Licensed under the terms of any of the following licenses
7  * at your choice:
8  *
9  * - GNU Lesser General Public License (LGPL)
10  * http://www.gnu.org/licenses/lgpl.html
11  * - Eclipse Public License (EPL)
12  * http://www.eclipse.org/org/documents/epl-v10.php
13  *
14  * See the license.txt file distributed with this work for
15  * additional information.
16  *
17  * $Id$
18  */
19 require_once(BASE."wcmf/lib/util/class.InifileParser.php");
20 require_once(BASE."wcmf/lib/util/class.FileUtil.php");
21 require_once(BASE."wcmf/lib/util/class.DBUtil.php");
22 
23 /**
24  * @class CreateInstanceController
25  * @ingroup Controller
26  * @brief This Controller creates a new instance of this application.
27  *
28  * <b>Input actions:</b>
29  * - unspecified: Create the index
30  *
31  * <b>Output actions:</b>
32  * - none
33  *
34  * @param[in] newInstanceName The name of the new instance.
35  *
36  * @author ingo herwig <ingo@wemove.com>
37  */
39 {
40  /**
41  * @see Controller::validate()
42  */
43  function validate()
44  {
45  if(strlen($this->_request->getValue('newInstanceName')) == 0)
46  {
47  $this->setErrorMsg("No 'newInstanceName' given in data.");
48  return false;
49  }
50  if (!is_writable($this->getBaseLocation()))
51  {
52  $this->setErrorMsg("The target path is not writable.");
53  return false;
54  }
55  $name = $this->_request->getValue('newInstanceName');
56  if(file_exists($this->getNewInstanceLocation($name)))
57  {
58  $this->setErrorMsg("The instance '".$name."' already exists. Please choose a different name.");
59  return false;
60  }
61  return parent::validate();
62  }
63  /**
64  * @see Controller::hasView()
65  */
66  function hasView()
67  {
68  return false;
69  }
70 
71  /**
72  * @see Controller::executeKernel()
73  */
74  function executeKernel()
75  {
76  $appName = $this->_request->getValue('newInstanceName');
77  $newLocation = $this->getNewInstanceLocation($appName);
78  $newDatabase = $this->getNewInstanceDatabase($appName);
79 
80  // copy the application
81  Log::debug("Create new instance: ".$newLocation, __CLASS__);
82  $directories = FileUtil::getDirectories('../');
83  foreach($directories as $directory) {
84  $sourceDir = "../".$directory;
85  $destDir = $newLocation."/".$directory;
86  Log::debug("Copy directory: ".$sourceDir." to ".$destDir, __CLASS__);
87  FileUtil::copyRecDir($sourceDir, $destDir);
88  }
89 
90  // copy database
91  Log::debug("Create database: ".$newDatabase, __CLASS__);
92  $parser = InifileParser::getInstance();
93  $dbParams = $parser->getSection('database');
94  DBUtil::copyDatabase($dbParams['dbName'], $newDatabase, $dbParams['dbHostName'], $dbParams['dbUserName'], $dbParams['dbPassword']);
95 
96  // configure application (server.ini)
97  global $CONFIG_PATH;
98 
99  $applicationDir = array_pop(split('/', dirname($_SERVER['PHP_SELF'])));
100  $configFilename = $newLocation."/".$applicationDir."/".$CONFIG_PATH.'server.ini';
101  Log::debug("Modify configuration: ".$configFilename, __CLASS__);
102  $configFile = new InifileParser();
103  $configFile->parseIniFile($configFilename, false);
104  if ($configFile->setValue('dbName', $newDatabase, 'database', false) === false) {
105  $this->appendErrorMsg($configFile->getErrorMsg());
106  }
107  $configFile->writeIniFile($configFilename);
108 
109  $this->_response->setAction('ok');
110  return true;
111  }
112 
113  /**
114  * Get the base location for the new instances
115  * @return A relative path to this application
116  */
117  function getBaseLocation()
118  {
119  $parser = InifileParser::getInstance();
120  if (($targetDir = $parser->getValue('targetDir', 'newinstance', false)) === false) {
121  WCMFException::throwEx($parser->getErrorMsg(), __FILE__, __LINE__);
122  }
123  return $targetDir;
124  }
125 
126  /**
127  * Get the location for the new instance
128  * @param name The name of the new instance
129  * @return A relative path to this application
130  */
131  function getNewInstanceLocation($name)
132  {
133  return $this->getBaseLocation().FileUtil::sanitizeFilename($name);
134  }
135 
136  /**
137  * Get the database name for the new instance
138  * @param name The name of the new instance
139  * @return The database name
140  */
141  function getNewInstanceDatabase($name)
142  {
143  $parser = InifileParser::getInstance();
144  if (($dbPrefix = $parser->getValue('dbPrefix', 'newinstance', false)) === false) {
145  WCMFException::throwEx($parser->getErrorMsg(), __FILE__, __LINE__);
146  }
147  return $dbPrefix.strtolower(FileUtil::sanitizeFilename($name));
148  }
149 }
150 ?>
This Controller creates a new instance of this application.
debug($message, $category)
Definition: class.Log.php:39
throwEx($message, $file='', $line='')
Controller is the base class of all controllers. If a Controller has a view it is expected to reside ...
InifileParser provides basic services for parsing a ini file from the file system.
copyRecDir($source, $dest)
sanitizeFilename($name)
getDirectories($directory, $pattern='/./', $prependDirectoryName=false, $recursive=false)
static copyDatabase($srcName, $destName, $server, $user, $password)