Module:Video game review score: Difference between revisions
Appearance
Content deleted Content added
No edit summary |
Latest rewrite as we continue to learn and grow. See Module:Video game wikidata. |
||
Line 1: | Line 1: | ||
local Date = require('Module:Date')._Date |
local Date = require('Module:Date')._Date |
||
local vgwd = require('Module:Video game wikidata') |
local vgwd = require('Module:Video game wikidata/sandbox') |
||
local yesno = require('Module:Yesno') |
local yesno = require('Module:Yesno') |
||
local p = {} |
local p = {} |
||
local function sortByPlatform(a,b) |
|||
local platformA = nil; |
|||
local platformB = nil; |
|||
if(not(a['qualifiers']['P400'] == nil or 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(not(b['qualifiers']['P400'] == nil or 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 buildCiteWeb(reference, df) |
|||
local referenceUrl = nil; |
|||
local cite = nil; |
|||
if(not(reference['snaks']['P854'] == nil or reference['snaks']['P854'][1] == nil)) then |
|||
referenceUrl = reference['snaks']['P854'][1]['datavalue']['value']; |
|||
end; |
|||
if(not (referenceUrl == nil or referenceUrl == "")) then |
|||
cite = "{{cite web|url="..referenceUrl; |
|||
local timestamp = nil; |
|||
local publisher = nil; |
|||
local work = nil; |
|||
local title = nil; |
|||
if(not (reference['snaks']['P813'] == nil or reference['snaks']['P813'][1] == nil)) then |
|||
timestamp = reference['snaks']['P813'][1]['datavalue']['value']['time']; |
|||
end; |
|||
if(not (reference['snaks']['P123'] == nil or reference['snaks']['P123'][1] == nil)) then |
|||
publisher = mw.wikibase.label('Q'..reference['snaks']['P123'][1]['datavalue']['value']['numeric-id']); |
|||
end; |
|||
if(not (reference['snaks']['P1433'] == nil or reference['snaks']['P1433'][1] == nil)) then |
|||
work = mw.wikibase.label('Q'..reference['snaks']['P1433'][1]['datavalue']['value']['numeric-id']); |
|||
end; |
|||
if(not (reference['snaks']['P1476'] == nil or reference['snaks']['P1476'][1] == nil)) then |
|||
title = reference['snaks']['P1476'][1]['datavalue']['value']['text']; |
|||
end; |
|||
if(not(title == nil or title == "")) then |
|||
cite = cite .. "|title="..title; |
|||
end; |
|||
if(not(publisher == nil or publisher == "")) then |
|||
cite = cite .. "|publisher="..publisher; |
|||
end; |
|||
if(not(work == nil or work == "")) then |
|||
cite = cite .. "|work="..work; |
|||
end; |
|||
if(not(timestamp == nil or 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, gameName, reviewscore, df, showRefs, proseScore) |
|||
local ret = "" |
|||
local system = nil; |
|||
local score = nil; |
|||
local reference = nil; |
|||
if(not(reviewscore['qualifiers']['P400'] == nil or reviewscore['qualifiers']['P400'][1] == nil)) then |
|||
system = vgwd.getSystemAlias(reviewscore['qualifiers']['P400'][1]['datavalue']['value']['numeric-id']); |
|||
end |
|||
if(not (system == nil or system == "") and not(proseScore)) then |
|||
ret = ret.."("..system..") "; |
|||
end; |
|||
score = reviewscore['mainsnak']['datavalue']['value']; |
|||
if(not (score == nil or score == "")) then |
|||
ret = ret..score; |
|||
end; |
|||
if(not (reviewscore['references'] == nil or reviewscore['references'][1] == nil) and showRefs and not(proseScore)) then |
|||
local cite = buildCiteWeb(reviewscore['references'][1], df); |
|||
if(not(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 = gameName..'-'..scoreBy; |
|||
if(not (system == nil or 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 |
|||
function p._main(frame, args) |
function p._main(frame, args) |
||
local |
local ret = vgwd.setReviewer(args["reviewer"]) |
||
vgwd.setDateFormat(args["df"]) |
|||
ret = vgwd.setGame(args["game"]) |
|||
-- No reviewer, stop. Must have reviewer at least. |
|||
vgwd.setSystem(args["system"]) |
|||
if(reviewer == nil or reviewer == "") then |
|||
vgwd.setGenerateReferences(args['showRefs']) |
|||
return "Missing reviewer"; |
|||
end; |
|||
-- Old template argument, may change later |
|||
-- See if supplied reviewer is in the aggregator table. |
|||
if(args["proseScore"]) then |
|||
reviewer = vgwd.getAggregatorID(string.upper(args["reviewer"])); |
|||
local proseScore = yesno(args["proseScore"], false); |
|||
if(reviewer == nil or reviewer == "") then |
|||
if(proseScore and args["system"] ~= nil and args["system"] ~= '') then |
|||
-- No? Maybe in the reviewer table. |
|||
vgwd.setGenerateReferences(false) |
|||
reviewer = vgwd.getReviewerID(string.upper(args["reviewer"])); |
|||
vgwd.setShowSystem(false) |
|||
if(reviewer == nil or reviewer == "") then |
|||
vgwd.setShowUpdateLink(false) |
|||
return "Invalid reviewer"; |
|||
end |
end |
||
end; |
|||
-- Check for a date format parameter. Default to mdy if missing. |
|||
local df = args["df"]; |
|||
if(df == nil or df == "") then |
|||
df = "mdy"; |
|||
end; |
end; |
||
df = string.lower(df); |
|||
-- Check for a game parameter. If missing, default to current article. |
|||
local game = args["game"]; |
|||
local entity = nil; |
|||
if(game == nil or game == "") then |
|||
entity = mw.wikibase.getEntity(); |
|||
else |
|||
entity = mw.wikibase.getEntity(game); |
|||
end; |
|||
if(not entity) then |
|||
return "No matching wikidata entity found"; |
|||
end |
|||
if(ret == nil) then |
|||
-- Reference suppression. |
|||
ret = vgwd.printReviewScores(frame); |
|||
local showRefs = true; |
|||
if(args['showRefs']) then |
|||
showRefs = yesno(args['showRefs'], true); |
|||
end; |
end; |
||
-- Prose score formatting. |
|||
local proseScore = false; |
|||
if(args['proseScore']) then |
|||
proseScore = yesno(args['proseScore'], false); |
|||
end; |
|||
-- Check for system parameter, and resolve it's QID if possible. |
|||
local system = args["system"]; |
|||
local systemId = nil; |
|||
if(not(system == nil or system == "")) then |
|||
system = string.upper(system); |
|||
systemId = vgwd.getSystemID(system); |
|||
elseif(proseScore) then |
|||
-- If no system was specified, force proseScore off. |
|||
proseScore = false; |
|||
end; |
|||
-- 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(not(review['qualifiers']['P447'] == nil or 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(not(review['qualifiers']['P400'] == nil or 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(not(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, entity:getLabel(), review, df, showRefs, proseScore); |
|||
end; |
|||
end; |
|||
-- Add edit link at end. If proseScore is set, suppress. |
|||
if(not(proseScore)) then |
|||
ret = string.sub(ret, 1, -7)..'<sub>[[d:'..entity['id']..'#P444|[±]]]</sub>'; |
|||
--ret = ret..'<sup>[[d:'..entity['id']..'#P444|[±]]]</sup>'; |
|||
else |
|||
ret = string.sub(ret, 1, -7); |
|||
end; |
|||
return ret; |
return ret; |
||
end; |
end; |
||
-- Template main function |
|||
function p.main(frame) |
function p.main(frame) |
||
local args = require('Module:Arguments').getArgs(frame, { |
local args = require('Module:Arguments').getArgs(frame, { |
||
wrappers = 'Template:Video game review score' |
wrappers = 'Template:Video game review score' |
||
}) |
}) |
||
return p._main(frame, args) |
return p._main(frame, args); |
||
end |
end; |
||
return p |
return p |
Revision as of 22:54, 25 April 2016
![]() | 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 p = {}
function p._main(frame, args)
local ret = vgwd.setReviewer(args["reviewer"])
vgwd.setDateFormat(args["df"])
ret = vgwd.setGame(args["game"])
vgwd.setSystem(args["system"])
vgwd.setGenerateReferences(args['showRefs'])
-- Old template argument, may change later
if(args["proseScore"]) then
local proseScore = yesno(args["proseScore"], false);
if(proseScore and args["system"] ~= nil and args["system"] ~= '') then
vgwd.setGenerateReferences(false)
vgwd.setShowSystem(false)
vgwd.setShowUpdateLink(false)
end
end;
if(ret == nil) then
ret = vgwd.printReviewScores(frame);
end;
return ret;
end;
-- Template main function
function p.main(frame)
local args = require('Module:Arguments').getArgs(frame, {
wrappers = 'Template:Video game review score'
})
return p._main(frame, args);
end;
return p