2010-12-16 20:36:29 +00:00
< ? php
2018-02-01 13:06:32 +00:00
use Illuminate\Support\Facades\DB ;
2017-08-14 16:13:46 -04:00
use ProcessMaker\Core\System ;
2011-01-27 15:04:37 +00:00
CLI :: taskName ( 'upgrade' );
2015-07-20 12:02:33 -04:00
CLI :: taskDescription ( " Upgrade workspaces. \n \n This command should be run after upgrading ProcessMaker to a new version so that all workspaces are also upgraded to the \n new version. " );
2011-01-27 15:04:37 +00:00
2016-09-21 15:26:01 -04:00
CLI :: taskOpt ( 'buildACV' , 'If this option is enabled, the Cache View is built.' , 'ACV' , 'buildACV' );
CLI :: taskOpt ( 'noxml' , 'If this option is enabled, the XML files translation is not built.' , 'NoXml' , 'no-xml' );
2018-06-07 15:41:10 -04:00
CLI :: taskOpt ( 'include_dyn_content' , " Include the DYN_CONTENT_HISTORY value. Ex: --include_dyn_content " , 'i' , 'include_dyn_content' );
2014-03-17 11:54:09 -04:00
CLI :: taskRun ( " run_upgrade " );
2014-12-03 11:03:31 -04:00
/*----------------------------------********---------------------------------*/
2014-09-04 16:28:17 -04:00
CLI :: taskName ( 'unify-database' );
2017-12-04 13:25:35 +00:00
CLI :: taskDescription (
<<< EOT
2015-07-20 12:02:33 -04:00
Unify RBAC , Reports and Workflow database schemas to match the latest version
2014-09-04 16:28:17 -04:00
2015-07-20 12:02:33 -04:00
Specify the workspaces whose databases schemas should be unified .
If no workspace is specified , then the database schema will be upgraded or
repaired on all available workspaces .
2014-09-04 16:28:17 -04:00
2015-07-20 12:02:33 -04:00
This command will read the system schema and attempt to modify the workspaces '
tables to match this new schema . In version 2.8 and later , it will merge the 3
databases used in previous versions of ProcessMaker into one database . This
command may be used after upgrading from ProcessMaker 2.5 to a later version
of ProcessMaker .
2014-09-04 16:28:17 -04:00
EOT
);
2014-12-03 11:03:31 -04:00
/*----------------------------------********---------------------------------*/
2014-09-04 16:28:17 -04:00
CLI :: taskArg ( 'workspace' );
2014-12-03 11:03:31 -04:00
/*----------------------------------********---------------------------------*/
2014-09-04 16:28:17 -04:00
CLI :: taskRun ( " run_unify_database " );
2014-12-03 11:03:31 -04:00
/*----------------------------------********---------------------------------*/
2014-09-04 16:28:17 -04:00
2011-01-27 15:04:37 +00:00
/**
2011-02-21 15:35:50 +00:00
* A version of rm_dir which does not exits on error .
2011-01-27 15:04:37 +00:00
*
* @ param string $filename directory or file to remove
* @ param bool $filesOnly either to remove the containing directory as well or not
*/
2012-09-26 10:15:20 -04:00
function rm_dir ( $filename , $filesOnly = false )
{
if ( is_file ( $filename )) {
@ unlink ( $filename ) or CLI :: logging ( CLI :: error ( " Could not remove file $filename " ) . " \n " );
} else {
foreach ( glob ( " $filename /* " ) as $f ) {
rm_dir ( $f );
}
if ( ! $filesOnly ) {
@ rmdir ( $filename ) or CLI :: logging ( CLI :: error ( " Could not remove directory $filename " ) . " \n " );
}
2011-01-27 15:04:37 +00:00
}
}
2010-12-16 20:36:29 +00:00
2012-09-26 10:15:20 -04:00
function run_upgrade ( $command , $args )
{
CLI :: logging ( " UPGRADE " , PROCESSMAKER_PATH . " upgrade.log " );
CLI :: logging ( " Checking files integrity... \n " );
2012-10-16 10:17:25 -04:00
//setting flag to true to check into sysGeneric.php
2016-07-19 13:41:10 -04:00
$workspaces = get_workspaces_from_args ( $command );
$oneWorkspace = 'true' ;
if ( count ( $workspaces ) == 1 ) {
foreach ( $workspaces as $index => $workspace ) {
$oneWorkspace = $workspace -> name ;
}
}
$flag = G :: isPMUnderUpdating ( 1 , $oneWorkspace );
2012-10-16 10:17:25 -04:00
//start to upgrade
2017-08-14 16:13:46 -04:00
$checksum = System :: verifyChecksum ();
2012-09-26 10:15:20 -04:00
if ( $checksum === false ) {
CLI :: logging ( CLI :: error ( " checksum.txt not found, integrity check is not possible " ) . " \n " );
if ( ! CLI :: question ( " Integrity check failed, do you want to continue the upgrade? " )) {
CLI :: logging ( " Upgrade failed \n " );
2013-01-18 16:10:12 -04:00
$flag = G :: isPMUnderUpdating ( 0 );
2012-09-26 10:15:20 -04:00
die ();
}
} else {
if ( ! empty ( $checksum [ 'missing' ])) {
CLI :: logging ( CLI :: error ( " The following files were not found in the installation: " ) . " \n " );
foreach ( $checksum [ 'missing' ] as $missing ) {
CLI :: logging ( " $missing\n " );
}
}
if ( ! empty ( $checksum [ 'diff' ])) {
CLI :: logging ( CLI :: error ( " The following files have modifications: " ) . " \n " );
foreach ( $checksum [ 'diff' ] as $diff ) {
CLI :: logging ( " $diff\n " );
}
}
if ( ! ( empty ( $checksum [ 'missing' ]) || empty ( $checksum [ 'diff' ]))) {
if ( ! CLI :: question ( " Integrity check failed, do you want to continue the upgrade? " )) {
CLI :: logging ( " Upgrade failed \n " );
2013-01-18 16:10:12 -04:00
$flag = G :: isPMUnderUpdating ( 0 );
2012-09-26 10:15:20 -04:00
die ();
}
}
2010-12-20 15:26:23 +00:00
}
2012-09-26 10:15:20 -04:00
CLI :: logging ( " Clearing cache... \n " );
if ( defined ( 'PATH_C' )) {
2012-12-05 15:17:57 -04:00
G :: rm_dir ( PATH_C );
2013-01-29 11:51:34 -04:00
G :: mk_dir ( PATH_C , 0777 );
2010-12-16 20:36:29 +00:00
}
2016-09-21 15:26:01 -04:00
2012-09-26 10:15:20 -04:00
$count = count ( $workspaces );
$first = true ;
$errors = false ;
2013-02-14 10:43:05 -04:00
$countWorkspace = 0 ;
2016-09-21 15:26:01 -04:00
$buildCacheView = array_key_exists ( 'buildACV' , $args );
$flagUpdateXml = ! array_key_exists ( 'noxml' , $args );
2018-06-07 15:41:10 -04:00
$optionMigrateHistoryData = [
'includeDynContent' => array_key_exists ( 'include_dyn_content' , $args )
];
2016-09-21 15:26:01 -04:00
2012-09-26 10:15:20 -04:00
foreach ( $workspaces as $index => $workspace ) {
2017-10-10 12:33:25 -04:00
if ( empty ( config ( " system.workspace " ))) {
2015-06-05 15:44:28 -04:00
define ( " SYS_SYS " , $workspace -> name );
2017-10-10 12:33:25 -04:00
config ([ " system.workspace " => $workspace -> name ]);
2015-06-05 15:44:28 -04:00
}
if ( ! defined ( " PATH_DATA_SITE " )) {
2017-10-10 12:33:25 -04:00
define ( " PATH_DATA_SITE " , PATH_DATA . " sites " . PATH_SEP . config ( " system.workspace " ) . PATH_SEP );
2015-06-05 15:44:28 -04:00
}
2017-12-04 13:25:35 +00:00
if ( ! defined ( 'DB_ADAPTER' )) {
2016-10-01 09:54:09 -04:00
define ( 'DB_ADAPTER' , 'mysql' );
}
2012-09-26 10:15:20 -04:00
try {
2013-02-14 10:43:05 -04:00
$countWorkspace ++ ;
2013-01-24 14:19:25 -04:00
CLI :: logging ( " Upgrading workspaces ( $countWorkspace / $count ): " . CLI :: info ( $workspace -> name ) . " \n " );
2018-06-07 15:41:10 -04:00
$workspace -> upgrade ( $buildCacheView , $workspace -> name , false , 'en' , [ 'updateXml' => $flagUpdateXml , 'updateMafe' => $first ], $optionMigrateHistoryData );
2012-09-26 10:15:20 -04:00
$workspace -> close ();
$first = false ;
2016-09-21 15:26:01 -04:00
$flagUpdateXml = false ;
2012-09-26 10:15:20 -04:00
} catch ( Exception $e ) {
CLI :: logging ( " Errors upgrading workspace " . CLI :: info ( $workspace -> name ) . " : " . CLI :: error ( $e -> getMessage ()) . " \n " );
$errors = true ;
}
2010-12-16 20:36:29 +00:00
}
2013-02-20 10:11:03 -04:00
2018-01-11 10:16:48 -04:00
//Verify the information of the singleton ServConf by changing the name of the class if is required.
CLI :: logging ( " \n Check/Fix serialized instance in serverConf.singleton file \n \n " );
2018-01-10 16:41:12 -04:00
$serverConf = ServerConf :: getSingleton ();
2018-01-11 10:16:48 -04:00
$serverConf -> updateClassNameInFile ();
2018-01-10 16:41:12 -04:00
2013-02-20 10:11:03 -04:00
// SAVE Upgrades/Patches
$arrayPatch = glob ( PATH_TRUNK . 'patch-*' );
2013-02-20 17:33:13 -04:00
if ( $arrayPatch ) {
2013-02-20 10:11:03 -04:00
foreach ( $arrayPatch as $value ) {
if ( file_exists ( $value )) {
// copy content the patch
$names = pathinfo ( $value );
$nameFile = $names [ 'basename' ];
2013-02-20 17:33:13 -04:00
$contentFile = file_get_contents ( $value );
$contentFile = preg_replace ( " [ \n | \r | \n \r ] " , '' , $contentFile );
CLI :: logging ( $contentFile . ' installed (' . $nameFile . ')' , PATH_DATA . 'log/upgrades.log' );
2013-02-20 10:11:03 -04:00
// move file of patch
$newFile = PATH_DATA . $nameFile ;
G :: rm_dir ( $newFile );
copy ( $value , $newFile );
G :: rm_dir ( $value );
}
}
} else {
2017-08-14 16:13:46 -04:00
CLI :: logging ( 'ProcessMaker ' . System :: getVersion () . ' installed' , PATH_DATA . 'log/upgrades.log' );
2013-02-20 10:11:03 -04:00
}
2013-04-12 16:48:23 -04:00
//Safe upgrade for JavaScript files
CLI :: logging ( " \n Safe upgrade for files cached by the browser \n \n " );
G :: browserCacheFilesSetUid ();
//Status
if ( $errors ) {
CLI :: logging ( " Upgrade finished but there were errors upgrading workspaces. \n " );
CLI :: logging ( CLI :: error ( " Please check the log above to correct any issues. " ) . " \n " );
} else {
CLI :: logging ( " Upgrade successful \n " );
}
2012-10-16 10:17:25 -04:00
//setting flag to false
$flag = G :: isPMUnderUpdating ( 0 );
2010-12-16 20:36:29 +00:00
}
2017-12-04 13:25:35 +00:00
function listFiles ( $dir )
{
2014-04-09 11:26:22 -04:00
$files = array ();
$lista = glob ( $dir . '/*' );
2017-12-04 13:25:35 +00:00
foreach ( $lista as $valor ) {
2014-04-09 11:26:22 -04:00
if ( is_dir ( $valor )) {
$inner_files = listFiles ( $valor );
2017-12-04 13:25:35 +00:00
if ( is_array ( $inner_files )) {
$files = array_merge ( $files , $inner_files );
}
2014-04-09 11:26:22 -04:00
}
if ( is_file ( $valor )) {
array_push ( $files , $valor );
2014-04-01 11:46:06 -04:00
}
}
2014-04-09 11:26:22 -04:00
return $files ;
}
2014-12-03 11:03:31 -04:00
/*----------------------------------********---------------------------------*/
2014-09-04 16:28:17 -04:00
function run_unify_database ( $args )
2014-10-10 16:41:15 -04:00
{
2014-10-09 10:36:34 -04:00
$workspaces = array ();
2014-10-10 16:41:15 -04:00
2017-12-04 13:25:35 +00:00
if ( count ( $args ) > 2 ) {
2014-10-09 10:36:34 -04:00
$filename = array_pop ( $args );
foreach ( $args as $arg ) {
2017-08-11 14:10:44 -04:00
$workspaces [] = new WorkspaceTools ( $arg );
2014-10-09 10:36:34 -04:00
}
2017-12-04 13:25:35 +00:00
} elseif ( count ( $args ) > 0 ) {
2017-08-11 14:10:44 -04:00
$workspace = new WorkspaceTools ( $args [ 0 ]);
2014-10-09 10:36:34 -04:00
$workspaces [] = $workspace ;
}
2014-09-04 16:28:17 -04:00
CLI :: logging ( " UPGRADE " , PROCESSMAKER_PATH . " upgrade.log " );
CLI :: logging ( " Checking workspaces... \n " );
//setting flag to true to check into sysGeneric.php
$flag = G :: isPMUnderUpdating ( 0 );
2014-10-10 16:41:15 -04:00
2014-09-04 16:28:17 -04:00
//start to unify
$count = count ( $workspaces );
2014-10-10 16:41:15 -04:00
2014-09-04 16:28:17 -04:00
if ( $count > 1 ) {
2017-12-04 13:25:35 +00:00
if ( ! Bootstrap :: isLinuxOs ()) {
2015-04-27 12:16:42 -04:00
CLI :: error ( " This is not a Linux enviroment, please specify workspace. \n " );
2014-09-04 16:28:17 -04:00
return ;
}
}
$first = true ;
$errors = false ;
$countWorkspace = 0 ;
$buildCacheView = array_key_exists ( " buildACV " , $args );
2014-10-10 16:41:15 -04:00
foreach ( $workspaces as $workspace ) {
try {
2014-09-04 16:28:17 -04:00
$countWorkspace ++ ;
2014-10-09 10:36:34 -04:00
if ( ! $workspace -> workspaceExists ()) {
echo " Workspace { $workspace -> name } not found \n " ;
return false ;
}
2014-10-10 16:41:15 -04:00
2014-10-09 10:36:34 -04:00
$ws = $workspace -> name ;
2017-12-04 13:25:35 +00:00
$sContent = file_get_contents ( PATH_DB . $ws . PATH_SEP . 'db.php' );
2014-10-09 10:36:34 -04:00
if ( strpos ( $sContent , 'rb_' )) {
$workspace -> onedb = false ;
} else {
$workspace -> onedb = true ;
}
2014-09-04 16:28:17 -04:00
if ( $workspace -> onedb ) {
2014-10-10 16:41:15 -04:00
CLI :: logging ( " The \" $workspace->name\ " workspace already using one database ... \n " );
2014-09-04 16:28:17 -04:00
} else {
//create destination path
$parentDirectory = PATH_DATA . " upgrade " ;
2017-12-04 13:25:35 +00:00
if ( ! file_exists ( $parentDirectory )) {
mkdir ( $parentDirectory );
2014-09-04 16:28:17 -04:00
}
$tempDirectory = $parentDirectory . basename ( tempnam ( __FILE__ , '' ));
2017-12-04 13:25:35 +00:00
if ( is_writable ( $parentDirectory )) {
mkdir ( $tempDirectory );
2014-09-04 16:28:17 -04:00
} else {
2017-12-04 13:25:35 +00:00
throw new Exception ( " Could not create directory: " . $parentDirectory );
2014-09-04 16:28:17 -04:00
}
$metadata = $workspace -> getMetadata ();
2017-12-04 13:25:35 +00:00
CLI :: logging ( " Exporting rb and rp databases to a temporal location... \n " );
$metadata [ " databases " ] = $workspace -> exportDatabase ( $tempDirectory , true );
2014-09-04 16:28:17 -04:00
$metadata [ " version " ] = 1 ;
2014-10-10 16:41:15 -04:00
2017-12-04 13:25:35 +00:00
list ( $dbHost , $dbUser , $dbPass ) = @ explode ( SYSTEM_HASH , G :: decrypt ( HASH_INSTALLATION , SYSTEM_HASH ));
2018-02-01 13:06:32 +00:00
$connectionName = 'UPGRADE' ;
InstallerModule :: setNewConnection ( $connectionName , $dbHost , $dbUser , $dbPass , '' , '' );
2014-10-10 16:41:15 -04:00
2014-10-09 10:36:34 -04:00
foreach ( $metadata [ 'databases' ] as $db ) {
2014-10-27 18:14:16 -04:00
$dbName = $metadata [ 'DB_NAME' ];
2017-12-04 13:25:35 +00:00
CLI :: logging ( " +> Restoring { $db [ 'name' ] } to $dbName database \n " );
2014-10-09 10:36:34 -04:00
2018-02-01 13:06:32 +00:00
$aParameters = [ 'dbHost' => $dbHost , 'dbUser' => $dbUser , 'dbPass' => $dbPass ];
2014-10-10 16:41:15 -04:00
2018-02-01 13:06:32 +00:00
$restore = $workspace -> executeScript ( $dbName , " $tempDirectory / { $db [ 'name' ] } .sql " , $aParameters , $connectionName );
2014-10-10 16:41:15 -04:00
2014-10-09 10:36:34 -04:00
if ( $restore ) {
2017-12-04 13:25:35 +00:00
CLI :: logging ( " +> Remove { $db [ 'name' ] } database \n " );
2014-09-04 16:28:17 -04:00
2018-02-01 13:06:32 +00:00
DB :: connection ( $connectionName ) -> statement ( " DROP DATABASE IF EXISTS { $db [ 'name' ] } " );
2014-09-04 16:28:17 -04:00
}
}
2018-02-01 13:06:32 +00:00
DB :: disconnect ( $connectionName );
2014-10-10 16:41:15 -04:00
2017-12-04 13:25:35 +00:00
CLI :: logging ( " Removing temporary files \n " );
G :: rm_dir ( $tempDirectory );
2014-10-10 16:41:15 -04:00
2017-12-04 13:25:35 +00:00
$newDBNames = $workspace -> resetDBInfo ( $dbHost , true , true , true );
2014-09-04 16:28:17 -04:00
2017-12-04 13:25:35 +00:00
CLI :: logging ( CLI :: info ( " Done restoring databases " ) . " \n " );
2014-10-10 16:41:15 -04:00
}
} catch ( Exception $e ) {
2014-09-04 16:28:17 -04:00
CLI :: logging ( " Errors upgrading workspace " . CLI :: info ( $workspace -> name ) . " : " . CLI :: error ( $e -> getMessage ()) . " \n " );
$errors = true ;
}
}
$flag = G :: isPMUnderUpdating ( 0 );
2014-12-03 11:03:31 -04:00
}
2017-12-04 13:25:35 +00:00
/*----------------------------------********---------------------------------*/