BxDataMigrationSharedPhotos.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385
  1. <?php
  2. require_once('BxDataMigrationData.php');
  3. class BxDataMigrationSharedPhotos extends BxDataMigrationData {
  4. var $sType;
  5. var $sOldFilesPath;
  6. var $aOldTables;
  7. var $sNewAlbumName;
  8. var $sNewFilesPath;
  9. var $aNewSizes;
  10. /**
  11. * Class constructor;
  12. *
  13. * @param : $oMigrationModule (object) - object instance of migration class;
  14. * @param : $rOldDb (resourse) - connect to old dolphin's database;
  15. * @param : $oDolModule (object);
  16. * @return : void;
  17. */
  18. function BxDataMigrationSharedPhotos (&$oMigrationModule, &$rOldDb, $oDolModule = '') {
  19. parent::BxDataMigrationData($oMigrationModule, $rOldDb, $oDolModule);
  20. $this->sType = 'bx_photos';
  21. $this->sOldFilesPath = 'media/images/sharingImages/';
  22. $this->aOldTables = array(
  23. 'main' => 'sharePhotoFiles',
  24. 'favorite' => 'sharePhotoFavorites',
  25. 'rate' => 'gphoto_rating',
  26. 'rate_track' => 'gphoto_voting_track',
  27. 'cmts' => 'CmtsSharedPhoto'
  28. );
  29. $this->sNewAlbumName = $oDolModule->_oConfig->getGlParam('profile_album_name');
  30. $this->sNewFilesPath = 'modules/boonex/photos/data/files/';
  31. $this->aNewSizes = array(
  32. 'icon' => array('width' => $oDolModule->_oConfig->getGlParam('icon_width'), 'height' => $oDolModule->_oConfig->getGlParam('icon_height'), 'postfix' => '_ri'),
  33. 'thumb' => array('width' => $oDolModule->_oConfig->getGlParam('thumb_width'), 'height' => $oDolModule->_oConfig->getGlParam('thumb_height'), 'postfix' => '_rt'),
  34. 'browse' => array('width' => $oDolModule->_oConfig->getGlParam('browse_width'), 'height' => $oDolModule->_oConfig->getGlParam('browse_height'), 'postfix' => '_t'),
  35. 'file' => array('width' => $oDolModule->_oConfig->getGlParam('file_width'), 'height' => $oDolModule->_oConfig->getGlParam('file_height'), 'postfix' => '_m')
  36. );
  37. }
  38. /**
  39. * Function migrate shared_photos data;
  40. *
  41. * @return : (integer) operation result;
  42. */
  43. function getMigration () {
  44. if (!$this->oDolModule) {
  45. $this->setResultStatus('System error: object instance is not received');
  46. return MIGRATION_FAILED;
  47. }
  48. // set new status;
  49. $this->setResultStatus('Shared photos transfer now');
  50. mysql_query('SET NAMES utf8', $this->rOldDb);
  51. $sqlQuery = "SELECT * FROM `{$this->aOldTables['main']}` GROUP BY `medID`";
  52. $rResult = mysql_query($sqlQuery, $this->rOldDb);
  53. while ($aRow = mysql_fetch_assoc($rResult)) {
  54. $sUri = !$this->isEntryExisting($aRow['medUri'])
  55. ? $aRow['medUri']
  56. : uriGenerate($aRow['medTitle'], $this->sType . '_main', 'Uri');
  57. $sUri = $this -> oMigrationModule -> _oDb -> escape($sUri);
  58. $aSizeInfo = $this->getSizeInfo($aRow['medID'] . '.' . $aRow['medExt']);
  59. $sSize = $aSizeInfo['width'] . 'x' . $aSizeInfo['height'];
  60. $sStatus = $aRow['Approved'] == 'true' ? 'approved' : 'disapproved';
  61. $sHash = md5(microtime());
  62. // escape all data;
  63. $aRow = $this -> escapeData($aRow);
  64. $sqlQuery =
  65. "
  66. INSERT INTO
  67. `{$this->sType}_main`
  68. SET
  69. `Owner` = '{$aRow['medProfId']}',
  70. `Ext` = '{$aRow['medExt']}',
  71. `Size` = '$sSize',
  72. `Title` = '{$aRow['medTitle']}',
  73. `Uri` = '{$sUri}',
  74. `Desc` = '{$aRow['medDesc']}',
  75. `Tags` = '{$aRow['medTags']}',
  76. `Date` = '{$aRow['medDate']}',
  77. `Views` = '{$aRow['medViews']}',
  78. `Hash` = '$sHash',
  79. `Status` = '$sStatus'
  80. ";
  81. $iResult = (int)$this->oMigrationModule->_oDb->query($sqlQuery);
  82. if ($iResult <= 0) {
  83. $this->setResultStatus('Database error. Cannot insert new photo to the database.');
  84. return MIGRATION_FAILED;
  85. }
  86. // get last file id;
  87. $iFileId = $this->oMigrationModule->_oDb->lastId();
  88. //transform the file
  89. $this->_exportFile(
  90. array(
  91. 'medID' => $aRow['medID'],
  92. 'medFile' => $aRow['medFile'],
  93. 'medProfId' => $aRow['medProfId'],
  94. 'medExt' => $aRow['medExt'],
  95. 'newID' => $iFileId,
  96. ));
  97. $oTag = new BxDolTags();
  98. $oTag->reparseObjTags($this->sType, $iFileId);
  99. // define album
  100. $sError = $this->_defineAlbum($iFileId, $aRow['medProfId'], $aRow['medTitle']);
  101. if ($sError) {
  102. $this->setResultStatus($sError);
  103. return MIGRATION_FAILED;
  104. }
  105. $sError = $this->_exportFavorites(array('old'=>$aRow['medID'], 'new'=>$iFileId));
  106. if ($sError) {
  107. $this->setResultStatus($sError);
  108. return MIGRATION_FAILED;
  109. }
  110. $sError = $this->_exportVotings(array('old'=>$aRow['medID'], 'new'=>$iFileId));
  111. if ($sError) {
  112. $this->setResultStatus($sError);
  113. return MIGRATION_FAILED;
  114. }
  115. $sError = $this->_exportComments(array('old'=>$aRow['medID'], 'new'=>$iFileId));
  116. if ($sError) {
  117. $this->setResultStatus($sError);
  118. return MIGRATION_FAILED;
  119. }
  120. }
  121. // set as finished;
  122. $this->setResultStatus('All photos were transferred');
  123. return MIGRATION_SUCCESSFUL;
  124. }
  125. /**
  126. * Function will check existing entry in module;
  127. *
  128. * @param : $sEntryUri (string) - entry's uri;
  129. * @return : true if exist;
  130. */
  131. function isEntryExisting($sEntryUri)
  132. {
  133. $sEntryUri = $this -> oMigrationModule -> _oDb -> escape($sEntryUri);
  134. $sqlQuery = "SELECT COUNT(*) FROM `{$this->sType}_main` WHERE `Uri` = '$sEntryUri'";
  135. return $this -> oMigrationModule -> _oDb -> getOne($sqlQuery) ? true : false;
  136. }
  137. /**
  138. * Function returns sizes array of photo;
  139. *
  140. * @param : $sFile (string) - file name;
  141. * @return : array with 'width' and 'height' elements;
  142. */
  143. function getSizeInfo ($sFile) {
  144. $sFile = strip_tags($sFile);
  145. $sFilePath = $this->oMigrationModule->_oDb->getExtraParam('config_root') . $this->sOldFilesPath . $sFile;
  146. if (file_exists($sFilePath)) {
  147. $aInfo = getimagesize($sFilePath);
  148. return array('width' => $aInfo[0], 'height' => $aInfo[1]);
  149. }
  150. else
  151. return $this->aNewSizes['file'];
  152. }
  153. /**
  154. * Function creates album and add new object there;
  155. *
  156. * @param : $iFileId (int) - file id;
  157. * @param : $iOwnerId (int) - file's owner id;
  158. * @param : $sFileName (string) - file name;
  159. * @return : nothing;
  160. */
  161. function _defineAlbum($iFileId, $iOwnerId, $sFileName) {
  162. $sNickName = getNickName($iOwnerId);
  163. $sAlbum = str_replace('{nickname}', $sNickName, $this->sNewAlbumName);
  164. $iFileId = (int) $iFileId;
  165. $iOwnerId = (int) $iOwnerId;
  166. $oAlbums = &new BxDolAlbums($this->sType, $iOwnerId);
  167. $aData = array(
  168. 'caption' => $this->oMigrationModule->_oDb->escape($sAlbum),
  169. 'owner' => $iOwnerId,
  170. 'description' => $this->oMigrationModule->_oDb->escape($sAlbum),
  171. 'location' => _t('_Undefined'),
  172. 'AllowAlbumView' => BX_DOL_PG_ALL,
  173. );
  174. $iAlbumId = (int) $oAlbums->addAlbum($aData);
  175. // check objects;
  176. $sqlQuery = "SELECT COUNT(*) FROM `sys_albums_objects` WHERE `id_album` = {$iAlbumId} AND `id_object` = {$iFileId}";
  177. if (!$this->oMigrationModule->_oDb->getOne($sQuery)) {
  178. $oAlbums->addObject($iAlbumId, $iFileId, true);
  179. }
  180. }
  181. /**
  182. * Function export photo file;
  183. *
  184. * @param : $aFile (array) - array with medID and medExt elements;
  185. * @return : (boolean) - true if was copied;
  186. */
  187. function _exportFile ($aFile = array()) {
  188. $bOpperation = true;
  189. $sFile = $aFile['medID'] . '.' . $aFile['medExt'];
  190. $sSourceFilePath = $this->oMigrationModule->_oDb->getExtraParam('config_root') . $this->sOldFilesPath . $sFile;
  191. $sNewDirectory = BX_DIRECTORY_PATH_ROOT . $this->sNewFilesPath;
  192. if ( file_exists($sSourceFilePath) ) {
  193. if (!copy($sSourceFilePath, $sNewDirectory . $aFile['newID'] . '.' . $aFile['medExt'])) {
  194. return false;
  195. }
  196. foreach ($this->aNewSizes as $aValue) {
  197. $sNewFile = $sNewDirectory . $aFile['newID'] . $aValue['postfix'] . '.jpg';
  198. imageResize($sSourceFilePath, $sNewFile, $aValue['width'], $aValue['height'], true);
  199. }
  200. }
  201. else {
  202. $bOpperation = false;
  203. }
  204. return $bOpperation;
  205. }
  206. /**
  207. * Function export all comments;
  208. *
  209. * @param : $iFileId (integer) - file id;
  210. * @return : (string) - error message or empty;
  211. */
  212. function _exportComments ($aFile) {
  213. $sQuery = "SELECT * FROM `{$this->aOldTables['cmts']}` WHERE `cmt_object_id` = {$aFile['old']}";
  214. $rResult = mysql_query($sQuery, $this->rOldDb);
  215. if ($rResult) {
  216. while ($aRow = mysql_fetch_assoc($rResult)) {
  217. $sText = $this->oMigrationModule->_oDb->escape($aRow['cmt_text']);
  218. $sqlQuery =
  219. "
  220. INSERT INTO
  221. `{$this->sType}_cmts`
  222. SET
  223. `cmt_id` = {$aRow['cmt_id']},
  224. `cmt_parent_id` = {$aRow['cmt_parent_id']},
  225. `cmt_object_id` = {$aFile['new']},
  226. `cmt_author_id` = {$aRow['cmt_author_id']},
  227. `cmt_text` = '{$sText}',
  228. `cmt_rate` = {$aRow['cmt_rate']},
  229. `cmt_rate_count` = {$aRow['cmt_rate_count']},
  230. `cmt_time` = '{$aRow['cmt_time']}',
  231. `cmt_replies` = {$aRow['cmt_replies']}
  232. ";
  233. $this->oMigrationModule->_oDb->query($sqlQuery);
  234. }
  235. }
  236. }
  237. /**
  238. * Function export all votings;
  239. *
  240. * @param : $iFileId (integer);
  241. * @return : (string) - error message or empty;
  242. */
  243. function _exportVotings ($aFile) {
  244. $sqlQuery = "SELECT * FROM `{$this->aOldTables['rate']}` WHERE `gal_id` = {$aFile['old']}";
  245. $rResult = mysql_query($sqlQuery, $this->rOldDb);
  246. if ($rResult) {
  247. while ($aRow = mysql_fetch_assoc($rResult)) {
  248. $sqlQuery =
  249. "
  250. INSERT INTO
  251. `{$this->sType}_rating`
  252. SET
  253. `gal_id` = {$aFile['new']},
  254. `gal_rating_count` = {$aRow['gal_rating_count']},
  255. `gal_rating_sum` = {$aRow['gal_rating_sum']}
  256. ";
  257. $iResult = (int)$this->oMigrationModule->_oDb->query($sqlQuery);
  258. if ($iResult <= 0) {
  259. return 'Database error. Cannot insert votes information in the database.';
  260. }
  261. //export voting tracks;
  262. $sError = $this->_exportVotingTrack($aFile);
  263. if ($sError) {
  264. return $sError;
  265. }
  266. $fRate = $aRow['gal_rating_sum'] / $aRow['gal_rating_count'];
  267. //update primary table;
  268. $sqlQuery =
  269. "
  270. UPDATE
  271. `{$this->sType}_main`
  272. SET
  273. `Rate` = $fRate,
  274. `RateCount` = {$aRow['gal_rating_count']}
  275. WHERE
  276. `ID` = {$aFile['new']}
  277. ";
  278. $iResult = (int)$this->oMigrationModule->_oDb->query($sqlQuery);
  279. if ($iResult <= 0) {
  280. return 'Database error. Cannot update votes information in the database.';
  281. }
  282. }
  283. }
  284. }
  285. /**
  286. * Function export all votings track;
  287. *
  288. * @param : $iGalId (integer);
  289. * @return : (string) - error message or empty;
  290. */
  291. function _exportVotingTrack ($aFile) {
  292. $sqlQuery = "SELECT * FROM `{$this->aOldTables['rate_track']}` WHERE `gal_id` = {$aFile['old']}";
  293. $rResult = mysql_query($sqlQuery, $this->rOldDb);
  294. if ($rResult) {
  295. while ($aRow = mysql_fetch_assoc($rResult)) {
  296. $sqlQuery =
  297. "
  298. INSERT INTO
  299. `{$this->sType}_voting_track`
  300. SET
  301. `gal_id` = {$aFile['new']},
  302. `gal_ip` = '{$aRow['gal_ip']}',
  303. `gal_date` = '{$aRow['gal_date']}'
  304. ";
  305. $iResult = (int)$this->oMigrationModule->_oDb->query($sqlQuery);
  306. if ($iResult <= 0) {
  307. return 'Database error. Cannot insert votes track information to the database.';
  308. }
  309. }
  310. }
  311. }
  312. /**
  313. * Function export all favorites records;
  314. *
  315. * @param : $iFileId (integer);
  316. * @return : (string) - error message or empty;
  317. */
  318. function _exportFavorites ($aFile) {
  319. $sqlQuery = "SELECT `medID`, `userID`, UNIX_TIMESTAMP(`favDate`) as `favDate` FROM `{$this->aOldTables['favorite']}` WHERE `medID` = {$aFile['old']}";
  320. $rResult = mysql_query($sqlQuery, $this->rOldDb);
  321. while ($aRow = mysql_fetch_assoc($rResult)) {
  322. $sqlQuery =
  323. "
  324. INSERT INTO
  325. `{$this->sType}_favorites`
  326. SET
  327. `ID` = {$aFile['new']},
  328. `Profile` = '{$aRow['userID']}',
  329. `Date` = '{$aRow['gal_date']}'
  330. ";
  331. $iResult = (int)$this->oMigrationModule->_oDb->query($sqlQuery);
  332. if ($iResult <= 0) {
  333. return 'Database error. Cannot insert favorites information to the database.';
  334. }
  335. }
  336. }
  337. }
  338. ?>