Module:Cite source

local util_vars = require('Module:VarsUtil') local util_link = require('Module:LinkUtil') local cache = require('mw.ext.LuaCache') local PREFIX = 'SC_CACHE_01-' local concat local dump local print local p = {} --p stands for package local h = {} --h stands for helper

-- from https://stackoverflow.com/a/7615129 function h.splitString (inputstr, sep) if sep == nil then sep = "%s" end local t = {} for str in string.gmatch(inputstr, "([^"..sep.."]+)") do               table.insert(t, str) end return t end

function p.infoboxStore( frame ) local args = frame:getParent.args local outputText = "" if args["citation text"] then outputText = args["citation text"] else if not args["separator"] then args["separator"] = "," end -- combines and if args["anthology"] and args["audio anthology"] then args["anthology"] = args["anthology"] .. ", " .. args["audio anthology"] elseif (not args["anthology"]) and args["audio anthology"] then args["anthology"] = args["audio anthology"] end -- combines and if args["adapted from"] and args["novelisation of"] then args["adapted from"] = args["adapted from"] .. ", " .. args["novelisation of"] elseif (not args["adapted from"]) and args["novelisation of"] then args["adapted from"] = args["novelisation of"] end local data = {["anthology"] = "", ["writer"] = "", ["publisher"] = "", ["adapted from"] = ""} for key, val in pairs(data) do			if args[key] then local current = h.splitString(args[key], args["separator"]) for i = 1, #current do data[key] = data[key] .. "" .. util_link.stripSMW(util_link.stripDab(current[i])) .. "" if i == #current-1 then data[key] = data[key] .. " and " elseif i == #current then data[key] = data[key] else data[key] = data[key] .. ", "	   			end end end end data["issues"] = "" if args["publication"] then local current = h.splitString(args["publication"], args["separator"]) for i = 1, #current do data["issues"] = data["issues"] .. util_link.stripSMW(current[i]) .. util_link.stripSMW(util_link.stripDab(current[i])) if i == #current-1 then data["issues"] = data["issues"] .. " and " elseif i == #current then data["issues"] = data["issues"] else data["issues"] = data["issues"] .. ", "   			end end end -- making series text if args["citation series"] then data["series"] = args["citation series"] elseif args["range"] then data["series"] = "\'\'" .. util_link.stripSMW(util_link.stripDab(args["range"])) .. "\'\'" elseif args["series"] then if args["series"] == "Doctor Who television stories" then data["series"] = "\'\'Doctor Who\'\'" else data["series"] = args["series"] end if args["season number"] then data["series"] = data["series"] .. " " .. "" .. util_link.stripSMW(util_link.stripDab(args["season number"])) .. "" end else data["series"] = "" end -- making release year text if args["release date"] or args["broadcast date"] or args["premiere"] or args["beta release date"] or args["cover date"] then args["release date"] = h.splitString(util_link.stripDab(args["release date"] or args["broadcast date"] or args["premiere"] or args["beta release date"] or args["cover date"]), "-") local releaseYear local releaseEndYear if #args["release date"] >= 2 then releaseYear = args["release date"][1]:gsub("%s+", ""):sub(-4) releaseEndYear = args["release date"][2]:gsub("%s+", ""):sub(-4) if tonumber(releaseEndYear) == nil then releaseEndYear = nil end if tonumber(releaseYear) == nil then releaseYear = releaseEndYear releaseEndYear = nil end else releaseYear = args["release date"][1]:gsub("%s+", ""):sub(-4) end if releaseEndYear and releaseYear and (releaseEndYear ~= releaseYear) then if (tonumber(releaseEndYear) - tonumber(releaseYear)) == 1 then data["release year"] = "in" else data["release year"] = "between" end data["release year"] = data["release year"] .. " the years " .. releaseYear .." and " .. releaseEndYear .. "" else data["release year"] = "in the year " .. releaseYear .."" end else data["release year"] = "" end -- making outputText local outputText = "" if data["adapted from"] ~= "" then outputText = outputText .. "Adapted from ''" .. data["adapted from"] .. "''"   		if data["writer"] ~= "" then outputText = outputText .. ", written by " .. data["writer"] if data["publisher"] ~= "" then outputText = outputText .. " and released by " .. data["publisher"] end elseif data["publisher"] ~= "" then outputText = outputText .. " and released by " .. data["publisher"] end elseif data["writer"] ~= "" then outputText = outputText .. "Written by " .. data["writer"] if data["publisher"] ~= "" then outputText = outputText .. " and released by " .. data["publisher"] end elseif data["publisher"] ~= "" then outputText = outputText .. " Released by " .. data["publisher"] else outputText = outputText .. "Released " end if data["anthology"] ~= "" then outputText = outputText .. " as part of ''" .. data["anthology"] .. "''"   			if data["series"] ~= "" then if args["season number"] ~= "" then outputText = outputText .. " from " .. data["series"] else outputText = outputText .. " from the series " .. data["series"] end end elseif data["series"] ~= "" then if args["season number"] ~= "" then outputText = outputText .. " as part of " .. data["series"] else outputText = outputText .. " as part of the series " .. data["series"] end end if data["release year"] ~= "" then if (data["publisher"] ~= "") or (data["writer"] ~= "") then outputText = outputText .. " " .. data["release year"] else outputText = outputText .. " and released " .. data["release year"] end end if data["issues"] ~= "" then outputText = outputText .. " throught " .. data["issues"] end if outputText == "" then outputText = "No data." else outputText = outputText .. "."		end local name = frame.args[1] --outputText = dump(data) -- for testing local varBin = util_vars.setVar(PREFIX .. name, outputText) local cacheBin = cache.set(PREFIX .. name, outputText) local smwBin = mw.smw.set("Story info", outputText) end end

function p.getInfo( frame ) local queryResult = mw.smw.ask( [=[ The Daleks (TV story) || Regeneration (TV story) || The Holy Terror (audio story) || Rosa (TV story) || The Eight Doctors (novel) || Mr Saldaamir (short story) || Skywatch-7 (comic story) || The Klepton Parasites (comic story) || The Lonely Assassins (video game) || Doctor Who in an Exciting Adventure with the Daleks (novelisation) || The Dreadful Flap (short story) || Rise and Fall (audio story) || Martha Jones' MySpace blog (short story)	if queryResult == nil then       return "No data: " .. frame["args"]["stories"]    end    if type( queryResult ) == "table" then    	for queryResultsCounter = 1, #queryResult do    		local current = queryResult[queryResultsCounter]    		local outputText = ""	    	local data = {["Anthology"] = "", ["Writer"] = "", ["Publisher"] = "", ["Adapted from"] = ""}	    	for key, val in pairs(data) do	    		if type( current[key] ) == "string" then data[key] = "" .. util_link.stripSMW(util_link.stripDab(current[key])) .. "" elseif type( current[key] ) == "table" then data[key] = "" for i = 1, #current[key] do data[key] = data[key] .. "" .. util_link.stripSMW(util_link.stripDab(current[key][i])) .."" if i == #current[key]-1 then data[key] = data[key] .. " and " elseif i == #current[key] then data[key] = data[key] else data[key] = data[key] .. ", "		   			end end end end if current["Issues"] then data["Issues"] = current["Issues"] else data["Issues"] = "" end -- making series text if current["Citation series"] then data["Series"] = current["Citation series"] elseif current["Range"] then data["Series"] = "\'\'" .. util_link.stripSMW(util_link.stripDab(current["Range"])) .. "\'\'" elseif current["Series"] then if current["Series"] == "Doctor Who television stories" then data["Series"] = "\'\'Doctor Who\'\'" else data["Series"] = current["Series"] end if current["Season"] then data["Series"] = data["Series"] .. " " .. "" .. util_link.stripSMW(util_link.stripDab(current["Season"])) .. "" end else data["Series"] = "" end -- making release year text if current["Release date"] then local releaseYear = (current["Release date"]):sub(1,4) local releaseEndYear = (current["Release end date"]):sub(1,4) if releaseEndYear ~= releaseYear then if (tonumber(releaseEndYear) - tonumber(releaseYear)) == 1 then data["Release year"] = "in" else data["Release year"] = "between" end data["Release year"] = data["Release year"] .. " the years " .. releaseYear .." and " .. releaseEndYear .. "" else data["Release year"] = "in the year " .. releaseYear .."" end else data["Release year"] = "" end -- making outputText local outputText = " " if data["Adapted from"] ~= "" then outputText = outputText .. "Adapted from ''" .. data["Adapted from"] .. "''"	   		if data["Writer"] ~= "" then outputText = outputText .. ", written by " .. data["Writer"] if data["Publisher"] ~= "" then outputText = outputText .. " and released by " .. data["Publisher"] end elseif data["Publisher"] ~= "" then outputText = outputText .. " and released by " .. data["Publisher"] end elseif data["Writer"] ~= "" then outputText = outputText .. "Written by " .. data["Writer"] if data["Publisher"] ~= "" then outputText = outputText .. " and released by " .. data["Publisher"] end elseif data["Publisher"] ~= "" then outputText = outputText .. " Released by " .. data["Publisher"] else outputText = outputText .. "Released " end if data["Anthology"] ~= "" then outputText = outputText .. " as part of ''" .. data["Anthology"] .. "''"	   			if data["Series"] ~= "" then if current["Season"] then outputText = outputText .. " from " .. data["Series"] else outputText = outputText .. " from the series " .. data["Series"] end end elseif data["Series"] ~= "" then if current["Season"] then outputText = outputText .. " as part of " .. data["Series"] else outputText = outputText .. " as part of the series " .. data["Series"] end end if data["Release year"] ~= "" then if (data["Publisher"] ~= "") or (data["Writer"] == "") then outputText = outputText .. " " .. data["Release year"] else outputText = outputText .. " and released " .. data["Release year"] end end if data["Issues"] ~= "" then outputText = outputText .. " throught " .. data["Issues"] end if outputText == "" then outputText = "No data." else outputText = outputText .. "."			end outputText = "+ " .. outputText .. ""   		local name = current["Pagename"] local varBin = util_vars.setVar("sc-" .. name, outputText) end end end
 * ?Pagename
 * ?Anthology
 * ?Issues
 * ?Season
 * ?Range
 * ?Audio series
 * ?Writer
 * ?Publisher
 * ?Release date
 * ?Release end date
 * ?Series
 * ?Adapted from
 * ?Citation series ]=] )

function h.getInfo(story) return util_vars.getVar(PREFIX .. story) or cache.get(PREFIX .. story) or mw.smw.ask( story .. [=[	?Story info ]=]) end

function p.displayCitation(frame) local story = frame:getParent.args[1] return "" .. util_link.stripDab(story) .. " + " .. h.getInfo(story) .. "" end

--from https://sandbox.semantic-mediawiki.org/wiki/Module:Sm --- Concatenates a variable number of strings and numbers to one single string -- ignores tables, bools, functions, and such and replaces them with the empty string -- -- What is the benefit of using variable.concat instead of the .. operator? -- Answer: .. throws an error, when trying to concat bools, tables, functions, etc. -- This here handels them by converting them to an empty string -- -- @param ... varaibles to concatenate -- -- @return string concat = function(...) local args = {...} if #args == 0 then error('you must supply at least one argument to \'concat\' (got none)') end local firstArg = table.remove(args, 1) if type(firstArg) == 'string' or type(firstArg) == 'number' then firstArg = print(firstArg) else firstArg = '' end if #args == 0 then return firstArg else return firstArg .. concat(unpack(args)) end end

--- This dumps the variable (converts it into a string representation of itself) -- -- @param entity mixed, value to dump -- @param indent string, can bu used to set an indentation -- @param omitType bool, set to true to omit the () in front of the value -- -- @return string dump = function(entity, indent, omitType) local entity = entity local indent = indent and indent or '' local omitType = omitType if type( entity ) == 'table' then local subtable if not omitType then subtable = '(table)[' .. #entity .. ']:'       end indent = indent .. '\t' for k, v in pairs( entity ) do           subtable = concat(subtable, '\n', indent, k, ': ', dump(v, indent, omitType)) end return subtable elseif type( entity ) == 'nil' or type( entity ) == 'function' or type( entity ) == 'boolean' then return ( not omitType and '(' .. type(entity) .. ') ' or '' ) .. print(entity) elseif type( entity ) == 'string' then entity = mw.ustring.gsub(mw.ustring.gsub(entity, "\\'", "'"), "'", "\\'") return concat(omitType or '(string) ', '\, entity, '\) else -- number value expected return concat(omitType or '(' .. type( entity ) .. ') ', entity) end end

--- This function prints a variable depending on its type: -- * tables get concatenated by a comma -- * bools get printed as true or false -- * strings and numbers get simple returned as string -- * functions and nils return as emtpy string -- @return string print = function(v) if type( v ) == 'table' then return table.concat(v, ',') elseif type( v ) == 'boolean' then return ( v and 'true' or 'false' ) elseif type(v) == 'string' or type(v) == 'number' then return tostring(v) else return '' end end

return p