MediaWiki:Gadget-EdytujWNondanych.js: Różnice pomiędzy wersjami
Z Nonsensopedii, polskiej encyklopedii humoru
Ostrzyciel (dyskusja • edycje) M (ech) |
|||
(Nie pokazano 14 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 title = mw.config.get("wgPageName").replace(/_/g, " "); |
var title = mw.config.get("wgPageName").replace(/_/g, " "); |
||
var filledPageCode = "<a href='https://dane.nonsa.pl/wiki/" + |
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/" + |
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) |
isSinglePageFilled(title) |
||
Linia 63: | Linia 71: | ||
{ |
{ |
||
console.log(error); |
console.log(error); |
||
deferred.resolve(false); |
|||
}); |
}); |
||
} |
} |
||
Linia 78: | Linia 87: | ||
} |
} |
||
function |
function getTitleFromUrl(url) |
||
{ |
{ |
||
var match = url.match(/( |
var match = url.match(/((?:\?title=)|(?:\/wiki\/))([^&\?#]*)/); //raz link wewnętrzny jest przez index.php?title, raz przez /wiki/... |
||
return match ? match[ |
return match ? match[2] : null; |
||
} |
} |
||
function parsePageLinks() |
function parsePageLinks() |
||
{ |
{ |
||
var filledPageLinkCSS = {"text-shadow": "rgba(0, 255, 0, 3) 0em 0em 1em"}; |
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"}; |
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]"); |
var links = $(".mw-parser-output a[href]"); |
||
Linia 101: | Linia 110: | ||
//console.dir(links); |
//console.dir(links); |
||
var |
var allPages = []; |
||
var mapDOMElementsToLinks = {}; |
var mapDOMElementsToLinks = {}; |
||
links.each(function() |
links.each(function() |
||
{ |
{ |
||
//TODO: usuwanie duplikatów w |
//TODO: usuwanie duplikatów w allPages |
||
var $this = $(this), |
var $this = $(this), |
||
title = getTitleFromUrl($this.attr("href")); |
|||
title = decodeURIComponent(title).replace(/_/g, " "); |
|||
{ |
|||
if (title.indexOf("Gra:") == -1) |
|||
title = $this.attr("title"); |
|||
} |
|||
else //linki zewnętrzne |
|||
{ |
|||
title = getTitleFromExternalUrl($this.attr("href")); |
|||
} |
|||
title = decodeURI(title); |
|||
if (title == null || title.indexOf("Gra:") == -1) |
|||
{ |
{ |
||
return; |
return; |
||
} |
} |
||
mapDOMElementsToLinks[title] = mapDOMElementsToLinks[title] || []; |
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); |
mapDOMElementsToLinks[title].push($this); |
||
allPages.push(title); |
|||
}); |
}); |
||
//console.dir(mapDOMElementsToLinks); |
//console.dir(mapDOMElementsToLinks); |
||
SMWPageList = SMWPageList.substr(0, SMWPageList.length - 2); //pozbycie się dwóch ostatnich | |
|||
var SMWQuery = "[[Nie posiada typu strony Gry::true]] [[" + SMWPageList + "]]"; |
|||
//console.log(SMWPageList); |
|||
mw.loader.using("mediawiki.ForeignApi", function() |
mw.loader.using("mediawiki.ForeignApi", function() |
||
Linia 137: | Linia 135: | ||
var api = new mw.ForeignApi("https://dane.nonsa.pl/api.php"); |
var api = new mw.ForeignApi("https://dane.nonsa.pl/api.php"); |
||
SMWQueryFull(allPages) |
|||
api.post( |
|||
.done(function(pagesPossiblyFilled) |
|||
{ |
{ |
||
pagesMissingQueryFull(allPages) |
|||
action: 'ask', |
|||
.done(function(pagesMissing) |
|||
format: 'json', |
|||
query: SMWQuery |
|||
}).done(function(pagesPossiblyFilled) |
|||
{ |
|||
api.post( |
|||
{ |
{ |
||
/*console.dir(pagesMissing); |
|||
action: "query", |
|||
console.dir(allPages);*/ |
|||
format: "json", |
|||
$.each(mapDOMElementsToLinks, function(name, arrayOfDOMLinkObjects) |
|||
formatversion: 2, |
|||
titles: SMWPageList.replace("||", "|"), |
|||
}).done(function(existingPages) |
|||
{ |
|||
//console.dir(pagesPossiblyFilled); |
|||
//console.dir(existingPages); |
|||
var missingPages = []; |
|||
existingPages.query.pages.forEach(function(page) |
|||
{ |
|||
if (page.missing == "true") |
|||
{ |
|||
missingPages.push(page.title); |
|||
} |
|||
}); |
|||
$.each(mapDOMElementsToLinks, function(name, arrayOfDOMObjects) |
|||
{ |
{ |
||
if (typeof pagesPossiblyFilled |
if (typeof pagesPossiblyFilled[name] === "undefined" && |
||
typeof pagesMissing[name] === "undefined") |
|||
{ |
{ |
||
//console.log("Wypełniona strona " + name); |
//console.log("Wypełniona strona " + name); |
||
$( |
$(arrayOfDOMLinkObjects).each(function(index, DOMLinkObject) |
||
{ |
{ |
||
$( |
$(DOMLinkObject).css(filledPageLinkCSS).attr("title", ($(DOMLinkObject).attr("title") || name) + " (opisana w Nondanych)"); |
||
}); |
}); |
||
} |
} |
||
Linia 178: | Linia 157: | ||
{ |
{ |
||
//console.log("Niewypełniona strona " + name); |
//console.log("Niewypełniona strona " + name); |
||
$( |
$(arrayOfDOMLinkObjects).each(function(index, DOMLinkObject) |
||
{ |
{ |
||
$( |
$(DOMLinkObject).css(notFilledPageLinkCSS).attr("title", ($(DOMLinkObject).attr("title") || name) + " (nieopisana w Nondanych)"); |
||
}); |
}); |
||
} |
} |
||
Linia 195: | Linia 174: | ||
//console.log(SMWPageList); |
//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();
}