Content deleted Content added
implement template auto selection Tag: Reverted |
|||
Line 38:
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
Line 75 ⟶ 92:
args = args or {}
local title = mw.title.getCurrentTitle()
-- Get the template config.
if template == 'autoselect' then
-- MOVE THIS TO /config
cfg['template-autoselect'] = {
{
template = 'Find video game sources',
all = {'Template:WikiProject Video games'},
none = {'Template:WikiProject Biography'}
}
}
cfg['template-autoselect-default'] = 'Find sources'
local autoselect = cfg['template-autoselect']
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 = cfg['template-autoselect-default']
end
end
local templateCfgPage = TEMPLATE_ROOT .. template
local templateCfg = maybeLoadData(templateCfgPage)
Line 109 ⟶ 172:
-- another title is provided. If the page uses a disambiguator like
-- "Foo (bar)", make "Foo" the first term and "bar" the second.
▲ local searchTitle = args.title or title.subpageText
local term, dab = searchTitle:match('^(.*) (%b())$')
if dab then
Line 170 ⟶ 232:
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)
|