Jump to content

Module:Make Wikisource link: Difference between revisions

From Wikipedia, the free encyclopedia
Content deleted Content added
save a WIP
 
save a WIP
Line 1: Line 1:
local p = {}
local p = {}
local getArgs = require('Module:Arguments').getArgs
local getArgs = require('Module:Arguments').getArgs
local yesno = require('Module:Yesno')


function p.getSourceName(frame)
function p.makeLink(frame)
local args = getArgs(frame)
local args = getArgs(frame)
local lang = args['explicit-lang-param'] or args['implicit-lang-param'] or 'en'
local lang = args['explicit-lang-param'] or args['implicit-lang-param'] or 'en'
local pagename = mw.title.getCurrentTitle().text
local linkTarget
local displayText
local toReturn
-- get the Wikidata sitelink
local wikidataSitelink = mw.wikibase.getSitelink(
wikibase.getEntityIdForCurrentPage() or '',
lang .. 'wikisource'
)
-- if we have a language parameter, we look at the second unnamed parameter for the source title
-- if we have a language parameter, we look at the second unnamed parameter for the source title
local checkIndex = args['implicit-lang-param'] and 2 or 1
local checkIndexForTarget = args['implicit-lang-param'] and 2 or 1
-- and then use the next index for display
local checkIndexForDisplay = checkIndexForTarget + 1
if args[checkIndex] then
--[[---------
Set the link target
--]]---------
if args[checkIndexForTarget] then
-- we have a source title parameter, so return that
-- we have a source title parameter, so return that
return args[checkIndex]
linkTarget = args[checkIndexForTarget]
elseif wikidataSitelink then
else
-- now we check Wikidata
-- use Wikidata
linkTarget = wikidataSitelink
-- there's probably a better way to do this
else
local wikidataSitelink = mw.wikibase.getSitelink(
-- if Wikidata returns nothing, search for the {{PAGENAME}}
wikibase.getEntityIdForCurrentPage() or '',
linkTarget = 'Special:Search/' .. pagename
lang .. 'wikisource'
-- 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
if wikidataSitelink then
displayText = pagename
return wikidataSitelink
else
-- if Wikidata returns nothing, search for the {{PAGENAME}}
return 'Special:Search/' .. mw.title.getCurrentTitle().text
end
end
end
--[[---------
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[checkIndexForDisplay] or
('' .. string.gsub(linkTarget, '^Author:', ''))
end
-- build the link
toReturn = '[[:m:s:' .. lang .. ':' .. linkTarget .. '|' .. displayText .. ']]'
-- if no Wikidata link and no nocat parameter, throw in the error category
-- we do this regardless of whether we used the Wikidata link for linking
-- because the Wikisource text should be added to Wikidata regardless
if not (wikidataSitelink or yesno(args.nocat)) then
toReturn = toReturn .. '[[Category:Wikisource templates with missing id|' .. pagename .. ']]'
end
return toReturn
end
end



Revision as of 04:01, 2 June 2025

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 pagename = mw.title.getCurrentTitle().text
	local linkTarget
	local displayText
	local toReturn
	-- get the Wikidata sitelink
	local wikidataSitelink = mw.wikibase.getSitelink(
		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[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
	
	--[[---------	
	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[checkIndexForDisplay] or 
		('' .. string.gsub(linkTarget, '^Author:', ''))
	end
	
	-- build the link
	toReturn = '[[:m:s:' .. lang .. ':' .. linkTarget .. '|' .. displayText .. ']]'
	
	-- if no Wikidata link and no nocat parameter, throw in the error category
	-- we do this regardless of whether we used the Wikidata link for linking
	-- because the Wikisource text should be added to Wikidata regardless
	if not (wikidataSitelink or yesno(args.nocat)) then
		toReturn = toReturn .. '[[Category:Wikisource templates with missing id|' .. pagename .. ']]' 
	end
	
	return toReturn
end

return p