Herzlich Willkommen zum Mitglieder-Wiki von Wikimedia Österreich.
Für ein Benutzerkonto schreibe bitte an verein [at] wikimedia.at
Modul:WLink: Unterschied zwischen den Versionen
Zur Navigation springen
Zur Suche springen
2018-09-23 Special:Diff/181178730/next
K (1 Version: Geo-Vorlagenergänzung) |
(2018-09-23 Special:Diff/181178730/next) |
||
Zeile 1: | Zeile 1: | ||
local WLink = { suite = "WLink", | local WLink = { suite = "WLink", | ||
serial = " | serial = "2018-09-23", | ||
item = 19363224 }; | |||
--[=[ | --[=[ | ||
ansiPercent() | ansiPercent() | ||
Zeile 6: | Zeile 7: | ||
getArticleBase() | getArticleBase() | ||
getBaseTitle() | getBaseTitle() | ||
getEscapedTitle() | |||
getExtension() | getExtension() | ||
getFile() | getFile() | ||
Zeile 11: | Zeile 13: | ||
getLanguage() | getLanguage() | ||
getNamespace() | getNamespace() | ||
getNamespaced() | |||
getPlain() | getPlain() | ||
getProject() | getProject() | ||
getTarget() | getTarget() | ||
getTalkPage() | |||
getTargetPage() | getTargetPage() | ||
getTitle() | getTitle() | ||
getWeblink() | getWeblink() | ||
getWikilink() | |||
isBracketedLink() | isBracketedLink() | ||
isBracketedURL() | isBracketedURL() | ||
Zeile 24: | Zeile 29: | ||
isInterwiki() | isInterwiki() | ||
isMedia() | isMedia() | ||
isTalkPage() | |||
isTitledLink() | isTitledLink() | ||
isValidLink() | isValidLink() | ||
isWikilink() | isWikilink() | ||
pageTarget() | |||
wikilink() | wikilink() | ||
failsafe() | |||
]=] | ]=] | ||
Zeile 56: | Zeile 64: | ||
return URLutil; | return URLutil; | ||
end -- utilURL() | end -- utilURL() | ||
local cleanWikilink = function ( access ) | |||
-- Refine wikilink spacing and decode | |||
-- Precondition: | |||
-- access -- string, with presumable link | |||
-- Postcondition: | |||
-- Returns string, with pretty target | |||
local r = access:gsub( "_", " " ) | |||
:gsub( " ", " " ) | |||
:gsub( " ", " " ) | |||
:gsub( " ", " " ) | |||
:gsub( " ", " " ) | |||
:gsub( "%s+", " " ); | |||
r = mw.text.decode( r ); | |||
return r; | |||
end -- cleanWikilink() | |||
Zeile 186: | Zeile 212: | ||
r1 = false; | r1 = false; | ||
else | else | ||
r1 = | r1 = cleanWikilink( r1 ); | ||
end | end | ||
end | end | ||
Zeile 200: | Zeile 220: | ||
local prefix = function ( ask | local prefix = function ( ask ) | ||
-- Interprete prefix of language or project type | -- Interprete prefix of language or project type | ||
-- Precondition: | -- Precondition: | ||
-- ask -- string, with presumable prefix | -- ask -- string, with presumable prefix | ||
-- Postcondition: | -- Postcondition: | ||
-- Returns string,string or nil | -- Returns string,string or nil | ||
Zeile 239: | Zeile 258: | ||
}; | }; | ||
local s = mw.text.trim( ask ); | local s = mw.text.trim( ask ); | ||
if s | if s ~= "" then | ||
local p; | local p; | ||
s = s:lower(); | s = s:lower(); | ||
Zeile 268: | Zeile 282: | ||
function | local target = function ( attempt, lonely ) | ||
-- Retrieve first target (wikilink or URL), or entire string | -- Retrieve first target (wikilink or URL), or entire string | ||
-- Precondition: | -- Precondition: | ||
Zeile 295: | Zeile 309: | ||
function WLink.ansiPercent( attempt ) | function WLink.ansiPercent( attempt, alter ) | ||
-- Convert string by ANSI encoding rather than UTF-8 encoding | -- Convert string by ANSI encoding rather than UTF-8 encoding | ||
-- Precondition: | -- Precondition: | ||
-- attempt -- string, with presumable ANSI characters | -- attempt -- string, with presumable ANSI characters | ||
-- alter -- string or nil, to use for spaces instead of %20 | |||
-- Postcondition: | -- Postcondition: | ||
-- Returns string, encoded | -- Returns string, encoded | ||
local k, s; | local k, s; | ||
local r = attempt; | local r = attempt; | ||
for i = mw.ustring.len( | if alter then | ||
k = mw.ustring.codepoint( | r = r:gsub( " ", alter ); | ||
end | |||
for i = mw.ustring.len( r ), 1, -1 do | |||
k = mw.ustring.codepoint( r, i, i ); | |||
if k <= 32 or k > 126 then | if k <= 32 or k > 126 then | ||
if k > 255 then | if k > 255 then | ||
s = mw.ustring.sub( | s = mw.ustring.sub( r, i, i ); | ||
if k > 2047 then | if k > 2047 then | ||
s = string.format( "%%%2X%%%2X%%%2X", | s = string.format( "%%%2X%%%2X%%%2X", | ||
Zeile 322: | Zeile 340: | ||
end | end | ||
r = string.format( "%s%s%s", | r = string.format( "%s%s%s", | ||
mw.ustring.sub( | mw.ustring.sub( r, 1, i - 1 ), | ||
s, | s, | ||
mw.ustring.sub( r, i + 1 ) ); | mw.ustring.sub( r, i + 1 ) ); | ||
Zeile 429: | Zeile 447: | ||
return r; | return r; | ||
end -- WLink.getBaseTitle() | end -- WLink.getBaseTitle() | ||
function WLink.getEscapedTitle( attempt ) | |||
-- Retrieve escaped link title | |||
-- Precondition: | |||
-- attempt -- string, with presumable link title | |||
-- Postcondition: | |||
-- Returns string, with suitable link title | |||
local s = mw.text.trim( attempt ); | |||
return s:gsub( "\n", " " ) | |||
:gsub( "%[", "[" ) | |||
:gsub( "%]", "]" ) | |||
:gsub( "|", "|" ); | |||
end -- WLink.getEscapedTitle() | |||
Zeile 574: | Zeile 607: | ||
return r; | return r; | ||
end -- WLink.getNamespace() | 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: | |||
-- Returns page prefixed by namespace, | |||
-- false if invalid | |||
local r = false; | |||
local s = type( area ); | |||
local room; | |||
if s == "string" then | |||
room = mw.site.namespaces[ tonumber( area ) or area ]; | |||
elseif s == "number" then | |||
room = mw.site.namespaces[ area ]; | |||
end | |||
if room then | |||
local m; | |||
s, m = WLink.getTarget( attempt ); | |||
if not s then | |||
s = attempt; | |||
elseif m ~= 2 then | |||
s = false; | |||
end | |||
if s then | |||
local w = WLink.wikilink( s ); | |||
if w and not w.lang and not w.project and | |||
( not w.ns or w.ns == room.id ) then | |||
r = string.format( "%s:%s", | |||
room.name, w.title ); | |||
end | |||
end | |||
end | |||
return r; | |||
end -- WLink.getNamespaced() | |||
Zeile 585: | Zeile 656: | ||
local r = attempt; | local r = attempt; | ||
local i = 1; | local i = 1; | ||
local j, k, n, lean, s, shift, space, suffix; | local j, k, n, lean, s, shift, span, space, suffix; | ||
while ( true ) do | while ( true ) do | ||
j = r:find( "[", i, true ); | j = r:find( "[", i, true ); | ||
Zeile 606: | Zeile 677: | ||
if lean then | if lean then | ||
s, shift = extractWikilink( suffix ); | s, shift = extractWikilink( suffix ); | ||
space = s:match( "^([^:]+):" ) | if s then | ||
space = s:match( "^([^:]+):" ); | |||
if space then | if space then | ||
space = space.id; | space = mw.site.namespaces[ space ]; | ||
if space then | |||
space = space.id; | |||
end | |||
end | |||
if space == 6 or space == 14 then | |||
shift = ""; | |||
elseif not shift then | |||
shift = s; | |||
end | end | ||
else | |||
s = ""; | |||
shift = ""; | shift = ""; | ||
end | end | ||
else | else | ||
span, shift = extractExtlink( suffix ); | |||
if not shift then | if span then | ||
shift = ""; | if not shift then | ||
shift = ""; | |||
end | |||
else | |||
shift = string.format( "[%s]", s ); | |||
end | end | ||
i = i - 1; | i = i - 1; | ||
Zeile 639: | Zeile 719: | ||
break; -- while true | break; -- while true | ||
end | end | ||
end -- while true | end -- while true | ||
return r; | return r; | ||
end -- WLink.getPlain() | end -- WLink.getPlain() | ||
Zeile 662: | Zeile 742: | ||
return r; | return r; | ||
end -- WLink.getProject() | end -- WLink.getProject() | ||
function WLink.getTalkPage( attempt ) | |||
-- Retrieve talk page name for attempt, or that page name itself | |||
-- Precondition: | |||
-- attempt -- string, with presumable link somewhere | |||
-- Postcondition: | |||
-- Returns string or false | |||
local r = false; | |||
local s, m = WLink.getTarget( attempt ); | |||
if m ~= 2 and attempt then | |||
s = mw.text.trim( attempt ); | |||
end | |||
if s and s ~= "" then | |||
local w = mw.title.new( s ); | |||
if w then | |||
w = w.talkPageTitle; | |||
if w then | |||
r = w.prefixedText; | |||
end | |||
end | |||
end | |||
return r; | |||
end -- WLink.getTalkPage() | |||
Zeile 759: | Zeile 864: | ||
function WLink.getWeblink( attempt, anURLutil ) | function WLink.getWeblink( attempt, anURLutil ) | ||
-- Retrieve bracketed link from URL | -- Retrieve bracketed link from resource URL | ||
-- Precondition: | -- Precondition: | ||
-- attempt -- string, with URL, or something different | -- attempt -- string, with URL, or something different | ||
Zeile 766: | Zeile 871: | ||
-- Returns string, with first detected link target | -- Returns string, with first detected link target | ||
-- false if nothing found | -- false if nothing found | ||
local second = ".ac.co.gv.or."; | local second = ".ac.co.go.gv.or."; | ||
local r; | local r; | ||
if type( anURLutil ) == "table" then | if type( anURLutil ) == "table" then | ||
Zeile 774: | Zeile 879: | ||
end | end | ||
if URLutil.isResourceURL( attempt ) then | if URLutil.isResourceURL( attempt ) then | ||
local | local site = URLutil.getAuthority( attempt ); | ||
local service = attempt; | |||
local show; | |||
if #attempt == #site then | |||
site = site .. "/"; | |||
end | |||
show = URLutil.getTop3domain( "//" .. site ); | |||
if show then | if show then | ||
local scan = "[%./](%a[%a%%%-]*%a)(%.%l%l%.)(%a+)$"; | |||
local search = "." .. show; | local search = "." .. show; | ||
local s1, s2, s3 = search:match( scan ); | local s1, s2, s3 = search:match( scan ); | ||
if s2 then | if s2 then | ||
if not second:find( s2, 1, true ) then | if not second:find( s2, 1, true ) then | ||
show = string.format( "%s | show = string.format( "%s%s", s2:sub( 2 ), s3 ); | ||
end | end | ||
else | else | ||
Zeile 788: | Zeile 899: | ||
end | end | ||
if not show then | if not show then | ||
show = URLutil.getTop2domain( | show = URLutil.getTop2domain( "//" .. site ); | ||
if not show then | |||
show = URLutil.getHost( "//" .. site ); | |||
end | |||
end | |||
if not service:match( "^[a-z:]*//.+/" ) then | |||
service = service .. "/"; | |||
end | end | ||
r = string.format( "[%s %s]", | r = string.format( "[%s %s]", service, show ); | ||
else | else | ||
r = attempt; | r = attempt; | ||
Zeile 796: | Zeile 913: | ||
return r; | return r; | ||
end -- WLink.getWeblink() | end -- WLink.getWeblink() | ||
function WLink.getWikilink( attempt, appear ) | |||
-- Retrieve bracketed link from text | |||
-- Precondition: | |||
-- attempt -- string, with current target, or plain | |||
-- appear -- string, with link title, or nil | |||
-- Postcondition: | |||
-- Returns string, with first detected link target | |||
-- false if nothing found | |||
local r = WLink.pageTarget( attempt ); | |||
if r then | |||
if appear then | |||
local show = WLink.getEscapedTitle( appear ); | |||
if show ~= r and show ~= "" then | |||
r = string.format( "%s|%s", r, show ); | |||
end | |||
end | |||
r = string.format( "[[%s]]", r ); | |||
end | |||
return r; | |||
end -- WLink.getWikilink() | |||
Zeile 929: | Zeile 1.069: | ||
return r; | return r; | ||
end -- WLink.isMedia() | end -- WLink.isMedia() | ||
function WLink.isTalkPage( attempt ) | |||
-- Does attempt describe a talk page? | |||
-- Precondition: | |||
-- attempt -- string, with presumable link somewhere | |||
-- Postcondition: | |||
-- Returns boolean | |||
local r = false; | |||
local s, m = WLink.getTarget( attempt ); | |||
if m ~= 2 and attempt then | |||
s = mw.text.trim( attempt ); | |||
end | |||
if s and s ~= "" then | |||
local w = mw.title.new( s ); | |||
if w then | |||
r = w.isTalkPage; | |||
end | |||
end | |||
return r; | |||
end -- WLink.isTalkPage() | |||
Zeile 986: | Zeile 1.148: | ||
return ( m == 2 ); | return ( m == 2 ); | ||
end -- WLink.isWikilink() | end -- WLink.isWikilink() | ||
function WLink.pageTarget( attempt ) | |||
-- Create standardized wikilink target of a page | |||
-- Precondition: | |||
-- attempt -- string, with presumable link | |||
-- expected to be enclosed in "[[" "]]" | |||
-- else wikilink | |||
-- Postcondition: | |||
-- Returns string with link target | |||
local p = WLink.wikilink( attempt ); | |||
local r; | |||
if p then | |||
local lead; | |||
if p.ns then | |||
if p.lang or | |||
( p.project and ( p.project == "commons" or | |||
p.project == "d" or | |||
p.project == "meta" or | |||
p.project == "mw" ) ) then | |||
p.space = mw.site.namespaces[ p.ns ].canonicalName; | |||
end | |||
lead = ( p.ns == 6 or p.ns == 14 ); | |||
end | |||
if p.lang then | |||
lead = true; | |||
end | |||
if p.project then | |||
r = p.project .. ":"; | |||
elseif lead then | |||
r = ":"; | |||
else | |||
r = ""; | |||
end | |||
if p.lang then | |||
r = string.format( "%s%s:", r, p.lang ); | |||
end | |||
if p.space then | |||
r = string.format( "%s%s:", r, p.space ); | |||
end | |||
if p.title then | |||
r = r .. p.title; | |||
end | |||
if r == "" then | |||
r = false; | |||
end | |||
end | |||
if not r then | |||
r = string.format( ":%s:", | |||
mw.language.getContentLanguage():getCode() ); | |||
end | |||
return r; | |||
end -- WLink.pageTarget() | |||
Zeile 997: | Zeile 1.213: | ||
-- Postcondition: | -- Postcondition: | ||
-- Returns table or false | -- Returns table or false | ||
-- table of assignments with { type, value} | -- table of assignments with { type, value } | ||
-- type is one of "lead", | -- type is one of "lead", | ||
-- "project", "lang", "ns", "space", "title" | -- "project", "lang", | ||
-- "ns", "space", "title" | |||
-- false if nothing found | -- false if nothing found | ||
local s = contentWikilink( attempt ); | local s = contentWikilink( attempt or "" ); | ||
local got, n, r; | local got, n, r; | ||
if not s then | if not s then | ||
s = attempt; | s = attempt; | ||
end | |||
if s:find( "%", 1, true ) then | |||
s = mw.uri.decode( s, "PATH" ); | |||
end | end | ||
i = s:find( "|", 1, true ); | i = s:find( "|", 1, true ); | ||
Zeile 1.017: | Zeile 1.237: | ||
local j, k, o, v; | local j, k, o, v; | ||
r = { title = "" }; | r = { title = "" }; | ||
while ( got[ 1 ] == "" ) do | |||
r.lead = true; | |||
table.remove( got, 1 ); | |||
n = n - 1; | |||
end -- while got[ 1 ] == "" | |||
if n > 4 then | if n > 4 then | ||
k = 4; | k = 4; | ||
elseif n > 1 then | |||
k = n - 1; | |||
else | else | ||
k = | k = 1; | ||
end | end | ||
j = k; | j = k; | ||
Zeile 1.037: | Zeile 1.264: | ||
end -- for i | end -- for i | ||
for i = 1, j do | for i = 1, j do | ||
o, v = prefix( got[ i ] | o, v = prefix( got[ i ] ); | ||
if o then | if o then | ||
if r[ o ] then | if r[ o ] then | ||
Zeile 1.043: | Zeile 1.270: | ||
break; -- for i | break; -- for i | ||
else | else | ||
if i >= k then | |||
k = i + 1; | |||
end | |||
r[ o ] = v; | r[ o ] = v; | ||
end | end | ||
else | else | ||
if i == 1 and r.ns then | |||
r.ns = false; | |||
r.space = false; | |||
end | |||
k = i; | k = i; | ||
break; -- for i | break; -- for i | ||
end | end | ||
end -- for i | end -- for i | ||
for i = k, n do | if k > 0 then | ||
for i = k, n do | |||
r.title = r.title .. got[ i ]; | |||
if i < n then | |||
r.title = r.title .. ":"; | |||
end | |||
end -- for i | |||
end | |||
end | end | ||
r.title = cleanWikilink( r.title ); | |||
if r.lead and | if r.lead and | ||
( r.project | ( r.project or | ||
( not r.lang and r.ns ~= 6 and r.ns ~= 14 ) ) then | ( not r.lang and r.ns ~= 6 and r.ns ~= 14 ) ) then | ||
r.lead = false; | r.lead = false; | ||
Zeile 1.064: | Zeile 1.301: | ||
return r; | return r; | ||
end -- WLink.wikilink() | end -- WLink.wikilink() | ||
function WLink.failsafe( 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; | |||
if since == "wikidata" then | |||
local item = WLink.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 <= WLink.serial then | |||
r = WLink.serial; | |||
else | |||
r = false; | |||
end | |||
end | |||
return r; | |||
end -- WLink.failsafe() | |||
Zeile 1.076: | Zeile 1.350: | ||
-- Postcondition: | -- Postcondition: | ||
-- Return string; might be error message | -- Return string; might be error message | ||
local lucky = true; | local lucky = true; | ||
local s = false; | local s = false; | ||
local r = false; | local r = false; | ||
local space; | |||
for k, v in pairs( frame.args ) do | for k, v in pairs( frame.args ) do | ||
if k == 1 then | if k == 1 then | ||
Zeile 1.086: | Zeile 1.360: | ||
else | else | ||
s = mw.text.trim( v ); | s = mw.text.trim( v ); | ||
end | |||
elseif ( k == 2 and | |||
( action == "getNamespaced" or | |||
action == "getWikilink" ) ) or | |||
( k == "space" and action == "ansiPercent" ) then | |||
if v ~= "" then | |||
space = v; | |||
end | end | ||
elseif k ~= "template" then | elseif k ~= "template" then | ||
Zeile 1.099: | Zeile 1.380: | ||
if lucky then | if lucky then | ||
if s or lone then | if s or lone then | ||
lucky, r = pcall( WLink[ action ], s ); | lucky, r = pcall( WLink[ action ], s, space ); | ||
else | else | ||
r = "Parameter missing"; | r = "Parameter missing"; | ||
Zeile 1.114: | Zeile 1.395: | ||
end | end | ||
else | else | ||
local e = mw.html.create( "span" ); | |||
r = tostring( e:addClass( "error" ) | |||
:wikitext( r ) ); | |||
end | end | ||
return r; | return r; | ||
Zeile 1.135: | Zeile 1.418: | ||
p.getBaseTitle = function ( frame ) | p.getBaseTitle = function ( frame ) | ||
return Template( frame, "getBaseTitle" ); | return Template( frame, "getBaseTitle" ); | ||
end | |||
p.getEscapedTitle = function ( frame ) | |||
return Template( frame, "getEscapedTitle" ); | |||
end | end | ||
p.getExtension = function ( frame ) | p.getExtension = function ( frame ) | ||
Zeile 1.153: | Zeile 1.439: | ||
p.getNamespace = function ( frame ) | p.getNamespace = function ( frame ) | ||
return tostring( Template( frame, "getNamespace" ) ); | return tostring( Template( frame, "getNamespace" ) ); | ||
end | |||
p.getNamespaced = function ( frame ) | |||
return tostring( Template( frame, "getNamespaced" ) ); | |||
end | end | ||
p.getPlain = function ( frame ) | p.getPlain = function ( frame ) | ||
Zeile 1.159: | Zeile 1.448: | ||
p.getProject = function ( frame ) | p.getProject = function ( frame ) | ||
return Template( frame, "getProject" ); | return Template( frame, "getProject" ); | ||
end | |||
p.getTalkPage = function ( frame ) | |||
return Template( frame, "getTalkPage" ); | |||
end | end | ||
p.getTarget = function ( frame ) | p.getTarget = function ( frame ) | ||
Zeile 1.171: | Zeile 1.463: | ||
p.getWeblink = function ( frame ) | p.getWeblink = function ( frame ) | ||
return Template( frame, "getWeblink" ); | return Template( frame, "getWeblink" ); | ||
end | |||
p.getWikilink = function ( frame ) | |||
return Template( frame, "getWikilink" ); | |||
end | end | ||
p.isBracketedLink = function ( frame ) | p.isBracketedLink = function ( frame ) | ||
Zeile 1.192: | Zeile 1.487: | ||
p.isMedia = function ( frame ) | p.isMedia = function ( frame ) | ||
return Template( frame, "isMedia" ); | return Template( frame, "isMedia" ); | ||
end | |||
p.isTalkPage = function ( frame ) | |||
return Template( frame, "isTalkPage" ); | |||
end | end | ||
p.isTitledLink = function ( frame ) | p.isTitledLink = function ( frame ) | ||
Zeile 1.204: | Zeile 1.502: | ||
p.isWikilink = function ( frame ) | p.isWikilink = function ( frame ) | ||
return Template( frame, "isWikilink" ); | return Template( frame, "isWikilink" ); | ||
end | |||
p.failsafe = function ( frame ) | |||
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 WLink.failsafe( since ) or ""; | |||
end | end | ||
p.WLink = function () | p.WLink = function () |