Module:PowerNetworkCountrySheet

From OpenStreetMap Wiki
Jump to navigation Jump to search
[Create] Documentation
-- ######### Experimentation for providing PowerNetworkCountrySheet information
-- Will be deleted if not success within may 2025
-- * {{#invoke:PowerNetworkCountrySheet|population}}
-- * {{#statements:population|from=Q924}}
-- * {{#invoke:Wikibase|P1082|Q924}}

data = {
	population = {TZ = "61741120"},
}
local p = {}


function p.getLabelFromWikidata(frame)
    local entityId = frame.args[1] or "Q42"
    local lang = frame.args[2] or "fr"

    local url = "https://www.wikidata.org/w/api.php?action=wbgetentities&ids=" .. entityId ..
                "&format=json&languages=" .. lang .. "&props=labels"

    local response = http.fetch(url)
    if not response or response.status ~= 200 then
        return "Erreur d’accès à l’API Wikidata"
    end

    local data = json(response.body)
    local label = data.entities[entityId].labels[lang]
    if label then
        return label.value
    else
        return "Label non trouvé"
    end
end


function p.hello( frame )
    return "Hello, world!"
end

function p.getdata(frame)
	local entityId = frame.args[1]
	local nameproperty = frame.args[2]
	return data[nameproperty][entityId]
end

function p.population(frame)
	local entityId = frame.args[1]
	return data_population[entityId]
end

function p.population2(frame)
	--mytable = mw.wikibase.getBestStatements( 'Q924', 'P1082' )
	entity = mw.wikibase.getEntity('Q924')
	pop = entity:getAllStatements('P1082' )
	pop = mw.wikibase.getAllStatements( 'Q924', 'P1082' )[1]
	pop = mw.wikibase.getLabel('Q924')
	return pop
	---return pop
end

function p.test(frame)
   -- local entity = mw.wikibase.getEntity('Q924')
    if not entity then
        return "Entity not found in Wikidata"
    end

    local claims = entity.claims
    if not claims or not claims["P1082"] then
        return "??"
    end

    local populationClaim = claims["P1082"][1]
    if not populationClaim or not populationClaim.mainsnak or not populationClaim.mainsnak.datavalue then
        return "???"
    end

    return populationClaim.mainsnak.datavalue.value.amount or "????"
end

return p