MediaWiki:Gadget-EdytujWNondanych.js: Różnice pomiędzy wersjami
Z Nonsensopedii, polskiej encyklopedii humoru
(pakietowanie zapytań, mocniejszy cień CSS) |
|||
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 86: | Linia 94: | ||
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 109: | ||
//console.dir(links); |
//console.dir(links); |
||
var SMWPageList = ""; |
var allPages = [], SMWPageList = ""; |
||
var mapDOMElementsToLinks = {}; |
var mapDOMElementsToLinks = {}; |
||
links.each(function() |
links.each(function() |
||
Linia 122: | Linia 130: | ||
} |
} |
||
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); |
|||
SMWPageList += title + "||"; |
SMWPageList += title + "||"; |
||
}); |
}); |
||
Linia 137: | Linia 146: | ||
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( |
|||
{ |
{ |
||
$.each(mapDOMElementsToLinks, function(name, arrayOfDOMLinkObjects) |
|||
action: "query", |
|||
format: "json", |
|||
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 166: | ||
{ |
{ |
||
//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 183: | ||
//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_SMW_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(); |
|||
} |
} |
Wersja z 15:01, 7 lis 2019
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/" + encodeURI(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/" + encodeURI(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/" + encodeURI(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);
});
}
else
{
deferred.resolve(false);
}
}).fail(function(error)
{
console.log(error);
deferred.resolve(false);
});
});
return deferred.promise();
}
function getTitleFromExternalUrl(url)
{
var match = url.match(/(?<=(?:title=)|(?:wiki\/))([^&\?])*/); //raz link zewnętrzny jest przez index.php?title, raz przez /wiki/...
return match ? match[0] : 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 = [], SMWPageList = "";
var mapDOMElementsToLinks = {};
links.each(function()
{
//TODO: usuwanie duplikatów w SMWPageList
var $this = $(this), title;
if ($this.attr("title")) //linki wewnętrzne
{
title = $this.attr("title");
}
else //linki zewnętrzne
{
title = getTitleFromExternalUrl($this.attr("href"));
}
title = decodeURI(title);
if (title == null || 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);
SMWPageList += title + "||";
});
//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()
{
var api = new mw.ForeignApi("https://dane.nonsa.pl/api.php");
SMWQueryFull(allPages)
.done(function(pagesPossiblyFilled)
{
pagesMissingQueryFull(allPages)
.done(function(pagesMissing)
{
$.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_SMW_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();
}