wCMF  3.6
 All Classes Namespaces Files Functions Variables Groups Pages
class.BackupController.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: class.BackupController.php 1462 2014-02-04 23:52:27Z iherwig $
18  */
19 require_once(BASE."wcmf/application/controller/class.BatchController.php");
20 require_once(BASE."wcmf/lib/util/class.InifileParser.php");
21 
22 /**
23  * @class BackupController
24  * @ingroup Controller
25  * @brief This Controller creates a backup (action 'makebackup') from a directory
26  * and restores (action 'restorebackup') a created one to that directory respectively.
27  * It creates a directory named after the 'backupName' parameter in the backup directory
28  * whose name is determined by the configuration key 'backupDir' (section 'cms'). Then
29  * it copies all files found in the directory given in the 'sourceDir' parameter to that
30  * directory.
31  * Subclasses may add additional work packages by overriding the
32  * BackupController::getAdditionalWorkPackage method.
33  *
34  * <b>Input actions:</b>
35  * - @em makebackup Create a backup
36  * - @em restorebackup Restore a backup
37  * - more actions see BatchController
38  *
39  * <b>Output actions:</b>
40  * - see BatchController
41  *
42  * @param[in] backupName The name of the backup to create/restore.
43  * @param[in] sourceDir The name of the directory to backup.
44  * @param[out] @see BatchController
45  *
46  * @author ingo herwig <ingo@wemove.com>
47  */
49 {
50  // session name constants
51  var $BACKUP_NAME_VARNAME = 'BackupController.backupName';
52  var $SOURCE_DIR_VARNAME = 'BackupController.sourceDir';
53 
54  /**
55  * @see Controller::validate()
56  */
57  function validate()
58  {
59  $session = &SessionData::getInstance();
60 
61  if(strlen($this->_request->getValue('backupName')) == 0 && !$session->exist($this->BACKUP_NAME_VARNAME))
62  {
63  $this->setErrorMsg("No 'backupName' given in data.");
64  return false;
65  }
66  if(strlen($this->_request->getValue('sourceDir')) == 0 && !$session->exist($this->SOURCE_DIR_VARNAME))
67  {
68  $this->setErrorMsg("No 'sourceDir' given in data.");
69  return false;
70  }
71  return true;
72  }
73  /**
74  * @see Controller::initialize()
75  */
76  function initialize(&$request, &$response)
77  {
78  parent::initialize($request, $response);
79 
80  // store parameters in session
81  if ($request->getAction() != 'continue')
82  {
83  $session = &SessionData::getInstance();
84 
85  // replace illegal characters in backupname
86  $this->_request->setValue('backupName', preg_replace("/[^a-zA-Z0-9\-_\.]+/", "_", $this->_request->getValue('backupName')));
87 
88  $session->set($this->BACKUP_NAME_VARNAME, $this->_request->getValue('backupName'));
89  $session->set($this->SOURCE_DIR_VARNAME, $this->_request->getValue('sourceDir'));
90  }
91  }
92  /**
93  * @see BatchController::getWorkPackage()
94  */
95  function getWorkPackage($number)
96  {
97  if ($number == 0)
98  {
99  if ($this->_request->getAction() == 'makebackup')
100  return array('name' => 'backup files', 'size' => 1, 'oids' => array(1), 'callback' => 'backupFiles');
101  else if ($this->_request->getAction() == 'restorebackup')
102  return array('name' => 'restore files', 'size' => 1, 'oids' => array(1), 'callback' => 'restoreFiles');
103  }
104  else
105  return $this->getAdditionalWorkPackage($number, $this->_request->getAction());
106  }
107 
108  /**
109  * Get definitions of additional work packages (@see BatchController::getWorkPackage()).
110  * The default implementation returns null immediately.
111  * @param number The number of the work package (first number is 1, number is incremented on every call)
112  * @param action 'makebackup' or 'restorebackup'
113  * @note subclasses will override this method to implement special application requirements.
114  */
115  function getAdditionalWorkPackage($number, $action)
116  {
117  return null;
118  }
119 
120  /**
121  * Copy all files from sourceDir to the backup location defined by backupDir and backupName.
122  */
123  function backupFiles()
124  {
125  $session = &SessionData::getInstance();
126  $sourceDir = $session->get($this->SOURCE_DIR_VARNAME);
127 
128  FileUtil::copyRecDir($sourceDir, $this->getBackupDir().$sourceDir);
129  }
130  /**
131  * Copy all files from the backup location defined by backupDir and backupName to sourceDir.
132  * It empties the source directory first.
133  */
134  function restoreFiles()
135  {
136  $session = &SessionData::getInstance();
137  $sourceDir = $session->get($this->SOURCE_DIR_VARNAME);
138 
139  // return on error
140  if (!file_exists($this->getBackupDir().$sourceDir))
141  {
142  $this->setErrorMsg(Message::get("Can't restore backup from %1%. The directory does not exists.", array($this->getBackupDir().$sourceDir)));
143  return;
144  }
145 
146  // empty source directory
147  FileUtil::emptyDir($sourceDir);
148  // copy files
149  FileUtil::copyRecDir($this->getBackupDir().$sourceDir, $sourceDir);
150  }
151 
152  /**
153  * Get the actual backup directory defined by the key 'backupDir' in configuration
154  * section 'cms' and the backup name.
155  * @return The name of the actual backup directory
156  */
157  function getBackupDir()
158  {
159  $session = &SessionData::getInstance();
160  $backupName = $session->get($this->BACKUP_NAME_VARNAME);
161 
162  $parser = &InifileParser::getInstance();
163  if (($backupDir = $parser->getValue('backupDir', 'cms')) === false)
164  WCMFException::throwEx($parser->getErrorMsg(), __FILE__, __LINE__);
165  if (strrpos($backupDir, '/') != strlen($backupDir)-1)
166  $backupDir .= '/';
167  return $backupDir.$backupName.'/';
168  }
169 }
170 ?>
get($message, $parameters=null, $domain='', $lang='')
This Controller creates a backup (action 'makebackup') from a directory and restores (action 'restore...
emptyDir($dirname)
throwEx($message, $file='', $line='')
getAdditionalWorkPackage($number, $action)
copyRecDir($source, $dest)
initialize(&$request, &$response)
BatchController allows to define work packages that will be processed in a sequence. It simplifies the usage of LongTaskController functionality for splitting different bigger tasks into many smaller (similar) tasks where the whole number of tasks isn't known at designtime.