Module:LegacyLinks: Difference between revisions

Rvogel (talk | contribs)
Created page with "-- Module:LegacyLinks -- Shows a link to the old BlueSpice Classic helpdesk when the requested -- (missing) page existed there. Meant to be invoked from -- MediaWiki:Noarticletext and MediaWiki:Noarticletext-nopermission: -- -- {{#invoke:LegacyLinks|main}} -- -- Optional parameters (all may be omitted): -- |title= Page title to look up (default: the current page) -- |intro= Text shown before the link(s) -- |de= Label for the German link |en= Label for..."
 
Mlink-rodrigue (talk | contribs)
No edit summary
Line 21: Line 21:
}
}


local DEFAULT_INTRO = 'This page is not available in the new helpdesk. '
local DEFAULT_INTRO = 'You can find it in the BlueSpice 5 helpdesk:'
.. 'You can still find it in the previous version:'


local function normalize( s )
local function normalize( s )

Revision as of 15:29, 25 November 2026

Documentation for this module may be created at Module:LegacyLinks/doc

-- Module:LegacyLinks
-- Shows a link to the old BlueSpice Classic helpdesk when the requested
-- (missing) page existed there. Meant to be invoked from
-- MediaWiki:Noarticletext and MediaWiki:Noarticletext-nopermission:
--
--   {{#invoke:LegacyLinks|main}}
--
-- Optional parameters (all may be omitted):
--   |title=   Page title to look up (default: the current page)
--   |intro=   Text shown before the link(s)
--   |de=      Label for the German link   |en= Label for the English link

local p = {}

local DATA = 'Module:LegacyLinks/data'

-- Old systems. Adjust if the hostnames differ.
local OLD_WIKIS = {
	{ key = 'de', base = 'https://de.wiki5.bluespice.com/wiki/', label = 'Deutsch' },
	{ key = 'en', base = 'https://en.wiki5.bluespice.com/wiki/', label = 'English' },
}

local DEFAULT_INTRO = 'You can find it in the BlueSpice 5 helpdesk:'

local function normalize( s )
	s = mw.text.trim( s or '' ):gsub( '_', ' ' ):gsub( '%s+', ' ' )
	return s
end

local function urlFor( base, title )
	-- 'WIKI' encoding: spaces -> underscores, keeps '/' and ':' readable
	return base .. mw.uri.encode( title, 'WIKI' )
end

-- Returns a list of { url, label } for the given title (may be empty)
function p.lookup( title, labels )
	labels = labels or {}
	local data = mw.loadData( DATA )
	local found = {}
	for _, wiki in ipairs( OLD_WIKIS ) do
		local set = data[ wiki.key ]
		if set and set[ title ] then
			table.insert( found, {
				url = urlFor( wiki.base, title ),
				label = labels[ wiki.key ] or wiki.label,
			} )
		end
	end
	return found
end

function p.main( frame )
	local args = frame.args
	local parentArgs = frame:getParent() and frame:getParent().args or {}
	local function arg( name )
		local v = args[ name ]
		if v == nil or v == '' then v = parentArgs[ name ] end
		if v == '' then v = nil end
		return v
	end

	local title = arg( 'title' )
	if title then
		title = normalize( title )
	else
		local current = mw.title.getCurrentTitle()
		-- Never render on existing pages, special pages or the message pages
		-- themselves. (.exists on the current title is not an expensive call.)
		if current.exists or current.namespace < 0 or current.namespace == 8 then
			return ''
		end
		-- Prefixed title, e.g. "Training:Presentation/Backend". The old
		-- namespaces don't exist on the new wiki, so these are main-namespace
		-- pages whose name contains the prefix; text == prefixedText then.
		title = current.prefixedText
	end

	local links = p.lookup( title, { de = arg( 'de' ), en = arg( 'en' ) } )
	if #links == 0 then
		return ''
	end

	local box = mw.html.create( 'div' )
		:addClass( 'legacy-helpdesk-link plainlinks' )
		:attr( 'role', 'note' )
	box:tag( 'p' ):wikitext( arg( 'intro' ) or DEFAULT_INTRO )
	local ul = box:tag( 'ul' )
	for _, l in ipairs( links ) do
		ul:tag( 'li' ):wikitext( string.format( '[%s %s] (%s)', l.url, title, l.label ) )
	end
	return tostring( box )
end

return p