Module:Redirect hatnote

From TechInfoDepot
Jump to navigationJump to search
Documentation icon Module documentation[view] [edit] [history] [purge]

This module produces a hatnote for disambiguating a page that is linked to by a given redirect. It implements the {{redirect}} hatnote template.

Usage from wikitext

This module cannot be used directly from wikitext. Please use the {{redirect}} or {{redirect2}} templates instead.

Usage from Lua

To use this module from Lua, first load the module.

local mRedirectHatnote = require('Module:Redirect hatnote')

The module can then be used with the following syntax:

mRedirectHatnote._redirect(redirect, data, options, titleObj)

See also

  1 --[[
  2 -- This module produces a "redirect" hatnote. It looks like this:
  3 -- '"X" redirects here. For other uses, see Y.'
  4 -- It implements the {{redirect}} template.
  5 --]]
  6 
  7 local mHatnote = require('Module:Hatnote')
  8 local mHatList = require('Module:Hatnote list')
  9 local mArguments --lazily initialize
 10 local libraryUtil = require('libraryUtil')
 11 local checkType = libraryUtil.checkType
 12 local checkTypeMulti = libraryUtil.checkTypeMulti
 13 
 14 local p = {}
 15 
 16 local function getTitle(...)
 17 	local success, titleObj = pcall(mw.title.new, ...)
 18 	if success then
 19 		return titleObj
 20 	else
 21 		return nil
 22 	end
 23 end
 24 
 25 function p.redirect(frame)
 26 	mArguments = require('Module:Arguments')
 27 	local args = mArguments.getArgs(frame, {parentOnly=true})
 28 	--Get number of redirects
 29 	local numRedirects = tonumber(frame.args[1]) or 1
 30 	-- Create the options table.
 31 	local options = {}
 32 	options.selfref = args.selfref
 33 	return p._redirect(args, numRedirects, options)
 34 end
 35 
 36 function p._redirect(args, numRedirects, options, currentTitle, redirectTitle, targetTitle)
 37 	-- Validate the input. Don't bother checking currentTitle, redirectTitle or
 38 	-- targetTitle, as they are only used in testing.
 39 	checkType('_redirect', 1, args, 'table')
 40 	checkType('_redirect', 2, numRedirects, 'number', true)
 41 	numRedirects = numRedirects or 1
 42 	checkType('_redirect', 3, options, 'table', true)
 43 	options = options or {}
 44 	currentTitle = currentTitle or mw.title.getCurrentTitle()
 45 	-- Get the table of redirects
 46 	local redirect = {}
 47 	for i = 1, numRedirects do
 48 		-- Return an error if a redirect parameter is missing.
 49 		if not args[i] then
 50 			return mHatnote.makeWikitextError(
 51 				'missing redirect parameter',
 52 				'Template:Redirect#Errors',
 53 				args.category
 54 			)
 55 		end
 56 		redirect[i] = args[i]
 57 	end
 58 	-- Generate the text.
 59 	local formattedRedirect = {}
 60 	for k,v in pairs(redirect) do
 61 		formattedRedirect[k] = '"' .. v .. '"'
 62 	end
 63 	local text = {
 64 		mHatList.andList(formattedRedirect) .. ' ' .. (#redirect == 1 and 'redirects' or 'redirect') .. ' here.',
 65 		mHatList._forSee(args, #redirect + 1, {title = redirect[1], extratext = args.text})
 66 	}
 67 	text = table.concat(text, ' ')
 68 	-- Functionality for adding categories	
 69 	local categoryTable = {}
 70 	function addCategory(cat)
 71 		if cat and cat ~= '' then
 72 			-- Add by index to avoid duplicates
 73 			categoryTable[string.format('[[Category:%s]]', cat)] = true
 74 		end
 75 	end
 76 	--Generate tracking categories
 77 	local mhOptions = {}
 78 	for k,v in pairs(redirect) do
 79 		-- We don't need a tracking category if the template invocation has been
 80 		-- copied directly from the docs, or if we aren't in main- or category-space.
 81 		if not v:find('^REDIRECT%d*$') and v ~= 'TERM' -- 
 82 			and currentTitle.namespace == 0 or currentTitle.namespace == 14
 83 		then
 84 			redirectTitle = redirectTitle or getTitle(v)
 85 			if not redirectTitle or not redirectTitle.exists then
 86 				addCategory('Missing redirects')
 87 			elseif not redirectTitle.isRedirect then
 88 				addCategory('Articles with redirect hatnotes needing review')
 89 			else
 90 				local mRedirect = require('Module:Redirect')
 91 				local target = mRedirect.getTarget(redirectTitle)
 92 				targetTitle = targetTitle or target and getTitle(target)
 93 				if targetTitle and targetTitle ~= currentTitle then
 94 					addCategory('Articles with redirect hatnotes needing review')
 95 				end
 96 			end
 97 		end
 98 
 99 		-- Generate the options to pass to [[Module:Hatnote]].
100 		if currentTitle.namespace == 0 and not mhOptions.selfref
101 			and redirectTitle and redirectTitle.namespace ~= 0
102 		then
103 			-- We are on a mainspace page, and the hatnote starts with something
104 			-- like "Wikipedia:Foo redirects here", so automatically label it as
105 			-- a self-reference.
106 			mhOptions.selfref = true
107 		else
108 			mhOptions.selfref = options.selfref
109 		end
110 	end
111 	--concatenate all the categories
112 	local category = ''
113 	for k,v in pairs(categoryTable) do
114 		category = category .. k
115 	end
116 
117 	return mHatnote._hatnote(text, mhOptions) .. category
118 end
119  
120 return p