BxSEMigBlogs.php 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  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 BxSEMigBlogs extends BxSEMigData {
  14. public function BxSEMigBlogs (&$oMigrationModule, &$oSE) {
  15. parent::BxSEMigData($oMigrationModule, $oSE);
  16. }
  17. public function getTotalRecords(){
  18. return (int)$this -> _seDb -> getOne("SELECT COUNT(*) FROM `{$this -> _sEnginePrefix}blog_blogs`");
  19. }
  20. public function runMigration () {
  21. if (!$this -> getTotalRecords()){
  22. $this -> setResultStatus(_t('_bx_se_migration_no_data_to_transfer'));
  23. return BX_SEMIG_SUCCESSFUL;
  24. }
  25. $this -> setResultStatus(_t('_bx_se_migration_started_migration_blogs'));
  26. $aResult = $this -> _seDb -> getAll("SELECT * FROM `{$this -> _sEnginePrefix}blog_blogs` ORDER BY `blog_id`");
  27. $iComments = 0;
  28. foreach($aResult as $iKey => $aValue){
  29. $iProfileId = $this -> getContentId((int)$aValue['owner_id']);
  30. if (!$iProfileId || $this -> isBlogExisted($iProfileId, $aValue['title'])) continue;
  31. $sQuery = $this -> _oDb -> prepare(
  32. "
  33. INSERT INTO
  34. `bx_posts_posts`
  35. SET
  36. `author` = ?,
  37. `added` = ?,
  38. `changed` = ?,
  39. `thumb` = 0,
  40. `title` = ?,
  41. `text` = ?,
  42. `views` = ?,
  43. `comments` = ?,
  44. `status` = 'active',
  45. `cat` = ?
  46. ",
  47. $iProfileId,
  48. isset($aValue['creation_date']) ? strtotime($aValue['creation_date']) : time(),
  49. isset($aValue['modified_date']) ? strtotime($aValue['modified_date']) : time(),
  50. isset($aValue['title']) && $aValue['title'] ? $aValue['title'] : '',
  51. isset($aValue['body']) && $aValue['body'] ? $aValue['body'] : '',
  52. (int)$aValue['view_count'],
  53. (int)$aValue['comment_count'],
  54. (int)$aValue['category_id']
  55. );
  56. $this -> _oDb -> query($sQuery);
  57. $iBlogId = $this -> _oDb -> lastId();
  58. if (!$iBlogId){
  59. $this -> setResultStatus(_t('_bx_se_migration_started_migration_blogs_error', (int)$aValue['blog_id']));
  60. return BX_SEMIG_FAILED;
  61. }
  62. $iComments += $this -> transferComments((int)$aValue['blog_id'], $iBlogId, $iProfileId);
  63. $this -> _iTransferred++;
  64. }
  65. $iCategories = 0;
  66. if ($this -> _iTransferred) $iCategories = $this -> transferCategories();
  67. // set as finished;
  68. $this -> setResultStatus(_t('_bx_se_migration_started_migration_blogs_finished', $this -> _iTransferred, $iComments, $iCategories));
  69. return BX_SEMIG_SUCCESSFUL;
  70. }
  71. /**
  72. * Returns Post's plugin Languages Category ID
  73. * @return Integer
  74. */
  75. private function getPostsCategoryId(){
  76. return (int)$this -> _oDb -> getOne("SELECT `ID` FROM `sys_localization_categories` WHERE `Name` = 'Posts' LIMIT 1");
  77. }
  78. /**
  79. * Check if category already transferred
  80. * @param int $iID category ID
  81. * @param string $sName language key
  82. * @return Boolean
  83. */
  84. private function isCategoryExisted($iID, $sName){
  85. $sQuery = $this -> _oDb -> prepare("SELECT COUNT(*) FROM `sys_form_pre_values` WHERE `Value` = ? AND `LKey` = ? LIMIT 1", $iID, $sName);
  86. return (int)$this -> _oDb -> getOne($sQuery) == 1;
  87. }
  88. /**
  89. * Transfers all categories
  90. * @return Integer
  91. */
  92. private function transferCategories(){
  93. $oLanguage = BxDolStudioLanguagesUtils::getInstance();
  94. $iPostsCategoryId = $this -> getPostsCategoryId();
  95. /* get all blogs categories from SE*/
  96. $aSECategories = $this -> _seDb -> getPairs("SELECT * FROM `{$this -> _sEnginePrefix}blog_categories` ORDER BY `category_id` ASC", 'category_id', 'category_name');
  97. if (empty($aSECategories) || !$iPostsCategoryId) return false;
  98. /* remove all current posts categories */
  99. $aCategories = $this -> _oDb -> getPairs("SELECT * FROM `sys_form_pre_values` WHERE `Key` = 'bx_posts_cats' AND `Value` <> '' ORDER BY `Order` ASC", 'Value', 'LKey');
  100. foreach($aCategories as $iKey => $sValue){
  101. $oLanguage -> deleteLanguageString($sValue);
  102. $this -> _oDb -> query("DELETE FROM `sys_form_pre_values` WHERE `Key` = 'bx_posts_cats' AND `LKey` = '{$sValue}'");
  103. }
  104. /* transfer blogs categories from Social Engine */
  105. $iTransferred = 0;
  106. foreach($aSECategories as $iKey => $sValue){
  107. $sValue = bx_process_input($sValue);
  108. if ($this -> isCategoryExisted($iKey, $sValue)) continue;
  109. $sKey = '_bx_posts_cats_' . $sValue;
  110. $sQuery = $this -> _oDb -> prepare("INSERT INTO `sys_form_pre_values` SET
  111. `Key` = 'bx_posts_cats',
  112. `Value` = ?,
  113. `Order` = ?,
  114. `LKey` = ?",
  115. $iKey,
  116. $iKey,
  117. $sKey);
  118. /*add new Language Keys*/
  119. $oLanguage -> addLanguageString($sKey, $sValue, 0, $iPostsCategoryId);
  120. if ($this -> _oDb -> query($sQuery)) $iTransferred++;
  121. }
  122. if ($iTransferred)
  123. return $iTransferred;
  124. }
  125. /**
  126. * Check if Blog already transferred by Author ID and Blog's Title
  127. * @param int $iAuthor social engine's profile ID
  128. * @param string $sTitle blog's title
  129. * @return Boolean
  130. */
  131. private function isBlogExisted($iAuthor, $sTitle){
  132. $sQuery = $this -> _oDb -> prepare("SELECT COUNT(*) FROM `bx_posts_posts` WHERE `author` = ? AND `title` = ? LIMIT 1", $iAuthor, $sTitle);
  133. return (bool)$this -> _oDb -> getOne($sQuery);
  134. }
  135. /**
  136. * Transfer all Blogs' Comments
  137. * @param int $iOldBlogId social engine's blog's ID
  138. * @param int $iNewBlogId una post's ID
  139. * @return Integer
  140. */
  141. private function transferComments($iOldBlogId, $iNewBlogId){
  142. $aComments = $this -> _seDb -> getAll("SELECT * FROM `{$this -> _sEnginePrefix}core_comments` WHERE `resource_type` = 'blog' AND `resource_id` = '{$iOldBlogId}' AND `poster_type` = 'user' ORDER BY `comment_id` ASC");
  143. if (empty($aComments)) return false;
  144. $iComments = 0;
  145. foreach($aComments as $iKey => $aComment){
  146. $iProfileId = $this -> getContentId($aComment['poster_id']);
  147. $sQuery = $this -> _oDb -> prepare("INSERT INTO `bx_posts_cmts` SET
  148. `cmt_object_id` = ?,
  149. `cmt_author_id` = ?,
  150. `cmt_text` = ?,
  151. `cmt_rate_count` = ?,
  152. `cmt_time` = ?
  153. ",
  154. $iNewBlogId,
  155. $iProfileId,
  156. bx_process_input($aComment['body']),
  157. (int)$aComment['like_count'],
  158. isset($aComment['creation_date']) ? strtotime($aComment['creation_date']) : time()
  159. );
  160. $this -> _oDb -> query($sQuery);
  161. $iComments++;
  162. }
  163. return $iComments;
  164. }
  165. }
  166. /** @} */