From 361617b3620acfc3cf025520e6bddd872c87cccd Mon Sep 17 00:00:00 2001 From: Mzhelskiy Maxim Date: Wed, 11 Apr 2012 21:19:56 +0400 Subject: [PATCH] =?UTF-8?q?=D0=94=D0=BE=D1=80=D0=B0=D0=B1=D0=BE=D1=82?= =?UTF-8?q?=D0=BA=D0=B0=20=D0=BA=D0=BE=D0=BD=D0=B2=D0=B5=D1=80=D1=82=D0=B5?= =?UTF-8?q?=D1=80=D0=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- install/convert_0.5.1_to_1.0.sql | 7 +- install/geo_base.sql | 2 - install/index.php | 119 ++++++++++++++++++++++++++++++- 3 files changed, 124 insertions(+), 4 deletions(-) diff --git a/install/convert_0.5.1_to_1.0.sql b/install/convert_0.5.1_to_1.0.sql index b3903195..53b2c63d 100644 --- a/install/convert_0.5.1_to_1.0.sql +++ b/install/convert_0.5.1_to_1.0.sql @@ -92,4 +92,9 @@ ALTER TABLE `prefix_talk` ADD `talk_user_id_last` INT NOT NULL AFTER `talk_date_ ADD INDEX ( `talk_user_id_last` ); ALTER TABLE `prefix_talk` ADD `talk_comment_id_last` INT NULL DEFAULT NULL AFTER `talk_user_ip`; -ALTER TABLE `prefix_talk_user` ADD INDEX ( `comment_count_new` ); \ No newline at end of file +ALTER TABLE `prefix_talk_user` ADD INDEX ( `comment_count_new` ); + +DROP TABLE `prefix_country_user`; +DROP TABLE `prefix_country`; +DROP TABLE `prefix_city_user`; +DROP TABLE `prefix_city`; \ No newline at end of file diff --git a/install/geo_base.sql b/install/geo_base.sql index 95689cad..4aa61560 100644 --- a/install/geo_base.sql +++ b/install/geo_base.sql @@ -19534,8 +19534,6 @@ CREATE TABLE IF NOT EXISTS `prefix_geo_target` ( -- Дамп данных таблицы `prefix_geo_target` -- -INSERT INTO `prefix_geo_target` (`geo_type`, `geo_id`, `target_type`, `target_id`, `country_id`, `region_id`, `city_id`) VALUES -('region', 1040, 'user', 1, 149, 1040, NULL); -- -- Ограничения внешнего ключа сохраненных таблиц diff --git a/install/index.php b/install/index.php index 9b51c226..a1e67cfe 100644 --- a/install/index.php +++ b/install/index.php @@ -1334,7 +1334,7 @@ class Install { /** * Обновляем значения */ - $sQuery3="UPDATE {$sTable1} SET talk_user_id_last='{$iUserLast}', talk_comment_id_last={$iCommentLast} WHERE talk_id='{$iTalk}' "; + $sQuery3="UPDATE {$sTable1} SET talk_user_id_last='{$iUserLast}', talk_comment_id_last=".($iCommentLast ? $iCommentLast : 'null')." WHERE talk_id='{$iTalk}' "; if(!mysql_query($sQuery3)) { $aErrors[] = mysql_error(); break; @@ -1345,6 +1345,123 @@ class Install { } $iPage++; } while (1); + /** + * Перенос стран и городов на новую структуру + */ + $sTableUser=$aParams['prefix'].'user'; + $sTableGeoCountry=$aParams['prefix'].'geo_country'; + $sTableGeoCity=$aParams['prefix'].'geo_city'; + $sTableGeoRegion=$aParams['prefix'].'geo_region'; + $sTableGeoTarget=$aParams['prefix'].'geo_target'; + $iPage=1; + do { + $iLimitStart=($iPage-1)*100; + $sQuery="SELECT * FROM {$sTableUser} WHERE `user_profile_country` IS NOT NULL and `user_profile_country`<>'' LIMIT {$iLimitStart},100"; + if(!$aResults = mysql_query($sQuery)){ + $aErrors[] = mysql_error(); + break; + } + if (mysql_num_rows($aResults)) { + while($aRow = mysql_fetch_assoc($aResults)) { + /** + * Обрабатываем каждого пользователя + */ + $iUserId=$aRow['user_id']; + $sCountry=mysql_real_escape_string($aRow['user_profile_country']); + $sCity=mysql_real_escape_string((string)$aRow['user_profile_city']); + /** + * Ищем страну в гео-базе + */ + $sQuery2="SELECT id, name_ru FROM {$sTableGeoCountry} WHERE name_ru='{$sCountry}' or name_en='{$sCountry}' LIMIT 0,1"; + if(!$aResults2 = mysql_query($sQuery2)){ + $aErrors[] = mysql_error(); + break; + } + if($aRow2 = mysql_fetch_assoc($aResults2)) { + $iCountryId=$aRow2['id']; + $sCountryName=mysql_real_escape_string($aRow2['name_ru']); + } else { + $sQuery2="UPDATE {$sTableUser} SET user_profile_country=null, user_profile_region=null, user_profile_city=null WHERE user_id={$iUserId} "; + if(!$aResults2 = mysql_query($sQuery2)){ + $aErrors[] = mysql_error(); + } + break; + } + /** + * Ищем город в гео-базе + */ + $iCityId=null; + $sCityName=null; + $iRegionId=null; + $sRegionName=null; + if ($sCity) { + $sQuery2="SELECT id, region_id, name_ru FROM {$sTableGeoCity} WHERE country_id='{$iCountryId}' and (name_ru='{$sCity}' or name_en='{$sCity}') LIMIT 0,1"; + if(!$aResults2 = mysql_query($sQuery2)){ + $aErrors[] = mysql_error(); + break; + } + if($aRow2 = mysql_fetch_assoc($aResults2)) { + $iCityId=$aRow2['id']; + $sCityName=mysql_real_escape_string($aRow2['name_ru']); + $iRegionId=$aRow2['region_id']; + /** + * Получаем название региона + */ + $sQuery3="SELECT name_ru FROM {$sTableGeoRegion} WHERE id='{$iRegionId}' LIMIT 0,1"; + if(!$aResults3 = mysql_query($sQuery3)){ + $aErrors[] = mysql_error(); + break; + } + if($aRow3 = mysql_fetch_assoc($aResults3)) { + $sRegionName=mysql_real_escape_string($aRow3['name_ru']); + } else { + break; + } + } + } + /** + * Добавляем связь пользователя с гео-объектом + */ + $iGeoId=$iCountryId; + $sGeoType='country'; + if ($iCityId) { + $iGeoId=$iCityId; + $sGeoType='city'; + } + /** + * Проверяем отсутствие связи + */ + $sQuery2="SELECT * FROM {$sTableGeoTarget} WHERE target_type='user' and target_id='{$iUserId}' LIMIT 0,1"; + if(!$aResults2 = mysql_query($sQuery2)){ + $aErrors[] = mysql_error(); + break; + } + if($aRow2 = mysql_fetch_assoc($aResults2)) { + // пропускаем этого пользователя + break; + } + /** + * Создаем новую связь + */ + $sQuery2="INSERT INTO {$sTableGeoTarget} SET geo_type='{$sGeoType}', geo_id='{$iGeoId}', target_type='user', target_id='{$iUserId}', country_id=".($iCountryId ? $iCountryId : 'null').", region_id=".($iRegionId ? $iRegionId : 'null')." , city_id=".($iCityId ? $iCityId : 'null')." "; + if(!$aResults2 = mysql_query($sQuery2)){ + $aErrors[] = mysql_error(); + break; + } + /** + * Обновляем информацию о пользователе + */ + $sQuery2="UPDATE {$sTableUser} SET user_profile_country=".($iCountryId ? "'$sCountryName'" : 'null').", user_profile_region=".($sRegionName ? "'$sRegionName'" : 'null').", user_profile_city=".($sCityName ? "'$sCityName'" : 'null')." WHERE user_id={$iUserId} "; + if(!$aResults2 = mysql_query($sQuery2)){ + $aErrors[] = mysql_error(); + break; + } + } + } else { + break; + } + $iPage++; + } while (1); if(count($aErrors)==0) {