MediaWiki:Gadget-EdytujWNondanych.js: Różnice pomiędzy wersjami
Z Nonsensopedii, polskiej encyklopedii humoru
M (w sumie) |
Ostrzyciel (dyskusja • edycje) M (ech) |
||
(Nie pokazano 15 wersji utworzonych przez 2 użytkowników) | |||
Linia 1: | Linia 1: | ||
var MAX_PAGES_IN_SMW_QUERY = 11, |
|||
MAX_PAGES_IN_EXIST_QUERY = 50; |
|||
if (mw.config.get("wgUserGroups").indexOf("sysop") != -1 || mw.config.get("wgUserGroups").indexOf("bot") != -1) |
|||
{ |
|||
MAX_PAGES_IN_EXIST_QUERY = 500; |
|||
} |
|||
$(document).ready(function() |
$(document).ready(function() |
||
{ |
{ |
||
Linia 4: | Linia 12: | ||
{ |
{ |
||
//przycisk "Edytuj w Nondanych" |
//przycisk "Edytuj w Nondanych" |
||
$("#ca-edit").before("<li id='ca-nondaneEdit'><span><a href='https://dane.nonsa.pl/wiki/" + |
$("#ca-edit").before("<li id='ca-nondaneEdit'><span><a href='https://dane.nonsa.pl/wiki/" + encodeURIComponent(mw.config.get("wgPageName")) + "?action=formedit'>Edytuj w Nondanych</a></span></li>"); |
||
//button pokazujący, czy dana strona została już uzupełniona w Nondanych |
//button pokazujący, czy dana strona została już uzupełniona w Nondanych |
||
var |
var title = mw.config.get("wgPageName").replace(/_/g, " "); |
||
var |
var filledPageCode = "<a href='https://dane.nonsa.pl/wiki/" + encodeURIComponent(mw.config.get("wgPageName")) + "?action=formedit'><img id='nondaneEditedButton' src='https://upload.wikimedia.org/wikipedia/commons/1/1e/Button_Icon_GreenForest.svg' width='20px' height='20px' style='padding: 0px 0px 3px 10px;' title='Ta strona została już uzupełniona w Nondanych' /></a>"; |
||
var |
var notFilledPageCode = "<a href='https://dane.nonsa.pl/wiki/" + encodeURIComponent(mw.config.get("wgPageName")) + "?action=formedit'><img id='nondaneEditedButton' src='https://upload.wikimedia.org/wikipedia/commons/0/07/Button_Icon_Red.svg' width='20px' height='20px' style='padding: 0px 0px 3px 10px;' title='Ta strona nie została jeszcze uzupełniona w Nondanych' /></a>"; |
||
isSinglePageFilled(title) |
|||
var api = new mw.ForeignApi("https://dane.nonsa.pl/api.php"); |
|||
.done(function(filled) |
|||
api.post( |
|||
{ |
|||
if (filled) |
|||
action: 'ask', |
|||
{ |
|||
format: 'json', |
|||
$("#firstHeading").append(filledPageCode); |
|||
query: '[[Nie posiada typu strony Gry::true]] [[' + tytul + ']]' |
|||
} |
|||
}).done(function(dane) |
|||
else |
|||
{ |
|||
{ |
|||
var wypelniona = (typeof dane.query.results[tytul] === "undefined"); |
|||
$("#firstHeading").append(notFilledPageCode); |
|||
if (wypelniona) |
|||
} |
|||
}); |
|||
parsePageLinks(); |
|||
} |
|||
}); |
|||
function isSinglePageFilled(title) |
|||
{ |
|||
var deferred = $.Deferred(); |
|||
mw.loader.using("mediawiki.ForeignApi", function() |
|||
{ |
|||
var api = new mw.ForeignApi("https://dane.nonsa.pl/api.php"); |
|||
api.post( |
|||
{ |
|||
action: 'ask', |
|||
format: 'json', |
|||
query: '[[Nie posiada typu strony Gry::true]] [[' + title + ']]' |
|||
}).done(function(data) |
|||
{ |
|||
var possiblyFilled = (typeof data.query.results[title] === "undefined"); |
|||
if (possiblyFilled) |
|||
{ |
{ |
||
api.post ( |
api.post ( |
||
Linia 26: | Linia 57: | ||
action: 'query', |
action: 'query', |
||
format: 'json', |
format: 'json', |
||
titles: |
titles: title |
||
}).done(function( |
}).done(function(data) |
||
{ |
{ |
||
if (typeof |
if (typeof data.query.pages["-1"] === "undefined") |
||
{ |
{ |
||
deferred.resolve(true); |
|||
} |
} |
||
else |
else |
||
{ |
{ |
||
deferred.resolve(false); |
|||
$("#firstHeading").append(kodNiewypelnionej); |
|||
} |
} |
||
}).fail(function(error) |
}).fail(function(error) |
||
{ |
|||
console.log(error); |
|||
deferred.resolve(false); |
|||
}); |
|||
}); |
|||
} |
} |
||
else |
else |
||
{ |
{ |
||
deferred.resolve(false); |
|||
$("#firstHeading").append(kodNiewypelnionej); |
|||
} |
} |
||
}).fail(function(error) |
|||
{ |
|||
console.log(error); |
|||
deferred.resolve(false); |
|||
}); |
|||
}); |
|||
}); |
|||
return deferred.promise(); |
|||
} |
|||
function getTitleFromUrl(url) |
|||
{ |
|||
var match = url.match(/((?:\?title=)|(?:\/wiki\/))([^&\?#]*)/); //raz link wewnętrzny jest przez index.php?title, raz przez /wiki/... |
|||
return match ? match[2] : null; |
|||
} |
|||
function parsePageLinks() |
|||
{ |
|||
var filledPageLinkCSS = {"text-shadow": "rgba(0, 255, 0, 3) 0em 0em 1em, rgba(0, 255, 0, 0.5) 0em 0em 1em"}; //celowo powtórzone, żeby cień był mocniejszy |
|||
var notFilledPageLinkCSS = {"text-shadow": "rgba(255, 0, 0, 3) 0em 0em 1em, rgba(255, 0, 0, 0.5) 0em 0em 1em"}; |
|||
var links = $(".mw-parser-output a[href]"); |
|||
//dlaczego nie ma klasy dla linków występujących bezpośrednio w wikitekście, a nie dodawanych automatycznie (np. do edycji sekcji lub do obrazków)...? |
|||
links = links.filter(function() |
|||
{ |
|||
var $this = $(this); |
|||
/*console.log($this.parent()); |
|||
console.log($this.parent().hasClass("mw-editsection"));*/ |
|||
return ($this.parent().hasClass("mw-editsection") == false && $this.hasClass("image") == false); |
|||
}); |
|||
//console.dir(links); |
|||
var allPages = []; |
|||
var mapDOMElementsToLinks = {}; |
|||
links.each(function() |
|||
{ |
|||
//TODO: usuwanie duplikatów w allPages |
|||
var $this = $(this), |
|||
title = getTitleFromUrl($this.attr("href")); |
|||
title = decodeURIComponent(title).replace(/_/g, " "); |
|||
if (title.indexOf("Gra:") == -1) |
|||
{ |
|||
return; |
|||
} |
|||
mapDOMElementsToLinks[title] = mapDOMElementsToLinks[title] || []; //początkowo zmienna będzie undefined, a przez to stanie się pustą tablicą, co pozwoli użyć metody push |
|||
mapDOMElementsToLinks[title].push($this); |
|||
allPages.push(title); |
|||
}); |
|||
//console.dir(mapDOMElementsToLinks); |
|||
mw.loader.using("mediawiki.ForeignApi", function() |
|||
{ |
|||
var api = new mw.ForeignApi("https://dane.nonsa.pl/api.php"); |
|||
SMWQueryFull(allPages) |
|||
.done(function(pagesPossiblyFilled) |
|||
{ |
|||
pagesMissingQueryFull(allPages) |
|||
.done(function(pagesMissing) |
|||
{ |
|||
/*console.dir(pagesMissing); |
|||
console.dir(allPages);*/ |
|||
$.each(mapDOMElementsToLinks, function(name, arrayOfDOMLinkObjects) |
|||
{ |
|||
if (typeof pagesPossiblyFilled[name] === "undefined" && |
|||
typeof pagesMissing[name] === "undefined") |
|||
{ |
|||
//console.log("Wypełniona strona " + name); |
|||
$(arrayOfDOMLinkObjects).each(function(index, DOMLinkObject) |
|||
{ |
|||
$(DOMLinkObject).css(filledPageLinkCSS).attr("title", ($(DOMLinkObject).attr("title") || name) + " (opisana w Nondanych)"); |
|||
}); |
|||
} |
|||
else |
|||
{ |
|||
//console.log("Niewypełniona strona " + name); |
|||
$(arrayOfDOMLinkObjects).each(function(index, DOMLinkObject) |
|||
{ |
|||
$(DOMLinkObject).css(notFilledPageLinkCSS).attr("title", ($(DOMLinkObject).attr("title") || name) + " (nieopisana w Nondanych)"); |
|||
}); |
|||
} |
|||
}); |
|||
}).fail(function(error) |
|||
{ |
|||
console.log(error); |
|||
}); |
|||
}).fail(function(error) |
|||
{ |
|||
console.log(error); |
|||
}); |
|||
}); |
|||
//console.log(SMWPageList); |
|||
} |
|||
function SMWQueryPartial(allPages, startingIndex, pagesPossiblyFilled) |
|||
{ |
|||
var deferred = $.Deferred(), |
|||
currentPages = allPages.slice(startingIndex, Math.min(allPages.length, startingIndex + MAX_PAGES_IN_SMW_QUERY)), |
|||
SMWQuery = "[[Nie posiada typu strony Gry::true]] [[" + currentPages.join("||") + "]]"; |
|||
mw.loader.using("mediawiki.ForeignApi", function() |
|||
{ |
|||
var api = new mw.ForeignApi("https://dane.nonsa.pl/api.php"); |
|||
api.post( |
|||
{ |
|||
action: 'ask', |
|||
format: 'json', |
|||
query: SMWQuery |
|||
}).done(function(data) |
|||
{ |
|||
$.each(data.query.results, function(name, value) |
|||
{ |
|||
pagesPossiblyFilled[name] = true; |
|||
}); |
|||
deferred.resolve(); |
|||
}).fail(function(error) |
|||
{ |
|||
console.log(error); |
|||
deferred.reject(); |
|||
}); |
|||
}); |
|||
return deferred.promise(); |
|||
} |
|||
function SMWQueryFull(allPages) |
|||
{ |
|||
var numPartialQueries = Math.ceil(allPages.length / MAX_PAGES_IN_SMW_QUERY), |
|||
deferred = $.Deferred(), |
|||
completed = 0, |
|||
pagesPossiblyFilled = {}; //obiekt, żeby można było używać jako tablicy asocjacyjnej |
|||
for (var index = 0, i = 0; i < numPartialQueries; ++i, index += MAX_PAGES_IN_SMW_QUERY) |
|||
{ |
|||
SMWQueryPartial(allPages, index, pagesPossiblyFilled) |
|||
.always(function() |
|||
{ |
|||
++completed; |
|||
if (completed == numPartialQueries) |
|||
{ |
|||
deferred.resolve(pagesPossiblyFilled); |
|||
} |
|||
}); |
|||
} |
} |
||
}); |
|||
return deferred.promise(); |
|||
} |
|||
function pagesMissingQueryPartial(allPages, startingIndex, pagesMissing) |
|||
{ |
|||
var deferred = $.Deferred(), |
|||
currentPages = allPages.slice(startingIndex, Math.min(allPages.length, startingIndex + MAX_PAGES_IN_EXIST_QUERY)); |
|||
mw.loader.using("mediawiki.ForeignApi", function() |
|||
{ |
|||
var api = new mw.ForeignApi("https://dane.nonsa.pl/api.php"); |
|||
api.post( |
|||
{ |
|||
action: "query", |
|||
format: "json", |
|||
formatversion: 2, |
|||
titles: currentPages.join("|"), |
|||
}).done(function(data) |
|||
{ |
|||
data.query.pages.forEach(function(page) |
|||
{ |
|||
if (page.missing == true) |
|||
{ |
|||
pagesMissing[page.title] = true; |
|||
} |
|||
}); |
|||
deferred.resolve(); |
|||
}).fail(function(error) |
|||
{ |
|||
console.log(error); |
|||
deferred.reject(); |
|||
}); |
|||
}); |
|||
return deferred.promise(); |
|||
} |
|||
function pagesMissingQueryFull(allPages) |
|||
{ |
|||
var numPartialQueries = Math.ceil(allPages.length / MAX_PAGES_IN_EXIST_QUERY), |
|||
deferred = $.Deferred(), |
|||
completed = 0, |
|||
pagesMissing = {}; |
|||
for (var index = 0, i = 0; i < numPartialQueries; ++i, index += MAX_PAGES_IN_EXIST_QUERY) |
|||
{ |
|||
pagesMissingQueryPartial(allPages, index, pagesMissing) |
|||
.always(function() |
|||
{ |
|||
++completed; |
|||
if (completed == numPartialQueries) |
|||
{ |
|||
deferred.resolve(pagesMissing); |
|||
} |
|||
}); |
|||
} |
|||
return deferred.promise(); |
|||
} |
Aktualna wersja na dzień 21:48, 19 sie 2020
var MAX_PAGES_IN_SMW_QUERY = 11,
MAX_PAGES_IN_EXIST_QUERY = 50;
if (mw.config.get("wgUserGroups").indexOf("sysop") != -1 || mw.config.get("wgUserGroups").indexOf("bot") != -1)
{
MAX_PAGES_IN_EXIST_QUERY = 500;
}
$(document).ready(function()
{
if (mw.config.get("wgNamespaceNumber") == 108)
{
//przycisk "Edytuj w Nondanych"
$("#ca-edit").before("<li id='ca-nondaneEdit'><span><a href='https://dane.nonsa.pl/wiki/" + encodeURIComponent(mw.config.get("wgPageName")) + "?action=formedit'>Edytuj w Nondanych</a></span></li>");
//button pokazujący, czy dana strona została już uzupełniona w Nondanych
var title = mw.config.get("wgPageName").replace(/_/g, " ");
var filledPageCode = "<a href='https://dane.nonsa.pl/wiki/" + encodeURIComponent(mw.config.get("wgPageName")) + "?action=formedit'><img id='nondaneEditedButton' src='https://upload.wikimedia.org/wikipedia/commons/1/1e/Button_Icon_GreenForest.svg' width='20px' height='20px' style='padding: 0px 0px 3px 10px;' title='Ta strona została już uzupełniona w Nondanych' /></a>";
var notFilledPageCode = "<a href='https://dane.nonsa.pl/wiki/" + encodeURIComponent(mw.config.get("wgPageName")) + "?action=formedit'><img id='nondaneEditedButton' src='https://upload.wikimedia.org/wikipedia/commons/0/07/Button_Icon_Red.svg' width='20px' height='20px' style='padding: 0px 0px 3px 10px;' title='Ta strona nie została jeszcze uzupełniona w Nondanych' /></a>";
isSinglePageFilled(title)
.done(function(filled)
{
if (filled)
{
$("#firstHeading").append(filledPageCode);
}
else
{
$("#firstHeading").append(notFilledPageCode);
}
});
parsePageLinks();
}
});
function isSinglePageFilled(title)
{
var deferred = $.Deferred();
mw.loader.using("mediawiki.ForeignApi", function()
{
var api = new mw.ForeignApi("https://dane.nonsa.pl/api.php");
api.post(
{
action: 'ask',
format: 'json',
query: '[[Nie posiada typu strony Gry::true]] [[' + title + ']]'
}).done(function(data)
{
var possiblyFilled = (typeof data.query.results[title] === "undefined");
if (possiblyFilled)
{
api.post (
{
action: 'query',
format: 'json',
titles: title
}).done(function(data)
{
if (typeof data.query.pages["-1"] === "undefined")
{
deferred.resolve(true);
}
else
{
deferred.resolve(false);
}
}).fail(function(error)
{
console.log(error);
deferred.resolve(false);
});
}
else
{
deferred.resolve(false);
}
}).fail(function(error)
{
console.log(error);
deferred.resolve(false);
});
});
return deferred.promise();
}
function getTitleFromUrl(url)
{
var match = url.match(/((?:\?title=)|(?:\/wiki\/))([^&\?#]*)/); //raz link wewnętrzny jest przez index.php?title, raz przez /wiki/...
return match ? match[2] : null;
}
function parsePageLinks()
{
var filledPageLinkCSS = {"text-shadow": "rgba(0, 255, 0, 3) 0em 0em 1em, rgba(0, 255, 0, 0.5) 0em 0em 1em"}; //celowo powtórzone, żeby cień był mocniejszy
var notFilledPageLinkCSS = {"text-shadow": "rgba(255, 0, 0, 3) 0em 0em 1em, rgba(255, 0, 0, 0.5) 0em 0em 1em"};
var links = $(".mw-parser-output a[href]");
//dlaczego nie ma klasy dla linków występujących bezpośrednio w wikitekście, a nie dodawanych automatycznie (np. do edycji sekcji lub do obrazków)...?
links = links.filter(function()
{
var $this = $(this);
/*console.log($this.parent());
console.log($this.parent().hasClass("mw-editsection"));*/
return ($this.parent().hasClass("mw-editsection") == false && $this.hasClass("image") == false);
});
//console.dir(links);
var allPages = [];
var mapDOMElementsToLinks = {};
links.each(function()
{
//TODO: usuwanie duplikatów w allPages
var $this = $(this),
title = getTitleFromUrl($this.attr("href"));
title = decodeURIComponent(title).replace(/_/g, " ");
if (title.indexOf("Gra:") == -1)
{
return;
}
mapDOMElementsToLinks[title] = mapDOMElementsToLinks[title] || []; //początkowo zmienna będzie undefined, a przez to stanie się pustą tablicą, co pozwoli użyć metody push
mapDOMElementsToLinks[title].push($this);
allPages.push(title);
});
//console.dir(mapDOMElementsToLinks);
mw.loader.using("mediawiki.ForeignApi", function()
{
var api = new mw.ForeignApi("https://dane.nonsa.pl/api.php");
SMWQueryFull(allPages)
.done(function(pagesPossiblyFilled)
{
pagesMissingQueryFull(allPages)
.done(function(pagesMissing)
{
/*console.dir(pagesMissing);
console.dir(allPages);*/
$.each(mapDOMElementsToLinks, function(name, arrayOfDOMLinkObjects)
{
if (typeof pagesPossiblyFilled[name] === "undefined" &&
typeof pagesMissing[name] === "undefined")
{
//console.log("Wypełniona strona " + name);
$(arrayOfDOMLinkObjects).each(function(index, DOMLinkObject)
{
$(DOMLinkObject).css(filledPageLinkCSS).attr("title", ($(DOMLinkObject).attr("title") || name) + " (opisana w Nondanych)");
});
}
else
{
//console.log("Niewypełniona strona " + name);
$(arrayOfDOMLinkObjects).each(function(index, DOMLinkObject)
{
$(DOMLinkObject).css(notFilledPageLinkCSS).attr("title", ($(DOMLinkObject).attr("title") || name) + " (nieopisana w Nondanych)");
});
}
});
}).fail(function(error)
{
console.log(error);
});
}).fail(function(error)
{
console.log(error);
});
});
//console.log(SMWPageList);
}
function SMWQueryPartial(allPages, startingIndex, pagesPossiblyFilled)
{
var deferred = $.Deferred(),
currentPages = allPages.slice(startingIndex, Math.min(allPages.length, startingIndex + MAX_PAGES_IN_SMW_QUERY)),
SMWQuery = "[[Nie posiada typu strony Gry::true]] [[" + currentPages.join("||") + "]]";
mw.loader.using("mediawiki.ForeignApi", function()
{
var api = new mw.ForeignApi("https://dane.nonsa.pl/api.php");
api.post(
{
action: 'ask',
format: 'json',
query: SMWQuery
}).done(function(data)
{
$.each(data.query.results, function(name, value)
{
pagesPossiblyFilled[name] = true;
});
deferred.resolve();
}).fail(function(error)
{
console.log(error);
deferred.reject();
});
});
return deferred.promise();
}
function SMWQueryFull(allPages)
{
var numPartialQueries = Math.ceil(allPages.length / MAX_PAGES_IN_SMW_QUERY),
deferred = $.Deferred(),
completed = 0,
pagesPossiblyFilled = {}; //obiekt, żeby można było używać jako tablicy asocjacyjnej
for (var index = 0, i = 0; i < numPartialQueries; ++i, index += MAX_PAGES_IN_SMW_QUERY)
{
SMWQueryPartial(allPages, index, pagesPossiblyFilled)
.always(function()
{
++completed;
if (completed == numPartialQueries)
{
deferred.resolve(pagesPossiblyFilled);
}
});
}
return deferred.promise();
}
function pagesMissingQueryPartial(allPages, startingIndex, pagesMissing)
{
var deferred = $.Deferred(),
currentPages = allPages.slice(startingIndex, Math.min(allPages.length, startingIndex + MAX_PAGES_IN_EXIST_QUERY));
mw.loader.using("mediawiki.ForeignApi", function()
{
var api = new mw.ForeignApi("https://dane.nonsa.pl/api.php");
api.post(
{
action: "query",
format: "json",
formatversion: 2,
titles: currentPages.join("|"),
}).done(function(data)
{
data.query.pages.forEach(function(page)
{
if (page.missing == true)
{
pagesMissing[page.title] = true;
}
});
deferred.resolve();
}).fail(function(error)
{
console.log(error);
deferred.reject();
});
});
return deferred.promise();
}
function pagesMissingQueryFull(allPages)
{
var numPartialQueries = Math.ceil(allPages.length / MAX_PAGES_IN_EXIST_QUERY),
deferred = $.Deferred(),
completed = 0,
pagesMissing = {};
for (var index = 0, i = 0; i < numPartialQueries; ++i, index += MAX_PAGES_IN_EXIST_QUERY)
{
pagesMissingQueryPartial(allPages, index, pagesMissing)
.always(function()
{
++completed;
if (completed == numPartialQueries)
{
deferred.resolve(pagesMissing);
}
});
}
return deferred.promise();
}