2009/4/22 Kent Wang kwang@kwang.org:
I'm building an application that uses DifferenceEngine.php to generate word level unified diffs. I've figured out how to do this but now need to generate patches given the diff.
This is what I have written to generate the diff:
$orig = array('One Two Three', 'One Two Three'); $closing = array('One Two Three', 'One Two Four');
$diff = new WordLevelDiff($orig, $closing); $formatter = new UnifiedDiffFormatter(); echo $formatter->format($diff);
Which returns:
@@ -5,3 +5,3 @@ One Two
- Three
- Four
So my application will store this and when it comes to time to patch that diff, I need a function that will do that, i.e. given the diff string above and $orig, it should generate $closing.
Does such a patch functionality exist in MediaWiki, or anywhere else?
It's not in MediaWiki, and I don't know if it's in PHP, but there's a very widespread command line program installed on virtually every UNIX/Linux system that can do this. Unsurprisingly, it's called "patch". You should be able to create two temporary files, say tmp1 and tmp2, put $orig in tmp1 and the unified diff in tmp2, then run "patch tmp1 tmp2", and read the contents of tmp1 into $closing (don't forget to delete the temp files, of course).
Roan Kattouw (Catrope)