Modul:URL
Erscheinungsbild
Vorlagenprogrammierung | Diskussionen | Lua | Unterseiten | ||
Modul | Deutsch
|
Modul: | Dokumentation |
Diese Seite enthält Code in der Programmiersprache Lua. Einbindungszahl Cirrus
--[=[ Version: 2022-11-25
URL-Object
Author: Vollbracht
Constructor: URL:new(<external link string>)
Fields:
full: addressable link string as given in new() parameter
protocol: (optional, default: empty) http, ftp, etc.
server: (optional, default: empty) de, www, www2, etc.; access default
server (for https e.g.) at given site if void
domain: (mandantory) dns portion of a link string: wikipedia.org e. g.
path: (optional, default: empty, i. e. www root for https e. g.)
/wiki or /wiki/Wikipedia:Lua/Modul/Wikidata/Time e. g.
file: (optional, default: empty, i. e. index.html e. g. depending on
server configuration) index.php e. g.
parameters: (optional, default: empty) portion after ?:
title=Modul:URL&action=edit&redlink=1 e. g.
portion: (optional, default: empty, not in combination with parameters)
#chapter_5 e.g.
methods:
relative() new URL object ### still to be implemented
]=]
local eTLDs = {'tw', 'uk'}
service = {
toString = function(this)
return this.full
end
}
--[[
URL:new()
]]
function service:new(link)
if not link then return nil end
mw.log('link')
local result = {}
if type(link) == "string" then
mw.log('string')
result.full = link:match('^%S+')
local i = result.full:find('://', nil, true)
if i then
mw.log('protocol')
result.protocol = result.full:sub(1, i-1)
link = link:sub(i+3)
end
i = link:find('%/')
local file = ''
if i then
result.server = link:sub(1, i-1)
link = link:sub(i+1)
i = link:find('?', nil, true)
if i > 1 then
result.params = link:sub(i+1)
result.file = link:sub(1, i-1)
else
i = link:find('#')
if i then
result.portion = link:sub(i+1)
result.file = link:sub(1, i-1)
else
result.file = link
end
end
else
i = link:find('?', nil, true)
if i then
result.params = link:sub(i+1)
result.server = link:sub(1, i-1)
else
i = link:find('#')
if i then
result.portion = link:sub(i+1)
result.server = link:sub(1, i-1)
else
result.server = link
end
end
end
local DNPs = mw.text.split(result.server, '%.')
if #DNPs == 1 then
result.domain = 'nic.' .. DNPs[1]
result.TLD = NDPs[1]
else
local TLDi = #DNPs
result.TLD = DNPs[#DNPs]
for _, eTLD in ipairs(eTLDs) do
if eTLD == result.TLD then
TLDi = TLDi - 1
result.TLD = DNPs[TLDi] .. '.' .. result.TLD
end
end
if TLDi == 1 then
result.domain = 'nic.' .. result.TLD
elseif TLDi == 2 then
result.domain = result.server
else
result.serverName = DNPs[1]
result.domain = DNPs[TLDi - 1] .. '.' .. result.TLD
if TLDi > 3 then
i = 3
result.subdomain = DNPs[2]
while i < TLDi-2 do
result.subdomain = result.subdomain .. '.' .. DNPs[i]
i = i + 1
end
end
end
end
elseif type(link) == 'table' then
if not link.domain then return nil end
result = link
if not link.full then
link.full = ''
if link.protocol then
result.full = link.protocol .. '://'
end
if link.server then result.full = result.full .. link.server
else
if link.serverName then
result.full = result.full .. link.serverName .. '.'
if link.subdomain then
result.full = result.full .. link.subdomain .. '.'
end
end
result.full = result.full .. link.domain
end
if link.file then result.full = result.full .. link.file end
if link.parameters then
result.full = result.full .. '?' .. link.parameters
elseif link.portion then
result.full = result.full .. '#' .. link.portion
end
end
else
return nil
end
setmetatable(result, self)
self.__index = self
return result
end
service.test = function(frame)
local p = frame.args[1]
if not p then
p = ''
for k, v in pairs(frame.args) do
p = p .. k .. '=' .. v
end
end
local u = service:new(p)
mw.logObject(u)
end
return service