Module:Cite Q/sandbox: Difference between revisions

Content deleted Content added
fix
only include |id= parameter if template is not arxiv, add guessing of arxiv template based on arxiv identifier
 
(39 intermediate revisions by 7 users not shown)
Line 1:
-- Version: 2021-0210-2119
 
local p = {}
 
require('Module:No globalsstrict')
local wdib = require('Module:WikidataIB')
local getValue = wdib._getValue
Line 9:
local followQid = wdib._followQid
local getPropertyIDs = wdib._getPropertyIDs
local getSitelink = wdib._getSitelink
 
local i18n = {
Line 176 ⟶ 175:
if v.qualifiers and v.qualifiers.P1932 then
label = v.qualifiers.P1932[1].datavalue.value
elseif v.qualifiers and v.qualifiers.P1810 then
label = v.qualifiers.P1810[1].datavalue.value
else
label = mw.wikibase.getLabel(qnumber)
Line 221 ⟶ 218:
if v.qualifiers and v.qualifiers.P1932 then
label = v.qualifiers.P1932[1].datavalue.value
elseif v.qualifiers and v.qualifiers.P1810 then
label = v.qualifiers.P1810[1].datavalue.value
else
label = i18n["unknown-author"] .. (i18n["unknown-author-trackingcat"] or "")
Line 275 ⟶ 270:
end
 
local entity = mw.wikibase.getEntity(qid)
local props = nil
local fallback = nil
if mw.wikibase.entityExists(qid) then
if entity and entity.claims then
props = entitymw.claims[wikibase.getAllStatements(qid, propertyID])
if props and fallbackID then
fallback = entitymw.claims[wikibase.getAllStatements(qid, fallbackID])
end
end
Line 317 ⟶ 311:
table.sort(keys) -- as they might be out of order
for i, k in ipairs(keys) do
out[k] = out[k]:gsub (''', '\''); -- prevent cs1|2 multiple names categorization; replace html entity with the actual character
mw.log(i .. " " .. k .. " " .. (out[k]))
if args[nl_type .. i] then -- name gets overwritten
Line 395 ⟶ 390:
-- parameters that don't get passed to Citation
local expand = citeq_args.expand -- when set to anything, causes {{cite q}} to render <code><nowiki>{{citation|...}}</nowiki></code>
local qid = citeq_args[1].qid or citeq_args.qid[1]
local wdl = citeq_args.wdl
local template = citeq_args.template
Line 480 ⟶ 475:
if url then
citeq_args.url = mw.text.split( url, " (", true )[1]
local arcurl = mw.ustring.match( url, " %((.*)%)" ) -- when there is an archive url, <url> holds: url<space>(archive url); here extract the archive url if present
if arcurl then
local arcy, arcm, arcd = arcurl:match("(20%d%d)%p?(%d%d)%p?(%d%d)")
Line 490 ⟶ 485:
break
end
end
end
 
-- See if we have wikisource
if not citeq_args.url then
sitelink = getSitelink(qid, "enwikisource") or nil
if sitelink then
citeq_args.url = 'https://en.wikisource.org/wiki/' .. sitelink
end
end
Line 507 ⟶ 494:
 
if not titleforced then
-- Handle subtitle.
if citeq_args.title then
local subtitle = mw.wikibase.getBestStatements (qid, 'P1680');
if 0 ~= #subtitle then
subtitle = subtitle[1].mainsnak.datavalue.value.text;
citeq_args.title = citeq_args.title .. ": " .. subtitle
end
end
local htmltitle = getValue( {"P1476", qual = "P6833", ps = 1, qid = qid, maxvals = 1, qo = "y"} )
if htmltitle then
Line 521 ⟶ 517:
local slink_flag = false
local wrap_title = ''
local wslink = false
if not slink then
-- See if we have wikisource
if not citeq_args.url then
local wikisource_sitelink = mw.wikibase.getSitelink(qid, "enwikisource") or nil
if wikisource_sitelink then
slink = ':s:'..wikisource_sitelink
wslink = true
end
end
end
if citeq_args.title then
if slink then
Line 529 ⟶ 536:
end
else
if slink and not wslink then
if slink:lower() == title_display:lower() then
citeq_args.title = '[[' .. slink .. ']]'
Line 536 ⟶ 543:
slink_flag = true
end
elseif wslink then
wrap_title = wrap_nowiki(title_display)
slink_flag = true
else
citeq_args.title = wrap_nowiki(title_display)
Line 541 ⟶ 551:
end
if slink_flag then
if slink == wrap_title and not wslink then -- direct link
citeq_args.title = '[[' .. slink .. ']]'
else -- piped link
Line 604 ⟶ 614:
end
if author_count > 8 then -- convention in astronomy journals, optional mode for this?
citeq_args[if 'display-authorsall'] == citeq_args['display-authors'] or 3then
citeq_args['display-authors'] = nil; -- unset because no longer needed
else
citeq_args['display-authors'] = citeq_args['display-authors'] or 3 -- limit to three displayed names
end
end
 
Line 614 ⟶ 628:
end
if editor_count > 8 then -- convention in astronomy journals, optional mode for this?
citeq_args[if 'display-editorsall'] == citeq_args['display-editors'] or 3then
citeq_args['display-editors'] = nil; -- unset because no longer needed
else
citeq_args['display-editors'] = citeq_args['display-editors'] or 3 -- limit to three displayed names
end
end
 
Line 629 ⟶ 647:
elseif citeq_args.website then
template = template or "web"
elseif citeq_args.arxiv then
template = template or "arxiv"
end
 
-- support arXiv classification
-- template is CS1 designator: journal, web, news, etc.
if templateciteq_args.arxiv then
local arxiv_class
citeq_args.mode = citeq_args.mode or "cs1"
arxiv_class = getValue( {"P818", qual = "P820", qid = qid, maxvals = 1, rank="best", qualsonly=true} )
template = "Cite " .. template
if arxiv_class then
else
-- See https://en.wikipedia.org/wiki/Template:Cite_arXiv
citeq_args.mode = citeq_args.mode or "cs2"
-- "class: arXiv classification, e.g. hep-th. Optional.
template = "Citation"
-- To be used only with new-style (2007 and later) eprint identifiers
-- that do not include the classification."
citeq_args.class = arxiv_class
end
end
 
-- ************* |id= parameter **********************
-- |id= could hold more than one identifier pulled from Wikidata not supported by {{citation}}, right now only add our qid to the list
-- Only include |id= if template is not arxiv
local list_sep = '. '
if citeq_args.modetemplate ~= 'cs1'"arxiv" then
-- |id= could hold more than one identifier pulled from Wikidata not supported by {{citation}}, right now only add our qid to the list
list_sep = ', '
local list_sep = '. '
end
if citeq_args.mode ~= 'cs1' then
local id = '[[WDQ (identifier)|Wikidata]]&nbsp;[[:d:' .. qid .. '|' .. qid .. ']]' -- go through "WDQ (identifier)" redirect to reduce clutter in "What links here" and improve reverse lookup. Keep in sync with {{QID}}.
list_sep = ', '
local old_id = citeq_args.id
if wdl then -- show WD logo
id = id .. '[[File:Wikidata-logo.svg|16px|alt=|link=]]' -- possibly replace by WD edit icon?
end
if is_set (old_id) then
citeq_args.id = old_id .. list_sep .. id -- append to user-specified contents
else
citeq_args.id = id
end
 
-- clean up any blank parameters
for k, v in pairs(citeq_args) do
if v == "" then citeq_args[k] = nil end
end
 
-- if |expand=<anything>, write a nowiki'd version to see what the {{citation}} template call looks like
if expand then
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
local id = '[[WDQ (identifier)|Wikidata]]&nbsp;[[:d:' .. qid .. '|' .. qid .. ']]' -- go through "WDQ (identifier)" redirect to reduce clutter in "What links here" and improve reverse lookup. Keep in sync with {{QID}}.
-- make a sortable table and sort it by param name
local sorttableold_id = {}citeq_args.id
if wdl then -- show WD logo
for param, val in pairs (citeq_args) do
id = id .. '[[File:Wikidata-logo.svg|16px|alt=|link=]]' -- possibly replace by WD edit icon?
table.insert(sorttable, {param, val})
end
if is_set (old_id) then
table.sort(sorttable, comp_key)
citeq_args.id = old_id .. list_sep .. id -- append to user-specified contents
-- add contents to expand_args
else
for idx, val in ipairs(sorttable) do
citeq_args.id = id
table.insert(expand_args, val[1] .. '=' .. val[2])
end
 
-- clean up any blank parameters
for k, v in pairs(citeq_args) do
if v == "" then citeq_args[k] = nil end
end
 
-- if |expand=<anything>, write a nowiki'd version to see what the {{citation}} template call looks like
if expand then
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
-- make a sortable table and sort it by param name
local sorttable = {}
for param, val in pairs (citeq_args) do
table.insert(sorttable, {param, val})
end
table.sort(sorttable, comp_key)
-- add contents to expand_args
for idx, val in ipairs(sorttable) do
table.insert(expand_args, val[1] .. '=' .. val[2])
end
-- make the nowiki'd string and done
return frame:preprocess (table.concat ({'<syntaxhighlight lang="wikitext" inline="1">', table.concat (expand_args, ' |') .. '}}', '</syntaxhighlight>'}));
end
-- make the nowiki'd string and done
return "<code>" .. table.concat (expand_args, ' |') .. "}}</code>"
end
 
-- ************* Assign template **********************
-- template is CS1 designator: journal, web, news, etc.
if template then
-- citeq_args.mode = citeq_args.mode or "cs1" -- a cs1 template already knows that it is cs1 so this line is superfluous
template = "Cite " .. template
else
-- citeq_args.mode = citeq_args.mode or "cs2" -- a cs2 template already knows that it is cs2 so this line is superfluous
template = "Citation"
end
 
local erratumid = getPropertyIDs( { "P2507", qid = qid, fwd = "ALL", osd = "no", rank = "best", maxvals = 1 } )
Line 751 ⟶ 789:
end
end
 
return p