return self.conf.states[parameters.reference]:iterate(claim.references, {[parameters.general] = hookNames[parameters.reference][2], count = 1}) -- pass reference state with level 2 hook
else
return {} -- return empty array
end
end  
-- level 2 hook
function State:getReference(statement)
local key, citeWeb, citeQ, label
local params = {}
local citeParams = {['web'] = {}, ['q'] = {}}
local citeMismatch = {}
local useCite = nil
local useParams = nil
local value = ""
local ref = {}
local referenceEmpty = true -- will be set to false if at least one parameter is unremoved
local numAuthorParameters = 0
local numAuthorNameStringParameters = 0  
local version = 2 -- increment this each time the below logic is changed to avoid conflict errors  
if statement.snaks then
-- don't include "imported from", which is added by a bot
if statement.snaks[aliasesP.importedFrom] then
statement.snaks[aliasesP.importedFrom] = nil
end  
-- don't include "inferred from", which is added by a bot
if statement.snaks[aliasesP.inferredFrom] then
statement.snaks[aliasesP.inferredFrom] = nil
end  
-- don't include "type of reference"
if statement.snaks[aliasesP.typeOfReference] then
statement.snaks[aliasesP.typeOfReference] = nil
end  
-- don't include "image" to prevent littering
if statement.snaks[aliasesP.image] then
statement.snaks[aliasesP.image] = nil
end  
-- don't include "language" if it is equal to the local one
if self:getReferenceDetail(statement.snaks, aliasesP.language) == self.conf.langName then
statement.snaks[aliasesP.language] = nil
end  
-- retrieve all the parameters
for i in pairs(statement.snaks) do
label = ""  
-- multiple authors may be given
if i == aliasesP.author or i == aliasesP.authorNameString then
params[i] = self:getReferenceDetails(statement.snaks, i, false, self.linked, true) -- link = true/false, anyLang = true
else
params[i] = {self:getReferenceDetail(statement.snaks, i, false, (self.linked or (i == aliasesP.statedIn)) and (statement.snaks[i][1].datatype ~= 'url'), true)} -- link = true/false, anyLang = true
end  
if #params[i] == 0 then
params[i] = nil
else
referenceEmpty = false  
if statement.snaks[i][1].datatype == 'external-id' then
key = "external-id"
label = self.conf:getLabel(i)  
if label ~= "" then
label = label .. " "
end
else
key = i
end  
-- add the parameter to each matching type of citation
for j in pairs(citeParams) do
-- do so if there was no mismatch with a previous parameter
if not citeMismatch[j] then
-- check if this parameter is not mismatching itself
if i18n['cite'][j][key] then
-- continue if an option is available in the corresponding cite template
if i18n['cite'][j][key] ~= "" then
-- handle non-author properties (and author properties ("author" and "author name string"), if they don't use the same template parameter)
if (i ~= aliasesP.author and i ~= aliasesP.authorNameString) or (i18n['cite'][j][aliasesP.author] ~= i18n['cite'][j][aliasesP.authorNameString]) then
citeParams[j][i18n['cite'][j][key]] = label .. params[i][1]
-- to avoid problems with non-author multiple parameters (if existent), the following old code is retained
for k=2, #params[i] do
citeParams[j][i18n['cite'][j][key]..k] = label .. params[i][k]
end
-- handle "author" and "author name string" specially if they use the same template parameter
elseif i == aliasesP.author or i == aliasesP.authorNameString then
if params[aliasesP.author] ~= nil then
numAuthorParameters = #params[aliasesP.author]
else
numAuthorParameters = 0
end  
if params[aliasesP.authorNameString] ~= nil then
numAuthorNameStringParameters = #params[aliasesP.authorNameString]
else
numAuthorNameStringParameters = 0
end  
-- execute only if both "author" and "author name string" satisfy this condition: the property is both in params and in statement.snaks or it is neither in params nor in statement.snaks
-- reason: parameters are added to params each iteration of the loop, not before the loop
if ((statement.snaks[aliasesP.author] == nil) == (numAuthorParameters == 0)) and ((statement.snaks[aliasesP.authorNameString] == nil) == (numAuthorNameStringParameters == 0)) then
for k=1, numAuthorParameters + numAuthorNameStringParameters do
if k <= numAuthorParameters then -- now handling the authors from the "author" property
citeParams[j][i18n['cite'][j][aliasesP.author]..k] = label .. params[aliasesP.author][k]
else -- now handling the authors from "author name string"
citeParams[j][i18n['cite'][j][aliasesP.authorNameString]..k] = label .. params[aliasesP.authorNameString][k - numAuthorParameters]
end
end
end
end
end
else
citeMismatch[j] = true
end
end
end
end
end  
-- get title of general template for citing web references
citeWeb = split(mw.wikibase.getSitelink(aliasesQ.citeWeb) or "", ":")[2] -- split off namespace from front  
-- get title of template that expands stated-in references into citations
citeQ = split(mw.wikibase.getSitelink(aliasesQ.citeQ) or "", ":")[2] -- split off namespace from front  
-- (1) use the general template for citing web references if there is a match and if at least both "reference URL" and "title" are present
if citeWeb and not citeMismatch['web'] and citeParams['web'][i18n['cite']['web'][aliasesP.referenceURL]] and citeParams['web'][i18n['cite']['web'][aliasesP.title]] then
useCite = citeWeb
useParams = citeParams['web']  
-- (2) use the template that expands stated-in references into citations if there is a match and if at least "stated in" is present
elseif citeQ and not citeMismatch['q'] and citeParams['q'][i18n['cite']['q'][aliasesP.statedIn]] then
-- we need the raw "stated in" Q-identifier for the this template
citeParams['q'][i18n['cite']['q'][aliasesP.statedIn]] = self:getReferenceDetail(statement.snaks, aliasesP.statedIn, true) -- raw = true  
useCite = citeQ
useParams = citeParams['q']
end  
if useCite and useParams then
-- if this module is being substituted then build a regular template call, otherwise expand the template
if mw.isSubsting then
for i, v in pairs(useParams) do
value = value .. "|" .. i .. "=" .. v
end  
value = ""
else
value = mw.getCurrentFrame:expandTemplate{title=useCite, args=useParams}
end  
-- (3) if the citation couldn't be displayed using Cite web or Cite Q, but has properties other than the removed ones, throw an error
elseif not referenceEmpty then
value = "<span style=\"color: crimson\">" .. errorText("malformed-reference") .. "</span>"
end  
if value ~= "" then
value = {value} -- create one value object  
if not self.rawValue then
-- this should become a <ref> tag, so save the reference's hash for later
value.refHash = "wikidata-" .. statement.hash .. "-v" .. (tonumber(i18n['cite']['version']) + version)
end  
ref = {value} -- wrap the value object in an array
end
end  
return ref
end  
-- gets a detail of one particular type for a reference
function State:getReferenceDetail(snaks, dType, raw, link, anyLang)
local switchLang = anyLang
local value = nil  
if not snaks[dType] then
return nil
end  
-- if anyLang, first try the local language and otherwise any language
repeat
for _, v in ipairs(snaks[dType]) do
value = self.conf:getValue(v, raw, link, false, anyLang and not switchLang, false, true) -- noSpecial = true  
if value then
break
end
end  
if value or not anyLang then
break
end  
switchLang = not switchLang
until anyLang and switchLang  
return value
end  
-- gets the details of one particular type for a reference
function State:getReferenceDetails(snaks, dType, raw, link, anyLang)
local values = {}  
if not snaks[dType] then
return {}
end  
for _, v in ipairs(snaks[dType]) do
-- if nil is returned then it will not be added to the table
values[#values + 1] = self.conf:getValue(v, raw, link, false, anyLang, false, true) -- noSpecial = true
end  
return values
end  
-- level 1 hook
function State:getAlias(object)
local value = object.value
local title = nil  
if value and self.linked then
if self.conf.entityID:sub(1,1) == "Q" then
title = mw.wikibase.getSitelink(self.conf.entityID)