• Main Page
  • Related Pages
  • Namespaces
  • Data Structures
  • Files
  • Examples
  • File List
  • Globals

cms/diff.lib.php

Go to the documentation of this file.
00001 <?php
00002 if(!defined('__PRAGYAN_CMS'))
00003 { 
00004         header($_SERVER['SERVER_PROTOCOL'].' 403 Forbidden');
00005         echo "<h1>403 Forbidden<h1><h4>You are not authorized to access the page.</h4>";
00006         echo '<hr/>'.$_SERVER['SERVER_SIGNATURE'];
00007         exit(1);
00008 }
00009 
00061 function diff( $original, $updated )
00062 {
00079     function print_diff( $c, $x, $y, $i, $j, $last = '', $change = false, $end_i = 0, $end_j = 0, $add_i = 0, $add_j = 0 )
00080     {
00081         $patch = '';
00082         if ( ( $i >= 0 ) && ( $j >= 0 ) && ( $x[$i] == $y[$j] ) ) {
00083             $patch .= print_diff( $c, $x, $y, $i - 1, $j - 1 );
00084             if ( $last != '' ) {
00085                 $i_text = ( ( $i + $add_i ) == ( $end_i + 1 ) ) ? ( $i + $add_i ) : ( $i + $add_i ) . ',' . ( $end_i + 1 );
00086                 $j_text = ( ( $j + $add_j ) == ( $end_j + 1 ) ) ? ( $j + $add_j ) : ( $j + $add_j ) . ',' . ( $end_j + 1 );
00087                 $last = ( $change ) ? 'c' : $last;
00088                 $patch .= $i_text . $last . $j_text . "\n";
00089             }
00090         } elseif ( ( $i == -1) && ( $j == -1 ) ) {
00091             $i_text = ( ( $i + $add_i ) == ( $end_i + 1 ) ) ? ( $i + $add_i ) : ( $i + $add_i ) . ',' . ( $end_i + 1 );
00092             $j_text = ( ( $j + $add_j ) == ( $end_j + 1 ) ) ? ( $j + $add_j ) : ( $j + $add_j ) . ',' . ( $end_j + 1 );
00093             $last = ( $change ) ? 'c' : $last;
00094             $patch .= $i_text . $last . $j_text . "\n";
00095         } else {
00096             $end_i = ( $end_i == 0 ) ? $i : $end_i;
00097             $end_j = ( $end_j == 0 ) ? $j : $end_j;
00098             if ( ($j >= 0 ) && ( ( $i == -1 ) || ( $c[$i][$j - 1] >= $c[$i - 1][$j] ) ) ) {
00099                 $change = ( ( $last == 'd' ) || ( $change ) );
00100                 $patch .= print_diff( $c, $x, $y, $i, $j - 1, 'a', $change, $end_i, $end_j, 1, 2 );
00101                 $patch .= '> ' . $y[$j] . "\n";
00102             } elseif ( ( $i >= 0 ) && ( ( $j == -1 ) || ( $c[$i][$j - 1] < $c[$i - 1][$j] ) ) ) {
00103                 $change = ( ( $last == 'a' ) || ( $change ) );
00104                 $patch .= print_diff( $c, $x, $y, $i - 1, $j, 'd', $change, $end_i, $end_j, 2, 2 );
00105             }
00106         }
00107 
00108         return $patch;
00109     }
00110 
00111     $x = explode( "\n", str_replace( "\r\n", "\n", $original ) );
00112     $y = explode( "\n", str_replace( "\r\n", "\n", $updated ) );
00113 
00114     $m_start = 0;
00115     $m_end = count( $x ) - 1;
00116     $n_start = 0;
00117     $n_end = count( $y ) - 1;
00118 
00119     // trim off matching items at the beginning
00120     while ( ( $m_start < $m_end ) && ( $n_start < $n_end ) && ( $x[$m_start] == $y[$n_start] ) ) {
00121         $m_start++;
00122         $n_start++;
00123     }
00124     // trim off matching items at the end
00125     while ( ( $m_start < $m_end ) && ( $n_start < $n_end ) && ( $x[$m_end] == $y[$n_end] ) ) {
00126         $m_end--;
00127         $n_end--;
00128     }
00129     // now the LCS magic
00130     $c = array();
00131     for ( $a = -1; $a <= $m_end; $a++ ) {
00132         $c[$a] = array();
00133         for ( $b = -1; $b <= $n_end; $b++ ) {
00134             $c[$a][$b] = 0;
00135         }
00136     }
00137     for ( $i = $m_start; $i <= $m_end; $i++ ) {
00138         for ( $j = $n_start; $j <= $n_end; $j++ ) {
00139             if ( $x[$i] == $y[$j] ) {
00140                 $c[$i][$j] = $c[$i - 1][$j - 1] + 1;
00141             } else {
00142                 $c[$i][$j] = max( $c[$i][$j - 1], $c[$i - 1][$j] );
00143             }
00144         }
00145     }
00146 
00147     return print_diff( $c, $x, $y, count( $x ) - 1, count( $y ) - 1 );
00148 }
00149 
00157 function patch( $original, $patch )
00158 {
00159     $new = array();
00160     $original_array = explode( "\n", str_replace( "\r\n", "\n", $original ) );
00161     $patch_array = explode( "\n", $patch );
00162     $i = 0;
00163     foreach ( $patch_array as $line ) {
00164         if ( ( !empty( $line ) ) && ( $line[0] == '>' ) ) {
00165             $new[] = substr( $line, 2 );
00166         } elseif ( !empty( $line ) ) {
00167             $pos = ( strpos( $line, 'a' ) !== false ) ? strpos( $line, 'a' ) : ( ( strpos( $line, 'c' ) !== false ) ? strpos( $line, 'c' ) : ( ( strpos( $line, 'd' ) !== false ) ? strpos( $line, 'd' ) : false ) );
00168             $type = $line[$pos];
00169             list( $i_half, $j_half ) = explode( $type, $line );
00170             list( $i_start, $i_end ) = explode( ',', $i_half . ',' . $i_half );
00171             $sub = ( $type == 'a' ) ? 0 : 1;
00172             for ( $a = $i; $a < ( $i_start - $sub ); $a++ ) {
00173                 $new[] = $original_array[$a];
00174             }
00175             $i = $i_end;
00176         }
00177     }
00178     for ( $a = $i; $a < count( $original_array ); $a++ ) {
00179         $new[] = $original_array[$a];
00180     }
00181 
00182     return implode( "\n", $new );
00183 }
00184 /*
00185 $original = 'This part of the
00186 document has stayed the
00187 same from version to
00188 version.  It shouldn\'t
00189 be shown if it doesn\'t
00190 change.  Otherwise, that
00191 would not be helping to
00192 compress the size of the
00193 changes.
00194 
00195 This paragraph contains
00196 text that is outdated.
00197 It will be deleted in the
00198 near future.
00199 
00200 It is important to spell
00201 check this dokument. On
00202 the other hand, a
00203 misspelled word isn\'t
00204 the end of the world.
00205 Nothing in the rest of
00206 this paragraph needs to
00207 be changed. Things can
00208 be added after it.';
00209 
00210 $updated = 'This is an important
00211 notice! It should
00212 therefore be located at
00213 the beginning of this
00214 document!
00215 
00216 This part of the
00217 document has stayed the
00218 same from version to
00219 version.  It shouldn\'t
00220 be shown if it doesn\'t
00221 change.  Otherwise, that
00222 would not be helping to
00223 compress anything.
00224 
00225 It is important to spell
00226 check this document. On
00227 the other hand, a
00228 misspelled word isn\'t
00229 the end of the world.
00230 Nothing in the rest of
00231 this paragraph needs to
00232 be changed. Things can
00233 be added after it.
00234 
00235 This paragraph contains
00236 important new additions
00237 to this document.';
00238 
00239 $patch = diff( $original, $updated );
00240 $new = patch( $original, $patch );
00241 echo $patch , "\n", $new;
00242 
00243 */

Generated on Sun Jan 2 2011 04:55:31 for Pragyan CMS by  doxygen 1.7.1