Modul:PageUtil: Unterschied zwischen den Versionen
2018-10-12
(2017-11-11) |
(2018-10-12) |
||
Zeile 1: | Zeile 1: | ||
local PageUtil = { suite = "PageUtil", | local PageUtil = { suite = "PageUtil", | ||
serial = " | serial = "2018-10-12", | ||
item = 56033297 } | |||
--[=[ | --[=[ | ||
PageUtil | PageUtil | ||
Zeile 14: | Zeile 15: | ||
-- Format message with class="error" | -- Format message with class="error" | ||
-- alert -- string, with message | -- alert -- string, with message | ||
-- frame -- object | -- frame -- object, if known | ||
-- Returns message with markup | -- Returns message with markup | ||
local | local scream = alert | ||
if frame then | if frame then | ||
scream = string.format( "%s * %s", frame:getTitle(), scream ) | |||
end | end | ||
return tostring( mw.html.create( "span" ) | |||
:addClass( "error" ) | |||
:wikitext( scream ) ) | |||
end -- fault() | end -- fault() | ||
Zeile 75: | Zeile 77: | ||
end | end | ||
else | else | ||
r = | r = tostring( mw.html.create( "div" ) | ||
:addClass( "error" ) | |||
:wikitext( source ) ) | |||
end | end | ||
end | end | ||
Zeile 105: | Zeile 108: | ||
seed = source | seed = source | ||
end | end | ||
r = frame:expandTemplate( { title = seed } | r = tostring( mw.html.create( "span" ) | ||
:attr( "id", segment ) | |||
:newline() ) | |||
.. frame:expandTemplate( { title = seed } ) | |||
table.insert( assembly, { source, segment } ) | table.insert( assembly, { source, segment } ) | ||
else | else | ||
r = | r = tostring( mw.html.create( "div" ) | ||
:addClass( "error" ) | |||
:wikitext( page.prefixedText ) ) | |||
end | end | ||
else | else | ||
r = string.format( " | r = string.format( "%s '%s'", "Unknown page", access ) | ||
r = tostring( mw.html.create( "div" ) | |||
:addClass( "error" ) | |||
:wikitext( r ) ) | |||
end | end | ||
return r | return r | ||
Zeile 122: | Zeile 130: | ||
PageUtil.failsafe = function ( assert ) | PageUtil.failsafe = function ( assert ) | ||
-- Retrieve versioning and check for compliance | |||
-- Precondition: | |||
-- assert -- string, with required version or "wikidata", | |||
-- or false | |||
-- Postcondition: | |||
-- Returns string with appropriate version, or false | |||
local since = assert | |||
local r | local r | ||
if not | if since == "wikidata" then | ||
r = PageUtil. | local item = PageUtil.item | ||
since = false | |||
if type( item ) == "number" and item > 0 then | |||
local ent = mw.wikibase.getEntity( string.format( "Q%d", | |||
item ) ) | |||
if type( ent ) == "table" then | |||
local vsn = ent:formatPropertyValues( "P348" ) | |||
if type( vsn ) == "table" and | |||
type( vsn.value ) == "string" and | |||
vsn.value ~= "" then | |||
r = vsn.value | |||
end | |||
end | |||
end | |||
end | |||
if not r then | |||
if not since or since <= PageUtil.serial then | |||
r = PageUtil.serial | |||
else | |||
r = false | |||
end | |||
end | |||
return r | |||
end -- PageUtil.failsafe() | |||
PageUtil.getProtection = function ( access, action ) | |||
-- Retrieve protection | |||
-- access -- string or title or nil, with page, default: current | |||
-- action -- string or nil, with action, default: edit | |||
-- Returns number: One of: 0, 0.5, 0.75, 1 | |||
local t = type( access ) | |||
local r = 0 | |||
local p | |||
if t == "string" then | |||
t = mw.title.new( access ) | |||
elseif t == "table" then | |||
t = access | |||
else | else | ||
r = | t = mw.title.getCurrentTitle() | ||
end | |||
p = t.protectionLevels | |||
if type( p ) == "table" then | |||
local s | |||
if type( action ) == "string" then | |||
s = mw.text.trim( action ) | |||
if s == "" then | |||
s = false | |||
end | |||
end | |||
p = p[ s or "edit" ] | |||
if type( p ) == "table" then | |||
for k, v in pairs( p ) do | |||
if v == "autoconfirmed" then | |||
r = 0.5 | |||
elseif v == "editeditorprotected" then | |||
r = 0.75 | |||
elseif v == "sysop" then | |||
r = 1 | |||
end | |||
end -- for k, v | |||
end | |||
end | end | ||
return r | return r | ||
end -- PageUtil. | end -- PageUtil.getProtection() | ||
Zeile 190: | Zeile 265: | ||
-- Export | -- Export | ||
local p = { } | local p = { } | ||
p.getProtection = function ( frame ) | |||
local n = PageUtil.getProtection( frame.args[ 1 ], frame.args[ 2 ] ) | |||
local t = { [ 0 ] = "", | |||
[ 0.5 ] = mw.ustring.char( 189 ), | |||
[ 0.75 ] = mw.ustring.char( 190 ), | |||
[ 1 ] = "1" } | |||
return t[ n ] | |||
end -- p.getProtection | |||
function p.isRedirect() | function p.isRedirect() | ||
return mw.title.getCurrentTitle().isRedirect and "1" or "" | return mw.title.getCurrentTitle().isRedirect and "1" or "" | ||
end -- p.isRedirect | end -- p.isRedirect | ||
Zeile 201: | Zeile 285: | ||
end | end | ||
return r | return r | ||
end | end -- p.merge | ||
p.failsafe = function ( frame ) | p.failsafe = function ( frame ) |