BxDolMModule.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. <?php defined('BX_DOL') or die('hack attempt');
  2. /**
  3. * Copyright (c) UNA, Inc - https://una.io
  4. * MIT License - https://opensource.org/licenses/MIT
  5. *
  6. * @defgroup DolphinMigration Dolphin Migration
  7. * @ingroup UnaModules
  8. *
  9. * @{
  10. */
  11. require_once('BxMDb.php');
  12. if ( function_exists('ini_set'))
  13. {
  14. ini_set('max_execution_time', 0);
  15. }
  16. class BxDolMModule extends BxBaseModGeneralModule
  17. {
  18. protected $_oMDb = null;
  19. public function __construct(&$aModule){
  20. parent::__construct($aModule);
  21. }
  22. public function actionPerformAction($sModule, $sAction = 'clean')
  23. {
  24. if (!$sModule || !isset($this -> _oConfig -> _aMigrationModules[$sModule]))
  25. {
  26. echo json_encode(array('code' => 1, 'message' => _t('_bx_dolphin_migration_nothing_to_remove')));
  27. exit;
  28. }
  29. require_once($this -> _oConfig -> _aMigrationModules[$sModule]['migration_class'] . '.php');
  30. $oModule = new $this -> _oConfig -> _aMigrationModules[$sModule]['migration_class']($this, $this -> _oMDb);
  31. return $sAction == 'clean' ? $oModule -> dropMID() : $oModule -> removeContent();
  32. }
  33. public function actionStartTransfer($aModules)
  34. {
  35. if (empty($aModules))
  36. {
  37. echo json_encode(array('code' => 1, 'message' => _t('_bx_dolphin_migration_successfully_finished')));
  38. exit;
  39. }
  40. $this -> initDb();
  41. header('Content-Type:text/javascript');
  42. foreach($aModules as $iKey => $sModule){
  43. if( $sModule && !empty(($this -> _oConfig -> _aMigrationModules[$sModule])))
  44. {
  45. $sTransferred = $this -> _oDb -> getTransferStatus($sModule);
  46. if ($sTransferred == 'finished')
  47. continue;
  48. if(isset($this -> _oConfig -> _aMigrationModules[$sModule]['dependencies']) && is_array($this -> _oConfig -> _aMigrationModules[$sModule]['dependencies']))
  49. {
  50. foreach($this -> _oConfig -> _aMigrationModules[$sModule]['dependencies'] as $iKey => $sDependenciesModule)
  51. {
  52. $sTransferred = $this -> _oDb -> getTransferStatus($sDependenciesModule);
  53. if( $sTransferred != 'finished')
  54. return _t('_bx_dolphin_migration_install_before', _t("_bx_dolphin_migration_data_{$sDependenciesModule}"));
  55. }
  56. }
  57. if(isset($this -> _oConfig -> _aMigrationModules[$sModule]['plugins']) && is_array($this -> _oConfig -> _aMigrationModules[$sModule]['plugins']))
  58. {
  59. $sPlugins = '';
  60. foreach($this -> _oConfig -> _aMigrationModules[$sModule]['plugins'] as $sKey => $sTitle)
  61. {
  62. if (!$this -> _oDb -> isPluginInstalled($sKey))
  63. $sPlugins .= $sTitle . ', ';
  64. }
  65. if ($sPlugins)
  66. return _t('_bx_dolphin_migration_install_plugin', trim($sPlugins, ', '), $sModule);
  67. }
  68. // create new module's instance;
  69. require_once($this -> _oConfig -> _aMigrationModules[$sModule]['migration_class'] . '.php');
  70. // set as started;
  71. $this -> _oDb -> updateTransferStatus($sModule, 'started');
  72. // create new migration instance;
  73. $oModule = new $this -> _oConfig -> _aMigrationModules[$sModule]['migration_class']($this, $this -> _oMDb);
  74. if($oModule -> runMigration())
  75. $this -> _oDb -> updateTransferStatus($sModule, 'finished');
  76. else
  77. $this -> _oDb -> updateTransferStatus($sModule, 'error');
  78. }
  79. }
  80. return _t('_bx_dolphin_migration_successfully_finished');
  81. }
  82. /**
  83. * Creates date for migration
  84. */
  85. public function createMigration()
  86. {
  87. if (is_null($this -> _oMDb))
  88. $this -> initDb();
  89. $this -> _oDb -> cleanTransfersTable();
  90. foreach ($this -> _oConfig -> _aMigrationModules as $sName => $aModule)
  91. {
  92. if ($this -> _oMDb -> isTableExists($aModule['table_name']))
  93. {
  94. //Create transferring class object
  95. require_once($aModule['migration_class'] . '.php');
  96. $oObject = new $aModule['migration_class']($this, $this -> _oMDb);
  97. if ($mixedNumber = $oObject -> getTotalRecords())
  98. $this -> _oDb -> addToTransferList($sName, $mixedNumber);
  99. }
  100. }
  101. }
  102. /**
  103. * Init Dolphin database connect
  104. * @return mixed
  105. */
  106. public function initDb()
  107. {
  108. $aConfig = array(
  109. 'host' => $this -> _oDb -> getExtraParam('host'),
  110. 'user' => $this -> _oDb -> getExtraParam('user'),
  111. 'pwd' => $this -> _oDb -> getExtraParam('passwd'),
  112. 'name' => $this -> _oDb -> getExtraParam('db'),
  113. 'port' => $this -> _oDb -> getExtraParam('port'),
  114. 'sock' => $this -> _oDb -> getExtraParam('sock'),
  115. );
  116. $this -> _oMDb = new BxMDb($aConfig);
  117. return $this -> _oMDb -> connect();
  118. }
  119. }
  120. /** @} */