Module:Sandbox/GreenC/Internet Archive author

--[[

This module is for functions related to Internet Archive.

]]

local p = {}

--[[

This function used for Template:Internet Archive author

]] function p.author(frame)

local pframe = frame:getParent local args = pframe.args local tname = "Internet Archive author" -- name of calling template. Change if template is renamed. local name = nil -- article name (default: current page) local dname = nil -- display name (default: current page name) local sname = nil -- search name (default: current page name) local birth = nil local death = nil local byabout = "Works by or about" local tagline = "at Internet Archive" local urlhead = "//archive.org/search.php?query=" local media = "" local pagetext = nil

--- Determine name if args.name == "" or args.name == nil then name = mw.title.getCurrentTitle.text dname = name sname = dname else name = mw.text.trim(args.name) dname = name sname = dname end if args.sname ~= nil and args.sname ~= "" then sname = mw.text.trim(args.sname) end if args.dname ~= nil and args.dname ~= "" then dname = mw.text.trim(args.dname) end dname = mw.ustring.gsub(dname,"%s%(.*%)", "") -- remove disambiguation sname = mw.ustring.gsub(sname,"%s%(.*%)", "")

--- Determine tagline if args.coda ~= "" and args.coda ~= nil then tagline = tagline .. " " .. mw.text.trim(args.coda) end

--- Custom search. Do early to avoid unnecessary processing. if args.search ~= "" and args.search ~= nil then local search = ia_url_encode(mw.text.trim(args.search)) return "[" .. urlhead .. search .. " " .. byabout .. " " .. dname .. "] " .. tagline end

--- Determine media types if args.media ~="" and args.media ~=nil then local medialist, acount = mw.text.split(mw.text.trim(args.media), " ") --   local al, acount = mw.ustring.gsub(mw.text.trim(args.media), "%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 if(i == 1) then media = media .. ia_url_encode("(mediatype:texts")               else          media = media .. ia_url_encode(" OR mediatype:texts")                 end      end      if(mw.ustring.lower(medialist[i]) == "audio") then        if(i == 1) then          media = media .. ia_url_encode("(mediatype:audio") else media = media .. ia_url_encode(" OR mediatype:audio") end end if(mw.ustring.lower(medialist[i]) == "video") then if(i == 1) then media = media .. ia_url_encode("(mediatype:video")       else          media = media .. ia_url_encode(" OR mediatype:video")        end      end    until i == acount    if media ~= nil then      media = media .. ")%20AND%20" else media = "" end else media = "" end

--- Determine dob if args.birth == "" or args.birth == nil then local pagetext = nil -- Load the page local t = mw.title.new(name) if(t.exists) then pagetext = t:getContent end if pagetext == nil then return "Error in Template:"..tname..": " ..name.. " doesn't exist." end -- Remove false positives pagetext = mw.ustring.gsub( mw.ustring.gsub(pagetext, "", ""), " .- ", "") -- Scrape for the Category and find date 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 = "none" end else birth = mw.ustring.gsub(mw.text.trim(args.birth), " ", "") end --Determine dod if args.death == "" or args.death == nil then

local pagetext = nil -- Load the page if pagetext == nil then -- don't load again if already done above local t = mw.title.new(name) if(t.exists) then pagetext = t:getContent end if pagetext == nil then return "Error in Template:"..tname..": " ..name.. " doesn't exist." end

-- Remove false positives pagetext = mw.ustring.gsub( mw.ustring.gsub(pagetext, "", ""), " .- ", "")

end

-- Scrape for the Category and find date 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 = "none" end else death = mw.ustring.gsub(mw.text.trim(args.death), " ", "") end

--- Split sname into words and count words local exploded = mw.text.split(sname, " ") local l, count = mw.ustring.gsub(sname, "%S+", "")

--[[

Begin formatting URL

]]

-- If no dob and dod, return a simple search if birth == "none" or death == "none" then

local search = "(subject%3A%22"..exploded[2].."%2C%20"..exploded[1].."%22%20OR%20creator%3A%22"..exploded[2].."%2C%20"..exploded[1].."%22%20OR%20creator%3A%22"..exploded[1].."%20"..exploded[2].."%22%20OR%20title%3A%22"..exploded[1].."%20"..exploded[2].."%22%20OR%20description%3A%22"..exploded[1].."%20"..exploded[2].."%22%20)"

return "[" .. urlhead .. media .. search .. " " .. byabout .. " " .. dname .. "] " .. tagline

else -- Optimized search based on number of words

-- One or Five+ words search string if count == 1 or count > 4 then

local nameurl = ia_url_encode(sname)

local search = "(subject%3A%22"..nameurl.."%22%20OR%20creator%3A%22"..nameurl.."%22%20description%3A%22"..nameurl.."%22%20title%3A%22"..nameurl.."%22%20)"

return "[" .. urlhead .. media .. search .. " " .. byabout .. " " .. dname .. "] " .. tagline

end

-- Two words search string if count == 2 then

local search = "(subject%3A%22"..exploded[2].."%2C%20"..exploded[1].."%2C%20"..birth.."-"..death.."%22%20OR%20creator%3A%22"..exploded[2].."%2C%20"..exploded[1].."%2C%20"..birth.."-"..death.."%22%20OR%20creator%3A%22"..exploded[1].."%20"..exploded[2].."%22%20OR%20title%3A%22"..exploded[1].."%20"..exploded[2].."%22%20OR%20description%3A%22"..exploded[1].."%20"..exploded[2].."%22)"

return "[" .. urlhead .. media .. search .. " " .. byabout .. " " .. dname .. "] " .. tagline

end

-- Three words search string. This is at its near-maximum length for Internet Archive to handle. if count == 3 then

local firstinitial = mw.ustring.sub(exploded[1], 1, 1) local middleinitial = mw.ustring.sub(exploded[2], 1, 1)

local search1 = "(subject%3A%22"..exploded[3].."%2C%20"..exploded[1].."%20"..exploded[2].."%2C%20"..birth.."-"..death.."%22%20OR%20subject%3A%22"..exploded[3].."%2C%20"..firstinitial.."%2E%20"..middleinitial.."%2E%20%28"..exploded[1].."%20"..exploded[2].."%29%2C%20"..birth.."-"..death.."%22%20OR%20subject%3A%22"..exploded[3].."%2C%20"..exploded[1].."%20"..middleinitial.."%2E%20%28"..exploded[1].."%20"..exploded[2].."%29%2C%20"..birth.."-"..death.."%22%20OR%20subject%3A%22"..exploded[3].."%2C%20"..exploded[1].."%20"..exploded[2].."%22%20OR%20subject%3A%22"..exploded[3].."%2C%20"..firstinitial.."%2E%20"..middleinitial.."%2E%20%28"..exploded[1].."%20"..exploded[2].."%29%22%20OR%20subject%3A%22"..exploded[3].."%2C%20"..exploded[1].."%20"..middleinitial.."%2E%20%28"..exploded[1].."%20"..exploded[2].."%29%22%20OR%20subject%3A%22"..exploded[1].."%20"..exploded[2].."%20"..exploded[3].."%22%20OR%20subject%3A%22"..exploded[1].."%20"..middleinitial.."%2E%20"..exploded[3].."%22%20OR%20subject%3A%22"..firstinitial.."%2E%20"..middleinitial.."%2E%20"..exploded[3].."%22%20OR%20creator%3A%22"..exploded[3].."%2C%20"..exploded[1].."%20"..exploded[2].."%2C%20"..birth.."-"..death.."%22%20OR%20creator%3A%22"..exploded[3].."%2C%20"..exploded[1].."%20"..exploded[2].."%2C%20Sir%2C%20"..birth.."-"..death.."%22%20OR%20creator%3A%22"..exploded[3].."%2C%20"..firstinitial.."%2E%20"..middleinitial.."%2E%20%28"..exploded[1].."%20"..exploded[2].."%29%2C%20"..birth.."-"..death.."%22"

local search2 = "%20OR%20creator%3A%22"..exploded[3].."%2C%20"..exploded[1].."%20"..middleinitial.."%2E%20%28"..exploded[1].."%20"..exploded[2].."%29%2C%20"..birth.."-"..death.."%22%20OR%20creator%3A%22"..exploded[3].."%2C%20"..exploded[1].."%20"..exploded[2].."%22%20OR%20creator%3A%22"..exploded[3].."%2C%20"..firstinitial.."%2E%20"..middleinitial.."%2E%20%28"..exploded[1].."%20"..exploded[2].."%29%22%20OR%20creator%3A%22"..exploded[3].."%2C%20"..exploded[1].."%20"..middleinitial.."%2E%20%28"..exploded[1].."%20"..exploded[2].."%29%22%20OR%20creator%3A%22"..exploded[1].."%20"..exploded[2].."%20"..exploded[3].."%22%20OR%20creator%3A%22"..exploded[1].."%20"..middleinitial.."%2E%20"..exploded[3].."%22%20OR%20creator%3A%22"..firstinitial.."%2E%20"..middleinitial.."%2E%20"..exploded[3].."%22%20OR%20title%3A%22"..exploded[1].."%20"..exploded[2].."%20"..exploded[3].."%22%20OR%20title%3A%22"..exploded[1].."%20"..middleinitial.."%2E%20"..exploded[3].."%22%20OR%20title%3A%22"..firstinitial.."%2E%20"..middleinitial.."%2E%20"..exploded[3].."%22%20OR%20description%3A%22"..exploded[1].."%20"..exploded[2].."%20"..exploded[3].."%22%20OR%20description%3A%22"..exploded[1].."%20"..middleinitial.."%2E%20"..exploded[3].."%22%20OR%20description%3A%22"..firstinitial.."%2E%20"..middleinitial.."%2E%20"..exploded[3].."%22%20OR%20description%3A%22"..exploded[3].."%2C%20"..exploded[1].."%20"..exploded[2].."%22%20OR%20description%3A%22"..exploded[3].."%2C%20"..firstinitial.."%2E%20"..middleinitial.."%2E%20%28"..exploded[1].."%20"..exploded[2].."%29%22%20OR%20description%3A%22"..exploded[3].."%2C%20"..exploded[1].."%20"..middleinitial.."%2E%20%28"..exploded[1].."%20"..exploded[2].."%29%22)"

return "[" .. urlhead .. media .. search1 .. search2 .. " " .. byabout .. " " .. dname .. "] " .. tagline

end

-- Four words search string if count == 4 then

local search = "(subject%3A%22"..exploded[4].."%2C%20"..exploded[1].."%20"..exploded[2].."%20"..exploded[3].."%2C%20"..birth.."-"..death.."%22%20OR%20creator%3A%22"..exploded[4].."%2C%20"..exploded[1].."%20"..exploded[2].."%20"..exploded[3].."%2C%20"..birth.."-"..death.."%22%20OR%20creator%3A"..exploded[1].."%20"..exploded[2].."%20"..exploded[3].."%20"..exploded[4]..")"

return "[" .. urlhead .. media .. search .. " " .. byabout .. " " .. dname .. "] " .. tagline end

end return "Unknown error (1). Please check documentation for Template:"..tname..""

end

--- URL-encode a string --- http://lua-users.org/wiki/StringRecipes --- function 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

return p