Difference between revisions of "Module:Cite Q/sandbox"

From blackwiki
Jump to navigation Jump to search
test>RexxS
(Create sandbox version of Module:Citeq)
 
test>RexxS
(modify to output links as authorlink)
Line 58: Line 58:
 
 
 
-- So now we have something to return:
 
-- So now we have something to return:
-- table 'out' is going to to store the return value(s):
+
-- table 'out' is going to to store the author names(s):
 +
-- and table 'link' will store any links to the author's article
 
local out = {}
 
local out = {}
 +
local link = {}
 
if props[1].mainsnak.datavalue.type == "wikibase-entityid" then
 
if props[1].mainsnak.datavalue.type == "wikibase-entityid" then
 
-- it's wiki-linked value, so output as link if possible
 
-- it's wiki-linked value, so output as link if possible
Line 72: Line 74:
 
end
 
end
 
if sitelink then
 
if sitelink then
out[#out + 1] = "[[" .. sitelink .. "|" .. label .. "]]"
+
-- just the plain author name,
 +
-- but keep a record of the links, using the same index
 +
out[#out + 1] = label
 +
link[#out] = sitelink
 
else
 
else
 
-- no sitelink, so check first for a redirect with that label
 
-- no sitelink, so check first for a redirect with that label
Line 78: Line 83:
 
if artitle.id > 0 then
 
if artitle.id > 0 then
 
if artitle.isRedirect then
 
if artitle.isRedirect then
-- no sitelink, but there's a redirect with the same title as the label; let's link to that
+
-- no sitelink,
out[#out + 1] = "[[" .. label .. "]]"
+
-- but there's a redirect with the same title as the label;
 +
-- so store the link to that
 +
out[#out + 1] = label
 +
link[#out] = label
 
else
 
else
 
-- no sitelink and not a redirect but an article exists with the same title as the label
 
-- no sitelink and not a redirect but an article exists with the same title as the label
Line 107: Line 115:
 
-- construct the list in the format we want
 
-- construct the list in the format we want
 
for k,v in ipairs(out) do
 
for k,v in ipairs(out) do
local parameter_name = 'author' .. k;
+
local parameter_name = 'author' .. k
args[parameter_name] = v;
+
args[parameter_name] = v
 +
end
 +
for k,v in pairs(link) do
 +
local parameter_name = 'authorlink' .. k
 +
args[parameter_name] = v
 
end
 
end
 
end
 
end

Revision as of 20:49, 27 May 2017

Documentation for this module may be created at Module:Cite Q/sandbox/doc

require('Module:No globals')

local citeq = {}

--[[--------------------------< I S _ S E T >------------------------------------------------------------------

Returns true if argument is set; false otherwise. Argument is 'set' when it exists (not nil) or when it is not an empty string.

]]
local function is_set( var )
	return not (var == nil or var == '');
end


--[=[-------------------------< G E T _ A U T H O R S >--------------------------------------------------------

getAuthors -- adapted from code taken from [[Module:RexxS]]
arguments:
	args - pointer to the parameter arguments table from the template call
	qid - value from |qid= parameter; the Q-id of the source (book, etc.) in qid
	wdl - value from the |wdlinks= parameter; a boolean passed to enable links to Wikidata when no article exists

returns nothing; modifies the args table

]=]

local function get_authors (args, qid, wdl)
	local propertyID = "P50"
	
	-- wdlinks is a boolean passed to enable links to Wikidata when no article exists
	-- if "false" or "no" or "0" is passed set it false
	-- if nothing or an empty string is passed set it false
	if wdl and (#wdl > 0) then
		wdl = wdl:lower()
		if (wdl == "false") or (wdl == "no") or (wdl == "0") then
			wdl = false
		else
			wdl = true
		end
	else
		-- wdl is empty, so
		wdl = false
	end
	
	local entity, props
	local entity = mw.wikibase.getEntity(qid)
	if entity and entity.claims then
		props = entity.claims[propertyID]
	else
		-- there's no such entity or no claims for the entity
		return nil
	end
	
	-- Make sure it actually has the property requested
	if not props or not props[1] then 
		return nil
	end
	
	-- So now we have something to return:
	-- table 'out' is going to to store the author names(s):
	-- and table 'link' will store any links to the author's article
	local out = {}
	local link = {}
	if props[1].mainsnak.datavalue.type == "wikibase-entityid" then
		-- it's wiki-linked value, so output as link if possible
		for k, v in pairs(props) do
			local qnumber = "Q" .. v.mainsnak.datavalue.value["numeric-id"]
			local sitelink = mw.wikibase.sitelink(qnumber)
			local label = mw.wikibase.label(qnumber)
			if label then
				label = mw.text.nowiki(label)
			else
				label = qnumber
			end
			if sitelink then
				-- just the plain author name,
				-- but keep a record of the links, using the same index
				out[#out + 1] = label
				link[#out] = sitelink
			else
				-- no sitelink, so check first for a redirect with that label
				local artitle = mw.title.new(label, 0)
				if artitle.id > 0 then
					if artitle.isRedirect then
						-- no sitelink,
						-- but there's a redirect with the same title as the label;
						-- so store the link to that
						out[#out + 1] = label
						link[#out] = label
					else
						-- no sitelink and not a redirect but an article exists with the same title as the label
						-- that's probably a dab page, so output the plain label
						out[#out + 1] = label
					end
				else
					-- no article or redirect with the same title as the label
					if wdl then
						-- show that there's a Wikidata entry available
						out[#out + 1] = "[[:d:Q" .. v.mainsnak.datavalue.value["numeric-id"] .. "|" .. label .. "]]&nbsp;<span title='" .. i18n["errors"]["local-article-not-found"] .. "'>[[File:Wikidata-logo.svg|16px|alt=|link=]]</span>"
					else
						-- no wikidata links wanted, so just give the plain label
						out[#out + 1] = label
					end
				end
			end
		end
	else
		-- not a linkable article title
		out[#out+1] = entity:formatPropertyValues(propertyID).value
	end
	
	-- if there's anything to return, then return a list
	-- in the form |author1=firstname secondname |author2= ...
	if #out > 0 then
		-- construct the list in the format we want
		for k,v in ipairs(out) do
			local parameter_name = 'author' .. k
			args[parameter_name] = v
		end
		for k,v in pairs(link) do
			local parameter_name = 'authorlink' .. k
			args[parameter_name] = v
		end
	end
end


--[[-------------------------< C I T E _ Q >------------------------------------------------------------------

Takes standard cs1|2 template parameters and passes all to {{citation}}.  If neither of |author= and |author1=
are set, calls get_authors() to try to get an author name-list from wikidata.  The result is passed to 
{{citation}} for rendering.

]]

function citeq.cite_q (frame)
local citeq_args = {};
local qid;
local wdl;

local args = frame.args;														-- get arguments

	for k, v in pairs (args) do													-- copy args into citeq_args
		if 'qid' == k then														-- don't copy qid
			qid = v;															-- save its value
		elseif 'wdlinks' == k then												-- don't copy wdlinks
			wdl = v;															-- save its value
		else
			citeq_args[k] = v													-- but copy everything else
		end
	end
	
	if not is_set (citeq_args.author) and not is_set (citeq_args.author1) then	-- if neither are set, try to get authors from wikidata
		if is_set (qid) then
			get_authors (citeq_args, qid, wdl);									-- modify citeq_args table with authors from wikidata
		end
	end

	return frame:expandTemplate{title = 'citation', args = citeq_args};			-- render the citation
end

return citeq