BxDataMigrationSounds.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363
  1. <?php
  2. bx_import('BxDolCategories');
  3. bx_import('BxDolAlbums');
  4. require_once 'BxDataMigrationData.php';
  5. class BxDataMigrationSounds extends BxDataMigrationData
  6. {
  7. var $sNewAlbumName;
  8. var $sType;
  9. var $sTablePrefix;
  10. var $iTransffered = 0;
  11. /**
  12. * Class constructor;
  13. *
  14. * @param : $oMigrationModule (object) - object instance of migration class;
  15. * @param : $rOldDb (resourse) - connect to old dolphin's database;
  16. * @param : $oDolModule (object);
  17. * @return : void;
  18. */
  19. function BxDataMigrationSounds(&$oMigrationModule, &$rOldDb, $oDolModule = '')
  20. {
  21. parent::BxDataMigrationData($oMigrationModule, $rOldDb, $oDolModule);
  22. $this -> sNewAlbumNames = getParam('bx_sounds_profile_album_name');
  23. $sMainPrefix = $oDolModule -> _oConfig -> getMainPrefix();
  24. $this -> sType = $sMainPrefix;
  25. $this -> sTablePrefix = $sMainPrefix . '_';
  26. }
  27. /**
  28. * Function migrate sounds;
  29. *
  30. * @return : (integer) operation result;
  31. */
  32. function getMigration()
  33. {
  34. if(!$this -> oDolModule) {
  35. $this -> setResultStatus('System error: object instance is not received');
  36. return MIGRATION_FAILED;
  37. }
  38. // set new status;
  39. $this -> setResultStatus('All sounds transfer now');
  40. mysql_query('SET NAMES utf8', $this->rOldDb);
  41. $sQuery = "SELECT * FROM `RayMusicFiles`";
  42. $rResult = mysql_query($sQuery, $this -> rOldDb);
  43. while( $aRow = mysql_fetch_assoc($rResult) )
  44. {
  45. if( !$this -> isSoundsExisting($aRow['ID']) ) {
  46. // escape all data;
  47. $aRow = $this -> escapeData($aRow);
  48. //transform the file
  49. if(!$this -> _exportFile($aRow['ID'])) {
  50. continue;
  51. }
  52. // define file status;
  53. $sFileStatus = $aRow['Approved'] == 'true' ? 'approved' : 'disapproved';
  54. // execute query;
  55. $sQuery =
  56. "
  57. INSERT INTO
  58. `RayMp3Files`
  59. SET
  60. `ID` = {$aRow['ID']},
  61. `Title` = '{$aRow['Title']}',
  62. `Uri` = '{$aRow['Uri']}',
  63. `Tags` = '{$aRow['Tags']}',
  64. `Description` = '{$aRow['Description']}',
  65. `Time` = '{$aRow['Time']}',
  66. `Date` = '{$aRow['Date']}',
  67. `Reports` = '{$aRow['Reports']}',
  68. `Owner` = '{$aRow['Owner']}',
  69. `Listens` = '{$aRow['Listens']}',
  70. `Status` = '$sFileStatus'
  71. ";
  72. $iResult = (int) $this -> oMigrationModule -> _oDb -> query($sQuery);
  73. if($iResult <= 0) {
  74. $this -> setResultStatus('Database error. Cannot insert new sound in the database.');
  75. return MIGRATION_FAILED;
  76. }
  77. $oTag = new BxDolTags();
  78. $oTag -> reparseObjTags($this -> sType, $aRow['ID']);
  79. // define sound's album;
  80. $sError = $this -> _defineSoundsAlbum($aRow['ID'], $aRow['Owner'], $aRow['Title']);
  81. if($sError) {
  82. $this -> setResultStatus($sError);
  83. return MIGRATION_FAILED;
  84. }
  85. $sError = $this -> _exportVotings($aRow['ID']);
  86. if($sError) {
  87. $this -> setResultStatus($sError);
  88. return MIGRATION_FAILED;
  89. }
  90. $sError = $this -> _exportComments($aRow['ID']);
  91. if($sError) {
  92. $this -> setResultStatus($sError);
  93. return MIGRATION_FAILED;
  94. }
  95. $this -> iTransffered++;
  96. }
  97. else {
  98. $this -> setResultStatus('Duplicate data.
  99. Sounds with similar info already exists
  100. (remove all sounds and start again');
  101. return MIGRATION_FAILED;
  102. }
  103. }
  104. // set as finished;
  105. $this -> setResultStatus('All sound were transferred (' . $this -> iTransffered . ' items)');
  106. return MIGRATION_SUCCESSFUL;
  107. }
  108. /**
  109. * Function define sound's album and if not exist will create;
  110. *
  111. * @param : $iFileId (integer);
  112. * @param : $iOwnerId (integer) - sound's owner profile id;
  113. * @return : (string) - error message or empty;
  114. */
  115. function _defineSoundsAlbum($iFileId, $iOwnerId, $sFileName)
  116. {
  117. $iFileId = (int) $iFileId;
  118. $iOwnerId = (int) $iOwnerId;
  119. $sNickName = getNickName($iOwnerId);
  120. if(!$sNickName) {
  121. //return 'Owner of ' . $sFileName . ' was not defined';
  122. }
  123. $sAlbum = str_replace('{nickname}', $sNickName, $this -> sNewAlbumNames);
  124. $oAlbums = & new BxDolAlbums($this -> sType, $iOwnerId);
  125. $aData = array(
  126. 'caption' => $this -> oMigrationModule -> _oDb -> escape($sAlbum),
  127. 'owner' => $iOwnerId,
  128. 'description' => '',
  129. 'location' => '',
  130. 'AllowAlbumView' => BX_DOL_PG_ALL,
  131. );
  132. $iAlbumId = (int) $oAlbums -> addAlbum($aData);
  133. // check objects;
  134. $sQuery = "SELECT COUNT(*) FROM `sys_albums_objects` WHERE `id_album` = {$iAlbumId} AND `id_object` = {$iFileId}";
  135. if( !$this -> oMigrationModule -> _oDb -> getOne($sQuery) ) {
  136. $oAlbums -> addObject($iAlbumId, $iFileId, true);
  137. }
  138. }
  139. /**
  140. * Function export all sound comments;
  141. *
  142. * @param : $iFileId (integer);
  143. * @return : (string) - error message or empty;
  144. */
  145. function _exportComments($iFileId)
  146. {
  147. $iFileId = (int) $iFileId;
  148. $sQuery = "SELECT * FROM `CmtsSharedMusic` WHERE `cmt_object_id` = {$iFileId}";
  149. $rResult = mysql_query($sQuery, $this -> rOldDb);
  150. while( $aRow = mysql_fetch_assoc($rResult) )
  151. {
  152. $sText = $this -> oMigrationModule -> _oDb -> escape($aRow['cmt_text']);
  153. $sQuery =
  154. "
  155. INSERT INTO
  156. `{$this -> sTablePrefix}cmts`
  157. SET
  158. `cmt_id` = {$aRow['cmt_id']},
  159. `cmt_parent_id` = {$aRow['cmt_parent_id']},
  160. `cmt_object_id` = {$aRow['cmt_object_id']},
  161. `cmt_author_id` = {$aRow['cmt_author_id']},
  162. `cmt_text` = '{$sText}',
  163. `cmt_rate` = {$aRow['cmt_rate']},
  164. `cmt_rate_count` = {$aRow['cmt_rate_count']},
  165. `cmt_time` = '{$aRow['cmt_time']}',
  166. `cmt_replies` = {$aRow['cmt_replies']}
  167. ";
  168. $iResult = (int) $this -> oMigrationModule -> _oDb -> query($sQuery);
  169. if($iResult <= 0) {
  170. return 'Database error. Cannot insert comments information in the database.';
  171. }
  172. }
  173. }
  174. /**
  175. * Function export all sound votings;
  176. *
  177. * @param : $iFileId (integer);
  178. * @return : (string) - error message or empty;
  179. */
  180. function _exportVotings($iFileId)
  181. {
  182. $iFileId = (int) $iFileId;
  183. $sQuery = "SELECT * FROM `gmusic_rating` WHERE `gal_id` = {$iFileId}";
  184. $rResult = mysql_query($sQuery, $this -> rOldDb);
  185. while( $aRow = mysql_fetch_assoc($rResult) )
  186. {
  187. $sQuery =
  188. "
  189. INSERT INTO
  190. `{$this -> sTablePrefix}rating`
  191. SET
  192. `gal_id` = {$aRow['gal_id']},
  193. `gal_rating_count` = {$aRow['gal_rating_count']},
  194. `gal_rating_sum` = {$aRow['gal_rating_sum']}
  195. ";
  196. $iResult = (int) $this -> oMigrationModule -> _oDb -> query($sQuery);
  197. if($iResult <= 0) {
  198. return 'Database error. Cannot insert votes information in the database.';
  199. }
  200. //export voting tracks;
  201. $sError = $this -> exportVotingTrack($aRow['gal_id']);
  202. if($sError) {
  203. return $sError;
  204. }
  205. $fRate = $aRow['gal_rating_sum'] / $aRow['gal_rating_count'];
  206. //update primary table;
  207. $sQuery =
  208. "
  209. UPDATE
  210. `RayMp3Files`
  211. SET
  212. `Rate` = $fRate,
  213. `RateCount` = {$aRow['gal_rating_count']}
  214. WHERE
  215. `ID` = {$iFileId}
  216. ";
  217. $iResult = (int) $this -> oMigrationModule -> _oDb -> query($sQuery);
  218. if($iResult <= 0) {
  219. return 'Database error. Cannot update votes information in the database.';
  220. }
  221. }
  222. }
  223. /**
  224. * Function export all sound votings track;
  225. *
  226. * @param : $iGalId (integer);
  227. * @return : (string) - error message or empty;
  228. */
  229. function exportVotingTrack($iGalId)
  230. {
  231. $iGalId = (int) $iGalId;
  232. $sQuery = "SELECT * FROM `gmusic_voting_track` WHERE `gal_id` = {$iGalId}";
  233. $rResult = mysql_query($sQuery, $this -> rOldDb);
  234. while( $aRow = mysql_fetch_assoc($rResult) )
  235. {
  236. $sQuery =
  237. "
  238. INSERT INTO
  239. `{$this -> sTablePrefix}voting_track`
  240. SET
  241. `gal_id` = {$aRow['gal_id']},
  242. `gal_ip` = '{$aRow['gal_ip']}',
  243. `gal_date` = '{$aRow['gal_date']}'
  244. ";
  245. $iResult = (int) $this -> oMigrationModule -> _oDb -> query($sQuery);
  246. if($iResult <= 0) {
  247. return 'Database error. Cannot insert votes track information in the database.';
  248. }
  249. }
  250. }
  251. /**
  252. * Function export sound file;
  253. *
  254. * @param : $iFileId (integer);
  255. * @return : (boolean);
  256. */
  257. function _exportFile($iFileId)
  258. {
  259. $iFileId = (int) $iFileId;
  260. $bOpperation = true;
  261. // copy file;
  262. $sFileName = $iFileId . '.mp3';
  263. $sOldFile = $this -> oMigrationModule -> _oDb -> getExtraParam('config_root');
  264. $sOldFile .= 'ray/modules/music/files/' . $sFileName;
  265. $sNewDirectory = BX_DIRECTORY_PATH_ROOT . 'flash/modules/mp3/files/';
  266. if( !@copy($sOldFile, $sNewDirectory . $sFileName) ){
  267. $bOpperation = false;
  268. }
  269. return $bOpperation;
  270. }
  271. /**
  272. * Function export all favorites records;
  273. *
  274. * @param : $iFileId (integer);
  275. * @return : (string) - error message or empty;
  276. */
  277. function _exportFavorites ($iFileId) {
  278. $iFileId = (int)$iFileId;
  279. $sqlQuery = "SELECT `medID`, `userID`, UNIX_TIMESTAMP(`favDate`) as `favDate` FROM `shareMusicFavorites` WHERE `medID` = {$iFileId}";
  280. $rResult = mysql_query($sqlQuery, $this->rOldDb);
  281. while ($aRow = mysql_fetch_assoc($rResult)) {
  282. $sqlQuery =
  283. "
  284. INSERT INTO
  285. `{$this->sType}_favorites`
  286. SET
  287. `ID` = {$aRow['medID']},
  288. `Profile` = '{$aRow['userID']}',
  289. `Date` = '{$aRow['gal_date']}'
  290. ";
  291. $iResult = (int)$this->oMigrationModule->_oDb->query($sqlQuery);
  292. if ($iResult <= 0) {
  293. return 'Database error. Cannot insert favorites information to the database.';
  294. }
  295. }
  296. }
  297. /**
  298. * Function check exsiting sounds;
  299. *
  300. * @param : $iSoundId (integer) - sound's id;
  301. * @return : true if exist;
  302. */
  303. function isSoundsExisting($iSoundId)
  304. {
  305. $iSoundId = (int) $iSoundId;
  306. $sQuery = "SELECT COUNT(*) FROM `RayMp3Files` WHERE `ID` = '{$iSoundId}'";
  307. return $this -> oMigrationModule -> _oDb -> getOne($sQuery) ? true : false;
  308. }
  309. }