ሞድዩል:Wikidata/Formats
Appearance
Documentation for this module may be created at ሞድዩል:Wikidata/Formats/doc
local p = {}
local ti = mw.language.new('ti')
local tablesModule = require('Module:Tables')
local tableElement = tablesModule.element
local onTable = tablesModule.on
local insertInTable = tablesModule.put
local wikidataModule = require('Module:Wikidata')
local getWikidataProperty = wikidataModule.getProperty
local wikidataDataFormat = wikidataModule.dataFormat
local toLinkURL = require('Module:URL').toLink
function p.formatCoor(value)
local coorstring = value
return frame:preprocess('{{coord|' .. coorstring[tostring('latitude')] ..'|'.. coorstring[tostring('longitude')] .. '|format=dms}}')
end
function p.unitFormat(value, options)
amount = mw.ustring.gsub(value.value['amount'], '+','')
unit = mw.wikibase.label(mw.ustring.gsub(value.value['unit'], '^.-/(Q%d+)$', '%1'))
if unit and tonumber(amount) > 1 then
textUnit = mw.ustring.gsub(unit, '(%S+)', plural)
end
if not unit or (options['unitFormat'] and options['unitFormat'] == 'ቁጽሪ') then
return tonumber(amount)
elseif unit and options['unitFormat'] and options['unitFormat'] == 'ደቓይቕ' and unit == 'ካልኢት' then
local seconds = tostring(tonumber(amount)%60)
if mw.ustring.len(seconds) == 1 then seconds = '0' .. seconds end
return math.floor(tonumber(amount)/60) .. ':' .. seconds .. ' ' .. 'ደቓይቕ'
else
return p.numberFormat(amount) .. ((textUnit and ' ' .. textUnit) or '')
end
end
function p.numberFormat(value, options)
if value['amount'] then value = mw.ustring.gsub(value['amount'], '+', '') end
if not tonumber(value) then return value end
local Format = value
while true do
Format, k = string.gsub(Format, "^(-?%d+)(%d%d%d)", '%1 %2')
if (k==0) then
break
end
end
return Format
end
-- Qualifier formats
function p.periodFormat(value, options)
if not value then
return
end
local startYear, endYear
local startDate, endDate
local dateOptions= {['textFormat']='uppercase', ['dateFormat'] = 'year', ['link']='no'}
local startDates = value['P580']
local endDates = value['P582']
local istartDate = 1
local iendDate = 1
if not startDates and not endDates then
return
end
local periods = {}
if startDates and startDates[1] then
--if true then return require('Module:Tables').tostring(startDates[1]) end
startYear = formatDataWikidata(startDates[1],dateOptions , {})
startDate= startYear -- for now
end
if endDates and endDates[1] then
endYear = formatDataWikidata(endDates[1], dateOptions, {})
endDate = endYear -- for now
end
while startDate or endDate do -- For each period
if startDate and endDate and tonumber(startDate) and tonumber(endDate) and (tonumber(startDate) <= tonumber(endDate)) then
if startYear == endYear then
table.insert(periods, startYear)
else
table.insert(periods, startYear .. '-' .. endYear)
end
istartDate = istartDate + 1
iendDate = iendDate + 1
if startDates[istartDate] then
startYear = formatDataWikidata(startDates[istartDate],dateOptions , {})
startDate= startYear -- for now
else
startYear = nil
startDate= nil
end
if endDates[iendDate] then
endYear = formatDataWikidata(endDates[iendDate],dateOptions , {})
endDate= endYear -- for now
else
endYear = nil
endDate= nil
end
elseif startYear then
table.insert(periods, 'ካብ ' .. startYear)
istartDate = istartDate + 1
if startDates[istartDate] then
startYear = formatDataWikidata(startDates[istartDate],dateOptions , {})
startDate= startYear -- for now
else
startYear = nil
startDate= nil
end
elseif endYear then
table.insert(periods, 'ክሳብ ' .. endYear)
iendDate = iendDate + 1
if endDates[iendDate] then
endYear = formatDataWikidata(endDates[iendDate],dateOptions , {})
endDate= endYear -- for now
else
endYear = nil
endDate= nil
end
end
end
return table.concat(periods, '፣ ')
end
-- Internal functions
-- The following functions do much the same as the function
-- entityIdFormat of the Wikidata module.
-- p.getData fetches the entity data and p.toLink formats it.
function p.getData(value)
if value and value['numeric-id'] then
local idEntity = 'Q' .. value['numeric-id']
local label = mw.wikibase.label( idEntity )
local link = mw.wikibase.sitelink( idEntity )
return link, label, idEntity
end
end
function p.linkInOtherLanguage(link, label, language)
local category = '[[መደብ:ዊኪፐድያ:Pages with Wikidata properties tagged in another language]]'
if link and label then
return label .. ' <small>([[:' .. language .. ':' .. link .. '|' .. language .. ']])</small>' .. category
elseif label then
return label .. ' <small>(' .. language .. ')</small>' .. category
elseif link then
return link .. ' <small>([[:' .. language .. ':' .. link .. '|' .. language .. ']])</small>' .. category
end
end
local alreadyLinked = {}
function p.toLink(link, label, idEntity, options)
if not link and not label and not idEntity then
return
end
if options then
-- Label options
if options.label and options.label ~= 'null' then -- To find out where it is used.
label = options.label
end
--if options['nameSpace'] then link = options['nameSpace'] .. ':' .. link end
-- Convert the first character to uppercase in his case
if label and options['uppercase'] == 'yes' then
label = ti:ucfirst(label)
end
-- Link options
if options['link'] == 'no' then
-- No p.toLink
link = nil
elseif options['itMustExist'] == 'yes' then
-- Do not return anything if the Wikipedia article is not informed
if not link then
return
end
elseif options['link'] == 'yes' then
if not link and label and not require('Module:Page').exists(label) then
-- Take the label as a link if the corresponding article does not exist
link = label
end
end
end
-- Do not link the same thing several times
if link and alreadyLinked[link] then
link = nil
end
-- As reported by the label or link
local result
if label and link and label ~= link then --Both if they are not the same
result = '[[' .. link .. '|' .. label .. ']]'
alreadyLinked[link] = true
elseif link then -- Just the link or ambps and they are the same
result = '[[' .. link .. ']]'
alreadyLinked[link] = true
elseif label then -- Only the label
result = label
elseif idEntity then -- Only the entity
-- Get the Wikidata tag and link from the English, French or German Wikipedia
local entity=mw.wikibase.getEntityObject(idEntity)
--if true then return require('Module:Tables').tostring(entity) end
if entity then
local language
for k, language in pairs({'en', 'fr', 'it', 'de'}) do
label = tableElement(entity, 'labels', language, 'value')
link = tableElement(entity, 'sitelinks', language .. 'wiki', 'title')
if label or link then
result = p.linkInOtherLanguage(link, label, language)
break
end
end
end
if not result then
return '[[:d:'.. idEntity .. '|unlabeled]]' ..
'[[መደብ:ዊኪፐድያ:Pages with unlabeled Wikidata properties]]'
end
end
-- Add italics
if options and options.italics == 'yes' then
result = "''" .. result .. "''"
end
return result
end
function p.formatId( value, options )
local wikidata = require('Module:Wikidata')
local id = 'Q'.. value['numeric-id']
local options = {separator = '<br>',property = options.propertyValue, one = 'yes', entityId = id, textFormat = 'uppercase'}
return wikidata.getProperty(options)
end
function p.formatFlagcountry( value, options )
local flag = require('Module:Flag')
local id = 'Q'.. value['numeric-id']
local label = mw.wikibase.label( 'Q' .. value['numeric-id'] ) or ''
local kind = mw.ustring.gsub(label,'(%D+)%sde%s%D+','%1')
if options.cut and options.cut ~= '' then
coincidence = mw.ustring.find( es:lc(options.cut), es:lc(kind), plain )
end
if coincidence then
name = mw.ustring.gsub(label,'%D+%sde%s(%D+)','%1')
elseif label and label ~= '' then
name = label
end
return flag.luaFlag(id, '20px', '', name)
end
function p.formatInfobox( value, options )
local infobox = require('Module:Infobox')
local options = {propertyValue='P31'}
local label = p.formatId( value, options)
options.cut = label
local parameters = {
child = 'yes',
labelstyle1 = 'border:0;font-weight:100;padding:0px 7px',
datastyle1 = 'border:0;font-weight:100;padding:0px 7px 0px 2px',
label1='• '.. ti:ucfirst(label),
data1 = p.formatFlagcountry(value, options)}
return infobox.infobox(parameters)
end
function p.idFormat ( value, options )
return value['numeric-id'] and 'Q' .. value['numeric-id']
end
--*****************************************************************************
-- Functions for URL data types
--*****************************************************************************
function p.URLFormat(value, options, mount)
return toLinkURL(value)
end
function p.twitterFormat(value, options, mount)
return '[https://twitter.com/' .. value .. ' ' .. value .. ']'
end
function p.facebookFormat(value, options, mount)
return '[https://www.facebook.com/' .. value .. ' ' .. value .. ']'
end
--*****************************************************************************
-- Format aliases to use in template property
--*****************************************************************************
p['flag'] = p['formatFlag']
p['facebook'] = p['facebookFormat']
p['twitter'] = p['twitterFormat']
p['url'] = p['URLFormat']
return p