BxDolMFiles.php 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  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 BxDolMFiles extends BxDolMData
  14. {
  15. /**
  16. *@var path to the directory with Dolphin files
  17. */
  18. private $_sFilesPath = '';
  19. public function __construct(&$oMigrationModule, &$oDb)
  20. {
  21. parent::__construct($oMigrationModule, $oDb);
  22. $this -> _sModuleName = 'files';
  23. $this -> _sTableWithTransKey = 'bx_files_main';
  24. $this -> _sFilesPath = $this -> _oDb -> getExtraParam('root') . 'modules' . DIRECTORY_SEPARATOR . 'boonex' . DIRECTORY_SEPARATOR . 'files' . DIRECTORY_SEPARATOR . 'data' . 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'] . "` WHERE `Status` IN ('approved', 'pending')");
  29. }
  30. public function runMigration()
  31. {
  32. if (!$this -> getTotalRecords())
  33. {
  34. $this -> setResultStatus(_t('_bx_dolphin_migration_no_data_to_transfer'));
  35. return BX_MIG_SUCCESSFUL;
  36. }
  37. $this -> setResultStatus(_t('_bx_dolphin_migration_started_migration_files'));
  38. $this -> createMIdField();
  39. $iFileId = $this -> getLastMIDField();
  40. $sStart = '';
  41. if ($iFileId)
  42. $sStart = " AND `ID` >= {$iFileId}";
  43. $aResult = $this -> _mDb -> getAll("SELECT * FROM `" . $this -> _oConfig -> _aMigrationModules[$this -> _sModuleName]['table_name'] . "` WHERE `Status` IN ('approved', 'pending') {$sStart} ORDER BY `ID`");
  44. $iCmts = 0;
  45. foreach($aResult as $iKey => $aValue)
  46. {
  47. $iProfileId = $this -> getProfileId((int)$aValue['Owner']);
  48. $iFileId = $this -> isItemExisted($aValue['ID']);
  49. if (!$iProfileId)
  50. continue;
  51. $iDate = isset($aValue['Date']) ? $aValue['Date'] : time();
  52. if (!$iFileId)
  53. {
  54. $sQuery = $this -> _oDb -> prepare(
  55. "
  56. INSERT INTO
  57. `{$this -> _sTableWithTransKey}`
  58. SET
  59. `author` = ?,
  60. `added` = ?,
  61. `changed` = ?,
  62. `title` = ?,
  63. `desc` = ?,
  64. `cat` = ?,
  65. `views` = ?,
  66. `featured` = ?,
  67. `allow_view_to` = ?
  68. ",
  69. $iProfileId,
  70. $iDate,
  71. $iDate,
  72. isset($aValue['Title']) ? $aValue['Title'] : '',
  73. isset($aValue['Desc']) ? $aValue['Desc'] : '',
  74. $this -> transferCategory($aValue['Categories']),
  75. isset($aValue['Views']) ? (int)$aValue['Views'] : 0,
  76. isset($aValue['Featured']) ? (int)$aValue['Featured'] : 0,
  77. $this -> getPrivacy((int)$aValue['Owner'], isset($aValue['AllowDownload']) ? (int)$aValue['AllowDownload'] : 0, 'files', 'download')
  78. );
  79. $this -> _oDb -> query($sQuery);
  80. $iFileId = $this -> _oDb -> lastId();
  81. if (!$iFileId)
  82. {
  83. $this -> setResultStatus(_t('_bx_dolphin_migration_started_migration_files_error', (int)$aValue['ID']));
  84. return BX_MIG_FAILED;
  85. }
  86. $this -> setMID($iFileId, $aValue['ID']);
  87. $this -> exportFile($aValue['ID'] . '_' . sha1($iDate), $aValue['Ext'], $iFileId, $iProfileId);
  88. }
  89. $iCmts = $this -> transferComments($iFileId, (int)$aValue['ID'], 'files');
  90. $this -> _iTransferred++;
  91. $this -> _oDb -> query("UPDATE `{$this -> _sTableWithTransKey}`
  92. SET
  93. `comments` = :cmts
  94. WHERE `id` = :id", array('id' => $iFileId, 'cmts' => $iCmts));
  95. $this -> transferTags((int)$aValue['ID'], $iFileId, $this -> _oConfig -> _aMigrationModules[$this -> _sModuleName]['type'], $this -> _oConfig -> _aMigrationModules[$this -> _sModuleName]['keywords']);
  96. }
  97. // set as finished;
  98. $this -> setResultStatus(_t('_bx_dolphin_migration_started_migration_files_finished', $this -> _iTransferred, $iCmts));
  99. return BX_MIG_SUCCESSFUL;
  100. }
  101. /**
  102. * Export file by hash name
  103. *
  104. * @param string $sName hash
  105. * @param string $sExt file extension
  106. * @param int $iFileID filed id in UNA
  107. * @param int $iProfileId file owner id in UNA
  108. * @return mixed affected rows or false
  109. */
  110. private function exportFile($sName, $sExt, $iFileID, $iProfileId)
  111. {
  112. $sFilesPath = $this -> _sFilesPath . "{$sName}";
  113. if (file_exists($sFilesPath) && copy($sFilesPath, BX_DIRECTORY_PATH_TMP . "{$sName}.{$sExt}"))
  114. {
  115. $oStorage = BxDolStorage::getObjectInstance('bx_files_files');
  116. $iId = $oStorage->storeFileFromPath(BX_DIRECTORY_PATH_TMP . "{$sName}.{$sExt}", false, $iProfileId);
  117. $oStorage -> afterUploadCleanup($iId, $iProfileId);
  118. $sQuery = $this -> _oDb -> prepare("UPDATE `{$this -> _sTableWithTransKey}` SET `file_id`=? WHERE `id`=?", $iId, $iFileID);
  119. return $this -> _oDb-> query($sQuery);
  120. }
  121. return false;
  122. }
  123. protected function transferCategory($sCategory, $sPrefix = 'bx_files', $sPreValueCateg = 'bx_files_cats', $iValue = 0, $sData = '')
  124. {
  125. return parent::transferCategory($sCategory, $sPrefix, $sPreValueCateg);
  126. }
  127. public function removeContent()
  128. {
  129. if (!$this -> _oDb -> isTableExists($this -> _sTableWithTransKey) || !$this -> _oDb -> isFieldExists($this -> _sTableWithTransKey, $this -> _sTransferFieldIdent))
  130. return false;
  131. $iNumber = 0;
  132. $aRecords = $this -> _oDb -> getAll("SELECT * FROM `{$this -> _sTableWithTransKey}` WHERE `{$this -> _sTransferFieldIdent}` !=0");
  133. if (!empty($aRecords))
  134. {
  135. foreach($aRecords as $iKey => $aValue)
  136. {
  137. BxDolService::call('bx_files', 'delete_entity', array($aValue['id']));
  138. $iNumber++;
  139. }
  140. }
  141. parent::removeContent();
  142. return $iNumber;
  143. }
  144. }
  145. /** @} */