moduleinstaller.class.inc.php 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. <?php
  2. // Copyright (C) 2010-2012 Combodo SARL
  3. //
  4. // This file is part of iTop.
  5. //
  6. // iTop is free software; you can redistribute it and/or modify
  7. // it under the terms of the GNU Affero General Public License as published by
  8. // the Free Software Foundation, either version 3 of the License, or
  9. // (at your option) any later version.
  10. //
  11. // iTop is distributed in the hope that it will be useful,
  12. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. // GNU Affero General Public License for more details.
  15. //
  16. // You should have received a copy of the GNU Affero General Public License
  17. // along with iTop. If not, see <http://www.gnu.org/licenses/>
  18. require_once(APPROOT.'setup/setuppage.class.inc.php');
  19. /**
  20. * Class ModuleInstaller
  21. * Defines the API to implement module specific actions during the setup
  22. *
  23. * @copyright Copyright (C) 2010-2012 Combodo SARL
  24. * @license http://opensource.org/licenses/AGPL-3.0
  25. */
  26. abstract class ModuleInstallerAPI
  27. {
  28. public static function BeforeWritingConfig(Config $oConfiguration)
  29. {
  30. return $oConfiguration;
  31. }
  32. /**
  33. * Handler called before creating or upgrading the database schema
  34. * @param $oConfiguration Config The new configuration of the application
  35. * @param $sPreviousVersion string Previous version number of the module (empty string in case of first install)
  36. * @param $sCurrentVersion string Current version number of the module
  37. */
  38. public static function BeforeDatabaseCreation(Config $oConfiguration, $sPreviousVersion, $sCurrentVersion)
  39. {
  40. }
  41. /**
  42. * Handler called after the creation/update of the database schema
  43. * @param $oConfiguration Config The new configuration of the application
  44. * @param $sPreviousVersion string Previous version number of the module (empty string in case of first install)
  45. * @param $sCurrentVersion string Current version number of the module
  46. */
  47. public static function AfterDatabaseCreation(Config $oConfiguration, $sPreviousVersion, $sCurrentVersion)
  48. {
  49. }
  50. /**
  51. * Helper to complete the renaming of a class
  52. * The renaming is made in the datamodel definition, but the name has to be changed in the DB as well
  53. * Must be called after DB update, i.e within an implementation of AfterDatabaseCreation()
  54. *
  55. * @param string $sFrom Original name (already INVALID in the current datamodel)
  56. * @param string $sTo New name (valid in the current datamodel)
  57. * @return void
  58. */
  59. public static function RenameClassInDB($sFrom, $sTo)
  60. {
  61. if (!MetaModel::IsStandaloneClass($sTo))
  62. {
  63. $sRootClass = MetaModel::GetRootClass($sTo);
  64. $sTableName = MetaModel::DBGetTable($sRootClass);
  65. $sFinalClassCol = MetaModel::DBGetClassField($sRootClass);
  66. $sRepair = "UPDATE `$sTableName` SET `$sFinalClassCol` = '$sTo' WHERE `$sFinalClassCol` = BINARY '$sFrom'";
  67. CMDBSource::Query($sRepair);
  68. $iAffectedRows = CMDBSource::AffectedRows();
  69. SetupPage::log_info("Renaming class in DB - final class from '$sFrom' to '$sTo': $iAffectedRows rows affected");
  70. }
  71. }
  72. /**
  73. * Helper to modify an enum value
  74. * The change is made in the datamodel definition, but the value has to be changed in the DB as well
  75. * Must be called BEFORE DB update, i.e within an implementation of BeforeDatabaseCreation()
  76. *
  77. * @param string $sClass A valid class name
  78. * @param string $sAttCode The enum attribute code
  79. * @param string $sFrom Original value (already INVALID in the current datamodel)
  80. * @param string $sTo New value (valid in the current datamodel)
  81. * @return void
  82. */
  83. public static function RenameEnumValueInDB($sClass, $sAttCode, $sFrom, $sTo)
  84. {
  85. $sOriginClass = MetaModel::GetAttributeOrigin($sClass, $sAttCode);
  86. $sTableName = MetaModel::DBGetTable($sOriginClass);
  87. $oAttDef = MetaModel::GetAttributeDef($sOriginClass, $sAttCode);
  88. if ($oAttDef instanceof AttributeEnum)
  89. {
  90. $oValDef = $oAttDef->GetValuesDef();
  91. if ($oValDef)
  92. {
  93. $aNewValues = array_keys($oValDef->GetValues(array(), ""));
  94. if (in_array($sTo, $aNewValues))
  95. {
  96. $aAllValues = $aNewValues;
  97. $aAllValues[] = $sFrom;
  98. if (!in_array($sFrom, $aNewValues))
  99. {
  100. $sEnumCol = $oAttDef->Get("sql");
  101. $sNullSpec = $oAttDef->IsNullAllowed() ? 'NULL' : 'NOT NULL';
  102. if (strtolower($sTo) == strtolower($sFrom))
  103. {
  104. SetupPage::log_info("Changing enum in DB - $sClass::$sAttCode from '$sFrom' to '$sTo' (just a change in the case)");
  105. $sColumnDefinition = "ENUM(".implode(",", CMDBSource::Quote($aNewValues)).") $sNullSpec";
  106. $sRepair = "ALTER TABLE `$sTableName` MODIFY `$sEnumCol` $sColumnDefinition";
  107. CMDBSource::Query($sRepair);
  108. }
  109. else
  110. {
  111. // 1st - Allow both values in the column definition
  112. //
  113. SetupPage::log_info("Changing enum in DB - $sClass::$sAttCode from '$sFrom' to '$sTo'");
  114. $sColumnDefinition = "ENUM(".implode(",", CMDBSource::Quote($aAllValues)).") $sNullSpec";
  115. $sRepair = "ALTER TABLE `$sTableName` MODIFY `$sEnumCol` $sColumnDefinition";
  116. CMDBSource::Query($sRepair);
  117. // 2nd - Change the old value into the new value
  118. //
  119. $sRepair = "UPDATE `$sTableName` SET `$sEnumCol` = '$sTo' WHERE `$sEnumCol` = BINARY '$sFrom'";
  120. CMDBSource::Query($sRepair);
  121. $iAffectedRows = CMDBSource::AffectedRows();
  122. SetupPage::log_info("Changing enum in DB - $iAffectedRows rows updated");
  123. // 3rd - Remove the useless value from the column definition
  124. //
  125. $sColumnDefinition = "ENUM(".implode(",", CMDBSource::Quote($aNewValues)).") $sNullSpec";
  126. $sRepair = "ALTER TABLE `$sTableName` MODIFY `$sEnumCol` $sColumnDefinition";
  127. CMDBSource::Query($sRepair);
  128. SetupPage::log_info("Changing enum in DB - removed useless value '$sFrom'");
  129. }
  130. }
  131. else
  132. {
  133. SetupPage::log_warning("Changing enum in DB - $sClass::$sAttCode - '$sFrom' is still a valid value (".implode(', ', $aNewValues).")");
  134. }
  135. }
  136. else
  137. {
  138. SetupPage::log_warning("Changing enum in DB - $sClass::$sAttCode - '$sTo' is not a known value (".implode(', ', $aNewValues).")");
  139. }
  140. }
  141. }
  142. }
  143. }
  144. ?>