Module:Tool2

From Wikidata
Jump to navigation Jump to search
Lua
CodeDiscussionLinksLink count SubpagesDocumentationTestsResultsSandboxLive code All modules

Documentation for this module may be created at Module:Tool2/doc

Code

-- Lua code that powers https://www.wikidata.org/wiki/Template:Tool2

local p = {}

-- debug with e.g. =p.main(nil, {link='http://example.com'})
function p.main (frame, debugArgs)
	local args = debugArgs or frame:getParent().args
	
	if not args.name then
		args.name = '{{{name}}}'	
	end
	
	local linkNs = mw.getCurrentFrame():callParserFunction('NAMESPACE', {args.link or ''})
	local kind
	local title
	if linkNs == 'MediaWiki' then
		kind = 'gadget'
		title = 'Gadget'
	elseif linkNs == '' then
		kind = 'external'
		title = 'External tool'
	else
		kind = 'userscript'
		title = 'Userscript'
	end
	
	local div = mw.html.create('div'):attr('class', 'tool2 tool2-' .. kind):attr('title', title)
	local heading = div:tag('strong'):attr('id', args.name):attr('class', 'tool2-title')
	if args.link and kind == 'external' then
		heading:wikitext('[' .. args.link .. ' ' .. args.name ..']')
	else
		heading:wikitext(args.name)
	end
	
	if args.item and args.item ~= '' then
		div:wikitext(' ([[' .. args.item .. ']])')
	end
	
	if args.screenshot and args.screenshot ~= '' then
		local caption = args.screenshot_caption or args.name
		div:wikitext('[[File:' .. args.screenshot .. '|thumb|' .. caption .. ']]')
	end
	
	div:tag('div'):wikitext(args.features or '')
	
	div:wikitext('\n') -- for some reason that newline is important otherwise the howtouse div can get messed up
	
	local howtouse = args.howtouse
	if howtouse == nil and kind == 'gadget' then
		howtouse = 'This script is a gadget. You can enable it in [[Special:Preferences#mw-prefsection-gadgets|your preferences]].'
	elseif howtouse == nil and kind == 'userscript' then
		local url = tostring(mw.uri.fullUrl(args.link, 'action=raw&ctype=text/javascript'))
		local js = "mw.loader.load( '" .. url .. "' ); // [[" .. args.link .. "]]"
		howtouse = 'Please write the following line into [[Special:MyPage/common.js|your common.js]]: ' .. mw.getCurrentFrame():extensionTag('source', js, {lang='js'})
	end
	
	if howtouse then
		if kind == 'gadget' then
			div:wikitext('[[File:Settings (89645) - The Noun Project.svg|16px|left|link=|alt=Gadget]] ')
		elseif kind == 'userscript' then
			div:wikitext('[[File:Edit icon (the Noun Project 30184).svg|16px|left|link=|alt=Userscript]] ')		
		end
		div:tag('div'):attr('class', 'mw-collapsible mw-collapsed'):attr('data-expandtext', 'activate'):wikitext(howtouse)
	end
	
	if args.tooltranslate then
		-- get user language
		local lang = frame:callParserFunction("int",{"lang"})
		-- translate "translate" in its language too
		local translate = frame:callParserFunction("int",{"translate"})
		-- link to tooltranslate in the user language
    	div:wikitext( "[" .. "https://tooltranslate.toolforge.org/#tool=" .. tostring(args.tooltranslate) .. "&languages=" .. lang .. "&interface_language=" .. lang
    	            .. " " .. translate .. "]")
    	div:wikitext('\n')
	end


	
	if args.author and args.author ~= '' then
		local author_txt = 'by ' .. userLink(args.author, args['!alink'])

		if args.author2 and args.author2 ~= '' then
			author_txt = author_txt .. (args.author2_sp or ' and ') .. userLink(args.author2, args['!alink2'])
		end
		div:tag('small'):wikitext(author_txt)	
	end
	
	if args.link and kind ~= 'external' then
		div:wikitext(' ')
		div:tag('small'):tag('i'):wikitext('([[' .. args.link .. '|source]])')
	end

	if args.other_versions then
			div:tag('div'):attr('class', 'mw-collapsible mw-collapsed'):attr('data-expandtext', 'Alternative Versions'):wikitext(args.other_versions)
	end

	return tostring(div)
end

function userLink(username, dont_link)
	if dont_link then
		return username
	end
	return '[[User:' .. username .. '|' .. username .. ']]'
end

return p