local p = {}
local getArgs = require('Module:Arguments').getArgs
local yesno = require('Module:Yesno')
function p.makeLink(frame)
local args = getArgs(frame)
local lang = args['explicit_lang_param'] or args['implicit_lang_param'] or 'en'
local page = mw.title.getCurrentTitle()
local pagename = page.text
local authorPrefix = args.works and frame:expandTemplate{ title = 'Wikisource/Author', args = { lang } } or ''
local linkTarget
local displayText
local toReturn
-- get the Wikidata sitelink
local wikidataSitelink = mw.wikibase.getSitelink(
mw.wikibase.getEntityIdForCurrentPage() or '',
lang .. 'wikisource'
)
-- if we have a language parameter, we look at the second unnamed parameter for the source title
local checkIndexForTarget = args['implicit_lang_param'] and 2 or 1
-- and then use the next index for display
local checkIndexForDisplay = checkIndexForTarget + 1
--[[---------
Set the link target
--]]---------
if args['wslink'] then
linkTarget = args['wslink']
elseif args[checkIndexForTarget] then
-- we have a source title parameter, so return that
linkTarget = args[checkIndexForTarget]
elseif wikidataSitelink then
-- use Wikidata
linkTarget = wikidataSitelink
else
-- if Wikidata returns nothing, search for the {{PAGENAME}}
linkTarget = 'Special:Search/' .. pagename
-- we have no parameters and nothing at Wikidata, so we are flying blind
-- We set displayText now to avoid including the Special:Search in the display text
displayText = pagename
end
-- clear the Author: prefix for now; will add it back later if needed
-- this prevents duplicate prefixes (Author:Author:Shakespeare)
-- and avoids displayText with any author prefix
linkTarget = string.gsub(linkTarget, '^' .. authorPrefix, '')
--[[---------
Now build the displayText
--]]---------
if not displayText then
-- we did not set displayText in the above steps, so set it now
-- first we check for an explicit display text, or else we set it to be the link target
displayText = args['title'] or args[checkIndexForDisplay] or linkTarget
end
--[[---------
Now we check whether we should categorize in Category:Wikisource templates with missing id
--]]---------
-- initialize errorCategory as true
local errorCategory = true
if wikidataSitelink then
-- we do have an error category
errorCategory = false
elseif yesno(args.nocat, true) then
-- we have a |nocat parameter
errorCategory = false
elseif page.namespace ~= 0 then
-- only care about mainspace
errorCategory = false
elseif string.match(pagename, '^List') then
-- we are on a list page, and those should not have Wikisource links
errorCategory = false
else
-- Check whether this is a disambiguation page or set index article
-- we do this check last to avoid using [[WP:EXPENSIVE]] parser calls if possible
for _, cat in ipairs(page.categories) do
if cat == 'All disambiguation pages' or cat == 'All set index articles' then
errorCategory = false
break
end
end
end
-- build the link
toReturn = '[[:s:' .. lang .. ':' .. authorPrefix .. linkTarget .. '|' .. displayText .. ']]'
-- append the error category if needed
if errorCategory then
toReturn = toReturn .. '[[Category:Wikisource templates with missing id|' .. pagename .. ']]'
end
return toReturn
end
return p