Content deleted Content added
The Earwig (talk | contribs) m Protected "Module:TemplatePar": High-risk Lua module: thousands of mainspace transclusions ([Edit=Allow only autoconfirmed users] (indefinite) [Move=Allow only autoconfirmed users] (indefinite)) |
update from global upstream version, as requested by the developer; pre-view tested |
||
(One intermediate revision by one other user not shown) | |||
Line 1:
suite = "TemplatePar",
item = 15393417,
globals = { DateTime = 20652535,
FileMedia = 24765326,
Multilingual = 47541920,
TemplUtl = 52364930,
URLutil = 10859193 } }
--[=[
Template parameter utility
* assert
Line 6 ⟶ 14:
* countNotEmpty
* downcase()
* duplicates
* match
* valid
* verify()
* TemplatePar()
* failsafe()
]=]
local Local = { frame = false }
local Failsafe = TemplatePar
local GlobalMod = Local
-- Module globals
Local.messagePrefix = "lua-module-TemplatePar-"
Local.L10nDef = {}
badPattern = "#invoke:TemplatePar pattern syntax error",
dupOpt = "#invoke:TemplatePar repeated optional parameter",
Line 33 ⟶ 47:
tooLong = "Error in template * parameter too long",
tooShort = "Error in template * parameter too short",
unavailable = "Error in template * parameter name missing",
undefined = "Error in template * mandatory parameter missing",
unknown = "Error in template * unknown parameter name",
unknownRule = "#invoke:TemplatePar unknown rule"
}
[ "ASCII" ] = "^[ -~]*$",
[ "ASCII+" ] = "^[ -~]+$",
Line 90 ⟶ 87:
[ "pagename" ] = string.format( "^[^#<>%%[%%]|{}%c-%c%c]+$",
1, 31, 127 ),
[ "ref" ] = string.format( "%c'%c`UNIQ%s%sref%s%s%sQINU`%c'%c",
127, 34, "%-", "%-", "%-", "%x+",
"%-", 34, 127 ),
[ "+" ] = "%S"
}
Local.boolean = { ["1"] = true,
["true"] = true,
y = true,
yes = true,
on = true,
["0"] = true,
["false"] = true,
["-"] = true,
n = true,
no = true,
off = true }
Local.patternCJK = false
local foreignModule = function
-- Fetch global module
-- Precondition:
-- access -- string, with name of base module
-- advanced -- true, for require(); else mw.loadData()
-- append -- string, with subpage part, if any; or false
-- alt -- number, of wikidata item of root; or false
-- alert -- true, for throwing error on data problem
-- Postcondition:
-- Returns whatever, probably table
-- 2020-01-01
local storage = access
local finer = function ()
if append then
storage = string.format( "%s/%s",
storage,
append )
end
end
local fun, lucky, r, suited
if advanced then
fun = require
else
fun = mw.loadData
end
GlobalMod.globalModules = GlobalMod.globalModules or { }
suited = GlobalMod.globalModules[ access ]
if not suited then
finer()
lucky, r = pcall( fun, "Module:" .. storage )
end
if not lucky then
if not suited and
type( alt ) == "number" and
alt > 0 then
suited = string.format( "Q%d", alt )
suited = mw.wikibase.getSitelink( suited )
GlobalMod.globalModules[ access ] = suited or true
end
if type( suited ) == "string" then
storage = suited
finer()
lucky, r = pcall( fun, storage )
end
if not lucky and alert then
error( "Missing or invalid page: " .. storage )
end
end
return r
end -- foreignModule()
local function Foreign( access )
-- Access standardized library
-- Precondition:
-- access -- string, with name of base module
-- Postcondition:
-- Return library table, or not
-- Uses:
local r
if Local[ access ] then
r = Local[ access ]
else
local bib = foreignModule( access,
true,
false,
TemplatePar.globals[ access ],
false )
if type( bib ) == "table" and
type( bib[ access ] ) == "function" then
bib = bib[ access ]()
if type( bib ) == "table" then
r = bib
Local[ access ] = bib
end
end
end
return r
end -- Foreign()
local function containsCJK( analyse )
-- Is any CJK character present?
-- Precondition:
--
-- Postcondition:
-- Return false iff no CJK present
-- Uses:
-- >< Local.patternCJK
-- mw.ustring.char()
-- mw.ustring.match()
local r = false
if not Local.patternCJK then
Local.patternCJK = mw.ustring.char( 91,
13312, 45, 40959,
131072, 45, 178207,
93 )
end
if mw.ustring.match(
r = true
end
Line 138 ⟶ 232:
-- Uses:
-- Module:FileMedia
-- Foreign()
-- FileMedia.isFile()
-- FileMedia.isType()
local r
if attempt and attempt ~= "" then
local
if FileMedia and type( FileMedia.isFile ) == "
local s, live = accept:match( "^([a-z]+)(:?)%+?$" )
if live then
Line 180 ⟶ 276:
-- Return some message string
-- Uses:
-- >
-- > Local.L10nDef
-- mw.message.new()
-- Module:Multilingual
-- Foreign()
-- TemplatePar.framing()
-- Multilingual.tabData()
local m = mw.message.new( Local.messagePrefix .. say )
local r = false
if m:isBlank() then
local
r = l10n[ say ]
else
local MultiL = Foreign( "Multilingual" )
if MultiL and type( MultiL.tabData ) == "function" then
local lang
r, lang = MultiL.tabData( "I18n/Module:TemplatePar",
say,
false,
TemplatePar.framing() )
end
end
if not r then
r = Local.L10nDef.en[ say ]
end
else
m:inLanguage( c )
Line 205 ⟶ 316:
local function
-- Check string as possible boolean
-- Precondition:
--
--
-- boolean+
-- attempt -- string; to be tested
-- Postcondition:
-- Return
-- Uses:
--
-- Foreign()
local r
r = mw.text.trim( attempt ):lower()
if r == "" then
if accept == "boolean+" then
r = "empty"
else
r = false
end
elseif Local.boolean[ r ] or r:match( "^[01%-]+$" ) then
r = false
else
local TemplUtl = Foreign( "TemplUtl" )
if TemplUtl and type( TemplUtl.faculty ) == "function" then
r = TemplUtl.faculty( r, "-" )
if r == "-" then
r = "invalid"
else
r = false
end
else
r = "invalid"
end
end
return r
end -- faculty()
Line 243 ⟶ 381:
return r
end -- failure()
local function fair( story, scan )
-- Test for match (possibly user-defined with syntax error)
-- Precondition:
-- story -- string; parameter value
-- scan -- string; pattern
-- Postcondition:
-- Return nil, if not matching, else non-nil
-- Uses:
-- mw.ustring.match()
return mw.ustring.match( story, scan )
end -- fair()
local function familiar( accept, attempt )
-- Check string as possible language name or list
-- Precondition:
-- accept -- string; requirement
-- lang
-- langs
-- langW
-- langsW
-- lang+
-- langs+
-- langW+
-- langsW+
-- attempt -- string; to be tested
-- Postcondition:
-- Return error keyword, or false
-- Uses:
-- Module:Multilingual
-- Foreign()
-- Multilingual.isLang()
local r
if attempt and attempt ~= "" then
local MultiL = Foreign( "Multilingual" )
if MultiL and type( MultiL.isLang ) == "function" then
local lazy = accept:find( "W", 1, true )
if accept:find( "s", 1, true ) then
local group = mw.text.split( attempt, "%s+" )
r = false
for i = 1, #group do
if not MultiL.isLang( group[ i ], lazy ) then
r = "invalid"
break -- for i
end
end -- for i
elseif MultiL.isLang( attempt, lazy ) then
r = false
else
r = "invalid"
end
else
r = "missing"
end
elseif accept:find( "+", 1, true ) then
r = "empty"
else
r = false
end
return r
end -- familiar()
local function far( accept, attempt )
-- Check string as possible URL
-- Precondition:
-- accept -- string; requirement
-- url
-- url+
-- attempt -- string; to be tested
-- Postcondition:
-- Return error keyword, or false
-- Uses:
-- Module:URLutil
-- Foreign()
-- URLutil.isWebURL()
local r
if attempt and attempt ~= "" then
local URLutil = Foreign( "URLutil" )
if URLutil and type( URLutil.isWebURL ) == "function" then
if URLutil.isWebURL( attempt ) then
r = false
else
r = "invalid"
end
else
r = "missing"
end
elseif accept:find( "+", 1, true ) then
r = "empty"
else
r = false
end
return r
end -- far()
local function fast( accept, attempt )
-- Check string as possible date or time
-- Precondition:
-- accept -- string; requirement
-- datetime
-- datetime+
-- datetime/y
-- datetime/y+
-- datetime/ym
-- datetime/ym+
-- datetime/ymd
-- datetime/ymd+
-- attempt -- string; to be tested
-- Postcondition:
-- Return error keyword, or false
-- Uses:
-- Module:DateTime
-- Foreign()
-- DateTime.DateTime()
local r
r = mw.text.trim( attempt )
if r == "" then
if accept:find( "+", 1, true ) then
r = "empty"
else
r = false
end
else
local DateTime = Foreign( "DateTime" )
if type( DateTime ) == "table" then
local d = DateTime( attempt )
if type( d ) == "table" then
if accept:find( "/", 1, true ) then
r = "invalid"
if accept:sub( 1, 10 ) == "datetime/y" then
if d.year then
r = false
if accept:sub( 1, 11 ) == "datetime/ym" then
if d.month then
if accept:sub( 1, 12 )
== "datetime/ymd" then
if not d.dom then
r = "invalid"
end
end
else
r = "invalid"
end
end
end
end
else
r = false
end
else
r = "invalid"
end
else
r = "invalid"
end
end
return r
end -- fast()
Line 283 ⟶ 587:
-- false if valid or no answer permitted
-- Uses:
-- >
-- failure()
-- mw.text.trim()
-- faculty()
-- fast()
-- facility()
--
-- far()
-- fair()
-- containsCJK()
local r = false
local s = false
local show = nil
local scan = false
local stuff = mw.text.trim( analyze )
if type( options.pattern ) == "string" then
if options.key then
Line 306 ⟶ 615:
end
if s ~= "*" then
scan =
end
if type( scan ) == "string" then
if s == "n" or s == "0,0" or s == "0.0" then
if not
not
scan = false
if options.say then
show = string.format( "
end
if abbr then
Line 328 ⟶ 637:
n = tonumber( n )
if n then
local i = tonumber(
if i then
if op == "<" then
Line 352 ⟶ 661:
r = "undefined"
end
elseif s:match( "^boolean%+?$" ) then
r = faculty( s, stuff )
n = true
elseif s:match( "^datetime/?y?m?d?%+?$" ) then
r = fast( s, stuff )
n = true
elseif s:match( "^image%+?:?$" ) or
s:match( "^file%+?:?$" ) then
r = facility( s,
n = true
elseif s:match( "langs?W?%+?" ) then
r = familiar( s, stuff )
n = true
elseif s:match( "url%+?" ) then
r = far( s, stuff )
n = true
end
-- datetime+
-- iso8631+
-- line+
if not n and not r then
r = "unknownRule"
Line 366 ⟶ 686:
if r then
if options.say then
show = string.format( "
else
show = s
Line 379 ⟶ 699:
end
if scan then
local legal, got = pcall(
if legal then
if not got then
if s == "aa" then
got = containsCJK(
end
if not got then
if options.say then
show = string.format( "
end
if abbr then
Line 414 ⟶ 734:
-- Postcondition:
-- Return true iff found
local k, v, r
for k, v in pairs( haystack ) do
if k == needle then
end
end -- for k, v
return r or false
end -- fed()
Line 435 ⟶ 755:
-- Uses:
-- TemplatePar.downcase()
--
-- frame:getParent()
local g, k, v
Line 442 ⟶ 762:
g = TemplatePar.downcase( options )
else
g =
if light then
g = g:getParent()
Line 486 ⟶ 806:
if type( sub ) == "string" then
sub = sub:gsub( "%%!", "|" )
:gsub( "\\n", string.char( 10 ) )
options.pattern = sub
options.key = nil
Line 526 ⟶ 847:
local function finalize( submit, options
-- Finalize message
-- Precondition:
Line 535 ⟶ 856:
-- options.cat
-- options.template
-- Postcondition:
-- Return string or false
-- Uses:
-- TemplatePar.framing()
-- factory()
local r = false
if submit then
local lazy = false
local learn = false
local show = false
local opt, s
if type( options ) == "table" then
opt = options
Line 551 ⟶ 873:
s = opt.preview
if type( s ) == "string" and
local sniffer = "{{REVISIONID}}"
if lazy then
show = ""
lazy = false
end
if
if s == "1" then
show = "*"
Line 565 ⟶ 885:
show = s
end
learn = true
end
end
Line 581 ⟶ 902:
local i
if not show or show == "*" then
:attr( "class", "error" )
:wikitext( "@@@" )
if learn then
local max = 1000000000
local id = math.floor( os.clock() * max )
local sign = string.format( "error_%d", id )
local btn = mw.html.create( "span" )
local top = mw.html.create( "div" )
e:attr( "id", sign )
btn:css( { ["background"] = "#FFFF00",
["border"] = "#FF0000 3px solid",
["font-weight"] = "bold",
["padding"] = "2px",
["text-decoration"] = "none" } )
:wikitext( ">>>" )
sign = string.format( "[[#%s|%s]]",
sign, tostring( btn ) )
top:wikitext( sign, " ", submit )
mw.addWarning( tostring( top ) )
end
show = tostring( e )
end
i = show:find( "@@@", 1, true )
Line 593 ⟶ 935:
r = show
end
end
if learn and r then
-- r = fatal( r )
end
s = opt.cat
if type( s ) == "string" then
local link
if opt.errNS then
local ns = mw.title.getCurrentTitle().namespace
Line 603 ⟶ 949:
local spaces = string.format( " %s ", opt.errNS )
if spaces:match( space ) then
end
elseif st == "table" then
for i = 1, #opt.errNS do
if opt.errNS[ i ] == ns then
break -- for i
end
end -- for i
end
else
link = true
end
if
if not r then
r = ""
Line 625 ⟶ 972:
end
end
for i = 1, #cats do
s = mw.text.trim( cats[ i ] )
Line 673 ⟶ 1,019:
-- failure()
-- fed()
local r = false
local lack
for k, v in pairs( got ) do
if
lack = true
break -- for k, v
elseif not finder( valid, k ) then
r = fault( r, k )
end
end -- for k, v
if
r = failure( "unavailable", false, options )
elseif r then
r = failure( "unknown",
string.format( "
options )
else -- all names valid
Line 804 ⟶ 1,155:
-- options.mandatory
-- options.optional
-- frame -- object; #invoke environment, or false
-- Postcondition:
-- Return string with error message as configured;
-- false if valid
-- Uses:
-- TemplatePar.framing()
-- fold()
-- fetch()
Line 814 ⟶ 1,166:
-- finalize()
local duty, r
if frame then
TemplatePar.framing( frame )
end
if type( options ) == "table" then
if type( options.mandatory ) ~= "table" then
Line 836 ⟶ 1,191:
end
end
return finalize( r, options
end -- form()
Line 871 ⟶ 1,226:
show = " <" .. options.min
if options.say then
show = string.format( "%s
end
r = failure( "tooShort", show, options )
Line 884 ⟶ 1,239:
show = " >" .. options.max
if options.say then
show = string.format( "%s
end
r = failure( "tooLong", show, options )
Line 907 ⟶ 1,262:
-- false if valid or no answer permitted
-- Uses:
-- mw.text.trim()
-- format()
-- failure()
Line 914 ⟶ 1,269:
local story = assignment.args[ access ] or ""
if type( access ) == "number" then
story = mw.text.trim( story )
end
if type( options ) ~= "table" then
Line 986 ⟶ 1,341:
end
if r then
r = finalize( r, options
else
s = frame.args[ 1 ] or ""
Line 994 ⟶ 1,349:
end
if action == "valid" then
r = TemplatePar.valid( s, options
elseif action == "assert" then
r = TemplatePar.assert( s, "", options )
Line 1,021 ⟶ 1,376:
if ( type( append ) == "string" ) then
if ( append ~= "" ) then
r = string.format( "%s<br /> %s", append, r )
end
else
Line 1,104 ⟶ 1,459:
TemplatePar.valid = function ( access, options
-- Check validity of one particular template parameter
-- Precondition:
Line 1,110 ⟶ 1,465:
-- string or number
-- options -- table or nil; optional details
-- Postcondition:
-- Return string with error message as configured;
Line 1,117 ⟶ 1,471:
-- mw.text.trim()
-- TemplatePar.downcase()
-- TemplatePar.framing()
-- frame:getParent()
-- formatted()
Line 1,140 ⟶ 1,495:
params = TemplatePar.downcase( options )
else
params =
end
r = formatted( params, access, options )
Line 1,146 ⟶ 1,501:
r = failure( "noname", false, options )
end
return finalize( r, options
end -- TemplatePar.valid()
Line 1,162 ⟶ 1,517:
return form( false, options, false )
end -- TemplatePar.verify()
TemplatePar.framing = function( frame )
-- Ensure availability of frame object
-- Precondition:
-- frame -- object; #invoke environment, or false
-- Postcondition:
-- Return frame object
-- Uses:
-- >< Local.frame
if not Local.frame then
if type( frame ) == "table" and
type( frame.args ) == "table" and
type( frame.getParent ) == "function" and
type( frame:getParent() ) == "table" and
type( frame:getParent().getParent ) == "function" and
type( frame:getParent():getParent() ) == "nil" then
Local.frame = frame
else
Local.frame = mw.getCurrentFrame()
end
end
return Local.frame
end -- TemplatePar.framing()
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
-- 2020-08-17
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
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()
Line 1,179 ⟶ 1,617:
-- furnish()
return furnish( frame, "assert" )
end -- p.assert()
Line 1,216 ⟶ 1,654:
end
return r or ""
end -- p.check()
Line 1,227 ⟶ 1,665:
-- TemplatePar.count()
return tostring( TemplatePar.count() )
end -- p.count()
Line 1,238 ⟶ 1,676:
-- TemplatePar.countNotEmpty()
return tostring( TemplatePar.countNotEmpty() )
end -- p.countNotEmpty()
Line 1,244 ⟶ 1,682:
function p.match( frame )
-- Combined analysis of parameters and their values
-- Precondition:
-- frame -- object; #invoke environment
-- Postcondition:
-- Return string with error message or ""
-- Uses:
-- TemplatePar.framing()
-- mw.text.trim()
-- mw.ustring.lower()
Line 1,266 ⟶ 1,707:
local k, v, s
local params = { }
TemplatePar.framing( frame )
for k, v in pairs( frame.args ) do
if type( k ) == "number" then
Line 1,312 ⟶ 1,754:
for k, v in pairs( params ) do
options.say = k
if s then
if s == "" then
Line 1,330 ⟶ 1,771:
if lack then
if errMiss then
errMiss = string.format(
else
errMiss = string.format( "
k )
end
elseif not errMiss then
Line 1,349 ⟶ 1,791:
r = failure( "invalid", errValues, options )
end
r = finalize( r, options
end
end
return r or ""
end -- p.match()
Line 1,366 ⟶ 1,808:
-- furnish()
return furnish( frame, "valid" )
end -- p.valid()
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
Line 1,375 ⟶ 1,837:
-- Return table with functions
return TemplatePar
end -- p.TemplatePar()
setmetatable( p, { __call = function ( func, ... )
setmetatable( p, nil )
return Failsafe
end } )
return p
|