User:Bosco/Unsigned helper.js: Difference between revisions
From Test Wiki
Content deleted Content added
tweak edit summary |
code formatting |
||
| Line 1: | Line 1: | ||
var UnsignedHelper={ |
var UnsignedHelper = { |
||
months: ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'], |
|||
makeUnsignedTemplate: function (user, ts, template) { |
|||
ts = new Date(ts); |
|||
var h = ts.getUTCHours(); |
|||
if (h < 10) |
|||
var m=ts.getUTCMinutes(); if(m<10) m='0'+m; |
|||
h = '0' + h; |
|||
ts=h+':'+m+', '+ts.getUTCDate()+' '+UnsignedHelper.months[ts.getUTCMonth()]+' '+ts.getUTCFullYear()+' (UTC)'; |
|||
var m = ts.getUTCMinutes(); |
|||
return '\x7b\x7bsubst:'+template+'|'+user+'|'+ts+'\x7d\x7d'; |
|||
if (m < 10) |
|||
m = '0' + m; |
|||
ts = h + ':' + m + ', ' + ts.getUTCDate() + ' ' + UnsignedHelper.months[ts.getUTCMonth()] + ' ' + ts.getUTCFullYear() + ' (UTC)'; |
|||
appendToEditSummary:function(newSummary) { |
|||
return '\x7b\x7bsubst:' + template + '|' + user + '|' + ts + '\x7d\x7d'; |
|||
const editSummaryField = $("#wpSummary:first"); |
|||
}, |
|||
if(editSummaryField.length==0) { |
|||
console.warn('Cannot find edit summary text field.'); |
|||
return; |
|||
} |
|||
const oldText = editSummaryField.val().trimEnd(); // get text without trailing whitespace |
|||
let newText = ""; |
|||
if (oldText.match(/[*]\/$/)) { // check if "/* section name */" is present |
|||
newText = oldText + " " + newSummary; |
|||
} else if (oldText.length!=0) { |
|||
newText = oldText + ", " + newSummary; |
|||
} else { |
|||
newText = newSummary; |
|||
} |
|||
editSummaryField.val(newText); |
|||
}, |
|||
appendToEditSummary: function (newSummary) { |
|||
addUnsignedTemplate:function(evt){ |
|||
const editSummaryField = $("#wpSummary:first"); |
|||
mw.loader.using(['mediawiki.util','jquery.ui'], function(){ |
|||
if (editSummaryField.length == 0) { |
|||
var f=document.getElementById('editform'); |
|||
console.warn('Cannot find edit summary text field.'); |
|||
var e=f.elements.wpTextbox1; |
|||
return; |
|||
var pos=$(e).textSelection('getCaretPosition', {startAndEnd:true}); |
|||
} |
|||
var txt; |
|||
const oldText = editSummaryField.val().trimEnd(); |
|||
if(pos[0]!=pos[1]){ |
|||
// get text without trailing whitespace |
|||
txt=e.value.substring(pos[0],pos[1]); |
|||
let newText = ""; |
|||
pos=pos[1]; |
|||
if (oldText.match(/[*]\/$/)) { |
|||
} else { |
|||
// check if "/* section name */" is present |
|||
pos=pos[1]; |
|||
newText = oldText + " " + newSummary; |
|||
if(pos<=0) pos=e.value.length; |
|||
} else if (oldText.length != 0) { |
|||
txt=e.value.substr(0,pos); |
|||
newText = oldText + ", " + newSummary; |
|||
txt=txt.replace(new RegExp('[\\s\\S]*\\d\\d:\\d\\d, \\d+ ('+UnsignedHelper.months.join('|')+') \\d\\d\\d\\d \\(UTC\\)'), ''); |
|||
} else { |
|||
txt=txt.replace(/[\s\S]*\n=+.*=+\s*\n/, ''); |
|||
newText = newSummary; |
|||
} |
|||
} |
|||
txt=txt.replace(/^\s+|\s+$/g, ''); |
|||
editSummaryField.val(newText); |
|||
}, |
|||
addUnsignedTemplate: function (evt) { |
|||
var rvct=1; |
|||
mw.loader.using(['mediawiki.util', 'jquery.ui'], function () { |
|||
var dialog=$('<div>Examining revision 1...</div>').dialog({ |
|||
var f = document.getElementById('editform'); |
|||
buttons:{ Cancel:function(){ dialog.dialog('close'); } }, |
|||
var e = f.elements.wpTextbox1; |
|||
modal:true, |
|||
var pos = $(e).textSelection('getCaretPosition', { startAndEnd: true }); |
|||
title:'Adding \x7b\x7bunsigned\x7d\x7d' |
|||
var txt; |
|||
}); |
|||
if (pos[0] != pos[1]) { |
|||
txt = e.value.substring(pos[0], pos[1]); |
|||
pos = pos[1]; |
|||
} else { |
|||
pos = pos[1]; |
|||
if (pos <= 0) |
|||
pos = e.value.length; |
|||
txt = e.value.substr(0, pos); |
|||
txt = txt.replace(new RegExp('[\\s\\S]*\\d\\d:\\d\\d, \\d+ (' + UnsignedHelper.months.join('|') + ') \\d\\d\\d\\d \\(UTC\\)'), ''); |
|||
txt = txt.replace(/[\s\S]*\n=+.*=+\s*\n/, ''); |
|||
} |
|||
txt = txt.replace(/^\s+|\s+$/g, ''); |
|||
var rvct = 1; |
|||
var revid, user, ts, comment, template; |
|||
var dialog = $('<div>Examining revision 1...</div>').dialog({ |
|||
var q={ |
|||
buttons: { |
|||
url:mw.util.wikiScript('api'), |
|||
Cancel: function () { |
|||
dataType:'json', |
|||
dialog.dialog('close'); |
|||
type:'POST', |
|||
} }, |
|||
data:{ |
|||
modal: true, |
|||
format:'json', |
|||
title: 'Adding \x7b\x7bunsigned\x7d\x7d' |
|||
action:'query', |
|||
}); |
|||
titles:mw.config.get('wgPageName'), |
|||
prop:'revisions', |
|||
rvprop:'ids|timestamp|user|parsedcomment|content', |
|||
rvlimit:1, |
|||
rawcontinue:1 |
|||
}, |
|||
success:function(r,sts,xhr){ |
|||
if(!dialog.dialog('isOpen')) return; |
|||
var revid, user, ts, comment, template; |
|||
if(!r.query || !r.query.pages){ |
|||
var q = { |
|||
dialog.html('<span style="color:red"><b>Error:</b> Bad response from API</span>'); |
|||
url: mw.util.wikiScript('api'), |
|||
if(window.console && typeof(window.console.error)=='function') |
|||
dataType: 'json', |
|||
window.console.error("Bad response", r); |
|||
type: 'POST', |
|||
return; |
|||
data: { |
|||
} |
|||
format: 'json', |
|||
action: 'query', |
|||
titles: mw.config.get('wgPageName'), |
|||
prop: 'revisions', |
|||
rvprop: 'ids|timestamp|user|parsedcomment|content', |
|||
rvlimit: 1, |
|||
rawcontinue: 1 |
|||
}, |
|||
success: function (r, sts, xhr) { |
|||
if (!dialog.dialog('isOpen')) |
|||
return; |
|||
if (!r.query || !r.query.pages) { |
|||
dialog.html('<span style="color:red"><b>Error:</b> Bad response from API</span>'); |
|||
var rr=r.query.pages[k].revisions[0]; |
|||
if (window.console && typeof (window.console.error) == 'function') |
|||
var cont = function () { |
|||
window.console.error("Bad response", r); |
|||
if(r['query-continue'] && r['query-continue'].revisions){ |
|||
return; |
|||
dialog.html('Evaluating revision '+(++rvct)+'...'); |
|||
} |
|||
q.data.rvcontinue=r['query-continue'].revisions.rvcontinue; |
|||
$.ajax(q); |
|||
} else { |
|||
var t=UnsignedHelper.makeUnsignedTemplate(user,ts,template); |
|||
var tt=e.value.substr(0,pos).replace(/\s*$/,' ')+t; |
|||
e.value=tt+e.value.substr(pos); |
|||
$(e).textSelection('setSelection', { start:tt.length }); |
|||
UnsignedHelper.appendToEditSummary(`mark unsigned [[Special:Diff/${revid}]]`); |
|||
dialog.dialog('close'); |
|||
} |
|||
}; |
|||
for (var k in r.query.pages) { |
|||
if(typeof(rr['*'])!='undefined' && rr['*'].indexOf(txt)<0){ |
|||
var rr = r.query.pages[k].revisions[0]; |
|||
if(!user){ |
|||
var cont = function () { |
|||
dialog.html('<span style="color:red"><b>Error:</b> Text was not found in the starting revision! Did you edit it?</span>'); |
|||
if (r['query-continue'] && r['query-continue'].revisions) { |
|||
return; |
|||
dialog.html('Evaluating revision ' + (++rvct) + '...'); |
|||
} |
|||
q.data.rvcontinue = r['query-continue'].revisions.rvcontinue; |
|||
var cb = function () { |
|||
$.ajax(q); |
|||
var t=UnsignedHelper.makeUnsignedTemplate(user,ts,template); |
|||
} else { |
|||
var tt=e.value.substr(0,pos).replace(/\s*$/,' ')+t; |
|||
var t = UnsignedHelper.makeUnsignedTemplate(user, ts, template); |
|||
e.value=tt+e.value.substr(pos); |
|||
var tt = e.value.substr(0, pos).replace(/\s*$/, ' ') + t; |
|||
$(e).textSelection('setSelection', { start:tt.length }); |
|||
e.value = tt + e.value.substr(pos); |
|||
UnsignedHelper.appendToEditSummary(`unsigned [[Special:Diff/${revid}]]`); |
|||
$(e).textSelection('setSelection', { |
|||
dialog.dialog('close'); |
|||
start: tt.length }); |
|||
}; |
|||
UnsignedHelper.appendToEditSummary(`mark unsigned [[Special:Diff/${revid}]]`); |
|||
if(/reverted|undid/i.test(comment)){ |
|||
dialog.dialog('close'); |
|||
var dialog2 = $( '<div>' ) |
|||
} |
|||
.append( |
|||
}; |
|||
'The ', |
|||
$( '<a>' ).prop( { href: '/w/index.php?diff=prev&oldid=' + revid, target: '_blank' } ).text( 'found revision' ), |
|||
if (typeof (rr['*']) != 'undefined' && rr['*'].indexOf(txt) < 0) { |
|||
' may be a revert: ', |
|||
if (!user) { |
|||
comment |
|||
dialog.html('<span style="color:red"><b>Error:</b> Text was not found in the starting revision! Did you edit it?</span>'); |
|||
) |
|||
return; |
|||
.dialog( { |
|||
} |
|||
title: "Possible revert!", |
|||
var cb = function () { |
|||
modal: true, |
|||
var t = UnsignedHelper.makeUnsignedTemplate(user, ts, template); |
|||
buttons: { |
|||
var tt = e.value.substr(0, pos).replace(/\s*$/, ' ') + t; |
|||
"Use that revision": function () { dialog2.dialog( 'close' ); cb(); }, |
|||
e.value = tt + e.value.substr(pos); |
|||
"Keep looking": function () { dialog2.dialog( 'close' ); cont(); }, |
|||
$(e).textSelection('setSelection', { |
|||
"Cancel": function () { dialog2.dialog( 'close' ); dialog.dialog( 'close' ); }, |
|||
start: tt.length }); |
|||
} |
|||
UnsignedHelper.appendToEditSummary(`unsigned [[Special:Diff/${revid}]]`); |
|||
} ); |
|||
dialog.dialog('close'); |
|||
} else { |
|||
}; |
|||
cb(); |
|||
if (/reverted|undid/i.test(comment)) { |
|||
} |
|||
var dialog2 = $('<div>') |
|||
} else { |
|||
.append( |
|||
revid=rr.revid; |
|||
'The ', |
|||
user=rr.user; |
|||
$('<a>').prop({ |
|||
ts=rr.timestamp; |
|||
href: '/w/index.php?diff=prev&oldid=' + revid, target: '_blank' }).text('found revision'), |
|||
comment=rr.parsedcomment; |
|||
' may be a revert: ', |
|||
if ( typeof(rr.anon) !== 'undefined' ) { |
|||
comment |
|||
template = 'Unsigned IP'; |
|||
) |
|||
} else if ( typeof(rr.temp) !== 'undefined' ) { |
|||
.dialog({ |
|||
// Seems unlikely "temporary" users will have a user page, so this seems the better template for them for now. |
|||
title: "Possible revert!", |
|||
template = 'Unsigned IP'; |
|||
modal: true, |
|||
} else { |
|||
buttons: { |
|||
template = 'Unsigned'; |
|||
"Use that revision": function () { |
|||
} |
|||
dialog2.dialog('close'); |
|||
cont(); |
|||
cb(); |
|||
} |
|||
}, |
|||
return; |
|||
"Keep looking": function () { |
|||
} |
|||
dialog2.dialog('close'); |
|||
dialog.html('<span style="color:red"><b>Error:</b> No revisions found in the page!</span>'); |
|||
cont(); |
|||
}, |
|||
}, |
|||
error:function(xhr,textStatus,errorThrown){ |
|||
"Cancel": function () { |
|||
if(!dialog.dialog('isOpen')) return; |
|||
dialog2.dialog('close'); |
|||
dialog.html('<span style="color:red"><b>Error:</b> '+textStatus+' '+errorThrown+'</span>'); |
|||
dialog.dialog('close'); |
|||
} |
|||
}, |
|||
}; |
|||
} |
|||
if(f.elements.baseRevId) q.data.rvstartid=f.elements.baseRevId.value; |
|||
}); |
|||
$.ajax(q); |
|||
} else { |
|||
}); |
|||
cb(); |
|||
evt.preventDefault(); |
|||
} |
|||
evt.stopPropagation(); |
|||
} else { |
|||
return false; |
|||
revid = rr.revid; |
|||
} |
|||
user = rr.user; |
|||
ts = rr.timestamp; |
|||
comment = rr.parsedcomment; |
|||
if (typeof (rr.anon) !== 'undefined') { |
|||
template = 'Unsigned IP'; |
|||
} else if (typeof (rr.temp) !== 'undefined') { |
|||
// Seems unlikely "temporary" users will have a user page, so this seems the better template for them for now. |
|||
template = 'Unsigned IP'; |
|||
} else { |
|||
template = 'Unsigned'; |
|||
} |
|||
cont(); |
|||
} |
|||
return; |
|||
} |
|||
dialog.html('<span style="color:red"><b>Error:</b> No revisions found in the page!</span>'); |
|||
}, |
|||
error: function (xhr, textStatus, errorThrown) { |
|||
if (!dialog.dialog('isOpen')) |
|||
return; |
|||
dialog.html('<span style="color:red"><b>Error:</b> ' + textStatus + ' ' + errorThrown + '</span>'); |
|||
} |
|||
}; |
|||
if (f.elements.baseRevId) |
|||
q.data.rvstartid = f.elements.baseRevId.value; |
|||
$.ajax(q); |
|||
}); |
|||
evt.preventDefault(); |
|||
evt.stopPropagation(); |
|||
return false; |
|||
} |
|||
}; |
}; |
||
if(!window.charinsertCustom) |
if (!window.charinsertCustom) |
||
window.charinsertCustom = {}; |
|||
window.charinsertCustom['Insert'] |
if (!window.charinsertCustom['Insert']) |
||
window.charinsertCustom['Insert'] = ''; |
|||
window.charinsertCustom[' |
window.charinsertCustom['Insert'] += ' \x7b\x7bunsigned\x7d\x7d\x10UnsignedHelper.addUnsignedTemplate'; |
||
if (!window.charinsertCustom['Wiki markup']) |
|||
if(window.updateEditTools) window.updateEditTools(); |
|||
window.charinsertCustom['Wiki markup'] = ''; |
|||
window.charinsertCustom['Wiki markup'] += ' \x7b\x7bunsigned\x7d\x7d\x10UnsignedHelper.addUnsignedTemplate'; |
|||
if (window.updateEditTools) |
|||
window.updateEditTools(); |
|||