Module:Find sources: Difference between revisions

No edit summary
Tag: Reverted
No edit summary
Tag: Manual revert
 
(2 intermediate revisions by the same user not shown)
Line 1: Line 1:
-- Module:Find sources
-- This module implements {{find sources}} and other similar templates, and
-- Simple implementation for find sources functionality
-- 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 p = {}


-- Link configurations
local function maybeLoadData(page)
local links = {
local success, data = pcall(mw.loadData, page)
news = {
return success and data
url = 'https://www.google.com/search?tbm=nws&q={{{1}}}',
end
display = 'news',
 
},
local function substituteParams(msg, ...)
newspapers = {
return mw.message.newRawMessage(msg, ...):plain()
url = 'https://www.google.com/search?tbm=nws&q={{{1}}}',
end
display = 'newspapers',
 
},
local function renderSearchString(searchTerms, separator, transformFunc)
books = {
-- This takes a table of search terms and turns it into a search string
url = 'https://www.google.com/search?tbm=bks&q={{{1}}}',
-- that can be used in a URL or in a display value. The transformFunc
display = 'books',
-- parameter can be used to transform each search term in some way (for
},
-- example, URL-encoding them).
scholar = {
local searchStrings = {}
url = 'https://scholar.google.com/scholar?q={{{1}}}',
for i, s in ipairs(searchTerms) do
display = 'scholar',
searchStrings[i] = s
},
end
jstor = {
if transformFunc then
url = 'https://www.jstor.org/action/doBasicSearch?Query={{{1}}}',
for i, s in ipairs(searchStrings) do
display = 'JSTOR',
searchStrings[i] = transformFunc(s)
},
end
}
end
return table.concat(searchStrings, separator)
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.


function p._renderLink(code, args)
-- Get link config.
local linkCfg = links[code]
local linkCfg = maybeLoadData(LINK_ROOT .. code)
if not linkCfg then
if not linkCfg then
return string.format('[%s]', code)
error(string.format(
"invalid link code '%s'; no link config found at [[%s]]",
code,
LINK_ROOT .. code
))
end
end
 
local query = table.concat(args, '+')
-- Make URL.
local url = string.gsub(linkCfg.url, '{{{1}}}', query)
local url
url = string.gsub(url, '{{{q}}}', query)
do
local separator = linkCfg.separator or "+"
return string.format('[%s %s]', url, linkCfg.display)
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
end


function p._main(template, args)
function p._main(template, args)
local templateCfgs = {
-- The main access point from Lua.
mainspace = {
checkType('_main', 1, template, 'string')
blurb = 'Find sources',
checkType('_main', 2, args, 'table', true)
introLink = nil,
args = args or {}
links = {
local title = mw.title.getCurrentTitle()
{code = 'news'},
 
{code = 'newspapers'},
-- Get the template config.
{code = 'books'},
local templateCfgPage = TEMPLATE_ROOT .. template
{code = 'scholar'},
local templateCfg = maybeLoadData(templateCfgPage)
{code = 'jstor'},
if not templateCfg then
},
error(string.format(
separator = ' • ',  -- Changed to bullet point for better visibility
"invalid template name '%s'; no template config found at [[%s]]",
},
template, templateCfgPage
}
))
local templateCfg = templateCfgs[template] or templateCfgs.mainspace
local links_output = {}
for i, linkCfg in ipairs(templateCfg.links) do
links_output[#links_output + 1] = p._renderLink(linkCfg.code, args)
end
end
return table.concat(links_output, templateCfg.separator)
end


function p.main(frame)
-- Namespace check.
local args = {}
if not templateCfg.isUsedInMainspace and title.namespace == 0 then
local parentArgs = frame:getParent().args
local formatString = '<strong class="error">%s</strong>'
if cfg['namespace-error-category'] then
-- Collect numbered arguments
formatString = formatString .. '[[%s:%s]]'
for i = 1, 20 do
if parentArgs[i] and parentArgs[i] ~= '' then
args[#args + 1] = parentArgs[i]
end
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
end
if not searchTerms[1] then
-- If no arguments, use the page name as default search term
-- Use the current subpage name as the default search term, unless
if #args == 0 then
-- another title is provided. If the page uses a disambiguator like
local title = mw.title.getCurrentTitle()
-- "Foo (bar)", make "Foo" the first term and "bar" the second.
if title and title.namespace == 0 then
local searchTitle = args.title or title.subpageText
args[1] = title.text
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
else
args[1] = 'example'
searchTerms[1] = searchTitle
end
end
end
end
searchTerms[1] = '"' .. searchTerms[1] .. '"'
local template = frame.args[1] or 'mainspace'
 
-- Make the intro link
return p._main(template, args)
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
end


-- Create the function that the template is looking for
setmetatable(p, { __index = function(t, template)
p['Find sources mainspace'] = p.main
-- 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
})
return t._main(template, args)
end
end})


return p
return p