Module:Period: Difference between revisions

From Wikimedia Commons, the free media repository
Jump to navigation Jump to search
Content deleted Content added
let empty "object" input string fail gracefully; logical simplification; use quotes consistently; set "period" as sortkey for Category:Unsupported period
m Protected "Module:Period": High-risk Lua module ([Edit=Allow only template editors and administrators] (indefinite) [Move=Allow only template editors and administrators] (indefinite))
 
(8 intermediate revisions by 2 users not shown)
Line 1: Line 1:
local wikidata_label = require('Module:Wikidata label')._getLabel
local LookupTable = mw.loadData('Module:Period/data')

local p = {}
local p = {}

function p._periodSpan(item, lang)
-- get the time span of the period given by "item"from the Wikidata values
-- for P580/P582 or P571/P576
local getDate = require('Module:Wikidata date')._date
local start = getDate(item, 'P580' , lang).str or getDate(item, 'P571' , lang).str
local stop = getDate(item, 'P582' , lang).str or getDate(item, 'P576' , lang).str
-- TODO: [[Module:Wikidata label]] has issues with RTL text when falling back
-- to some LTR text, see the example at [[Template:Period/testcases]],
-- has to get fixed there probably
local spantext = nil
if start or stop then
spantext = string.format('(%s–%s)', start or '', stop or '')
end
return spantext
end


function p._period(period, dates, lang)
function p._period(period, dates, lang)
local wikitext = ''
local wikitext = ''
local LookupTable = mw.loadData('Module:Period/data')
local item = LookupTable[period] -- try to find an item in the dictionary
local item = LookupTable[string.lower(period)]
-- try to find an item in the dictionary
if item then
if item then
-- if an item was found use it
-- if an item was found use it
local wikidata_label = require('Module:Wikidata label')._getLabel
wikitext = wikidata_label(LookupTable[period], lang)
wikitext = wikidata_label(item, lang)
-- now construct the time span if requested and append it to the wikitext string
-- now get the time span if requested and append it to the wikitext string
if dates == 'date' or dates == 'yes' then
if dates == 'date' or dates == 'yes' then
local spantext = p._periodSpan(item, lang)
-- span from P580/P582 or e.g. for Q1143524 from P571/P576
if spantext then
local getDate = require('Module:Wikidata date')._date
wikitext = wikitext .. ' ' .. spantext
local start = getDate(item, 'P580' , lang).str or getDate(item, 'P571' , lang).str
local stop = getDate(item, 'P582' , lang).str or getDate(item, 'P576' , lang).str
-- TODO: [[Module:Wikidata label]] has issues with RTL text when falling back to some LTR text,
-- see the example at [[Template:Period/testcases]],
-- has to get fixed there probably
if start or stop then
wikitext = string.format('%s (%s–%s)', wikitext, start or '', stop or '')
end
end
elseif dates and dates ~= '' then
elseif dates and dates ~= '' then
wikitext = string.format('%s (%s)', wikitext, dates)
wikitext = string.format('%s (%s)', wikitext, dates)
end
end
-- create and append non-visible language independent marking
-- in QuickStatements-like format
-- using property "time period" (P2348)
local qsText = '<div style="display: none;">era QS:P2348,' .. item .. '</div>'
wikitext = wikitext .. qsText


elseif period and period ~= '' then
elseif period and period ~= '' then
Line 37: Line 52:
function p.period(frame)
function p.period(frame)
local args = frame.args
local args = frame.args
return p._period(string.lower(args.period), args.dates, args.lang)
return p._period(args.period, args.dates, args.lang)
end
end



Latest revision as of 16:52, 18 August 2020

Lua

CodeDiscussionEditHistoryLinksLink count Subpages:DocumentationTestsResultsSandboxLive code All modules

Code behind {{Period}}. See documentation there.

Relies on:

_periodSpan() retrievs data from the Wikidata properties start time (P580)/end time (P582) and inception (P571)/dissolved, abolished or demolished date (P576) respectively.

Code

local p = {}

function p._periodSpan(item, lang)
	-- get the time span of the period given by "item"from the Wikidata values
	-- for P580/P582 or P571/P576
	local getDate = require('Module:Wikidata date')._date
	local start = getDate(item, 'P580' , lang).str or getDate(item, 'P571' , lang).str
	local stop = getDate(item, 'P582' , lang).str or getDate(item, 'P576' , lang).str 
	-- TODO: [[Module:Wikidata label]] has issues with RTL text when falling back
	-- to some LTR text, see the example at [[Template:Period/testcases]],
	-- has to get fixed there probably
	local spantext = nil
	if start or stop then
		spantext = string.format('(%s&ndash;%s)', start or '', stop or '')
	end
	return spantext
end

function p._period(period, dates, lang)
	local wikitext = ''
	local LookupTable = mw.loadData('Module:Period/data')
	local item = LookupTable[string.lower(period)]
	-- try to find an item in the dictionary
	if item then
	-- if an item was found use it
		local wikidata_label = require('Module:Wikidata label')._getLabel
		wikitext = wikidata_label(item, lang)
		-- now get the time span if requested and append it to the wikitext string
		if dates == 'date' or dates == 'yes' then
			local spantext = p._periodSpan(item, lang)
			if spantext then
				wikitext = wikitext .. ' ' .. spantext
			end
		elseif dates and dates ~= '' then
			wikitext = string.format('%s (%s)', wikitext, dates)
		end
		-- create and append non-visible language independent marking
		-- in QuickStatements-like format
		-- using property "time period" (P2348)
		local qsText = '<div style="display: none;">era QS:P2348,' .. item .. '</div>'
		wikitext = wikitext .. qsText

	elseif period and period ~= '' then
		wikitext = '[[Category:Unsupported period|' .. period .. ']]' .. period
		if dates and dates ~= '' then
			wikitext = string.format('%s (%s)', wikitext, dates)
		end
	end
	return wikitext
end

function p.period(frame)
	local args = frame.args
	return p._period(args.period, args.dates, args.lang)
end

return p