Wikidiff2

Wikidiff2 is a native extension for PHP that provides a faster diff engine to MediaWiki. It is partly based on the original wikidiff, and partly on MediaWiki's DifferenceEngine class. It produces diffs from input text (line-based or word-level) and can format these as HTML or JSON.

Wikidiff2 includes support for character-level diffs for text composed of characters from the Japanese and Thai alphabets and the unified Han, and includes support for Thai segmentation for word-level diffs in that language. Japanese, Chinese and Thai do not use spaces to separate words. The input is assumed to be UTF-8 encoded. Invalid UTF-8 may cause undesirable operation, such as truncation of the output, so the input should be validated by the application. The input text should have unix-style line endings.

Debian or Ubuntu
On older versions of the package you may need to run a command to actually enable the extension:

Manually
First, get and compile libthai (it should be on your OS or distro's packages).

You can download wikidiff2 through git or by downloading a tarball from https://releases.wikimedia.org/wikidiff2/.

Then compile wikidiff2. You need phpize (shipped with PHP).

Make sure that your php option is set. This is usually set in your "php.ini" file.

Configuration
The following "php.ini" parameters are supported:

Wikidiff2 estimates similarity of added and deleted lines based on changed character count. When the similarity of an added and deleted line is greater than this threshold, the lines are displayed as moved.

Range 0.0 .. 1.0. Default.

Changed lines with a similarity value below this threshold will be split into a deleted line and added line. This helps matching up moved lines in some cases.

Range 0.0 .. 1.0. Default.

When the number of added and deleted lines in a table diff is greater than this limit, no attempt to detect moved lines will be made.

Default.

When comparing two lines for changes within the line, a word-level diff will be done unless the product of the LHS word count and the RHS word count exceeds this limit.

Default.

MediaWiki
If the module is installed into PHP, MediaWiki will try and use it. See $wgExternalDiffEngine for configuration options.

Usage
The input is assumed to be UTF-8 encoded. Invalid UTF-8 may cause undesirable operation, such as truncation of the output, so the input should be validated by the application. The input text should have UNIX-style line endings.

wikidiff2_do_diff
Compare two strings  and , and produce output formatted as a fragment of an HTML table, that is, a series of ` ` elements.

is the number of copied context lines shown before and after each change. Before each block of context lines and changes, a line number will appear as an HTML comment inside a tr/td, e.g.

Compare two strings  and , and produce output formatted as inline HTML.

wikidiff2_inline_json_diff
Compare two strings  and   and produce output formatted as JSON. See the JSON diff format documentation. wikidiff2_multi_format_diff

Compare two strings  and   with an associative array of options:
 * numContextLines: The number of context lines shown before and after each block
 * changeThreshold: The minimum similarity a pair of lines must have to be detected as a change and shown as a word-level diff. If present, this overrides php.ini wikidiff2.change_threshold.
 * movedLineThreshold: The minimum similarity a pair of lines must have to be detected as a moved line. If present, this overrides php.ini wikidiff2.moved_line_threshold.
 * maxMovedLines: The maximum number of added or deleted lines, above which no move detection will be performed. If present, this overrides php.ini moved_paragraph_detection_cutoff.
 * maxWordLevelDiffComplexity: The maximum complexity of a word-level diff. If the product of the word count in the LHS and RHS exceeds this value, a word-level diff will not be done. If present, this overrides php.ini wikidiff2.max_word_level_diff_complexity.
 * maxSplitSize: The maximum number of lines in  which may be considered for a word-level diff against a single line of  . Default: 1.
 * initialSplitThreshold: The minimum similarity which must be maintained during a split detection search. The search terminates when the similarity falls below this level. Default: 0.1.
 * finalSplitThreshold: The minimum similarity which must be achieved in order to display the comparison between one line and several lines as a split. Default 0.6.
 * formats: An array of desired formats. Each format is one of the following strings: table, inline or inlineJSON. The default is ['table'].

The return value is an associative array of formatted outputs. The key of each element is the format name table, inline or inlineJSON, and the value is a string.

wikidiff2_version
Produces the same thing as.

HTML
The HTML diff—a number of HTML table rows with the rest of the document structure omitted—is available as a side-by-side or inline comparison. The characters "<", ">" and "&" will be HTML-escaped in the output. In the Wikidiff2 C++ library, you can access the side-by-side diff using the  class or the inline diff using the   class. Both classes include an execute method that returns the diff of the text passed in as parameters. You can also access these execute methods using the PHP wrapper functions  (for the side-by-side diff) and   (for the inline diff).

JSON
The JSON diff provides structured data to compose a visual, line-by-line comparison between two sets of text. In the Wikidiff2 C++ library, you can access the JSON diff using the  class, which includes an execute method that returns the diff of the text passed in as parameters. You can also access this execute method using the PHP wrapper function.

JSON diff schema

The JSON diff includes properties to identify changes between the two sets of text. For an example of a JSON diff, see the MediaWiki REST API compare revisions endpoint.

Links

 * Overview on the Wikidiff2 improvements by the WMDE tech team in 2017/18
 * How to release Wikidiff2 on the Wikimedia production system
 * Wikidiff2 on Phabricator