/** * CSVファイルを書きだす * * @param string $configKeyName * @param string $path * @return boolean * @access protected */ function _writeCsv($configKeyName, $plugin, $path, $exclude = array()) { $pluginTables = array(); if ($plugin != 'core') { $pluginPath = BcUtil::getSchemaPath($plugin); $Folder = new Folder($pluginPath); $files = $Folder->read(true, true, false); $pluginTables = $files[1]; foreach ($pluginTables as $key => $pluginTable) { if (preg_match('/^(.+)\\.php$/', $pluginTable, $matches)) { $pluginTables[$key] = $matches[1]; } else { unset($pluginTables[$key]); } } } $pluginKey = Inflector::underscore($plugin); $db = ConnectionManager::getDataSource($configKeyName); $db->cacheSources = false; $tables = $db->listSources(); $result = true; foreach ($tables as $table) { if (preg_match("/^" . $db->config['prefix'] . "([^_].+)\$/", $table, $matches) && !preg_match("/^" . Configure::read('BcEnv.pluginDbPrefix') . "[^_].+\$/", $matches[1])) { $table = $matches[1]; if (in_array($table, $exclude)) { continue; } if ($pluginKey != 'core') { // プラグインの場合は対象プラグイン名が先頭にない場合スキップ //if (!preg_match("/^" . $pluginKey . "_([^_].+)$/", $table)) { if (!in_array($table, $pluginTables)) { // メールプラグインの場合、先頭に、「mail_」 がなくとも 末尾にmessagesがあれば対象とする if ($pluginKey != 'mail') { continue; } elseif (!preg_match("/messages\$/", $table)) { continue; } } } if (!$db->writeCsv(array('path' => $path . $table . '.csv', 'encoding' => 'SJIS', 'init' => false, 'plugin' => $plugin == 'core' ? null : $plugin))) { $result = false; } } } return $result; }
/** * データベースをプラグインインストール前の状態に戻す * * @param string $plugin プラグイン名 * @return bool */ public function resetDb($plugin) { $path = BcUtil::getSchemaPath($plugin); if (!$path) { return true; } $baserDb = ConnectionManager::getDataSource('baser'); $baserDb->cacheSources = false; $baserListSources = $baserDb->listSources(); $baserPrefix = $baserDb->config['prefix']; $pluginDb = ConnectionManager::getDataSource('plugin'); $pluginDb->cacheSources = false; $pluginListSources = $pluginDb->listSources(); $pluginPrefix = $pluginDb->config['prefix']; $Folder = new Folder($path); $files = $Folder->read(true, true); if (empty($files[1])) { return true; } $tmpdir = TMP . 'schemas' . DS; $result = true; foreach ($files[1] as $file) { $oldSchemaPath = ''; if (preg_match('/^(.*?)\\.php$/', $file, $matches)) { $type = 'drop'; $table = $matches[1]; $File = new File($path . DS . $file); $data = $File->read(); if (preg_match('/(public|var)\\s+\\$connection\\s+=\\s+\'([a-z]+?)\';/', $data, $matches)) { $conType = $matches[2]; $listSources = ${$conType . 'ListSources'}; $prefix = ${$conType . 'Prefix'}; } else { continue; } $schemaPath = $tmpdir; if (preg_match('/^create_(.*?)\\.php$/', $file, $matches)) { $type = 'drop'; $table = $matches[1]; if (!in_array($prefix . $table, $listSources)) { continue; } copy($path . DS . $file, $tmpdir . $table . '.php'); } elseif (preg_match('/^alter_(.*?)\\.php$/', $file, $matches)) { $type = 'alter'; $table = $matches[1]; if (!in_array($prefix . $table, $listSources)) { continue; } $corePlugins = implode('|', Configure::read('BcApp.corePlugins')); if (preg_match('/^(' . $corePlugins . ')/', Inflector::camelize($table), $matches)) { $pluginName = $matches[1]; } $File = new File($path . DS . $file); $data = $File->read(); $data = preg_replace('/class\\s+' . Inflector::camelize($table) . 'Schema/', 'class Alter' . Inflector::camelize($table) . 'Schema', $data); $oldSchemaPath = $tmpdir . $file; $File = new File($oldSchemaPath); $File->write($data); if ($conType == 'baser') { $schemaPath = BcUtil::getSchemaPath() . DS; } else { $schemaPath = BcUtil::getSchemaPath($pluginName) . DS; } } elseif (preg_match('/^drop_(.*?)\\.php$/', $file, $matches)) { $type = 'create'; $table = $matches[1]; if (in_array($prefix . $table, $listSources)) { continue; } copy($path . DS . $file, $tmpdir . $table . '.php'); } else { if (!in_array($prefix . $table, $listSources)) { continue; } copy($path . DS . $file, $tmpdir . $table . '.php'); } if ($conType == 'baser') { $db = $baserDb; } else { $db = $pluginDb; } if (!$db->loadSchema(array('type' => $type, 'path' => $schemaPath, 'file' => $table . '.php', 'dropField' => true, 'oldSchemaPath' => $oldSchemaPath))) { $result = false; } @unlink($tmpdir . $table . '.php'); if (file_exists($oldSchemaPath)) { unlink($oldSchemaPath); } } } return $result; }
/** * データベースを初期化 * * 既に存在するテーブルは上書きしない * * @param array データベース設定名 * @param string プラグイン名 * @return boolean */ public function initDb($dbConfigName, $pluginName = '', $loadCsv = true, $filterTable = '', $filterType = '') { // 初期データフォルダを走査 if (!$pluginName) { $path = BASER_CONFIGS . 'Schema'; } else { $path = BcUtil::getSchemaPath($pluginName); if (!$path) { return true; } } if ($this->loadSchema($dbConfigName, $path, $filterTable, $filterType, array(), $dropField = false)) { if ($loadCsv) { $path = BcUtil::getDefaultDataPath($pluginName); if ($path) { return $this->loadCsv($dbConfigName, $path); } else { return true; } } else { return true; } } else { return false; } }
/** * テーブルをリセットする * * @param type $dbConfigKeyName * @param type $dbConfig * @return boolean */ public function resetTables($dbConfigKeyName = 'baser', $dbConfig = null, $plugin = 'core', $excludes = array()) { $db = $this->_getDataSource($dbConfigKeyName, $dbConfig); $dbConfig = $db->config; $db->reconnect(); $sources = $db->listSources(); $result = true; $pluginTables = array(); if ($plugin != 'core') { $path = BcUtil::getSchemaPath($plugin); $Folder = new Folder($path); $files = $Folder->read(true, true, false); if (empty($files[1])) { return true; } foreach ($files[1] as $file) { if (preg_match('/\\.php$/', $file)) { $pluginTables[] = preg_replace('/\\.php/', '', $file); } } } foreach ($sources as $source) { if (preg_match("/^" . $dbConfig['prefix'] . "([^_].+)\$/", $source, $matches)) { $table = $matches[1]; if ($plugin == 'core') { if (preg_match("/^" . Configure::read('BcEnv.pluginDbPrefix') . "/", $table)) { continue; } } else { // プラグインの場合は対象プラグイン名が先頭にない場合スキップ if (!in_array($table, $pluginTables)) { // メールプラグインの場合、先頭に、「mail_」 がなくとも 末尾にmessagesがあれば対象とする if ($plugin != 'Mail') { continue; } elseif (!preg_match("/messages\$/", $table)) { continue; } } } if (!in_array($table, $excludes)) { if (!$db->truncate($table)) { $result = false; } } } } return $result; }
/** * プラグインファイルを削除する * * @param string $pluginName プラグイン名 * @return void */ private function __deletePluginFile($pluginName) { $paths = App::path('Plugin'); foreach ($paths as $path) { $pluginPath = $path . $pluginName; if (is_dir($pluginPath)) { break; } } $tmpPath = TMP . 'schemas' . DS . 'uninstall' . DS; $folder = new Folder(); $folder->delete($tmpPath); $folder->create($tmpPath); // インストール用スキーマをdropスキーマとして一時フォルダに移動 $path = BcUtil::getSchemaPath($pluginName); $folder = new Folder($path); $files = $folder->read(true, true); if (is_array($files[1])) { foreach ($files[1] as $file) { if (preg_match('/\\.php$/', $file)) { $from = $path . DS . $file; $to = $tmpPath . 'drop_' . $file; copy($from, $to); chmod($to, 0666); } } } // テーブルを削除 $this->Plugin->loadSchema('plugin', $tmpPath); // プラグインフォルダを削除 $folder->delete($pluginPath); // 一時フォルダを削除 $folder->delete($tmpPath); }
/** * スキーマ情報のパスを取得する */ public function testGetSchemaPath() { // Core $result = BcUtil::getSchemaPath(); $this->assertEquals(BASER_CONFIGS . 'Schema', $result, 'Coreのスキーマ情報のパスを正しく取得できません'); // Blog $result = BcUtil::getSchemaPath('Blog'); $this->assertEquals(BASER_PLUGINS . 'Blog/Config/Schema', $result, 'プラグインのスキーマ情報のパスを正しく取得できません'); }
/** * CSVファイルを書きだす * * @param string $configKeyName * @param string $path * @return boolean */ function _writeCsv($plugin, $path, $exclude = array()) { $pluginTables = array(); if ($plugin != 'core') { $pluginPath = BcUtil::getSchemaPath($plugin); $Folder = new Folder($pluginPath); $files = $Folder->read(true, true, false); $pluginTables = $files[1]; foreach ($pluginTables as $key => $pluginTable) { if (preg_match('/^(.+)\\.php$/', $pluginTable, $matches)) { $pluginTables[$key] = $matches[1]; } else { unset($pluginTables[$key]); } } } $pluginKey = Inflector::underscore($plugin); $db = ConnectionManager::getDataSource('default'); $db->cacheSources = false; $tables = $db->listSources(); $tableList = getTableList(); $result = true; foreach ($tables as $table) { if ($plugin == 'core' && in_array($table, $tableList['core']) || $plugin != 'core' && in_array($table, $tableList['plugin'])) { $table = str_replace($db->config['prefix'], '', $table); if (in_array($table, $exclude)) { continue; } if ($pluginKey != 'core' && !in_array($table, $pluginTables)) { continue; } if (!$db->writeCsv(array('path' => $path . $table . '.csv', 'encoding' => 'SJIS', 'init' => false, 'plugin' => $plugin == 'core' ? null : $plugin))) { $result = false; } } } return $result; }
/** * データベースを初期化 * * 既に存在するテーブルは上書きしない * * @param array データベース設定名 * @param string プラグイン名 * @return boolean */ public function initDb($pluginName = '', $options = []) { $options = array_merge(['loadCsv' => true, 'filterTable' => '', 'filterType' => '', 'dbDataPattern' => ''], $options); // 初期データフォルダを走査 if (!$pluginName) { $path = BASER_CONFIGS . 'Schema'; } else { $path = BcUtil::getSchemaPath($pluginName); if (!$path) { return true; } } $dbDataPattern = null; if (!empty($options['dbDataPattern'])) { $dbDataPattern = $options['dbDataPattern']; } elseif (!empty($_SESSION['dbDataPattern'])) { $dbDataPattern = $_SESSION['dbDataPattern']; unset($_SESSION['dbDataPattern']); } if ($this->loadSchema('default', $path, $options['filterTable'], $options['filterType'], [], $dropField = false)) { if ($options['loadCsv']) { $theme = $pattern = null; if ($dbDataPattern) { list($theme, $pattern) = explode('.', $dbDataPattern); } $path = BcUtil::getDefaultDataPath($pluginName, $theme, $pattern); if ($path) { return $this->loadCsv('default', $path); } else { return true; } } else { return true; } } else { return false; } }
/** * テーブルをリセットする * * @param type $dbConfigKeyName * @param type $dbConfig * @return boolean */ public function resetTables($dbConfigKeyName = 'default', $dbConfig = null, $plugin = 'core', $excludes = array()) { $db = $this->_getDataSource($dbConfigKeyName, $dbConfig); $dbConfig = $db->config; $db->reconnect(); $sources = $db->listSources(); $result = true; $pluginTables = array(); if ($plugin != 'core') { $path = BcUtil::getSchemaPath($plugin); $Folder = new Folder($path); $files = $Folder->read(true, true, false); if (empty($files[1])) { return true; } foreach ($files[1] as $file) { if (preg_match('/\\.php$/', $file)) { $pluginTables[] = preg_replace('/\\.php/', '', $file); } } } foreach ($sources as $source) { if (preg_match("/^" . $dbConfig['prefix'] . "([^_].+)\$/", $source, $matches)) { $table = $matches[1]; if ($plugin == 'core') { if (in_array($table, $pluginTables)) { continue; } } else { if (!in_array($table, $pluginTables)) { continue; } } if (!in_array($table, $excludes)) { if (!$db->truncate($table)) { $result = false; } } } } return $result; }