[ Index ]

PHP Cross Reference of Unnamed Project

title

Body

[close]

/mod/wiki/diff/ -> difflib.php (source)

   1  <?php
   2  /**
   3   * Standard diff function plus some extras for handling XHTML diffs.
   4   * @copyright &copy; 2007 The Open University
   5   * @author s.marshall@open.ac.uk
   6   * @license http://www.gnu.org/copyleft/gpl.html GNU Public License
   7   * @package ouwiki
   8   *//** */
   9   
  10  // Standard diff
  11  //////////////// 
  12   
  13  /**
  14   * Basic diff utility function, using standard diff algorithm.
  15   *
  16   * Based on Bell Laboratories Computing Science Technical Report #41, 
  17   * July 1976, Hunt & McIlroy, Appendix A.1 and A.3.
  18   * 
  19   * http://www.cs.dartmouth.edu/~doug/diff.ps
  20   *
  21   * @param array $file1 Array of lines in file 1. The first line in the file
  22   *   MUST BE INDEX 1 NOT ZERO!!
  23   * @param array $file2 Array of lines in file 2, again starting from 1.
  24   * @return array An array with one entry (again 1-based) for each line in  
  25   *   file 1, with its corresponding position in file 2 or 0 if it isn't there.
  26   */
  27  function ouwiki_diff_internal($file1,$file2) {
  28      // Basic variables
  29      $n=count($file2);
  30      $m=count($file1);
  31      
  32      // Special-case for empty file2 which otherwise causes error
  33      if($n==0) 
  34      {
  35          $result=array();
  36          for($i=1;$i<=$m;$i++) 
  37          {
  38              $result[$i]=0;
  39          }
  40          return $result;
  41      }
  42      
  43      // Step 1   Build list of elements
  44      /////////
  45      
  46      $V=array(); 
  47      for($j=1;$j<=$n;$j++) {
  48          $V[$j]=new StdClass;
  49          $V[$j]->serial=$j;
  50          $V[$j]->hash=crc32($file2[$j]);
  51      }
  52      
  53      // Step 2   Sort by hash,serial
  54      /////////
  55      
  56      usort($V,"ouwiki_diff_sort_v");
  57      
  58      // Make it start from 1 again
  59      array_unshift($V,'bogus');
  60      unset($V[0]);
  61      
  62      // $V is now an array including the line number 'serial' and hash
  63      // of each line in file 2, sorted by hash and then serial.    
  64      
  65      // Step 3   Equivalence classes
  66      /////////
  67      
  68      $E=array();
  69      $E[0]=new StdClass;
  70      $E[0]->serial=0;
  71      $E[0]->last=true;
  72      for($j=1;$j<=$n;$j++) {
  73          $E[$j]=new StdClass;
  74          $E[$j]->serial=$V[$j]->serial;
  75          $E[$j]->last=$j===$n || $V[$j]->hash!==$V[$j+1]->hash;
  76      }
  77  
  78      // E is now an array sorted the same way as $V which includes 
  79      // the line number 'serial' and whether or not that is the 'last'
  80      // line in the given equivalence class, i.e. set of identical lines
  81      
  82      // Step 4   For each line in file1, finds start of equivalence class
  83      /////////
  84      $P=array();
  85      for($i=1;$i<=$m;$i++) {
  86          // Find matching last entry from equivalence list    
  87          $P[$i]=ouwiki_diff_find_last($V,$E,crc32($file1[$i]));
  88      }
  89      
  90      // P is now an array that finds the index (within $V) of the *first*
  91      // matching line in $V (referencing file 2, but not a line number,
  92      // because sorted in $V order) for each line in file 1. In other words
  93      // if you were to start at the P-value in $V and continue through, you
  94      // would find all the lines from file 2 that are equal to the given line 
  95      // from file 1.    
  96      
  97      // Step 5   Initialise vector of candidates
  98      /////////
  99      
 100      // I do not trust PHP references further than I can throw them (preferably
 101      // at the idiot who came up with the idea) so I am using a separate array
 102      // to store candidates and all references are integers into that.
 103      
 104      $candidates=array();
 105      $candidates[0]=new StdClass;
 106      $candidates[0]->a=0;
 107      $candidates[0]->b=0;
 108      $candidates[0]->previous=null;
 109      $candidates[1]=new StdClass;
 110      $candidates[1]->a=$m+1; 
 111      $candidates[1]->b=$n+1;
 112      $candidates[1]->previous=null;
 113      
 114      $K=array();
 115      $K[0]=0; // Ref to candidate 0
 116      $K[1]=1; // Ref to candidate 1
 117      $k=0;
 118      
 119      // Step 6   Merge stage
 120      /////////
 121      
 122      for($i=1;$i<=$m;$i++) {
 123          if($P[$i]!==0) {
 124              ouwiki_diff_merge($K,$k,$i,$E,$P[$i],$candidates);
 125          }
 126      }
 127      
 128      // Step 7
 129      /////////
 130      
 131      $J=array();
 132      for($i=1;$i<=$m;$i++) {
 133          $J[$i]=0;
 134      }
 135      
 136      // Step 8   Follow candidate chain to make nice representation
 137      /////////
 138      
 139      $index=$K[$k];
 140      while(!is_null($index)) {
 141          // Stop when we reach the first, dummy candidate
 142          if($candidates[$index]->a!=0) {
 143              $J[$candidates[$index]->a]=$candidates[$index]->b;
 144          }
 145          $index=$candidates[$index]->previous;
 146      }    
 147      
 148      // Step 9   Get rid of 'jackpots' (hash collisions)
 149      /////////
 150      
 151      for($i=1;$i<=$m;$i++) {
 152          if($J[$i]!=0 && $file1[$i]!=$file2[$J[$i]]) {
 153              $J[$i]=0;
 154          }
 155      }
 156      
 157      // Done! (Maybe.)
 158      return $J;
 159  }
 160  
 161  // Functions needed by parts of the algorithm
 162  /////////////////////////////////////////////
 163  
 164  // Merge, from step 7 (Appendix A.3)
 165  function ouwiki_diff_merge(&$K,&$k,$i,&$E,$p,&$candidates) {
 166      $r=0;
 167      $c=$K[0];
 168      
 169      while(true) {
 170          $j=$E[$p]->serial; // Paper says 'i' but this is wrong (OCR)
 171          
 172          // Binary search in $K from $r to $k
 173          $min=$r;
 174          $max=$k+1;
 175          
 176          while(true) {
 177              $try = (int)(($min+$max)/2);
 178              if($candidates[$K[$try]]->b >= $j) {
 179                  $max=$try;
 180              } else if($candidates[$K[$try+1]]->b <= $j) {
 181                  $min=$try+1;
 182              } else { // $try is less and $try+1 is more
 183                  $s=$try;
 184                  break;
 185              }
 186              if($max<=$min) {
 187                  $s=-1;
 188                  break;
 189              }
 190          }
 191  
 192          if($s>-1) {
 193              if($candidates[$K[$s+1]]->b > $j) {
 194                  // Create new candidate
 195                  $index=count($candidates);
 196                  $candidates[$index]=new StdClass;
 197                  $candidates[$index]->a=$i;
 198                  $candidates[$index]->b=$j;
 199                  $candidates[$index]->previous=$K[$s];
 200                  $K[$r]=$c;
 201                  $r=$s+1;
 202                  $c=$index; // Or should this go before?
 203              }    
 204              
 205              if($s===$k) {
 206                  $K[$k+2]=$K[$k+1];
 207                  $k++;
 208                   break;
 209              }            
 210          }
 211          
 212          if($E[$p]->last) {
 213              break;
 214          }
 215          
 216          $p++;
 217      }
 218      $K[$r]=$c;
 219      
 220  }
 221  
 222  // From Step 2
 223  function ouwiki_diff_sort_v($a,$b) {
 224      if($a->hash < $b->hash) {
 225          return -1;
 226      } else if($a->hash > $b->hash) {
 227          return 1;    
 228      } else if($a->serial < $b->serial) {
 229          return -1;
 230      } else if($a->serial > $b->serial) {
 231          return 1;
 232      } else {
 233          return 0;
 234      }
 235  }
 236  
 237  // From Step 4
 238  function ouwiki_diff_find_last(&$V,&$E,$hash) {
 239      // Binary search in $V until we find something with $hash
 240      
 241      // Min = 1, array is 1-indexed
 242      $min=1;
 243      // Max = 1 higher than highest key
 244      end($V);
 245      $max=key($V)+1;
 246      while(true) {
 247          $try = (int)(($min+$max)/2);
 248          if($V[$try]->hash > $hash) {
 249              $max=$try;
 250          } else if($V[$try]->hash < $hash) {
 251              $min=$try+1;
 252          } else { // Equal
 253              break;
 254          }
 255          if($max<=$min) {
 256              // No matching line
 257              return 0;
 258          }
 259      }
 260      
 261      // Now check back in $E to find the first line of that equivalence class
 262      for($j=$try;!$E[$j-1]->last;$j--) ;
 263      return $j;
 264  }
 265  
 266  ///////////////////////////
 267  
 268  
 269  /**
 270   * Class representing one 'line' of HTML content for the purpose of 
 271   * text comparison. 
 272   */
 273  class ouwiki_line {
 274      /** Array of ouwiki_words */
 275      var $words=array();
 276      
 277      /**
 278       * Construct line object based on a chunk of text.
 279       * @param string $data Text data that makes up this 'line'. (May include line breaks etc.)
 280       * @param int $linepos Position number for first character in text
 281       */
 282      public function __construct($data,$linepos) {
 283          // 1. Turn things we don't want into spaces (so that positioning stays same)
 284          
 285          // Whitespace replaced with space
 286          $data=preg_replace('/\s/',' ',$data);
 287          
 288          // Various ways of writing non-breaking space replaced with space
 289          // Note that using a single param for replace only works because all
 290          // the search strings are 6 characters long
 291          $data=str_replace(array('&nbsp;','&#xA0;','&#160;'),'      ',$data);
 292          
 293          // Tags replaced with equal number of spaces
 294          $data=preg_replace_callback('/<.*?'.'>/',create_function(
 295              '$matches','return preg_replace("/./"," ",$matches[0]);'),$data);
 296              
 297          // 2. Analyse string so that each space-separated thing 
 298          // is counted as a 'word' (note these may not be real words,
 299          // for instance words may include punctuation at either end)
 300          $pos=0;
 301          while(true) {
 302              // Find a non-space
 303              $strlendata = strlen($data);
 304              for(;$pos < $strlendata && substr($data,$pos,1)===' ';$pos++) ;
 305              if($pos==$strlendata) {
 306                  // No more content
 307                  break;
 308              }
 309              
 310              // Aaaand find the next space after that
 311              $space2=strpos($data,' ',$pos);
 312              if($space2===false) {
 313                  // No more spaces? Everything left must be a word
 314                  $this->words[]=new ouwiki_word(substr($data,$pos),$pos+$linepos);
 315                  break;
 316              } else {
 317                  $this->words[]=new ouwiki_word(substr($data,$pos,$space2-$pos),$pos+$linepos);
 318                  $pos=$space2;
 319              }
 320          }
 321      }
 322  
 323      /**
 324       * Old syntax of class constructor. Deprecated in PHP7.
 325       *
 326       * @deprecated since Moodle 3.1
 327       */
 328      public function ouwiki_line($data, $linepos) {
 329          debugging('Use of class name as constructor is deprecated', DEBUG_DEVELOPER);
 330          self::__construct($data, $linepos);
 331      }
 332  
 333      /**
 334       * @return string Normalised string representation of this line object
 335       */
 336      function get_as_string() {
 337          $result='';
 338          foreach($this->words as $word) {
 339              if($result!=='') {
 340                  $result.=' ';
 341              }
 342              $result.=$word->word;
 343          }
 344          return $result;
 345      }
 346      
 347      /**
 348       * Static function converts lines to strings.
 349       * @param array $lines Array of ouwiki_line
 350       * @return array Array of strings
 351       */
 352      static function get_as_strings($lines) {
 353          $strings=array();
 354          foreach($lines as $key=>$value) {
 355              $strings[$key]=$value->get_as_string();        
 356          }
 357          return $strings;
 358      }
 359      
 360      
 361      /**
 362       * @return True if there are no words in the line
 363       */
 364      function is_empty() {
 365          return count($this->words)===0;
 366      }
 367  }
 368  
 369  /**
 370   * Represents single word for html comparison. Note that words
 371   * are just chunks of plain text and may not be actual words;
 372   * they could include punctuation or (if there was e.g. a span
 373   * in the middle of something) even be part-words.
 374   */
 375  class ouwiki_word {
 376      /** Word as plain string */
 377      var $word;
 378      /** Start position in original xhtml */
 379      var $start;
 380      
 381      public function __construct($word,$start) {
 382          $this->word=$word;
 383          $this->start=$start;
 384      }
 385  
 386      /**
 387       * Old syntax of class constructor. Deprecated in PHP7.
 388       *
 389       * @deprecated since Moodle 3.1
 390       */
 391      public function ouwiki_word($word, $start) {
 392          debugging('Use of class name as constructor is deprecated', DEBUG_DEVELOPER);
 393          self::__construct($word, $start);
 394      }
 395  }
 396  
 397  /**
 398   * Prepares XHTML content for text difference comparison. 
 399   * @param string $content XHTML content [NO SLASHES]
 400   * @return array Array of ouwiki_line objects
 401   */
 402  function ouwiki_diff_html_to_lines($content) {
 403      // These functions are a pain mostly because PHP preg_* don't provide
 404      // proper information as to the start/end position of matches. As a
 405      // consequence there is a lot of hackery going down. At every point we
 406      // replace things with spaces rather than getting rid, in order to store
 407      // positions within original content.
 408      
 409      // Get rid of all script, style, object tags (that might contain non-text
 410      // outside tags)
 411      $content=preg_replace_callback(
 412          '^(<script .*?</script>)|(<object .*?</object>)|(<style .*?</style>)^i',create_function(
 413              '$matches','return preg_replace("/./"," ",$matches[0]);'),$content); 
 414      
 415      // Get rid of all ` symbols as we are going to use these for a marker later.
 416      $content=preg_replace('/[`]/',' ',$content);
 417      
 418      // Put line breaks on block tags. Mark each line break with ` symbol
 419      $blocktags=array('p','div','h1','h2','h3','h4','h5','h6','td','li');
 420      $taglist='';
 421      foreach($blocktags as $blocktag) {
 422          if($taglist!=='') {
 423              $taglist.='|';
 424          }
 425          $taglist.="<$blocktag>|<\\/$blocktag>";
 426      }
 427      $content=preg_replace_callback('/(('.$taglist.')\s*)+/i',create_function(
 428          '$matches','return "`".preg_replace("/./"," ",substr($matches[0],1));'),$content);
 429          
 430      // Now go through splitting each line
 431      $lines=array(); $index=1;
 432      $pos=0;
 433      while($pos<strlen($content)) {
 434          $nextline=strpos($content,'`',$pos);
 435          if($nextline===false) {
 436              // No more line breaks? Take content to end
 437              $nextline=strlen($content);
 438          }
 439          
 440          $linestr=substr($content,$pos,$nextline-$pos);
 441          $line=new ouwiki_line($linestr,$pos);
 442          if(!$line->is_empty()) {
 443              $lines[$index++]=$line;
 444          }
 445          $pos=$nextline+1;
 446      }
 447      return $lines; 
 448  }
 449  
 450  /** 
 451   * Represents a changed area of file and where it is located in the 
 452   * two source files. 
 453   */ 
 454  class ouwiki_change_range {
 455      var $file1start,$file1count;
 456      var $file2start,$file2count;
 457  }
 458  
 459  /**
 460   * A more logical representation of the results from ouwiki_internal_diff()
 461   */
 462  class ouwiki_changes {
 463      
 464      /** Array of indexes (in file 2) of added lines */
 465      var $adds;
 466      
 467      /** Array of indexes (in file 1) of deleted lines */
 468      var $deletes;
 469      
 470      /** Array of changed ranges */
 471      var $changes;
 472      
 473      /** 
 474       * @param array $diff Array from line indices in file1
 475       *   to indices in file2. All indices 1-based.
 476       * @param int $count2 Number of lines in file2
 477       */
 478      public function __construct($diff,$count2) {
 479          // Find deleted lines
 480          $this->deletes=self::internal_find_deletes($diff,$count2);
 481          
 482          // Added lines work the same way after the comparison is
 483          // reversed.
 484          $this->adds=self::internal_find_deletes(
 485              ouwiki_diff_internal_flip($diff,$count2),count($diff));
 486          
 487          // Changed ranges are all the other lines from file 1 that
 488          // weren't found in file 2 but aren't deleted, and the 
 489          // corresponding lines from file 2 (between the equivalent
 490          // 'found' lines).        
 491          $this->changes=array();
 492          $matchbefore=0;
 493          $inrange=-1; $lastrange=-1;
 494          foreach($diff as $index1=>$index2) {
 495              // Changed line if this isn't in 'deleted' section and
 496              // doesn't have a match in file2.
 497              if($index2===0 && !in_array($index1,$this->deletes)) {
 498                  if($inrange===-1) {
 499                      // Not already in a range, start a new one at array end
 500                      $inrange=count($this->changes);
 501                      $this->changes[$inrange]=new ouwiki_change_range;
 502                      $this->changes[$inrange]->file1start=$index1;
 503                      $this->changes[$inrange]->file1count=1;                    
 504                      $this->changes[$inrange]->file2start=$matchbefore+1; // Last valid from file2                    
 505                      $this->changes[$inrange]->file2count=0;
 506                      $lastrange=$inrange;
 507                  } else {
 508                      // One more line that gets added to the range
 509                      $this->changes[$inrange]->file1count++;
 510                  }
 511              } else {
 512                  // Not in a range any more
 513                  $inrange=-1;
 514                  // If we have a line match...
 515                  if($index2!==0) {
 516                      // Remember this line as next range must start after it
 517                      $matchbefore=$index2;
 518                      // If last range is still looking for a number, fill that in too
 519                      if($lastrange!==-1) {
 520                          $this->changes[$lastrange]->file2count=$index2
 521                              -$this->changes[$lastrange]->file2start;
 522                          $lastrange=-1;
 523                      }
 524                  }
 525              }
 526          }
 527          // Unfinished range in file2 gets end of file
 528          if($lastrange!==-1) {
 529              $this->changes[$lastrange]->file2count=$count2
 530                  -$this->changes[$lastrange]->file2start+1;
 531          }
 532      }
 533  
 534      /**
 535       * Old syntax of class constructor. Deprecated in PHP7.
 536       *
 537       * @deprecated since Moodle 3.1
 538       */
 539      public function ouwiki_changes($diff, $count2) {
 540          debugging('Use of class name as constructor is deprecated', DEBUG_DEVELOPER);
 541          self::__construct($diff, $count2);
 542      }
 543  
 544      /**
 545       * Find deleted lines. These are lines in file1 that
 546       * cannot be present even in modified form in file2
 547       * because we have matching lines around them.
 548       * O(n) algorithm.
 549       * @param array $diff Array of file1->file2 indexes
 550       * @param int $count2 Count of lines in file2
 551       */
 552      function internal_find_deletes($diff,$count2) {
 553          $deletes=array();
 554          
 555          // 1. Create a new array that includes the lowest-valued
 556          //    index2 value below each run of 0s.
 557          //    I.e. if our array is say 1,2,0,0,0,3,0 then the
 558          //    resulting array will be -,-,3,3,3,-,0
 559          $squidges=array();
 560          $lowest=0;
 561          $countdiff = count($diff);
 562          for($index1=$countdiff;$index1>=1;$index1--) {
 563              $index2=$diff[$index1];
 564              if($index2===0) {
 565                  $squidges[$index1]=$lowest;
 566              } else {
 567                  $lowest=$index2;
 568              }
 569          }
 570          
 571          // 2. OK now we can use this new array to work out 
 572          //    items that are known to be deleted because we
 573          //    have matching items either side        
 574          $highest=0;
 575          foreach($diff as $index1=>$index2) {
 576              if($index2===0) {
 577                  if($highest===$count2 || $highest+1===$squidges[$index1]) {
 578                      // Yep! Definitely deleted.
 579                      $deletes[]=$index1;                    
 580                  } 
 581              } else {
 582                  $highest=$index2;                
 583              }
 584          }
 585          return $deletes;        
 586      }
 587  }
 588  
 589  /**
 590   * Flips around the array returned by ouwiki_diff_internal
 591   * so that it refers to lines from the other file. 
 592   * @param array $diff Array of index1=>index2
 593   * @param int $count2 Count of lines in file 2
 594   * @return array Flipped version
 595   */
 596  function ouwiki_diff_internal_flip($diff,$count2) {
 597      $flip=array();
 598      for($i=1;$i<=$count2;$i++) {
 599          $flip[$i]=0;
 600      }
 601      foreach($diff as $index1=>$index2) {
 602          if($index2!==0) {
 603              $flip[$index2]=$index1;
 604          }
 605      }
 606      return $flip;
 607  }
 608  
 609  /**
 610   * Compares two files based initially on lines and then on words within the lines that
 611   * differ.
 612   * @param array $lines1 Array of ouwiki_line
 613   * @param array $lines2 Array of ouwiki_line
 614   * @return array (deleted,added); deleted and added are arrays of ouwiki_word with
 615   *   position numbers from $lines1 and $lines2 respectively 
 616   */
 617  function ouwiki_diff_words($lines1,$lines2) {
 618      // Prepare arrays
 619      $deleted=array();
 620      $added=array();
 621      // Get line difference
 622      $linediff=ouwiki_diff(
 623          ouwiki_line::get_as_strings($lines1),
 624          ouwiki_line::get_as_strings($lines2));
 625          
 626      // Handle lines that were entirely deleted
 627      foreach($linediff->deletes as $deletedline) {
 628          $deleted = array_merge($deleted, $lines1[$deletedline]->words);
 629      }
 630      // And ones that were entirely added
 631      foreach($linediff->adds as $addedline) {
 632          $added = array_merge($added, $lines2[$addedline]->words);
 633      }
 634      
 635      // Changes get diffed at the individual-word level
 636      foreach($linediff->changes as $changerange) {
 637          // Build list of all words in each side of the range
 638          $file1words=array();
 639          for($index=$changerange->file1start;
 640              $index<$changerange->file1start+$changerange->file1count;$index++) {
 641              foreach($lines1[$index]->words as $word) {
 642                  $file1words[]=$word;
 643              }
 644          }
 645          $file2words=array();
 646          for($index=$changerange->file2start;
 647              $index<$changerange->file2start+$changerange->file2count;$index++) {
 648              foreach($lines2[$index]->words as $word) {
 649                  $file2words[]=$word;
 650              }
 651          }
 652                  
 653          // Make arrays 1-based
 654          array_unshift($file1words,'dummy');
 655          unset($file1words[0]);
 656          array_unshift($file2words,'dummy');
 657          unset($file2words[0]);
 658          
 659          // Convert word lists into plain strings
 660          $file1strings=array();
 661          foreach($file1words as $index=>$word) {
 662              $file1strings[$index]=$word->word;
 663          }
 664          $file2strings=array();
 665          foreach($file2words as $index=>$word) {
 666              $file2strings[$index]=$word->word;
 667          }
 668          
 669          // Run diff on strings
 670          $worddiff=ouwiki_diff($file1strings,$file2strings);
 671          foreach($worddiff->adds as $index) {
 672              $added[]=$file2words[$index];
 673          }
 674          foreach($worddiff->deletes as $index) {
 675              $deleted[]=$file1words[$index];
 676          }
 677          foreach($worddiff->changes as $changerange) {
 678              for($index=$changerange->file1start;
 679                  $index<$changerange->file1start+$changerange->file1count;$index++) {
 680                  $deleted[]=$file1words[$index];
 681              }
 682              for($index=$changerange->file2start;
 683                  $index<$changerange->file2start+$changerange->file2count;$index++) {
 684                  $added[]=$file2words[$index];
 685              }
 686          }
 687      }
 688      
 689      return array($deleted,$added);
 690  }
 691  
 692  /**
 693   * Runs diff and interprets results into ouwiki_changes object.
 694   * @param array $file1 Array of lines in file 1. The first line in the file
 695   *   MUST BE INDEX 1 NOT ZERO!!
 696   * @param array $file2 Array of lines in file 2, again starting from 1.
 697   * @return ouwiki_changes Object describing changes
 698   */
 699  function ouwiki_diff($file1,$file2) {
 700      return new ouwiki_changes(ouwiki_diff_internal($file1,$file2),count($file2));
 701  }
 702  
 703  /**
 704   * Adds HTML span elements to $html around the words listed in $words.
 705   * @param string $html HTML content
 706   * @param array $words Array of ouwiki_word to mark
 707   * @param string $markerclass Name of class for span element
 708   * @return HTML with markup added
 709   */
 710  function ouwiki_diff_add_markers($html,$words,$markerclass,$beforetext,$aftertext) {
 711      // Sort words by start position
 712      usort($words, create_function('$a,$b','return $a->start-$b->start;'));
 713      
 714      // Add marker for each word. We use an odd tag name which will
 715      // be replaced by span later, this for ease of replacing 
 716      $spanstart="<ouwiki_diff_add_markers>";
 717      $pos=0;
 718      $result='';
 719      foreach($words as $word) {
 720          // Add everything up to the word
 721          $result.=substr($html,$pos,$word->start-$pos);
 722          // Add word
 723          $result.=$spanstart.$word->word.'</ouwiki_diff_add_markers>';
 724          // Update position
 725          $pos=$word->start+strlen($word->word);
 726      }
 727  
 728      // Add everything after last word
 729      $result.=substr($html,$pos);
 730      
 731      // If we end a marker then immediately start one, get rid of
 732      // both the end and start
 733      $result=preg_replace('^</ouwiki_diff_add_markers>(\s*)<ouwiki_diff_add_markers>^','$1',$result);
 734      
 735      // Turn markers into proper span
 736      $result=preg_replace('^<ouwiki_diff_add_markers>^',$beforetext.'<span class="'.$markerclass.'">',$result);
 737      $result=preg_replace('^</ouwiki_diff_add_markers>^','</span>'.$aftertext,$result);
 738      
 739      return $result;
 740  }
 741  
 742  /**
 743   * Compares two HTML files. (This is the main function that everything else supports.)
 744   * @param string $html1 XHTML for file 1  
 745   * @param string $html2 XHTML for file 2
 746   * @return array ($result1,$result2) to be displayed indicating the differences  
 747   */
 748  function ouwiki_diff_html($html1,$html2) {
 749      $lines1=ouwiki_diff_html_to_lines($html1);
 750      $lines2=ouwiki_diff_html_to_lines($html2);
 751      list($deleted,$added)=ouwiki_diff_words($lines1,$lines2);
 752      $result1=ouwiki_diff_add_markers($html1,$deleted,'ouw_deleted',
 753          '<strong class="accesshide">'.get_string('deletedbegins','wiki').'</strong>',
 754          '<strong class="accesshide">'.get_string('deletedends','wiki').'</strong>');
 755      $result2=ouwiki_diff_add_markers($html2,$added,'ouw_added',
 756          '<strong class="accesshide">'.get_string('addedbegins','wiki').'</strong>',
 757          '<strong class="accesshide">'.get_string('addedends','wiki').'</strong>');
 758      return array($result1,$result2);    
 759  }
 760  


Generated: Thu Aug 11 10:00:09 2016 Cross-referenced by PHPXref 0.7.1