1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467 |
- <?php
- // Copyright (C) 2010-2017 Combodo SARL
- //
- // This file is part of iTop.
- //
- // iTop is free software; you can redistribute it and/or modify
- // it under the terms of the GNU Affero General Public License as published by
- // the Free Software Foundation, either version 3 of the License, or
- // (at your option) any later version.
- //
- // iTop is distributed in the hope that it will be useful,
- // but WITHOUT ANY WARRANTY; without even the implied warranty of
- // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- // GNU Affero General Public License for more details.
- //
- // You should have received a copy of the GNU Affero General Public License
- // along with iTop. If not, see <http://www.gnu.org/licenses/>
- /**
- * ModelFactory: in-memory manipulation of the XML MetaModel
- *
- * @copyright Copyright (C) 2010-2017 Combodo SARL
- * @license http://opensource.org/licenses/AGPL-3.0
- */
- require_once(APPROOT.'setup/moduleinstaller.class.inc.php');
- require_once(APPROOT.'setup/itopdesignformat.class.inc.php');
- require_once(APPROOT.'core/designdocument.class.inc.php');
- /**
- * Special exception type thrown when the XML stacking fails
- *
- */
- class MFException extends Exception
- {
- /**
- * @var integer
- */
- protected $iSourceLineNumber;
- /**
- * @var string
- */
- protected $sXPath;
- /**
- * @var string
- */
- protected $sExtraInfo;
-
- const COULD_NOT_BE_ADDED = 1;
- const COULD_NOT_BE_DELETED = 2;
- const COULD_NOT_BE_MODIFIED_NOT_FOUND = 3;
- const COULD_NOT_BE_MODIFIED_ALREADY_DELETED = 4;
- const INVALID_DELTA = 5;
- const ALREADY_DELETED = 6;
- const NOT_FOUND = 7;
- const PARENT_NOT_FOUND = 8;
-
- public function __construct ($message = null, $code = null, $iSourceLineNumber = 0, $sXPath = '', $sExtraInfo = '', $previous = null)
- {
- parent::__construct($message, $code, $previous);
- $this->iSourceLineNumber = $iSourceLineNumber;
- $this->sXPath = $sXPath;
- $this->sExtraInfo = $sExtraInfo;
- }
-
- /**
- * Get the source line number where the problem happened
- * @return number
- */
- public function GetSourceLineNumber()
- {
- return $this->iSourceLineNumber;
- }
- /**
- * Get the XPath in the whole document where the problem happened
- * @return string
- */
- public function GetXPath()
- {
- return $this->sXPath;
- }
-
- /**
- * Get some extra info (depending on the exception's code), like the invalid value for the _delta attribute
- * @return string
- */
- public function GetExtraInfo()
- {
- return $this->sExtraInfo;
- }
- }
- /**
- * ModelFactoryModule: the representation of a Module (i.e. element that can be selected during the setup)
- * @package ModelFactory
- */
- class MFModule
- {
- protected $sId;
- protected $sName;
- protected $sVersion;
- protected $sRootDir;
- protected $sLabel;
- protected $aDataModels;
- protected $bAutoSelect;
- protected $sAutoSelect;
- protected $aFilesToInclude;
-
- public function __construct($sId, $sRootDir, $sLabel, $bAutoSelect = false)
- {
- $this->sId = $sId;
-
- list($this->sName, $this->sVersion) = ModuleDiscovery::GetModuleName($sId);
- if (strlen($this->sVersion) == 0)
- {
- $this->sVersion = '1.0.0';
- }
- $this->sRootDir = $sRootDir;
- $this->sLabel = $sLabel;
- $this->aDataModels = array();
- $this->bAutoSelect = $bAutoSelect;
- $this->sAutoSelect = 'false';
- $this->aFilesToInclude = array('addons' => array(), 'business' => array(), 'webservices' => array(),);
-
- // Scan the module's root directory to find the datamodel(*).xml files
- if ($hDir = opendir($sRootDir))
- {
- // This is the correct way to loop over the directory. (according to the documentation)
- while (($sFile = readdir($hDir)) !== false)
- {
- if (preg_match('/^datamodel(.*)\.xml$/i', $sFile, $aMatches))
- {
- $this->aDataModels[] = $this->sRootDir.'/'.$aMatches[0];
- }
- }
- closedir($hDir);
- }
- }
-
-
- public function GetId()
- {
- return $this->sId;
- }
-
- public function GetName()
- {
- return $this->sName;
- }
- public function GetVersion()
- {
- return $this->sVersion;
- }
- public function GetLabel()
- {
- return $this->sLabel;
- }
-
- public function GetRootDir()
- {
- return $this->sRootDir;
- }
- public function GetModuleDir()
- {
- return basename($this->sRootDir);
- }
- public function GetDataModelFiles()
- {
- return $this->aDataModels;
- }
-
- /**
- * List all classes in this module
- */
- public function ListClasses()
- {
- return array();
- }
-
- public function GetDictionaryFiles()
- {
- $aDictionaries = array();
- if ($hDir = opendir($this->sRootDir))
- {
- while (($sFile = readdir($hDir)) !== false)
- {
- $aMatches = array();
- if (preg_match("/^[^\\.]+.dict.".$this->sName.".php$/i", $sFile, $aMatches)) // Dictionary files are named like <Lang>.dict.<ModuleName>.php
- {
- $aDictionaries[] = $this->sRootDir.'/'.$sFile;
- }
- }
- closedir($hDir);
- }
- return $aDictionaries;
- }
-
- public function IsAutoSelect()
- {
- return $this->bAutoSelect;
- }
-
- public function SetAutoSelect($sAutoSelect)
- {
- $this->sAutoSelect = $sAutoSelect;
- }
- public function GetAutoSelect()
- {
- return $this->sAutoSelect;
- }
-
- public function SetFilesToInclude($aFiles, $sCategory)
- {
- // Now ModuleDiscovery provides us directly with relative paths... nothing to do
- $this->aFilesToInclude[$sCategory] = $aFiles;
-
- /*
- $sDir = basename($this->sRootDir);
- $iLen = strlen($sDir.'/');
- foreach($aFiles as $sFile)
- {
- $iPos = strpos($sFile, $sDir.'/');
- //$this->aFilesToInclude[$sCategory][] = substr($sFile, $iPos+$iLen);
- $this->aFilesToInclude[$sCategory][] = $sFile;
- }
- */
- }
-
- public function GetFilesToInclude($sCategory)
- {
- return $this->aFilesToInclude[$sCategory];
- }
-
- }
- /**
- * MFDeltaModule: an optional module, made of a single file
- * @package ModelFactory
- */
- class MFDeltaModule extends MFModule
- {
- public function __construct($sDeltaFile)
- {
- $this->sId = 'datamodel-delta';
-
- $this->sName = 'delta';
- $this->sVersion = '1.0';
- $this->sRootDir = '';
- $this->sLabel = 'Additional Delta';
- $this->aDataModels = array($sDeltaFile);
- $this->aFilesToInclude = array('addons' => array(), 'business' => array(), 'webservices' => array(),);
- }
- public function GetName()
- {
- return ''; // Objects created inside this pseudo module retain their original module's name
- }
- public function GetRootDir()
- {
- return '';
- }
- public function GetModuleDir()
- {
- return '';
- }
- public function GetDictionaryFiles()
- {
- return array();
- }
- }
- /**
- * MFDeltaModule: an optional module, made of a single file
- * @package ModelFactory
- */
- class MFCoreModule extends MFModule
- {
- public function __construct($sName, $sLabel, $sDeltaFile)
- {
- $this->sId = $sName;
- $this->sName = $sName;
- $this->sVersion = '1.0';
- $this->sRootDir = '';
- $this->sLabel = $sLabel;
- $this->aDataModels = array($sDeltaFile);
- $this->aFilesToInclude = array('addons' => array(), 'business' => array(), 'webservices' => array(),);
- }
-
- public function GetRootDir()
- {
- return '';
- }
- public function GetModuleDir()
- {
- return '';
- }
- public function GetDictionaryFiles()
- {
- return array();
- }
- }
- /**
- * MFDictModule: an optional module, consisting only of dictionaries
- * @package ModelFactory
- */
- class MFDictModule extends MFModule
- {
- public function __construct($sName, $sLabel, $sRootDir)
- {
- $this->sId = $sName;
- $this->sName = $sName;
- $this->sVersion = '1.0';
- $this->sRootDir = $sRootDir;
- $this->sLabel = $sLabel;
- $this->aDataModels = array();
- $this->aFilesToInclude = array('addons' => array(), 'business' => array(), 'webservices' => array(),);
- }
- public function GetRootDir()
- {
- return '';
- }
- public function GetModuleDir()
- {
- return '';
- }
-
- public function GetDictionaryFiles()
- {
- $aDictionaries = array();
- if ($hDir = opendir($this->sRootDir))
- {
- while (($sFile = readdir($hDir)) !== false)
- {
- $aMatches = array();
- if (preg_match("/^.*dictionary\\.itop.*.php$/i", $sFile, $aMatches)) // Dictionary files are named like <Lang>.dict.<ModuleName>.php
- {
- $aDictionaries[] = $this->sRootDir.'/'.$sFile;
- }
- }
- closedir($hDir);
- }
- return $aDictionaries;
- }
- }
- /**
- * ModelFactory: the class that manages the in-memory representation of the XML MetaModel
- * @package ModelFactory
- */
- class ModelFactory
- {
- protected $aRootDirs;
- protected $oDOMDocument;
- protected $oRoot;
- protected $oModules;
- protected $oClasses;
- protected $oMenus;
- protected $oDictionaries;
- static protected $aLoadedClasses;
- static protected $aWellKnownParents = array('DBObject', 'CMDBObject','cmdbAbstractObject');
- // static protected $aWellKnownMenus = array('DataAdministration', 'Catalogs', 'ConfigManagement', 'Contact', 'ConfigManagementCI', 'ConfigManagement:Shortcuts', 'ServiceManagement');
- static protected $aLoadedModules;
- static protected $aLoadErrors;
- protected $aDict;
- protected $aDictKeys;
-
-
- public function __construct($aRootDirs, $aRootNodeExtensions = array())
- {
- $this->aDict = array();
- $this->aDictKeys = array();
- $this->aRootDirs = $aRootDirs;
- $this->oDOMDocument = new MFDocument();
- $this->oRoot = $this->oDOMDocument->CreateElement('itop_design');
- $this->oRoot->setAttribute('xmlns:xsi', "http://www.w3.org/2001/XMLSchema-instance");
- $this->oRoot->setAttribute('version', ITOP_DESIGN_LATEST_VERSION);
- $this->oDOMDocument->AppendChild($this->oRoot);
- $this->oModules = $this->oDOMDocument->CreateElement('loaded_modules');
- $this->oRoot->AppendChild($this->oModules);
- $this->oClasses = $this->oDOMDocument->CreateElement('classes');
- $this->oRoot->AppendChild($this->oClasses);
- $this->oDictionaries = $this->oDOMDocument->CreateElement('dictionaries');
- $this->oRoot->AppendChild($this->oDictionaries);
-
- foreach (self::$aWellKnownParents as $sWellKnownParent)
- {
- $this->AddWellKnownParent($sWellKnownParent);
- }
- $this->oMenus = $this->oDOMDocument->CreateElement('menus');
- $this->oRoot->AppendChild($this->oMenus);
-
- $this->oMeta = $this->oDOMDocument->CreateElement('meta');
- $this->oRoot->AppendChild($this->oMeta);
-
- foreach($aRootNodeExtensions as $sElementName)
- {
- $oElement = $this->oDOMDocument->CreateElement($sElementName);
- $this->oRoot->AppendChild($oElement);
- }
- self::$aLoadedModules = array();
- self::$aLoadErrors = array();
- libxml_use_internal_errors(true);
- }
-
- public function Dump($oNode = null, $bReturnRes = false)
- {
- if (is_null($oNode))
- {
- $oNode = $this->oRoot;
- }
- return $oNode->Dump($bReturnRes);
- }
- public function LoadFromFile($sCacheFile)
- {
- $this->oDOMDocument->load($sCacheFile);
- $this->oRoot = $this->oDOMDocument->firstChild;
-
- $this->oModules = $this->oRoot->getElementsByTagName('loaded_modules')->item(0);
- self::$aLoadedModules = array();
- foreach($this->oModules->getElementsByTagName('module') as $oModuleNode)
- {
- $sId = $oModuleNode->getAttribute('id');
- $sRootDir = $oModuleNode->GetChildText('root_dir');
- $sLabel = $oModuleNode->GetChildText('label');
- self::$aLoadedModules[] = new MFModule($sId, $sRootDir, $sLabel);
- }
- }
- public function SaveToFile($sCacheFile)
- {
- $this->oDOMDocument->save($sCacheFile);
- }
- /**
- * To progressively replace LoadModule
- * @param MFElement $oSourceNode
- * @param MFElement $oTargetParentNode
- */
- public function LoadDelta($oSourceNode, $oTargetParentNode)
- {
- if (!$oSourceNode instanceof DOMElement) return;
- //echo "Load $oSourceNode->tagName::".$oSourceNode->getAttribute('id')." (".$oSourceNode->getAttribute('_delta').")<br/>\n";
- $oTarget = $this->oDOMDocument;
- $sDeltaSpec = $oSourceNode->getAttribute('_delta');
- if (($oSourceNode->tagName == 'class') && ($oSourceNode->parentNode->tagName == 'classes') && ($oSourceNode->parentNode->parentNode->tagName == 'itop_design'))
- {
- $sParentId = $oSourceNode->GetChildText('parent');
- if (($sDeltaSpec == 'define') || ($sDeltaSpec == 'force'))
- {
- // This tag is organized in hierarchy: determine the real parent node (as a subnode of the current node)
- $oTargetParentNode = $oTarget->GetNodeById('/itop_design/classes//class', $sParentId)->item(0);
-
- if (!$oTargetParentNode)
- {
- echo "Dumping target doc - looking for '$sParentId'<br/>\n";
- $this->oDOMDocument->firstChild->Dump();
- $sPath = MFDocument::GetItopNodePath($oSourceNode);
- $iLine = $oSourceNode->getLineNo();
- throw new MFException($sPath.' at line '.$iLine.": parent class '$sParentId' could not be found", MFException::PARENT_NOT_FOUND, $iLine, $sPath, $sParentId);
- }
- }
- else
- {
- $oTargetNode = $oTarget->GetNodeById('/itop_design/classes//class', $oSourceNode->getAttribute('id'))->item(0);
- if (!$oTargetNode)
- {
- if ($sDeltaSpec === 'if_exists')
- {
- // Just ignore it
- }
- else
- {
- echo "Dumping target doc - looking for '".$oSourceNode->getAttribute('id')."'<br/>\n";
- $this->oDOMDocument->firstChild->Dump();
- $sPath = MFDocument::GetItopNodePath($oSourceNode);
- $iLine = $oSourceNode->getLineNo();
- throw new MFException($sPath.' at line '.$iLine.": could not be found", MFException::NOT_FOUND, $iLine, $sPath);
-
- }
- }
- else
- {
- $oTargetParentNode = $oTargetNode->parentNode;
- if (($sDeltaSpec == 'redefine') && ($oTargetParentNode->getAttribute('id') != $sParentId))
- {
- // A class that has moved <=> deletion and creation elsewhere
- $oTargetParentNode = $oTarget->GetNodeById('/itop_design/classes//class', $sParentId)->item(0);
- $oTargetNode->Delete();
- $oSourceNode->setAttribute('_delta', 'define');
- $sDeltaSpec = 'define';
- }
- }
-
- }
- }
- switch ($sDeltaSpec)
- {
- case 'if_exists':
- case 'must_exist':
- case 'merge':
- case '':
- $bMustExist = ($sDeltaSpec == 'must_exist');
- $bIfExists =($sDeltaSpec == 'if_exists');
- $sSearchId = $oSourceNode->hasAttribute('_rename_from') ? $oSourceNode->getAttribute('_rename_from') : $oSourceNode->getAttribute('id');
- $oTargetNode = $oSourceNode->MergeInto($oTargetParentNode, $sSearchId, $bMustExist, $bIfExists);
- if ($oTargetNode)
- {
- foreach ($oSourceNode->childNodes as $oSourceChild)
- {
- // Continue deeper
- $this->LoadDelta($oSourceChild, $oTargetNode);
- }
- }
- break;
- case 'define_if_not_exists':
- $oExistingNode = $oTargetParentNode->_FindChildNode($oSourceNode);
- if ( ($oExistingNode == null) || ($oExistingNode->getAttribute('_alteration') == 'removed') )
- {
- // Same as 'define' below
- $oTargetNode = $oTarget->ImportNode($oSourceNode, true);
- $oTargetParentNode->AddChildNode($oTargetNode);
- }
- else
- {
- $oTargetNode = $oExistingNode;
- }
- $oTargetNode->setAttribute('_alteration', 'needed');
- break;
-
- case 'define':
- // New node - copy child nodes as well
- $oTargetNode = $oTarget->ImportNode($oSourceNode, true);
- $oTargetParentNode->AddChildNode($oTargetNode);
- break;
-
- case 'force':
- // Force node - copy child nodes as well
- $oTargetNode = $oTarget->ImportNode($oSourceNode, true);
- $oTargetParentNode->SetChildNode($oTargetNode, null, true);
- break;
-
- case 'redefine':
- // Replace the existing node by the given node - copy child nodes as well
- $oTargetNode = $oTarget->ImportNode($oSourceNode, true);
- $sSearchId = $oSourceNode->hasAttribute('_rename_from') ? $oSourceNode->getAttribute('_rename_from') : $oSourceNode->getAttribute('id');
- $oTargetParentNode->RedefineChildNode($oTargetNode, $sSearchId);
- break;
- case 'delete_if_exists':
- $oTargetNode = $oTargetParentNode->_FindChildNode($oSourceNode);
- if (($oTargetNode !== null) && ($oTargetNode->getAttribute('_alteration') !== 'removed'))
- {
- // Delete the node if it actually exists and is not already marked as deleted
- $oTargetNode->Delete();
- }
- // otherwise fail silently
- break;
-
- case 'delete':
- $oTargetNode = $oTargetParentNode->_FindChildNode($oSourceNode);
- $sPath = MFDocument::GetItopNodePath($oSourceNode);
- $iLine = $oSourceNode->getLineNo();
- if ($oTargetNode == null)
- {
- throw new MFException($sPath.' at line '.$iLine.": could not be deleted (not found)", MFException::COULD_NOT_BE_DELETED, $iLine, $sPath);
- }
- if ($oTargetNode->getAttribute('_alteration') == 'removed')
- {
- throw new MFException($sPath.' at line '.$iLine.": could not be deleted (already marked as deleted)", MFException::ALREADY_DELETED, $iLine, $sPath);
- }
- $oTargetNode->Delete();
- break;
-
- default:
- $sPath = MFDocument::GetItopNodePath($oSourceNode);
- $iLine = $oSourceNode->getLineNo();
- throw new MFException($sPath.' at line '.$iLine.": unexpected value for attribute _delta: '".$sDeltaSpec."'", MFException::INVALID_DELTA, $iLine, $sPath, $sDeltaSpec);
- }
- if ($oTargetNode)
- {
- if ($oSourceNode->hasAttribute('_rename_from'))
- {
- $oTargetNode->Rename($oSourceNode->getAttribute('id'));
- }
- if ($oTargetNode->hasAttribute('_delta'))
- {
- $oTargetNode->removeAttribute('_delta');
- }
- }
- }
- /**
- * Loads the definitions corresponding to the given Module
- * @param MFModule $oModule
- * @param Array $aLanguages The list of languages to process (for the dictionaries). If empty all languages are kept
- */
- public function LoadModule(MFModule $oModule, $aLanguages = array())
- {
- try
- {
- $aDataModels = $oModule->GetDataModelFiles();
- $sModuleName = $oModule->GetName();
- $aClasses = array();
- self::$aLoadedModules[] = $oModule;
-
- // For persistence in the cache
- $oModuleNode = $this->oDOMDocument->CreateElement('module');
- $oModuleNode->setAttribute('id', $oModule->GetId());
- $oModuleNode->AppendChild($this->oDOMDocument->CreateElement('root_dir', $oModule->GetRootDir()));
- $oModuleNode->AppendChild($this->oDOMDocument->CreateElement('label', $oModule->GetLabel()));
-
- $this->oModules->AppendChild($oModuleNode);
-
- foreach($aDataModels as $sXmlFile)
- {
- $oDocument = new MFDocument();
- libxml_clear_errors();
- $oDocument->load($sXmlFile);
- //$bValidated = $oDocument->schemaValidate(APPROOT.'setup/itop_design.xsd');
- $aErrors = libxml_get_errors();
- if (count($aErrors) > 0)
- {
- self::$aLoadErrors[$sModuleName] = $aErrors;
- return;
- }
-
- $oXPath = new DOMXPath($oDocument);
- $oNodeList = $oXPath->query('/itop_design/classes//class');
- foreach($oNodeList as $oNode)
- {
- if ($oNode->getAttribute('_created_in') == '')
- {
- $oNode->SetAttribute('_created_in', $sModuleName);
- }
- }
- $oNodeList = $oXPath->query('/itop_design/constants/constant');
- foreach($oNodeList as $oNode)
- {
- if ($oNode->getAttribute('_created_in') == '')
- {
- $oNode->SetAttribute('_created_in', $sModuleName);
- }
- }
- $oNodeList = $oXPath->query('/itop_design/menus/menu');
- foreach($oNodeList as $oNode)
- {
- if ($oNode->getAttribute('_created_in') == '')
- {
- $oNode->SetAttribute('_created_in', $sModuleName);
- }
- }
- $oUserRightsNode = $oXPath->query('/itop_design/user_rights')->item(0);
- if ($oUserRightsNode)
- {
- if ($oUserRightsNode->getAttribute('_created_in') == '')
- {
- $oUserRightsNode->SetAttribute('_created_in', $sModuleName);
- }
- }
-
- $oAlteredNodes = $oXPath->query('/itop_design//*[@_delta]');
- if ($oAlteredNodes->length > 0)
- {
- foreach($oAlteredNodes as $oAlteredNode)
- {
- $oAlteredNode->SetAttribute('_altered_in', $sModuleName);
- }
- }
-
- $oFormat = new iTopDesignFormat($oDocument);
- if (!$oFormat->Convert())
- {
- $sError = implode(', ', $oFormat->GetErrors());
- throw new Exception("Cannot load module $sModuleName, failed to upgrade to datamodel format of: $sXmlFile. Reason(s): $sError");
- }
-
- $oDeltaRoot = $oDocument->childNodes->item(0);
- $this->LoadDelta($oDeltaRoot, $this->oDOMDocument);
- }
-
- $aDictionaries = $oModule->GetDictionaryFiles();
-
- try
- {
- $this->ResetTempDictionary();
- foreach($aDictionaries as $sPHPFile)
- {
- $sDictFileContents = file_get_contents($sPHPFile);
- $sDictFileContents = str_replace(array('<'.'?'.'php', '?'.'>'), '', $sDictFileContents);
- $sDictFileContents = str_replace('Dict::Add', '$this->AddToTempDictionary', $sDictFileContents);
- eval($sDictFileContents);
- }
-
- foreach ($this->aDict as $sLanguageCode => $aDictDefinition)
- {
- if ((count($aLanguages) > 0 ) && !in_array($sLanguageCode, $aLanguages))
- {
- // skip some languages if the parameter says so
- continue;
- }
-
- $oNodes = $this->GetNodeById('dictionary', $sLanguageCode, $this->oDictionaries);
- if ($oNodes->length == 0)
- {
- $oXmlDict = $this->oDOMDocument->CreateElement('dictionary');
- $oXmlDict->setAttribute('id', $sLanguageCode);
- $this->oDictionaries->AddChildNode($oXmlDict);
- $oXmlEntries = $this->oDOMDocument->CreateElement('english_description', $aDictDefinition['english_description']);
- $oXmlDict->AppendChild($oXmlEntries);
- $oXmlEntries = $this->oDOMDocument->CreateElement('localized_description', $aDictDefinition['localized_description']);
- $oXmlDict->AppendChild($oXmlEntries);
- $oXmlEntries = $this->oDOMDocument->CreateElement('entries');
- $oXmlDict->AppendChild($oXmlEntries);
- }
- else
- {
- $oXmlDict = $oNodes->item(0);
- $oXmlEntries = $oXmlDict->GetUniqueElement('entries');
- }
-
- foreach ($aDictDefinition['entries'] as $sCode => $sLabel)
- {
-
- $oXmlEntry = $this->oDOMDocument->CreateElement('entry');
- $oXmlEntry->setAttribute('id', $sCode);
- $oXmlValue = $this->oDOMDocument->CreateCDATASection($sLabel);
- $oXmlEntry->appendChild($oXmlValue);
- if (array_key_exists($sLanguageCode, $this->aDictKeys) && array_key_exists($sCode, $this->aDictKeys[$sLanguageCode]))
- {
- $oMe = $this->aDictKeys[$sLanguageCode][$sCode];
- $sFlag = $oMe->getAttribute('_alteration');
- $oMe->parentNode->replaceChild($oXmlEntry, $oMe);
- $sNewFlag = $sFlag;
- if ($sFlag == '')
- {
- $sNewFlag = 'replaced';
- }
- $oXmlEntry->setAttribute('_alteration', $sNewFlag);
-
- }
- else
- {
- $oXmlEntry->setAttribute('_alteration', 'added');
- $oXmlEntries->appendChild($oXmlEntry);
- }
- $this->aDictKeys[$sLanguageCode][$sCode] = $oXmlEntry;
- }
- }
- }
- catch(Exception $e)
- {
- throw new Exception('Failed to load dictionary file "'.$sPHPFile.'", reason: '.$e->getMessage());
- }
-
- }
- catch(Exception $e)
- {
- $aLoadedModuleNames = array();
- foreach (self::$aLoadedModules as $oModule)
- {
- $aLoadedModuleNames[] = $oModule->GetName();
- }
- throw new Exception('Error loading module "'.$oModule->GetName().'": '.$e->getMessage().' - Loaded modules: '.implode(',', $aLoadedModuleNames));
- }
- }
- /**
- * Collects the PHP Dict entries into the ModelFactory for transforming the dictionary into an XML structure
- * @param string $sLanguageCode The language code
- * @param string $sEnglishLanguageDesc English description of the language (unused but kept for API compatibility)
- * @param string $sLocalizedLanguageDesc Localized description of the language (unused but kept for API compatibility)
- * @param hash $aEntries The entries to load: string_code => translation
- */
- protected function AddToTempDictionary($sLanguageCode, $sEnglishLanguageDesc, $sLocalizedLanguageDesc, $aEntries)
- {
- $this->aDict[$sLanguageCode]['english_description'] = $sEnglishLanguageDesc;
- $this->aDict[$sLanguageCode]['localized_description'] = $sLocalizedLanguageDesc;
- if (!array_key_exists('entries', $this->aDict[$sLanguageCode]))
- {
- $this->aDict[$sLanguageCode]['entries'] = array();
- }
- foreach($aEntries as $sKey => $sValue)
- {
- $this->aDict[$sLanguageCode]['entries'][$sKey] = $sValue;
- }
- }
-
- protected function ResetTempDictionary()
- {
- $this->aDict = array();
- }
-
- /**
- * XML load errors (XML format and validation)
- */
- function HasLoadErrors()
- {
- return (count(self::$aLoadErrors) > 0);
- }
- function GetLoadErrors()
- {
- return self::$aLoadErrors;
- }
- function GetLoadedModules($bExcludeWorkspace = true)
- {
- if ($bExcludeWorkspace)
- {
- $aModules = array();
- foreach(self::$aLoadedModules as $oModule)
- {
- if (!$oModule instanceof MFWorkspace)
- {
- $aModules[] = $oModule;
- }
- }
- }
- else
- {
- $aModules = self::$aLoadedModules;
- }
- return $aModules;
- }
-
-
- function GetModule($sModuleName)
- {
- foreach(self::$aLoadedModules as $oModule)
- {
- if ($oModule->GetName() == $sModuleName) return $oModule;
- }
- return null;
- }
-
- public function CreateElement($sTagName, $sValue = '')
- {
- return $this->oDOMDocument->createElement($sTagName, $sValue);
- }
-
- public function GetNodeById($sXPath, $sId, $oContextNode = null)
- {
- return $this->oDOMDocument->GetNodeById($sXPath, $sId, $oContextNode);
- }
- /**
- * Apply extensibility rules into the DOM
- * @param array aRestrictionRules Array of array ('selectors' => array of XPaths, 'rules' => array of rules)
- * @return void
- */
- public function RestrictExtensibility($aRestrictionRules)
- {
- foreach ($aRestrictionRules as $aRestriction)
- {
- foreach ($aRestriction['selectors'] as $sSelector)
- {
- foreach($this->GetNodes($sSelector) as $oNode)
- {
- $oNode->RestrictExtensibility($aRestriction['rules']);
- }
- }
- }
- }
-
- /**
- * Check if the class specified by the given node already exists in the loaded DOM
- * @param DOMNode $oClassNode The node corresponding to the class to load
- * @throws Exception
- * @return bool True if the class exists, false otherwise
- */
- protected function ClassExists(DOMNode $oClassNode)
- {
- assert(false);
- if ($oClassNode->hasAttribute('id'))
- {
- $sClassName = $oClassNode->GetAttribute('id');
- }
- else
- {
- throw new Exception('ModelFactory::AddClass: Cannot add a class with no name');
- }
-
- return (array_key_exists($sClassName, self::$aLoadedClasses));
- }
-
- /**
- * Check if the class specified by the given name already exists in the loaded DOM
- * @param string $sClassName The node corresponding to the class to load
- * @throws Exception
- * @return bool True if the class exists, false otherwise
- */
- protected function ClassNameExists($sClassName)
- {
- return !is_null($this->GetClass($sClassName));
- }
- /**
- * Add the given class to the DOM
- * @param DOMNode $oClassNode
- * @param string $sModuleName The name of the module in which this class is declared
- * @throws Exception
- */
- public function AddClass(DOMNode $oClassNode, $sModuleName)
- {
- if ($oClassNode->hasAttribute('id'))
- {
- $sClassName = $oClassNode->GetAttribute('id');
- }
- else
- {
- throw new Exception('ModelFactory::AddClass: Cannot add a class with no name');
- }
- if ($this->ClassNameExists($oClassNode->getAttribute('id')))
- {
- throw new Exception("ModelFactory::AddClass: Cannot add the already existing class $sClassName");
- }
-
- $sParentClass = $oClassNode->GetChildText('parent', '');
- $oParentNode = $this->GetClass($sParentClass);
- if ($oParentNode == null)
- {
- throw new Exception("ModelFactory::AddClass: Cannot find the parent class of '$sClassName': '$sParentClass'");
- }
- else
- {
- if ($sModuleName != '')
- {
- $oClassNode->SetAttribute('_created_in', $sModuleName);
- }
- $oParentNode->AddChildNode($this->oDOMDocument->importNode($oClassNode, true));
-
- if (substr($sParentClass, 0, 1) == '/') // Convention for well known parent classes
- {
- // Remove the leading slash character
- $oParentNameNode = $oClassNode->GetOptionalElement('parent')->firstChild; // Get the DOMCharacterData node
- $oParentNameNode->data = substr($sParentClass, 1);
- }
- }
- }
-
- public function GetClassXMLTemplate($sName, $sIcon)
- {
- $sHeader = '<?'.'xml version="1.0" encoding="utf-8"?'.'>';
- return
- <<<EOF
- $sHeader
- <class id="$sName">
- <comment/>
- <properties>
- </properties>
- <naming format=""><attributes/></naming>
- <reconciliation><attributes/></reconciliation>
- <display_template/>
- <icon>$sIcon</icon>
- </properties>
- <fields/>
- <lifecycle/>
- <methods/>
- <presentation>
- <details><items/></details>
- <search><items/></search>
- <list><items/></list>
- </presentation>
- </class>
- EOF
- ;
- }
- /**
- * List all constants from the DOM, for a given module
- * @param string $sModuleName
- * @throws Exception
- */
- public function ListConstants($sModuleName)
- {
- return $this->GetNodes("/itop_design/constants/constant[@_created_in='$sModuleName']");
- }
- /**
- * List all classes from the DOM, for a given module
- * @param string $sModuleName
- * @throws Exception
- */
- public function ListClasses($sModuleName)
- {
- return $this->GetNodes("/itop_design/classes//class[@_created_in='$sModuleName']");
- }
-
- /**
- * List all classes from the DOM
- * @throws Exception
- */
- public function ListAllClasses()
- {
- return $this->GetNodes("/itop_design/classes//class");
- }
-
- /**
- * List top level (non abstract) classes having child classes
- * @throws Exception
- */
- public function ListRootClasses()
- {
- return $this->GetNodes("/itop_design/classes/class/class[class]");
- }
- public function GetClass($sClassName)
- {
- $oClassNode = $this->GetNodes("/itop_design/classes//class[@id='$sClassName']")->item(0);
- return $oClassNode;
- }
-
- public function AddWellKnownParent($sWellKnownParent)
- {
- $oWKClass = $this->oDOMDocument->CreateElement('class');
- $oWKClass->setAttribute('id', $sWellKnownParent);
- $this->oClasses->AppendChild($oWKClass);
- return $oWKClass;
- }
-
- public function GetChildClasses($oClassNode)
- {
- return $this->GetNodes("class", $oClassNode);
- }
-
-
- public function GetField($sClassName, $sAttCode)
- {
- if (!$this->ClassNameExists($sClassName))
- {
- return null;
- }
- $oClassNode = self::$aLoadedClasses[$sClassName];
- $oFieldNode = $this->GetNodes("fields/field[@id='$sAttCode']", $oClassNode)->item(0);
- if (($oFieldNode == null) && ($sParentClass = $oClassNode->GetChildText('parent')))
- {
- return $this->GetField($sParentClass, $sAttCode);
- }
- return $oFieldNode;
- }
-
- /**
- * List all classes from the DOM
- * @throws Exception
- */
- public function ListFields(DOMNode $oClassNode)
- {
- return $this->GetNodes("fields/field", $oClassNode);
- }
-
- /**
- * List all transitions from a given state
- * @param DOMNode $oStateNode The state
- * @throws Exception
- */
- public function ListTransitions(DOMNode $oStateNode)
- {
- return $this->GetNodes("transitions/transition", $oStateNode);
- }
-
- /**
- * List all states of a given class
- * @param DOMNode $oClassNode The class
- * @throws Exception
- */
- public function ListStates(DOMNode $oClassNode)
- {
- return $this->GetNodes("lifecycle/states/state", $oClassNode);
- }
-
- public function ApplyChanges()
- {
- return $this->oRoot->ApplyChanges();
- }
-
- public function ListChanges()
- {
- return $this->oRoot->ListChanges();
- }
- /**
- * Import the node into the delta
- */
- protected function SetDeltaFlags($oNodeClone)
- {
- $sAlteration = $oNodeClone->getAttribute('_alteration');
- $oNodeClone->removeAttribute('_alteration');
- if ($oNodeClone->hasAttribute('_old_id'))
- {
- $oNodeClone->setAttribute('_rename_from', $oNodeClone->getAttribute('_old_id'));
- $oNodeClone->removeAttribute('_old_id');
- }
- switch ($sAlteration)
- {
- case '':
- if ($oNodeClone->hasAttribute('id'))
- {
- $oNodeClone->setAttribute('_delta', 'must_exist');
- }
- break;
- case 'added':
- $oNodeClone->setAttribute('_delta', 'define');
- break;
- case 'replaced':
- $oNodeClone->setAttribute('_delta', 'redefine');
- break;
- case 'removed':
- $oNodeClone->setAttribute('_delta', 'delete');
- break;
- case 'needed':
- $oNodeClone->setAttribute('_delta', 'define_if_not_exists');
- break;
- case 'forced':
- $oNodeClone->setAttribute('_delta', 'force');
- break;
- }
- return $oNodeClone;
- }
- /**
- * Create path for the delta
- * @param Array aMovedClasses The classes that have been moved in the hierarchy (deleted + created elsewhere)
- * @param DOMDocument oTargetDoc Where to attach the top of the hierarchy
- * @param MFElement oNode The node to import with its path
- */
- protected function ImportNodeAndPathDelta($aMovedClasses, $oTargetDoc, $oNode)
- {
- // Preliminary: skip the parent if this node is organized hierarchically into the DOM
- // Only class nodes are organized this way
- $oParent = $oNode->parentNode;
- if ($oNode->IsClassNode())
- {
- while (($oParent instanceof DOMElement) && ($oParent->IsClassNode()))
- {
- $oParent = $oParent->parentNode;
- }
- }
- // Recursively create the path for the parent
- if ($oParent instanceof DOMElement)
- {
- $oParentClone = $this->ImportNodeAndPathDelta($aMovedClasses, $oTargetDoc, $oParent);
- }
- else
- {
- // We've reached the top let's add the node into the root recipient
- $oParentClone = $oTargetDoc;
- }
- $sAlteration = $oNode->getAttribute('_alteration');
- if ($oNode->IsClassNode() && ($sAlteration != ''))
- {
- // Handle the moved classes
- //
- // Import the whole root node
- $oNodeClone = $oTargetDoc->importNode($oNode->cloneNode(true), true);
- $oParentClone->appendChild($oNodeClone);
- $this->SetDeltaFlags($oNodeClone);
- // Handle the moved classes found under the root node (or the root node itself)
- foreach($oNodeClone->GetNodes("descendant-or-self::class[@id]", false) as $oClassNode)
- {
- if (array_key_exists($oClassNode->getAttribute('id'), $aMovedClasses))
- {
- if ($sAlteration == 'removed')
- {
- // Remove that node: this specification will be overriden by the 'replaced' spec (see below)
- $oClassNode->parentNode->removeChild($oClassNode);
- }
- else
- {
- // Move the class at the root, with the flag 'modified'
- $oParentClone->appendChild($oClassNode);
- $oClassNode->setAttribute('_alteration', 'replaced');
- $this->SetDeltaFlags($oClassNode);
- }
- }
- }
- }
- else
- {
- // Look for the node into the parent node
- // Note: this is an identified weakness of the algorithm,
- // because for each node modified, and each node of its path
- // we will have to lookup for the existing entry
- // Anyhow, this loop is quite quick to execute because in the delta
- // the number of nodes is limited
- $oNodeClone = null;
- foreach ($oParentClone->childNodes as $oChild)
- {
- if (($oChild instanceof DOMElement) && ($oChild->tagName == $oNode->tagName))
- {
- if (!$oNode->hasAttribute('id') || ($oNode->getAttribute('id') == $oChild->getAttribute('id')))
- {
- $oNodeClone = $oChild;
- break;
- }
- }
- }
- if (!$oNodeClone)
- {
- $bCopyContents = ($sAlteration == 'replaced') || ($sAlteration == 'added') || ($sAlteration == 'needed') || ($sAlteration == 'forced');
- $oNodeClone = $oTargetDoc->importNode($oNode->cloneNode($bCopyContents), $bCopyContents);
- $this->SetDeltaFlags($oNodeClone);
- $oParentClone->appendChild($oNodeClone);
- }
- }
- return $oNodeClone;
- }
- /**
- * Set the value for a given trace attribute
- * See MFElement::SetTrace to enable/disable change traces
- */
- public function SetTraceValue($sAttribute, $sPreviousValue, $sNewValue)
- {
- // Search into the deleted node as well!
- $oNodeSet = $this->oDOMDocument->GetNodes("//*[@$sAttribute='$sPreviousValue']", null, false);
- foreach($oNodeSet as $oTouchedNode)
- {
- $oTouchedNode->setAttribute($sAttribute, $sNewValue);
- }
- }
- /**
- * Get the document version of the delta
- */
- public function GetDeltaDocument($aNodesToIgnore = array(), $aAttributes = null)
- {
- $oDelta = new MFDocument();
- // Handle classes moved from one parent to another
- // This will be done in two steps:
- // 1) Identify the moved classes (marked as deleted under the original parent, and created under the new parent)
- // 2) When importing those "moved" classes into the delta (see ImportNodeAndPathDelta), extract them from the hierarchy (the alteration can be done at an upper level in the hierarchy) and mark them as "modified"
- $aMovedClasses = array();
- foreach($this->GetNodes("/itop_design/classes//class/class[@_alteration='removed']", null, false) as $oNode)
- {
- $sId = $oNode->getAttribute('id');
- if ($this->GetNodes("/itop_design/classes//class/class[@id='$sId']/properties", null, false)->length > 0)
- {
- $aMovedClasses[$sId] = true;
- }
- }
- foreach($this->ListChanges() as $oAlteredNode)
- {
- $this->ImportNodeAndPathDelta($aMovedClasses, $oDelta, $oAlteredNode);
- }
- foreach($aNodesToIgnore as $sXPath)
- {
- $oNodesToRemove = $oDelta->GetNodes($sXPath);
- foreach($oNodesToRemove as $oNode)
- {
- if ($oNode instanceof DOMAttr)
- {
- $oNode->ownerElement->removeAttributeNode($oNode);
- }
- else
- {
- $oNode->parentNode->removeChild($oNode);
- }
- }
- }
- $oNodesToClean = $oDelta->GetNodes('/itop_design//*[@_altered_in]');
- foreach($oNodesToClean as $oNode)
- {
- $oNode->removeAttribute('_altered_in');
- }
-
- if ($aAttributes != null)
- {
- foreach ($aAttributes as $sAttribute => $value)
- {
- if ($oDelta->documentElement) // yes, this may happen when still no change has been performed (and a module has been selected for installation)
- {
- $oDelta->documentElement->setAttribute($sAttribute, $value);
- }
- }
- }
- return $oDelta;
- }
- /**
- * Get the text/XML version of the delta
- */
- public function GetDelta($aNodesToIgnore = array(), $aAttributes = null)
- {
- $oDelta = $this->GetDeltaDocument($aNodesToIgnore, $aAttributes);
- return $oDelta->saveXML();
- }
-
- /**
- * Searches on disk in the root directories for module description files
- * and returns an array of MFModules
- * @return array Array of MFModules
- */
- public function FindModules()
- {
- $aAvailableModules = ModuleDiscovery::GetAvailableModules($this->aRootDirs);
- $aResult = array();
- foreach($aAvailableModules as $sId => $aModule)
- {
- $oModule = new MFModule($sId, $aModule['root_dir'], $aModule['label'], isset($aModule['auto_select']));
- if (isset($aModule['auto_select']))
- {
- $oModule->SetAutoSelect($aModule['auto_select']);
- }
- if (isset($aModule['datamodel']) && is_array($aModule['datamodel']))
- {
- $oModule->SetFilesToInclude($aModule['datamodel'], 'business');
- }
- if (isset($aModule['webservice']) && is_array($aModule['webservice']))
- {
- $oModule->SetFilesToInclude($aModule['webservice'], 'webservices');
- }
- if (isset($aModule['addons']) && is_array($aModule['addons']))
- {
- $oModule->SetFilesToInclude($aModule['addons'], 'addons');
- }
- $aResult[] = $oModule;
- }
- return $aResult;
- }
-
- public function TestAlteration()
- {
- try
- {
- $sHeader = '<?xml version="1.0" encoding="utf-8"?'.'>';
- $sOriginalXML =
- <<<EOF
- $sHeader
- <itop_design>
- <a id="first a">
- <b>Text</b>
- <c id="1">
- <d>D1</d>
- <d>D2</d>
- </c>
- </a>
- <a id="second a">
- <parent>first a</parent>
- </a>
- <a id="third a">
- <parent>first a</parent>
- <x>blah</x>
- </a>
- </itop_design>
- EOF;
- $this->oDOMDocument = new MFDocument();
- $this->oDOMDocument->loadXML($sOriginalXML);
-
- // DOM Get the original values, then modify its contents by the mean of the API
- $oRoot = $this->GetNodes('//itop_design')->item(0);
- //$oRoot->Dump();
- $sDOMOriginal = $oRoot->Dump(true);
-
- $oNode = $oRoot->GetNodes('a/b')->item(0);
- $oNew = $this->oDOMDocument->CreateElement('b', 'New text');
- $oNode->parentNode->RedefineChildNode($oNew);
-
- $oNode = $oRoot->GetNodes('a/c')->item(0);
- $oNewC = $this->oDOMDocument->CreateElement('c');
- $oNewC->setAttribute('id', '1');
- $oNode->parentNode->RedefineChildNode($oNewC);
- $oNewC->appendChild($this->oDOMDocument->CreateElement('d', 'x'));
- $oNewC->appendChild($this->oDOMDocument->CreateElement('d', 'y'));
- $oNewC->appendChild($this->oDOMDocument->CreateElement('d', 'z'));
- $oNamedNode = $this->oDOMDocument->CreateElement('z');
- $oNamedNode->setAttribute('id', 'abc');
- $oNewC->AddChildNode($oNamedNode);
- $oNewC->AddChildNode($this->oDOMDocument->CreateElement('r', 'to be replaced'));
- // Alter this "modified node", no flag should be set in its subnodes
- $oNewC->Rename('blah');
- $oNewC->Rename('foo');
- $oNewC->AddChildNode($this->oDOMDocument->CreateElement('y', '(no flag)'));
- $oNewC->AddChildNode($this->oDOMDocument->CreateElement('x', 'To delete programatically'));
- $oSubNode = $oNewC->GetUniqueElement('z');
- $oSubNode->Rename('abcdef');
- $oSubNode = $oNewC->GetUniqueElement('x');
- $oSubNode->Delete();
- $oNewC->RedefineChildNode($this->oDOMDocument->CreateElement('r', 'replacement'));
- $oNode = $oRoot->GetNodes("//a[@id='second a']")->item(0);
- $oNode->Rename('el 2o A');
- $oNode->Rename('el secundo A');
- $oNew = $this->oDOMDocument->CreateElement('e', 'Something new here');
- $oNode->AddChildNode($oNew);
- $oNewA = $this->oDOMDocument->CreateElement('a');
- $oNewA->setAttribute('id', 'new a');
- $oNode->AddChildNode($oNewA);
- $oSubnode = $this->oDOMDocument->CreateElement('parent', 'el secundo A');
- $oSubnode->setAttribute('id', 'to be changed');
- $oNewA->AddChildNode($oSubnode);
- $oNewA->AddChildNode($this->oDOMDocument->CreateElement('f', 'Welcome to the newcomer'));
- $oNewA->AddChildNode($this->oDOMDocument->CreateElement('x', 'To delete programatically'));
-
- // Alter this "new a", as it is new, no flag should be set
- $oNewA->Rename('new_a');
- $oSubNode = $oNewA->GetUniqueElement('parent');
- $oSubNode->Rename('alter ego');
- $oNewA->RedefineChildNode($this->oDOMDocument->CreateElement('f', 'dummy data'));
- $oSubNode = $oNewA->GetUniqueElement('x');
- $oSubNode->Delete();
-
- $oNode = $oRoot->GetNodes("//a[@id='third a']")->item(0);
- $oNode->Delete();
-
- //$oRoot->Dump();
- $sDOMModified = $oRoot->Dump(true);
- // Compute the delta
- //
- $sDeltaXML = $this->GetDelta();
- //echo "<pre>\n";
- //echo htmlentities($sDeltaXML);
- //echo "</pre>\n";
-
- // Reiterating - try to remake the DOM by applying the computed delta
- //
- $this->oDOMDocument = new MFDocument();
- $this->oDOMDocument->loadXML($sOriginalXML);
- $oRoot = $this->GetNodes('//itop_design')->item(0);
- //$oRoot->Dump();
- echo "<h4>Rebuild the DOM - Delta applied...</h4>\n";
- $oDeltaDoc = new MFDocument();
- $oDeltaDoc->loadXML($sDeltaXML);
-
- //$oDeltaDoc->Dump();
- //$this->oDOMDocument->Dump();
- $oDeltaRoot = $oDeltaDoc->childNodes->item(0);
- $this->LoadDelta($oDeltaRoot, $this->oDOMDocument);
- //$oRoot->Dump();
- $sDOMRebuilt = $oRoot->Dump(true);
- }
- catch (Exception $e)
- {
- echo "<h1>Exception: ".$e->getMessage()."</h1>\n";
- echo "<pre>\n";
- debug_print_backtrace();
- echo "</pre>\n";
- }
- $sArrStyle = "font-size: 40;";
- echo "<table>\n";
- echo " <tr>\n";
- echo " <td width=\"50%\">\n";
- echo " <h4>DOM - Original values</h4>\n";
- echo " <pre>".htmlentities($sDOMOriginal)."</pre>\n";
- echo " </td>\n";
- echo " <td width=\"50%\" align=\"left\" valign=\"center\"><span style=\"$sArrStyle\">⇒ ⇒ ⇒</span></td>\n";
- echo " </tr>\n";
- echo " <tr><td align=\"center\"><span style=\"$sArrStyle\">⇓</div></td><td align=\"center\"><span style=\"$sArrStyle\"><span style=\"$sArrStyle\">⇓</div></div></td></tr>\n";
- echo " <tr>\n";
- echo " <td width=\"50%\">\n";
- echo " <h4>DOM - Altered with various changes</h4>\n";
- echo " <pre>".htmlentities($sDOMModified)."</pre>\n";
- echo " </td>\n";
- echo " <td width=\"50%\">\n";
- echo " <h4>DOM - Rebuilt from the Delta</h4>\n";
- echo " <pre>".htmlentities($sDOMRebuilt)."</pre>\n";
- echo " </td>\n";
- echo " </tr>\n";
- echo " <tr><td align=\"center\"><span style=\"$sArrStyle\">⇓</div></td><td align=\"center\"><span style=\"$sArrStyle\">⇑</div></td></tr>\n";
- echo " <td width=\"50%\">\n";
- echo " <h4>Delta (Computed by ModelFactory)</h4>\n";
- echo " <pre>".htmlentities($sDeltaXML)."</pre>\n";
- echo " </td>\n";
- echo " <td width=\"50%\" align=\"left\" valign=\"center\"><span style=\"$sArrStyle\">⇒ ⇒ ⇒</span></td>\n";
- echo " </tr>\n";
- echo "</table>\n";
- } // TEST !
- /**
- * Extracts some nodes from the DOM
- * @param string $sXPath A XPath expression
- * @return DOMNodeList
- */
- public function GetNodes($sXPath, $oContextNode = null, $bSafe = true)
- {
- return $this->oDOMDocument->GetNodes($sXPath, $oContextNode, $bSafe);
- }
- }
- /**
- * Allow the setup page to load and perform its checks (including the check about the required extensions)
- */
- if (!class_exists('DOMElement'))
- {
- class DOMElement {function __construct(){throw new Exception('The dom extension is not enabled');}}
- }
- /**
- * MFElement: helper to read/change the DOM
- * @package ModelFactory
- */
- class MFElement extends Combodo\iTop\DesignElement
- {
- /**
- * Extracts some nodes from the DOM
- * @param string $sXPath A XPath expression
- * @return DOMNodeList
- */
- public function GetNodes($sXPath, $bSafe = true)
- {
- return $this->ownerDocument->GetNodes($sXPath, $this, $bSafe);
- }
-
- /**
- * Extracts some nodes from the DOM (active nodes only !!!)
- * @param string $sXPath A XPath expression
- * @return DOMNodeList
- */
- public function GetNodeById($sXPath, $sId)
- {
- return $this->ownerDocument->GetNodeById($sXPath, $sId, $this);
- }
- /**
- * Returns the node directly under the given node
- */
- public function GetUniqueElement($sTagName, $bMustExist = true)
- {
- $oNode = null;
- foreach($this->childNodes as $oChildNode)
- {
- if (($oChildNode->nodeName == $sTagName) && (($oChildNode->getAttribute('_alteration') != 'removed')))
- {
- $oNode = $oChildNode;
- break;
- }
- }
- if ($bMustExist && is_null($oNode))
- {
- throw new DOMFormatException('Missing unique tag: '.$sTagName);
- }
- return $oNode;
- }
-
- /**
- * Assumes the current node to be either a text or
- * <items>
- * <item [key]="..."]>value<item>
- * <item [key]="..."]>value<item>
- * </items>
- * where value can be the either a text or an array of items... recursively
- * Returns a PHP array
- */
- public function GetNodeAsArrayOfItems($sElementName = 'items')
- {
- $oItems = $this->GetOptionalElement($sElementName);
- if ($oItems)
- {
- $res = array();
- $aRanks = array();
- foreach($oItems->childNodes as $oItem)
- {
- if ($oItem instanceof DOMElement)
- {
- // When an attribute is missing
- if ($oItem->hasAttribute('id'))
- {
- $key = $oItem->getAttribute('id');
- if (array_key_exists($key, $res))
- {
- // Houston!
- throw new DOMFormatException("id '$key' already used", null, null, $oItem);
- }
- $res[$key] = $oItem->GetNodeAsArrayOfItems();
- }
- else
- {
- $res[] = $oItem->GetNodeAsArrayOfItems();
- }
- $sRank = $oItem->GetChildText('rank');
- if ($sRank != '')
- {
- $aRanks[] = (float) $sRank;
- }
- else
- {
- $aRanks[] = count($aRanks) > 0 ? max($aRanks) + 1 : 0;
- }
- array_multisort($aRanks, $res);
- }
- }
- }
- else
- {
- $res = $this->GetText();
- }
- return $res;
- }
- public function SetNodeAsArrayOfItems($aList)
- {
- $oNewNode = $this->ownerDocument->CreateElement($this->tagName);
- if ($this->getAttribute('id') != '')
- {
- $oNewNode->setAttribute('id', $this->getAttribute('id'));
- }
- self::AddItemToNode($this->ownerDocument, $oNewNode, $aList);
- $this->parentNode->RedefineChildNode($oNewNode);
- }
-
- protected static function AddItemToNode($oXmlDoc, $oXMLNode, $itemValue)
- {
- if (is_array($itemValue))
- {
- $oXmlItems = $oXmlDoc->CreateElement('items');
- $oXMLNode->AppendChild($oXmlItems);
-
- foreach($itemValue as $key => $item)
- {
- $oXmlItem = $oXmlDoc->CreateElement('item');
- $oXmlItems->AppendChild($oXmlItem);
-
- if (is_string($key))
- {
- $oXmlItem->SetAttribute('key', $key);
- }
- self::AddItemToNode($oXmlDoc, $oXmlItem, $item);
- }
- }
- else
- {
- $oXmlText = $oXmlDoc->CreateTextNode((string) $itemValue);
- $oXMLNode->AppendChild($oXmlText);
- }
- }
- /**
- * Helper to remove child nodes
- */
- protected function DeleteChildren()
- {
- while (isset($this->firstChild))
- {
- if ($this->firstChild instanceof MFElement)
- {
- $this->firstChild->DeleteChildren();
- }
- $this->removeChild($this->firstChild);
- }
- }
- /**
- * Find the child node matching the given node.
- * UNSAFE: may return nodes marked as _alteration="removed"
- * A method with the same signature MUST exist in MFDocument for the recursion to work fine
- * @param MFElement $oRefNode The node to search for
- * @param string $sSearchId substitutes to the value of the 'id' attribute
- */
- public function _FindChildNode(MFElement $oRefNode, $sSearchId = null)
- {
- return self::_FindNode($this, $oRefNode, $sSearchId);
- }
-
- /**
- * Find the child node matching the given node under the specified parent.
- * UNSAFE: may return nodes marked as _alteration="removed"
- * @param DOMNode $oParent
- * @param MFElement $oRefNode
- * @param string $sSearchId
- * @throws Exception
- */
- public static function _FindNode(DOMNode $oParent, MFElement $oRefNode, $sSearchId = null)
- {
- $oRes = null;
- if ($oParent instanceof DOMDocument)
- {
- $oDoc = $oParent->firstChild->ownerDocument;
- $oRoot = $oParent;
- }
- else
- {
- $oDoc = $oParent->ownerDocument;
- $oRoot = $oParent;
- }
- $oXPath = new DOMXPath($oDoc);
- if ($oRefNode->hasAttribute('id'))
- {
- // Find the first element having the same tag name and id
- if (!$sSearchId)
- {
- $sSearchId = $oRefNode->getAttribute('id');
- }
- $sXPath = './'.$oRefNode->tagName."[@id='$sSearchId']";
-
- $oRes = $oXPath->query($sXPath, $oRoot)->item(0);
- }
- else
- {
- // Get the first one having the same tag name (ignore others)
- $sXPath = './'.$oRefNode->tagName;
-
- $oRes = $oXPath->query($sXPath, $oRoot)->item(0);
- }
- return $oRes;
- }
- /**
- * Check if the current node is under a node 'added' or 'altered'
- * Usage: In such a case, the change must not be tracked
- */
- public function IsInDefinition()
- {
- // Iterate through the parents: reset the flag if any of them has a flag set
- for($oParent = $this ; $oParent instanceof MFElement ; $oParent = $oParent->parentNode)
- {
- if ($oParent->getAttribute('_alteration') != '')
- {
- return true;
- }
- }
- return false;
- }
- /**
- * Check if the given node is (a child of a node) altered by one of the supplied modules
- * @param array $aModules The list of module codes to consider
- * @return boolean
- */
- public function IsAlteredByModule($aModules)
- {
- // Iterate through the parents: reset the flag if any of them has a flag set
- for($oParent = $this ; $oParent instanceof MFElement ; $oParent = $oParent->parentNode)
- {
- if (in_array($oParent->getAttribute('_altered_in'), $aModules))
- {
- return true;
- }
- }
- return false;
- }
-
- static $aTraceAttributes = null;
- /**
- * Enable/disable the trace on changed nodes
- *
- *@param aAttributes array Array of attributes (key => value) to be added onto any changed node
- */
- static public function SetTrace($aAttributes = null)
- {
- self::$aTraceAttributes = $aAttributes;
- }
- /**
- * Mark the node as touched (if tracing is active)
- */
- public function AddTrace()
- {
- if (!is_null(self::$aTraceAttributes))
- {
- foreach (self::$aTraceAttributes as $sAttribute => $value)
- {
- $this->setAttribute($sAttribute, $value);
- }
- }
- }
- /**
- * Add a node and set the flags that will be used to compute the delta
- * @param MFElement $oNode The node (including all subnodes) to add
- */
- public function AddChildNode(MFElement $oNode)
- {
- // First: cleanup any flag behind the new node, and eventually add trace data
- $oNode->ApplyChanges();
- $oNode->AddTrace();
- $oExisting = $this->_FindChildNode($oNode);
- if ($oExisting)
- {
- if ($oExisting->getAttribute('_alteration') != 'removed')
- {
- $sPath = MFDocument::GetItopNodePath($oNode);
- $iLine = $oNode->getLineNo();
- throw new MFException($sPath.' at line '.$iLine.": could not be added (already exists)", MFException::COULD_NOT_BE_ADDED, $iLine, $sPath);
- }
- $oExisting->ReplaceWith($oNode);
- $sFlag = 'replaced';
- }
- else
- {
- $this->appendChild($oNode);
- $sFlag = 'added';
- }
- if (!$this->IsInDefinition())
- {
- $oNode->setAttribute('_alteration', $sFlag);
- }
- }
- /**
- * Modify a node and set the flags that will be used to compute the delta
- * @param MFElement $oNode The node (including all subnodes) to set
- */
- public function RedefineChildNode(MFElement $oNode, $sSearchId = null)
- {
- // First: cleanup any flag behind the new node, and eventually add trace data
- $oNode->ApplyChanges();
- $oNode->AddTrace();
- $oExisting = $this->_FindChildNode($oNode, $sSearchId);
- if (!$oExisting)
- {
- $sPath = MFDocument::GetItopNodePath($this)."/".$oNode->tagName.(empty($sSearchId) ? '' : "[$sSearchId]");
- $iLine = $oNode->getLineNo();
- throw new MFException($sPath." at line $iLine: could not be modified (not found)", MFException::COULD_NOT_BE_MODIFIED_NOT_FOUND, $sPath, $iLine);
- }
- $sPrevFlag = $oExisting->getAttribute('_alteration');
- if ($sPrevFlag == 'removed')
- {
- $sPath = MFDocument::GetItopNodePath($this)."/".$oNode->tagName.(empty($sSearchId) ? '' : "[$sSearchId]");
- $iLine = $oNode->getLineNo();
- $sSourceNode = MFDocument::GetItopNodePath($this)."/".$oNode->tagName.(is_null($sSearchId) ? '' : "[$sSearchId]").' at line '.$this->getLineNo();
- throw new MFException($sPath." at line $iLine: could not be modified (marked as deleted)", MFException::COULD_NOT_BE_MODIFIED_ALREADY_DELETED, $sPath, $iLine);
- }
- $oExisting->ReplaceWith($oNode);
- if (!$this->IsInDefinition())
- {
- if ($sPrevFlag == '')
- {
- $sPrevFlag = 'replaced';
- }
- $oNode->setAttribute('_alteration', $sPrevFlag);
- }
- }
- /**
- * Combination of AddChildNode or RedefineChildNode... it depends
- * This should become the preferred way of doing things (instead of implementing a test + the call to one of the APIs!
- * @param MFElement $oNode The node (including all subnodes) to set
- * @param string $sSearchId Optional Id of the node to SearchMenuNode
- * @param bool $bForce Force mode to dynamically add or replace nodes
- */
- public function SetChildNode(MFElement $oNode, $sSearchId = null, $bForce = false)
- {
- // First: cleanup any flag behind the new node, and eventually add trace data
- $oNode->ApplyChanges();
- $oNode->AddTrace();
- $oExisting = $this->_FindChildNode($oNode, $sSearchId);
- if ($oExisting)
- {
- $sPrevFlag = $oExisting->getAttribute('_alteration');
- if ($sPrevFlag == 'removed')
- {
- $sFlag = $bForce ? 'forced': 'replaced';
- }
- else
- {
- $sFlag = $sPrevFlag; // added, replaced or ''
- }
- $oExisting->ReplaceWith($oNode);
- }
- else
- {
- $this->appendChild($oNode);
- $sFlag = $bForce ? 'forced': 'added';
- }
- if (!$this->IsInDefinition())
- {
- if ($sFlag == '')
- {
- $sFlag = $bForce ? 'forced': 'replaced';
- }
- $oNode->setAttribute('_alteration', $sFlag);
- }
- }
- /**
- * Check that the current node is actually a class node, under classes
- */
- public function IsClassNode()
- {
- if ($this->tagName == 'class')
- {
- if (($this->parentNode->tagName == 'classes') && ($this->parentNode->parentNode->tagName == 'itop_design') ) // Beware: classes/class also exists in the group definition
- {
- return true;
- }
- return $this->parentNode->IsClassNode();
- }
- else
- {
- return false;
- }
- }
- /**
- * Replaces a node by another one, making sure that recursive nodes are preserved
- * @param MFElement $oNewNode The replacement
- */
- protected function ReplaceWith($oNewNode)
- {
- // Move the classes from the old node into the new one
- if ($this->IsClassNode())
- {
- foreach($this->GetNodes('class') as $oChild)
- {
- $oNewNode->appendChild($oChild);
- }
- }
- $oParentNode = $this->parentNode;
- $oParentNode->replaceChild($oNewNode, $this);
- }
- /**
- * Remove a node and set the flags that will be used to compute the delta
- */
- public function Delete()
- {
- $oParent = $this->parentNode;
- switch ($this->getAttribute('_alteration'))
- {
- case 'replaced':
- $sFlag = 'removed';
- break;
- case 'added':
- case 'needed':
- $sFlag = null;
- break;
- case 'removed':
- throw new Exception("Attempting to remove a deleted node: $this->tagName (id: ".$this->getAttribute('id')."");
- default:
- $sFlag = 'removed';
- if ($this->IsInDefinition())
- {
- $sFlag = null;
- break;
- }
- }
- if ($sFlag)
- {
- $this->setAttribute('_alteration', $sFlag);
- $this->DeleteChildren();
- // Add trace data
- $this->AddTrace();
- }
- else
- {
- // Remove the node entirely
- $this->parentNode->removeChild($this);
- }
- }
- /**
- * Merge the current node into the given container
- *
- * @param DOMNode $oContainer An element or a document
- * @param string $sSearchId The id to consider (could be blank)
- * @param bool $bMustExist Throw an exception if the node must already be found (and not marked as deleted!)
- * @param bool $bIfExists Return null if the node does not exists (or is marked as deleted)
- * @return DOMNode|null
- */
- public function MergeInto($oContainer, $sSearchId, $bMustExist, $bIfExists = false)
- {
- $oTargetNode = $oContainer->_FindChildNode($this, $sSearchId);
- if ($oTargetNode)
- {
- if ($oTargetNode->getAttribute('_alteration') == 'removed')
- {
- if ($bMustExist)
- {
- throw new Exception(MFDocument::GetItopNodePath($this).' at line '.$this->getLineNo().": could not be found (marked as deleted)");
- }
- // Beware: ImportNode(xxx, false) DOES NOT copy the node's attribute on *some* PHP versions (<5.2.17)
- // So use this workaround to import a node and its attributes on *any* PHP version
- $oTargetNode = $oContainer->ownerDocument->ImportNode($this->cloneNode(false), true);
- $oContainer->AddChildNode($oTargetNode);
- }
- }
- else
- {
- if ($bMustExist)
- {
- echo "Dumping parent node<br/>\n";
- $oContainer->Dump();
- throw new Exception(MFDocument::GetItopNodePath($this).' at line '.$this->getLineNo().": could not be found");
- }
- if (!$bIfExists)
- {
- // Beware: ImportNode(xxx, false) DOES NOT copy the node's attribute on *some* PHP versions (<5.2.17)
- // So use this workaround to import a node and its attributes on *any* PHP version
- $oTargetNode = $oContainer->ownerDocument->ImportNode($this->cloneNode(false), true);
- $oContainer->AddChildNode($oTargetNode);
- }
- }
- return $oTargetNode;
- }
- /**
- * Renames a node and set the flags that will be used to compute the delta
- * @param String $sNewId The new id
- */
- public function Rename($sId)
- {
- if (($this->getAttribute('_alteration') == 'replaced') || !$this->IsInDefinition())
- {
- $sOriginalId = $this->getAttribute('_old_id');
- if ($sOriginalId == '')
- {
- $this->setAttribute('_old_id', $this->getAttribute('id'));
- }
- else if($sOriginalId == $sId)
- {
- $this->removeAttribute('_old_id');
- }
- }
- $this->setAttribute('id', $sId);
- // Leave a trace of this change
- $this->AddTrace();
- }
- /**
- * Apply extensibility rules onto this node
- * @param array aRules Array of rules (strings)
- * @return void
- */
- public function RestrictExtensibility($aRules)
- {
- $oRulesNode = $this->GetOptionalElement('rules');
- if ($oRulesNode)
- {
- $aCurrentRules = $oRulesNode->GetNodeAsArrayOfItems();
- $aCurrentRules = array_merge($aCurrentRules, $aRules);
- $oRulesNode->SetNodeAsArrayOfItems($aCurrentRules);
- }
- else
- {
- $oNewNode = $this->ownerDocument->CreateElement('rules');
- $this->appendChild($oNewNode);
- $oNewNode->SetNodeAsArrayOfItems($aRules);
- }
- }
- /**
- * Read extensibility rules for this node
- * @return Array of rules (strings)
- */
- public function GetExtensibilityRules()
- {
- $aCurrentRules = array();
- $oRulesNode = $this->GetOptionalElement('rules');
- if ($oRulesNode)
- {
- $aCurrentRules = $oRulesNode->GetNodeAsArrayOfItems();
- }
- return $aCurrentRules;
- }
- /**
- * List changes below a given node (see also MFDocument::ListChanges)
- */
- public function ListChanges()
- {
- // Note: omitting the dot will make the query be global to the whole document!!!
- return $this->ownerDocument->GetNodes('.//*[@_alteration or @_old_id]', $this, false);
- }
- /**
- * List changes below a given node (see also MFDocument::ApplyChanges)
- */
- public function ApplyChanges()
- {
- $oNodes = $this->ListChanges();
- foreach($oNodes as $oNode)
- {
- $sOperation = $oNode->GetAttribute('_alteration');
- switch($sOperation)
- {
- case 'added':
- case 'replaced':
- case 'needed':
- // marked as added or modified, just reset the flag
- $oNode->removeAttribute('_alteration');
- break;
-
- case 'removed':
- // marked as deleted, let's remove the node from the tree
- $oNode->parentNode->removeChild($oNode);
- break;
- }
- if ($oNode->hasAttribute('_old_id'))
- {
- $oNode->removeAttribute('_old_id');
- }
- }
- }
- }
- /**
- * Allow the setup page to load and perform its checks (including the check about the required extensions)
- */
- if (!class_exists('DOMDocument'))
- {
- class DOMDocument {function __construct(){throw new Exception('The dom extension is not enabled');}}
- }
- /**
- * MFDocument - formating rules for XML input/output
- * @package ModelFactory
- */
- class MFDocument extends \Combodo\iTop\DesignDocument
- {
- /**
- * Overloadable. Called prior to data loading.
- */
- protected function Init()
- {
- parent::Init();
- $this->registerNodeClass('DOMElement', 'MFElement');
- }
- /**
- * Overload the standard API
- */
- public function saveXML(DOMNode $node = null, $options = 0)
- {
- $oRootNode = $this->firstChild;
- if (!$oRootNode)
- {
- $oRootNode = $this->createElement('itop_design'); // make sure that the document is not empty
- $oRootNode->setAttribute('xmlns:xsi', "http://www.w3.org/2001/XMLSchema-instance");
- $oRootNode->setAttribute('version', ITOP_DESIGN_LATEST_VERSION);
- $this->appendChild($oRootNode);
- }
- return parent::saveXML($node);
- }
-
- /**
- * Overload createElement to make sure (via new DOMText) that the XML entities are
- * always properly escaped
- * (non-PHPdoc)
- * @see DOMDocument::createElement()
- */
- function createElement($sName, $value = null, $namespaceURI = null)
- {
- $oElement = $this->importNode(new MFElement($sName, null, $namespaceURI));
- if (($value !== '') && ($value !== null))
- {
- $oElement->appendChild(new DOMText($value));
- }
- return $oElement;
- }
- /**
- * Find the child node matching the given node
- * A method with the same signature MUST exist in MFElement for the recursion to work fine
- * @param MFElement $oRefNode The node to search for
- * @param string $sSearchId substitutes to the value of the 'id' attribute
- */
- public function _FindChildNode(MFElement $oRefNode, $sSearchId = null)
- {
- return MFElement::_FindNode($this, $oRefNode, $sSearchId);
- }
- /**
- * Extracts some nodes from the DOM
- * @param string $sXPath A XPath expression
- * @return DOMNodeList
- */
- public function GetNodes($sXPath, $oContextNode = null, $bSafe = true)
- {
- $oXPath = new DOMXPath($this);
- if ($bSafe)
- {
- $sXPath .= "[not(@_alteration) or @_alteration!='removed']";
- }
-
- if (is_null($oContextNode))
- {
- $oResult = $oXPath->query($sXPath);
- }
- else
- {
- $oResult = $oXPath->query($sXPath, $oContextNode);
- }
- return $oResult;
- }
-
- public function GetNodeById($sXPath, $sId, $oContextNode = null)
- {
- $oXPath = new DOMXPath($this);
- $sQuotedId = self::XPathQuote($sId);
- $sXPath .= "[@id=$sQuotedId and(not(@_alteration) or @_alteration!='removed')]";
-
- if (is_null($oContextNode))
- {
- return $oXPath->query($sXPath);
- }
- else
- {
- return $oXPath->query($sXPath, $oContextNode);
- }
- }
- }
- /**
- * Helper class manage parameters stored as XML nodes
- * to be converted to a PHP structure during compilation
- * Values can be either a hash, an array, a string, a boolean, an int or a float
- */
- class MFParameters
- {
- protected $aData = null;
- public function __construct(DOMNode $oNode)
- {
- $this->aData = array();
- $this->LoadFromDOM($oNode);
- }
- public function Get($sCode, $default = '')
- {
- if (array_key_exists($sCode, $this->aData))
- {
- return $this->aData[$sCode];
- }
- return $default;
- }
- public function GetAll()
- {
- return $this->aData;
- }
- public function LoadFromDOM(DOMNode $oNode)
- {
- $this->aData = array();
- foreach($oNode->childNodes as $oChildNode)
- {
- if ($oChildNode instanceof DOMElement)
- {
- $this->aData[$oChildNode->nodeName] = $this->ReadElement($oChildNode);
- }
- }
- }
- protected function ReadElement(DOMNode $oNode)
- {
- if ($oNode instanceof DOMElement)
- {
- $sDefaultNodeType = ($this->HasChildNodes($oNode)) ? 'hash' : 'string';
- $sNodeType = $oNode->getAttribute('type');
- if ($sNodeType == '')
- {
- $sNodeType = $sDefaultNodeType;
- }
- switch($sNodeType)
- {
- case 'array':
- $value = array();
- // Treat the current element as zero based array, child tag names are NOT meaningful
- $sFirstTagName = null;
- foreach($oNode->childNodes as $oChildElement)
- {
- if ($oChildElement instanceof DOMElement)
- {
- if ($sFirstTagName == null)
- {
- $sFirstTagName = $oChildElement->nodeName;
- }
- else if ($sFirstTagName != $oChildElement->nodeName)
- {
- throw new Exception("Invalid Parameters: mixed tags ('$sFirstTagName' and '".$oChildElement->nodeName."') inside array '".$oNode->nodeName."'");
- }
- $val = $this->ReadElement($oChildElement);
- // No specific Id, just push the value at the end of the array
- $value[] = $val;
- }
- }
- ksort($value, SORT_NUMERIC);
- break;
-
- case 'hash':
- $value = array();
- // Treat the current element as a hash, child tag names are keys
- foreach($oNode->childNodes as $oChildElement)
- {
- if ($oChildElement instanceof DOMElement)
- {
- if (array_key_exists($oChildElement->nodeName, $value))
- {
- throw new Exception("Invalid Parameters file: duplicate tags '".$oChildElement->nodeName."' inside hash '".$oNode->nodeName."'");
- }
- $val = $this->ReadElement($oChildElement);
- $value[$oChildElement->nodeName] = $val;
- }
- }
- break;
-
- case 'int':
- case 'integer':
- $value = (int)$this->GetText($oNode);
- break;
-
- case 'bool':
- case 'boolean':
- if (($this->GetText($oNode) == 'true') || ($this->GetText($oNode) == 1))
- {
- $value = true;
- }
- else
- {
- $value = false;
- }
- break;
-
- case 'string':
- default:
- $value = str_replace('\n', "\n", (string)$this->GetText($oNode));
- }
- }
- else if ($oNode instanceof DOMText)
- {
- $value = $oNode->wholeText;
- }
- return $value;
- }
- protected function GetAttribute($sAttName, $oNode, $sDefaultValue)
- {
- $sRet = $sDefaultValue;
- foreach($oNode->attributes as $oAttribute)
- {
- if ($oAttribute->nodeName == $sAttName)
- {
- $sRet = $oAttribute->nodeValue;
- break;
- }
- }
- return $sRet;
- }
- /**
- * Returns the TEXT of the current node (possibly from several subnodes)
- */
- public 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;
- }
- }
- /**
- * Check if a node has child nodes (apart from text nodes)
- */
- public function HasChildNodes($oNode)
- {
- if ($oNode instanceof DOMElement)
- {
- foreach($oNode->childNodes as $oChildNode)
- {
- if ($oChildNode instanceof DOMElement)
- {
- return true;
- }
- }
- }
- return false;
- }
- function Merge(XMLParameters $oTask)
- {
- //todo: clarify the usage of this function that CANNOT work
- $this->aData = $this->array_merge_recursive_distinct($this->aData, $oTask->aData);
- }
- /**
- * array_merge_recursive does indeed merge arrays, but it converts values with duplicate
- * keys to arrays rather than overwriting the value in the first array with the duplicate
- * value in the second array, as array_merge does. I.e., with array_merge_recursive,
- * this happens (documented behavior):
- *
- * array_merge_recursive(array('key' => 'org value'), array('key' => 'new value'));
- * => array('key' => array('org value', 'new value'));
- *
- * array_merge_recursive_distinct does not change the datatypes of the values in the arrays.
- * Matching keys' values in the second array overwrite those in the first array, as is the
- * case with array_merge, i.e.:
- *
- * array_merge_recursive_distinct(array('key' => 'org value'), array('key' => 'new value'));
- * => array('key' => array('new value'));
- *
- * Parameters are passed by reference, though only for performance reasons. They're not
- * altered by this function.
- *
- * @param array $array1
- * @param array $array2
- * @return array
- * @author Daniel <daniel (at) danielsmedegaardbuus (dot) dk>
- * @author Gabriel Sobrinho <gabriel (dot) sobrinho (at) gmail (dot) com>
- */
- protected function array_merge_recursive_distinct ( array &$array1, array &$array2 )
- {
- $merged = $array1;
- foreach ( $array2 as $key => &$value )
- {
- if ( is_array ( $value ) && isset ( $merged [$key] ) && is_array ( $merged [$key] ) )
- {
- $merged [$key] = $this->array_merge_recursive_distinct ( $merged [$key], $value );
- }
- else
- {
- $merged [$key] = $value;
- }
- }
- return $merged;
- }
- }
|