Module:Make Wikisource link
Appearance
![]() | This module is rated as beta, and is ready for widespread use. It is still new and should be used with some caution to ensure the results are as expected. |
![]() | This module is subject to page protection. It is a highly visible module in use by a very large number of pages, or is substituted very frequently. Because vandalism or mistakes would affect many pages, and even trivial editing might cause substantial load on the servers, it is protected from editing. |
![]() | This module depends on the following other modules: |
This module creates a link to a page at Wikisource. It is intended to be called from {{Wikisource/outer core}}; you probably should not use it anywhere else.
If the template is used in mainspace and no link at Wikidata is found, it sometimes populates Category:Wikisource templates with missing id. In the following cases, it will not populate the category:
|nocat=yes
is provided- "Year in topic" articles (e.g. 1871 in Canada)
- "Decade in topic" articles (e.g. 2020s in music)
- The article is a list (title begins with
List
) - Disambiguation page and set index articles
- Articles about species. Note that there is no all included species category, so we make use of Category:Articles with 'species' microformats to check whether the article is about a species.
If none of the above are true, the category is populated.
Usage
{{#invoke:Make Wikisource link|makeLink}}
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 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[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 = '[[: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 page.namespace == 0 and not (wikidataSitelink or yesno(args.nocat)) then
toReturn = toReturn .. '[[Category:Wikisource templates with missing id|' .. pagename .. ']]'
end
return toReturn
end
return p