Module:Wb

From OpenStreetMap Wiki
Jump to navigation Jump to search

-- Wikibase utility functions

require("strict")

local languages = require("Module:Languages")

local p = {}

-- get entity across languages
function p.get_item()
	local entity = mw.wikibase.getEntity()

	local title = mw.title.getCurrentTitle()
	if not entity and languages.languageFromTitle(title) ~= "en" then
		local en_title = languages.sourceTitleFromTitle(title)
		local qid = mw.wikibase.getEntityIdForTitle(en_title.fullText)
		entity = mw.wikibase.getEntity(qid)
	end

	return entity
end

function p._debug_get_item()
    local page_content = mw.title.getCurrentTitle():getContent()
    local en_page_name = page_content:match('{{Languages|([^}|]+)}}')

	local entity = p.get_item()
	if not entity then return "Data item not found! page name: " .. en_page_name end
	return entity:getId()
end

return p