/** * Generate a diff, no caching * * @param $otext String: old text, must be already segmented * @param $ntext String: new text, must be already segmented */ function generateDiffBody($otext, $ntext) { global $wgExternalDiffEngine, $wgContLang; wfProfileIn(__METHOD__); $otext = str_replace("\r\n", "\n", $otext); $ntext = str_replace("\r\n", "\n", $ntext); $this->initDiffEngines(); if ($wgExternalDiffEngine == 'wikidiff' && function_exists('wikidiff_do_diff')) { # For historical reasons, external diff engine expects # input text to be HTML-escaped already $otext = htmlspecialchars($wgContLang->segmentForDiff($otext)); $ntext = htmlspecialchars($wgContLang->segmentForDiff($ntext)); wfProfileOut(__METHOD__); return $wgContLang->unsegmentForDiff(wikidiff_do_diff($otext, $ntext, 2)) . $this->debug('wikidiff1'); } if ($wgExternalDiffEngine == 'wikidiff2' && function_exists('wikidiff2_do_diff')) { # Better external diff engine, the 2 may some day be dropped # This one does the escaping and segmenting itself wfProfileIn('wikidiff2_do_diff'); $text = wikidiff2_do_diff($otext, $ntext, 2); $text .= $this->debug('wikidiff2'); wfProfileOut('wikidiff2_do_diff'); wfProfileOut(__METHOD__); return $text; } if ($wgExternalDiffEngine != 'wikidiff3' && $wgExternalDiffEngine !== false) { # Diff via the shell global $wgTmpDirectory; $tempName1 = tempnam($wgTmpDirectory, 'diff_'); $tempName2 = tempnam($wgTmpDirectory, 'diff_'); $tempFile1 = fopen($tempName1, "w"); if (!$tempFile1) { wfProfileOut(__METHOD__); return false; } $tempFile2 = fopen($tempName2, "w"); if (!$tempFile2) { wfProfileOut(__METHOD__); return false; } fwrite($tempFile1, $otext); fwrite($tempFile2, $ntext); fclose($tempFile1); fclose($tempFile2); $cmd = wfEscapeShellArg($wgExternalDiffEngine, $tempName1, $tempName2); wfProfileIn(__METHOD__ . "-shellexec"); $difftext = wfShellExec($cmd); $difftext .= $this->debug("external {$wgExternalDiffEngine}"); wfProfileOut(__METHOD__ . "-shellexec"); unlink($tempName1); unlink($tempName2); wfProfileOut(__METHOD__); return $difftext; } # Native PHP diff $ota = explode("\n", $wgContLang->segmentForDiff($otext)); $nta = explode("\n", $wgContLang->segmentForDiff($ntext)); $diffs = new Diff($ota, $nta); $formatter = new TableDiffFormatter(); $difftext = $wgContLang->unsegmentForDiff($formatter->format($diffs)) . wfProfileOut(__METHOD__); return $difftext; }
/** * Generate a diff, no caching * $otext and $ntext must be already segmented */ function generateDiffBody($otext, $ntext) { global $wgExternalDiffEngine, $wgContLang; $fname = 'DifferenceEngine::generateDiffBody'; $otext = str_replace("\r\n", "\n", $otext); $ntext = str_replace("\r\n", "\n", $ntext); if ($wgExternalDiffEngine == 'wikidiff') { # For historical reasons, external diff engine expects # input text to be HTML-escaped already $otext = htmlspecialchars($wgContLang->segmentForDiff($otext)); $ntext = htmlspecialchars($wgContLang->segmentForDiff($ntext)); if (!function_exists('wikidiff_do_diff')) { dl('php_wikidiff.so'); } return $wgContLang->unsegementForDiff(wikidiff_do_diff($otext, $ntext, 2)); } if ($wgExternalDiffEngine == 'wikidiff2') { # Better external diff engine, the 2 may some day be dropped # This one does the escaping and segmenting itself if (!function_exists('wikidiff2_do_diff')) { wfProfileIn("{$fname}-dl"); @dl('php_wikidiff2.so'); wfProfileOut("{$fname}-dl"); } if (function_exists('wikidiff2_do_diff')) { wfProfileIn('wikidiff2_do_diff'); $text = wikidiff2_do_diff($otext, $ntext, 2); wfProfileOut('wikidiff2_do_diff'); return $text; } } if ($wgExternalDiffEngine !== false) { # Diff via the shell global $wgTmpDirectory; $tempName1 = tempnam($wgTmpDirectory, 'diff_'); $tempName2 = tempnam($wgTmpDirectory, 'diff_'); $tempFile1 = fopen($tempName1, "w"); if (!$tempFile1) { wfProfileOut($fname); return false; } $tempFile2 = fopen($tempName2, "w"); if (!$tempFile2) { wfProfileOut($fname); return false; } fwrite($tempFile1, $otext); fwrite($tempFile2, $ntext); fclose($tempFile1); fclose($tempFile2); $cmd = wfEscapeShellArg($wgExternalDiffEngine, $tempName1, $tempName2); wfProfileIn("{$fname}-shellexec"); $difftext = wfShellExec($cmd); wfProfileOut("{$fname}-shellexec"); unlink($tempName1); unlink($tempName2); return $difftext; } # Native PHP diff $ota = explode("\n", $wgContLang->segmentForDiff($otext)); $nta = explode("\n", $wgContLang->segmentForDiff($ntext)); $diffs =& new Diff($ota, $nta); $formatter =& new TableDiffFormatter(); return $wgContLang->unsegmentForDiff($formatter->format($diffs)); }
/** * Generates diff, to be wrapped internally in a logging/instrumentation * * @param string $otext Old text, must be already segmented * @param string $ntext New text, must be already segmented * @return bool|string */ protected function textDiff($otext, $ntext) { global $wgExternalDiffEngine, $wgContLang; $otext = str_replace("\r\n", "\n", $otext); $ntext = str_replace("\r\n", "\n", $ntext); if ($wgExternalDiffEngine == 'wikidiff' || $wgExternalDiffEngine == 'wikidiff3') { wfDeprecated("\$wgExternalDiffEngine = '{$wgExternalDiffEngine}'", '1.27'); $wgExternalDiffEngine = false; } if ($wgExternalDiffEngine == 'wikidiff2') { if (function_exists('wikidiff2_do_diff')) { # Better external diff engine, the 2 may some day be dropped # This one does the escaping and segmenting itself $text = wikidiff2_do_diff($otext, $ntext, 2); $text .= $this->debug('wikidiff2'); return $text; } } elseif ($wgExternalDiffEngine !== false) { # Diff via the shell $tmpDir = wfTempDir(); $tempName1 = tempnam($tmpDir, 'diff_'); $tempName2 = tempnam($tmpDir, 'diff_'); $tempFile1 = fopen($tempName1, "w"); if (!$tempFile1) { return false; } $tempFile2 = fopen($tempName2, "w"); if (!$tempFile2) { return false; } fwrite($tempFile1, $otext); fwrite($tempFile2, $ntext); fclose($tempFile1); fclose($tempFile2); $cmd = wfEscapeShellArg($wgExternalDiffEngine, $tempName1, $tempName2); $difftext = wfShellExec($cmd); $difftext .= $this->debug("external {$wgExternalDiffEngine}"); unlink($tempName1); unlink($tempName2); return $difftext; } # Native PHP diff $ota = explode("\n", $wgContLang->segmentForDiff($otext)); $nta = explode("\n", $wgContLang->segmentForDiff($ntext)); $diffs = new Diff($ota, $nta); $formatter = new TableDiffFormatter(); $difftext = $wgContLang->unsegmentForDiff($formatter->format($diffs)); return $difftext; }
/** * Generates diff, to be wrapped internally in a logging/instrumentation * * @param string $otext Old text, must be already segmented * @param string $ntext New text, must be already segmented * @return bool|string */ protected function textDiff($otext, $ntext) { global $wgExternalDiffEngine, $wgContLang; $otext = str_replace("\r\n", "\n", $otext); $ntext = str_replace("\r\n", "\n", $ntext); if ($wgExternalDiffEngine == 'wikidiff' || $wgExternalDiffEngine == 'wikidiff3') { wfDeprecated("\$wgExternalDiffEngine = '{$wgExternalDiffEngine}'", '1.27'); $wgExternalDiffEngine = false; } elseif ($wgExternalDiffEngine == 'wikidiff2') { // Same as above, but with no deprecation warnings $wgExternalDiffEngine = false; } elseif (!is_string($wgExternalDiffEngine) && $wgExternalDiffEngine !== false) { // And prevent people from shooting themselves in the foot... wfWarn('$wgExternalDiffEngine is set to a non-string value, forcing it to false'); $wgExternalDiffEngine = false; } if (function_exists('wikidiff2_do_diff') && $wgExternalDiffEngine === false) { # Better external diff engine, the 2 may some day be dropped # This one does the escaping and segmenting itself $text = wikidiff2_do_diff($otext, $ntext, 2); $text .= $this->debug('wikidiff2'); return $text; } elseif ($wgExternalDiffEngine !== false && is_executable($wgExternalDiffEngine)) { # Diff via the shell $tmpDir = wfTempDir(); $tempName1 = tempnam($tmpDir, 'diff_'); $tempName2 = tempnam($tmpDir, 'diff_'); $tempFile1 = fopen($tempName1, "w"); if (!$tempFile1) { return false; } $tempFile2 = fopen($tempName2, "w"); if (!$tempFile2) { return false; } fwrite($tempFile1, $otext); fwrite($tempFile2, $ntext); fclose($tempFile1); fclose($tempFile2); $cmd = wfEscapeShellArg($wgExternalDiffEngine, $tempName1, $tempName2); $difftext = wfShellExec($cmd); $difftext .= $this->debug("external {$wgExternalDiffEngine}"); unlink($tempName1); unlink($tempName2); return $difftext; } # Native PHP diff $ota = explode("\n", $wgContLang->segmentForDiff($otext)); $nta = explode("\n", $wgContLang->segmentForDiff($ntext)); $diffs = new Diff($ota, $nta); $formatter = new TableDiffFormatter(); $difftext = $wgContLang->unsegmentForDiff($formatter->format($diffs)); return $difftext; }
<?php print wikidiff2_do_diff($x, $y, 2); print wikidiff3_do_diff($x, $y, 3);