Module:Wiki Loves Living Heritage

Permanently protected module
From Meta, a Wikimedia project coordination wiki
Module documentation

-- This module includes little helper functions for the Wiki Loves Living Heritage pages

local p = {}

-- Any time you use {{#invoke:}}, the object passed in is a frame (representing the page using the module as well as arguments passed to the {{#invoke:}} call).

function p.wikidata_source_link(frame)
    local entity_id = frame.args[1] -- Wikidata entity ID
    local property_id = "P856" -- Wikidata property ID

    local value = mw.wikibase.getEntity(entity_id):getPropertyValue(property_id)
    local source_url = mw.wikibase.getBestStatements(entity_id, property_id)[1]:getSourceUrl()

    return string.format("[[%s|%s]]", source_url, value)
end

function p.wikipediaLink(frame)
    local entity = frame.args[1] -- Wikidata entity ID
    local lang = frame.args[2] -- Wikidata property ID
	local link
	if type(entity) == 'table' then
		link = entity:getSitelink(lang .. 'wiki')
	else
		link = mw.wikibase.getSitelink(entity, lang .. 'wiki')
	end
	if link then
		linktxt = ':' .. lang .. ':' .. link
		return string.format("[[%s|%s]]", linktxt, link)
	end
	return nil
end

function p.organizerlistsimple(frame)
    local output = ""
    -- Get the first argument passed to the {{#invoke:}} call (after the name of the module and the function name).
    local input = frame.args[1]
    -- Use 'mw.ustring' rather than 'string', since strings that are passed in are almost always Unicode text.
    for i in mw.ustring.gmatch(input, "[^,]+") do
        -- Get the result from using the {{label}} template, with i as an argument, on this page.
        current_label = frame:expandTemplate{
          title='Wiki Loves Living Heritage/Organizerlinksimple',
          args={i}
        }
        -- Add a bullet point before every label except the first.
        if output ~= "" then
            output = output .. ", "
        end
        -- Add the current label to the output.
        output = output .. current_label
    end
    return output
end

function p.organizerlist(frame)
    local output = ""
    -- Get the first argument passed to the {{#invoke:}} call (after the name of the module and the function name).
    local input = frame.args[1]
    -- Use 'mw.ustring' rather than 'string', since strings that are passed in are almost always Unicode text.
    for i in mw.ustring.gmatch(input, "[^,]+") do
        -- Get the result from using the {{label}} template, with i as an argument, on this page.
        current_label = frame:expandTemplate{
          title='Wiki Loves Living Heritage/Organizerlink',
          args={i}
        }
        -- Add a bullet point before every label except the first.
        if output ~= "" then
            output = output .. "<br />"
        end
        -- Add the current label to the output.
        output = output .. current_label
    end
    return output
end

function p.labellist(frame)
    local output = ""
    -- Get the first argument passed to the {{#invoke:}} call (after the name of the module and the function name).
    local input = frame.args[1]
    -- Use 'mw.ustring' rather than 'string', since strings that are passed in are almost always Unicode text.
    for i in mw.ustring.gmatch(input, "[^,]+") do
        -- Get the result from using the {{label}} template, with i as an argument, on this page.
        current_label = frame:expandTemplate{
          title='Wiki Loves Living Heritage/Originlink',
          args={i}
        }
        -- Add a bullet point before every label except the first.
        if output ~= "" then
            output = output .. " • "
        end
        -- Add the current label to the output.
        output = output .. current_label
    end
    return output
end

function p.inventorylist(frame)
    local output = ""
    local inventories = frame.args.inventories
    local item = frame.args.item
    local ichid = frame.args.ichid
    -- Use 'mw.ustring' rather than 'string', since strings that are passed in are almost always Unicode text.
    for i in mw.ustring.gmatch(inventories, "[^,]+") do
        current_label = frame:expandTemplate{
          title='Wiki Loves Living Heritage/Inventorylink',
	        args = {
	            item = item,
	            inventory = i,
	            ichid = ichid
	        }
        }
        output = output .. "<div class='boxrow'>" .. current_label .. "</div>"
    end
    return output
end

function p.participantlist(frame)
    local output = ""
    local arguments = {}
	local input = frame.args[1]
    for i in mw.ustring.gmatch(input, "[^,]+") do
        arguments['item'] = i
        current_label = frame:expandTemplate{
          title='Wiki Loves Living Heritage/Contact',
          args = arguments
        }
        output = output .. current_label
    end
    return output
end

-- Once you've finished adding functions to 'p', you need to return it.
return p