BxDataMigrationBlogs.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. <?php
  2. require_once 'BxDataMigrationData.php';
  3. class BxDataMigrationBlogs extends BxDataMigrationData {
  4. var $iTransffered = 0;
  5. var $iPostsTransffered = 0;
  6. /**
  7. * Class constructor;
  8. *
  9. * @param : $oMigrationModule (object) - object instance of migration class;
  10. * @param : $rOldDb (resourse) - connect to old dolphin's database;
  11. * @param : $oDolModule (object);
  12. * @return : void;
  13. */
  14. function BxDataMigrationBlogs(&$oMigrationModule, &$rOldDb, $oDolModule = '') {
  15. parent::BxDataMigrationData($oMigrationModule, $rOldDb, $oDolModule);
  16. }
  17. /**
  18. * Function migrate blogs data;
  19. *
  20. * @return : (integer) operation result;
  21. */
  22. function getMigration() {
  23. // set new status;
  24. $this -> setResultStatus('All blogs transfer now');
  25. mysql_query('SET NAMES utf8', $this->rOldDb);
  26. // transfer all profiles blogs, categories and posts;
  27. $this -> transferProfileBlogs();
  28. // set as finished;
  29. $this -> setResultStatus('All blogs were transferred (' . $this -> iTransffered . ' blogs, ' . $this -> iPostsTransffered . ' posts)');
  30. return MIGRATION_SUCCESSFUL;
  31. }
  32. /**
  33. * Function will transfer all profile's blogs, categories and posts;
  34. *
  35. * @return : void;
  36. */
  37. function transferProfileBlogs() {
  38. $sTruncateSQL1 = "TRUNCATE TABLE `bx_blogs_cmts`";
  39. $this -> oMigrationModule -> _oDb -> query($sTruncateSQL1);
  40. $sTruncateSQL2 = "TRUNCATE TABLE `bx_blogs_main`";
  41. $this -> oMigrationModule -> _oDb -> query($sTruncateSQL2);
  42. $sTruncateSQL3 = "TRUNCATE TABLE `bx_blogs_posts`";
  43. $this -> oMigrationModule -> _oDb -> query($sTruncateSQL3);
  44. $sTruncateSQL4 = "DELETE FROM `sys_categories` WHERE `Type` = 'bx_blogs' AND `Owner` > 0";
  45. $this -> oMigrationModule -> _oDb -> query($sTruncateSQL4);
  46. $sSrcPath = $this -> oMigrationModule -> _oDb -> getExtraParam('config_mediaImages') . 'blog/';
  47. $sDstPath = BX_DIRECTORY_PATH_ROOT . "media/images/blog/";
  48. $aPosts = array();
  49. $aCategories = array();
  50. $sQuery = "SELECT * FROM `Blogs` ORDER BY `OwnerID` ASC";
  51. $rResult = mysql_query($sQuery, $this -> rOldDb);
  52. while($aRow = mysql_fetch_assoc($rResult)) {
  53. $iBlogID = (int)$aRow['ID'];
  54. $iBlogOwnerID = (int)$aRow['OwnerID'];
  55. $sBlogDescription = $this -> oMigrationModule -> _oDb -> escape($aRow['Description']);
  56. $sCatSQL = "SELECT * FROM `BlogCategories` WHERE `OwnerID`='{$iBlogOwnerID}' ORDER BY `BlogCategories`.`CategoryID` ASC";
  57. $rCatRes = mysql_query($sCatSQL, $this -> rOldDb);
  58. while($aCatRow = mysql_fetch_assoc($rCatRes)) {
  59. $iCatID = (int)$aCatRow['CategoryID'];
  60. if (! array_key_exists($iCatID, $aCategories)) {
  61. $aCategories[$iCatID] = $iCatID;
  62. $sCatName = $aCatRow['CategoryName'];
  63. //-- eSASe modification --//
  64. if($sCatName == 'Uncategorized') {
  65. $sCatName = '';
  66. }
  67. if($sCatName && $this -> isCategoryExist('bx_blogs', $sCatName) ) {
  68. $sCatName = '';
  69. }
  70. //--
  71. $sCatName = $this -> oMigrationModule -> _oDb -> escape($sCatName);
  72. $sPostsSQL = "SELECT *, UNIX_TIMESTAMP(`PostDate`) AS 'Date_UTS' FROM `BlogPosts` WHERE `CategoryID`='{$iCatID}' ORDER BY `BlogPosts`.`CategoryID` ASC";
  73. $rPostsRes = mysql_query($sPostsSQL, $this -> rOldDb);
  74. if (mysql_num_rows($rPostsRes)) {
  75. while($aPostsRow = mysql_fetch_assoc($rPostsRes)) {
  76. $iPostID = (int)$aPostsRow['PostID'];
  77. if (! array_key_exists($iPostID, $aPosts)) {
  78. $aPosts[$iPostID] = $iPostID;
  79. $iPostDateUTS = (int)$aPostsRow['Date_UTS'];
  80. $sPostCaption = $this -> oMigrationModule -> _oDb -> escape($aPostsRow['PostCaption']);
  81. $sPostUri = $this -> oMigrationModule -> _oDb -> escape($aPostsRow['PostUri']);
  82. $sPostText = $this -> oMigrationModule -> _oDb -> escape($aPostsRow['PostText']);
  83. $sPostStatus = $this -> oMigrationModule -> _oDb -> escape($aPostsRow['PostStatus']);
  84. $sPostPhoto = $this -> oMigrationModule -> _oDb -> escape($aPostsRow['PostPhoto']);
  85. $sPostTags = $this -> oMigrationModule -> _oDb -> escape($aPostsRow['Tags']);
  86. $iPostDateUTS = (int)$aPostsRow['Date_UTS'];
  87. $iAllowView = ($aPostsRow['PostReadPermission'] == 'public') ? 3 : 5;
  88. $iAllowComment = ($aPostsRow['PostCommentPermission'] == 'public') ? 3 : 5;
  89. $sPostCommentsCntSQL = "SELECT COUNT(*) FROM `CmtsBlogPosts` WHERE `cmt_object_id`='{$iPostID}'";
  90. $rPostCommentsCntRes = mysql_query($sPostCommentsCntSQL, $this -> rOldDb);
  91. $aPostCommentsCnt = mysql_fetch_array($rPostCommentsCntRes);
  92. $iPostCommentsCnt = (int)$aPostCommentsCnt[0];
  93. // adding posts
  94. $sPostQuery =
  95. "
  96. INSERT INTO
  97. `bx_blogs_posts`
  98. SET
  99. `PostID` = '{$iPostID}',
  100. `PostCaption` = '{$sPostCaption}',
  101. `PostUri` = '{$sPostUri}',
  102. `PostText` = '{$sPostText}',
  103. `PostDate` = '{$iPostDateUTS}',
  104. `PostStatus` = '{$sPostStatus}',
  105. `PostPhoto` = '{$sPostPhoto}',
  106. `Tags` = '{$sPostTags}',
  107. `Featured` = 0,
  108. `Views` = 0,
  109. `Rate` = 0,
  110. `RateCount` = 0,
  111. `CommentsCount` = {$iPostCommentsCnt},
  112. `OwnerID` = '{$iBlogOwnerID}',
  113. `Categories` = '{$sCatName}',
  114. `allowView` = {$iAllowView},
  115. `allowRate` = 3,
  116. `allowComment` = {$iAllowComment}
  117. ";
  118. $this -> oMigrationModule -> _oDb -> query($sPostQuery);
  119. $this -> iPostsTransffered++;
  120. // TODO images
  121. @copy($sSrcPath . 'big_' . $sPostPhoto, $sDstPath . '_big_' . $sPostPhoto);
  122. imageResize($sDstPath . '_big_' . $sPostPhoto, $sDstPath . 'small_' . $sPostPhoto, $this -> oDolModule->iIconSize, $this -> oDolModule->iIconSize);
  123. imageResize($sDstPath . '_big_' . $sPostPhoto, $sDstPath . 'big_' . $sPostPhoto, $this -> oDolModule->iThumbSize, $this -> oDolModule->iThumbSize);
  124. imageResize($sDstPath . '_big_' . $sPostPhoto, $sDstPath . 'orig_' . $sPostPhoto, $this -> oDolModule->iImgSize, $this -> oDolModule->iImgSize);
  125. @unlink ($sDstPath . '_big_' . $sPostPhoto);
  126. // adding custom categories (with >0 posts inside)
  127. if($sCatName) {
  128. $sCatQuery =
  129. "
  130. INSERT INTO
  131. `sys_categories`
  132. SET
  133. `Type` = 'bx_blogs',
  134. `Category` = '{$sCatName}',
  135. `Owner` = '{$iBlogOwnerID}',
  136. `ID` = '{$iPostID}'
  137. ";
  138. $this -> oMigrationModule -> _oDb -> query($sCatQuery);
  139. }
  140. }
  141. }
  142. } else {
  143. // adding custom categories (with 0 posts inside)
  144. if($sCatName) {
  145. $sCatQuery =
  146. "
  147. INSERT INTO
  148. `sys_categories`
  149. SET
  150. `Type` = 'bx_blogs',
  151. `Category` = '{$sCatName}',
  152. `Owner` = '{$iBlogOwnerID}'
  153. ";
  154. $this -> oMigrationModule -> _oDb -> query($sCatQuery);
  155. }
  156. }
  157. }
  158. }
  159. // adding blogs
  160. $sBlogQuery =
  161. "
  162. INSERT INTO
  163. `bx_blogs_main`
  164. SET
  165. `ID` = {$iBlogID},
  166. `OwnerID` = '{$iBlogOwnerID}',
  167. `Description` = '{$sBlogDescription}'
  168. ";
  169. $this -> oMigrationModule -> _oDb -> query($sBlogQuery);
  170. $this -> iTransffered++;
  171. }
  172. // moving comments
  173. $sCommQuery = "SELECT * FROM `CmtsBlogPosts`";
  174. $rCommResult = mysql_query($sCommQuery, $this -> rOldDb);
  175. while($aCommRow = mysql_fetch_assoc($rCommResult)) {
  176. $iCmtID = (int)$aCommRow['cmt_id'];
  177. $iCmtParentID = (int)$aCommRow['cmt_parent_id'];
  178. $iCmtObjectID = (int)$aCommRow['cmt_object_id'];
  179. $iCmtAuthorID = (int)$aCommRow['cmt_author_id'];
  180. $sCmtText = $this -> oMigrationModule -> _oDb -> escape($aCommRow['cmt_text']);
  181. $iCmtRate = (int)$aCommRow['cmt_rate'];
  182. $iCmtRateCnt = (int)$aCommRow['cmt_rate_count'];
  183. $sCmtTime = $aCommRow['cmt_time'];
  184. $sCmtReplies = (int)$aCommRow['cmt_replies'];
  185. $sCommSQL = "
  186. INSERT INTO `bx_blogs_cmts` (`cmt_id`, `cmt_parent_id`, `cmt_object_id`, `cmt_author_id`, `cmt_text`, `cmt_mood`, `cmt_rate`, `cmt_rate_count`, `cmt_time`, `cmt_replies`) VALUES ({$iCmtID}, {$iCmtParentID}, {$iCmtObjectID}, {$iCmtAuthorID}, '{$sCmtText}', 0, {$iCmtRate}, {$iCmtRateCnt}, '{$sCmtTime}', {$sCmtReplies});
  187. ";
  188. $this -> oMigrationModule -> _oDb -> query($sCommSQL);
  189. }
  190. }
  191. /**
  192. * Check category;
  193. *
  194. * @return booelan - true if exist;
  195. */
  196. function isCategoryExist($sType, $sCategory)
  197. {
  198. $sCategory = $this -> oMigrationModule -> _oDb -> escape($sCategory);
  199. $sType = $this -> oMigrationModule -> _oDb -> escape($sType);
  200. $sQuery = "SELECT COUNT(*) FROM `sys_categories` WHERE `Type` = '{$sType}' AND `Category` = '{$sCategory}'";
  201. return $this -> oMigrationModule -> _oDb -> getOne($sQuery) ? true : false;
  202. }
  203. }
  204. ?>