function diff_local($from_lines, $to_lines) { global $wgExternalDiffEngine; wfProfileIn(__METHOD__); if ($wgExternalDiffEngine == 'wikidiff3') { // wikidiff3 $wikidiff3 = new WikiDiff3(); $wikidiff3->diff($from_lines, $to_lines); $this->xchanged = $wikidiff3->removed; $this->ychanged = $wikidiff3->added; unset($wikidiff3); } else { // old diff $n_from = sizeof($from_lines); $n_to = sizeof($to_lines); $this->xchanged = $this->ychanged = array(); $this->xv = $this->yv = array(); $this->xind = $this->yind = array(); unset($this->seq); unset($this->in_seq); unset($this->lcs); // Skip leading common lines. for ($skip = 0; $skip < $n_from && $skip < $n_to; $skip++) { if ($from_lines[$skip] !== $to_lines[$skip]) { break; } $this->xchanged[$skip] = $this->ychanged[$skip] = false; } // Skip trailing common lines. $xi = $n_from; $yi = $n_to; for ($endskip = 0; --$xi > $skip && --$yi > $skip; $endskip++) { if ($from_lines[$xi] !== $to_lines[$yi]) { break; } $this->xchanged[$xi] = $this->ychanged[$yi] = false; } // Ignore lines which do not exist in both files. for ($xi = $skip; $xi < $n_from - $endskip; $xi++) { $xhash[$this->_line_hash($from_lines[$xi])] = 1; } for ($yi = $skip; $yi < $n_to - $endskip; $yi++) { $line = $to_lines[$yi]; if ($this->ychanged[$yi] = empty($xhash[$this->_line_hash($line)])) { continue; } $yhash[$this->_line_hash($line)] = 1; $this->yv[] = $line; $this->yind[] = $yi; } for ($xi = $skip; $xi < $n_from - $endskip; $xi++) { $line = $from_lines[$xi]; if ($this->xchanged[$xi] = empty($yhash[$this->_line_hash($line)])) { continue; } $this->xv[] = $line; $this->xind[] = $xi; } // Find the LCS. $this->_compareseq(0, sizeof($this->xv), 0, sizeof($this->yv)); } wfProfileOut(__METHOD__); }
public function getMatchRatio(TextOnlyComparator $other) { $nbOthers = count($other->leafs); $nbThis = count($this->leafs); if ($nbOthers == 0 || $nbThis == 0) { return -log(0); } $diffengine = new WikiDiff3(25000, 1.35); $diffengine->diff($this->leafs, $other->leafs); $lcsLength = $diffengine->getLcsLength(); $distanceThis = $nbThis - $lcsLength; return (2.0 - $lcsLength / $nbOthers - $lcsLength / $nbThis) / 2.0; }