Last active
December 17, 2015 18:58
-
-
Save bcremer/5656517 to your computer and use it in GitHub Desktop.
Dumps a given shopware4 plugin (subscribes, forms, elements) into a dbdeploy compatible delta-file. Usage:
php dump_plugin.php HttpCache > insert.sql
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| if (file_exists('../config.php')) { | |
| $config = include '../config.php'; | |
| } elseif (file_exists('../engine/Shopware/Configs/Custom.php')) { | |
| $config = include '../engine/Shopware/Configs/Custom.php'; | |
| } else { | |
| die('Could not find config'); | |
| } | |
| $dbConfig = $config['db']; | |
| try { | |
| $conn = new PDO('mysql:host=' . $dbConfig['host'] . ';dbname=' . $dbConfig['dbname'], $dbConfig['username'], $dbConfig['password']); | |
| $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); | |
| $conn->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_ASSOC); | |
| } catch(PDOException $e) { | |
| echo 'ERROR: ' . $e->getMessage(); | |
| exit(1); | |
| } | |
| if (!isset($argv[1]) || empty($argv[1])) { | |
| echo "Usage: \n"; | |
| echo "php " . $argv[0] . " PluginName\n"; | |
| exit(1); | |
| } | |
| try { | |
| $dumper = new PluginDumper($conn); | |
| $dumper->dumpPlugin($argv[1]); | |
| } catch (\Exception $e) { | |
| echo $e->getMessage() . "\n"; | |
| exit(1); | |
| } | |
| /** | |
| * Class update | |
| */ | |
| class PluginDumper | |
| { | |
| /** | |
| * @var PDO | |
| */ | |
| protected $conn; | |
| /** | |
| * @param PDO $conn | |
| */ | |
| public function __construct(\PDO $conn) | |
| { | |
| $this->conn = $conn; | |
| } | |
| /** | |
| * @param string $pluginName | |
| * @throws Exception | |
| */ | |
| public function dumpPlugin($pluginName) | |
| { | |
| $pluginSql = "SELECT * FROM s_core_plugins WHERE name LIKE :name"; | |
| $pluginStmt = $this->conn->prepare($pluginSql); | |
| $pluginStmt->execute(array(':name' => $pluginName)); | |
| $plugin = $pluginStmt->fetch(\PDO::FETCH_ASSOC); | |
| if (!$plugin) { | |
| throw new \Exception(sprintf('Plugin by name "%s" not found', $pluginName)); | |
| } | |
| $pluginId = $plugin['id']; | |
| unset($plugin['id']); | |
| echo '-- //' . "\n\n"; | |
| echo $this->insertRow('s_core_plugins', $plugin) . "\n"; | |
| echo sprintf("SET @plugin_id = (SELECT id FROM %s WHERE name='%s');\n", 's_core_plugins', $pluginName) . "\n"; | |
| $subscribesSql = "SELECT * FROM `s_core_subscribes` WHERE pluginID = :pluginId"; | |
| $subscribesStmt = $this->conn->prepare($subscribesSql); | |
| $subscribesStmt->execute(array(':pluginId' => $pluginId)); | |
| $subscibes = $subscribesStmt->fetchAll(\PDO::FETCH_ASSOC); | |
| foreach ($subscibes as $subscriber) { | |
| unset($subscriber['id']); | |
| $subscriber['pluginID'] = '@plugin_id'; | |
| echo $this->insertRow('s_core_subscribes', $subscriber); | |
| } | |
| echo "\n" . 'SET @parent_form_id = (SELECT id FROM `s_core_config_forms` WHERE `name` LIKE "Core");' . "\n\n"; | |
| $formsSql = "SELECT * FROM `s_core_config_forms` WHERE plugin_id = :pluginId"; | |
| $formsStmt = $this->conn->prepare($formsSql); | |
| $formsStmt->execute(array(':pluginId' => $pluginId)); | |
| $form = $formsStmt->fetch(\PDO::FETCH_ASSOC); | |
| $formId = $form['id']; | |
| unset($form['id']); | |
| $form['plugin_id'] = '@plugin_id'; | |
| $form['parent_id'] = '@parent_form_id'; | |
| echo "\n" . $this->insertRow('s_core_config_forms', $form) . "\n"; | |
| echo 'SET @form_id = (SELECT id FROM `s_core_config_forms` WHERE plugin_id = @plugin_id);' . "\n\n"; | |
| $elementsSql = " | |
| SELECT e . * , v.value AS customValue | |
| FROM `s_core_config_elements` e | |
| LEFT JOIN s_core_config_values v ON v.element_id = e.id AND v.shop_id = 1 | |
| WHERE form_id = :formId | |
| "; | |
| $elementsStmt = $this->conn->prepare($elementsSql); | |
| $elementsStmt->execute(array(':formId' => $formId)); | |
| $elements = $elementsStmt->fetchAll(\PDO::FETCH_ASSOC); | |
| foreach ($elements as $element) { | |
| unset($element['id']); | |
| $element['form_id'] = '@form_id'; | |
| if ($element['customValue'] !== null) { | |
| $element['value'] = $element['customValue']; | |
| } | |
| unset($element['customValue']); | |
| echo $this->insertRow('s_core_config_elements', $element); | |
| } | |
| echo "\n\n" . '-- //@UNDO' . "\n\n"; | |
| echo '-- //' . "\n\n"; | |
| } | |
| /** | |
| * @param string $tableName | |
| * @param array $values | |
| * @return string | |
| */ | |
| public function insertRow($tableName, $values) | |
| { | |
| $mapFields = function ($value) { | |
| return '`' . $value . '`'; | |
| }; | |
| $mapValues = function ($value) { | |
| if ($value === null) { | |
| return 'NULL'; | |
| } | |
| if ($value[0] == '@') { | |
| return $value; | |
| } | |
| return $this->conn->quote($value); | |
| }; | |
| $fields = array_map($mapFields, array_keys($values)); | |
| $fields = implode(', ', $fields); | |
| $values = array_map($mapValues, $values); | |
| $values = implode(', ', $values); | |
| $sql = "INSERT IGNORE INTO `%s` (%s) VALUES (%s);\n"; | |
| $sql = sprintf($sql, $tableName, $fields, $values); | |
| return $sql; | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment