Changeset 12706
- Timestamp:
- Aug 20, 2009, 7:28:15 PM (13 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
OpenPNE3/branches/fukamachi/db-convert-from-2.x/lib/task/openpneUpgradeFrom2Task.class.php
r12555 r12706 26 26 protected function execute($arguments = array(), $options = array()) 27 27 { 28 $databaseManager = new sfDatabaseManager($this->configuration); 29 $connection = $databaseManager->getDatabase($options['connection'] ? $options['connection'] : null)->getConnection(); 28 // install needed plugins 29 $pluginInstall = new opPluginInstallTask($this->dispatcher, $this->formatter); 30 $pluginInstall->run(array('opDiaryPlugin'), array('--stability=beta')); 31 $pluginInstall->run(array('opCommunityTopicPlugin'), array('--stability=beta')); 32 //$pluginInstall->run(array('opMessagePlugin'), array('--stability=beta')); 33 //$pluginInstall->run(array('opAshiatoPlugin'), array('--stability=beta')); 34 35 // install OpenPNE3 36 $openpneInstall = new openpneInstallTask($this->dispatcher, $this->formatter); 37 $openpneInstall->run(); 38 39 // ask OpenPNE2 database 40 while( 41 !($dbname2 = $this->ask('Type old (OpenPNE2) database name')) 42 ); 43 44 // get database configuration 45 $file = sfConfig::get('sf_config_dir').'/databases.yml'; 46 $config = array(); 47 if (file_exists($file)) 48 { 49 $config = sfYaml::load($file); 50 } 51 $dsn = $config['all']['doctrine']['param']['dsn']; 52 $dsn2 = preg_replace('/dbname=[^ ;]+/', 'dbname='.$dbname2, $dsn); 53 $username = $config['all']['doctrine']['param']['username']; 54 $password = $config['all']['doctrine']['param']['password']; 55 56 // create temporary directory 57 $tmpdir = sfConfig::get('sf_cache_dir').'/models_tmp'; 58 $this->getFilesystem()->mkdirs($tmpdir); 59 60 // configure Doctrine 61 $manager = Doctrine_Manager::getInstance(); 62 $manager->setAttribute(Doctrine::ATTR_QUERY_CLASS, 'Doctrine_Query'); 63 64 // generate OpenPNE2 models 65 $conn = Doctrine_Manager::connection(new PDO($dsn2, $username, $password), 'OpenPNE2'); 66 Doctrine::generateModelsFromDb($tmpdir, array('OpenPNE2'), array('generateBaseClasses' => false, 'baseClassName' => 'sfDoctrineRecord')); 67 68 // generate OpenPNE3 models 69 $this->generateTemporaryBaseModels($tmpdir, $this->getBaseModels()); 70 71 // load temporary models 72 Doctrine::loadModels($tmpdir, Doctrine::MODEL_LOADING_AGGRESSIVE); 73 74 // TODO:convert every tables 75 76 // delete models' cache 77 $this->clearCache(); 78 } 79 80 protected function clearCache() 81 { 82 $cc = new sfCacheClearTask($this->dispatcher, $this->formatter); 83 $cc->run(); 84 } 85 86 protected function getBaseModelsDirectories($dir) 87 { 88 if (!is_dir($dir) || '.' === basename($dir)) 89 { 90 return; 91 } 92 elseif ('base' === basename($dir)) 93 { 94 return array($dir); 95 } 96 97 $baseDirs = array(); 98 foreach (scandir($dir) as $file) 99 { 100 if (0 === strpos($file, '.')) 101 { 102 continue; 103 } 104 if ($base = $this->getBaseModelsDirectories($dir.'/'.$file)) 105 { 106 $baseDirs = array_merge($baseDirs, $base); 107 } 108 } 109 110 return $baseDirs; 111 } 112 113 protected function getBaseModels() 114 { 115 $baseDirs = $this->getBaseModelsDirectories(sfConfig::get('sf_lib_dir').'/model/doctrine'); 116 $baseModels = array(); 117 foreach ($baseDirs as $dir) 118 { 119 foreach (scandir($dir) as $file) 120 { 121 if (!is_dir($file) && 0 !== strpos($file, '.')) 122 { 123 array_push($baseModels, $dir.'/'.$file); 124 } 125 } 126 } 127 128 return $baseModels; 129 } 130 131 protected function generateTemporaryBaseModels($tmpdir, $baseModels = array()) 132 { 133 foreach ($baseModels as $model) 134 { 135 $className = basename($model, '.class.php'); 136 $className = preg_replace('/^Base/', 'Temp', $className); 137 $contents = file_get_contents($model); 138 $contents 139 = preg_replace( 140 '/\nabstract class Base.+? extends opDoctrineRecord\n/', 141 "\nclass ".$className." extends sfDoctrineRecord\n", 142 $contents 143 ); 144 145 $fp = fopen($tmpdir.'/'.$className.'.php', 'w'); 146 fwrite($fp, $contents); 147 fclose($fp); 148 } 30 149 } 31 150 }
Note: See TracChangeset
for help on using the changeset viewer.