Modul:WLink: Unterschied zwischen den Versionen

4.060 Bytes hinzugefügt ,  vor 6 Jahren
2019-01-01; gem. Anfrage Spezial:Diff/186813773/186814496
w>XenonX3
w>Mabschaaf
(2019-01-01; gem. Anfrage Spezial:Diff/186813773/186814496)
Zeile 1: Zeile 1:
local WLink = { suite  = "WLink",
local WLink = { suite  = "WLink",
                 serial = "2018-09-23",
                 serial = "2019-01-01",
                 item  = 19363224 };
                 item  = 19363224 };
--[=[
--[=[
Zeile 33: Zeile 33:
isValidLink()
isValidLink()
isWikilink()
isWikilink()
pageLink()
pageTarget()
pageTarget()
wikilink()
wikilink()
Zeile 217: Zeile 218:
     return r1, r2;
     return r1, r2;
end -- extractWikilink()
end -- extractWikilink()
local farming = function ( already )
    -- Retrieve wikifarm project information
    -- Precondition:
    --    already  -- table, with wikilink components
    -- Postcondition:
    --    Returns  table, with wikilink components extended
    local r = already;
    if not r.project then
        local codes  = { mediawiki  = "mw",
                        wikibooks  = "b",
                        wikidata    = "d",
                        wikinews    = "n",
                        wikipedia  = "w",
                        wikiquote  = "q",
                        wikisource  = "s",
                        wikiversity = "v",
                        wikivoyage  = "voy",
                        wiktionary  = "wikt" };
        local server = mw.site.server:gsub( "([/.])m%.",
                                            "%1" )
                                      :gsub( "%.beta%.wmflabs%.org$",
                                            ".org" );
        local site  = server:match( "[/.](%l+)%.org$" );
        r.project = codes[ site ];
        if r.project then
            if not r.lang  and
              r.project ~= "mw"  and  r.project ~= "d" then
                r.lang = server:match( "//(%l+)%." );
            end
        else
            site = server:match( "//(%l+)%.wikimedia%.org$" );
            if site == "commons"  or  site == "meta" then
                r.project = site;
            end
        end
    end
    if r.project  and  r.ns  and
      ( r.project == "commons"  or
        r.project == "d"        or
        r.project == "meta"    or
        r.project == "mw" ) then
        r.language = true;
    end
    return r;
end -- farming()




Zeile 354: Zeile 403:
     -- Precondition:
     -- Precondition:
     --    adjust  -- string, with URL or domain/path or bracketed link
     --    adjust  -- string, with URL or domain/path or bracketed link
    --    assure  -- boolean, true for secure HTTP
     -- Postcondition:
     -- Postcondition:
     --    Returns  string, with bracketed link
     --    Returns  string, with bracketed link
Zeile 367: Zeile 417:
             host = URLutil.getHost( adjust );
             host = URLutil.getHost( adjust );
             if not host then
             if not host then
                 url = "http://" .. adjust;
                 url = "://" .. adjust;
                if assure then
                    url = "s" .. url;
                end
                url  = "http" .. url;
                 host = URLutil.getHost( url );
                 host = URLutil.getHost( url );
             end
             end
Zeile 512: Zeile 566:
         r = find( "file" );
         r = find( "file" );
         if not r then
         if not r then
             local trsl = mw.site.namespaces[6];
             local trsl = mw.site.namespaces[ 6 ];
             r = find( trsl.name );
             r = find( trsl.name );
             if not r then
             if not r then
Zeile 1.151: Zeile 1.205:




function WLink.pageTarget( attempt )
function WLink.pageLink( attempt, appear, assure )
    -- Create safely standardized wikilink target of a page
    --    attempt  -- string, with presumable link
    --    appear  -- string or true or nil, with link title
    --    assure  -- string or nil, shield against wiki template syntax
    --                "URL" or "WIKI"
    -- Postcondition:
    --    Returns  string with link target
    local r = WLink.pageTarget( attempt, assure );
    if appear then
        local show;
        if type( appear ) == "string" then
            show = appear;
        else
            show = attempt;
        end
        r = string.format( "%s|%s", r, show );
    end
    return r;
end -- WLink.pageLink()
 
 
 
function WLink.pageTarget( attempt, assure )
     -- Create standardized wikilink target of a page
     -- Create standardized wikilink target of a page
     -- Precondition:
     -- Precondition:
Zeile 1.157: Zeile 1.234:
     --                        expected to be enclosed in "[[" "]]"
     --                        expected to be enclosed in "[[" "]]"
     --                        else wikilink
     --                        else wikilink
    --                table, of assignments with { type, value }
    --                        type is one of "lead",
    --                            "project", "lang",
    --                            "ns", "space", "title"
    --    assure  -- string or nil, shield against wiki template syntax
    --                "URL" or "WIKI"
     -- Postcondition:
     -- Postcondition:
     --    Returns  string with link target
     --    Returns  string with link target
     local p = WLink.wikilink( attempt );
     local p = type( attempt );
    local s = assure;
     local r;
     local r;
    if p == "string" then
        p = WLink.wikilink( attempt );
    elseif p == "table" then
        p = attempt;
    else
        p = false;
    end
     if p then
     if p then
        local site  = p.project;
        local slang = p.lang;
         local lead;
         local lead;
         if p.ns then
         if p.ns then
             if p.lang  or
             if not slang then
              ( p.project and ( p.project == "commons" or
                p = farming( p );
                                  p.project == "d" or
            end
                                  p.project == "meta" or
            if p.lang and
                                  p.project == "mw" ) ) then
              p.lang ~= mw.language.getContentLanguage():getCode() then
                p.language = true;
            end
            if p.language then
                 p.space = mw.site.namespaces[ p.ns ].canonicalName;
                 p.space = mw.site.namespaces[ p.ns ].canonicalName;
             end
             end
             lead = ( p.ns == 6  or  p.ns == 14 );
             lead = ( p.ns == 6  or  p.ns == 14 );
         end
         end
         if p.lang then
         if slang then
             lead = true;
             lead = true;
         end
         end
         if p.project then
         if s == "WIKI" then
             r = p.project .. ":";
            if not site  and
              ( lead  or
                ( not p.space  and
                  p.title and p.title:match( "^[*;]" ) ) ) then
                p    = farming( p );
                site  = p.project;
                slang = p.lang;
            end
            s = false;
        end
        if site then
             r = site .. ":";
         elseif lead then
         elseif lead then
             r = ":";
             r = ":";
Zeile 1.183: Zeile 1.290:
             r = "";
             r = "";
         end
         end
         if p.lang then
         if slang then
             r = string.format( "%s%s:", r, p.lang );
             r = string.format( "%s%s:", r, slang );
         end
         end
         if p.space then
         if p.space then
Zeile 1.197: Zeile 1.304:
     end
     end
     if not r then
     if not r then
         r = string.format( ":%s:",
         p = { lang = mw.language.getContentLanguage():getCode() };
                          mw.language.getContentLanguage():getCode() );
        if s == "WIKI" then
            r = WLink.pageTarget( p, s );
            s = false;
        else
            r = string.format( ":%s:", p.lang );
        end
    end
    if s == "URL"  and  r:match( "^[*#;:]" ) then
        r = mw.uri.encode( r:sub( 1, 1 ) ) ..  r:sub( 2 );
     end
     end
     return r;
     return r;
Zeile 1.253: Zeile 1.368:
             s = mw.text.trim( got[ i ] );
             s = mw.text.trim( got[ i ] );
             if s ~= "" then
             if s ~= "" then
                 o = mw.site.namespaces[ mw.text.trim( got[ i ] ) ];
                 o = mw.site.namespaces[ s ];
                 if o then
                 if o then
                     r.ns    = o.id;
                     r.ns    = o.id;
Zeile 1.353: Zeile 1.468:
     local s = false;
     local s = false;
     local r = false;
     local r = false;
     local space;
     local safe, 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.363: Zeile 1.478:
         elseif ( k == 2  and
         elseif ( k == 2  and
                 ( action == "getNamespaced"  or
                 ( action == "getNamespaced"  or
                   action == "getWikilink" ) )    or
                   action == "getWikilink"  or
                  action == "pageLink" ) )    or
               ( k == "space"  and  action == "ansiPercent" ) then
               ( k == "space"  and  action == "ansiPercent" ) then
            v = mw.text.trim( v );
             if v ~= "" then
             if v ~= "" then
                 space = v;
                 space = v;
            end
        elseif k == "safe"  and  action == "pageLink" then
            v = mw.text.trim( v );
            if v ~= "" then
                safe = v;
             end
             end
         elseif k ~= "template" then
         elseif k ~= "template" then
Zeile 1.380: Zeile 1.502:
     if lucky then
     if lucky then
         if s or lone then
         if s or lone then
             lucky, r = pcall( WLink[ action ],  s,  space );
             lucky, r = pcall( WLink[ action ],  s,  space,  safe );
         else
         else
             r = "Parameter missing";
             r = "Parameter missing";
Zeile 1.502: Zeile 1.624:
p.isWikilink = function ( frame )
p.isWikilink = function ( frame )
     return Template( frame, "isWikilink" );
     return Template( frame, "isWikilink" );
end
p.pageLink = function ( frame )
    return Template( frame, "pageLink" );
end
end
p.failsafe = function ( frame )
p.failsafe = function ( frame )
Anonymer Benutzer