Przejdź do zawartości

Moduł:Wikibase

Z Wikiźródeł, wolnej biblioteki

Generuje kod dla Szablonu {{Autorinfo}}.


local p = {}

function fdata(data)
		data = string.gsub( data, 'sty', 'stycznia' )
		data = string.gsub( data, 'lut', 'lutego' )
		data = string.gsub( data, 'mar', 'marca' )
		data = string.gsub( data, 'kwi', 'kwietnia' )
		data = string.gsub( data, 'maj', 'maja' )
		data = string.gsub( data, 'cze', 'czerwca' )
		data = string.gsub( data, 'lip', 'lipca' )
		data = string.gsub( data, 'sie', 'sierpnia' )
		data = string.gsub( data, 'wrz', 'września' )
		data = string.gsub( data, 'paź', 'października' )
		data = string.gsub( data, 'lis', 'listopada' )
		data = string.gsub( data, 'gru', 'grudnia' )
		return data
end

-- Returns the time value for the given property (conw. to author date) [beta]
function p.authordprop(frame)
	local item = mw.wikibase.getEntityObject()
	local lang = mw.language.new('pl')
	local julian = 0
	if not item then
		return ""
	end
	local statements = item:getBestStatements(frame.args.property)
	local start, leng, date_s
	local date_str = {}
	for i, statement in ipairs(statements) do
		if statement.mainsnak.datatype == 'time' and statement.mainsnak.datavalue~=nil then
			start, leng, date_s = statement.mainsnak.datavalue.value.time:find('([%+%-][%d-]+).*')
			date_str[i] = {}
			date_str[i]['t'] = date_s
			date_str[i]['p'] = statement.mainsnak.datavalue.value.precision
			date_str[i]['m'] = statement.mainsnak.datavalue.value.calendarmodel
		end
	end
	table.sort( date_str, function(a,b) return tonumber(a['t']:sub(1, 1) .. a['t']:sub(2):gsub('-', '' )) < tonumber(b['t']:sub(1, 1) .. b['t']:sub(2):gsub('-', '' )) end )

--statement.mainsnak.datavalue.value.precision: 7 - century, 8 - decade, 9 - year, 10 - month, 11 - day, 12 - hour, 13 - minute, 14 - second.	
	if #date_str > 0 then
		date_s = ''
		for i, date_frm in ipairs(date_str) do
			if string.match( date_frm['m'], 'Q1985786' ) ~= nil then julian = 1 end
			if date_frm['p'] > 6 then
				if date_frm['p'] == 7 then
					date_s = date_s .. (date_frm['t']:sub(2,3)+1) .. ' w.'
				elseif date_frm['p'] == 8 then
					date_s = date_s .. 'lata ' .. lang:formatDate( 'Y', date_frm['t']:sub(2,4) .. '0') .. '-te'
				elseif date_frm['p'] == 9 then
					date_s = date_s .. lang:formatDate( 'Y', date_frm['t']:sub(2,5))
				elseif date_frm['p'] == 10 then
					date_s = date_s .. lang:formatDate( 'F Y', date_frm['t']:sub(2,8))
				elseif date_frm['p'] > 10 then
					if i < #date_str then
						if date_frm['t']:sub(1,8) == date_str[i+1]['t']:sub(1,8) then
							date_s = date_s .. lang:formatDate( 'j', date_frm['t']:sub(2) )
						elseif date_frm['t']:sub(1,5) == date_str[i+1]['t']:sub(1,5) then
							date_s = date_s .. lang:formatDate( 'j xg', date_frm['t']:sub(2) )
						else
							date_s = date_s .. lang:formatDate( 'j xg Y', date_frm['t']:sub(2) )
						end
					else
						date_s = date_s .. lang:formatDate( 'j xg Y', date_frm['t']:sub(2) )
					end
				end
				date_s = date_s  .. (date_frm['t']:sub(1, 1) == '-' and ' p.n.e.' or '')
			end
			date_s =  date_s  .. ' lub '
		end
		date_s = (julian == 1 and '[[Kategoria:Powiązane z Wikidata z datami kalendarza juliańskiego]]' or '') .. date_s
		date_s = string.gsub(string.gsub(date_s, '([%D])0+', '%1' ), '^0+', '')
		return string.sub( date_s, 1, -6 )
	end

	return ''
end

-- Returns the value for the given property (conw. to date)
function p.dprop(frame)
	local item = mw.wikibase.getEntityObject()
	if not item then
		return ""
	end

	date = item:formatPropertyValues(frame.args.property).value
	if date ~= '' then
		return fdata(date)
	end
	return ''
end

-- Returns the value for the given property
function p.prop(frame)
	local item = mw.wikibase.getEntityObject()
	if not item then
		return ""
	end

	prop = item:formatPropertyValues(frame.args.property).value
	if prop ~= '' then
		return prop
	end
	return ''
end

-- Returns the first value for the given property (if many)
-- in some cases illustrations, more than one value returned
-- corrupts the templates.
function p.uprop(frame)
	local item = mw.wikibase.getEntityObject()
	if not item then
		return ""
	end
	prop = item:formatPropertyValues(frame.args.property).value
	local pl = string.len(prop)
	local pos = pl
	if prop ~= '' then
		pos = math.min((string.find (prop, '%.[Pp][Nn][Gg], ') or pl) + 3, (string.find (prop, '%.[Jj][Pp][Gg], ') or pl) + 3, (string.find (prop, '%.[Jj][Pp][Ee][Gg], ') or pl) + 4, (string.find (prop, '%.[Gg][Ii][Ff], ') or pl) + 3, (string.find (prop, '%.[Tt][Ii][Ff], ') or pl) + 3, (string.find (prop, '%.[Tt][Ii][Ff][Ff], ') or pl) + 4)
		if pos < pl then
			return prop:sub(1, pos)
		else
			return prop
		end
	end
	return ''
end

-- Returns the value for the given 'place of' property (datatype -> item);
function p.placeprop(frame)
	local item = mw.wikibase.getEntityObject()
	if not item then
		return ""
	end
	local statements = item:getBestStatements(frame.args.property)
	local labels = {}
	for _, statement in ipairs(statements) do
		if statement.mainsnak.datatype == 'wikibase-item' and statement.mainsnak.datavalue then
			table.insert( labels, mw.wikibase.label('Q' .. statement.mainsnak.datavalue.value['numeric-id']))
		end
	end
	if #labels > 0 then
		return table.concat( labels, ' lub ' )
	end	
	return ''
end

-- Returns the native language item ID of the item linked to the current page.
function p.langid(frame)
	local item = mw.wikibase.getEntityObject()

	if not item then
		return ''
	end
	if not item or not item.claims or not item.claims['P103'] then
		return ''
	end
 
	for _, claim in pairs(item.claims['P103']) do
		if claim.mainsnak.snaktype == 'value' then
			if claim.mainsnak.datavalue.type == 'wikibase-entityid' then
				if claim.mainsnak.datavalue.value['entity-type'] == 'item' then
					local lid = claim.mainsnak.datavalue.value['numeric-id']
					return lid
				end
			end
		end
	end 
	return ''

end

-- Returns the item ID of the item linked to the current page.
function p.id(frame)
	local item = mw.wikibase.getEntityObject()

	if not item then
		return ""
	end
 
	return item.id
end

-- Returns a sitelink of a given data item.
-- @param string|nil id the item id (default: the item linked to the current page)
-- @param string|nil site the site (default: the current site)
function p.sitelink(frame)
	if not frame.args.id then
		local item = mw.wikibase.getEntityObject()
		if not item then
			return ""
		end

		return item:getSitelink(frame.args.site or nil)
	end

	return wikibase.sitelink(frame.args.id)
end

-- Returns a link to Commons, with fallback to P373 (commons cat) for the current page
function p.commonsLinkOfLinkedPage(frame)
	local item = mw.wikibase.getEntityObject()
	if not item then
		return ""
	end

	local link = item:getSitelink('commonswiki')
	if link then
		return link
	end

	link = item:formatPropertyValues('P373').value
	if link ~= '' then
		return 'Category:' .. link
	end

	return ''
end

-- Returns a description for the current page
function p.description(frame)
	local item = mw.wikibase.getEntityObject()

	if not item or not item.descriptions or not item.descriptions.fr then
		return ""
	end
 
	return item.descriptions.fr.value
end

-- Returns the value as year for the given property
function p.year(frame)
	local item = mw.wikibase.getEntityObject()

	if not item or not item.claims or not item.claims[frame.args.property] then
		return 0
	end
 
	for _, claim in pairs(claims) do
		if claim.mainsnak.snaktype == 'value' then
			if claim.mainsnak.datavalue.type == 'time' then
				local beginMatch, endMatch, year = claim.mainsnak.datavalue.value.time:find('([%+%-][%d]+).*')
				return year
			end
		end
	end

	return item.descriptions.fr.value
end

return p