BxSEMigProfiles.php 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  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 SocialEngineMigration SocialEngine Migration
  7. * @ingroup UnaModules
  8. *
  9. * @{
  10. */
  11. bx_import('BxDolProfile');
  12. bx_import('BxDolTranscoderImage');
  13. bx_import('BxDolStorage');
  14. require_once('BxSEMigData.php');
  15. class BxSEMigProfiles extends BxSEMigData
  16. {
  17. public function BxSEMigProfiles(&$oMigrationModule, &$seDb)
  18. {
  19. parent::BxSEMigData($oMigrationModule, $seDb);
  20. }
  21. public function getTotalRecords(){
  22. return (int)$this -> _seDb -> getOne("SELECT COUNT(*) FROM `{$this -> _sEnginePrefix}users`");
  23. }
  24. public function runMigration(){
  25. if (!$this -> getTotalRecords()){
  26. $this -> setResultStatus(_t('_bx_se_migration_no_data_to_transfer'));
  27. return BX_SEMIG_SUCCESSFUL;
  28. }
  29. $this -> setResultStatus(_t('_bx_se_migration_started_migration_profiles'));
  30. $sError = $this -> profilesMigrtion();
  31. if($sError) {
  32. $this -> setResultStatus($sError);
  33. return BX_SEMIG_FAILED;
  34. }
  35. $this -> setResultStatus(_t('_bx_se_migration_started_migration_profiles_finished', $this -> _iTransferred));
  36. return BX_SEMIG_SUCCESSFUL;
  37. }
  38. function profilesMigrtion()
  39. {
  40. $this -> createSEIdField();
  41. // get ID of the latest transferred profile from social engine
  42. $iProfileID = (int)$this -> _oDb -> getOne("SELECT `se_id` FROM `sys_accounts` WHERE `se_id` <> 0 ORDER BY `id` DESC LIMIT 1");
  43. $sStart = '';
  44. if ($iProfileID)
  45. $sStart = " WHERE `user_id` > {$iProfileID}";
  46. $aResult = $this -> _seDb -> getAll("SELECT * FROM `{$this -> _sEnginePrefix}users` {$sStart} ORDER BY `user_id`");
  47. $oLanguage = BxDolLanguages::getInstance();
  48. foreach($aResult as $iKey => $aValue){
  49. if($aValue['username'] && !$this -> isProfileExisted($aValue['username']) ) {
  50. $aUsersFieldsValues = $this -> getMergedFieldsValues($aValue['user_id']);
  51. $sQuery = $this -> _oDb -> prepare(
  52. "
  53. INSERT INTO
  54. `sys_accounts`
  55. SET
  56. `name` = ?,
  57. `email` = ?,
  58. `password` = ?,
  59. `salt` = ?,
  60. `role` = '1',
  61. `lang_id` = ?,
  62. `added` = ?,
  63. `changed` = ?,
  64. `logged` = ?,
  65. `email_confirmed` = '1',
  66. `receive_updates` = '1',
  67. `receive_news` = '1',
  68. `se_id` = ?
  69. ",
  70. $aValue['username'],
  71. $aValue['email'],
  72. $aValue['password'],
  73. $aValue['salt'],
  74. $oLanguage -> getLangId(strtolower($aValue['language'][0].$aValue['language'][1])),
  75. isset($aValue['creation_date']) ? strtotime($aValue['creation_date']) : time(),
  76. isset($aValue['modified_dat'])? strtotime($aValue['modified_dat']) : time(),
  77. isset($aValue['lastlogin_date']) ? strtotime($aValue['lastlogin_date']) : time(),
  78. $aValue['user_id']
  79. );
  80. $this -> _oDb -> query($sQuery);
  81. $iAccountId = $this -> _oDb -> lastId();
  82. if (!$iAccountId) continue;
  83. $sQuery = $this -> _oDb -> prepare(
  84. "
  85. INSERT INTO
  86. `bx_persons_data`
  87. SET
  88. `author` = 0,
  89. `added` = ?,
  90. `changed` = ?,
  91. `picture` = 0,
  92. `cover` = 0,
  93. `fullname` = ?,
  94. `description` = ?
  95. ",
  96. isset($aValue['creation_date']) ? strtotime($aValue['creation_date']) : time(),
  97. isset($aValue['modified_dat'])? strtotime($aValue['modified_dat']) : time(),
  98. isset($aValue['displayname']) ? $aValue['displayname'] : "{$aUsersFieldsValues['first_name']} {$aUsersFieldsValues['last_name']}",
  99. isset($aUsersFieldsValues['about_me']) ? $aUsersFieldsValues['about_me'] : ''
  100. );
  101. $this -> _oDb -> query($sQuery);
  102. $iContentId = $this -> _oDb -> lastId();
  103. $this -> _oDb -> query("INSERT INTO `sys_profiles` SET `account_id` = {$iAccountId}, `type` = 'system', `content_id` = {$iContentId}, `status` = 'active'");
  104. $this -> _oDb -> query("INSERT INTO `sys_profiles` SET `account_id` = {$iAccountId}, `type` = 'bx_persons', `content_id` = {$iContentId}, `status` = 'active'");
  105. $iProfile = $this -> _oDb -> lastId();
  106. if($iProfile)
  107. BxDolAccountQuery::getInstance() -> updateCurrentProfile($iAccountId, $iProfile);
  108. $sQuery = $this -> _oDb -> prepare("UPDATE `bx_persons_data` SET `author` = ? WHERE `id` = ?", $iProfile, $iContentId);
  109. $this -> _oDb -> query($sQuery);
  110. if($aValue['photo_id']) $iPictureID = $this -> exportAvatar($iContentId, $aValue['photo_id']);
  111. $sResult = $this -> exportFriends($aValue['user_id']);
  112. if($sResult)
  113. return $sResult;
  114. $this -> _iTransferred++;
  115. }
  116. }
  117. }
  118. /**
  119. * Create se_id field in sys_accounts for transferring other content conntected with users from social engine
  120. * @return mixed
  121. */
  122. private function createSEIDField(){
  123. if ($this -> _oDb -> isFieldExists('sys_accounts', 'se_id')) return true;
  124. return $this -> _oDb -> query("ALTER TABLE `sys_accounts` ADD `se_id` int(11) unsigned NOT NULL default '0'");
  125. }
  126. /**
  127. * Find all fields with the same names and merge fields with the same names into one field with value
  128. * @param int $iUserId social engine's user id
  129. * @return array
  130. */
  131. private function getMergedFieldsValues($iUserId){
  132. $aValues = $this -> _seDb -> getAll("SELECT * FROM `{$this -> _sEnginePrefix}user_fields_values` WHERE `item_id` = {$iUserId} AND `value` != ''");
  133. $aMap = $this -> _seDb -> getPairs("SELECT * FROM `{$this -> _sEnginePrefix}user_fields_meta`", 'field_id', 'type');
  134. if (empty($aValues) || empty($aMap)) return array();
  135. $aResult = array();
  136. foreach($aValues as $iKey => $aValue){
  137. $aResult[$aMap[$aValue['field_id']]] = $aValue['value'];
  138. }
  139. return $aResult;
  140. }
  141. /**
  142. * Get Avatar's image path
  143. * @param int $iImgId image ID
  144. * @return string
  145. */
  146. private function getProfileImage($iImgId){
  147. $sQuery = $this -> _seDb -> prepare("SELECT `storage_path` FROM `{$this -> _sEnginePrefix}storage_files` WHERE `file_id` = ? AND `parent_file_id` IS NULL LIMIT 1" , $iImgId);
  148. return $this -> _seDb -> getOne($sQuery);
  149. }
  150. private function exportFriends($iProfileId)
  151. {
  152. $iProfileId = (int) $iProfileId;
  153. $sQuery = $this -> _seDb -> prepare("SELECT * FROM `{$this -> _sEnginePrefix}user_membership` WHERE `resource_id` = {$iProfileId} OR `user_id` = {$iProfileId}");
  154. $aFriends = $this -> _seDb -> getAll($sQuery);
  155. foreach($aFriends as $iKey => $aValue){
  156. if( !$this -> isFriendExists($aValue['resource_id'], $aValue['user_id'])) {
  157. $sQuery = $this -> _oDb -> prepare("
  158. INSERT IGNORE INTO
  159. `sys_profiles_conn_friends`
  160. SET
  161. `initiator` = ?,
  162. `content` = ?,
  163. `mutual` = ?,
  164. `added` = UNIX_TIMESTAMP()
  165. ", $aValue['resource_id'], $aValue['user_id'], $aValue['active']);
  166. $iResult = (int) $this -> _oDb -> query($sQuery);
  167. if($iResult <= 0) {
  168. return _t('_bx_se_migration_friends_exports_error');
  169. }
  170. }
  171. }
  172. }
  173. private function exportAvatar($iProfileId, $iAvatarId)
  174. {
  175. $iProfileId = (int) $iProfileId;
  176. $iAvatarId = (int) $iAvatarId;
  177. $sSEAvatar = $this -> getProfileImage($iAvatarId);
  178. if($sSEAvatar) {
  179. $sSEAvatarPath = $this -> _oDb -> getExtraParam('root') . $sSEAvatar;
  180. $oStorage = BxDolStorage::getObjectInstance('bx_persons_pictures');
  181. $iId = $oStorage->storeFileFromPath($sSEAvatarPath, false, $iProfileId);
  182. if ($iId){
  183. $sQuery = $this -> _oDb -> prepare("UPDATE `bx_persons_data` SET `Picture` = ? WHERE `id` = ?", $iId, $iProfileId);
  184. $this -> _oDb -> query($sQuery);
  185. }
  186. }
  187. }
  188. /**
  189. * Check if profile with the same nickname already exists
  190. * @param string $sNickName nameof the user
  191. * @return string
  192. */
  193. private function isProfileExisted($sNickName)
  194. {
  195. $sQuery = $this -> _oDb -> prepare("SELECT COUNT(*) FROM `sys_accounts` WHERE `name` = ?", $sNickName);
  196. return $this -> _oDb -> getOne($sQuery) ? true : false;
  197. }
  198. private function isFriendExists($iProfileId, $iFriendId)
  199. {
  200. $iProfileId = (int) $iProfileId;
  201. $iFriendId = (int) $iFriendId;
  202. $sQuery = "
  203. SELECT
  204. COUNT(*)
  205. FROM
  206. `sys_profiles_conn_friends`
  207. WHERE
  208. (
  209. (`initiator` = {$iProfileId} AND `content` = {$iFriendId})
  210. OR
  211. (`initiator` = {$iFriendId} AND `content` = {$iProfileId})
  212. )
  213. ";
  214. return $this -> _oDb -> getOne($sQuery) ? true : false;
  215. }
  216. }
  217. /** @} */