BxSEMigPhotoAlbums.php 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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. require_once('BxSEMigData.php');
  12. bx_import('BxDolStorage');
  13. class BxSEMigPhotoAlbums extends BxSEMigData {
  14. private $_iTransferredAlbums;
  15. public function BxSEMigPhotoAlbums (&$oMigrationModule, &$oSE) {
  16. parent::BxSEMigData($oMigrationModule, $oSE);
  17. }
  18. public function getTotalRecords(){
  19. return (int)$this -> _seDb -> getOne("SELECT SUM(`count`) FROM (
  20. SELECT COUNT(*) as `count` FROM `{$this -> _sEnginePrefix}storage_files`
  21. WHERE `parent_file_id` IS NULL AND `parent_type` IN ('album_photo', 'user') GROUP BY `parent_id`
  22. ) as `c`");
  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_photos'));
  30. $aResult = $this -> _seDb -> getAll("SELECT * FROM `{$this -> _sEnginePrefix}album_albums` WHERE `type` = 'profile' ORDER BY `album_id`");
  31. foreach($aResult as $iKey => $aValue){
  32. $iProfileId = $this -> getContentId((int)$aValue['owner_id']);
  33. if (!$iProfileId) continue;
  34. $sAlbumTitle = isset($aValue['title']) && $aValue['title'] ? $aValue['title'] : 'Profile Photos';
  35. if (!$this -> isAlbumExisted($iProfileId, $sAlbumTitle)){
  36. $sQuery = $this -> _oDb -> prepare(
  37. "
  38. INSERT INTO
  39. `bx_albums_albums`
  40. SET
  41. `author` = ?,
  42. `added` = ?,
  43. `changed` = ?,
  44. `thumb` = 0,
  45. `title` = ?,
  46. `text` = ?,
  47. `views` = ?
  48. ",
  49. $iProfileId,
  50. isset($aValue['creation_date']) ? strtotime($aValue['creation_date']) : time(),
  51. isset($aValue['modified_date']) ? strtotime($aValue['modified_date']) : time(),
  52. isset($aValue['title']) && $aValue['title'] ? $aValue['title'] : 'Profile Photos',
  53. isset($aValue['description']) && $aValue['description'] ? $aValue['description'] : '',
  54. (int)$aValue['view_count']
  55. );
  56. $this -> _oDb -> query($sQuery);
  57. $iAlbumId = $this -> _oDb -> lastId();
  58. if (!$iAlbumId){
  59. $this -> setResultStatus(_t('_bx_se_migration_started_migration_photos_album_error'));
  60. return BX_SEMIG_FAILED;
  61. }
  62. $this -> migrateAlbumPhotos($aValue['owner_id'], $iProfileId, $iAlbumId);
  63. }
  64. $this -> _iTransferredAlbums++;
  65. }
  66. // set as finished;
  67. $this -> setResultStatus(_t('_bx_se_migration_started_migration_photos_finished', $this -> _iTransferred));
  68. return BX_SEMIG_SUCCESSFUL;
  69. }
  70. /**
  71. * Migrates all photo albums and users photos
  72. * @param int $iUserId social engine's profile ID
  73. * @param int $iProfileId una profile ID
  74. * @param int $iAlbumID created una Album
  75. * @return Integer
  76. */
  77. private function migrateAlbumPhotos($iUserId, $iProfileId, $iAlbumID){
  78. $aResult = $this -> _seDb -> getAll("SELECT * FROM `{$this -> _sEnginePrefix}storage_files` WHERE `parent_file_id` IS NULL AND `parent_type` IN ('album_photo', 'user') AND `parent_id` = {$iUserId} ORDER BY `file_id` ASC");
  79. $iTransferred = 0;
  80. foreach($aResult as $iKey => $aValue){
  81. if ($this -> isFileExisted($iProfileId, $aValue['name'])) continue;
  82. $sOldImagePath = $this -> _oDb -> getExtraParam('root') . $aValue['storage_path'];
  83. if (file_exists($sOldImagePath)){
  84. $oStorage = BxDolStorage::getObjectInstance('bx_albums_files');
  85. $iId = $oStorage -> storeFileFromPath($sOldImagePath, false, $iProfileId);
  86. if ($iId){
  87. $sQuery = $this -> _oDb -> prepare("INSERT INTO `bx_albums_files2albums` SET `content_id` = ?, `file_id` = ?, `data` = ?, `title` = ?", $iAlbumID, $iId, $this -> getFileSize($sOldImagePath), $aValue['name']);
  88. $this -> _oDb -> query($sQuery);
  89. $this -> _iTransferred++;
  90. $iTransferred++;
  91. }
  92. }
  93. }
  94. return $iTransferred;
  95. }
  96. private function isAlbumExisted($iAuthor, $sTitle){
  97. $sQuery = $this -> _oDb -> prepare("SELECT COUNT(*) FROM `bx_albums_albums` WHERE `author` = ? AND `title` = ? LIMIT 1", $iAuthor, $sTitle);
  98. return (bool)$this -> _oDb -> getOne($sQuery);
  99. }
  100. private function isFileExisted($iAuthor, $sTitle){
  101. $sQuery = $this -> _oDb -> prepare("SELECT COUNT(*) FROM `bx_albums_files` WHERE `profile_id` = ? AND `file_name` = ? LIMIT 1", $iAuthor, $sTitle);
  102. return (bool)$this -> _oDb -> getOne($sQuery);
  103. }
  104. private function getFileSize ($sFile) {
  105. $aInfo = getimagesize($sFile);
  106. return $aInfo[0] . 'x' . $aInfo[1];
  107. }
  108. }
  109. /** @} */