Compare commits

..

9 commits

2 changed files with 74 additions and 46 deletions

View file

@ -6,10 +6,11 @@ local _getPropOfProp = wdib._getPropOfProp
local followQid = wdib._followQid
--local getPropertyIDs = wdib._getPropertyIDs
local cite_cfg = mw.loadData ('Module:Cite/config')
local cfg = mw.loadData ('Module:Cite_Q/config')
local cfg = mw.loadData ('Module:Cite Q/config')
local cs1_module = 'Module:Citation/CS1' -- don't load the module yet, may not be needed
local cs1_cfg = mw.loadData('Module:Citation/CS1/Configuration')
local opt_cat = ''
local opt_cat = {}
local cq_errors = ''
local max_authors_displayed = 10 -- Don't look up all 500+ author items (is this in CS1 config somewhere?)
@ -59,6 +60,14 @@ local function in_array( needle, haystack )
return false
end
--[[--------------------------< T A B L E S E T _ A D D >------------------------------------------------------
Add needle to the haystack if it's not already there.
]]
local function tableset_add(haystack, needle)
if needle == nil or in_array(needle, haystack) then return false end
table.insert(haystack, needle)
return true
end
--[[--------------------------< 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.
@ -105,21 +114,23 @@ local function getStyledNames(names, style)
end
end
-- Concatenate up to the first four names, and the year, into a CITEREF and return it
-- Concatenate up to the first four names (at least one name required), and the year (optional), into a CITEREF and return it; return nil else
local function make_citeref(names, year)
local refs = {}
for i, v in ipairs(names) do
local name = v.last or v.label or ''
refs[i] = mw.text.trim(name)
if i == 4 then break end
local name = v.last or v.label;
if not name then break end; -- no v.last or v.label? abandon the ipairs() loop
refs[i] = mw.text.trim (name);
if i == 4 then break end -- collect only four names
end
table.insert(refs, year)
local id = table.concat(refs) or ''
if id == '' then
return ''
else
return "CITEREF" .. id
if 0 < #refs then -- don't add <year> when there are no names in <refs>; CITEREFYYYY is more-or-less meaningless
table.insert (refs, year); -- add <year>
table.insert (refs, 1, 'CITEREF'); -- and insert the 'CITEREF' prefix
return table.concat (refs); -- concatenate and done
end
return; -- no names so return nil
end
-- function to fetch a value to display
@ -192,7 +203,7 @@ local function makelink(v, out, maxpos, wdl, namestyle)
if wdl then
-- show that there's a Wikidata entry available
out[position] = string.format(
"[[:d:Q%s|%s]]&nbsp;<span title='%s'>[[File:Wikidata-logo.svg|16px|alt=|link=]]</span>",
"[[:d:Q%s|%s]]&nbsp;<span title='%s'>[[File:Wikidata-logo.svg|16px|class=noviewer|alt=|link=]]</span>",
v.mainsnak.datavalue.value["numeric-id"],
label,
cfg.i18n_t["errors"]["local-article-not-found"]
@ -252,8 +263,9 @@ local function makelink(v, out, maxpos, wdl, namestyle)
}
else
-- use anonymous
tableset_add(opt_cat, cfg.i18n_t.trackingcats["unknown-author"])
label = {
label = mw.wikibase.getLabel("Q4233718"):gsub("^%l", mw.ustring.upper) .. (cfg.i18n_t.trackingcats["unknown-author"] or ""),
label = mw.wikibase.getLabel("Q4233718"):gsub("^%l", mw.ustring.upper),
qid = 'Q4233718',
}
end
@ -853,7 +865,7 @@ local function _cite_q (citeq_args)
if author.qid then
-- ignore anonymous or non-humans
if author.human then
table.insert(missing_names, author.label .. "&nbsp;<span title='edit author in Wikidata'>[[File:Edit icon on Wikisite.svg|14px|alt=|link=[[:d:" .. author.qid .. "]] ]]</span>")
table.insert(missing_names, string.format (cfg.i18n_t.errors.missing_name, author.label, 'author', author.qid));
end
else
table.insert(missing_names, author.label)
@ -865,7 +877,7 @@ local function _cite_q (citeq_args)
if editor.qid then
-- ignore anonymous or non-humans
if editor.human then
table.insert(missing_names, editor.label .. "&nbsp;<span title='edit editor in Wikidata'>[[File:Edit icon on Wikisite.svg|14px|alt=|link=[[:d:" .. editor.qid .. "]] ]]</span>")
table.insert(missing_names, string.format (cfg.i18n_t.errors.missing_name, editor.label, 'editor', editor.qid));
end
else
table.insert(missing_names, editor.label)
@ -877,7 +889,7 @@ local function _cite_q (citeq_args)
if translator.qid then
-- ignore anonymous or non-humans
if translator.human then
table.insert(missing_names, translator.label .. "&nbsp;<span title='edit translator in Wikidata'>[[File:Edit icon on Wikisite.svg|14px|alt=|link=[[:d:" .. translator.qid .. "]] ]]</span>")
table.insert(missing_names, string.format (cfg.i18n_t.errors.missing_name, translator.label, 'translator', translator.qid));
end
else
table.insert(missing_names, translator.label)
@ -886,11 +898,10 @@ local function _cite_q (citeq_args)
end
-- add category and display error if name errors found
local name_cat = cfg.i18n_t.trackingcats['missing-author-first-last-names']
if #missing_names > 0 and not string.find(opt_cat, name_cat, 1, true) then
opt_cat = opt_cat .. name_cat
if #missing_names > 0 and not in_array('missing-author-first-last-names', opt_cat) then
tableset_add(opt_cat, cfg.i18n_t.trackingcats['missing-author-first-last-names'])
-- TODO: format and i18n this string
opt_cat = opt_cat .. ' <span class="error" style="font-size:inherit"><code class="cs1-code">{{[[Template:Cite Q|Cite Q]]}}</code>: Missing first/last names in Wikidata for ' .. table.concat(missing_names, ', ') .. ' ([[Template:Cite Q#Error messages|help]])</span> '
cq_errors = cq_errors .. ' <span class="error" style="font-size:inherit"><code class="cs1-code">{{[[Template:Cite Q|Cite Q]]}}</code>: Missing first/last names in Wikidata for ' .. table.concat(missing_names, ', ') .. ' ([[Template:Cite Q#Error messages|help]])</span> '
end
-- change edition to ordinal if it's set and numeric
@ -901,13 +912,20 @@ local function _cite_q (citeq_args)
if names.authors_overridden or (#names.authors == 0 and names.editors_overridden) then
-- if names are supplied manually, then don't mess with supplied ref parameter
elseif citeq_args.ref and 'none' ~= citeq_args.ref then
local citeref = make_citeref(names.authors or names.editors, pubyear)
if citeq_args.ref == citeref then
citeq_args.ref = nil -- avoid CS1 maint: ref duplicates default
else
local refnames
if #names.authors > 0 then
refnames = names.authors
elseif #names.editors > 0 then
refnames = names.editors
end
local citeref = refnames and make_citeref (refnames, pubyear) or nil; -- <refnames> nil when no authors and no editors
if citeq_args.ref == citeref then -- CITEREF from wikidata authors / editors matches the value provided in |ref=
citeq_args.ref = nil -- unset to avoid CS1 maint: ref duplicates default maintenance message / category
elseif citeref then -- CITEREF from wikidata authors / editors is different from the value provided in |ref=
tableset_add(opt_cat, cfg.i18n_t.trackingcats['supplied-ref'])
local tpara = frame:expandTemplate{ title = 'para', args = {'ref', citeq_args.ref} }
-- TODO: format and i18n this string
opt_cat = opt_cat .. ' <span class="error" style="font-size:inherit"><code class="cs1-code">{{[[Template:Cite Q|Cite Q]]}}</code>: supplied ' .. tpara .. ' does not match value generated from Wikidata: <code class="cs1-code">' .. citeref .. '</code> ([[Template:Cite Q#Error messages|help]])</span> '
cq_errors = cq_errors .. ' <span class="error" style="font-size:inherit"><code class="cs1-code">{{[[Template:Cite Q|Cite Q]]}}</code>: supplied ' .. tpara .. ' does not match value generated from Wikidata: <code class="cs1-code">' .. citeref .. '</code> ([[Template:Cite Q#Error messages|help]])</span> '
end
end
@ -961,7 +979,7 @@ local function _cite_q (citeq_args)
local id = cfg.i18n_t.wdq.. '&nbsp;[[:d:' .. qid .. '|' .. qid .. ']]'
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?
id = id .. '[[File:Wikidata-logo.svg|16px|class=noviewer|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
@ -972,7 +990,7 @@ local function _cite_q (citeq_args)
-- Indicator if item has been retracted
local retract = ''
if citeq_args.retracted_by then
opt_cat = opt_cat .. cfg.i18n_t.trackingcats.retracted
tableset_add(opt_cat, cfg.i18n_t.trackingcats.retracted)
local retract_id = string.gsub(citeq_args.retracted_by, 'Q', '')
local retr_params = {
citeq = 'yes',
@ -993,18 +1011,31 @@ local function _cite_q (citeq_args)
end
retract = ' ' .. frame:expandTemplate{ title = "Retracted", args = retr_params }
end
-- remove non-CS1 retraction properties
citeq_args.retracted_by = nil
citeq_args['retracted-intentional'] = nil
-- clean up any blank parameters
citeq_args.retracted_by = nil -- remove non-CS1 parameters
citeq_args['retracted-intentional'] = nil
citeq_args['instance-of'] = nil
if citeq_args['name-list-style'] and citeq_args['name-list-style'] == 'apa' then
citeq_args['name-list-style'] = nil
end
for k, v in pairs(citeq_args) do
if v == "" then citeq_args[k] = nil end
if v == "" then
citeq_args[k] = nil; -- remove blank parameters
elseif 'string' == type (v) then -- here when not blank; must be a string; |pmid= and the like are type 'number'
local changed; -- flag is a number when changes have been made; nil else
local pattern = '%[%[([^|]+)|%1%]%]'; -- pattern that looks for [[<link>|<label>]] wikilinks where <link> and <label> are identical
v, changed = v:gsub (pattern, '[[%1]]'); -- where <link> and <label> are identical, rewrite all such [[<link>|<label>]] wikilinks in <v> using [[<link>]]
if changed then -- when changes have been made
citeq_args[k] = v; -- update <citeq_args[k]>
end
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 and ("Cite "..template) or "Citation") } -- init with citation template
if expand == "self" then
citeq_args.id = old_id -- restore original |id= parameter
@ -1027,7 +1058,7 @@ local function _cite_q (citeq_args)
-- Indicator if item has corrigendum/erratum
local erratumid = ""
if citeq_args.erratum then
opt_cat = opt_cat .. cfg.i18n_t.trackingcats.erratum
tableset_add(opt_cat, cfg.i18n_t.trackingcats.erratum)
local erratum_params = {
citeq = 'yes',
id = frame:expandTemplate{
@ -1053,7 +1084,7 @@ local function _cite_q (citeq_args)
-- Indicator if item has an expression of concern notice (P14891)
local eoc = ""
if citeq_args['expression-of-concern'] then
opt_cat = opt_cat .. cfg.i18n_t.trackingcats['expression-of-concern']
tableset_add(opt_cat, cfg.i18n_t.trackingcats['expression-of-concern'])
local eoc_params = {
citeq = 'yes',
id = frame:expandTemplate{
@ -1077,22 +1108,16 @@ local function _cite_q (citeq_args)
citeq_args['eoc-intentional'] = nil
if citeq_args.replaced_by then
opt_cat = opt_cat .. cfg.i18n_t.trackingcats.replaced
tableset_add(opt_cat, cfg.i18n_t.trackingcats.replaced)
citeq_args.replaced_by = nil
end
if not citeq_args.title then -- emit category link if missing title
opt_cat = opt_cat .. cfg.i18n_t.trackingcats['no-label-or-title'];
tableset_add(opt_cat, cfg.i18n_t.trackingcats['no-label-or-title'])
end
-- Disable CiteSeerX IDs for now
citeq_args.citeseerx = nil
-- remove any remaining non-CS1 fields
citeq_args['instance-of'] = nil
if citeq_args['name-list-style'] and citeq_args['name-list-style'] == 'apa' then
citeq_args['name-list-style'] = nil
end
-- render the template
local lc_template = template and template:lower() or "citation"
local output
@ -1102,9 +1127,10 @@ local function _cite_q (citeq_args)
else
output = frame:expandTemplate{ -- render the template
title = "Cite "..template, args = citeq_args
} .. cfg.i18n_t.trackingcats["unknown-template"]
}
tableset_add(opt_cat, cfg.i18n_t.trackingcats["unknown-template"])
end
return output .. erratumid .. eoc .. retract .. opt_cat
return output .. erratumid .. eoc .. retract .. table.concat(opt_cat, ' ') .. cq_errors
end
local function cite_q (frame)

View file

@ -12,6 +12,7 @@ local i18n_t = {
["errors"] = {
["local-article-not-found"] = "Local article not found.",
["no-label-or-title"] = "No label or title -- debug: ",
missing_name = '%s&nbsp;<span title="edit %s in Wikidata">[[File:Edit icon on Wikisite.svg|14px|class=noviewer|alt=|link=[[:d:%s]] ]]</span>'; -- %s: <name>.label, '<name>', <name>.qid
},
["months"] = {
"January", "February", "March", "April", "May", "June",
@ -33,6 +34,7 @@ local i18n_t = {
["unknown-author"] = "", -- [[Category:Cite Q - author unknown]] removed as misplaced; someone is welcome to restore it if it is placed outside of the template parameter value
["unknown-template"] = "[[Category:Cite Q - template unknown]]",
["missing-author-first-last-names"] = "[[Category:Cite Q - authors with missing given and family names]]",
["supplied-ref"] = "[[Category:Cite Q - supplied citeref does not match]]",
},
}