Module:Cite Q: Difference between revisions

Content deleted Content added
Ederporto (talk | contribs)
Syncing with sandbox
multiple minor fixes from sandbox
Line 1:
-- Version: 2020-12-26
 
local citeq = {}
 
require('Module:No globals')
local wdib = require('Module:WikidataIB')
Line 8 ⟶ 12:
["unknown-author"] = mw.wikibase.getLabel("Q4233718"):gsub("^%l", mw.ustring.upper),
["unknown-author-trackingcat"] = "[[Category:Cite Q - author unknown]]",
["ordinal"] = {
[1] = "st",
[2] = "nd",
[3] = "rd",
["default"] = "th"
},
}
 
-------------------------------------------------------------------------------
local simple_properties = { -- PXXX, is multiple?, linked?
-- makeOrdinal needs to be internationalised along with the above i18n
-- takes cardinal number as a numeric and returns the ordinal as a string
-- we need three exceptions in English for 1st, 2nd, 3rd, 21st, .. 31st, etc.
-------------------------------------------------------------------------------
local makeOrdinal = function(cardinal)
local card = tonumber(cardinal)
if not card then return cardinal end
local ordsuffix = i18n.ordinal.default
if card % 10 == 1 then
ordsuffix = i18n.ordinal[1]
elseif card % 10 == 2 then
ordsuffix = i18n.ordinal[2]
elseif card % 10 == 3 then
ordsuffix = i18n.ordinal[3]
end
-- In English, 1, 21, 31, etc. use 'st', but 11, 111, etc. use 'th'
-- similarly for 12 and 13, etc.
if (card % 100 == 11) or (card % 100 == 12) or (card % 100 == 13) then
ordsuffix = i18n.ordinal.default
end
return card .. ordsuffix
end
 
-- Table of simple properties that can be fetched in roughly the same way:
-- id = PXXX
-- maxvals = maximum number of multiple values (0 for all)
-- linked = "no" suppresses linking
-- populate_from_journal = true/false determines whether to look in a journal where the source is published
-- rank = "best", "preferred", normal, etc. determines how Wikidata ranks are treated
-- others = true - the value for the property goes to "others" section
local simple_properties = {
publisher = {id = "P123", maxvals = 1},
oclc = {id = "P243", maxvals = 1},
Line 52 ⟶ 93:
volume = {id = "P478", maxvals = 0, populate_from_journal = true},
-- part = {id = "P1545"?, maxvals = 0}, -- to be added to {{citation}} / COinS &rft.part=
title = {id = "P1476", maxvals rank="p 1n"},
url = {id = "P953", maxvals = 1}, -- full work available at
pages = {id = "P304", maxvals = 0, populate_from_journal = true},
Line 72 ⟶ 113:
performer = {id = "P175", maxvals = 10, others = true}, -- goes to "others" section
}
 
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 )
Line 86 ⟶ 122:
 
--[[--------------------------< I N _ A R R A Y >--------------------------------------------------------------
 
Whether needle is in haystack (taken from Module:Citation/CS1/Utilities)
 
]]
 
local function in_array( needle, haystack )
if needle == nil then
Line 105 ⟶ 138:
 
--[[--------------------------< A C C E P T _ V A L U E >-------------------------------------------------------
 
Accept WD value by framing in ((...)) if param_val is equal to keyword; else pass-through WD value as is.
 
]]
 
local function accept_value( param_val, wd_val )
local val = param_val
Line 137 ⟶ 167:
local qnumber = v.mainsnak.datavalue.value.id
local sitelink = mw.wikibase.getSitelink(qnumber)
if qnumber == "Q2818964" then sitelink = nil end -- suppress link to "Various authors"
if v.qualifiers and v.qualifiers.P1932 then
label = v.qualifiers.P1932[1].datavalue.value
Line 196 ⟶ 227:
 
--[=[-------------------------< G E T _ N A M E _ L I S T >----------------------------------------------------
 
get_name_list -- adapted from getAuthors code taken from Module:RexxS
arguments:
Line 203 ⟶ 233:
qid - value from |qid= parameter; the Q-id of the source (book, etc.) in qid
wdl - value from the |wdl= parameter; a Boolean passed to enable links to Wikidata when no article exists
 
returns nothing; modifies the args table
 
]=]
 
Line 211 ⟶ 239:
local propertyID = "P50"
local fallbackID = "P2093" -- author name string
 
if nl_type =="author" then
propertyID = 'P50'; -- for authors
fallbackID = 'P2093'; -- author-string
elseif nl_type =="editor" then
propertyID = 'P5769'; -- "editor-in-chief"
fallbackID = 'P98'; -- for editors - So-called "fallbacks" are actually a second set of properties processed
-- TBD. Take book series editors into account as well (if they have a separate P code as well)?
Line 252 ⟶ 280:
 
-- Make sure it actually has at least one of the properties requested
if not (props and props[1]) and not (fallback and fallback[1]) then
return nil
end
Line 300 ⟶ 328:
 
--[[-------------------------< 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.
 
]]
 
-- wraps a string in nowiki unless disable flag is set
local function wrap_nowiki(str)
local function wrap_nowiki(str, disable)
if disable then return str or '' end
return mw.text.nowiki(str or '')
end
Line 313 ⟶ 341:
function citeq.cite_q (frame)
local citeq_args = {};
local expand = ''; -- when set to anything, causes {{cite q}} to render <code><nowiki>{{citation|...}}</nowiki></code>
 
for k, v in pairs(frame:getParent().args) do
if in_array (k, {'expand', '_debug'}) then
if is_set(v) then
expand = v; -- record setting but don't pass |expand= to {{citation}}
end
else
if v ~= "" then citeq_args[k] = v end
end
end
 
for k, v in pairs(frame.args) do
if v ~= "" then citeq_args[k] = v end
end
 
-- parameters that don't get passed to Citation
local qid = citeq_args.qid
local wdl = citeq_args.wdl
local template = citeq_args.template
citeq_args.qid = nil
citeq_args.wdl = nil
citeq_args.template = nil
 
local titleforced = (citeq_args.title ~= nil)
 
local oth = {}
 
citeq_args.language = citeq_args.language or getPropOfProp( {qid = qid, prop1 = "P407", prop2 = "P218", ps = 1} )
if citeq_args.language == '' then -- is this needed now?
citeq_args.language = nil
end
Line 347 ⟶ 380:
 
for name, data in pairs(simple_properties) do
citeq_args[name] = getValue( {data.id, psfwd = 1"ALL", osd = "no", noicon = "true", qid = qid, maxvals = data.maxvals, linked = data.linked, rank = data.rank or "best", citeq_args[name] } )
if data.populate_from_journal then
citeq_args[name] = getValue( {"P1433", ps = 1, qid = qid, maxvals = 0, citeq_args[name], qual = data.id, qualsonly = 'yes'} )
Line 387 ⟶ 419:
 
citeq_args.isbn = getValue( {"P957", ps = 1, qid = qid, maxvals = 0, citeq_args.isbn } ) -- try ISBN 10
 
citeq_args.url = getValue( {"P856", ps = 1, qid = qid, maxvals = 0, citeq_args.url } ) -- try official website
citeq_args.url = getValue( {"P2699", ps = 1, qid = qid, maxvals = 0, citeq_args.url } ) -- try url
Line 398 ⟶ 430:
if slink then
citeq_args.url = nil
wrap_title = wrap_nowiki(citeq_args.title, titleforced)
slink_flag = true
else
citeq_args.title = wrap_nowiki(citeq_args.title, titleforced)
end
else
Line 409 ⟶ 441:
citeq_args.title = '[[' .. slink .. ']]'
else
wrap_title = wrap_nowiki(slink:gsub("%s%(.+%)$", ""):gsub(",.+$", ""), titleforced)
slink_flag = true
end
else
citeq_args.title = wrap_nowiki(label, titleforced)
end
end
Line 482 ⟶ 514:
local editor_count = 0
for k, v in pairs(citeq_args) do
if k:find("^editor%d+$") then
editor_count = editor_count + 1
end
end
if editor_count > 8 then -- convention in astronomy journals, optional mode for this?
citeq_args['display-editors'] = citeq_args['display-editors'] or 3
end
 
-- change edition to ordinal if it's set and numeric
citeq_args.edition = citeq_args.edition and makeOrdinal(citeq_args.edition)
 
-- code to make a guess what template to use from the supplied parameters
-- (first draft for proof-of-concept)
if citeq_args.isbn then
template = "book"
elseif citeq_args.journal then
template = "journal"
elseif citeq_args.website then
template = "web"
end
 
-- template is CS1 designator: journal, web, news, etc.
if template then
citeq_args.mode = citeq_args.mode or "cs1"
template = "Cite " .. template
else
citeq_args.mode = citeq_args.mode or "cs2"
template = "Citation"
end
 
Line 506 ⟶ 560:
end
 
if is_set(expand) then -- if |expand=<anything>, write a nowiki'd version to see what the {{citation}} template call looks like
if is_set(expand) then
local expand_args = {'{{citation'}; -- init with citation template
local expand_args = { '{{' .. template }; -- init with citation template
if expand == "self" then
citeq_args.id = old_id; -- restore original |id= parameter
expand_args = {'{{cite Q|' .. qid}; -- expand to itself
end
for p, v in pairs (citeq_args) do -- spin through citeq_args and
table.insert (expand_args, p .. '=' .. v); -- add parameter name = value
end
-- make the nowiki'd string and done
return table.concat ({'<code>', frame:callParserFunction ('#tag:nowiki', table.concat (expand_args, ' |') .. '}}'), '</code>'})
end
 
local opt_cat = ''
if getValue( {"P5824", ps = 1, qid = qid} ) then
Line 525 ⟶ 581:
opt_cat = opt_cat .. '[[Category:Cite Q - cites a replaced work]]'
end
return frame:expandTemplate{title = 'citation'template, args = citeq_args} .. opt_cat -- render the template
end