Modul:WLink: Unterschied zwischen den Versionen
2019-11-04, s. https://de.wikipedia.org/w/index.php?title=Wikipedia:Administratoren/Anfragen&curid=6974524&diff=193809804&oldid=193802072
w>NordNordWest (2019-06-17, siehe https://de.wikipedia.org/w/index.php?title=Wikipedia:Administratoren/Anfragen&curid=6974524&diff=189628063&oldid=189625851) |
w>NordNordWest (2019-11-04, s. https://de.wikipedia.org/w/index.php?title=Wikipedia:Administratoren/Anfragen&curid=6974524&diff=193809804&oldid=193802072) |
||
| Zeile 1: | Zeile 1: | ||
local WLink = { suite = "WLink", | local WLink = { suite = "WLink", | ||
serial = "2019- | serial = "2019-11-04", | ||
item = 19363224 }; | item = 19363224 }; | ||
--[=[ | --[=[ | ||
| Zeile 12: | Zeile 12: | ||
getFragment() | getFragment() | ||
getLanguage() | getLanguage() | ||
getLinktextProblem() | |||
getNamespace() | getNamespace() | ||
getNamespaced() | getNamespaced() | ||
| Zeile 43: | Zeile 44: | ||
-- local globals | -- local globals | ||
local URLutil = false; | local URLutil = false; | ||
local Failsafe = WLink; | |||
local GlobalMod = WLink; | |||
local htmlInline = { b = true, | local htmlInline = { b = true, | ||
bdi = true, | |||
bdo = true, | |||
big = true, | big = true, | ||
code = true, | code = true, | ||
| Zeile 58: | Zeile 63: | ||
span = true, | span = true, | ||
strong = true, | strong = true, | ||
style = true, | |||
sub = true, | sub = true, | ||
sup = true, | sup = true, | ||
tt = true, | tt = true, | ||
var = true }; | var = true }; | ||
local foreignModule = function ( access, advanced, append, alt, alert ) | |||
-- 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 | |||
-- 2019-10-29 | |||
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, 0 ); | |||
end | |||
end | |||
return r; | |||
end -- foreignModule() | |||
| Zeile 71: | Zeile 130: | ||
-- Throws error, if not available | -- Throws error, if not available | ||
if not URLutil then | if not URLutil then | ||
local | local util = foreignModule( "URLutil", true, false, 10859193 ); | ||
if type( util ) == "table" then | |||
URLutil = util.URLutil(); | |||
else | |||
util = "library URLutil invalid"; | util = "library URLutil invalid"; | ||
end | end | ||
| Zeile 418: | Zeile 476: | ||
function WLink.formatURL( adjust ) | function WLink.formatURL( adjust, assure ) | ||
-- Create bracketed link, if not yet | -- Create bracketed link, if not yet | ||
-- Precondition: | -- Precondition: | ||
| Zeile 663: | Zeile 721: | ||
function WLink. | function WLink.getLinktextProblem( attempt ) | ||
-- | -- Which problem has this presumable link text? | ||
-- Precondition: | -- Precondition: | ||
-- attempt -- string, with | -- attempt -- string, with presumable linktext | ||
-- Postcondition: | -- Postcondition: | ||
-- Returns | -- Returns string, with error message, or false | ||
local r; | |||
local r | if attempt:find( "]", 1, true ) then | ||
r = "]"; | |||
elseif mw.text.unstrip( attempt ) ~= attempt then | |||
r = "<wiki>"; | |||
elseif attempt:find( "\n", 1, true ) then | |||
r = | r = "\n"; | ||
else | |||
local i = attempt:find( "<", 1, true ); | |||
if i then | |||
local s = mw.ustring.lower( attempt:sub( i ) ); | |||
local sign = true; | |||
local skip; | |||
while sign and not r do | |||
skip, sign = s:match( "^([^<]*< *)(%l[%l%d]*)[ /]*.*>" ); | |||
if sign then | |||
if htmlInline[ sign ] then | |||
i = skip:len() + sign:len() + 1; | |||
s = s:sub( i ); | |||
else | |||
r = string.format( "<%s>", sign ); | |||
end | |||
end | |||
end -- while sign and not r | |||
end | |||
if not r then | |||
local s = attempt .. " "; | |||
if s:find( "ISBN ", 1, true ) then | |||
r = s:match( "(ISBN %d[%-%d]+[%dxX])%W" ); | |||
end | |||
if not r then | |||
if s:find( "PMID ", 1, true ) then | |||
r = s:match( "(PMID [1-9]%d*)%W" ); | |||
end | |||
if not r then | |||
if s:find( "RFC ", 1, true ) then | |||
r = s:match( "(RFC [1-9]%d?%d?%d?)%W" ); | |||
end | |||
end | |||
end | |||
end | end | ||
end | end | ||
return r; | return r or false; | ||
end -- WLink. | end -- WLink.getLinktextProblem() | ||
function WLink. | function WLink.getNamespace( attempt ) | ||
-- Retrieve | -- Retrieve namespace number | ||
-- Precondition: | -- Precondition: | ||
-- area -- string or number, with some namespace spec | -- attempt -- string, with wikilink or page title | ||
-- attempt -- string, with wikilink or page title or page name | -- Postcondition: | ||
-- Returns number, of detected namespace | |||
-- false if no namespace found | |||
local r = false; | |||
local s, m = WLink.getTarget( attempt ); | |||
if m == 2 then | |||
local w = WLink.wikilink( s ); | |||
if w and not w.lang and not w.project and w.ns then | |||
r = w.ns; | |||
end | |||
end | |||
return r; | |||
end -- WLink.getNamespace() | |||
function WLink.getNamespaced( area, attempt ) | |||
-- Retrieve page in namespace | |||
-- Precondition: | |||
-- area -- string or number, with some namespace spec | |||
-- attempt -- string, with wikilink or page title or page name | |||
-- Postcondition: | -- Postcondition: | ||
-- Returns page prefixed by namespace, | -- Returns page prefixed by namespace, | ||
| Zeile 1.227: | Zeile 1.338: | ||
-- Postcondition: | -- Postcondition: | ||
-- Returns boolean | -- Returns boolean | ||
local | local s; | ||
if allow and s:find( "\n", 1, true ) then | |||
s = attempt:gsub( "\n", " " ); | |||
else | else | ||
s = attempt; | |||
end | end | ||
return not WLink.getLinktextProblem( s ); | |||
end -- WLink.isValidLinktext() | |||
end -- WLink.isValidLinktext() | |||
| Zeile 1.424: | Zeile 1.493: | ||
-- false if nothing found | -- false if nothing found | ||
local s = contentWikilink( attempt or "" ); | local s = contentWikilink( attempt or "" ); | ||
local got, n, r; | local got, i, n, r; | ||
if not s then | if not s then | ||
s = attempt; | s = attempt; | ||
| Zeile 1.509: | Zeile 1.578: | ||
Failsafe.failsafe = function ( atleast ) | |||
-- Retrieve versioning and check for compliance | -- Retrieve versioning and check for compliance | ||
-- Precondition: | -- Precondition: | ||
-- | -- atleast -- string, with required version or "wikidata" or "~" | ||
-- | -- or false | ||
-- Postcondition: | -- Postcondition: | ||
-- Returns string with | -- Returns string -- with queried version, also if problem | ||
local since = | -- false -- if appropriate | ||
local last = ( atleast == "~" ); | |||
local since = atleast; | |||
local r; | local r; | ||
if since == "wikidata" then | if last or since == "wikidata" then | ||
local item = | local item = Failsafe.item; | ||
since = false; | since = false; | ||
if type( item ) == "number" and item > 0 then | if type( item ) == "number" and item > 0 then | ||
| Zeile 1.529: | Zeile 1.600: | ||
type( vsn.value ) == "string" and | type( vsn.value ) == "string" and | ||
vsn.value ~= "" then | vsn.value ~= "" then | ||
r = vsn.value; | if last and vsn.value == Failsafe.serial then | ||
r = false; | |||
else | |||
r = vsn.value; | |||
end | |||
end | end | ||
end | end | ||
end | end | ||
end | end | ||
if | if type( r ) == "nil" then | ||
if not since or since <= | if not since or since <= Failsafe.serial then | ||
r = | r = Failsafe.serial; | ||
else | else | ||
r = false; | r = false; | ||
| Zeile 1.542: | Zeile 1.617: | ||
end | end | ||
return r; | return r; | ||
end -- | end -- Failsafe.failsafe() | ||
| Zeile 1.622: | Zeile 1.697: | ||
p.ansiPercent = function ( frame ) | p.ansiPercent = function ( frame ) | ||
return Template( frame, "ansiPercent" ); | return Template( frame, "ansiPercent" ); | ||
end | end | ||
p.formatURL = function ( frame ) | p.formatURL = function ( frame ) | ||
return Template( frame, "formatURL" ); | return Template( frame, "formatURL" ); | ||
end | end | ||
p.getArticleBase = function ( frame ) | p.getArticleBase = function ( frame ) | ||
return Template( frame, "getArticleBase", false, true ); | return Template( frame, "getArticleBase", false, true ); | ||
end | end | ||
p.getBaseTitle = function ( frame ) | p.getBaseTitle = function ( frame ) | ||
return Template( frame, "getBaseTitle" ); | return Template( frame, "getBaseTitle" ); | ||
end | end | ||
p.getEscapedTitle = function ( frame ) | p.getEscapedTitle = function ( frame ) | ||
return Template( frame, "getEscapedTitle" ); | return Template( frame, "getEscapedTitle" ); | ||
end | end | ||
p.getExtension = function ( frame ) | p.getExtension = function ( frame ) | ||
return Template( frame, "getExtension" ); | return Template( frame, "getExtension" ); | ||
end | end | ||
p.getFile = function ( frame ) | p.getFile = function ( frame ) | ||
return Template( frame, "getFile" ); | return Template( frame, "getFile" ); | ||
end | end | ||
p.getFragment = function ( frame ) | p.getFragment = function ( frame ) | ||
return Template( frame, "getFragment" ); | return Template( frame, "getFragment" ); | ||
end | |||
p.getInterwiki = function ( frame ) | |||
return Template( frame, "getInterwiki" ); | |||
end | |||
p.getLanguage = function ( frame ) | |||
return Template( frame, "getLanguage" ); | |||
end | end | ||
p. | p.getLinktextProblem = function ( frame ) | ||
return Template( frame, " | return Template( frame, "getLinktextProblem" ); | ||
end | end | ||
p.getNamespace = function ( frame ) | p.getNamespace = function ( frame ) | ||
| Zeile 1.737: | Zeile 1.815: | ||
end | end | ||
end | end | ||
return | return Failsafe.failsafe( since ) or ""; | ||
end | end -- p.failsafe | ||
p.WLink = function () | p.WLink = function () | ||
return WLink; | return WLink; | ||