diff --git a/module_citeq.lua b/module_citeq.lua index 6bfb259..e08d61e 100644 --- a/module_citeq.lua +++ b/module_citeq.lua @@ -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 when there are no names in ; CITEREFYYYY is more-or-less meaningless + table.insert (refs, year); -- add + 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]] [[File:Wikidata-logo.svg|16px|alt=|link=]]", + "[[:d:Q%s|%s]] [[File:Wikidata-logo.svg|16px|class=noviewer|alt=|link=]]", 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 .. " [[File:Edit icon on Wikisite.svg|14px|alt=|link=[[:d:" .. author.qid .. "]] ]]") + 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 .. " [[File:Edit icon on Wikisite.svg|14px|alt=|link=[[:d:" .. editor.qid .. "]] ]]") + 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 .. " [[File:Edit icon on Wikisite.svg|14px|alt=|link=[[:d:" .. translator.qid .. "]] ]]") + 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 .. ' {{[[Template:Cite Q|Cite Q]]}}: Missing first/last names in Wikidata for ' .. table.concat(missing_names, ', ') .. ' ([[Template:Cite Q#Error messages|help]]) ' + cq_errors = cq_errors .. ' {{[[Template:Cite Q|Cite Q]]}}: Missing first/last names in Wikidata for ' .. table.concat(missing_names, ', ') .. ' ([[Template:Cite Q#Error messages|help]]) ' 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; -- 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 .. ' {{[[Template:Cite Q|Cite Q]]}}: supplied ' .. tpara .. ' does not match value generated from Wikidata: ' .. citeref .. ' ([[Template:Cite Q#Error messages|help]]) ' + cq_errors = cq_errors .. ' {{[[Template:Cite Q|Cite Q]]}}: supplied ' .. tpara .. ' does not match value generated from Wikidata: ' .. citeref .. ' ([[Template:Cite Q#Error messages|help]]) ' end end @@ -961,7 +979,7 @@ local function _cite_q (citeq_args) local id = cfg.i18n_t.wdq.. ' [[: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 [[|