Wikipedysta:MarMi wiki/disFixer.js i MediaWiki:Gadget-disFixer.js: Różnica pomiędzy stronami
Appearance
(Różnica między stronami)
Usunięta treść Dodana treść
MarMi wiki (dyskusja | edycje) test |
docs minor |
||
Linia 1: | Linia 1: | ||
var disFixerVersion = '2.8.2'; |
|||
/* |
/* |
||
DisFixer - disambig links fixer. |
|||
by Matma Rex |
|||
disFixer v. 2.2.15 |
|||
Co-authors: ToSter, Nux |
|||
by Matma Rex, zmiany by ToSter |
|||
See also: https://xtools.wmcloud.org/articleinfo/pl.wikipedia.org/MediaWiki:Gadget-disFixer.js?uselang=pl |
|||
cc-by-sa 3.0 |
|||
License CC-BY-SA 3.0: https://creativecommons.org/licenses/by-sa/3.0/deed |
|||
Użycie: wybierz disFixer w preferencjach |
Użycie: wybierz disFixer w preferencjach |
||
Linia 9: | Linia 11: | ||
Szerszy opis: [[Wikipedia:Narzędzia/disFixer]]. |
Szerszy opis: [[Wikipedia:Narzędzia/disFixer]]. |
||
== Hooks (advanced customization) == |
|||
Main hooks (search other with "userjs.disFixer"): |
|||
<li>userjs.disFixer.actioninit |
|||
<li>userjs.disFixer.actionready |
|||
=== Be carefull with actioninit === |
|||
mw.hook('userjs.disFixer.actioninit').add(function (disFixer, disambigs, redirs) { |
|||
console.log('disFixer.actioninit'); |
|||
// Replace one of the functions (class methods). |
|||
// Note! Any function might change and you might need to adjust yours in future. |
|||
disFixer.disHeaderButton = function(title) |
|||
{ |
|||
$('#content').prepend('<input id="disBeginButton" type="submit" class="disFixerButton" value="" title="'+title+'" />'); |
|||
var $button = $('#disBeginButton'); |
|||
return $button; |
|||
}; |
|||
}); |
|||
Note! Changing some functions and `disHeaderButton` specifically might be tricky as you need to register your function before the button is created. |
|||
So you might want to use `mw.loader` to load disFixer __after__ registering your function. |
|||
=== Using actionready === |
|||
Another approach is to adjust things when the main action is ready. |
|||
// move exisiting button (should preserve events, might brake styles) |
|||
mw.hook('userjs.disFixer.actionready').add(function (disFixer, disambigs, redirs) { |
|||
console.log('disFixer.actionready'); |
|||
if (disBeginButton) { |
|||
document.querySelector('#content').prepend(disBeginButton); |
|||
} |
|||
}); |
|||
== TODO == |
|||
TODO: Should remove most if not all dis* globals. |
|||
<li>Should make them class properties or pass as parameters. |
|||
<li>Should make sure other gadgets don't use them. |
|||
<li>Might expose some of them via hooks. |
|||
<nowiki> |
|||
*/ |
*/ |
||
/* globals $, jQuery, mw, OO */ |
|||
/* globals gConfig, jsMsg, markDisambigsGadget */ |
|||
/* globals dis, disRedirs, disResolvedRedirs, disHighlightedLinkTimeout, disHighlightedLink */ |
|||
/* eslint-disable no-redeclare */ |
|||
/* eslint-disable indent */ |
|||
/* eslint-disable no-useless-escape */ |
|||
/* eslint-disable no-empty */ |
|||
/** |
|||
// FIXME: Use mw.messages.set see example [[s:MediaWiki:Gadget-iw-links.js]] |
|||
* The strings of DisFixer. |
|||
* Translate this! |
|||
*/ |
|||
var disStr = window.disStr = |
|||
{ |
{ |
||
name: 'disFixer', |
name: 'disFixer', |
||
Linia 49: | Linia 104: | ||
}; |
}; |
||
mw.loader.using(['ext.gadget.gConfig', 'jquery.textSelection'], function(){ |
mw.loader.using(['ext.gadget.gConfig', 'jquery.textSelection'], function(){ |
||
var disPopups = false; |
|||
mw.hook('userjs.popups.completed').add(function(pg){ |
|||
disPopups = pg; |
|||
}); |
|||
// ustawienia |
// ustawienia |
||
gConfig.register('disFixer', { name: disStr.name, descriptionPage: disStr.descriptionPage }, [ |
gConfig.register('disFixer', { name: disStr.name, descriptionPage: disStr.descriptionPage }, [ |
||
{ |
|||
name: 'fixIfRedirsOnly', |
|||
desc: 'Wyświetl przycisk także wtedy, gdy do poprawy są same przekierowania.', |
|||
type: 'boolean', |
|||
deflt: false, |
|||
legacy: [window, 'disFixIfRedirsOnly'] |
|||
}, |
|||
{ |
{ |
||
name: 'markAsMinor', |
name: 'markAsMinor', |
||
Linia 73: | Linia 126: | ||
deflt: false, |
deflt: false, |
||
legacy: [window, 'disCodeCleanup'] |
legacy: [window, 'disCodeCleanup'] |
||
}, |
|||
{ |
|||
name: 'headerButton', |
|||
desc: 'Wyświetl przycisk w nagłówku (zamiast w narzędziach strony).', |
|||
type: 'boolean', |
|||
deflt: false, |
|||
}, |
|||
{ |
|||
name: 'fixIfRedirsOnly', |
|||
desc: 'Wyświetl przycisk także wtedy, gdy do poprawy są same przekierowania.', |
|||
type: 'boolean', |
|||
deflt: false, |
|||
legacy: [window, 'disFixIfRedirsOnly'] |
|||
}, |
|||
{ |
|||
name: 'fixAllContentTypes', |
|||
desc: '[zaawansowane] Wyświetl przycisk także dla Modułów, JS i innych typów zawartości (nie tylko wikitext).', |
|||
type: 'boolean', |
|||
deflt: false, |
|||
}, |
}, |
||
{ |
{ |
||
Linia 83: | Linia 155: | ||
]); |
]); |
||
class DisFixer { |
|||
function disCallApi( request, callback ) { |
|||
disCallApi( request, callback ) { |
|||
request.format = 'json'; |
request.format = 'json'; |
||
jQuery.post( mw.util.wikiScript( 'api' ), request, callback, 'json' ); |
jQuery.post( mw.util.wikiScript( 'api' ), request, callback, 'json' ); |
||
} |
} |
||
disScrollToLink (target) |
|||
{ |
{ |
||
console.log("disHighlightedLinkTimeout",typeof disHighlightedLinkTimeout=='undefined'?typeof disHighlightedLinkTimeout:disHighlightedLinkTimeout); |
|||
console.log("disHighlightedLink", typeof disHighlightedLink=='undefined'?typeof disHighlightedLink:disHighlightedLink); |
|||
if (typeof disHighlightedLinkTimeout != 'undefined' && typeof disHighlightedLink != 'undefined') { |
if (typeof disHighlightedLinkTimeout != 'undefined' && typeof disHighlightedLink != 'undefined') { |
||
clearTimeout(disHighlightedLinkTimeout); |
clearTimeout(disHighlightedLinkTimeout); |
||
disHighlightedLink.css('background', ''); |
disHighlightedLink.css('background', ''); |
||
} |
} |
||
//console.log("disHighlightedLinkTimeout",typeof disHighlightedLinkTimeout); |
|||
//console.log("disHighlightedLink", typeof disHighlightedLink); |
|||
//console.log("window.disHighlightedLink",window.disHighlightedLink); |
|||
window.disHighlightedLink = $(dis).filter('[title="' + target.replace('"', '\\"') + '"]').first(); |
window.disHighlightedLink = $(dis).filter('[title="' + target.replace('"', '\\"') + '"]').first(); |
||
//console.log("window.disHighlightedLink",window.disHighlightedLink); |
|||
disHighlightedLink.css('background', 'red'); |
disHighlightedLink.css('background', 'red'); |
||
Linia 107: | Linia 175: | ||
disHighlightedLink.css('background', ''); |
disHighlightedLink.css('background', ''); |
||
}, 3000); |
}, 3000); |
||
//console.log("disHighlightedLink",disHighlightedLink); |
|||
disHighlightedLink.get(0).scrollIntoView(); |
disHighlightedLink.get(0).scrollIntoView(); |
||
} |
} |
||
/** Init action (main button etc). */ |
|||
function disLoadFixer() |
|||
initAction() |
|||
{ |
{ |
||
// Nie poprawiaj linków w szablonach [[Szablon:Inne znaczenia]], [[Szablon:Przekierowanie]], itp. |
// Nie poprawiaj linków w szablonach [[Szablon:Inne znaczenia]], [[Szablon:Przekierowanie]], itp. |
||
Linia 124: | Linia 192: | ||
} |
} |
||
// fire hook to allow advanced customization |
|||
// usage: |
|||
// mw.hook('userjs.disFixer.actioninit').add(function (disFixer, disambigs, redirs) {...}); |
|||
mw.hook('userjs.disFixer.actioninit').fire(this, dis, disRedirs); |
|||
this.disMainButton(); |
|||
// main form container |
|||
this.disMainContainer(); |
|||
mw.hook('userjs.disFixer.actionready').fire(this, dis, disRedirs); |
|||
} |
|||
/** Prepare main container. */ |
|||
disMainContainer() |
|||
{ |
|||
var main = '<div id="disMainContainer"></div>'; |
|||
if(mw.config.get('skin') !== 'vector-2022') $('h1:first').before(main); |
|||
else $('.mw-body-header:first').before(main); |
|||
} |
|||
/** |
|||
* Prepare header button. |
|||
* |
|||
* @param {String} title Title/tooltip (with version). |
|||
* |
|||
* @returns jQuery element. Must be an input element! |
|||
*/ |
|||
disHeaderButton(title) |
|||
{ |
|||
var el = '<input id="disBeginButton" type="submit" class="disFixerButton" value="" title="'+title+'" />'; |
|||
// W Wektorze 2022 <h1> znajduje się w kontenerze typu flex, więc nie wkładaj tam przycisku, żeby nie zaburzyć układu |
|||
if(mw.config.get('skin') !== 'vector-2022') $('h1:first').before(el); |
|||
else $('.mw-body-header:first').append(el); |
|||
var $button = $('#disBeginButton'); |
|||
return $button; |
|||
} |
|||
/** Prepare main button. */ |
|||
disMainButton() |
|||
{ |
|||
var txt = disStr.fixLinks; |
var txt = disStr.fixLinks; |
||
if (dis.length === 0) { |
if (dis.length === 0) { |
||
Linia 131: | Linia 241: | ||
txt = disStr.fixLinksDisam; |
txt = disStr.fixLinksDisam; |
||
} |
} |
||
var el = '<input id="disBeginButton" type="submit" class="disFixerButton" value="' + txt + '" />'; |
|||
var title = 'disFixer v.'+disFixerVersion; |
|||
// W Wektorze 2022 <h1> znajduje się w kontenerze typu flex, więc nie wkładaj tam przycisku, żeby nie zaburzyć układu |
|||
if(mw.config.get('skin') !== 'vector-2022') $('h1:first').before(el); |
|||
// header button |
|||
else $('.mw-body-header:first').before(el); |
|||
if (gConfig.get('disFixer', 'headerButton')) { |
|||
var $button = this.disHeaderButton(title); |
|||
$('#disBeginButton').click(function() { |
|||
$button.val(txt); |
|||
try { |
|||
$button.click(() => { |
|||
$('#disBeginButton').val(disStr.wait); |
|||
try { |
|||
$button.val(disStr.wait); |
|||
disBegin(); |
|||
$button.prop('disabled', true); |
|||
}); |
|||
} catch(ex){ |
|||
console.warn('[disFixer]', 'Click problem', ex); |
|||
} |
|||
this.disBegin(function(hasAllPages) { |
|||
if (!hasAllPages) { |
|||
$button.val(txt); |
|||
$button.prop('disabled', false); |
|||
} |
|||
else { |
|||
$button.remove(); |
|||
} |
|||
}); |
|||
}); |
|||
} |
|||
// button in page tools |
|||
else { |
|||
var portletId = mw.config.get('skin') === 'timeless' ? 'p-pagemisc' : 'p-tb'; |
|||
var link = mw.util.addPortletLink(portletId, '#', txt, 't-disbeginaction', null); |
|||
link.title = title; |
|||
var $link = $(link); |
|||
$link.click((e) => { |
|||
e.preventDefault(); |
|||
if ($link.data('waiting')) { |
|||
console.warn('[disFixer]', 'Still waiting...'); |
|||
return; |
|||
} |
|||
$link.data('waiting', true); |
|||
this.disBegin(function() { |
|||
$link.data('waiting', false); |
|||
}); |
|||
}); |
|||
} |
|||
} |
} |
||
/** |
|||
function disBegin() |
|||
* |
|||
* @param {Function?} callback [optional] Callback for main disambig query. |
|||
*/ |
|||
disBegin(callback) |
|||
{ |
{ |
||
const me = this; |
|||
var el = '<div id="disRedirsStatus"></div>'; |
|||
// W Wektorze 2022 <h1> znajduje się w kontenerze typu flex, więc nie wkładaj tam komunikatu, żeby nie zaburzyć układu |
|||
if(mw.config.get('skin') !== 'vector-2022') $('h1:first').before(el); |
|||
else $('.mw-body-header:first').before(el); |
|||
$('#disMainContainer').empty(); |
|||
var el = '<div id="disRedirsStatus"></div>'; |
|||
$('#disRedirsStatus').append('<input type="checkbox" id="disFixRedirsCheckbox" style="display:none" />'); |
$('#disRedirsStatus').append('<input type="checkbox" id="disFixRedirsCheckbox" style="display:none" />'); |
||
if (window.disRedirs.length === 0) { |
if (window.disRedirs.length === 0) { |
||
Linia 159: | Linia 305: | ||
$('#disRedirsStatus').append(disStr.fixingInProgress); |
$('#disRedirsStatus').append(disStr.fixingInProgress); |
||
} |
} |
||
$('#disMainContainer').append(el); |
|||
var titles = []; |
var titles = []; |
||
var i; |
var i; |
||
for (i = 0; i < dis.length; i++) { |
for (i = 0; i < dis.length; i++) { |
||
var pageTitle = dis[i].title; |
|||
if (typeof pageTitle == 'string' && pageTitle.length) { |
|||
// avoid dupls |
|||
if (titles.indexOf(pageTitle) < 0) { |
|||
titles.push(pageTitle); |
|||
} |
|||
} |
|||
} |
} |
||
console.log("titles",titles.join( '|' )); |
|||
// not great when there is more then one link in the list item |
|||
// use action=render instead? (get text as option-label and get only first link as propoused link?) |
|||
disCallApi( { |
|||
this.disCallApi( { |
|||
action: 'query', |
action: 'query', |
||
prop: 'links', |
prop: 'links', |
||
Linia 175: | Linia 328: | ||
pllimit: 'max' |
pllimit: 'max' |
||
}, function( data ) { |
}, function( data ) { |
||
disCallback( data ); |
var hasAllPages = me.disCallback( data, titles.length ); |
||
if (typeof callback === 'function') callback(hasAllPages); |
|||
} ); |
} ); |
||
if (window.disRedirs.length > 0) { |
if (window.disRedirs.length > 0) { |
||
var titles2 = []; |
var titles2 = []; |
||
console.log("disRedirs",disRedirs); |
|||
for (i = 0; i < disRedirs.length; i++) { |
for (i = 0; i < disRedirs.length; i++) { |
||
titles2.push(disRedirs[i].title); |
titles2.push(disRedirs[i].title); |
||
Linia 187: | Linia 339: | ||
if (gConfig.get('disFixer', 'useOldRedirFixing')) { |
if (gConfig.get('disFixer', 'useOldRedirFixing')) { |
||
disCallApi( { |
this.disCallApi( { |
||
action: 'query', |
action: 'query', |
||
redirects: '', |
redirects: '', |
||
titles: titles2.join( '|' ) |
titles: titles2.join( '|' ) |
||
}, function( data ) { |
}, function( data ) { |
||
disRedirCallback( data ); |
me.disRedirCallback( data ); |
||
} ); |
} ); |
||
} |
} |
||
else { |
else { |
||
disCallApi( { |
this.disCallApi( { |
||
action: 'query', |
action: 'query', |
||
prop: 'revisions', |
prop: 'revisions', |
||
Linia 202: | Linia 354: | ||
titles: titles2.join( '|' ) |
titles: titles2.join( '|' ) |
||
}, function( data ) { |
}, function( data ) { |
||
disRedirCallback2( data ); |
me.disRedirCallback2( data ); |
||
} ); |
} ); |
||
} |
} |
||
Linia 208: | Linia 360: | ||
} |
} |
||
/** Create OOUI style submit button */ |
|||
function disCallback(res) |
|||
disSubmitButton(text, danger) { |
|||
var submit = new OO.ui.ButtonInputWidget( { |
|||
label: text, |
|||
useInputTag: true, |
|||
type: "submit", |
|||
flags: [ |
|||
'primary', |
|||
(danger ? 'destructive' : 'progressive'), |
|||
], |
|||
} ); |
|||
return submit; |
|||
} |
|||
/** |
|||
* Use data read from disambig article. |
|||
* |
|||
* @param {Object} res Response from links query API. |
|||
* @returns |
|||
* <li>null upon error |
|||
* <li>true if there were all pages in the response. |
|||
* <li>false if there were some pages missing in the response. |
|||
*/ |
|||
disCallback(res, expectedCount) |
|||
{ |
{ |
||
if(!res) return; |
if(!res) return null; |
||
let reCount = (typeof res.query !== 'object') ? false : Object.values(res.query.pages).length; |
|||
let hasAllPages = true; |
|||
var el = '<div id="disContainer"><input type="submit" class="disFixerButton" value="' + disStr.fixRedirsOnly + |
|||
if (!reCount) { |
|||
'" style="font-weight:bold;color:red" /></div>'; |
|||
console.warn('[disFixer]', 'Disamb response seems empty. Api error?', res); |
|||
hasAllPages = false; |
|||
} else if (reCount !== expectedCount) { |
|||
console.warn('[disFixer]', 'Disamb returned less pages then expected. Api error?', res, {reCount, expectedCount}); |
|||
hasAllPages = false; |
|||
} |
|||
if (reCount || gConfig.get('disFixer', 'fixIfRedirsOnly')) { |
|||
var disContainer = document.createElement('div'); |
|||
disContainer.id = 'disContainer'; |
|||
this.disPrepareForm(disContainer, reCount ? res.query.pages : null); |
|||
document.querySelector('#disMainContainer').prepend(disContainer); |
|||
} |
|||
return hasAllPages; |
|||
} |
|||
/** Prepare form based on disamb response. */ |
|||
$('#disBeginButton').replaceWith(el); |
|||
disPrepareForm(disContainer, pages) { |
|||
if (!pages && gConfig.get('disFixer', 'fixIfRedirsOnly')) { |
|||
var submit = this.disSubmitButton(disStr.fixRedirsOnly, true); |
|||
$(disContainer).append(submit.$element); |
|||
submit.$element.click(() => {this.disSend()}); |
|||
} |
} |
||
else { |
else if (pages) { |
||
var addScrollToLink = $('#content').get(0).scrollIntoView; |
var addScrollToLink = $('#content').get(0).scrollIntoView; |
||
var pages = res.query.pages; |
|||
disContainer.innerHTML = '<div>' + disStr.fixDabs + '</div><dislist></dislist>'; |
|||
var list = disContainer.querySelector('dislist'); |
|||
var i; |
|||
for (var i in pages) { |
|||
if (Object.hasOwnProperty.call(pages, i)) { |
|||
var page = pages[i]; |
|||
if (isNaN(+i)) {console.log("i",i); continue;} //prototypes, damn |
|||
this.disPreparePage(list, page, addScrollToLink); |
|||
var page = pages[i]; |
|||
str += |
|||
'<label>' + page.title + ' ' + |
|||
'<a href="/w/index.php?title=' +encodeURIComponent(page.title) + '" title="' + disStr.viewDabPage + |
|||
'" target="_new">⇗</a>' + |
|||
(addScrollToLink ? '<a href="javascript:disScrollToLink(' + '\'' + page.title + |
|||
'\'' + ')" title="' + disStr.scrollToLink + '">⇓</a>' |
|||
: '') + |
|||
': <select id="input-dislink' + page.title + '">'; |
|||
str += '<option>' + page.title + '</option>'; |
|||
if (typeof page.links == 'undefined') { |
|||
page.links = []; |
|||
} |
|||
for (i = 0; i < page.links.length; i++) { |
|||
str += '<option>' + page.links[i].title + '</option>'; |
|||
} |
} |
||
} |
|||
// popups enhance |
|||
str += '<option style="color:grey">[' + disStr.delink + ']</option>'; |
|||
if (disPopups && disPopups.fn.addTooltip) { |
|||
str += '</select> <input type="submit" class="disFixerButton" onclick="disSetLinkTarget(this,' + |
|||
var anchors = list.querySelectorAll('dislabel a[target="_new"]'); |
|||
'\'' + page.title + '\'' + ')" value="' + disStr.otherTarget + '" /></label><br />'; |
|||
if (anchors.length) { |
|||
anchors.forEach(function(anchor){ |
|||
disPopups.fn.addTooltip(anchor); |
|||
}); |
|||
} |
|||
} |
} |
||
var submit = this.disSubmitButton(disStr.fixButton); |
|||
$('#disBeginButton').replaceWith('<div id="disContainer">' + str + |
|||
$(disContainer).append(submit.$element); |
|||
'<input type="submit" class="disFixerButton" value="' + disStr.fixButton + '" /></div>'); |
|||
$ |
submit.$element.click(() => {this.disSend()}); |
||
} |
} |
||
} |
|||
/** |
|||
* Prepare page to select link from disambig. |
|||
* |
|||
* @example Page object: |
|||
{ |
|||
"pageid": 5388688, |
|||
"ns": 0, |
|||
"title": "Charmaine Smith", |
|||
"links": [{ |
|||
"ns": 0, |
|||
"title": "Charmaine Smith (bowls)" |
|||
}, |
|||
{ |
|||
"ns": 0, |
|||
"title": "Charmaine Smith (związek rugby)" |
|||
} |
|||
] |
|||
} |
|||
* @param {Object} page Disamb data. |
|||
*/ |
|||
disPreparePage(list, page, addScrollToLink) { |
|||
// create name + links |
|||
list.insertAdjacentHTML('beforeend', |
|||
'<dislabel>' + page.title + ' ' |
|||
+ '<a href="/w/index.php?title=' + encodeURIComponent(page.title) + '"' |
|||
+ ' title="' + disStr.viewDabPage + '"' |
|||
+ ' target="_new">⇗</a>' |
|||
+ (addScrollToLink |
|||
? '<a href="javascript:this.disScrollToLink(\'' + page.title + '\')" title="' + disStr.scrollToLink + '">⇓</a>' |
|||
: '' |
|||
) |
|||
+ '</dislabel><disitem data-from="'+page.title+'" />' |
|||
); |
|||
// prepare field |
|||
var titles = []; |
|||
if (Array.isArray(page.links)) { |
|||
titles = page.links.map(p=>p.title).sort(); |
|||
} |
|||
// option to remove links |
|||
titles.unshift('[' + disStr.delink + ']'); |
|||
var combo = new OO.ui.ComboBoxInputWidget( { |
|||
value: '', |
|||
options: titles.map(title=>({data:title})), |
|||
menu: { |
|||
filterFromInput: true, |
|||
filterMode: 'substring', |
|||
}, |
|||
} ); |
|||
$( 'disitem:last-of-type', list ).append( combo.$element ); |
|||
} |
} |
||
disRedirCallback(res) |
|||
{ |
{ |
||
if (typeof res.query !== 'object') { |
|||
window.disResolvedRedirs = []; |
|||
console.warn('[disFixer]', 'Unable to get redirects, api error?'); |
|||
return; |
|||
} |
|||
window.disResolvedRedirs = res.query.redirects; |
window.disResolvedRedirs = res.query.redirects; |
||
var el = '<input type="checkbox" id="disFixRedirsCheckbox" checked="checked" /> ' |
var el = '<input type="checkbox" id="disFixRedirsCheckbox" checked="checked" /> ' |
||
'<label for="disFixRedirsCheckbox">' + disStr.fixRedirsCheckbox |
+ '<label for="disFixRedirsCheckbox">' + disStr.fixRedirsCheckbox |
||
' (' + disResolvedRedirs.length + ')</label>'; |
+ ' (' + disResolvedRedirs.length + ')</label>'; |
||
$('#disRedirsStatus').html(el); |
$('#disRedirsStatus').html(el); |
||
$('#disRedirsStatus').css('color', 'green'); |
$('#disRedirsStatus').css('color', 'green'); |
||
} |
} |
||
disRedirCallback2(res) |
|||
{ |
{ |
||
if (typeof res.query !== 'object') { |
|||
window.disResolvedRedirs = []; |
|||
console.warn('[disFixer]', 'Unable to get redirects2, api error?'); |
|||
return; |
|||
} |
|||
var pages = res.query.pages; |
var pages = res.query.pages; |
||
console.log("pages",pages); |
|||
window.disResolvedRedirs = []; |
window.disResolvedRedirs = []; |
||
for (var i in pages) { |
for (var i in pages) { |
||
if (isNaN( |
if (isNaN(i)) continue; |
||
{ |
|||
console.log("i",i); |
|||
console.log("NaN",pages[i]); |
|||
continue; |
|||
} |
|||
var f = pages[i].title; |
var f = pages[i].title; |
||
Linia 293: | Linia 531: | ||
} |
} |
||
var el = '<input type="checkbox" id="disFixRedirsCheckbox" checked="checked" /> ' |
var el = '<input type="checkbox" id="disFixRedirsCheckbox" checked="checked" /> ' |
||
'<label for="disFixRedirsCheckbox">' + disStr.fixRedirsCheckbox |
+ '<label for="disFixRedirsCheckbox">' + disStr.fixRedirsCheckbox |
||
' (' + disResolvedRedirs.length + ')</label>'; |
+ ' (' + disResolvedRedirs.length + ')</label>'; |
||
$('#disRedirsStatus').html(el); |
$('#disRedirsStatus').html(el); |
||
$('#disRedirsStatus').css('color', 'green'); |
$('#disRedirsStatus').css('color', 'green'); |
||
} |
} |
||
/** Send fixes to the edit form (doesn't save yet). */ |
|||
function disSend() |
|||
disSend() |
|||
{ |
{ |
||
var |
var items = $('#disContainer disitem'); |
||
var inputs = $('#disContainer disitem input'); |
|||
var toFix = []; |
var toFix = []; |
||
var from, to; |
|||
for (var i = 0; i < inputs.length; i++) { |
for (var i = 0; i < inputs.length; i++) { |
||
from = |
var from = items[i].getAttribute('data-from'); |
||
to = inputs[i].value; |
var to = inputs[i].value; |
||
if (from == to) continue; |
if (from == to || to == "") continue; |
||
toFix.push(from + '~' + to); |
toFix.push(from + '~' + to); |
||
} |
} |
||
Linia 315: | Linia 554: | ||
if (typeof disResolvedRedirs != 'undefined' && $('#disFixRedirsCheckbox').prop('checked')) { |
if (typeof disResolvedRedirs != 'undefined' && $('#disFixRedirsCheckbox').prop('checked')) { |
||
toFix = []; |
toFix = []; |
||
for (i = 0; i < disResolvedRedirs.length; i++) { |
for (var i = 0; i < disResolvedRedirs.length; i++) { |
||
from = disResolvedRedirs[i].from; |
var from = disResolvedRedirs[i].from; |
||
to = disResolvedRedirs[i].to; |
var to = disResolvedRedirs[i].to; |
||
toFix.push(from + '~' + to); |
toFix.push(from + '~' + to); |
||
} |
} |
||
Linia 323: | Linia 562: | ||
} |
} |
||
window.location = mw.util.getUrl( mw.config.get('wgPageName'), { action: 'submit' } ); |
window.location = mw.util.getUrl( mw.config.get('wgPageName'), { action: 'submit', disFixer: 1 } ); // edit, but submit-action works also to disable VE code editor |
||
console.log("loc",window.location.search); |
|||
} |
} |
||
disOnloadEdit(wpSkObject) |
|||
{ |
{ |
||
var whatIsFixed = []; |
var whatIsFixed = []; |
||
Linia 334: | Linia 572: | ||
return; |
return; |
||
} |
} |
||
var str = $('#wpTextbox1').textSelection('getContents'); |
var str = $('#wpTextbox1').textSelection('getContents'); |
||
str = str.replace(/\r\n/g,'\n').replace(/\s*$/, ''); |
str = str.replace(/\r\n/g,'\n').replace(/\s*$/, ''); |
||
if (gConfig.get('disFixer', 'codeCleanup') && typeof |
if (gConfig.get('disFixer', 'codeCleanup') && typeof wpSkObject !== 'undefined') { |
||
str = |
str = wpSkObject.cleaner(str); |
||
} |
} |
||
else { |
else { |
||
str = disCleanLinks(str); |
str = this.disCleanLinks(str); |
||
} |
} |
||
var linksDisam = readCookie('disFixDis' + mw.config.get('wgPageName')); |
var linksDisam = readCookie('disFixDis' + mw.config.get('wgPageName')); |
||
console.log("linksDisam",linksDisam); |
|||
var linksRedir = readCookie('disFixRedirs' + mw.config.get('wgPageName')); |
var linksRedir = readCookie('disFixRedirs' + mw.config.get('wgPageName')); |
||
console.log("linksRedir",linksRedir); |
|||
var links = |
var links = new Array(); |
||
if (linksDisam != null && linksDisam != 'undefined' && linksDisam != '') { |
if (linksDisam != null && linksDisam != 'undefined' && linksDisam != '') { |
||
links = linksDisam.split('|'); |
links = linksDisam.split('|'); |
||
Linia 378: | Linia 613: | ||
} |
} |
||
} |
} |
||
str = disCleanLinks(str); |
str = this.disCleanLinks(str); |
||
eraseCookie('disFixDis' + mw.config.get('wgPageName')); |
eraseCookie('disFixDis' + mw.config.get('wgPageName')); |
||
Linia 384: | Linia 619: | ||
if (whatIsFixed.length == 0) { |
if (whatIsFixed.length == 0) { |
||
console.log("whatIsFixed",whatIsFixed); |
|||
return; |
return; |
||
} |
} |
||
console.log("setContents"); |
|||
console.log(str); |
|||
$('#wpTextbox1').textSelection('setContents', str); |
$('#wpTextbox1').textSelection('setContents', str); |
||
var newval = $('#wpSummary').val() + '[[' + disStr.descriptionPage + '|' + disStr.autosummaryBegin |
|||
+ ' ' + whatIsFixed.join(disStr.and) + ']]' + (gConfig.get('disFixer', 'codeCleanup') ? ', [[WP:SK]]' : ''); |
|||
var newval = $('#wpSummary').val() + '[[' + disStr.descriptionPage + '|' + disStr.autosummaryBegin + |
|||
' ' + whatIsFixed.join(disStr.and) + ']]' + (gConfig.get('disFixer', 'codeCleanup') ? ', [[WP:SK]]' : ''); |
|||
$('#wpSummary').val(newval); |
$('#wpSummary').val(newval); |
||
if (gConfig.get('disFixer', 'markAsMinor')) { |
if (gConfig.get('disFixer', 'markAsMinor')) { |
||
Linia 401: | Linia 632: | ||
} |
} |
||
/* |
|||
window.disSetLinkTarget = function(el, disName) //helper - for buttons |
window.disSetLinkTarget = function(el, disName) //helper - for buttons |
||
{ |
{ |
||
Linia 413: | Linia 645: | ||
sel.append(opt); |
sel.append(opt); |
||
sel.find('option:last').prop('selected', 'selected'); |
sel.find('option:last').prop('selected', 'selected'); |
||
} |
} |
||
*/ |
|||
// based on Nux's code cleanup |
// based on Nux's code cleanup |
||
// http://pl.wikipedia.org/wiki/Wikipedysta:Nux/wp_sk.js |
// http://pl.wikipedia.org/wiki/Wikipedysta:Nux/wp_sk.js |
||
disCleanLinks(str) |
|||
{ |
{ |
||
//najprostszy cleanup, głównie po to, żeby regeksy do poprawy linków mogły być prostsze |
//najprostszy cleanup, głównie po to, żeby regeksy do poprawy linków mogły być prostsze |
||
Linia 425: | Linia 658: | ||
{ |
{ |
||
try { |
try { |
||
name = decodeURIComponent(name); |
var name = decodeURIComponent(name); |
||
anchor = decodeURIComponent(anchor.replace(/\.([0-9A-F]{2})\.([0-9A-F]{2})/g, '%$1%$2')); |
var anchor = decodeURIComponent(anchor.replace(/\.([0-9A-F]{2})\.([0-9A-F]{2})/g, '%$1%$2')); |
||
a = '[[' + name + anchor + end; |
a = '[[' + name + anchor + end; |
||
} catch(err){} // błąd na linkach typu [[%]] |
} catch(err){} // błąd na linkach typu [[%]] |
||
Linia 455: | Linia 688: | ||
} |
} |
||
} |
|||
// http://www.quirksmode.org/js/cookies.html |
// http://www.quirksmode.org/js/cookies.html |
||
Linia 467: | Linia 701: | ||
else |
else |
||
{ |
{ |
||
var expires; |
|||
// fall back to cookies |
// fall back to cookies |
||
var expires = ""; |
|||
if (days) { |
if (days) { |
||
var date = new Date(); |
var date = new Date(); |
||
Linia 474: | Linia 708: | ||
expires = "; expires=" + date.toGMTString(); |
expires = "; expires=" + date.toGMTString(); |
||
} |
} |
||
else expires = ""; |
|||
document.cookie = escape(name) + "=" + escape(value) + expires + "; path=/"; |
document.cookie = escape(name) + "=" + escape(value) + expires + "; path=/"; |
||
} |
} |
||
Linia 502: | Linia 735: | ||
createCookie(name, "", -1); |
createCookie(name, "", -1); |
||
} |
} |
||
// main |
|||
let disFixer = new DisFixer(); |
|||
// export for a JS-link |
|||
// TODO: re-write to onclick? |
|||
window.disScrollToLink = disFixer.disScrollToLink; |
|||
/* |
/* |
||
AND FINALLY ONLOAD |
AND FINALLY ONLOAD |
||
*/ |
*/ |
||
var isFirstEdit = location.href.indexOf('disFixer=1') > 0; |
|||
const wgAction=mw.config.get( 'wgAction' ); |
|||
if ( mw.config.get( 'wgAction' ) == 'submit' && isFirstEdit ) { |
|||
console.log("wgAction",wgAction); |
|||
$( function() { |
|||
if ( wgAction == 'edit' || wgAction == 'submit' ) { |
|||
if ( gConfig.get('disFixer', 'codeCleanup')) { |
|||
jQuery( document ).ready( function() { |
|||
// Wait for WP:SK |
|||
if ( gConfig.get('disFixer', 'codeCleanup') && typeof wp_sk === 'undefined' ) { |
|||
mw.hook('userjs.wp_sk.ready').add(function (wp_sk) { |
|||
// Wait for RL to load WP:SK |
|||
disFixer.disOnloadEdit(wp_sk); |
|||
mw.loader.using( 'ext.gadget.wp_sk', function() { |
|||
console.log("ext.gadget.wp_sk"); |
|||
disOnloadEdit(); |
|||
} ); |
} ); |
||
} else { |
} else { |
||
disFixer.disOnloadEdit(); |
|||
/* console.log("!wp_sk"); |
|||
if( $('#wpTextbox1').length === 0 ) { |
|||
console.log("#wpTextbox1=0"); |
|||
// This is possible with the new source editor beta feature; we don't handle it (yet) |
|||
//return; |
|||
// wikiEditor.toolbarReady |
|||
mw.hook('ve.activationComplete').add(function () { |
|||
var surfaceModel = ve.init.target.getSurface().getModel(); |
|||
if (surfaceModel) { |
|||
var hist=surfaceModel.getHistory( ); |
|||
console.log(hist.length); |
|||
hist.forEach(function(){ |
|||
surfaceModel.undo( ); |
|||
}); |
|||
/*surfaceModel.removeDocStateAndChanges( ); |
|||
surfaceModel.truncateUndoStack( ); |
|||
console.log(surfaceModel.getHistory( ));*/ |
|||
/* } |
|||
disOnloadEdit(); |
|||
} ); |
|||
} |
|||
else */ |
|||
disOnloadEdit(); |
|||
} |
} |
||
} ); |
} ); |
||
Linia 549: | Linia 762: | ||
// Do not show interface, when previewing the page or on talk pages |
// Do not show interface, when previewing the page or on talk pages |
||
if ( |
if ( |
||
wgAction != 'submit' && mw.config.get( 'wgNamespaceNumber' ) % 2 == 0 |
mw.config.get( 'wgAction' ) != 'submit' && mw.config.get( 'wgNamespaceNumber' ) % 2 == 0 |
||
&& (gConfig.get('disFixer', 'fixAllContentTypes') || mw.config.get( 'wgPageContentModel' ) == "wikitext") // only plain code |
|||
&& mw.config.get( 'wgNamespaceNumber' ) !== 2600 // Flow's 'Topic' namespace |
|||
) { |
) { |
||
// Make sure the required dependency is loaded |
// Make sure the required dependency is loaded |
||
mw.loader.using( ['ext.gadget.mark-disambigs-core'], function() { |
mw.loader.using( ['ext.gadget.mark-disambigs-core', 'oojs-ui-core'], function() { |
||
if ( typeof( markDisambigsGadget ) === "undefined" ) { |
if ( typeof( markDisambigsGadget ) === "undefined" ) { |
||
jQuery( document ).ready( function() { |
jQuery( document ).ready( function() { |
||
console.log("jsMsg"); |
|||
jsMsg( disStr.markDisambigsMissing ); |
jsMsg( disStr.markDisambigsMissing ); |
||
} ); |
} ); |
||
Linia 562: | Linia 775: | ||
// register callback |
// register callback |
||
markDisambigsGadget.addCallback( function() { |
markDisambigsGadget.addCallback( function() { |
||
disFixer.initAction(); |
|||
console.log("+disLoadFixer"); |
|||
disLoadFixer(); |
|||
console.log("-disLoadFixer"); |
|||
} ); |
} ); |
||
} |
} |
||
Linia 570: | Linia 781: | ||
} |
} |
||
}); // end mw.loader. |
}); // end mw.loader: ext.gadget.gConfig, jquery.textSelection |
||
// </nowiki> |