BxDolMTimeline.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482
  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('BxDolMData.php');
  12. bx_import('BxDolStorage');
  13. class BxDolMTimeline extends BxDolMData
  14. {
  15. /**
  16. * @var path in Dolphin to the video modules files
  17. */
  18. private $_sImageVideoFiles = null;
  19. public function __construct(&$oMigrationModule, &$oDb)
  20. {
  21. parent::__construct($oMigrationModule, $oDb);
  22. $this -> _sModuleName = 'timeline';
  23. $this -> _sTableWithTransKey = 'bx_timeline_events';
  24. $this -> _sImageVideoFiles = $this -> _oDb -> getExtraParam('root') . 'flash' . DIRECTORY_SEPARATOR . 'modules' . DIRECTORY_SEPARATOR . "video" . DIRECTORY_SEPARATOR . "files" . DIRECTORY_SEPARATOR;
  25. }
  26. public function getTotalRecords()
  27. {
  28. return (int)$this -> _mDb -> getOne("SELECT COUNT(*) FROM `" . $this -> _oConfig -> _aMigrationModules[$this -> _sModuleName]['table_name'] . "` ORDER BY `id`");
  29. }
  30. /**
  31. * Returns data from the module into which data was added
  32. *
  33. * @param int $iModuleItemId object id
  34. * @param string $sModule module name @uses BxDolMConfig::_aMigrationModules
  35. * @return array
  36. */
  37. private function getModuleItemInfo($iModuleItemId, $sModule = 'forum')
  38. {
  39. $sTable = '';
  40. $sModule = '';
  41. switch($sModule)
  42. {
  43. case 'events':
  44. $sTable = 'bx_events_data';
  45. $sModule = 'bx_events';
  46. break;
  47. case 'store':
  48. $sTable = 'bx_market_products';
  49. $sModule = 'bx_market';
  50. break;
  51. case 'files':
  52. $sTable = 'bx_files_main';
  53. $sModule = 'bx_files';
  54. break;
  55. case 'polls':
  56. $sTable = 'bx_polls_entries';
  57. $sModule = 'bx_polls';
  58. break;
  59. case 'groups':
  60. $sTable = 'bx_groups_data';
  61. $sModule = 'bx_groups';
  62. break;
  63. case 'blogs':
  64. $sTable = 'bx_posts_posts';
  65. $sModule = 'bx_posts';
  66. break;
  67. }
  68. if (!$sTable)
  69. return array();
  70. if ($this->_oDb->isEnabledByName($sModule) && !$this -> _oDb -> isFieldExists($sTable, $this -> _sTransferFieldIdent))
  71. return array();
  72. return $this -> _oDb -> getRow("SELECT * FROM `{$sTable}` WHERE `{$this -> _sTransferFieldIdent}` = :id", array('id' => $iModuleItemId));
  73. }
  74. /**
  75. * Convert Dolphin timeline record into UNA timeline record
  76. *
  77. * @param array $aInfo Dolphin timeline record
  78. * @return array
  79. */
  80. private function getFormatedData(&$aInfo)
  81. {
  82. $sAction = '';
  83. $sType = '';
  84. $sContent = '';
  85. $sDescription = '';
  86. $iAuthor = $this -> getProfileId($aInfo['owner_id']);
  87. $iObjectId = $this -> getProfileId($aInfo['object_id']);
  88. $iTime = time();
  89. $sTitle = '';
  90. $aData = array();
  91. $mixedTransferred = $this -> isModuleContentTransferred($aInfo['type']);
  92. if ($mixedTransferred === 0)
  93. return array();
  94. switch($aInfo['type'])
  95. {
  96. case 'bx_events':
  97. case 'bx_forum':
  98. case 'bx_groups':
  99. case 'bx_blogs':
  100. case 'bx_store':
  101. case 'bx_polls':
  102. if (!($aInfo['action'] == 'add' || $aInfo['action'] == 'create'))
  103. return array();
  104. $sModule = $this -> _oConfig -> _aModulesAliases[$aInfo['type']];
  105. $aItemInfo = $this -> getModuleItemInfo($aInfo['object_id'], $sModule);
  106. if (empty($aItemInfo))
  107. return array();
  108. $sAction = 'added';
  109. $sType = $aInfo['type'];
  110. $iObjectId = $aItemInfo['id'];
  111. $iTime = $aInfo['date'];
  112. switch($sModule)
  113. {
  114. case 'events':
  115. $sTitle = $aItemInfo['event_name'];
  116. break;
  117. case 'store':
  118. $sTitle = $aItemInfo['title'];
  119. $sType = 'bx_market';
  120. break;
  121. case 'files':
  122. $sTitle = $aItemInfo['title'];
  123. break;
  124. case 'videos':
  125. $sTitle = $aItemInfo['title'];
  126. break;
  127. case 'photos':
  128. $sTitle = $aItemInfo['title'];
  129. break;
  130. case 'polls':
  131. $sTitle = $aItemInfo['text'];
  132. break;
  133. case 'groups':
  134. $sTitle = $aItemInfo['group_name'];
  135. break;
  136. case 'blogs':
  137. $sTitle = $aItemInfo['title'];
  138. $sType = 'bx_posts';
  139. break;
  140. }
  141. $oProfile = BxDolProfile::getInstance($iAuthor);
  142. $sDescription = _t('_bx_timeline_txt_user_added_sample', $oProfile -> getDisplayName(), _t("_{$sType}_txt_sample_single_with_article"));
  143. break;
  144. case 'wall_common_link':
  145. $sType = 'timeline_common_post';
  146. $sTitle = '<i class="sys-icon link"></i>';
  147. preg_match('!https?://\S+!', $aInfo['description'], $aUrl);
  148. if (!empty($aUrl[0]))
  149. {
  150. $aData = array(
  151. 'url' => $aUrl[0],
  152. 'type' => 'link'
  153. );
  154. $sContent = serialize(array());
  155. }
  156. break;
  157. case 'wall_common_photos':
  158. case 'wall_common_videos':
  159. $sType = 'timeline_common_post';
  160. $sTitle = $aInfo['type'] == 'wall_common_photos' ? '<i class="sys-icon far image"></i>' : '<i class="sys-icon film"></i>';
  161. $aItemInfo = @unserialize($aInfo['content']);
  162. if (!empty($aItemInfo))
  163. {
  164. $aData = array(
  165. 'media_id' => $aItemInfo['id'],
  166. 'type' => $aItemInfo['type']
  167. );
  168. $sContent = serialize(array());
  169. }
  170. break;
  171. case 'wall_common_text':
  172. $sTitle = html2txt($aInfo['content']);
  173. $sType = 'timeline_common_post';
  174. $sContent = serialize(array());
  175. break;
  176. case 'wall_common_repost':
  177. $sType = 'timeline_common_repost';
  178. if (isset($aInfo['content']))
  179. $aItemInfo = @unserialize($aInfo['content']);
  180. if (!isset($aItemInfo['action']) || !($aItemInfo['action'] == 'add' || $aItemInfo['action'] == 'create') || !isset($this -> _oConfig -> _aModulesAliases[$aItemInfo['type']]))
  181. break;
  182. else
  183. $sSubAction = 'added';
  184. $aSubInfo = $this -> getModuleItemInfo($aItemInfo['object_id'], $this -> _oConfig -> _aModulesAliases[$aItemInfo['type']]);
  185. if (empty($aSubInfo))
  186. break;
  187. $sSubType = $aItemInfo['type'];
  188. $sSubType = $sSubType == 'bx_blogs' ? 'bx_posts' : $sSubType;
  189. $sSubType = $sSubType == 'bx_store' ? 'bx_market' : $sSubType;
  190. $sContent = serialize(array('type' => $sSubType, 'action' => 'added', 'object_id' => $aSubInfo['id']));
  191. break;
  192. }
  193. $aResult = array
  194. (
  195. 'type' => $sType,
  196. 'action' => $sAction,
  197. 'owner_id' => $iAuthor,
  198. 'object_id' => $iObjectId,
  199. 'privacy' => 3,
  200. 'content' => $sContent,
  201. 'title' => $sTitle,
  202. 'description' => $sDescription,
  203. 'date' => $iTime,
  204. 'active' => (int)$aInfo['active'],
  205. 'hidden' => (int)$aInfo['hidden'],
  206. 'data' => $aData
  207. );
  208. return $aResult;
  209. }
  210. /**
  211. * Migrate timeline link from Dolphin to UNA
  212. *
  213. * @param int $iEventId record id
  214. * @param string $sUrl html url
  215. * @param int $iProfileId profile id
  216. * @return int affected rows
  217. */
  218. private function addLink($iEventId, $sUrl, $iProfileId)
  219. {
  220. $aSiteInfo = bx_get_site_info($sUrl, array(
  221. 'thumbnailUrl' => array('tag' => 'link', 'content_attr' => 'href'),
  222. 'OGImage' => array('name_attr' => 'property', 'name' => 'og:image'),
  223. ));
  224. $sTitle = !empty($aSiteInfo['title']) ? $aSiteInfo['title'] : _t('_Empty');
  225. $sDescription = !empty($aSiteInfo['description']) ? $aSiteInfo['description'] : _t('_Empty');
  226. $sMediaUrl = '';
  227. if(!empty($aSiteInfo['thumbnailUrl']))
  228. $sMediaUrl = $aSiteInfo['thumbnailUrl'];
  229. else if(!empty($aSiteInfo['OGImage']))
  230. $sMediaUrl = $aSiteInfo['OGImage'];
  231. $iMediaId = 0;
  232. $oStorage = null;
  233. if(!empty($sMediaUrl))
  234. {
  235. $oStorage = BxDolStorage::getObjectInstance('bx_timeline_photos');
  236. $iMediaId = $oStorage->storeFileFromUrl($sMediaUrl, true, $iProfileId);
  237. $oStorage -> afterUploadCleanup($iMediaId, $iProfileId);
  238. }
  239. if (!$sTitle && !$sDescription && !$iMediaId)
  240. return false;
  241. $sQuery = $this -> _oDb -> prepare("
  242. INSERT INTO
  243. `bx_timeline_links`
  244. SET
  245. `profile_id` = ?,
  246. `media_id` = ?,
  247. `url` = ?,
  248. `title` = ?,
  249. `text` = ?,
  250. `added` = UNIX_TIMESTAMP()
  251. ", $iProfileId, $iMediaId, $sUrl, $sTitle, $sDescription);
  252. $this -> _oDb -> query($sQuery);
  253. return $this -> _oDb -> query("INSERT INTO `bx_timeline_links2events` SET `event_id` = :event, `link_id` = :link", array('event' => $iEventId, 'link' => $this -> _oDb -> lastId()));
  254. }
  255. /**
  256. * Transfer timeline photos
  257. *
  258. * @param int $iEventId UNA record id
  259. * @param int $iPhotoId Description for $iPhotoId
  260. * @param int $iProfileId Description for $iProfileId
  261. * @return Return description
  262. */
  263. private function addPhoto($iEventId, $iPhotoId, $iProfileId)
  264. {
  265. $aMedia = $this -> _mDb -> getRow("SELECT * FROM `bx_photos_main` WHERE `ID`=:id LIMIT 1", array('id' => $iPhotoId));
  266. if (empty($aMedia))
  267. return false;
  268. $sImagePath = $this -> _sImagePhotoFiles . "{$aMedia['ID']}.{$aMedia['Ext']}";
  269. if (file_exists($sImagePath))
  270. {
  271. $oStorage = BxDolStorage::getObjectInstance('bx_timeline_photos');
  272. $iMediaId = $oStorage -> storeFileFromPath($sImagePath, true, $iProfileId, $iEventId);
  273. if ($iMediaId)
  274. {
  275. $oStorage -> afterUploadCleanup($iMediaId, $iProfileId);
  276. return $this -> _oDb -> query("INSERT INTO `bx_timeline_photos2events` SET `event_id` = :event, `media_id` = :media_id", array('event' => $iEventId, 'media_id' => $iMediaId));
  277. }
  278. }
  279. return false;
  280. }
  281. private function addVideo($iEventId, $iVideoId, $iProfileId)
  282. {
  283. $aMedia = $this -> _mDb -> getRow("SELECT * FROM `RayVideoFiles` WHERE `ID`=:id LIMIT 1", array('id' => $iVideoId));
  284. if (empty($aMedia))
  285. return false;
  286. $sVideoPath = $this -> _sImageVideoFiles . "{$iVideoId}.m4v";
  287. if (file_exists($sVideoPath))
  288. {
  289. $oStorage = BxDolStorage::getObjectInstance('bx_timeline_videos');
  290. $iMediaId = $oStorage -> storeFileFromPath($sVideoPath, true, $iProfileId, $iEventId);
  291. if ($iMediaId)
  292. {
  293. $oStorage -> afterUploadCleanup($iMediaId, $iProfileId);
  294. return $this -> _oDb -> query("INSERT INTO `bx_timeline_videos2events` SET `event_id` = :event, `media_id` = :media_id", array('event' => $iEventId, 'media_id' => $iMediaId));
  295. }
  296. }
  297. return false;
  298. }
  299. public function runMigration()
  300. {
  301. if (!$this -> getTotalRecords())
  302. {
  303. $this -> setResultStatus(_t('_bx_dolphin_migration_no_data_to_transfer'));
  304. return BX_MIG_SUCCESSFUL;
  305. }
  306. $this -> setResultStatus(_t('_bx_dolphin_migration_started_migration_timeline'));
  307. $this -> createMIdField();
  308. $iFeedsId = $this -> getLastMIDField();
  309. $sStart = '';
  310. if ($iFeedsId)
  311. $sStart = " WHERE `id` > {$iFeedsId}";
  312. $aResult = $this -> _mDb -> getAll("SELECT * FROM `". $this -> _oConfig -> _aMigrationModules[$this -> _sModuleName]['table_name'] . "` {$sStart} ORDER BY `id`");
  313. foreach($aResult as $iKey => $aValue)
  314. {
  315. $aInfo = $this -> getFormatedData($aValue);
  316. if (empty($aInfo) || !$aInfo['type'])
  317. continue;
  318. $iFeedId = $this -> isItemExisted($aValue['id']);
  319. if (!$iFeedId)
  320. {
  321. $sQuery = $this -> _oDb -> prepare(
  322. "
  323. INSERT INTO
  324. `{$this -> _sTableWithTransKey}`
  325. SET
  326. `owner_id` = ?,
  327. `type` = ?,
  328. `action` = ?,
  329. `object_id` = ?,
  330. `object_privacy_view` = ?,
  331. `content` = ?,
  332. `title` = ?,
  333. `description` = ?,
  334. `date` = ?,
  335. `active` = ?,
  336. `status` = ?
  337. ",
  338. $aInfo['owner_id'],
  339. $aInfo['type'],
  340. $aInfo['action'],
  341. $aInfo['object_id'],
  342. $this -> getValidPrivacy($aInfo['privacy']),
  343. $aInfo['content'],
  344. $aInfo['title'],
  345. $aInfo['description'],
  346. $aInfo['date'],
  347. $aInfo['active'],
  348. $aInfo['hidden'] ? 'hidden' : 'active'
  349. );
  350. $this -> _oDb -> query($sQuery);
  351. $iFeedId = $this -> _oDb -> lastId();
  352. if (!$iFeedId){
  353. $this -> setResultStatus(_t('_bx_dolphin_migration_started_migration_timeline_error', (int)$aValue['id']));
  354. return BX_MIG_FAILED;
  355. }
  356. $this -> _iTransferred++;
  357. }
  358. if (!empty($aInfo['data']))
  359. switch($aInfo['data']['type'])
  360. {
  361. case 'videos' : $this -> addVideo($iFeedId, $aInfo['data']['media_id'], $aInfo['owner_id']); break;
  362. case 'photos' : $this -> addPhoto($iFeedId, $aInfo['data']['media_id'], $aInfo['owner_id']); break;
  363. case 'link' : $this -> addLink($iFeedId, $aInfo['data']['url'], $aInfo['owner_id']); break;
  364. }
  365. $iComments = $this -> transferComments($iFeedId, (int)$aValue['id'], 'timeline');
  366. $this -> _oDb -> query("UPDATE `{$this -> _sTableWithTransKey}`
  367. SET
  368. `comments` = :cmts
  369. WHERE `id` = :id",
  370. array(
  371. 'id' => $iFeedId,
  372. 'cmts' => (int)$iComments,
  373. ));
  374. $this -> setMID($iFeedId, $aValue['id']);
  375. }
  376. // set as finished;
  377. $this -> setResultStatus(_t('_bx_dolphin_migration_started_migration_timeline_finished', $this -> _iTransferred));
  378. return BX_MIG_SUCCESSFUL;
  379. }
  380. public function removeContent()
  381. {
  382. if (!$this -> _oDb -> isTableExists($this -> _sTableWithTransKey) || !$this -> _oDb -> isFieldExists($this -> _sTableWithTransKey, $this -> _sTransferFieldIdent))
  383. return false;
  384. $aRecords = $this -> _oDb -> getAll("SELECT
  385. `e`.`id`,
  386. `l`.`link_id`,
  387. `p`.`media_id` as `photo_id`,
  388. `v`.`media_id` as `video_id`
  389. FROM `{$this -> _sTableWithTransKey}` as `e`
  390. LEFT JOIN `bx_timeline_links2events` as `l` ON `e`.`id` = `l`.`event_id`
  391. LEFT JOIN `bx_timeline_photos2events` as `p` ON `e`.`id` = `p`.`event_id`
  392. LEFT JOIN `bx_timeline_videos2events` as `v` ON `e`.`id` = `v`.`event_id`
  393. WHERE `e`.`{$this -> _sTransferFieldIdent}`!=0");
  394. if (!empty($aRecords))
  395. {
  396. $iNumber = 0;
  397. foreach($aRecords as $iKey => $aValue)
  398. {
  399. $oComments = BxDolCmts::getObjectInstance('bx_timeline', $aValue['id']);
  400. if($oComments !== false)
  401. $oComments->onObjectDelete($aValue['id']);
  402. if ($aValue['video_id'])
  403. {
  404. $oStorage = BxDolStorage::getObjectInstance('bx_timeline_videos');
  405. $oStorage->deleteFile($aValue['video_id']);
  406. $this -> _oDb -> query("DELETE FROM `bx_timeline_videos2events` WHERE `event_id`=:id", array('id' => $aValue['id']));
  407. }
  408. if ($aValue['photo_id'])
  409. {
  410. $oStorage = BxDolStorage::getObjectInstance('bx_timeline_photos');
  411. $oStorage->deleteFile($aValue['photo_id']);
  412. $this -> _oDb -> query("DELETE FROM `bx_timeline_photos2events` WHERE `event_id`=:id", array('id' => $aValue['id']));
  413. }
  414. if ($aValue['link_id'])
  415. {
  416. $sQuery = $this-> _oDb -> prepare("DELETE FROM `tl`, `tle` USING `bx_timeline_links` AS `tl` LEFT JOIN `bx_timeline_links2events` AS `tle` ON `tl`.`id`=`tle`.`link_id` WHERE `tle`.`event_id` = ?", $aValue['id']);
  417. $this -> _oDb -> query($sQuery);
  418. }
  419. $this -> _oDb -> query("DELETE FROM `{$this -> _sTableWithTransKey}` WHERE `id`=:id", array('id' => $aValue['id']));
  420. $iNumber++;
  421. }
  422. }
  423. parent::removeContent();
  424. return $iNumber;
  425. }
  426. }
  427. /** @} */