From 36d0127b9ecc3f3f95e382d6a9919685884593ce Mon Sep 17 00:00:00 2001 From: Marcel Werk Date: Tue, 19 Jul 2011 18:42:59 +0200 Subject: [PATCH] sql pip renamed --- com.woltlab.wcf/pip.xml | 2 +- .../SQLPackageInstallationPlugin.class.php | 143 ++++++++++++++++++ 2 files changed, 144 insertions(+), 1 deletion(-) create mode 100644 wcfsetup/install/files/lib/system/package/plugin/SQLPackageInstallationPlugin.class.php diff --git a/com.woltlab.wcf/pip.xml b/com.woltlab.wcf/pip.xml index c67580cc8e..9e55af9098 100644 --- a/com.woltlab.wcf/pip.xml +++ b/com.woltlab.wcf/pip.xml @@ -12,7 +12,7 @@ wcf\system\package\plugin\OptionsPackageInstallationPlugin wcf\system\package\plugin\PageMenuPackageInstallationPlugin wcf\system\package\plugin\ScriptPackageInstallationPlugin - wcf\system\package\plugin\SqlPackageInstallationPlugin + wcf\system\package\plugin\SQLPackageInstallationPlugin wcf\system\package\plugin\TemplateListenerPackageInstallationPlugin wcf\system\package\plugin\TemplatesPackageInstallationPlugin wcf\system\package\plugin\UserOptionsPackageInstallationPlugin diff --git a/wcfsetup/install/files/lib/system/package/plugin/SQLPackageInstallationPlugin.class.php b/wcfsetup/install/files/lib/system/package/plugin/SQLPackageInstallationPlugin.class.php new file mode 100644 index 0000000000..9d29b86826 --- /dev/null +++ b/wcfsetup/install/files/lib/system/package/plugin/SQLPackageInstallationPlugin.class.php @@ -0,0 +1,143 @@ + + * @package com.woltlab.wcf + * @subpackage system.package.plugin + * @category Community Framework + */ +class SQLPackageInstallationPlugin extends AbstractPackageInstallationPlugin { + /** + * @see wcf\system\package\plugin\AbstractPackageInstallationPlugin::$tableName + */ + public $tableName = 'package_installation_sql_log'; + + /** + * @see wcf\system\package\plugin\PackageInstallationPlugin::install() + */ + public function install() { + parent::install(); + + // extract sql file from archive + if ($queries = $this->getSQL($this->instruction['value'])) { + $package = $this->installation->getPackage(); + if ($package->parentPackageID) { + // package is a plugin; get parent package + $package = $package->getParentPackage(); + } + + if ($package->standalone == 1) { + // package is standalone + $packageAbbr = $package->getAbbreviation(); + $tablePrefix = WCF_N.'_'.$package->instanceNo.'_'; + + // Replace the variable xyz1_1 with $tablePrefix in the table names. + $queries = StringUtil::replace($packageAbbr.'1_1_', $packageAbbr.$tablePrefix, $queries); + } + + // replace wcf1_ with the actual WCF_N value + $queries = str_replace("wcf1_", "wcf".WCF_N."_", $queries); + + // check queries + $parser = new PackageInstallationSQLParser($queries, $package, $this->installation->getAction()); + $conflicts = $parser->test(); + if (count($conflicts)) { + // ask user here + // search default value in session + if (!WCF::getSession()->getVar('overrideAndDontAskAgain')) { + // show page + if (!empty($_POST['override']) || !empty($_POST['overrideAndDontAskAgain'])) { + if (!empty($_POST['overrideAndDontAskAgain'])) { + WCF::getSession()->register('overrideAndDontAskAgain', true); + WCF::getSession()->update(); + } + } + else { + WCF::getTPL()->assign('conflicts', $conflicts); + WCF::getTPL()->display('packageInstallationCheckOverrideTables'); + exit; + } + } + } + + // execute queries + $parser->execute(); + + // log changes + $parser->log(); + } + } + + /** + * Deletes the sql tables or columns which where installed by the package. + */ + public function uninstall() { + // get logged sql tables/columns + $sql = "SELECT * + FROM wcf".WCF_N."_package_installation_sql_log + WHERE packageID = ?"; + $statement = WCF::getDB()->prepareStatement($sql); + $statement->execute(array($this->installation->getPackageID())); + $entries = array(); + while ($row = $statement->fetchArray()) { + $entries[] = $row; + } + + // get all tablenames from database + $existingTableNames = WCF::getDB()->getEditor()->getTablenames(); + + // delete or alter tables + foreach ($entries as $entry) { + // don't alter table if it should be dropped + if (!empty($entry['sqlColumn']) || !empty($entry['sqlIndex'])) { + $isDropped = false; + foreach ($entries as $entry2) { + if ($entry['sqlTable'] == $entry2['sqlTable'] && empty($entry2['sqlColumn']) && empty($entry2['sqlIndex'])) { + $isDropped = true; + } + } + if ($isDropped) continue; + } + // drop table + if (!empty($entry['sqlTable']) && empty($entry['sqlColumn']) && empty($entry['sqlIndex'])) { + WCF::getDB()->getEditor()->dropTable($entry['sqlTable']); + } + // drop column + else if (in_array($entry['sqlTable'], $existingTableNames) && !empty($entry['sqlColumn'])) { + WCF::getDB()->getEditor()->dropColumn($entry['sqlTable'], $entry['sqlColumn']); + } + // drop index + else if (in_array($entry['sqlTable'], $existingTableNames) && !empty($entry['sqlIndex'])) { + WCF::getDB()->getEditor()->dropIndex($entry['sqlTable'], $entry['sqlIndex']); + } + } + // delete from log table + parent::uninstall(); + } + + /** + * Extracts and returns the sql file. + * If the specified sql file was not found, an error message is thrown. + * + * @param string $filename + * @return string + */ + protected function getSQL($filename) { + // search sql files in package archive + if (($fileindex = $this->installation->getArchive()->getTar()->getIndexByFilename($filename)) === false) { + throw new SystemException("SQL file '".$filename."' not found.", 13016); + } + + // extract sql file to string + return $this->installation->getArchive()->getTar()->extractToString($fileindex); + } +} -- 2.20.1