eZ Publish  [4.0]
ezdiffxmltextengine.php
Go to the documentation of this file.
00001 <?php
00002 //
00003 // Definition of eZDiffXMLTextEngine class
00004 //
00005 // <creation-tag>
00006 //
00007 // ## BEGIN COPYRIGHT, LICENSE AND WARRANTY NOTICE ##
00008 // SOFTWARE NAME: eZ Publish
00009 // SOFTWARE RELEASE: 4.0.x
00010 // COPYRIGHT NOTICE: Copyright (C) 1999-2008 eZ Systems AS
00011 // SOFTWARE LICENSE: GNU General Public License v2.0
00012 // NOTICE: >
00013 //   This program is free software; you can redistribute it and/or
00014 //   modify it under the terms of version 2.0  of the GNU General
00015 //   Public License as published by the Free Software Foundation.
00016 //
00017 //   This program is distributed in the hope that it will be useful,
00018 //   but WITHOUT ANY WARRANTY; without even the implied warranty of
00019 //   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00020 //   GNU General Public License for more details.
00021 //
00022 //   You should have received a copy of version 2.0 of the GNU General
00023 //   Public License along with this program; if not, write to the Free
00024 //   Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
00025 //   MA 02110-1301, USA.
00026 //
00027 //
00028 // ## END COPYRIGHT, LICENSE AND WARRANTY NOTICE ##
00029 //
00030 
00031 /*! \file ezdiffxmltextengine.php
00032   eZDiffXMLTextEngine class
00033 */
00034 
00035 /*!
00036   \class eZDiffXMLTextEngine ezdiffxmltextengine.php
00037   \ingroup eZDiff
00038   \brief This class creates a diff for xml text.
00039 */
00040 
00041 //include_once( 'lib/ezdiff/classes/ezdiffengine.php' );
00042 
00043 class eZDiffXMLTextEngine extends eZDiffEngine
00044 {
00045     function eZDiffXMLTextEngine()
00046     {
00047     }
00048 
00049     /*!
00050       This function calculates changes in xml text and creates an object to hold
00051       overview of changes.
00052     */
00053     function createDifferenceObject( $fromData, $toData )
00054     {
00055         //include_once( 'lib/ezdiff/classes/ezxmltextdiff.php' );
00056         //include_once( 'lib/ezdiff/classes/ezdifftextengine.php' );
00057         //include_once( 'lib/ezutils/classes/ezini.php' );
00058         //include_once( 'kernel/classes/datatypes/ezxmltext/handlers/input/ezsimplifiedxmleditoutput.php' );
00059 
00060         $changes = new eZXMLTextDiff();
00061         $contentINI = eZINI::instance( 'content.ini' );
00062         $useSimplifiedXML = $contentINI->variable( 'ContentVersionDiffSettings', 'UseSimplifiedXML' );
00063         $diffSimplifiedXML = ( $useSimplifiedXML == 'enabled' );
00064 
00065         $oldXMLTextObject = $fromData->content();
00066         $newXMLTextObject = $toData->content();
00067 
00068         $oldXML = $oldXMLTextObject->attribute( 'xml_data' );
00069         $newXML = $newXMLTextObject->attribute( 'xml_data' );
00070 
00071         $simplifiedXML = new eZSimplifiedXMLEditOutput();
00072 
00073         $domOld = new DOMDocument( '1.0', 'utf-8' );
00074         $domOld->preserveWhiteSpace = false;
00075         $domOld->loadXML( $oldXML );
00076 
00077         $domNew = new DOMDocument( '1.0', 'utf-8' );
00078         $domNew->preserveWhiteSpace = false;
00079         $domNew->loadXML( $newXML );
00080 
00081         $old = $simplifiedXML->performOutput( $domOld );
00082         $new = $simplifiedXML->performOutput( $domNew );
00083 
00084         if ( !$diffSimplifiedXML )
00085         {
00086             $old = trim( strip_tags( $old ) );
00087             $new = trim( strip_tags( $new ) );
00088 
00089             $pattern = array( '/[ ][ ]+/',
00090                               '/ \n( \n)+/',
00091                               '/^ /m',
00092                               '/(\n){3,}/' );
00093             $replace = array( ' ',
00094                               "\n",
00095                               '',
00096                               "\n\n" );
00097 
00098             $old = preg_replace( $pattern, $replace, $old );
00099             $new = preg_replace( $pattern, $replace, $new );
00100         }
00101 
00102         $oldArray = split( "\n", $old );
00103         $newArray = split( "\n", $new );
00104 
00105         $oldSums = array();
00106         foreach( $oldArray as $paragraph )
00107         {
00108             $oldSums[] = crc32( $paragraph );
00109         }
00110 
00111         $newSums = array();
00112         foreach( $newArray as $paragraph )
00113         {
00114             $newSums[] = crc32( $paragraph );
00115         }
00116 
00117         $textDiffer = new eZDiffTextEngine();
00118 
00119         $pre = $textDiffer->preProcess( $oldSums, $newSums );
00120         $out = $textDiffer->createOutput( $pre, $oldArray, $newArray );
00121         $changes->setChanges( $out );
00122         return $changes;
00123     }
00124 }
00125 
00126 ?>