Jump to content

Module:Video game review score

From Wikipedia, the free encyclopedia
This is an old revision of this page, as edited by Ferret (talk | contribs) at 13:21, 13 April 2016. The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.

local Date = require('Module:Date')._Date
local CS = require('Module:Citation/CS1')

local p = {}

-- List of accepted aggregator arguments and their related QID.
local aggregators = {
    ['mc'] = 'Q150248',
    ['gr'] = 'Q40160'
}     

-- Translation table for converting numeric-IDs to shorthand aliases.
local systems = {
	['13361286'] = 'XONE',
	['5014725'] = 'PS4',
	['16338'] = 'PC'
}

local function printRow(frame, reviewscore, df)
	local ret = ""

	local system = systems[reviewscore['qualifiers']['P400'][1]['datavalue']['value']['numeric-id']];
	local score = reviewscore['mainsnak']['datavalue']['value'];

	if(not (system == nil or system == "")) then
		ret = ret.."("..system..") ";
	end;
	
	if(not (score == nil or score == "")) then
		ret = ret..score;
	end;

	local reference = reviewscore['references'][1]['snaks']['P854'][1]['datavalue']['value'];	
	
	if(not (reference == nil or reference == "")) then
		local cite = "{{cite web|url="..reference;

		local timestamp = reviewscore['references'][1]['snaks']['P813'][1]['datavalue']['value']['time'];
		local publisher = mw.wikibase.label('Q'..reviewscore['references'][1]['snaks']['P123'][1]['datavalue']['value']['numeric-id']);
		local title = reviewscore['references'][1]['snaks']['P1476'][1]['datavalue']['value']['text'];

		if(not(title == nil or title == "")) then
			cite = cite .. "|title="..title;
		end;
		
		if(not(publisher == nil or publisher == "")) then
			cite = cite .. "|publisher="..publisher;
		end;
		
		--+2016-04-08T00:00:00Z
		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..'}}';
		cite = frame:extensionTag{ name = "ref", args = {name=name}, content=cite };
		ret = ret..cite;
	end;

	return ret .. "<br />";
end

function p._main(frame, args)
	local aggregator = args["aggregator"];
	local df = args["df"];

	-- No aggregator, stop. Must have aggregator at least.
	if(aggregator == nil or aggregator == "" or aggregators[aggregator] == nil) then
		return "Invalid or missing aggregator";
	end;
	
	if(df == nil or df == "") then
		df = "mdy";
	end;
	
	aggregator = aggregators[string.lower(aggregator)];
	df = string.lower(df);
		
	-- Use Q20112508 (Dark Souls III) for testing.
	local entity = mw.wikibase.getEntity( 'Q20112508' );
	if not entity then
		return "No entity found";
	end
	
	local ret = "";	
	local reviewscores = entity['claims']['P444'];
	local system = args["system"];
	local hasSystem = not(system == nil or system == "");	
	if(hasSystem) then
		system = string.lower(system);
	end;

	-- Loop all of "score by" for this title
	for i = 1, #reviewscores do
		local reviewer = 'Q'..reviewscores[i]['qualifiers']['P447'][1]['datavalue']['value']['numeric-id'];
		if(reviewer == aggregator) then
			-- If template specified a system, we need to check for the specific system and only output that one.
			if(hasSystem) then
				local systemName = mw.wikibase.label('Q'..reviewscores[i]['qualifiers']['P400'][1]['datavalue']['value']['numeric-id']);
				if(not(systemName == nil) and string.lower(systemName) == system) then
					ret = ret .. printRow(frame, reviewscores[i], df);
				end;
			else
				-- No system specified, so output each one found.
				ret = ret .. printRow(frame, reviewscores[i], df);
			end;
		end;
	end;

	return ret;
end;

function p.main(frame)
	local args = require('Module:Arguments').getArgs(frame, {
		wrappers = 'User:Ferret/sandbox/Video game aggregator'
	})
	return p._main(frame, args)
end

return p