Jump to content

Module:Find sources/sandbox

From Wikipedia, the free encyclopedia
This is an old revision of this page, as edited by Danski454 (talk | contribs) at 15:13, 13 September 2021 (fixed temporary testing code). The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.
-- This module implements {{find sources}} and other similar templates, and
-- also provides a mechanism to easily create new source-finding templates.

-- Define constants
local ROOT_PAGE = 'Module:Find sources'
local TEMPLATE_ROOT = ROOT_PAGE .. '/templates/' -- for template config modules
local LINK_ROOT = ROOT_PAGE .. '/links/' -- for link config modules
local CONFIG_PAGE = ROOT_PAGE .. '/config' -- for global config

-- Load required modules
local checkType = require('libraryUtil').checkType
local cfg = mw.loadData(CONFIG_PAGE)

local p = {}

local function maybeLoadData(page)
	local success, data = pcall(mw.loadData, page)
	return success and data
end

local function substituteParams(msg, ...)
	return mw.message.newRawMessage(msg, ...):plain()
end

local function renderSearchString(searchTerms, separator, transformFunc)
	-- This takes a table of search terms and turns it into a search string
	-- that can be used in a URL or in a display value. The transformFunc
	-- parameter can be used to transform each search term in some way (for
	-- example, URL-encoding them).
	local searchStrings = {}
	for i, s in ipairs(searchTerms) do
		searchStrings[i] = s
	end
	if transformFunc then
		for i, s in ipairs(searchStrings) do
			searchStrings[i] = transformFunc(s)
		end
	end
	return table.concat(searchStrings, separator)
end

local function getTemplates(title)
	-- This returns a set containing all templates used on a page, including
	-- duplicates and commented out transclusions, and following redirects.
	if title == nil then return {} end
	local content = title.getContent()
	if content == nil then return {} end
	local templates = {}
	for t in mw.ustring.gmatch(content, '{{%s*([^|}]-)%s*[|}]') do
		local temp = mw.title.new(t[1], 'Template')
		temp = (temp and temp.redirectTarget) or temp
		if temp then
			templates[temp.prefixedText] = true
		end
	end
	return templates
end

function p._renderLink(code, searchTerms, display)
	-- Renders the external link wikicode for one link, given the link code,
	-- a table of search terms, and an optional display value.

	-- Get link config.
	local linkCfg = maybeLoadData(LINK_ROOT .. code)
	if not linkCfg then
		error(string.format(
			"invalid link code '%s'; no link config found at [[%s]]",
			code,
			LINK_ROOT .. code
		))
	end

	-- Make URL.
	local url
	do
		local separator = linkCfg.separator or "+"
		local searchString = renderSearchString(
			searchTerms,
			separator,
			mw.uri.encode
		)
		url = substituteParams(linkCfg.url, searchString)
	end

	return string.format('[%s %s]', url, display or linkCfg.display)
end

function p._main(template, args)
	-- The main access point from Lua.
	checkType('_main', 1, template, 'string')
	checkType('_main', 2, args, 'table', true)
	args = args or {}
	local title = mw.title.getCurrentTitle()
	local searchTitle = args.title or title.subpageText

	-- Get the template config.
	if template == 'autoselect' then
		-- MOVE THIS TO /config
		local autoselectDefault = 'Find sources'
		local autoselect = {
			{
				template = 'Find video game sources',
				all = {'Template:WikiProject Video games'},
				none = {'Template:WikiProject Biography'}
			}
		}
		
		-- UNCOMMENT WHEN ABOVE MOVED TO /config
		-- local autoselect = cfg['template-autoselect']
		-- local autoselectDefault = cfg['template-autoselect-default']
		local templates = getTemplates(mw.title.new(searchTitle).talkPageTitle)
		
		for i = 1, #autoselect do
			-- Select the first template for which all templates in all and no
			-- templates in none are transclued
			local match = true
			if autoselect[i].all then
				for _, v in pairs(autoselect[i].all) do
					if not templates[v] then 
						match = false
						break
					end
				end
			end
			if autoselect[i].none then
				for _, v in pairs(autoselect[i].none) do
					if templates[v] then 
						match = false
						break
					end
				end
			end
			if match then
				template = autoselect[i].template
				break
			end
		end
		
		if template == 'autoselect' then
			-- if we haven't found a template use the default
			template = autoselectDefault
		end
	end
	local templateCfgPage = TEMPLATE_ROOT .. template
	local templateCfg = maybeLoadData(templateCfgPage)
	if not templateCfg then
		error(string.format(
			"invalid template name '%s'; no template config found at [[%s]]",
			template, templateCfgPage
		))
	end

	-- Namespace check.
	if not templateCfg.isUsedInMainspace and title.namespace == 0 then
		local formatString = '<strong class="error">%s</strong>'
		if cfg['namespace-error-category'] then
			formatString = formatString .. '[[%s:%s]]'
		end
		return string.format(
			formatString,
			cfg['namespace-error'],
			mw.site.namespaces[14].name,
			cfg['namespace-error-category']
		)
	end

	-- Get the search terms from the arguments.
	local searchTerms = {}
	for i, s in ipairs(args) do
		searchTerms[i] = s
	end
	if not searchTerms[1] then
		-- Use the current subpage name as the default search term, unless 
		-- another title is provided. If the page uses a disambiguator like
		-- "Foo (bar)", make "Foo" the first term and "bar" the second.
		local term, dab = searchTitle:match('^(.*) (%b())$')
		if dab then
			dab = dab:sub(2, -2) -- Remove parens
		end
		if term and dab then
			searchTerms[1] = term
			searchTerms[2] = dab
		else
			searchTerms[1] = searchTitle
		end
	end
	searchTerms[1] = '"' .. searchTerms[1] .. '"'

	-- Make the intro link
	local introLink
	if templateCfg.introLink then
		local code = templateCfg.introLink.code
		local display = templateCfg.introLink.display or renderSearchString(
			searchTerms,
			'&nbsp;'
		)
		introLink = p._renderLink(code, searchTerms, display)
	else
		introLink = ''
	end

	-- Make the other links
	local links = {}
	local separator = templateCfg.separator or cfg['default-separator']
	local sep = ''
	for i, t in ipairs(templateCfg.links) do
		links[i] = sep .. p._renderLink(t.code, searchTerms, t.display) ..
			(t.afterDisplay or '')
		sep = t.separator or separator
	end
	links = table.concat(links)

	-- Make the blurb.
	local blurb = substituteParams(templateCfg.blurb, introLink, links)
	local span = mw.html.create('span')
	span
		:addClass('plainlinks')
		:addClass(templateCfg.class)
		:cssText(templateCfg.style)
		:wikitext(blurb)

	return tostring(span)
end

setmetatable(p, { __index = function(t, template)
	-- The main access point from #invoke.
	-- Invocations will look like {{#invoke:Find sources|template name}},
	-- where "template name" is a subpage of [[Module:Find sources/templates]].
	local tname = template
	if tname:sub(-8) == '/sandbox' then
		-- This makes {{Find sources/sandbox|Albert Einstein}} work.
		tname = tname:sub(1, -9)
	end
	return function(frame)
		local args = require('Module:Arguments').getArgs(frame, {
			wrappers = mw.site.namespaces[10].name .. ':' .. tname,
			-- Force getting of args from parent if template = autoselect
			frameOnly = template ~= 'autoselect'
		})
		return t._main(template, args)
	end
end})

return p