Module:Make Wikisource link and Module:Make Wikisource link/sandbox: Difference between pages

(Difference between pages)
Jump to navigation Jump to search
Page 1
Page 2
imported>HouseBlaster
handle searching in authorspace correctly (by not searching in authorspace at all)
 
imported>HouseBlaster
sync with main
 
Line 4: Line 4:


function p.makeLink(frame)
function p.makeLink(frame)
--[[---------
Variable declaration
--]]---------
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 page = mw.title.getCurrentTitle()
local page = mw.title.getCurrentTitle()
local pagename = page.text
local pagename = page.text
local wikisourceEdition
local authorPrefix = args.works and frame:expandTemplate{ title = 'Wikisource/Author', args = { lang } } or ''
local linkTarget
local linkTarget
local displayText
local displayText
local toReturn
local toReturn
local prefix
--[[
Create the prefix for the interwiki link
It uses the lowest of:
1. 'Special:Search/', if using the enwiki pagename as the interwiki target
2. If not (1), an 'Author:' prefix, localized, if given a works parameter and not under (1)
3. If not (1) or (2), the empty string
We avoid 'Special:Search/Author:' intentionally; this is rarely helpful
Here we simply set it to (2) or (3), and override it when setting the link target if needed
--]]
prefix = args.works and frame:expandTemplate{ title = 'Wikisource/Author', args = { lang } } or ''
-- Handle multilingual Wikisource (aka old Wikisource) correctly
if lang == 'mul' then
wikisourceEdition = 'sourceswiki' -- siteid for multilingual Wikisource
else
wikisourceEdition = lang .. 'wikisource' -- other Wikisource editions are langcode + 'wikisource' (e.g. 'enwikisource')
end
-- get the Wikidata sitelink
-- get the Wikidata sitelink
local wikidataSitelink = mw.wikibase.getSitelink(
local wikidataSitelink = mw.wikibase.getSitelink(
mw.wikibase.getEntityIdForCurrentPage() or '',
mw.wikibase.getEntityIdForCurrentPage() or '',
wikisourceEdition
lang .. 'wikisource'
)
)
Line 58: Line 36:
linkTarget = wikidataSitelink
linkTarget = wikidataSitelink
else
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 have no parameters and nothing at Wikidata, so we are flying blind
-- set the linkTarget to the pagename
-- We set displayText now to avoid including the Special:Search in the display text
linkTarget = pagename
displayText = pagename
-- and set prefix to 'Special:Search/' to find the appropriate link
prefix = 'Special:Search/'
end
end
-- clear prefixes for now; will add it back later if needed
-- clear the Author: prefix for now; will add it back later if needed
-- this prevents duplicate prefixes (Author:Author:Shakespeare; Special:Search/Special:Search/)
-- this prevents duplicate prefixes (Author:Author:Shakespeare)
-- and avoids displayText with any author prefix
-- and avoids displayText with any author prefix
linkTarget = string.gsub(linkTarget, '^' .. prefix, '')
linkTarget = string.gsub(linkTarget, '^' .. authorPrefix, '')
--[[---------
--[[---------
Line 99: Line 77:
-- only care about mainspace
-- only care about mainspace
setFalse()
setFalse()
elseif string.match(pagename, '^Lists? of ') then
elseif string.match(pagename, '^List') then
-- we are on a list page, and those should not have Wikisource links
-- we are on a list page, and those should not have Wikisource links
setFalse()
setFalse()
Line 121: Line 99:
-- build the link
-- build the link
toReturn = '[[:s:' .. lang .. ':' .. prefix .. linkTarget .. '|' .. displayText .. ']]'
toReturn = '[[:s:' .. lang .. ':' .. authorPrefix .. linkTarget .. '|' .. displayText .. ']]'
-- append the error category if needed
-- append the error category if needed