Modul:PageTitleSimilar
Erscheinungsbild
Vorlagenprogrammierung | Diskussionen | Lua | Unterseiten | |||
Modul | Deutsch | English
|
Modul: | Dokumentation |
Diese Seite enthält Code in der Programmiersprache Lua. Einbindungszahl Cirrus
local PageTitleSimilar = { suite = "PageTitleSimilar",
serial = "2025-02-11",
item = 0 }
--[==[
Check whether similar page name is existing
]==]
local Failsafe = PageTitleSimilar
local THIS = { }
local fault = function ( alert )
-- Format error message
-- Precondition:
-- alert -- string, with message
-- Postcondition:
-- Returns string
local e = mw.html.create( "span" )
:addClass( "error" )
:wikitext( string.format( "%s * %s",
PageTitleSimilar.suite,
alert ) )
return tostring( e )
end -- fault()
local find = function ( ask )
-- Check whether page title exists in this namespace
-- Precondition:
-- ask -- string, with page title
-- Postcondition:
-- Returns true, if existing
-- THIS.title is the title of an existing page
local t = mw.title.new( ask, THIS.ns )
local r
if t then
r = t.exists
if r then
THIS.title = t
end
end
return r
end -- find()
local found = function ( apply )
-- Communicate match by transclusion
-- Precondition:
-- apply -- string, with message type
-- -- Typogr
-- -- URL
-- THIS.title has been defined
-- Postcondition:
-- Returns string
local source = "source" .. apply
local shift = THIS.JSON[ source ]
local r
if type( shift ) == "string" then
local t = mw.title.new( shift )
if t and t.exists then
local params = { ["1"] = THIS.title.prefixedText }
r = THIS.frame:expandTemplate{ title = t,
args = params }
end
end
if not r then
r = fault( "missing transclusion for " .. source )
end
return r
end -- found()
local fraction = function ()
-- Try URL completion when common special chars were truncated
-- Postcondition:
-- Returns string, or not
local s = THIS.self
local r
if s:find( "(", 1, true ) then
find( s .. ")" )
end
if not THIS.title then
local shift = s .. "."
if not find( shift ) then
shift = s .. "?"
find( shift )
end
end
if THIS.title then
r = found( "URL" )
end
return r
end -- fraction()
local further = function ()
-- Try improvements by typographical chars
-- Postcondition:
-- Returns string, or not
local rep = THIS.JSON.replace
local r
if type( rep ) == "table" then
local s = THIS.self
local shift
for k, v in pairs( rep ) do
if type( v[ 1 ] ) == "string" and
type( v[ 2 ] ) == "string" then
shift = mw.ustring.gsub( s, v[ 1 ], v[ 2 ] )
if shift ~= s then
if find( shift ) then
r = found( "Typogr" )
break -- for k, v
else
s = shift
end
end
else
r = fault( string.format( "replace[%s]", k ) )
break -- for k, v
end
end -- for k, v
end
return r
end -- further()
Failsafe.failsafe = function ( atleast )
-- Retrieve versioning and check for compliance
-- Precondition:
-- atleast -- string, with required version
-- or wikidata|item|~|@ or false
-- Postcondition:
-- Returns string -- with queried version/item, also if problem
-- false -- if appropriate
-- 2024-03-01
local since = atleast
local last = ( since == "~" )
local linked = ( since == "@" )
local link = ( since == "item" )
local r
if last or link or linked or since == "wikidata" then
local item = Failsafe.item
since = false
if type( item ) == "number" and item > 0 then
local suited = string.format( "Q%d", item )
if link then
r = suited
else
local entity = mw.wikibase.getEntity( suited )
if type( entity ) == "table" then
local seek = Failsafe.serialProperty or "P348"
local vsn = entity:formatPropertyValues( seek )
if type( vsn ) == "table" and
type( vsn.value ) == "string" and
vsn.value ~= "" then
if last and vsn.value == Failsafe.serial then
r = false
elseif linked then
if mw.title.getCurrentTitle().prefixedText
== mw.wikibase.getSitelink( suited ) then
r = false
else
r = suited
end
else
r = vsn.value
end
end
end
end
elseif link then
r = false
end
end
if type( r ) == "nil" then
if not since or since <= Failsafe.serial then
r = Failsafe.serial
else
r = false
end
end
return r
end -- Failsafe.failsafe()
-- Export
local p = { }
p.f = function ( frame )
local source = frame.args[ 1 ]
local r, scream
if source then
local lucky
lucky, THIS.JSON = pcall( mw.loadJsonData, source )
if lucky then
local t = mw.title.getCurrentTitle()
THIS.frame = frame
THIS.ns = t.ns
THIS.self = t.prefixedText
if frame.args.URL == "1" then
r = fraction()
end
if not r then
r = further()
end
else
scream = "no valid JSON found: " .. mw.text.nowiki( source )
end
else
scream = "invoked without JSON"
end
if scream then
r = fault( scream )
end
return r or ""
end -- p.f
p.failsafe = function ( frame )
-- Versioning interface
local s = type( frame )
local since
if s == "table" then
since = frame.args[ 1 ]
elseif s == "string" then
since = frame
end
if since then
since = mw.text.trim( since )
if since == "" then
since = false
end
end
return Failsafe.failsafe( since ) or ""
end -- p.failsafe
return p