function debugHtml() { $sHtml = "<li>AnwDiffKept : "; $sHtml .= htmlentities(AnwUtils::xmlDumpNode($this->getNode())); $sHtml .= "</li>"; return $sHtml; }
function debugHtml() { $sHtml = "<li>AnwDiffMoved : "; if ($this->getMovedNode()) { $sHtml .= htmlentities(AnwUtils::xmlDumpNode($this->getMovedNode())); } else { $sHtml .= "***NOT SET***"; } $sHtml .= "(was " . htmlentities(AnwUtils::xmlDumpNode($this->getDiffDeleted()->getNode())) . ")"; $sHtml .= "</li>"; return $sHtml; }
private function exportData($aaExportPageGroups) { $oDoc = new DOMDocument("1.0", "UTF-8"); //put information as comment $sComment = ""; $sComment .= $this->t_("xmlcomment_info") . "\n"; $sComment .= ANWIKI_WEBSITE . "\n\n"; $sComment .= $this->t_("xmlcomment_time", array("time" => Anwi18n::dateTime(time()))) . "\n"; $sComment .= $this->t_("xmlcomment_version", array("version" => ANWIKI_VERSION_NAME)) . "\n"; $sComment .= $this->t_("xmlcomment_user", array("user" => AnwCurrentSession::getUser()->getLogin())) . "\n"; $sComment .= $this->t_("xmlcomment_from", array("url" => self::globalCfgUrlRoot())) . "\n\n"; $sComment .= $this->t_("xmlcomment_contents") . "\n"; //list exported contents as comment foreach ($aaExportPageGroups as $amPageGroup) { foreach ($amPageGroup['PAGES'] as $oPage) { $sPageTime = Anwi18n::dateTime($oPage->getTime()); $sComment .= ' * ' . $oPage->getName() . " (" . $oPage->getLang() . ") (" . $sPageTime . ")\n"; } } $sCommentSeparator = "\n**************************************************\n"; $sComment = " " . $sCommentSeparator . $sComment . $sCommentSeparator . " "; $oCommentNode = $oDoc->createComment($sComment); $oDoc->appendChild($oCommentNode); //end comment //<anwexport time="" origin=""> $oRootNode = $oDoc->createElement(self::XMLTAG_ROOT); $oRootNode->setAttribute("time", time()); $oRootNode->setAttribute("from", AnwXml::xmlFileAttributeEncode(self::globalCfgUrlRoot())); $oRootNode->setAttribute("version_id", ANWIKI_VERSION_ID); $oRootNode->setAttribute("version_name", AnwXml::xmlFileAttributeEncode(ANWIKI_VERSION_NAME)); $oDoc->appendChild($oRootNode); foreach ($aaExportPageGroups as $amPageGroup) { $oPageGroup = $amPageGroup['GROUP']; $sContentClassName = $oPageGroup->getContentClass()->getName(); //<anwpagegroup> $oPageGroupNode = $oDoc->createElement(self::XMLTAG_PAGEGROUP); $oPageGroupNode->setAttribute("contentclass", AnwXml::xmlFileAttributeEncode($sContentClassName)); foreach ($amPageGroup['PAGES'] as $oPage) { //add comment $sPageTime = Anwi18n::dateTime($oPage->getTime()); $sComment = $oPage->getName() . " (" . $oPage->getLang() . ") (" . $sPageTime . ") (" . $oPageGroup->getContentClass()->getLabel() . "/" . $sContentClassName . ")"; //$sComment = " \n*\n* ".$sComment."\n*\n "; $sCommentSeparator = "\n**************************************************\n"; $sComment = " \n\n" . $sCommentSeparator . $sComment . $sCommentSeparator . " "; $oCommentNode = $oDoc->createComment($sComment); $oPageGroupNode->appendChild($oCommentNode); //end comment //using a CDATA node to preserve source breaklines :-) //$sPageContent = $oPage->getContent()->toXml(); //$oPageContentNode = $oDoc->createCDATASection($sPageContent); $oContentNodeDoc = $oPage->getContent()->toXml()->documentElement; //here we got a <doc> node $oPageContentNodeDoc = $oDoc->importNode($oContentNodeDoc, true); //<anwpage name="" lang="" time=""> $oPageNode = $oDoc->createElement(self::XMLTAG_PAGE); $oPageNode->setAttribute("name", AnwXml::xmlFileAttributeEncode($oPage->getName())); $oPageNode->setAttribute("lang", AnwXml::xmlFileAttributeEncode($oPage->getLang())); $oPageNode->setAttribute("time", $oPage->getTime()); //we need to do this to squeeze the unwanted <doc> node in //WARNING - special loop ! childs are getting modified... while ($oChildNode = $oPageContentNodeDoc->childNodes->item(0)) { $oPageNode->appendChild($oChildNode); } $oPageGroupNode->appendChild($oPageNode); } $oRootNode->appendChild($oPageGroupNode); } $sReturn = AnwUtils::xmlDumpNode($oRootNode); // even if final XML structure may be broken due to undeclared namespaces used in content, // we let raw content as it is for better compatibility in later versions. // $sReturn = AnwXml::prepareXmlValueToXml($sReturn); return $sReturn; }
function debugHtml() { $sHtml = "<li>AnwDiffDeleted : "; if ($this->getMovedDiff()) { $sHtml .= "[MOVED]"; } if ($this->hasSubMovedDiff()) { $sHtml .= "(*) "; } $sHtml .= htmlentities(AnwUtils::xmlDumpNode($this->getNode())); if ($this->hasSubDeletedDiffs()) { $sHtml .= '<ul>Subdeleted:'; $aoSubDiffs = $this->getSubDeletedDiffs(); foreach ($aoSubDiffs as $oSubDiff) { $sHtml .= $oSubDiff->debugHtml(); } $sHtml .= '</ul>'; } $sHtml .= "</li>"; return $sHtml; }
function debugHtml() { $sHtml = "<li>AnwDiffAdded : "; if ($this->getMovedNode()) { $sHtml .= "[MOVED???]"; } if ($this->hasSubMovedNode()) { $sHtml .= "[SUBMOVED - <" . $this->getNode()->nodeName . ">]"; $sHtml .= "<ul>"; foreach ($this->aoSubAddedDiffs as $oSubDiff) { $sHtml .= $oSubDiff->debugHtml(); } $sHtml .= "</ul>"; } else { $sHtml .= htmlentities(AnwUtils::xmlDumpNode($this->getNode())); } $sHtml .= "</li>"; return $sHtml; }
function debugHtml() { $sHtml = "<li>AnwDiffs : <" . $this->getNodeRootNew()->nodeName . "><br/>"; $sHtml .= "from : " . htmlentities(AnwUtils::xmlDumpNode($this->oNodeRootOlder)) . "<br/>"; $sHtml .= "to : " . htmlentities(AnwUtils::xmlDumpNode($this->getNodeRootNew())) . "<br/>"; $sHtml .= "<ul>"; $aoDiffs = $this->getAllDiffs(); foreach ($aoDiffs as $oDiff) { $sHtml .= $oDiff->debugHtml(); } $sHtml .= "</ul></li>"; return $sHtml; }
function diffKept($oDiff) { $sContent = AnwUtils::xmlDumpNode($oDiff->getNode()); $HTML = <<<EOF \t<span>{$sContent}</span> EOF; return $HTML; }
static function xmlAreSimilarNodes($oNode1, $oNode2, $bTestEquals = true, $bTestAttributesValues = true, $bTestRootNodes = true, $bDebugOnError = false, $bDontTestRootNodeValue = false) { if ($bTestRootNodes) { if (!$bTestEquals && !self::xmlIsTranslatableParent($oNode1)) { $bTestEquals = true; } //text nodes if (self::xmlIsTextNode($oNode1) || self::xmlIsTextNode($oNode2)) { if (!self::xmlIsTextNode($oNode1) || !self::xmlIsTextNode($oNode2)) { //trying to compare uncomparable nodes if ($bDebugOnError) { AnwDebug::log("xmlAreSimilarNodes - false - textnode null"); } return false; } if ($bTestEquals && !$bDontTestRootNodeValue) { $bReturn = $oNode1->nodeValue == $oNode2->nodeValue; if (!$bReturn) { if ($bDebugOnError) { AnwDebug::log("xmlAreSimilarNodes - false - textnodes values"); } } return $bReturn; } return true; } //php nodes if (self::xmlIsPhpNode($oNode1) || self::xmlIsPhpNode($oNode2)) { if (!self::xmlIsPhpNode($oNode1) || !self::xmlIsPhpNode($oNode2)) { //trying to compare uncomparable nodes if ($bDebugOnError) { AnwDebug::log("xmlAreSimilarNodes - false - php null"); } return false; } //always test equality $bReturn = $oNode1->nodeValue == $oNode2->nodeValue; if (!$bReturn) { if ($bDebugOnError) { AnwDebug::log("xmlAreSimilarNodes - false - php values"); } } return $bReturn; } //comment nodes if (self::xmlIsCommentNode($oNode1) || self::xmlIsCommentNode($oNode2)) { if (!self::xmlIsCommentNode($oNode1) || !self::xmlIsCommentNode($oNode2)) { //trying to compare uncomparable nodes if ($bDebugOnError) { AnwDebug::log("xmlAreSimilarNodes - false - comment null"); } return false; } //always test equality $bReturn = $oNode1->nodeValue == $oNode2->nodeValue; if (!$bReturn) { if ($bDebugOnError) { AnwDebug::log("xmlAreSimilarNodes - false - comment values"); } } return $bReturn; } //compare node names if ($oNode1->nodeName != $oNode2->nodeName) { if ($bDebugOnError) { AnwDebug::log("xmlAreSimilarNodes - false - nodeNames"); } return false; } //compare attributes if ($oNode1->hasAttributes() || $oNode2->hasAttributes()) { if (!$oNode1->hasAttributes() || !$oNode2->hasAttributes()) { if ($bDebugOnError) { AnwDebug::log("xmlAreSimilarNodes - false - attribute count1"); } return false; } if (count($oNode1->attributes) != count($oNode2->attributes)) { if ($bDebugOnError) { AnwDebug::log("xmlAreSimilarNodes - false - attribute count2"); } return false; } //compare attributes values if ($bTestAttributesValues) { foreach ($oNode1->attributes as $sAttrName => $oAttr) { if ($oNode1->getAttribute($sAttrName) != $oNode2->getAttribute($sAttrName)) { if ($bDebugOnError) { AnwDebug::log("xmlAreSimilarNodes - false - attribute eq1"); } return false; } } //warning! for a strange reason, count of attributes + previous test are not sufficient to pass testXmlAreSimilarNodes3 foreach ($oNode2->attributes as $sAttrName => $oAttr) { if ($oNode1->getAttribute($sAttrName) != $oNode2->getAttribute($sAttrName)) { //print htmlentities(AnwUtils::xmlDumpNode($oNode1->parentNode));print '<hr/>'; //print htmlentities(AnwUtils::xmlDumpNode($oNode2->parentNode)); if ($bDebugOnError) { AnwDebug::log("xmlAreSimilarNodes - false - attribute eq2"); } return false; } } } } } //------------------------ //now, check childs if ($oNode1->hasChildNodes() || $oNode2->hasChildNodes()) { if (!$oNode1->hasChildNodes() || !$oNode2->hasChildNodes()) { if ($bDebugOnError) { AnwDebug::log("xmlAreSimilarNodes - false - childs null"); } return false; } $oChilds1 = $oNode1->childNodes; $oChilds2 = $oNode2->childNodes; if ($oChilds1->length != $oChilds2->length) { if ($bDebugOnError) { AnwDebug::log("xmlAreSimilarNodes - false - childs count" . htmlentities(AnwUtils::xmlDumpNode($oNode1) . " VS " . AnwUtils::xmlDumpNode($oNode2))); } return false; } if ($bDontTestRootNodeValue) { $bDontTestRootNodeValue = false; //we are now browsing childs, so we need to test their values... } $n = $oChilds1->length; for ($i = 0; $i < $n; $i++) { $oChild1 = $oChilds1->item($i); $oChild2 = $oChilds2->item($i); if (!self::xmlAreSimilarNodes($oChild1, $oChild2, $bTestEquals, $bTestAttributesValues, true, $bDebugOnError)) { if ($bDebugOnError) { AnwDebug::log("xmlAreSimilarNodes - false - childs recursive"); } return false; } } } return true; //return (AnwUtils::xmlDumpNode($oNode1) == AnwUtils::xmlDumpNode($oNode2)); }
private static function applyDiffsToTranslation($oRootNode, $oDiffs, $oContentField) { //AnwDebug::log("Applying diffs to : ".htmlentities(AnwUtils::xmlDumpNode($oRootNode))); $aoDiffs = $oDiffs->getAllDiffs(); $oChildNodes = $oRootNode->childNodes; $i = 0; foreach ($aoDiffs as $oDiff) { $oChild = $oChildNodes->item($i); if (!$oChild) { //AnwDebug::logdetail("oChild=null"); } AnwDebug::logdetail("//step dump: " . htmlentities(AnwUtils::xmlDumpNode($oRootNode))); $sDiffType = $oDiff->getDiffType(); $bSkipIncrement = false; switch ($sDiffType) { case AnwDiff::TYPE_ADDED: if ($oDiff->hasSubMovedNode()) { $oNodeRef = $oDiff->getNode(); $oNewNode = $oRootNode->ownerDocument->createElement($oNodeRef->nodeName); //no need to check for UnmodifiableBlockNodes, a comment/php node can't have submoved nodes AnwXml::xmlCopyNodeAttributes($oNodeRef, $oNewNode); //recursive call for SubAdded nodes AnwDebug::log(" * SUBADDED : " . htmlentities(AnwUtils::xmlDumpNode($oNewNode))); if ($oChild) { AnwDebug::logdetail("added->insertBefore : before=" . htmlentities(AnwUtils::xmlDumpNode($oChild))); $oRootNode->insertBefore($oNewNode, $oChild); } else { AnwDebug::logdetail("added->appendChild"); $oRootNode->appendChild($oNewNode); } //continue on subadded diffs self::applyDiffsToTranslation($oNewNode, $oDiff, $oContentField); } else { //quick test for special case with xmlIsUnmodifiableBlockNode $oTmpNode = $oDiff->getMovedNode() ? $this->getMovedNode() : $oDiff->getNode(); if (AnwXml::xmlIsUnmodifiableBlockNode($oTmpNode)) { //keep it unchanged $oNodeToImport = $oTmpNode; } else { //mark it as untranslated only if translatable (contentfield check) $bMarkAsUntranslated = AnwXml::xmlAreTranslatableAncestors($oRootNode, $oContentField); //TODO: move code from getNodeWithTranslateTags() here $oNodeToImport = $oDiff->getNodeWithTranslateTags($bMarkAsUntranslated, $oContentField); } $oNewNode = $oRootNode->ownerDocument->importNode($oNodeToImport, true); AnwDebug::log(" * ADDED : " . htmlentities(AnwUtils::xmlDumpNode($oNewNode))); if ($oChild) { AnwDebug::logdetail("added->insertBefore : before=" . htmlentities(AnwUtils::xmlDumpNode($oChild))); $oRootNode->insertBefore($oNewNode, $oChild); } else { AnwDebug::logdetail("added->appendChild"); $oRootNode->appendChild($oNewNode); } } break; case AnwDiff::TYPE_MOVED: //No need to import node as we get a node from the same document $oMovedNode = $oDiff->getMovedNode(); //tmp check if (!$oMovedNode) { //print AnwDebug::log('-----'.AnwUtils::xmlDumpNode($oDiff->getDiffDeleted()->getNode())); AnwDebug::log("****ERROR**** getMovedNode() returned NULL on AnwDiffMoved !"); throw new AnwUnexpectedException("ERROR getMovedNode() returned NULL on AnwDiffMoved !"); break; } AnwDebug::log(" * MOVED : " . htmlentities(AnwUtils::xmlDumpNode($oMovedNode))); //did the textlayout change? if ($oDiff->hasTextLayoutChanged()) { //we need to apply the new textlayout... $oMovedNode->nodeValue = AnwXml::xmlPreserveTextLayout($oMovedNode->nodeValue, $oDiff->getTextLayoutReferenceValue()); } //the following operations will MOVE the node into the document if ($oChild) { AnwDebug::log("added->insertBefore" . htmlentities(AnwUtils::xmlDumpNode($oMovedNode))); $oRootNode->insertBefore($oMovedNode, $oChild); } else { AnwDebug::logdetail("added->appendChild"); if (!$oRootNode->appendChild($oMovedNode)) { throw new AnwUnexpectedException("appendChild failed"); } } break; case AnwDiff::TYPE_DELETED: //if (!$oDiff->getMovedDiff()) //{ AnwDebug::logdetail(" * DELETED : " . htmlentities(AnwUtils::xmlDumpNode($oDiff->getNode())) . " == " . htmlentities(AnwUtils::xmlDumpNode($oChild))); //if (!$oRootNode->removeChild($oChild)) throw new AnwUnexpectedException("removeChild failed"); //$bSkipIncrement = true; // !!! We don't delete nodes now, because we may need it if it contains moved nodes... // these nodes will be deleted later ////self::$aoNodesMarkedForDeletion[] = array($oRootNode,$oChild); if (!$oRootNode->removeChild($oChild)) { throw new AnwUnexpectedException("removeChild failed"); } $bSkipIncrement = true; //} //else //{ // AnwDebug::log(" * DELETED : deletion skipped (node will be moved)"); //} break; case AnwDiff::TYPE_KEPT: //don't touch anything AnwDebug::log(" * KEPT : " . htmlentities(AnwUtils::xmlDumpNode($oDiff->getNode())) . " == " . htmlentities(AnwUtils::xmlDumpNode($oChild))); break; case AnwDiff::TYPE_EDITED: AnwDebug::log(" * EDITED : " . htmlentities(AnwUtils::xmlDumpNode($oChild)) . " == " . htmlentities(AnwUtils::xmlDumpNode($oDiff->getDiffAdded()->getNode()))); //TODO: warning, this code is very crappy and needs to be cleaned and tested //update attributes $oNodeRef = $oDiff->getDiffAdded()->getNode(); if (AnwXml::xmlIsUnmodifiableBlockNode($oChild)) { $oNewNode = AnwXml::xmlReplaceUnmodifiableBlockNode($oChild, $oNodeRef); } else { if (!AnwXml::xmlIsTextNode($oChild)) { throw new AnwUnexpectedException("not a text node in TYPE_EDITED"); } if ($oDiff->isEmpty() || !AnwXml::xmlAreTranslatableAncestors($oRootNode, $oContentField)) { AnwDebug::log("//edited : empty/untranslatable content, copying value but keeping it as translated"); //copy new value, but keep it as translated $oNewNode = AnwXml::xmlReplaceTextNode($oChild, $oNodeRef); } else { if ($oDiff->getDiffDeleted()->getMovedDiff()) { //special - consider it as an added node : set new value & mark this as untranslated AnwDebug::log("//edited : special case, considering as added node"); $oNewNode = AnwXml::xmlReplaceTextNode($oChild, $oNodeRef); $oNewNode = AnwXml::xmlSetTextUntranslated($oNewNode, true); } else { //content has really changed, mark it as untranslated if translatable (contentfield check) //current node is translatable... set new value & mark this as untranslated if ($oChild->nodeValue != $oNodeRef->nodeValue) { //we need to check if text has really changed, or if it's just some layout (spaces, breaklines...) which changed the nodeValue... $oNodeRefOld = $oDiff->getDiffDeleted()->getNode(); if (AnwXml::xmlTrimTextLayout($oNodeRefOld->nodeValue) == AnwXml::xmlTrimTextLayout($oNodeRef->nodeValue)) { AnwDebug::log("//edited : case 3.1, only textLayout has changed"); $sOldValue = $oChild->nodeValue; //content has not really changed, we just added/removed a space, tab, line break before/after the text value //ex: "blah blah\n" --> "blah blah\n\n" //we silently apply the new text layout, without turning it untranslated $oChild->nodeValue = AnwXml::xmlPreserveTextLayout($oChild->nodeValue, $oNodeRef->nodeValue); } else { AnwDebug::log("//edited : case 3.2, content has really changed"); $oNewNode = AnwXml::xmlReplaceTextNode($oChild, $oNodeRef); $oNewNode = AnwXml::xmlSetTextUntranslated($oNewNode, true); } } /* //current node is translatable... keep CURRENT value and mark it as untranslated $oNewNode = $oChild; if ($oNewNode->nodeValue != $oNodeRef->nodeValue) { $oNewNode = AnwXml::xmlSetTextUntranslated($oNewNode, true); }*/ } } } break; case AnwDiff::TYPE_DIFFS: $oNodeDiffRef = $oDiff->getNodeRootNew(); AnwDebug::log(" * DIFFS : " . htmlspecialchars(AnwUtils::xmlDumpNode($oNodeDiffRef)) . " == " . htmlentities(AnwUtils::xmlDumpNode($oChild))); //update attributes AnwXml::xmlCopyNodeAttributes($oNodeDiffRef, $oChild); //recursive call self::applyDiffsToTranslation($oChild, $oDiff, $oContentField); break; default: AnwDebug::log("ERROR - Unknown DiffType :" . $sDiffType); } //just to prevent erros... unset($oNewNode); unset($oNodeRef); if (!$bSkipIncrement) { //AnwDebug::logdetail(" * Loop i++"); $i++; //AnwDebug::logdetail("//step dump_end : ".htmlentities(AnwUtils::xmlDumpNode($oRootNode))); } //AnwDebug::log("Loop[$i] applydiffs intermediate result : ".htmlentities(AnwUtils::xmlDumpNode($oRootNode))); } //AnwDebug::log("applydiffs result : ".htmlentities(AnwUtils::xmlDumpNode($oRootNode))); return $oRootNode; }
function genTranslatableField_onUntranslatableNode($oRootNode) { $sHtml = AnwUtils::xmlDumpNode($oRootNode); $sHtml = AnwOutput::cleanFixTags($sHtml); $this->genTranslatableField_html .= '<span class="fixeditem">' . $sHtml . '</span>'; }