Skip to content
This repository has been archived by the owner on Sep 10, 2021. It is now read-only.

Commit

Permalink
ENH: Added upgrade system based on midas2 and http://akrabat.com/zend…
Browse files Browse the repository at this point in the history
  • Loading branch information
Charles Marion committed Apr 15, 2011
1 parent 79e29a2 commit 9a9034e
Show file tree
Hide file tree
Showing 28 changed files with 668 additions and 64 deletions.
7 changes: 6 additions & 1 deletion core/AppController.php
Expand Up @@ -25,7 +25,12 @@ public function preDispatch()
$this->view->title=Zend_Registry::get('configGlobal')->application->name;

// Set the version
$this->view->version='3.0 beta';
$this->view->version='3.0.0';
if(isset(Zend_Registry::get('configDatabase')->version))
{
$this->view->version=Zend_Registry::get('configDatabase')->version;
}

//Init Session
if($fc->getRequest()->getActionName()!='login'||$fc->getRequest()->getControllerName()!='user')
{
Expand Down
10 changes: 7 additions & 3 deletions core/Bootstrap.php
Expand Up @@ -144,9 +144,12 @@ protected function _initConfig()
),
));
}
$logger->addWriter($writerDb);
$logger->setEventItem('datetime',date('Y-m-d H:i:s'));
$logger->setEventItem('module','unknown');
if($configDatabase->database->params->password!='set_your_password')
{
$logger->addWriter($writerDb);
$logger->setEventItem('datetime',date('Y-m-d H:i:s'));
$logger->setEventItem('module','unknown');
}
}
else
{
Expand All @@ -160,6 +163,7 @@ protected function _initConfig()
$notifyErrorCompoenent=new NotifyErrorComponent();
ini_set('display_errors',0);
register_shutdown_function(array($notifyErrorCompoenent,'fatalEror'),$logger,new Zend_Mail());
set_error_handler(array($notifyErrorCompoenent,'warningError'), E_NOTICE|E_WARNING);

return $config;
}
Expand Down
3 changes: 2 additions & 1 deletion core/configs/.gitignore
@@ -1 +1,2 @@
*.local.ini
*.local.ini
*.local.ini.old
6 changes: 3 additions & 3 deletions core/configs/database.ini
Expand Up @@ -5,7 +5,7 @@ database.adapter = PDO_MYSQL
database.params.host = localhost
database.params.port = 3306
database.params.username = root
database.params.password = 123
database.params.password = set_your_password
database.params.dbname = midas3
database.params.port = 3306

Expand All @@ -17,7 +17,7 @@ database.adapter = PDO_MYSQL
database.params.host = localhost
database.params.port = 3306
database.params.username = root
database.params.password = 123
database.params.password = set_your_password
database.params.dbname = midas3
database.params.port = 3306

Expand All @@ -28,6 +28,6 @@ database.adapter = PDO_MYSQL
database.params.host = localhost
database.params.port = 3306
database.params.username = root
database.params.password = 123
database.params.password = set_your_password
database.params.dbname = midas3_test
database.params.port = 3306
56 changes: 54 additions & 2 deletions core/controllers/AdminController.php
Expand Up @@ -7,13 +7,65 @@ class AdminController extends AppController
{
public $_models=array();
public $_daos=array();
public $_components=array();
public $_components=array('Upgrade');

/** index*/
function indexAction()
{

}
}//end indexAction


/** upgrade database*/
function upgradeAction()
{
if(!$this->logged||!$this->userSession->Dao->getAdmin()==1)
{
throw new Zend_Exception("You should be an administrator");
}

$db=Zend_Registry::get('dbAdapter');
$dbtype=Zend_Registry::get('configDatabase')->database->adapter;
$modulesConfig=Zend_Registry::get('configsModules');

if($this->_request->isPost())
{
$upgraded=false;
$modulesConfig=Zend_Registry::get('configsModules');
$modules=array();
foreach($modulesConfig as $key=>$module)
{
$this->Component->Upgrade->init($key,$db,$dbtype);
$upgraded=$upgraded||$this->Component->Upgrade->upgrade($module->version);
}
$this->Component->Upgrade->init('core',$db,$dbtype);
$upgraded=$upgraded||$this->Component->Upgrade->upgrade(Zend_Registry::get('configDatabase')->version);
$this->view->upgraded=$upgraded;

$dbtype=Zend_Registry::get('configDatabase')->database->adapter;
$modulesConfig=Zend_Registry::get('configsModules');
}

$modules=array();
foreach($modulesConfig as $key=>$module)
{
$this->Component->Upgrade->init($key,$db,$dbtype);
$modules[$key]['target']=$this->Component->Upgrade->getNewestVersion();
$modules[$key]['targetText']=$this->Component->Upgrade->getNewestVersion(true);
$modules[$key]['currentText']=$module->version;
$modules[$key]['current']=$this->Component->Upgrade->transformVersionToNumeric($module->version);
}

$this->view->modules=$modules;

$this->Component->Upgrade->init('core',$db,$dbtype);
$core['target']=$this->Component->Upgrade->getNewestVersion();
$core['targetText']=$this->Component->Upgrade->getNewestVersion(true);
$core['currentText']=Zend_Registry::get('configDatabase')->version;
$core['current']=$this->Component->Upgrade->transformVersionToNumeric(Zend_Registry::get('configDatabase')->version);
$this->view->core=$core;
}//end upgradeAction

/**
* \fn serversidefilechooser()
* \brief called by the server-side file chooser
Expand Down
34 changes: 29 additions & 5 deletions core/controllers/InstallController.php
Expand Up @@ -71,7 +71,7 @@ function step2Action()

foreach($phpextensions as $key => $t)
{
if(!file_exists(BASE_PATH."/core/sql/{$key}/{$this->view->version}.sql"))
if(!file_exists(BASE_PATH."/core/database/{$key}"))
{
unset($phpextensions[$key]);
}
Expand All @@ -92,10 +92,23 @@ function step2Action()
if($form->isValid($this->getRequest()->getPost()))
{
$databaseConfig=parse_ini_file (BASE_PATH.'/core/configs/database.ini',true);
$MyDirectory = opendir(BASE_PATH."/core/database/{$type}");
while($Entry = @readdir($MyDirectory))
{
if(strpos($Entry, ".sql")!=false)
{
$sqlFile=BASE_PATH."/core/database/{$type}/".$Entry;
}
}
if(!isset($sqlFile))
{
throw new Zend_Exception("Unable to find sql file");
}

switch($type)
{
case 'mysql':
$this->run_mysql_from_file(BASE_PATH."/core/sql/{$type}/{$this->view->version}.sql",
$this->run_mysql_from_file($sqlFile,
$form->getValue('host'), $form->getValue('username'), $form->getValue('password'), $form->getValue('dbname'),$form->getValue('port'));
$params= array(
'host' => $form->getValue('host'),
Expand All @@ -118,13 +131,14 @@ function step2Action()
$databaseConfig['production']['database.params.dbname']=$form->getValue('dbname');
$databaseConfig['development']['database.params.dbname']=$form->getValue('dbname');
$databaseConfig['development']['database.params.port']=$form->getValue('port');
$databaseConfig['production']['database.params.port']=$form->getValue('port');

$db = Zend_Db::factory("PDO_MYSQL",$params);
Zend_Db_Table::setDefaultAdapter($db);
Zend_Registry::set('dbAdapter', $db);
break;
case 'pgsql':
$this->run_pgsql_from_file(BASE_PATH."/core/sql/{$type}/{$this->view->version}.sql",
$this->run_pgsql_from_file($sqlFile,
$form->getValue('host'), $form->getValue('username'), $form->getValue('password'), $form->getValue('dbname'),$form->getValue('port'));
$params= array(
'host' => $form->getValue('host'),
Expand All @@ -147,6 +161,7 @@ function step2Action()
$databaseConfig['production']['database.params.dbname']=$form->getValue('dbname');
$databaseConfig['development']['database.params.dbname']=$form->getValue('dbname');
$databaseConfig['development']['database.params.port']=$form->getValue('port');
$databaseConfig['production']['database.params.port']=$form->getValue('port');

$db = Zend_Db::factory("PDO_PGSQL",$params);
Zend_Db_Table::setDefaultAdapter($db);
Expand All @@ -155,6 +170,9 @@ function step2Action()
default:
break;
}
$databaseConfig['production']['version']=str_replace('.sql', '', basename($sqlFile));
$databaseConfig['development']['version']=str_replace('.sql', '', basename($sqlFile));

$this->Component->Utility->createInitFile(BASE_PATH.'/core/configs/database.local.ini',$databaseConfig);
$this->User=new UserModel(); //reset Database adapter
$this->userSession->Dao=$this->User->createUser($form->getValue('email'),$form->getValue('userpassword1'),
Expand Down Expand Up @@ -234,22 +252,28 @@ function step3Action()
switch (Zend_Registry::get('configDatabase')->database->adapter)
{
case 'PDO_MYSQL':
$this->run_mysql_from_file(BASE_PATH.'/modules/'.$key.'/sql/mysql/'.$allModules[$key]->version.'.sql',
$this->run_mysql_from_file(BASE_PATH.'/modules/'.$key.'/database/mysql/'.$allModules[$key]->version.'.sql',
Zend_Registry::get('configDatabase')->database->params->host,
Zend_Registry::get('configDatabase')->database->params->username,
Zend_Registry::get('configDatabase')->database->params->password,
Zend_Registry::get('configDatabase')->database->params->dbname,
Zend_Registry::get('configDatabase')->database->params->port);
break;
case 'PDO_PGSQL':
$this->run_pgsql_from_file(BASE_PATH.'/modules/'.$key.'/sql/pgsql/'.$allModules[$key]->version.'.sql',
$this->run_pgsql_from_file(BASE_PATH.'/modules/'.$key.'/database/pgsql/'.$allModules[$key]->version.'.sql',
Zend_Registry::get('configDatabase')->database->params->host,
Zend_Registry::get('configDatabase')->database->params->username,
Zend_Registry::get('configDatabase')->database->params->password,
Zend_Registry::get('configDatabase')->database->params->dbname,
Zend_Registry::get('configDatabase')->database->params->port);
break;
}

$path=BASE_PATH.'/modules/'.$key.'/configs/module.ini';
$newpath=BASE_PATH.'/modules/'.$key.'/configs/module.local.ini';
$data=parse_ini_file ($path,true);
@unlink($newpath);
$this->Component->Utility->createInitFile($newpath, $data);
}
$this->Component->Utility->createInitFile(BASE_PATH.'/core/configs/application.local.ini',$applicationConfig);
$this->_redirect("/");
Expand Down
33 changes: 27 additions & 6 deletions core/controllers/components/NotifyErrorComponent.php
Expand Up @@ -33,7 +33,7 @@ public function fatalEror($logger,$mailer)
if(!is_null($e = error_get_last()))
{
$environment = Zend_Registry::get('configGlobal')->environment;
$db = Zend_Registry::get('dbAdapter');
$db = Zend_Registry::get('dbAdapter');
if(method_exists($db,"getProfiler"))
{
$profiler = $db->getProfiler();
Expand All @@ -42,12 +42,17 @@ public function fatalEror($logger,$mailer)
{
$profiler = new Zend_Db_Profiler();
}

switch ($environment) {
case 'production':
$message .= "It seems you have just encountered an unknown issue.";
$message .= "Our team has been notified and will deal with the problem as soon as possible.";
header('content-type: text/plain');
if($e['type']==E_NOTICE) $e['typeText']='E_NOTICE';
elseif($e['type']==E_ERROR ) $e['typeText']='E_ERROR';
elseif($e['type']==E_WARNING )$e['typeText']='E_WARNING';
elseif($e['type']==E_PARSE) $e['typeText ']='E_PARSE';
else return;
echo $message;
$this->_mailer=$mailer;
$this->_environment=$environment;
Expand All @@ -63,15 +68,31 @@ public function fatalEror($logger,$mailer)
header('content-type: text/plain');
echo $this->getFatalErrorMessage($e);
}
$logger->crit($this->getFatalErrorMessage($e));
$logger->__destruct();
$logger->crit($this->getFatalErrorMessage($e));
}
$logger->__destruct();
}

public function warningError($errno, $errstr, $errfile, $errline)
{
if($errno==E_WARNING&&Zend_Registry::get('configGlobal')->environment!='production')
{
echo "Warning: ".$errstr."<br/>\n";
echo " on line $errline in file $errfile<br/>\n";
}

if($errno==E_NOTICE&&Zend_Registry::get('configGlobal')->environment!='production')
{
echo "Notice : ".$errstr."<br/>\n";
echo " on line $errline in file $errfile<br/>\n";
}
}


private function curPageURL()
{
$pageURL = 'http';
if ($_SERVER["HTTPS"] == "on") {$pageURL .= "s";}
if (isset($_SERVER["HTTPS"] )&&$_SERVER["HTTPS"] == "on") {$pageURL .= "s";}
$pageURL .= "://";
if ($_SERVER["SERVER_PORT"] != "80") {
$pageURL .= $_SERVER["SERVER_NAME"].":".$_SERVER["SERVER_PORT"].$_SERVER["REQUEST_URI"];
Expand Down Expand Up @@ -107,7 +128,7 @@ public function getFatalErrorMessage($e)
$message .= "Referer: " . $this->_server['HTTP_REFERER'] . "\n";
}
$message .="Parameters (post): ".print_r($_POST,true)."\n";
$message .="Parameters (get): ".print_r($_GET,true)."\n";
$message .="Parameters (get): ".print_r($_GET,true)."\n\n";

return $message;
}
Expand Down

0 comments on commit 9a9034e

Please sign in to comment.