N°4569 - Fix deletion of light-grey theme for iTop 2.7 and older

This commit is contained in:
Eric Espie
2022-01-26 10:00:48 +01:00
parent aa20289dfe
commit 468de06fe1
12 changed files with 620 additions and 9 deletions

View File

@@ -17,6 +17,8 @@
* You should have received a copy of the GNU Affero General Public License
*/
use Combodo\iTop\DesignDocument;
/**
* Utility to upgrade the format of a given XML datamodel to the latest version
@@ -109,7 +111,9 @@ class iTopDesignFormat
*/
protected $aLog;
protected $bStatus;
protected $bKeepObsoleteNodes;
protected $sKeepVersion;
/**
* Creation from a loaded DOMDocument
* @param DOMDocument $oDocument The document to transform
@@ -210,6 +214,20 @@ class iTopDesignFormat
return self::GetItopNodePath($oNode->parentNode).'/'.$sNodeDesc;
}
/**
* Compute a real xpath from iTop one
*
* @param \Combodo\iTop\DesignElement $oNode
*
* @return string
*/
public static function GetNodeXPath($oNode)
{
$sITopXPath = DesignDocument::GetItopNodePath($oNode);
return preg_replace(["@\[@", "@]@"], ["[@id=\"", "\"]"], $sITopXPath);
}
/**
* Test the conversion without altering the DOM
*
@@ -227,19 +245,21 @@ class iTopDesignFormat
}
/**
* Make adjustements to the DOM to migrate it to the specified version (default is latest)
* Make adjustments to the DOM to migrate it to the specified version (default is latest)
* For now only the conversion from version 1.0 to 1.1 is supported.
*
* @param string $sTargetVersion The desired version (or the latest possible version if not specified)
* @param object $oFactory Full data model (not yet used, aimed at allowing conversion that could not be performed without knowing the
* @param \ModelFactory|null $oFactory Full data model (not yet used, aimed at allowing conversion that could not be performed without knowing the
* whole data model)
* @param bool $bKeepObsoleteNodes
*
* @return bool True on success, False if errors have been encountered (still the DOM may be altered!)
*/
public function Convert($sTargetVersion = ITOP_DESIGN_LATEST_VERSION, $oFactory = null)
public function Convert($sTargetVersion = ITOP_DESIGN_LATEST_VERSION, $oFactory = null, $bKeepObsoleteNodes = true)
{
$this->aLog = array();
$this->bStatus = true;
$this->bKeepObsoleteNodes = $bKeepObsoleteNodes;
$oXPath = new DOMXPath($this->oDocument);
// Retrieve the version number
@@ -304,6 +324,7 @@ class iTopDesignFormat
$sIntermediate = self::$aVersions[$sFrom]['next'];
$sTransform = self::$aVersions[$sFrom]['go_to_next'];
$this->LogInfo("Upgrading from $sFrom to $sIntermediate ($sTransform)");
$this->sKeepVersion = $sFrom;
}
else
{
@@ -311,12 +332,16 @@ class iTopDesignFormat
$sIntermediate = self::$aVersions[$sFrom]['previous'];
$sTransform = self::$aVersions[$sFrom]['go_to_previous'];
$this->LogInfo("Downgrading from $sFrom to $sIntermediate ($sTransform)");
$this->sKeepVersion = null;
}
// Transform to the intermediate format
$aCallSpec = array($this, $sTransform);
try
{
call_user_func($aCallSpec, $oFactory);
if ($iFrom > $iTo && $this->bKeepObsoleteNodes) {
$this->RestorePreviousNodes($sIntermediate);
}
// Recurse
$this->DoConvert($sIntermediate, $sTo, $oFactory);
@@ -325,7 +350,6 @@ class iTopDesignFormat
{
$this->LogError($e->getMessage());
}
return;
}
/**
@@ -947,13 +971,78 @@ class iTopDesignFormat
}
}
}
/**
* @param string $sPath
* @param string $sNodeMetaVersion
*
* @return void
*/
private function RemoveNodeFromXPath($sPath)
private function RestorePreviousNodes($sNodeMetaVersion)
{
$oXPath = new DOMXPath($this->oDocument);
$oTrashedNodes = $oXPath->query("/itop_design/meta/previous_versions/previous_version[@id='$sNodeMetaVersion']/trashed_nodes/trashed_node");
foreach ($oTrashedNodes as $oTrashedNode) {
if ($oTrashedNode->nodeType == XML_ELEMENT_NODE) {
$oXPathNode = $oXPath->query('parent_xpath', $oTrashedNode)->item(0);
$oNodeTreeNode = $oXPath->query('node_tree', $oTrashedNode)->item(0);
if (!is_null($oXPathNode) && !is_null($oNodeTreeNode)) {
$sXPath = $this->GetText($oXPathNode, '');
$oParentNode = $oXPath->query($sXPath)->item(0);
if ($oParentNode) {
$oNode = $oNodeTreeNode->firstChild;
while ($oNode) {
$oNextNode = $oNode->nextSibling;
if ($oNode->nodeType == XML_ELEMENT_NODE) {
// Restore the modification flags
$oModifiedNodeList = $oXPath->query('descendant-or-self::*[@_disabled_delta or @_disabled_rename_from]', $oNode);
foreach ($oModifiedNodeList as $oModifiedNode) {
foreach (['_delta', '_rename_from'] as $sModificationFlag) {
$sCurrentFlag = $oNode->getAttribute('_disabled'.$sModificationFlag);
if (!empty($sCurrentFlag)) {
$oModifiedNode->setAttribute($sModificationFlag, $sCurrentFlag);
$oModifiedNode->removeAttribute('_disabled'.$sModificationFlag);
}
}
}
// Move the node back in place
$oParentNode->appendChild($oNode);
}
$oNode = $oNextNode;
}
}
}
}
}
// Clean up the mess
$this->RemoveNodeFromXPath("/itop_design/meta/previous_versions/previous_version[@id='$sNodeMetaVersion']", false);
$this->RemoveEmptyNodeFromXPath("/itop_design/meta/previous_versions");
$this->RemoveEmptyNodeFromXPath("/itop_design/meta");
}
private function RemoveEmptyNodeFromXPath($sXPath, $bStoreThisNodeInMetaVersion = false)
{
$oXPath = new DOMXPath($this->oDocument);
$oNodeToRemove = $oXPath->query($sXPath)->item(0);
if (is_null($oNodeToRemove)) {
return;
}
$oNode = $oNodeToRemove->firstChild;
while ($oNode) {
if ($oNode->nodeType == XML_ELEMENT_NODE) {
return;
}
$oNode = $oNode->nextSibling;
}
$this->RemoveNodeFromXPath($sXPath, $bStoreThisNodeInMetaVersion);
}
/**
* @param string $sPath
* @param bool $bStoreThisNodeInMetaVersion
*
* @return void
*/
private function RemoveNodeFromXPath($sPath, $bStoreThisNodeInMetaVersion = true)
{
$oXPath = new DOMXPath($this->oDocument);
@@ -961,10 +1050,68 @@ class iTopDesignFormat
foreach ($oNodeList as $oNode)
{
$this->LogWarning('Node '.self::GetItopNodePath($oNode).' is irrelevant in this version, it will be removed.');
$this->DeleteNode($oNode);
if ($bStoreThisNodeInMetaVersion && $this->bKeepObsoleteNodes && $this->sKeepVersion) {
// Move the node to <Meta> to keep it safe for backward migration
$oItopDesignNode = $this->GetOrCreateNode('/itop_design', 'itop_design', null);
$oMetaNode = $this->GetOrCreateNode('meta', 'meta', $oItopDesignNode);
$oPreviousVersionsNode = $this->GetOrCreateNode('previous_versions', 'previous_versions', $oMetaNode);
$oPreviousVersionNode = $this->GetOrCreateNode("previous_version[@id='$this->sKeepVersion']", 'previous_version', $oPreviousVersionsNode);
$oPreviousVersionNode->setAttribute('id', $this->sKeepVersion);
$oPreviousVersionNode->setAttribute('_delta', 'define');
$oTrashedNodeList = $this->GetOrCreateNode('trashed_nodes', 'trashed_nodes', $oPreviousVersionNode);
$iMaxIndex = 0;
$oTrashedNodes = $oXPath->query('trashed_node', $oTrashedNodeList);
foreach ($oTrashedNodes as $oTrashedNode) {
if ($oTrashedNode->nodeType == XML_ELEMENT_NODE) {
$iId = $oTrashedNode->getAttribute('id');
if ($iId > $iMaxIndex) {
$iMaxIndex = $iId;
}
}
}
$iNextId = $iMaxIndex + 1;
$oTrashedNode = $this->GetOrCreateNode("trashed_node[@id='$iNextId']", 'trashed_node', $oTrashedNodeList);
$oTrashedNode->setAttribute('id', $iNextId);
$oXPathNode = $this->GetOrCreateNode('parent_xpath', 'parent_xpath', $oTrashedNode);
$oParentNode = $oNode->parentNode;
if ($oParentNode instanceof DOMElement) {
$sParentXPath = static::GetNodeXPath($oParentNode);
$oXPathNode->appendChild(new DOMText($sParentXPath));
}
$oNodeTreeNode = $this->GetOrCreateNode('node_tree', 'node_tree', $oTrashedNode);
// Store the modification flags
$oModifiedNodeList = $oXPath->query('descendant-or-self::*[@_delta or @_rename_from]', $oNode);
foreach ($oModifiedNodeList as $oModifiedNode) {
foreach (['_delta', '_rename_from'] as $sModificationFlag) {
$sCurrentFlag = $oNode->getAttribute($sModificationFlag);
if (!empty($sCurrentFlag)) {
$oModifiedNode->setAttribute('_disabled'.$sModificationFlag, $sCurrentFlag);
$oModifiedNode->removeAttribute($sModificationFlag);
}
}
}
$oNodeTreeNode->appendChild($oNode);
} else {
$this->DeleteNode($oNode);
}
}
}
private function GetOrCreateNode($sXPath, $sName, $oRootNode)
{
$oXPath = new DOMXPath($this->oDocument);
$oNode = $oXPath->query($sXPath, $oRootNode)->item(0);
if (is_null($oNode)) {
$oNode = $oRootNode->ownerDocument->createElement($sName);
$oRootNode->appendChild($oNode);
}
return $oNode;
}
/**
* Clean a collection node by removing the _delta="define" on it and moving it to the item nodes.
*
@@ -1151,4 +1298,31 @@ class iTopDesignFormat
return null;
}
/**
* Returns the TEXT of the current node (possibly from several child nodes)
* @param null $sDefault
* @return null|string
*/
protected function GetText($oNode, $sDefault = null)
{
$sText = null;
foreach($oNode->childNodes as $oChildNode)
{
if ($oChildNode instanceof \DOMText)
{
if (is_null($sText)) $sText = '';
$sText .= $oChildNode->wholeText;
}
}
if (is_null($sText))
{
return $sDefault;
}
else
{
return $sText;
}
}
}