Module:Video game review score/sandbox: Difference between revisions
Appearance
Content deleted Content added
No edit summary |
No edit summary |
||
Line 2: | Line 2: | ||
local vgwd = require('Module:Video game wikidata/sandbox') |
local vgwd = require('Module:Video game wikidata/sandbox') |
||
local yesno = require('Module:Yesno') |
local yesno = require('Module:Yesno') |
||
local p = {} |
|||
-- Local variables. |
-- Local variables. |
||
Line 37: | Line 35: | ||
function setDateFormat(iDf) |
function setDateFormat(iDf) |
||
-- Check for a date format parameter. Default to mdy if missing. |
-- Check for a date format parameter. Default to mdy if missing. |
||
if |
if(iDf ~= nil and iDf ~= "") then |
||
df = string.lower(iDf); |
df = string.lower(iDf); |
||
end; |
end; |
||
Line 44: | Line 42: | ||
function setGame(iGame) |
function setGame(iGame) |
||
-- Check for a game parameter. If missing, default to current article. |
-- Check for a game parameter. If missing, default to current article. |
||
if |
if(iGame ~= nil and iGame ~= "") then |
||
entity = mw.wikibase.getEntity(iGame); |
entity = mw.wikibase.getEntity(iGame); |
||
else |
else |
||
Line 50: | Line 48: | ||
end; |
end; |
||
if( |
if(entity == nil) then |
||
return "No matching wikidata entity found"; |
return "No matching wikidata entity found"; |
||
end; |
end; |
||
return nil; |
|||
end; |
end; |
||
function setSystem(iSystem) |
function setSystem(iSystem) |
||
-- Check for system parameter, and resolve it's QID if possible. |
|||
if |
if(iSystem ~= nil and iSystem ~= "") then |
||
system = string.upper(iSystem); |
system = string.upper(iSystem); |
||
systemId = vgwd.getSystemID(system); |
systemId = vgwd.getSystemID(system); |
||
Line 68: | Line 68: | ||
function setGenerateReferences(iGenRefs) |
function setGenerateReferences(iGenRefs) |
||
-- Reference suppression. |
-- Reference suppression. |
||
if |
if(iGenRefs ~= nil and iGenRefs ~= "") then |
||
genRefs = yesno(iGenRefs, true); |
genRefs = yesno(iGenRefs, true); |
||
end; |
end; |
||
Line 75: | Line 75: | ||
function setShowSystem(iShowSystem) |
function setShowSystem(iShowSystem) |
||
-- Suppression of system aliases in front of score, i.e. (XBOX) xx/100. |
-- Suppression of system aliases in front of score, i.e. (XBOX) xx/100. |
||
if |
if(iShowSystem ~= nil and iShowSystem ~= "") then |
||
showSystem = yesno(iShowSystem, false); |
showSystem = yesno(iShowSystem, false); |
||
end; |
end; |
||
Line 86: | Line 86: | ||
function setShowUpdateLink(iShowUpdateLink) |
function setShowUpdateLink(iShowUpdateLink) |
||
-- Suppression of update link to Wikidata at the end of the score, i.e. (XBOX) xx/100[+]. |
-- Suppression of update link to Wikidata at the end of the score, i.e. (XBOX) xx/100[+]. |
||
if |
if(iShowUpdateLink ~= nil and iShowUpdateLink ~= "") then |
||
showUpdateLink = yesno(iShowUpdateLink, false); |
showUpdateLink = yesno(iShowUpdateLink, false); |
||
end; |
end; |
||
Line 94: | Line 94: | ||
local platformA = nil; |
local platformA = nil; |
||
local platformB = nil; |
local platformB = nil; |
||
if |
if(a['qualifiers']['P400'] ~= nil and b['qualifiers']['P400'][1] ~= nil) then |
||
platformA = vgwd.getSystemAlias(a['qualifiers']['P400'][1]['datavalue']['value']['numeric-id']); |
platformA = vgwd.getSystemAlias(a['qualifiers']['P400'][1]['datavalue']['value']['numeric-id']); |
||
if(platformA == nil) then |
if(platformA == nil) then |
||
Line 100: | Line 100: | ||
end; |
end; |
||
end; |
end; |
||
if |
if(b['qualifiers']['P400'] ~= nil and b['qualifiers']['P400'][1] ~= nil) then |
||
platformB = vgwd.getSystemAlias(b['qualifiers']['P400'][1]['datavalue']['value']['numeric-id']); |
platformB = vgwd.getSystemAlias(b['qualifiers']['P400'][1]['datavalue']['value']['numeric-id']); |
||
if(platformB == nil) then |
if(platformB == nil) then |
||
Line 114: | Line 114: | ||
local cite = nil; |
local cite = nil; |
||
if |
if(reference['snaks']['P854'] ~= nil and reference['snaks']['P854'][1] ~= nil) then |
||
referenceUrl = reference['snaks']['P854'][1]['datavalue']['value']; |
referenceUrl = reference['snaks']['P854'][1]['datavalue']['value']; |
||
end; |
end; |
||
if |
if(referenceUrl ~= nil and referenceUrl ~= "") then |
||
cite = "{{cite web|url="..referenceUrl; |
cite = "{{cite web|url="..referenceUrl; |
||
Line 126: | Line 126: | ||
local title = nil; |
local title = nil; |
||
if |
if(reference['snaks']['P813'] ~= nil and reference['snaks']['P813'][1] ~= nil) then |
||
timestamp = reference['snaks']['P813'][1]['datavalue']['value']['time']; |
timestamp = reference['snaks']['P813'][1]['datavalue']['value']['time']; |
||
end; |
end; |
||
if |
if(reference['snaks']['P123'] ~= nil and reference['snaks']['P123'][1] ~= nil) then |
||
publisher = mw.wikibase.label('Q'..reference['snaks']['P123'][1]['datavalue']['value']['numeric-id']); |
publisher = mw.wikibase.label('Q'..reference['snaks']['P123'][1]['datavalue']['value']['numeric-id']); |
||
end; |
end; |
||
if |
if(reference['snaks']['P1433'] ~= nil and reference['snaks']['P1433'][1] ~= nil) then |
||
work = mw.wikibase.label('Q'..reference['snaks']['P1433'][1]['datavalue']['value']['numeric-id']); |
work = mw.wikibase.label('Q'..reference['snaks']['P1433'][1]['datavalue']['value']['numeric-id']); |
||
end; |
end; |
||
if |
if(reference['snaks']['P1476'] ~= nil and reference['snaks']['P1476'][1] ~= nil) then |
||
title = reference['snaks']['P1476'][1]['datavalue']['value']['text']; |
title = reference['snaks']['P1476'][1]['datavalue']['value']['text']; |
||
end; |
end; |
||
if |
if(title ~= nil and title ~= "") then |
||
cite = cite .. "|title="..title; |
cite = cite .. "|title="..title; |
||
end; |
end; |
||
if |
if(publisher ~= nil and publisher ~= "") then |
||
cite = cite .. "|publisher="..publisher; |
cite = cite .. "|publisher="..publisher; |
||
end; |
end; |
||
if |
if(work ~= nil and work ~= "") then |
||
cite = cite .. "|work="..work; |
cite = cite .. "|work="..work; |
||
end; |
end; |
||
if |
if(timestamp ~= nil and timestamp ~= "") then |
||
local year = string.sub(timestamp,2,5); |
local year = string.sub(timestamp,2,5); |
||
local month = string.sub(timestamp,7,8); |
local month = string.sub(timestamp,7,8); |
||
Line 170: | Line 170: | ||
local reference = nil; |
local reference = nil; |
||
if |
if(reviewscore['qualifiers']['P400'] ~= nil and reviewscore['qualifiers']['P400'][1] ~= nil) then |
||
system = vgwd.getSystemAlias(reviewscore['qualifiers']['P400'][1]['datavalue']['value']['numeric-id']); |
system = vgwd.getSystemAlias(reviewscore['qualifiers']['P400'][1]['datavalue']['value']['numeric-id']); |
||
end |
end |
||
if |
if(system ~= nil and system ~= "" and showSystem) then |
||
ret = ret.."("..system..") "; |
ret = ret.."("..system..") "; |
||
end; |
end; |
||
score = reviewscore['mainsnak']['datavalue']['value']; |
score = reviewscore['mainsnak']['datavalue']['value']; |
||
if |
if(score ~= nil and score ~= "") then |
||
ret = ret..score; |
ret = ret..score; |
||
end; |
end; |
||
if |
if(reviewscore['references'] ~= nil and reviewscore['references'][1] ~= nil and genRefs) then |
||
local cite = buildCite(reviewscore['references'][1], df); |
local cite = buildCite(reviewscore['references'][1], df); |
||
if |
if(cite ~= nil) then |
||
local scoreBy = vgwd.getAggregatorAlias(reviewscore['qualifiers']['P447'][1]['datavalue']['value']['numeric-id']); |
local scoreBy = vgwd.getAggregatorAlias(reviewscore['qualifiers']['P447'][1]['datavalue']['value']['numeric-id']); |
||
if(scoreBy == nil) then |
if(scoreBy == nil) then |
||
Line 192: | Line 192: | ||
local name = entity:getLabel()..'-'..scoreBy; |
local name = entity:getLabel()..'-'..scoreBy; |
||
if |
if(system ~= nil and system ~= "") then |
||
name = name..system; |
name = name..system; |
||
end; |
end; |
||
Line 217: | Line 217: | ||
for i,review in pairs(reviewscores) do |
for i,review in pairs(reviewscores) do |
||
local scoreBy = nil |
local scoreBy = nil |
||
if |
if(review['qualifiers']['P447'] ~= nil and review['qualifiers']['P447'][1] ~= nil) then |
||
scoreBy = review['qualifiers']['P447'][1]['datavalue']['value']['numeric-id']; |
scoreBy = review['qualifiers']['P447'][1]['datavalue']['value']['numeric-id']; |
||
end; |
end; |
||
Line 227: | Line 227: | ||
else |
else |
||
-- Get platform if it exists. |
-- Get platform if it exists. |
||
if |
if(review['qualifiers']['P400'] ~= nil and review['qualifiers']['P400'][1] ~= nil) then |
||
-- Try to match based on QID. |
-- Try to match based on QID. |
||
local reviewSysId = review['qualifiers']['P400'][1]['datavalue']['value']['numeric-id']; |
local reviewSysId = review['qualifiers']['P400'][1]['datavalue']['value']['numeric-id']; |
||
Line 235: | Line 235: | ||
-- If that failed, try to match based on label. |
-- If that failed, try to match based on label. |
||
local systemName = mw.wikibase.label('Q'..reviewSysId); |
local systemName = mw.wikibase.label('Q'..reviewSysId); |
||
if |
if(systemName ~= nil and string.upper(systemName) == system) then |
||
table.insert(reviewsToPrint,review); |
table.insert(reviewsToPrint,review); |
||
end; |
end; |
||
Line 272: | Line 272: | ||
if(args["proseScore"]) then |
if(args["proseScore"]) then |
||
local proseScore = yesno(args["proseScore"], false); |
local proseScore = yesno(args["proseScore"], false); |
||
if(proseScore and |
if(proseScore and system ~= nil and system ~= '') then |
||
setGenerateReferences(false) |
setGenerateReferences(false) |
||
setShowSystem(false) |
setShowSystem(false) |
Revision as of 21:52, 25 April 2016
![]() | This is the module sandbox page for Module:Video game review score (diff). |
![]() | This module is rated as alpha. It is ready for third-party input, and may be used on a few pages to see if problems arise, but should be watched. Suggestions for new features or changes in their input and output mechanisms are welcome. |
Usage
![]() | This module depends on the following other modules: |
This module uses the Wikidata properties:
platform (P400) (see uses)
review score (P444) (see uses)
review score by (P447) (see uses)
reference URL (P854) (see uses)
retrieved (P813) (see uses)
publisher (P123) (see uses)
title (P1476) (see uses)
published in (P1433) (see uses)
Implements {{Video game review score}}
local Date = require('Module:Date')._Date
local vgwd = require('Module:Video game wikidata/sandbox')
local yesno = require('Module:Yesno')
-- Local variables.
local reviewer = nil;
local df = "mdy";
local entity = nil;
local genRefs = true;
local showSystem = true;
local showUpdateLink = true;
local system = nil;
local systemId = nil;
function setReviewer(iReviewer)
-- No reviewer, stop. Must have reviewer at least.
if(iReviewer == nil or iReviewer == "") then
return "Missing reviewer";
end;
-- See if supplied reviewer is in the aggregator table.
iReviewer = string.upper(iReviewer)
reviewer = vgwd.getAggregatorID(iReviewer);
if(reviewer == nil or reviewer == "") then
-- No? Maybe in the reviewer table.
reviewer = vgwd.getReviewerID(iReviewer);
if(reviewer == nil or reviewer == "") then
return "Invalid reviewer";
end;
end;
return nil;
end;
function setDateFormat(iDf)
-- Check for a date format parameter. Default to mdy if missing.
if(iDf ~= nil and iDf ~= "") then
df = string.lower(iDf);
end;
end;
function setGame(iGame)
-- Check for a game parameter. If missing, default to current article.
if(iGame ~= nil and iGame ~= "") then
entity = mw.wikibase.getEntity(iGame);
else
entity = mw.wikibase.getEntity();
end;
if(entity == nil) then
return "No matching wikidata entity found";
end;
return nil;
end;
function setSystem(iSystem)
-- Check for system parameter, and resolve it's QID if possible.
if(iSystem ~= nil and iSystem ~= "") then
system = string.upper(iSystem);
systemId = vgwd.getSystemID(system);
elseif(not showSystem) then
-- If no system was specified, force showSystem on.
showSystem = true;
end;
end;
function setGenerateReferences(iGenRefs)
-- Reference suppression.
if(iGenRefs ~= nil and iGenRefs ~= "") then
genRefs = yesno(iGenRefs, true);
end;
end;
function setShowSystem(iShowSystem)
-- Suppression of system aliases in front of score, i.e. (XBOX) xx/100.
if(iShowSystem ~= nil and iShowSystem ~= "") then
showSystem = yesno(iShowSystem, false);
end;
if(system == nil or system == '') then
-- If no system was specified, force showSystem on.
showSystem = true;
end;
end;
function setShowUpdateLink(iShowUpdateLink)
-- Suppression of update link to Wikidata at the end of the score, i.e. (XBOX) xx/100[+].
if(iShowUpdateLink ~= nil and iShowUpdateLink ~= "") then
showUpdateLink = yesno(iShowUpdateLink, false);
end;
end;
local function sortByPlatform(a,b)
local platformA = nil;
local platformB = nil;
if(a['qualifiers']['P400'] ~= nil and b['qualifiers']['P400'][1] ~= nil) then
platformA = vgwd.getSystemAlias(a['qualifiers']['P400'][1]['datavalue']['value']['numeric-id']);
if(platformA == nil) then
platformA = mw.wikibase.label('Q'..a['qualifiers']['P400'][1]['datavalue']['value']['numeric-id']);
end;
end;
if(b['qualifiers']['P400'] ~= nil and b['qualifiers']['P400'][1] ~= nil) then
platformB = vgwd.getSystemAlias(b['qualifiers']['P400'][1]['datavalue']['value']['numeric-id']);
if(platformB == nil) then
platformB = mw.wikibase.label('Q'..b['qualifiers']['P400'][1]['datavalue']['value']['numeric-id']);
end;
end;
return platformA < platformB
end;
local function buildCite(reference)
local referenceUrl = nil;
local cite = nil;
if(reference['snaks']['P854'] ~= nil and reference['snaks']['P854'][1] ~= nil) then
referenceUrl = reference['snaks']['P854'][1]['datavalue']['value'];
end;
if(referenceUrl ~= nil and referenceUrl ~= "") then
cite = "{{cite web|url="..referenceUrl;
local timestamp = nil;
local publisher = nil;
local work = nil;
local title = nil;
if(reference['snaks']['P813'] ~= nil and reference['snaks']['P813'][1] ~= nil) then
timestamp = reference['snaks']['P813'][1]['datavalue']['value']['time'];
end;
if(reference['snaks']['P123'] ~= nil and reference['snaks']['P123'][1] ~= nil) then
publisher = mw.wikibase.label('Q'..reference['snaks']['P123'][1]['datavalue']['value']['numeric-id']);
end;
if(reference['snaks']['P1433'] ~= nil and reference['snaks']['P1433'][1] ~= nil) then
work = mw.wikibase.label('Q'..reference['snaks']['P1433'][1]['datavalue']['value']['numeric-id']);
end;
if(reference['snaks']['P1476'] ~= nil and reference['snaks']['P1476'][1] ~= nil) then
title = reference['snaks']['P1476'][1]['datavalue']['value']['text'];
end;
if(title ~= nil and title ~= "") then
cite = cite .. "|title="..title;
end;
if(publisher ~= nil and publisher ~= "") then
cite = cite .. "|publisher="..publisher;
end;
if(work ~= nil and work ~= "") then
cite = cite .. "|work="..work;
end;
if(timestamp ~= nil and timestamp ~= "") then
local year = string.sub(timestamp,2,5);
local month = string.sub(timestamp,7,8);
local day = string.sub(timestamp,10,11);
local accessdate = Date(year, month, day):text(df);
cite = cite .. "|accessdate="..accessdate;
end;
cite = cite..'}}';
end;
return cite;
end;
local function printReviewRow(frame, reviewscore)
local ret = ""
local system = nil;
local score = nil;
local reference = nil;
if(reviewscore['qualifiers']['P400'] ~= nil and reviewscore['qualifiers']['P400'][1] ~= nil) then
system = vgwd.getSystemAlias(reviewscore['qualifiers']['P400'][1]['datavalue']['value']['numeric-id']);
end
if(system ~= nil and system ~= "" and showSystem) then
ret = ret.."("..system..") ";
end;
score = reviewscore['mainsnak']['datavalue']['value'];
if(score ~= nil and score ~= "") then
ret = ret..score;
end;
if(reviewscore['references'] ~= nil and reviewscore['references'][1] ~= nil and genRefs) then
local cite = buildCite(reviewscore['references'][1], df);
if(cite ~= nil) then
local scoreBy = vgwd.getAggregatorAlias(reviewscore['qualifiers']['P447'][1]['datavalue']['value']['numeric-id']);
if(scoreBy == nil) then
scoreBy = vgwd.getReviewerAlias(reviewscore['qualifiers']['P447'][1]['datavalue']['value']['numeric-id']);
end;
local name = entity:getLabel()..'-'..scoreBy;
if(system ~= nil and system ~= "") then
name = name..system;
end;
cite = frame:extensionTag{ name = "ref", args = {name=name}, content=cite };
ret = ret..cite;
end;
end;
return ret.."<br />";
end
local function getUpdateLink()
return '<sub>[[d:'..entity['id']..'#P444|[±]]]</sub>';
end;
local function printReviewScores(frame)
-- Loop all of "review scores" for this title
local ret = "";
local reviewscores = entity['claims']['P444'];
if(reviewscores) then
-- Find reviews that qualify for printing and insert into array.
local reviewsToPrint = {}
for i,review in pairs(reviewscores) do
local scoreBy = nil
if(review['qualifiers']['P447'] ~= nil and review['qualifiers']['P447'][1] ~= nil) then
scoreBy = review['qualifiers']['P447'][1]['datavalue']['value']['numeric-id'];
end;
if(scoreBy == reviewer) then
-- If template specified a system, we need to check for the specific system and only output that one.
if(system == nil or system == "") then
-- No system specified, so output each one found.
table.insert(reviewsToPrint,review);
else
-- Get platform if it exists.
if(review['qualifiers']['P400'] ~= nil and review['qualifiers']['P400'][1] ~= nil) then
-- Try to match based on QID.
local reviewSysId = review['qualifiers']['P400'][1]['datavalue']['value']['numeric-id'];
if(systemId == reviewSysId) then
table.insert(reviewsToPrint,review);
else
-- If that failed, try to match based on label.
local systemName = mw.wikibase.label('Q'..reviewSysId);
if(systemName ~= nil and string.upper(systemName) == system) then
table.insert(reviewsToPrint,review);
end;
end;
end;
end;
end;
end
-- Sort the array by platform label.
table.sort(reviewsToPrint, sortByPlatform)
-- Print the reviews
for i,review in ipairs(reviewsToPrint) do
ret = ret .. printReviewRow(frame, review);
end;
end;
ret = string.sub(ret, 1, -7)
-- Add edit link at end if showUpdateLink is on.
if(showUpdateLink) then
ret = ret..getUpdateLink();
end;
return ret;
end;
function _main(frame, args)
local ret = setReviewer(args["reviewer"])
setDateFormat(args["df"])
ret = setGame(args["game"])
setSystem(args["system"])
setGenerateReferences(args['showRefs'])
-- Old template argument, may change later
if(args["proseScore"]) then
local proseScore = yesno(args["proseScore"], false);
if(proseScore and system ~= nil and system ~= '') then
setGenerateReferences(false)
setShowSystem(false)
setShowUpdateLink(false)
end
end;
if(ret == nil) then
ret = printReviewScores(frame);
end;
return ret;
end;
-- Template main function
function main(frame)
local args = require('Module:Arguments').getArgs(frame, {
wrappers = 'Template:Video game review score'
})
return _main(frame, args);
end;
return {
main = main,
_main = _main,
setReviewer = setReviewer,
setDateFormat = setDateFormat,
setGame = setGame,
setSystem = setSystem,
setGenerateReferences = setGenerateReferences,
setShowSystem = setShowSystem,
setShowUpdateLink = setShowUpdateLink,
printReviewScores = printReviewScores,
getUpdateLink = getUpdateLink,
}