Moduuli:Linkitetty data
Siirry navigaatioon
Siirry hakuun
Tämän moduulin ohjeistuksen voi tehdä sivulle Moduuli:Linkitetty data/ohje
local p = {} --p stands for package
-- p.printP18Image(frame) generates image for the image line
--
-- Parameters
-- * entity
-- Example return value:
-- "[[image:example.jpg|150px]]"
function p.printP18Image(frame)
local ret = ""
local entity = frame.args.entity or "";
local width = frame.args.width or "150px";
if entity == "" then
entity=mw.wikibase.getEntityIdForCurrentPage();
end
if not entity then
return ""
end
local claims=mw.wikibase.getBestStatements( entity, "P18")
for p, claim in pairs(claims) do
if claim["mainsnak"]
and claim["mainsnak"]["datavalue"]
and claim["mainsnak"]["datavalue"]["value"]
then
local filename=claim["mainsnak"]["datavalue"]["value"]
ret=ret .. "[[image:" .. filename .."|".. width .."]]"
end
end
return ret
end
-- p.printTitle(frame) generates text for the title line
--
-- Parameters
-- * entity
-- Example return value:
-- Linkitetty data: [[d:Q658627|<span title="Aki Kaurismäen elokuva vuodelta 1988">Ariel <small>(Q658627)</small></span>]]
function p.printTitle(frame)
local entity = frame.args.entity or "";
local prefix = (frame.args.title or "Linkitetty data") ..": ";
if entity == "" then
entity=mw.wikibase.getEntityIdForCurrentPage();
end
if not entity then
return ""
end
local label=mw.wikibase.getLabel(entity);
local description=mw.wikibase.getDescription(entity);
local linktext=mw.html.create('span')
:wikitext(label .. " (" .. entity ..")")
:attr("title", description )
:allDone();
local ret=prefix .."[[:d:" .. entity .."|" .. tostring(linktext) .."]]";
return ret;
end
-- p.printProperty(frame) generates property header col value
--
-- Parameters
-- * property
-- * wikidatapropertyrule
-- * default
-- Example return value:
--
-- <div class="wqswikidataproperty" data-wqswikidatapropertyrule="wdt:P136/wdt:P279*" data-wqswikidataproperty="P136">[[d:Property:P136|<span title="luovan työn lajityyppi tai lajityyppi, jonka parissa taiteilija työskentelee">lajityyppi]]</div>
function p.printProperty(frame)
local entity = frame.args.entity or "";
local property = frame.args.property or "";
local wikidatapropertyrule = frame.args.wikidatapropertyrule or "";
local default = frame.args.default or "";
if (property=="") then
return "";
end
if entity == "" then
entity=mw.wikibase.getEntityIdForCurrentPage();
end
if not entity then
entity="Q3019261"
end
local label=mw.wikibase.getLabel( property ) or "unknown";
-- Print property id:s also if we are in template documentation page
local titleobject=mw.title.getCurrentTitle();
if (titleobject.namespace==10) then
label=label .. " <small>(" .. property ..")</small>";
end
local description=mw.wikibase.getDescription( property );
local linktext=mw.html.create('span')
:wikitext(label)
:attr("title", description)
:allDone();
local target_prop = "d:property:" .. property
if entity and entity~="" then
target_prop="d:" .. entity .."#" .. property
end
local value="[[" .. target_prop .."|" .. tostring(linktext) .."]]";
local div=mw.html.create('div')
:addClass("wqswikidataproperty")
:attr("data-wqswikidataproperty", property)
:wikitext(value);
if (wikidatapropertyrule~="") then
div:attr("data-wikidatapropertyrule", wikidatapropertyrule);
end
if (default~="") then
div:addClass("wqsdefault");
end
div:allDone();
return tostring(div);
end
local function testHaystack(haystack, needle)
local ret = 1
if haystack~="" and needle~="" then
if mw.ustring.match(haystack, needle) then
ret=0
end
end
return ret
end
-- p.printValues(frame) generates valuelist for Linked data search
--
-- Parametes
-- * entity = wikidata item used
-- * property = property used
-- * link = if "-" then do not wikilink the value
-- * defaultlink = if "red" then create wikilinks even if the target page doesn't exist
-- * lcfirst = if "1" then lower case the first character of the value
-- * ucfirst = if "1" then upper case the first character of the value
--
-- return value example :
--
-- <span class="wqsbacklink" data-wqswikidatavalue="Q130232">[[Draamaelokuva|draamaelokuva]]</span><span class="wqsconj">, </span><span class="wqsbacklink" data-wqswikidatavalue="Q1135802">[[Taide-elokuva|taide-elokuva]]</span><span class="wqsconj">, </span><span class="wqsbacklink" data-wqswikidatavalue="Q1054574">[[Rakkauselokuva|romanttinen elokuva]]</span><span class="wqsconj">, </span><span class="wqsbacklink" data-wqswikidatavalue="Q157443">[[Komediaelokuva|komediaelokuva]]</span>
function p.printValues(frame)
local entity = frame.args.entity or "";
local lcfirst = frame.args.lcfirst or 0;
local link = frame.args.link or "";
local defaultlink = frame.args.defaultlink or "";
local property = frame.args.property or "";
local requiresource = frame.args.requiresource or ""
local referenceprops = {"P123","P248","P854", "P2013"}
local haystack = frame.args.haystack or "";
local showvaluetest=1
if property =="" then
return ""
end
if entity == "" then
entity=mw.wikibase.getEntityIdForCurrentPage();
end
if not entity then
return ""
end
local extwdlabels={}
local extwdlinks={}
local claims=mw.wikibase.getBestStatements( entity, property)
local out=""
local conj=""
if 1 then
for p, claim in pairs(claims) do
if claim["mainsnak"]["datatype"]=="wikibase-item"
and claim["mainsnak"]["datavalue"]~=nil
and claim["mainsnak"]["datavalue"]["value"]~=nil
and claim["mainsnak"]["datavalue"]["value"]["id"]~=nil
then
tmp_id=claim["mainsnak"]["datavalue"]["value"]["id"];
tmp_label=mw.wikibase.getLabel(tmp_id);
tmp_sitelink=mw.wikibase.getSitelink(tmp_id);
showvaluetest=testHaystack(haystack, tmp_label)
if (
1 -- only one pass
or (n==1 and tmp_sitelink~=nil) -- first pass for wikilinked pages
or (n==2 and tmp_sitelink==nil) -- second pass for pages without wikilink
) then
if lcfirst==1 then
tmp_label=mw.language:lcfirst(tmp_label)
elseif ucfirst==1 then
tmp_label=mw.language:ucfirst(tmp_label)
end
-- If address then print first house number
if property=="P669" then
if claim["qualifiers"]
and claim["qualifiers"]["P670"]~=nil
then
qualifconj=""
for p, qualifier in pairs(claim["qualifiers"]["P670"]) do
if qualifier["datavalue"] then
qualifiervalue = claim["qualifiers"]["P670"][1]["datavalue"]["value"]
tmp_label=mw.text.trim(tmp_label) .. qualifconj .. ' ' .. qualifiervalue
qualifconj = ','
showvaluetest = showvaluetest + testHaystack(haystack, qualifiervalue)
end
end
end
end
if link ~='-' then
if tmp_sitelink~=nil then
tmp_label = '[[' .. tmp_sitelink .. '|'.. tmp_label .. ']]';
elseif defaultlink=="red" then
tmp_label = '[[' .. tmp_label .. ']]';
end
end
out=out .. conj .. '<span class="wqsbacklink" data-wqswikidatavalue="'.. tmp_id .. '">' .. tmp_label ..'</span>';
conj='<span class="wqsconj">, </span>';
end
elseif claim["mainsnak"]["datatype"]=="string"
then
tmp_value=claim["mainsnak"]["datavalue"]["value"]
out=out .. conj .. '' .. tmp_value ..'';
conj='<span class="wqsconj">, </span>';
showvaluetest=testHaystack(haystack, tmp_value)
elseif claim["mainsnak"]["datavalue"]
and claim["mainsnak"]["datavalue"]["type"]
and claim["mainsnak"]["datavalue"]["type"]=="monolingualtext"
then
tmp_value = claim["mainsnak"]["datavalue"]["value"]["text"]
out=out .. conj .. '' .. tmp_value ..'';
conj='<span class="wqsconj">, </span>';
showvaluetest=testHaystack(haystack, tmp_value)
end
-- if all values and qualifiers are found on haystack then return ""
if showvaluetest == 0 then
return ""
end
if requiresource=="true" and claim["references"] then
local referencefound=0
for p, reference in pairs(claim["references"]) do
for r,referenceprop in pairs(referenceprops) do
if reference["snaks"][referenceprop] then
referencefound = 1
end
end
end
if referencefound == 0 then
out=out .. mw.getCurrentFrame():expandTemplate{title = "lähde"}
end
end
end
end
return out;
end
return p;