芝麻web文件管理V1.00
编辑当前文件:/home2/sdektunc/www/cepali/mod/wiki/diff/difflib.php
serial=$j; $V[$j]->hash=crc32($file2[$j]); } // Step 2 Sort by hash,serial ///////// usort($V,"ouwiki_diff_sort_v"); // Make it start from 1 again array_unshift($V,'bogus'); unset($V[0]); // $V is now an array including the line number 'serial' and hash // of each line in file 2, sorted by hash and then serial. // Step 3 Equivalence classes ///////// $E=array(); $E[0]=new StdClass; $E[0]->serial=0; $E[0]->last=true; for($j=1;$j<=$n;$j++) { $E[$j]=new StdClass; $E[$j]->serial=$V[$j]->serial; $E[$j]->last=$j===$n || $V[$j]->hash!==$V[$j+1]->hash; } // E is now an array sorted the same way as $V which includes // the line number 'serial' and whether or not that is the 'last' // line in the given equivalence class, i.e. set of identical lines // Step 4 For each line in file1, finds start of equivalence class ///////// $P=array(); for($i=1;$i<=$m;$i++) { // Find matching last entry from equivalence list $P[$i]=ouwiki_diff_find_last($V,$E,crc32($file1[$i])); } // P is now an array that finds the index (within $V) of the *first* // matching line in $V (referencing file 2, but not a line number, // because sorted in $V order) for each line in file 1. In other words // if you were to start at the P-value in $V and continue through, you // would find all the lines from file 2 that are equal to the given line // from file 1. // Step 5 Initialise vector of candidates ///////// // I do not trust PHP references further than I can throw them (preferably // at the idiot who came up with the idea) so I am using a separate array // to store candidates and all references are integers into that. $candidates=array(); $candidates[0]=new StdClass; $candidates[0]->a=0; $candidates[0]->b=0; $candidates[0]->previous=null; $candidates[1]=new StdClass; $candidates[1]->a=$m+1; $candidates[1]->b=$n+1; $candidates[1]->previous=null; $K=array(); $K[0]=0; // Ref to candidate 0 $K[1]=1; // Ref to candidate 1 $k=0; // Step 6 Merge stage ///////// for($i=1;$i<=$m;$i++) { if($P[$i]!==0) { ouwiki_diff_merge($K,$k,$i,$E,$P[$i],$candidates); } } // Step 7 ///////// $J=array(); for($i=1;$i<=$m;$i++) { $J[$i]=0; } // Step 8 Follow candidate chain to make nice representation ///////// $index=$K[$k]; while(!is_null($index)) { // Stop when we reach the first, dummy candidate if($candidates[$index]->a!=0) { $J[$candidates[$index]->a]=$candidates[$index]->b; } $index=$candidates[$index]->previous; } // Step 9 Get rid of 'jackpots' (hash collisions) ///////// for($i=1;$i<=$m;$i++) { if($J[$i]!=0 && $file1[$i]!=$file2[$J[$i]]) { $J[$i]=0; } } // Done! (Maybe.) return $J; } // Functions needed by parts of the algorithm ///////////////////////////////////////////// // Merge, from step 7 (Appendix A.3) function ouwiki_diff_merge(&$K,&$k,$i,&$E,$p,&$candidates) { $r=0; $c=$K[0]; while(true) { $j=$E[$p]->serial; // Paper says 'i' but this is wrong (OCR) // Binary search in $K from $r to $k $min=$r; $max=$k+1; while(true) { $try = (int)(($min+$max)/2); if($candidates[$K[$try]]->b >= $j) { $max=$try; } else if($candidates[$K[$try+1]]->b <= $j) { $min=$try+1; } else { // $try is less and $try+1 is more $s=$try; break; } if($max<=$min) { $s=-1; break; } } if($s>-1) { if($candidates[$K[$s+1]]->b > $j) { // Create new candidate $index=count($candidates); $candidates[$index]=new StdClass; $candidates[$index]->a=$i; $candidates[$index]->b=$j; $candidates[$index]->previous=$K[$s]; $K[$r]=$c; $r=$s+1; $c=$index; // Or should this go before? } if($s===$k) { $K[$k+2]=$K[$k+1]; $k++; break; } } if($E[$p]->last) { break; } $p++; } $K[$r]=$c; } // From Step 2 function ouwiki_diff_sort_v($a,$b) { if($a->hash < $b->hash) { return -1; } else if($a->hash > $b->hash) { return 1; } else if($a->serial < $b->serial) { return -1; } else if($a->serial > $b->serial) { return 1; } else { return 0; } } // From Step 4 function ouwiki_diff_find_last(&$V,&$E,$hash) { // Binary search in $V until we find something with $hash // Min = 1, array is 1-indexed $min=1; // Max = 1 higher than highest key end($V); $max=key($V)+1; while(true) { $try = (int)(($min+$max)/2); if($V[$try]->hash > $hash) { $max=$try; } else if($V[$try]->hash < $hash) { $min=$try+1; } else { // Equal break; } if($max<=$min) { // No matching line return 0; } } // Now check back in $E to find the first line of that equivalence class for($j=$try;!$E[$j-1]->last;$j--) ; return $j; } /////////////////////////// /** * Class representing one 'line' of HTML content for the purpose of * text comparison. */ class ouwiki_line { /** Array of ouwiki_words */ var $words=array(); /** * Construct line object based on a chunk of text. * @param string $data Text data that makes up this 'line'. (May include line breaks etc.) * @param int $linepos Position number for first character in text */ public function __construct($data,$linepos) { // 1. Turn things we don't want into spaces (so that positioning stays same) // Whitespace replaced with space $data=preg_replace('/\s/',' ',$data); // Various ways of writing non-breaking space replaced with space // Note that using a single param for replace only works because all // the search strings are 6 characters long $data=str_replace(array(' ',' ',' '),' ',$data); // Tags replaced with equal number of spaces $data = preg_replace_callback('/<.*?'.'>/', function($matches) { return preg_replace("/./", " ", $matches[0]); }, $data); // 2. Analyse string so that each space-separated thing // is counted as a 'word' (note these may not be real words, // for instance words may include punctuation at either end) $pos=0; while(true) { // Find a non-space $strlendata = strlen($data); for(;$pos < $strlendata && substr($data,$pos,1)===' ';$pos++) ; if($pos==$strlendata) { // No more content break; } // Aaaand find the next space after that $space2=strpos($data,' ',$pos); if($space2===false) { // No more spaces? Everything left must be a word $this->words[]=new ouwiki_word(substr($data,$pos),$pos+$linepos); break; } else { $this->words[]=new ouwiki_word(substr($data,$pos,$space2-$pos),$pos+$linepos); $pos=$space2; } } } /** * Old syntax of class constructor. Deprecated in PHP7. * * @deprecated since Moodle 3.1 */ public function ouwiki_line($data, $linepos) { debugging('Use of class name as constructor is deprecated', DEBUG_DEVELOPER); self::__construct($data, $linepos); } /** * @return string Normalised string representation of this line object */ function get_as_string() { $result=''; foreach($this->words as $word) { if($result!=='') { $result.=' '; } $result.=$word->word; } return $result; } /** * Static function converts lines to strings. * @param array $lines Array of ouwiki_line * @return array Array of strings */ static function get_as_strings($lines) { $strings=array(); foreach($lines as $key=>$value) { $strings[$key]=$value->get_as_string(); } return $strings; } /** * @return True if there are no words in the line */ function is_empty() { return count($this->words)===0; } } /** * Represents single word for html comparison. Note that words * are just chunks of plain text and may not be actual words; * they could include punctuation or (if there was e.g. a span * in the middle of something) even be part-words. */ class ouwiki_word { /** Word as plain string */ var $word; /** Start position in original xhtml */ var $start; public function __construct($word,$start) { $this->word=$word; $this->start=$start; } /** * Old syntax of class constructor. Deprecated in PHP7. * * @deprecated since Moodle 3.1 */ public function ouwiki_word($word, $start) { debugging('Use of class name as constructor is deprecated', DEBUG_DEVELOPER); self::__construct($word, $start); } } /** * Prepares XHTML content for text difference comparison. * @param string $content XHTML content [NO SLASHES] * @return array Array of ouwiki_line objects */ function ouwiki_diff_html_to_lines($content) { // These functions are a pain mostly because PHP preg_* don't provide // proper information as to the start/end position of matches. As a // consequence there is a lot of hackery going down. At every point we // replace things with spaces rather than getting rid, in order to store // positions within original content. // Get rid of all script, style, object tags (that might contain non-text // outside tags) $content=preg_replace_callback( '^(