Jump to content
Toggle menu
  • 8 articles
  • 75 files
  • 7 users
  • 37.8K edits
Neodyland Wiki
Toggle preferences menu
Toggle personal menu
Not logged in
Your IP address will be publicly visible if you make any edits.

Module:Core: Difference between revisions

From Neodyland Wiki
No edit summary
No edit summary
Line 43: Line 43:


------------------------------------------------------------------------------
------------------------------------------------------------------------------
-- code equivalent to https://commons.wikimedia.org/wiki/Template:LangSwitch
function core.langSwitch(list,lang)
function core.langSwitch(list,lang)
local langList = mw.language.getFallbacksFor(lang)
local langList = mw.language.getFallbacksFor(lang)

Revision as of 15:34, 16 April 2020

Module documentation[ create · purge ]
This documentation is transcluded from Module:Core/doc.
--[[  
  __  __           _       _         ____               
 |  \/  | ___   __| |_   _| | ___ _ / ___|___  _ __ ___ 
 | |\/| |/ _ \ / _` | | | | |/ _ (_) |   / _ \| '__/ _ \
 | |  | | (_) | (_| | |_| | |  __/_| |__| (_) | | |  __/
 |_|  |_|\___/ \__,_|\__,_|_|\___(_)\____\___/|_|  \___|
                                                        
This module is intended as collection of core functions shared among several Lua modules 
creating infobox templates on Commons. 

Authors and maintainers:
* User:Jarekt  
]]

local core = {}

------------------------------------------------------------------------------
-- Based on frame structure create "args" table with all the input parameters.
-- All inputs are not not case-sensitive and underscored are treated the same 
-- way as speces. Input values are trimmed and empty string are converted to 
-- nils. If "lang" is not provided than we substitute user's prefered language.
function core.getArgs(frame)
	local function normalize_input_args(input_args, output_args)
		for name, value in pairs( input_args ) do 
			value = string.gsub(value, "^%s*(.-)%s*$", "%1") -- trim whitespaces from the beggining and the end of the string
			if value ~= '' then -- nuke empty strings
				if type(name)=='string' then 
					name = string.gsub( string.lower(name), ' ', '_')
				end
				output_args[name] = value
			end
		end
		return output_args
	end
	local args = {}
	args = normalize_input_args(frame:getParent().args, args)
	args = normalize_input_args(frame.args, args)
	if not (args.lang and mw.language.isSupportedLanguage(args.lang)) then 
		args.lang = frame:callParserFunction("int","lang")  -- get user's chosen language
	end
	return args
end

------------------------------------------------------------------------------
-- code equivalent to https://commons.wikimedia.org/wiki/Template:LangSwitch
function core.langSwitch(list,lang)
	local langList = mw.language.getFallbacksFor(lang)
	table.insert(langList,1,lang)
	for i,language in ipairs(langList) do
		if list[language] then
			return list[language]
		end
	end
end

------------------------------------------------------------------------------
-- Function allowing for consistent treatment of boolean-like wikitext input.
-- It works similarly to Module:Yesno
function core.yesno(val, default)
	if type(val) == 'boolean' then
		return val
	elseif type(val) == 'number' then
		if val==1 then 
			return true
		elseif val==0 then
			return false
		end
	elseif type(val) == 'string' then
	    val = mw.ustring.lower(val)  -- put in lower case
	    if val == 'no'  or val == 'n' or val == 'false' or val == '0' then
	        return false
	    elseif val == 'yes' or val == 'y' or val == 'true'  or val == '1' then
	        return true
	    end
    end
    return default
end

-------------------------------------------------------------------------------
function core.getProperty(entity, prop, outputType)
	local Output = {}
	if entity.claims and entity.claims[prop] then
		for _, statement in pairs( entity:getBestStatements( prop )) do
			if (statement.mainsnak.snaktype == "value") then 
				local val = statement.mainsnak.datavalue.value
				if val.id then 
					val = val.id
				elseif val.text then
					val = val.text
				end
				table.insert(Output, val)
			end
		end
	end
	if #Output==0 then
		return nil
	elseif outputType=='one' then
		return Output[1]
	else
		return Output
	end
end

------------------------------------------------------------------------------
local function formatMessage(dataset, key, lang)
	for _, row in pairs(mw.ext.data.get(dataset, lang).data) do
		local id, msg = unpack(row)
		if id == key then
			return mw.message.newRawMessage(msg):plain()
		end
	end
	error('Invalid message key "' .. key .. '"')
end

-------------------------------------------------------------------------------
-- Assembles the "Edit at Wikidata" pen icon and returns it as wikitext string.
-- Dependencies: Data:I18n/EditAt.tab
-------------------------------------------------------------------------------
function core.editAtWikidata(entityID, propertyID, lang)
	local msg  = formatMessage('I18n/EditAt.tab', 'EditAtWikidata', lang)
	local link = 'https://www.wikidata.org/wiki/' .. entityID .. (propertyID == "" and "" or ("#" .. propertyID))
	return " [[File:OOjs UI icon edit-ltr-progressive.svg |frameless |text-top |10px |alt="..msg.."|link="..link.."|"..msg.."]]"
end

-------------------------------------------------------------------------------
-- Assembles the "Edit at SDC" pen icon and returns it as wikitext string.
-- Dependencies: Data:I18n/EditAt.tab
-------------------------------------------------------------------------------
function core.editAtSDC(propertyID, lang)
	local msg  = formatMessage('I18n/EditAt.tab', 'EditAtSDC', lang)
	local link =  mw.title.getCurrentTitle():fullUrl() .. (propertyID == "" and "" or ("#" .. propertyID))
	return " [[File:OOjs UI icon edit-ltr-progressive.svg |frameless |text-top |10px |alt="..msg.."|link="..link.."|"..msg.."]]"
end

return core
Cookies help us deliver our services. By using our services, you agree to our use of cookies.