Changeset 12840
- Timestamp:
- Sep 7, 2009, 11:06:39 AM (13 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
OpenPNE3/branches/fukamachi/db-convert-from-2.x/lib/task/openpneUpgradeFrom2Task.class.php
r12837 r12840 18 18 private $date = '0000-00-00 00:00:00'; 19 19 private $mapCategoryIds = array(); 20 private $mapFileNameToId = array(); 20 21 private $birthdayProfileId = null; 21 22 private $validatorFile = null; 22 23 private $unreadDiaryIds = array(); 24 private $memberIds = array(); 23 25 24 26 private $memoryBuf = 0; … … 39 41 protected function execute($arguments = array(), $options = array()) 40 42 { 41 ini_set('memory_limit', ' 512M');43 ini_set('memory_limit', '1024M'); 42 44 43 45 // install needed plugins … … 97 99 98 100 // clear db records 99 $tables = array('Blacklist', /*'File', 'FileBin',*/'CommunityCategory', 'Community', 'CommunityConfig', 'CommunityMember', 'Member', 'MemberConfig', 'MemberImage', 'Profile', 'ProfileTranslation', 'ProfileOption', 'ProfileOptionTranslation', 'MemberProfile', 'MemberRelationship', 'CommunityTopic', 'CommunityEvent', 'Diary', 'DiaryImage', 'DiaryComment', 'DiaryCommentImage', 'DiaryCommentUnread', /*'Message', 'MessageSendList', 'DeletedMessage', 'MessageFile'*/);101 $tables = array('Blacklist', 'File', 'FileBin', 'CommunityCategory', 'Community', 'CommunityConfig', 'CommunityMember', 'Member', 'MemberConfig', 'MemberImage', 'Profile', 'ProfileTranslation', 'ProfileOption', 'ProfileOptionTranslation', 'MemberProfile', 'MemberRelationship', 'CommunityTopic', 'CommunityEvent', 'Diary', 'DiaryImage', 'DiaryComment', 'DiaryCommentImage', 'DiaryCommentUnread', /*'Message', 'MessageSendList', 'DeletedMessage', 'MessageFile'*/); 100 102 101 103 foreach ($tables as $table) … … 109 111 print "converting Blacklist : ".memory_get_usage()."\n"; 110 112 $this->convertBlacklist(); 111 //print "converting File and FileBin : ".memory_get_usage()."\n";112 //$this->convertFileAndFileBin();113 print "converting File and FileBin : ".memory_get_usage()."\n"; 114 $this->convertFileAndFileBin(); 113 115 print "converting CommunityCategory : ".memory_get_usage()."\n"; 114 116 $this->convertCommunityCategory(); … … 152 154 { 153 155 $this->connectTo2(); 154 $cAdminUser = Doctrine::getTable('CAdminUser')->find(1);155 $this->connectTo3(); 156 $adminUser = Doctrine::getTable('AdminUser')->find(1);156 $cAdminUser = $this->selectOne('CAdminUser', 1); 157 $this->connectTo3(); 158 $adminUser = $this->selectOne('AdminUser', 1); 157 159 158 160 $this->update($adminUser, array( … … 196 198 { 197 199 $this->_createFile(array( 198 'id' => $cFile->getCFileId(), 199 'name' => $cFile->getFilename(), 200 'original_name' => $cFile->getOriginalFilename(), 201 'date' => $cFile->getRDatetime(), 202 'bin' => $cFile->getBin(), 200 'id' => $cFile->getCFileId(), 201 'name' => $cFile->getFilename(), 202 'original_filename' => $cFile->getOriginalFilename(), 203 'created_at' => $cFile->getRDatetime(), 204 'updated_at' => $cFile->getRDatetime(), 205 'bin' => $cFile->getBin(), 206 )); 207 208 if ($maxFileId < $cFile->getCFileId()) 209 { 210 $maxFileId = $cFile->getCFileId(); 211 } 212 213 $cFile->free(); 214 } 215 $offset += 100; 216 } 217 218 $offset = 0; 219 while ($cImages = $this->getModelsIterator('CImage', 100, $offset)) 220 { 221 foreach ($cImages as $cImage) 222 { 223 $this->_createFile(array( 224 'id' => $maxFileId + $cImage->getCImageId(), 225 'name' => $cImage->getFilename(), 226 'created_at' => $cImage->getRDatetime(), 227 'updated_at' => $cImage->getRDatetime(), 228 'bin' => $cImage->getBin(), 203 229 )); 204 230 205 if ($maxFileId < $cFile->getCFileId())206 {207 $maxFileId = $cFile->getCFileId();208 }209 210 $cFile->free();211 }212 $offset += 100;213 }214 215 $offset = 0;216 while ($cImages = $this->getModelsIterator('CImage', 100, $offset))217 {218 foreach ($cImages as $cImage)219 {220 $this->_createFile(array(221 'id' => $maxFileId + $cImage->getCImageId(),222 'name' => $cImage->getFilename(),223 'date' => $cImage->getRDatetime(),224 'bin' => $cImage->getBin(),225 ));226 227 231 $cImage->free(); 228 232 } … … 234 238 { 235 239 // NOTICE: ignore if the file with same name had saved 236 if ($this-> getFileByName($param['name']))240 if ($this->isExist('File', array('name' => $param['name']))) 237 241 { 238 242 return; … … 243 247 $file = $this->insertInto('File', $paramFile, true); 244 248 249 $param['file_id'] = $param['id']; 245 250 unset($param['name']); 246 unset($param['original_name']); 251 unset($param['original_filename']); 252 unset($param['id']); 247 253 $fileBin = $this->insertInto('FileBin', $param, true); 248 254 … … 330 336 'id' => $cCommu->getCCommuId(), 331 337 'name' => $cCommu->getName(), 338 'file_id' => $this->getFileIdByName($cCommu->getImageFilename()), 332 339 'created_at' => $cCommu->getRDate(), 333 340 'updated_at' => $cCommu->getUDatetime(), 334 341 ); 335 if ($file = $this->getFileByName($cCommu->getImageFilename()))336 {337 $param['file_id'] = $file->getId();338 $file->free();339 }340 342 if (array_key_exists($cCommu->getCCommuCategoryId(), $this->mapCategoryIds)) 341 343 { … … 380 382 { 381 383 // NOTICE: ignore if the community this member joins is not exist 382 if (!$this->isExist('Community', array('id' => $cCommuMember->getCCommuId())))384 if (!$this->isExist('Community', $cCommuMember->getCCommuId())) 383 385 { 384 386 $cCommuMember->free(); … … 396 398 if ( 397 399 $this->isExist('CCommu', array( 398 'c_commu_id' => $cCommuMember->getCCommuId(),400 'c_commu_id' => $cCommuMember->getCCommuId(), 399 401 'c_member_id_admin' => $cCommuMember->getCCommuMemberId() 400 402 )) … … 428 430 'is_active' => !$cMember->getIsLoginRejected(), 429 431 ); 430 if ($cMember->getCMemberIdInvite() && Doctrine::getTable('Member')->find($cMember->getCMemberIdInvite()))432 if ($cMember->getCMemberIdInvite() && in_array($cMember->getCMemberIdInvite(), $this->memberIds)) 431 433 { 432 434 $param['invite_member_id'] = $cMember->getCMemberIdInvite(); … … 434 436 435 437 $this->insertInto('Member', $param); 438 $this->memberIds[] = $cMember->getCMemberId(); 436 439 $cMember->free(); 437 440 } … … 450 453 { 451 454 // NOTICE: ignore if the member isn't exist 452 $this->connectTo2(); 453 if (!$this->isExist('CMember', array('c_member_id' => $cMemberSecure->getCMemberId()))) 455 if (!in_array($cMemberSecure->getCMemberId(), $this->memberIds)) 454 456 { 455 457 $cMemberSecure->free(); 456 458 continue; 457 459 } 458 $this->connectTo3();459 460 460 461 // common parameters … … 496 497 $getImageFilename = 'getImageFilename_'.$i; 497 498 498 if ($file = $this->getFileByName($cMember->$getImageFilename()))499 if ($fileId = $this->getFileIdByName($cMember->$getImageFilename())) 499 500 { 500 501 $this->insertInto('MemberImage', array( 501 502 'member_id' => $cMember->getCMemberId(), 502 'file_id' => $file ->getId(),503 'file_id' => $fileId, 503 504 'image_filename' => $cMember->$getImageFilename(), 504 505 'is_primary' => $cMember->getImageFilename() === $cMember->$getImageFilename(), … … 506 507 'updated_at' => $this->date, 507 508 )); 508 $file->free();509 509 } 510 510 } … … 651 651 { 652 652 // NOTICE: ignore if the member isn't exist 653 if (! $this->isExist('Member', array('id' => $cMemberProfile->getCMemberId())))653 if (!in_array($cMemberProfile->getCMemberId(), $this->memberIds)) 654 654 { 655 655 $cMemberProfile->free(); … … 835 835 'updated_at' => $cCommuTopic->getUDatetime(), 836 836 ); 837 if ( Doctrine::getTable('Member')->find($cCommuTopic->getCMemberId()))837 if (!in_array($cCommuTopic->getCMemberId(), $this->memberIds)) 838 838 { 839 839 $param['member_id'] = $cCommuTopic->getCMemberId(); … … 896 896 'updated_at' => $cCommuEvent->getUDatetime(), 897 897 ); 898 if ( Doctrine::getTable('Member')->find($cCommuEvent->getCMemberId()))898 if (!in_array($cCommuEvent->getCMemberId(), $this->memberIds)) 899 899 { 900 900 $param['member_id'] = $cCommuEvent->getCMemberId(); … … 932 932 { 933 933 $this->connectTo2(); 934 $pref = Doctrine::getTable('CProfilePref')->find($cCommuEvent->getOpenPrefId())->getPref();934 $pref = $this->selectOne('CProfilePref', $cCommuEvent->getOpenPrefId())->getPref(); 935 935 $this->connectTo3(); 936 936 if ($cCommuEvent->getOpenPrefComment()) … … 956 956 foreach ($cDiaries as $cDiary) 957 957 { 958 $this->connectTo2();959 958 // NOTICE: ignore if the member isn't exist 960 if (! $this->isExist('CMember', array('c_member_id' => $cDiary->getCMemberId())))959 if (!in_array($cDiary->getCMemberId(), $this->memberIds)) 961 960 { 962 961 $cDiary->free(); 963 962 continue; 964 963 } 965 $this->connectTo3();966 964 967 965 $diary = $this->insertInto('Diary', array( … … 978 976 { 979 977 $getImageFilename = 'getImageFilename_'.$i; 980 if ($file = $this->getFileByName($cDiary->$getImageFilename()))978 if ($fileId = $this->getFileIdByName($cDiary->$getImageFilename())) 981 979 { 982 980 $diary->setHasImages(true); … … 984 982 'diary_id' => $diary->getId(), 985 983 'number' => $i, 986 'file_id' => $file ->getId(),984 'file_id' => $fileId, 987 985 )); 988 $file->free();989 986 } 990 987 } … … 992 989 if (method_exists($diary, 'getIsChecked') && !$diary->getIsChecked()) 993 990 { 994 array_push($this->unreadDiaryIds, $diary->getId());991 $this->unreadDiaryIds[] = $diary->getId(); 995 992 } 996 993 … … 1015 1012 { 1016 1013 // NOTICE: ignore if that's diary isn't exist 1017 if (! Doctrine::getTable('Diary')->find($cDiaryComment->getCDiaryId()))1014 if (!$this->isExist('Diary', $cDiaryComment->getCDiaryId())) 1018 1015 { 1019 1016 continue; … … 1021 1018 1022 1019 // NOTICE: ignore if that's member isn't exist 1023 if (! Doctrine::getTable('Member')->find($cDiaryComment->getCMemberId()))1020 if (!in_array($cDiaryComment->getCMemberId(), $this->memberIds)) 1024 1021 { 1025 1022 continue; … … 1039 1036 { 1040 1037 $getImageFilename = 'getImageFilename_'.$i; 1041 if ($file = $this->getFileByName($cDiaryComment->$getImageFilename()))1038 if ($fileId = $this->getFileIdByName($cDiaryComment->$getImageFilename())) 1042 1039 { 1043 1040 $this->insertInto('DiaryCommentImage', array( 1044 1041 'diary_comment_id' => $cDiaryComment->getCDiaryCommentId(), 1045 'file_id' => $file ->getId(),1042 'file_id' => $fileId, 1046 1043 )); 1047 $file->free();1048 1044 } 1049 1045 } 1050 1046 1051 if ( array_search($cDiaryComment->getCDiaryCommentId(), $this->unreadDiaryIds))1047 if (in_array($cDiaryComment->getCDiaryCommentId(), $this->unreadDiaryIds)) 1052 1048 { 1053 1049 $this->insertInto('DiaryCommentUnread', array( … … 1126 1122 { 1127 1123 $getImageFilename = 'getImageFilename_'.$i; 1128 if ($file = $this->getFileByName($cMessage->$getImageFilename()))1124 if ($fileId = $this->getFileIdByName($cMessage->$getImageFilename())) 1129 1125 { 1130 $this->insertInto('MessageFile', array_merge($param, array('file_id' => $file->getId()))); 1131 $file->free(); 1126 $this->insertInto('MessageFile', array_merge($param, array('file_id' => $fileId))); 1132 1127 } 1133 1128 } 1134 if ($file = $this->getFileByName($cMessage->getFilename())) 1135 { 1136 $this->insertInto('MessageFile', array_merge($param, array('file_id' => $file->getId()))); 1137 $file->free(); 1129 if ($fileId = $this->getFileIdByName($cMessage->getFilename())) 1130 { 1131 $this->insertInto('MessageFile', array_merge($param, array('file_id' => $fileId))); 1138 1132 } 1139 1133 … … 1143 1137 foreach ($cMessages as $cMessage) 1144 1138 { 1145 $message = Doctrine::getTable('Message')->find($cMessage->getCMessageId());1139 $message = $this->selectOne('Message', $cMessage->getCMessageId()); 1146 1140 $message->setThreadMessageId($this->_getThreadMessageId($cMessage->getCMessageId())); 1147 1141 $message->save(); … … 1157 1151 if (!$messageId) 1158 1152 { 1159 return ;1160 } 1161 1162 $message = Doctrine::getTable('Message')->find($messageId);1153 return null; 1154 } 1155 1156 $message = $this->selectOne('Message', $messageId); 1163 1157 $returnMessageId = $message->getReturnMessageId(); 1164 1158 $message->free(); … … 1188 1182 } 1189 1183 1190 protected function getFileByName($name = null) 1191 { 1192 if ($name) 1193 { 1194 return $this->selectOne('File', array('name' => $name)); 1195 } 1184 protected function getFileIdByName($name = null) 1185 { 1186 if (!$name) 1187 { 1188 return null; 1189 } 1190 1191 if (!isset($this->mapFileNameToId[$name])) 1192 { 1193 $file = Doctrine::getTable('File')->createQuery() 1194 ->select('id') 1195 ->where('name = ?', $name) 1196 ->fetchOne(); 1197 1198 if ($file) 1199 { 1200 $this->mapFileNameToId[$name] = $file->getId(); 1201 } 1202 } 1203 1204 return $this->mapFileNameToId[$name]; 1196 1205 } 1197 1206 … … 1268 1277 if (!is_dir($file) && 0 !== strpos($file, '.')) 1269 1278 { 1270 array_push($baseModels, $dir.'/'.$file);1279 $baseModels[] = $dir.'/'.$file; 1271 1280 } 1272 1281 } … … 1301 1310 protected function getValidatorFile() 1302 1311 { 1303 if ( $this->validatorFile === null)1312 if (is_null($this->validatorFile)) 1304 1313 { 1305 1314 $this->validatorFile = new sfValidatorFile(); … … 1362 1371 * SQL like operations 1363 1372 */ 1364 protected function selectOne($tableName, $where = array()) 1365 { 1366 $q = Doctrine::getTable($tableName)->createQuery(); 1367 1368 foreach ($where as $key => $value) 1369 { 1370 $q->andWhere($key.' = ?', $value); 1371 } 1372 1373 return $q->fetchOne(); 1374 } 1375 1376 protected function isExist($tableName, $where = array()) 1373 protected function selectOne($tableName, $where) 1374 { 1375 if (is_array($where)) 1376 { 1377 $q = Doctrine::getTable($tableName)->createQuery(); 1378 1379 foreach ($where as $key => $value) 1380 { 1381 $q->andWhere($key.' = ?', $value); 1382 } 1383 1384 return $q->fetchOne(); 1385 } 1386 1387 return Doctrine::getTable($tableName)->find($where); 1388 } 1389 1390 protected function isExist($tableName, $where) 1377 1391 { 1378 1392 return (boolean) $this->selectOne($tableName, $where); … … 1392 1406 { 1393 1407 $methodName = $this->makeCamelCase('set'.$field); 1394 $record->{$this->makeCamelCase('set '.$field)}($value);1408 $record->{$this->makeCamelCase('set_'.$field)}($value); 1395 1409 } 1396 1410
Note: See TracChangeset
for help on using the changeset viewer.