Module:Gratisdata Infobox

local p = {} local GratisdataIB = require("Module:GratisdataIB")

-- Code from 'Module:No globals' local mt = getmetatable(_G) or {} function mt.__index (t, k)	if k ~= 'arg' then error('Tried to read nil global ' .. tostring(k), 2) end return nil end function mt.__newindex(t, k, v)	if k ~= 'arg' then error('Tried to write global ' .. tostring(k), 2) end rawset(t, k, v) end setmetatable(_G, mt) -- End of code from 'Module:No globals'

function p.getMID return "M" .. mw.title.getCurrentTitle.id end function p.getFilename return mw.title.getCurrentTitle.nsText .. ':' .. mw.title.getCurrentTitle.text end

function p.getP514vals(frame) local mid = frame.args[1] local prefix = frame.args[2] or '' local postfix = frame.args[3] or ' ' local text = '' local tablevals = mw.wikibase.getBestStatements( mid, 'P514') for i, v in ipairs(tablevals) do text = text .. prefix .. v.mainsnak.datavalue.value.id .. postfix end return text end

-- Get a list of all properties in the current item function p.preloadGratisdataProperties(frame) local qid = frame.args[1] or '' local proplist = '' if mw.text.trim(qid or ) ~=  then local entity = mw.wikibase.getEntity(qid) local properties = entity:getProperties for i, v in ipairs(properties) do proplist = proplist .. v .. ", "		end end if proplist == '' then proplist = 'None' end return proplist end

-- check if it is on the list, fork of GratisdataIB's checkBlacklist function function p.checkProplist(frame) local proplist = frame.args.fetchgratisdata or frame.args.fwd or "" local fieldname = frame.args.name or "" if proplist ~= "" and fieldname ~= "" then if proplist:find(fieldname .. ",") then return true else return '' end else -- one of the fields is missing: let's call that "on the list" return true end end

function p.getCombinedGratisdataTemplates(frame) local qid = frame.args[1] or '' local outputcode = '' if mw.text.trim(qid or ) ~=  then local tablevals = mw.wikibase.getBestStatements( qid, 'P583') local count = 0 local mapqid = 0 for i, v in ipairs(tablevals) do			count = count + 1 end if count < 3 then for i, v in ipairs(tablevals) do				local skip = 0 -- Skip building interiors (Q2985), maps (Q2986) and ship names (Q2987), atlantic ocean (Q2988), US state (Q2989) if v.mainsnak.datavalue.value.id == 'Q2985' or v.mainsnak.datavalue.value.id == 'Q2987' or v.mainsnak.datavalue.value.id == 'Q2986' or v.mainsnak.datavalue.value.id == 'Q2988' or v.mainsnak.datavalue.value.id == 'Q2989' then skip = 1 else local p12check = mw.wikibase.getBestStatements( v.mainsnak.datavalue.value.id, 'P12') for j, w in ipairs(p12check) do						skip = 1 end local p3check = mw.wikibase.getBestStatements( v.mainsnak.datavalue.value.id, 'P3') for j, w in ipairs(p3check) do						-- Skip countries (Q371), continents (Q2990), sovereign states (Q2991), oceans (Q2992) if w.mainsnak.datavalue.value.id == 'Q371' or w.mainsnak.datavalue.value.id == 'Q2990' or w.mainsnak.datavalue.value.id == 'Q2991' or  w.mainsnak.datavalue.value.id == 'Q2992' then skip = 1 mapqid = v.mainsnak.datavalue.value.id						end end end

if skip == 0 then local newframe = {} newframe.args = {} newframe.args[0] = '' newframe.args[1] = v.mainsnak.datavalue.value.id outputcode = outputcode .. frame:expandTemplate{ title = 'Gratisdata Infobox/core', args = { qid=v.mainsnak.datavalue.value.id, embed='Yes', conf_authoritycontrol='yes', fwd=p.preloadGratisdataProperties(newframe) } } end end end if mapqid ~= 0 then outputcode = outputcode .. frame:expandTemplate{ title = 'Gratisdata Infobox/countrymap', args={qid=mapqid}} end end return outputcode end

function p.ifThenShow(frame) if mw.text.trim(frame.args[1] or ) ~=  then return (frame.args[3] or '') .. (frame.args[1] or '') .. (frame.args[4] or '') else return (frame.args[2] or '') end end

function p.addCat(frame) if mw.text.trim(frame.args[1] or ) ~=  then return  .. frame.args[1] ..  end end

-- Given an input area, return a map zoom level to use with mw:Extension:Kartographer in. Defaults to mapzoom=15. function p.autoMapZoom(frame) local sizestr,null = frame.args[1]:gsub("%D+%.?%D+", ""):gsub(",","") local size = tonumber(sizestr) or 0 local LUT = { 5000000, 1000000, 100000, 50000, 10000, 2000, 150, 50, 19, 14, 5, 1, 0.5 } for zoom, scale in ipairs(LUT) do		if size > scale then return zoom+1 end end return 15 end

function p.formatLine(frame) local part2 = mw.text.trim(frame.args[2] or '') local returnstr = '' if part2 ~= '' then returnstr = '' .. mw.getContentLanguage:ucfirst(GratisdataIB.getLabel(newframe)) returnstr = returnstr .. ' ' .. part2 .. ' '	end return returnstr end

function p.hasValue (tab, val) for index, value in ipairs(tab) do       if value == val then return true end end

return false end

-- baseLang is a utility function that returns the base language in use -- so for example, both English (en) and British English (en-gb) return 'en' -- from https://gpcommons.miraheze.org/wiki/Module:Gratisdata2 function p.baseLang(frame) local txtlang = frame:callParserFunction( "int", "lang" ) or "" -- This deals with specific exceptions: be-tarask -> be_x_old if txtlang == "be-tarask" then return "be_x_old" end local pos = txtlang:find("-") local ret = "" if pos then ret = txtlang:sub(1, pos-1) else ret = txtlang end return ret end

function p.langDirection(frame) local lang = mw.text.trim(frame.args[1] or '') if (not mw.language.isSupportedLanguage(lang)) then lang = frame:callParserFunction( "int", "lang" ) -- get user's chosen language end return mw.getLanguage(lang):getDir end

-- convertChar returns the non-diacritic version of the supplied character. stripDiacrits replaces words with diacritical characters with their non-diacritic equivalent. strip_diacrits is available for export to other modules. stringIsLike tests two words, returning true if they only differ in diacritics, false otherwise. stringIs_like is available for export to other modules. --

local function characterMap -- table with characters with diacrits and their equivalent basic latin characters local charMap_from, charMap_to charMap_from = 'ÁÀÂÄǍĂĀÃÅĄƏĆĊĈČÇĎĐḌÐÉÈĖÊËĚĔĒẼĘẸĠĜĞĢĤĦḤİÍÌÎÏǏĬĪĨĮỊĴĶĹĿĽĻŁḶḸṂŃŇÑŅṆŊÓÒÔÖǑŎŌÕǪỌŐØŔŘŖṚṜŚŜŠŞȘṢŤŢȚṬÚÙÛÜǓŬŪŨŮŲỤŰǗǛǙǕŴÝŶŸỸȲŹŻŽ'.. 'áàâäǎăāãåąəćċĉčçďđḍðéèėêëěĕēẽęẹġĝğģĥħḥıíìîïǐĭīĩįịĵķĺŀľļłḷḹṃńňñņṇŋóòôöǒŏōõǫọőøŕřŗṛṝśŝšşșṣťţțṭúùûüǔŭūũůųụűǘǜǚǖŵýŷÿỹȳźżž' charMap_to  =  'AAAAAAAAAAACCCCCDDDDEEEEEEEEEEEGGGGHHHIIIIIIIIIIIJKLLLLLLLMNNNNNNOOOOOOOOOOOORRRRRSSSSSSTTTTUUUUUUUUUUUUUUUUWYYYYYZZZ'.. 'aaaaaaaaaaacccccddddeeeeeeeeeeegggghhhiiiiiiiiiiijklllllllmnnnnnnoooooooooooorrrrrssssssttttuuuuuuuuuuuuuuuuwyyyyyzzz' local charMap = {} for i = 1,mw.ustring.len(charMap_from) do		charMap[mw.ustring.sub(charMap_from, i, i)] = mw.ustring.sub(charMap_to, i, i)	end charMap['ß'] = 'ss' return charMap end

function p.convertChar(frame) local ch = frame.args.char or mw.text.trim(frame.args[1]) or "" local charMap = characterMap return charMap[ch] or ch end

function p.strip_diacrits(wrd) if wrd then local charMap = characterMap wrd = string.gsub(wrd, "[^\128-\191][\128-\191]*", charMap ) end return wrd end

function p.stripDiacrits(frame) return p.strip_diacrits(frame.args.word or mw.text.trim(frame.args[1])) end

function p.stringIs_like(wrd1, wrd2) return p.strip_diacrits(wrd1) == p.strip_diacrits(wrd2) end

function p.stringIsLike(frame) local wrd1 = frame.args.word1 or frame.args[1] local wrd2 = frame.args.word2 or frame.args[2] if p.strip_diacrits(wrd1) == p.strip_diacrits(wrd2) then return true else return nil end end

function p.expandhiero(frame, hiero) -- added by Jura1 -- for string values in Wikihiero syntax -- inline recommended by https://meta.wikimedia.org/wiki/Help_talk:WikiHiero_syntax#Unwanted_newlines https://en.wikipedia.org/wiki/Help:WikiHiero_syntax -- maybe not needed in all contexts return 	frame:preprocess('  ') end

local function format2rowline(cell1, cell2) -- added by Jura1 local tr = "" tr = ' ' .. cell1 .. ' '	tr = tr .. ' ' .. cell2 .. ' ' 	return tr end

local function format1rowline(trqid, cell1, cell2) -- added by Jura1 local tr = "" tr = '' .. cell1 .. ' '   tr = tr .. '' .. cell2 .. ' '								return tr end

function p.hieroP495(frame) -- added by Jura1 -- expand P495 value in ' end

return p