local al, acount = mw.ustring.gsub(mw.text.trim(argsmedia), "%S+", "")
local i = 0
repeat -- the following could be condensed but repetitive for clarity
i = i + 1
if(mw.ustring.lower(medialist[i]) == "text" or mw.ustring.lower(medialist[i]) == "texts") then
media = media .. p.ia_url_encode(" OR mediatype:texts")
end
if(mw.ustring.lower(medialist[i]) == "audio") then
media = media .. p.ia_url_encode(" OR mediatype:audio")
end
if(mw.ustring.lower(medialist[i]) == "video") then
media = media .. p.ia_url_encode(" OR mediatype:video")
end
until i == acount
end  
media = "%29%20AND%20%28" .. media .. "%29"  
return media  
end  
-- Alt way to get b/d dates via getContent
function p.bdDateAlt(argsbirth, argsdeath, name)  
local pagetext = nil
local birth = ""
local death = ""  
-- Load the page
local t = mw.title.new(name)
if(t.exists) then
pagetext = t:getContent
end
if pagetext == nil then
return "Error"
end  
-- Remove false positives
pagetext = mw.ustring.gsub( mw.ustring.gsub(pagetext, "", ""), "", "")  
-- "Category:1900 births"
if argsbirth == "" or argsbirth == nil then
local birthcheck = mw.ustring.match(pagetext, "%[%[%s-[Cc]ategory:%s-%d+%.?%d*%s-births%s-%]%]" )
if birthcheck ~= nil then
birth = mw.ustring.match(birthcheck, "%d+%.?%d*")
else
birth = ""
end
else
birth = mw.text.trim(argsbirth)
end  
-- "Category:2000 deaths"
if argsdeath == "" or argsdeath == nil then
local deathcheck = mw.ustring.match(pagetext, "%[%[%s-[Cc]ategory:%s-%d+%.?%d*%s-deaths%s-%]%]" )
if deathcheck ~= nil then
death = mw.ustring.match(deathcheck, "%d+%.?%d*")
else
death = ""
end
else
death = mw.text.trim(argsdeath)
end  
return birth .. " " .. death  
end  
-- Get b/d dates via Wikidata.
-- 
function p.bdDate(argsbirth, argsdeath, name)  
local pagetext = nil
local birth = ""
local death = ""  
entity = mw.wikibase.getEntityObject
if not entity or not entity.claims then
-- Alternative if template not on a page in mainspace. This is needed since Wikidata can only be retrieved
-- for the article where the template is located.
return p.bdDateAlt(argsbirth, argsdeath, name)
end  
-- Note: The below uses formatPropertyValues to get and format the date from Wikidata.
-- For an alternative method, see sandbox revision dated 5:58 am, 15 October 2014
if argsbirth == "" or argsbirth == nil then
local birthtable = entity:formatPropertyValues( 'P569' )
local birthsplit = mw.text.split(birthtable["value"], " ")
local l, count = mw.ustring.gsub(birthtable["value"], "%S+", "")
if count > 0 then
if string.find(birthsplit[count], "^%d") then
birth = birthsplit[count]
elseif string.find(birthsplit[count], "BCE") then
birth = birthsplit[count - 1]
elseif string.find(birthsplit[count], "BC") then
birth = birthsplit[count - 1]
elseif string.find(birthsplit[count], "AD") then
birth = birthsplit[count - 1]
end
end
else
birth = mw.text.trim(argsbirth)
end  
if argsdeath == "" or argsdeath == nil then
local deathtable = entity:formatPropertyValues( 'P570' )
local deathsplit = mw.text.split(deathtable["value"], " ")
local l, count = mw.ustring.gsub(deathtable["value"], "%S+", "")
if count > 0 then
if string.find(deathsplit[count], "^%d") then
death = deathsplit[count]
elseif string.find(deathsplit[count], "BCE") then
death = deathsplit[count - 1]
elseif string.find(deathsplit[count], "BC") then
death = deathsplit[count - 1]
elseif string.find(deathsplit[count], "AD") then
death = deathsplit[count - 1]
end
end
else
death = mw.text.trim(argsdeath)
end  
if birth == "" and death == "" then
-- Alternative if Wikidata is missing data
-- return p.bdDateAlt(name)
return birth .. " " .. death
else
return birth .. " " .. death
end  
end  
--- URL-encode special characters
--- Note: this function was added later to deal with "&" characters instead of using p.ia_url_encode since
--- that may break existing instances of the template.
function p.urlX(str)
if (str) then
str = mw.ustring.gsub (str, "&", "%%26")
end
return str
end  
--- URL-encode a string
--- http://lua-users.org/wiki/StringRecipes
---
function p.ia_url_encode(str)
if (str) then
str = mw.ustring.gsub (str, "\n", "\r\n")
str = mw.ustring.gsub (str, "([^%w %-%_%.%~])",
function (c) return mw.ustring.format ("%%%02X", string.byte(c)) end)
str = mw.ustring.gsub (str, " ", "+")
end
return str
end  
-- Does str contain extended ascii? 1 = yes
function p.ia_extendedascii(str)
for i = 1, str:len do
if (str:byte(i) >= 32 and str:byte(i) <= 126) and str:byte(i) ~= 39 then -- 39 = "'"
--do nothing
else
return 1
end
end
return 0
end  
-- UTF-8 aware replacement for string.sub which doesn't support UTF-8.
-- Note: Using instead of mw.ustring.sub which I suspect(?) might be cause of intermittent error, and faster here for first-letter job.
-- Source: prapin @ Stack Overflow http://stackoverflow.com/questions/13235091/extract-the-first-letter-of-a-utf-8-string-with-lua
function p.firstLetter(str)
return str:match("[%z\1-\127\194-\244][\128-\191]*")
end  
-- Replace all extended ascii characters with wildcard '*'
-- Replace "-" with <space> eg. Pierre-Jean -> Pierre Jean
function p.ia_extendedascii2wildcard(str)
local s = ""
local j = 0
local k = 0
for i = 1, str:len do
k = str:byte(i)
if k >= 32 and k <= 126 then
-- For list of Lucene special characters needing to be escaped:
-- http://lucene.apache.org/core/4_10_0/queryparser/org/apache/lucene/queryparser/classic/package-summary.html#Escaping_Special_Characters
-- We only worry about - (45) and " (34) since the others are unlikely to appear in a proper name.
-- Also ' (39) since it is sometimes the extended character ’
if k == 34 or k == 39 then
s = s .. "*"
elseif k == 45 then
s = s .. " "
else
s = s .. str:sub(i,i)
end
else
if j == 1 then
s = s .. "*"
j = 2
end
if j == 0 then j = 1 end
if j == 2 then j = 0 end
end
end
return s
end  
-- Replace accented letters with non-accented equivalent letters
-- Note: this is not a complete list of all possible accented letters. It is
-- all of the accented letters found in the first 10,000 names using
-- the Internet Archive author template.
function p.ia_deaccent(str)
local s = str  
s = mw.ustring.gsub(s, "á", "a")
s = mw.ustring.gsub(s, "a︡", "a")
s = mw.ustring.gsub(s, "Á", "A")
s = mw.ustring.gsub(s, "ă", "a")
s = mw.ustring.gsub(s, "â", "a")
s = mw.ustring.gsub(s, "æ", "ae")
s = mw.ustring.gsub(s, "Æ", "AE")
s = mw.ustring.gsub(s, "à", "a")
s = mw.ustring.gsub(s, "ā", "a")
s = mw.ustring.gsub(s, "Ā", "A")
s = mw.ustring.gsub(s, "ą", "a")
s = mw.ustring.gsub(s, "å", "a")
s = mw.ustring.gsub(s, "Å", "A")
s = mw.ustring.gsub(s, "ã", "a")
s = mw.ustring.gsub(s, "ä", "a")
s = mw.ustring.gsub(s, "Ä", "A")
s = mw.ustring.gsub(s, "β", "B")
s = mw.ustring.gsub(s, "ć", "c")
s = mw.ustring.gsub(s, "č", "c")
s = mw.ustring.gsub(s, "Č", "C")
s = mw.ustring.gsub(s, "ç", "c")
s = mw.ustring.gsub(s, "Ç", "C")
s = mw.ustring.gsub(s, "ĉ", "c")
s = mw.ustring.gsub(s, "ď", "d")
s = mw.ustring.gsub(s, "đ", "d")
s = mw.ustring.gsub(s, "é", "e")
s = mw.ustring.gsub(s, "É", "E")
s = mw.ustring.gsub(s, "ě", "e")
s = mw.ustring.gsub(s, "ê", "e")
s = mw.ustring.gsub(s, "è", "e")
s = mw.ustring.gsub(s, "È", "E")
s = mw.ustring.gsub(s, "ε", "e")
s = mw.ustring.gsub(s, "ē", "e")
s = mw.ustring.gsub(s, "Ē", "E")
s = mw.ustring.gsub(s, "ę", "e")
s = mw.ustring.gsub(s, "ð", "e")
s = mw.ustring.gsub(s, "ë", "e")
s = mw.ustring.gsub(s, "Ë", "E")
s = mw.ustring.gsub(s, "γ", "Y")
s = mw.ustring.gsub(s, "ħ", "h")
s = mw.ustring.gsub(s, "i︠a︡", "ia")
s = mw.ustring.gsub(s, "í", "i")
s = mw.ustring.gsub(s, "i︠", "i")
s = mw.ustring.gsub(s, "ĭ", "i")
s = mw.ustring.gsub(s, "Í", "I")
s = mw.ustring.gsub(s, "î", "i")
s = mw.ustring.gsub(s, "Î", "I")
s = mw.ustring.gsub(s, "ì", "i")
s = mw.ustring.gsub(s, "ī", "i")
s = mw.ustring.gsub(s, "ł", "i")
s = mw.ustring.gsub(s, "ï", "i")
s = mw.ustring.gsub(s, "Ï", "I")
s = mw.ustring.gsub(s, "ĺ", "I")
s = mw.ustring.gsub(s, "Ĺ", "L")
s = mw.ustring.gsub(s, "μ", "u")
s = mw.ustring.gsub(s, "µ", "u")
s = mw.ustring.gsub(s, "ń", "n")