BxDataMigrationOrcaForum.php 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521
  1. <?php
  2. require_once 'BxDataMigrationData.php';
  3. //-- this is abstract class, need to inherit --//
  4. class BxDataMigrationOrcaForum extends BxDataMigrationData
  5. {
  6. var $isGroupForum;
  7. var $sOldTablePrefix;
  8. var $sNewTablePrefix;
  9. /**
  10. * Class constructor;
  11. *
  12. * @param : $oMigrationModule (object) - object instance of migration class;
  13. * @param : $rOldDb (resourse) - connect to old dolphin's database;
  14. * @param : $oDolModule (object);
  15. * @return : void;
  16. */
  17. function BxDataMigrationOrcaForum(&$oMigrationModule, &$rOldDb, $oDolModule = '', $isGroupForum = false)
  18. {
  19. parent::BxDataMigrationData($oMigrationModule, $rOldDb, $oDolModule);
  20. $this -> isGroupForum = $isGroupForum;
  21. $this -> sOldTablePrefix = !$this -> isGroupForum ? 'pre_' : 'grp_' ;
  22. $this -> sNewTablePrefix = !$this -> isGroupForum ? 'bx_' : 'bx_groups_' ;
  23. }
  24. //-- private methods --//
  25. /**
  26. * Transfer all forum's categories;
  27. *
  28. * @return : (string) - error message or empty;
  29. */
  30. function _transferCategories()
  31. {
  32. if($this -> isGroupForum) {
  33. // clear all data;
  34. $sQuery = "TRUNCATE TABLE `{$this -> sNewTablePrefix}forum_cat`";
  35. $this -> oMigrationModule -> _oDb -> query($sQuery);
  36. }
  37. // get all categories;
  38. $sQuery = "SELECT * FROM `{$this -> sOldTablePrefix}forum_cat`";
  39. $rResult = mysql_query($sQuery, $this -> rOldDb);
  40. while( $aRow = mysql_fetch_assoc($rResult) )
  41. {
  42. if( !$this -> isCategoryExisting($aRow['cat_id']) ) {
  43. // escape all data;
  44. $aRow = $this -> escapeData($aRow);
  45. // insert new;
  46. $sQuery =
  47. "
  48. INSERT INTO
  49. `{$this -> sNewTablePrefix}forum_cat`
  50. SET
  51. `cat_id` = '{$aRow['cat_id']}',
  52. `cat_uri` = '{$aRow['cat_uri']}',
  53. `cat_name` = '{$aRow['cat_name']}',
  54. `cat_icon` = '{$aRow['cat_icon']}',
  55. `cat_order` = '{$aRow['cat_order']}'
  56. ";
  57. $iResult = (int) $this -> oMigrationModule -> _oDb -> query($sQuery);
  58. if($iResult <= 0) {
  59. return 'Database error. Cannot insert new forum\'s category in the database.';
  60. }
  61. }
  62. else {
  63. return 'Duplicate data (in forum categories).
  64. Remove all data from forum and start again';
  65. }
  66. }
  67. }
  68. /**
  69. * Transfer all forums;
  70. *
  71. * @return : (string) - error message or empty;
  72. */
  73. function _transferForums()
  74. {
  75. // get all forums;
  76. $sQuery = "SELECT * FROM `{$this -> sOldTablePrefix}forum`";
  77. $rResult = mysql_query($sQuery, $this -> rOldDb);
  78. $sExtraSql = '';
  79. while( $aRow = mysql_fetch_assoc($rResult) )
  80. {
  81. if( !$this -> isForumExisting($aRow['forum_id']) ) {
  82. // escape all data;
  83. $aRow = $this -> escapeData($aRow);
  84. if ($this -> isGroupForum) {
  85. $sExtraSql = ", `entry_id` = '{$aRow['forum_id']}'";
  86. }
  87. // insert new;
  88. $sQuery =
  89. "
  90. INSERT INTO
  91. `{$this -> sNewTablePrefix}forum`
  92. SET
  93. `forum_id` = '{$aRow['forum_id']}',
  94. `forum_uri` = '{$aRow['forum_uri']}',
  95. `cat_id` = '{$aRow['cat_id']}',
  96. `forum_title` = '{$aRow['forum_title']}',
  97. `forum_desc` = '{$aRow['forum_desc']}',
  98. `forum_posts` = '{$aRow['forum_posts']}',
  99. `forum_topics` = '{$aRow['forum_topics']}',
  100. `forum_last` = '{$aRow['forum_last']}',
  101. `forum_type` = '{$aRow['forum_type']}'
  102. {$sExtraSql}
  103. ";
  104. $iResult = (int) $this -> oMigrationModule -> _oDb -> query($sQuery);
  105. if($iResult <= 0) {
  106. return 'Database error. Cannot insert new forum in the database.';
  107. }
  108. }
  109. else {
  110. return 'Duplicate data (in forum).
  111. Remove all data from forum and start again';
  112. }
  113. }
  114. }
  115. /**
  116. * Transfer all forum's flags;
  117. *
  118. * @return : (string) - error message or empty;
  119. */
  120. function _transferFlags()
  121. {
  122. // get all flags;
  123. $sQuery = "SELECT * FROM `{$this -> sOldTablePrefix}forum_flag`";
  124. $rResult = mysql_query($sQuery, $this -> rOldDb);
  125. while( $aRow = mysql_fetch_assoc($rResult) )
  126. {
  127. if( !$this -> isFlagExisting($aRow['user'], $aRow['topic_id']) ) {
  128. // escape all data;
  129. $aRow = $this -> escapeData($aRow);
  130. // insert new;
  131. $sQuery =
  132. "
  133. INSERT INTO
  134. `{$this -> sNewTablePrefix}forum_flag`
  135. SET
  136. `user` = '{$aRow['user']}',
  137. `topic_id` = '{$aRow['topic_id']}',
  138. `when` = '{$aRow['when']}'
  139. ";
  140. $iResult = (int) $this -> oMigrationModule -> _oDb -> query($sQuery);
  141. if($iResult <= 0) {
  142. return 'Database error. Cannot insert new forum\'s flag in the database.';
  143. }
  144. }
  145. else {
  146. return 'Duplicate data (in forum flags).
  147. Remove all data from forum and start again';
  148. }
  149. }
  150. }
  151. /**
  152. * Transfer all forum's posts;
  153. *
  154. * @return : (string) - error message or empty;
  155. */
  156. function _transferPosts()
  157. {
  158. // get all posts;
  159. $sQuery = "SELECT * FROM `{$this -> sOldTablePrefix}forum_post`";
  160. $rResult = mysql_query($sQuery, $this -> rOldDb);
  161. while( $aRow = mysql_fetch_assoc($rResult) )
  162. {
  163. if( !$this -> isPostExisting($aRow['post_id']) ) {
  164. // escape all data;
  165. $aRow = $this -> escapeData($aRow);
  166. // insert new;
  167. $sQuery =
  168. "
  169. INSERT INTO
  170. `{$this -> sNewTablePrefix}forum_post`
  171. SET
  172. `post_id` = '{$aRow['post_id']}',
  173. `topic_id` = '{$aRow['topic_id']}',
  174. `forum_id` = '{$aRow['forum_id']}',
  175. `user` = '{$aRow['user']}',
  176. `post_text` = '{$aRow['post_text']}',
  177. `when` = '{$aRow['when']}',
  178. `votes` = '{$aRow['votes']}',
  179. `reports` = '{$aRow['reports']}'
  180. ";
  181. $iResult = (int) $this -> oMigrationModule -> _oDb -> query($sQuery);
  182. if($iResult <= 0) {
  183. return 'Database error. Cannot insert new forum\'s post in the database.';
  184. }
  185. }
  186. else {
  187. return 'Duplicate data (in forum posts).
  188. Remove all data from forum and start again';
  189. }
  190. }
  191. }
  192. /**
  193. * Transfer all forum's topics;
  194. *
  195. * @return : (string) - error message or empty;
  196. */
  197. function _transferTopics()
  198. {
  199. // get all topics;
  200. $sQuery = "SELECT * FROM `{$this -> sOldTablePrefix}forum_topic`";
  201. $rResult = mysql_query($sQuery, $this -> rOldDb);
  202. while( $aRow = mysql_fetch_assoc($rResult) )
  203. {
  204. if( !$this -> isTopicExisting($aRow['topic_id']) ) {
  205. // escape all data;
  206. $aRow = $this -> escapeData($aRow);
  207. // insert new;
  208. $sQuery =
  209. "
  210. INSERT INTO
  211. `{$this -> sNewTablePrefix}forum_topic`
  212. SET
  213. `topic_id` = '{$aRow['topic_id']}',
  214. `topic_uri` = '{$aRow['topic_uri']}',
  215. `forum_id` = '{$aRow['forum_id']}',
  216. `topic_title` = '{$aRow['topic_title']}',
  217. `when` = '{$aRow['when']}',
  218. `topic_posts` = '{$aRow['topic_posts']}',
  219. `first_post_user` = '{$aRow['first_post_user']}',
  220. `first_post_when` = '{$aRow['first_post_when']}',
  221. `last_post_user` = '{$aRow['last_post_user']}',
  222. `last_post_when` = '{$aRow['last_post_when']}',
  223. `topic_sticky` = '{$aRow['topic_sticky']}',
  224. `topic_locked` = '{$aRow['topic_locked']}'
  225. ";
  226. $iResult = (int) $this -> oMigrationModule -> _oDb -> query($sQuery);
  227. if($iResult <= 0) {
  228. return 'Database error. Cannot insert new forum\'s topic in the database.';
  229. }
  230. }
  231. else {
  232. return 'Duplicate data (in forum topics).
  233. Remove all data from forum and start again';
  234. }
  235. }
  236. }
  237. /**
  238. * Transfer all forum's users;
  239. *
  240. * @return : (string) - error message or empty;
  241. */
  242. function _transferUsers()
  243. {
  244. // get all users;
  245. $sQuery = "SELECT * FROM `{$this -> sOldTablePrefix}forum_user`";
  246. $rResult = mysql_query($sQuery, $this -> rOldDb);
  247. while( $aRow = mysql_fetch_assoc($rResult) )
  248. {
  249. if( !$this -> isUserExisting($aRow['user_name']) ) {
  250. // escape all data;
  251. $aRow = $this -> escapeData($aRow);
  252. // insert new;
  253. $sQuery =
  254. "
  255. INSERT INTO
  256. `{$this -> sNewTablePrefix}forum_user`
  257. SET
  258. `user_name` = '{$aRow['user_name']}',
  259. `user_pwd` = '{$aRow['user_pwd']}',
  260. `user_email` = '{$aRow['user_email']}',
  261. `user_join_date` = '{$aRow['user_join_date']}'
  262. ";
  263. $iResult = (int) $this -> oMigrationModule -> _oDb -> query($sQuery);
  264. if($iResult <= 0) {
  265. return 'Database error. Cannot insert new user in the database.';
  266. }
  267. }
  268. else {
  269. return 'Duplicate data (in forum users).
  270. Remove all data from forum and start again';
  271. }
  272. }
  273. }
  274. /**
  275. * Transfer all forum's users activity;
  276. *
  277. * @return : (string) - error message or empty;
  278. */
  279. function _transferUsersActivity()
  280. {
  281. // clear all data;
  282. $sQuery = "TRUNCATE TABLE `{$this -> sNewTablePrefix}forum_user_activity`";
  283. $this -> oMigrationModule -> _oDb -> query($sQuery);
  284. // get all users activity;
  285. $sQuery = "SELECT * FROM `{$this -> sOldTablePrefix}forum_user_activity`";
  286. $rResult = mysql_query($sQuery, $this -> rOldDb);
  287. while( $aRow = mysql_fetch_assoc($rResult) )
  288. {
  289. // escape all data;
  290. $aRow = $this -> escapeData($aRow);
  291. // insert new;
  292. $sQuery =
  293. "
  294. INSERT INTO
  295. `{$this -> sNewTablePrefix}forum_user_activity`
  296. SET
  297. `user` = '{$aRow['user']}',
  298. `act_current` = '{$aRow['act_current']}',
  299. `act_last` = '{$aRow['act_last']}'
  300. ";
  301. $iResult = (int) $this -> oMigrationModule -> _oDb -> query($sQuery);
  302. if($iResult <= 0) {
  303. return 'Database error. Cannot insert new user activity in the database.';
  304. }
  305. }
  306. }
  307. /**
  308. * Transfer all forum's users stat;
  309. *
  310. * @return : (string) - error message or empty;
  311. */
  312. function _transferUsersStat()
  313. {
  314. // clear all data;
  315. $sQuery = "TRUNCATE TABLE `{$this -> sNewTablePrefix}forum_user_stat`";
  316. $this -> oMigrationModule -> _oDb -> query($sQuery);
  317. // get all users stat;
  318. $sQuery = "SELECT * FROM `{$this -> sOldTablePrefix}forum_user_stat`";
  319. $rResult = mysql_query($sQuery, $this -> rOldDb);
  320. while( $aRow = mysql_fetch_assoc($rResult) )
  321. {
  322. // escape all data;
  323. $aRow = $this -> escapeData($aRow);
  324. // insert new;
  325. $sQuery =
  326. "
  327. INSERT INTO
  328. `{$this -> sNewTablePrefix}forum_user_stat`
  329. SET
  330. `user` = '{$aRow['user']}',
  331. `posts` = '{$aRow['posts']}',
  332. `user_last_post` = '{$aRow['user_last_post']}'
  333. ";
  334. $iResult = (int) $this -> oMigrationModule -> _oDb -> query($sQuery);
  335. if($iResult <= 0) {
  336. return 'Database error. Cannot insert new user stat in the database.';
  337. }
  338. }
  339. }
  340. /**
  341. * Transfer all forum's users vote;
  342. *
  343. * @return : (string) - error message or empty;
  344. */
  345. function _transferUsersVote()
  346. {
  347. // get all users vote;
  348. $sQuery = "SELECT * FROM `{$this -> sOldTablePrefix}forum_vote`";
  349. $rResult = mysql_query($sQuery, $this -> rOldDb);
  350. while( $aRow = mysql_fetch_assoc($rResult) )
  351. {
  352. if( !$this -> isUserVoteExisting($aRow['user']) ) {
  353. // escape all data;
  354. $aRow = $this -> escapeData($aRow);
  355. // insert new;
  356. $sQuery =
  357. "
  358. INSERT INTO
  359. `{$this -> sNewTablePrefix}forum_vote`
  360. SET
  361. `user_name` = '{$aRow['user_name']}',
  362. `post_id` = '{$aRow['post_id']}',
  363. `vote_when` = '{$aRow['vote_when']}',
  364. `vote_point` = '{$aRow['vote_point']}'
  365. ";
  366. $iResult = (int) $this -> oMigrationModule -> _oDb -> query($sQuery);
  367. if($iResult <= 0) {
  368. return 'Database error. Cannot insert new user vote in the database.';
  369. }
  370. }
  371. else {
  372. return 'Duplicate data (in forum users vote).
  373. Remove all data from forum and start again';
  374. }
  375. }
  376. }
  377. //-- public methods --//
  378. /**
  379. * Check category existing;
  380. *
  381. * @param : $iCatId integer - category id;
  382. * @return : boolean true if exist;
  383. */
  384. function isCategoryExisting($iCatId)
  385. {
  386. $iCatId = (int) $iCatId;
  387. $sQuery = "SELECT COUNT(*) FROM `{$this -> sNewTablePrefix}forum_cat` WHERE `cat_id` = {$iCatId}";
  388. return $this -> oMigrationModule -> _oDb -> getOne($sQuery) ? true : false;
  389. }
  390. /**
  391. * Check forum existing;
  392. *
  393. * @param : $iForumId integer - forum id;
  394. * @return : boolean true if exist;
  395. */
  396. function isForumExisting($iForumId)
  397. {
  398. $iForumId = (int) $iForumId;
  399. $sQuery = "SELECT COUNT(*) FROM `{$this -> sNewTablePrefix}forum` WHERE `forum_id` = {$iForumId}";
  400. return $this -> oMigrationModule -> _oDb -> getOne($sQuery) ? true : false;
  401. }
  402. /**
  403. * Check flags existing;
  404. *
  405. * @param : $iUserId string - user nickname;
  406. * @param : $iTopicId integer - topic id;
  407. * @return : boolean true if exist;
  408. */
  409. function isFlagExisting($sUserName, $iTopicId)
  410. {
  411. $sUserName = $this -> oMigrationModule -> _oDb -> escape($sUserName);
  412. $iTopicId = (int) $iTopicId;
  413. $sQuery = "SELECT COUNT(*) FROM `{$this -> sNewTablePrefix}forum_flag` WHERE `user` = '{$sUserName}' AND `topic_id` = {$iTopicId}";
  414. return $this -> oMigrationModule -> _oDb -> getOne($sQuery) ? true : false;
  415. }
  416. /**
  417. * Check post existing;
  418. *
  419. * @param : $iPostId integer - post id;
  420. * @return : boolean true if exist;
  421. */
  422. function isPostExisting($iPostId)
  423. {
  424. $iPostId = (int) $iPostId;
  425. $sQuery = "SELECT COUNT(*) FROM `{$this -> sNewTablePrefix}forum_post` WHERE `post_id` = {$iPostId}";
  426. return $this -> oMigrationModule -> _oDb -> getOne($sQuery) ? true : false;
  427. }
  428. /**
  429. * Check topic existing;
  430. *
  431. * @param : $iTopicId integer - topic id;
  432. * @return : boolean true if exist;
  433. */
  434. function isTopicExisting($iTopicId)
  435. {
  436. $iTopicId = (int) $iTopicId;
  437. $sQuery = "SELECT COUNT(*) FROM `{$this -> sNewTablePrefix}forum_topic` WHERE `topic_id` = {$iTopicId}";
  438. return $this -> oMigrationModule -> _oDb -> getOne($sQuery) ? true : false;
  439. }
  440. /**
  441. * Check user existing;
  442. *
  443. * @param : $sUserName string;
  444. * @return : boolean true if exist;
  445. */
  446. function isUserExisting($sUserName)
  447. {
  448. $sUserName = $this -> oMigrationModule -> _oDb -> escape($sUserName);
  449. $sQuery = "SELECT COUNT(*) FROM `{$this -> sNewTablePrefix}forum_user` WHERE `user_name` = '{$sUserName}'";
  450. return $this -> oMigrationModule -> _oDb -> getOne($sQuery) ? true : false;
  451. }
  452. /**
  453. * Check user vote;
  454. *
  455. * @param : $sUserName string;
  456. * @return : boolean true if exist;
  457. */
  458. function isUserVoteExisting($sUserName)
  459. {
  460. $sUserName = $this -> oMigrationModule -> _oDb -> escape($sUserName);
  461. $sQuery = "SELECT COUNT(*) FROM `{$this -> sNewTablePrefix}forum_vote` WHERE `user_name` = '{$sUserName}'";
  462. return $this -> oMigrationModule -> _oDb -> getOne($sQuery) ? true : false;
  463. }
  464. }