User:Bawolff/EditConflictAutoMerge.js
Note: After publishing, you may have to bypass your browser's cache to see the changes.
- Firefox / Safari: Hold Shift while clicking Reload, or press either Ctrl-F5 or Ctrl-R (⌘-R on a Mac)
- Google Chrome: Press Ctrl-Shift-R (⌘-Shift-R on a Mac)
- Edge: Hold Ctrl while clicking Refresh, or press Ctrl-F5.
// Script to add a button to edit conflicts which try to automatically merge a conflict.
$( function() {
var $explain = $( '.mw-explainconflict' );
if ( mw.config.get( 'wgAction' ) === 'submit' && $( '#wpTextbox2' ).length && $explain.length ) {
mw.loader.load( 'https://meta.wikimedia.org/w/index.php?title=User:Bawolff/diffMergePatch.js&action=raw&ctype=text/javascript' );
var $button = $( '<button type="button" class="em-mergebutton">Automatically merge changes</button>' );
var $useMine = $( '<button type="button" class="em-useMine">Throw away conflicting changes and use only my edit</button>' );
var $div = $( '<div class="em-automerge">If auto-merging changes, be sure to preview and view diffs before saving, as results may be incorrect if both users are editing the same area of the text. Be careful selecting <i>Throw away conflicting changes...</i> as throwing away other people\'s edits is impolite.</div>' );
var sendForm = function( res ) {
$( '#wpTextbox1' )[0].value = res;
$( '#wpDiff' ).click();
}
$button.click( function() {
var theirs = $( '#wpTextbox1' )[0].value;
var mine = $( '#wpTextbox2' )[0].value;
mergeChanges( mine, theirs, sendForm);
} );
$useMine.click( function() {
sendForm( $( '#wpTextbox2' )[0].value );
} );
$explain.append( $button );
$explain.append( $('<span> </span>') );
$explain.append( $useMine );
$explain.append( $div );
}
var mergeChanges = function( mine, theirs, cb ) {
mw.loader.using( 'mw.Api', function() {
var api = new mw.Api();
api.get( {
action: 'query',
prop: 'revisions',
titles: mw.config.get( 'wgPageName' ),
rvlimit: 1,
rvprop: 'content',
rvstart: $( '#editform input[name=wpStarttime]' )[0].value,
indexpageids: 1
}).done( function ( d ) {
if ( d && d.query && d.query.pages && d.query.pageids && d.query.pages[d.query.pageids[0]].revisions && d.query.pages[d.query.pageids[0]].revisions[0] && d.query.pages[d.query.pageids[0]].revisions[0]['*'].length ) {
var old = d.query.pages[d.query.pageids[0]].revisions[0]['*'];
var shadow = theirs;
var dmp = new diff_match_patch();
// TODO: determine if its appropriate to mess with dmh.Match_Distance and dmh.Match_Threshold, etc
var myPatch = dmp.patch_make( old, mine );
shadow = dmp.patch_apply( myPatch, shadow )[0];
cb( shadow );
} else {
alert( "Could not merge. Cannot find original revision. Perhaps page has been subsequently deleted?" );
}
});
});
}
});