Module:Tool

From Wikitech

Documentation for this module may be created at Module:Tool/doc (Test casesTest results)

-- Cribbed from https://www.mediawiki.org/w/index.php?title=Module:Extension&oldid=2042804

local addr = {
	GNU = '//www.gnu.org/licenses/',
	OSI = '//opensource.org/license/',
	CC  = '//creativecommons.org/licenses/',
	Mozilla = '//www.mozilla.org/'
}
local cats = {
	GPL    = 'GPL licensed tools',
	LGPL   = 'LGPL licensed tools',
	AGPL   = 'AGPL licensed tools',
	MIT    = 'MIT licensed tools',
	ISC    = 'ISC licensed tools',
	BSD    = 'BSD licensed tools',
	MPL    = 'MPL licensed tools',
	WTFPL  = 'WTFPL licensed tools',
	Apache = 'Apache licensed tools',
	PD     = 'Public domain licensed tools',
	CC     = 'Creative Commons licensed tools',
	ECL    = 'Educational Community licensed tools',
	EFL    = 'EFL licensed tools',
}
local licenses = {
	['GPL-2.0']      = { '[' .. addr.GNU .. 'gpl-2.0.html GNU General Public License 2.0]', 'GPL' },
	['GPL-2.0+']     = { '[' .. addr.GNU .. 'gpl-2.0.html GNU General Public License 2.0] or later', 'GPL' },
	['GPL-3.0']      = { '[' .. addr.GNU .. 'gpl-3.0.html GNU General Public License 3.0]', 'GPL' },
	['GPL-3.0+']     = { '[' .. addr.GNU .. 'gpl-3.0.html GNU General Public License 3.0] or later', 'GPL' },
	['AGPL-3.0']     = { '[' .. addr.GNU .. 'agpl-3.0.html GNU Affero General Public License 3.0]', 'AGPL' },
	['AGPL-3.0+']    = { '[' .. addr.GNU .. 'agpl-3.0.html GNU Affero General Public License 3.0] or later', 'AGPL' },
	['LGPL-2.1']     = { '[' .. addr.GNU .. 'lgpl-2.1.html GNU Lesser General Public License 2.1]', 'LGPL' },
	['LGPL-2.1+']    = { '[' .. addr.GNU .. 'lgpl-2.1.html GNU Lesser General Public License 2.1] or later', 'LGPL' },
	['LGPL-3.0']     = { '[' .. addr.GNU .. 'lgpl-3.0.html GNU Lesser General Public License 3.0]', 'LGPL' },
	['LGPL-3.0+']    = { '[' .. addr.GNU .. 'lgpl-3.0.html GNU Lesser General Public License 3.0] or later', 'LGPL' },
	['FDL']          = { '[' .. addr.GNU .. 'fdl.html GNU Free Documentation License]' },
	['MIT']          = { '[' .. addr.OSI .. 'mit MIT License]', 'MIT' },
	['ISC']          = { '[' .. addr.OSI .. 'ISC ISC License]', 'ISC' },
	['BSD-2-Clause'] = { '[' .. addr.OSI .. 'BSD-2-Clause BSD 2-clause "Simplified" License]', 'BSD' },
	['BSD-3-Clause'] = { '[' .. addr.OSI .. 'BSD-3-Clause BSD 3-clause "Modified" License]', 'BSD' },
	['BSD-4-Clause'] = { '[' .. addr.GNU .. 'license-list.html#OriginalBSD BSD-4-Clause BSD 4-clause "Original" License]', 'BSD' },
	['MPL-1.0']      = { '[' .. addr.Mozilla .. 'MPL/1.0/ Mozilla Public License 1.0]', 'MPL' },
	['MPL-2.0']      = { '[' .. addr.Mozilla .. 'MPL/2.0/ Mozilla Public License 2.0]', 'MPL' },
	['WTFPL']        = { '[http://www.wtfpl.net WTFPL 2.0]', 'WTFPL' },
	['Apache-2.0']   = { '[http://www.apache.org/licenses/LICENSE-2.0 Apache License 2.0]', 'Apache' },
	['Zlib']         = { '[' .. addr.OSI .. 'Zlib zlib License]' },
	['CC0-1.0']      = { '[//creativecommons.org/publicdomain/zero/1.0/ Creative Commons Zero v1.0 Universal]' },
	['CC-BY-3.0']    = { '[' .. addr.CC .. 'by/3.0/ Creative Commons Attribution 3.0]', 'CC' },
	['CC-BY-SA-2.0'] = { '[' .. addr.CC .. 'by-sa/2.0/ Creative Commons Attribution Share Alike 2.0]', 'CC' },
	['CC-BY-SA-3.0'] = { '[' .. addr.CC .. 'by-sa/3.0/ Creative Commons Attribution Share Alike 3.0]', 'CC' },
	['CC-BY-SA-4.0'] = { '[' .. addr.CC .. 'by-sa/4.0/ Creative Commons Attribution Share Alike 4.0]', 'CC' },
	['ECL-2.0']      = { '[[wikipedia:Educational Community License|Educational Community License 2.0]]', 'ECL' },
	['PD']			 = { '[[wikipedia:Public domain|Public domain]]', 'PD' },
	['EFL-2.0']      = { '[' .. addr.OSI .. 'EFL-2.0 Eiffel Forum License, Version 2]', 'EFL' },
	['Unlicense']    = { '[https://unlicense.org/ The Unlicense]', 'PD' },
}

local function cat( title )
	return '[[Category:' .. title .. ']]'
end

local function getLicenseCategory( str )
	local cnf = licenses[str]
	if cnf then
		if #cnf > 1 then
			return cat( cats[cnf[2]] )
		end
	else
		return cat( 'Tools with unknown license' )
	end
end

local function getFormattedLicense( str )
	local cnf = licenses[str]
	if cnf then
		return cnf[1]
	else
		return str
	end
end

local p = {}

function p.getLicenseCategory( frame )
	return getLicenseCategory( frame.args[1] )
end

function p.getFormattedLicense( frame )
	return getFormattedLicense( frame.args[1] )
end

function p.toolinfojson( frame )
	local args = frame:getParent().args
	local ret = {
		name = args["name"],
		description = args["description"],
		url = args["website"],
		keywords = args["keywords"],
		author = args["author"],
		repository = args["repository"] -- this does not map well but let's see what happens
	}
	
	return mw.text.jsonEncode( ret )
end

function p.gettoolinfojson( frame )
	local args = frame.args
	local tool = mw.title.makeTitle( 'Tool', args[1])
	local parsed = frame:preprocess( tool:getContent() )
	
	return string.match(parsed, '<div id="toolinfo" style="display: none;">(.-)</div>')
end	

return p