Modul:Vorlage:DNB-Portal

aus Wikipedia, der freien Enzyklopädie
Zur Navigation springen Zur Suche springen

Die Dokumentation für dieses Modul kann unter Modul:Vorlage:DNB-Portal/Doku erstellt werden

--[=[ 2015-05-25
{{DNB-Portal}}
Requires:
    Module:TemplUtl
    Module:TemplatePar
    Module:URIutil
    Module:WLink
]=]



local TemplUtl;
local Skate = "[[Kategorie:Wikipedia:Vorlagenfehler/Vorlage:%s/%s]]";



local function flat( arg )
    -- Trim and nullify value
    --     arg  -- string, with value, or not
    -- Returns no-empty string, or false
    local r;
    if type( value ) == "string" then
        r = mw.text.trim( r );
        if r == "" then
            r = false;
        end
    end
    return r;
end -- flat()



local function fault( alert )
    -- Format message with class="error"
    --     alert  -- string, with message
    -- Returns message with markup
    return string.format( "<span class=\"error\">%s</span>", alert );
end -- fault()



local furnishNormdaten = function ( above )
    -- Retrieve template parameters
    -- Precondition:
    --     above  -- object; Title of current top page
    --     TemplUtl has been loaded.
    -- Postcondition:
    --     Returns table with information, or false
    local story = above:getContent();
    local joint = TemplUtl.find( story, "Normdaten" );
    local r;
    if joint then
        story = mw.ustring.sub( story, joint );
        if story then
            r = { };
            r.GND = TemplUtl.fetch( story, "GND" );
        end
    end
    return r;
end -- furnishNormdaten()



local dnbPortal = function ( args, frame )
    -- Vorlage:DNB-Portal
    -- Precondition:
    --     args    -- Parameter der Vorlageneinbindung
    --     frame   -- object, or nil
    local params =
               {  optional = { "1", "2", "3", "NAME", "TEXT", "TYP" }  };
    local lucky, r, scream, title;
    if frame then
        local TemplatePar;
        lucky, TemplatePar = pcall( require, "Module:TemplatePar" );
        if type( TemplatePar ) == "table" then
            TemplatePar  = TemplatePar.TemplatePar();
            params.cat   =  string.format( Skate,
                                           "DNB-Portal",
                                           "Parametername" );
            params.errNS = "0";
            scream       = TemplatePar().check( params );
        else
            scream = "Modul TemplatePar fehlt";
        end
    end
    if not scream then
        local sign = flat( args[ 1 ] );
        local URIutil;
        lucky, URIutil = pcall( require, "Module:URIutil" );
        if type( URIutil ) == "table" then
            URIutil = URIutil.URIutil();
            if not sign then
                lucky, TemplUtl = pcall( require, "Module:TemplUtl" );
                if type( TemplUtl ) == "table" then
                    local normdaten;
                    TemplUtl  = TemplUtl.TemplUtl();
                    title     = mw.title.getCurrentTitle();
                    normdaten = furnishNormdaten( title );
                    if normdaten then
                        sign = normdaten.GND;
                        if not sign then
                            scream = "Normdaten ohne GND";
                        end
                    else
                        scream = "Ohne Vorlage:Normdaten";
                    end
                    if sign == nil  and  title.namespace == 0 then
                        local entity = mw.wikibase.getEntityObject();
                        if type( entity ) == "table" then
                            local value = entity.claims.P227;
                            if flat( value ) then
                                scream = "Ohne Normdaten, mit Wikidata";
                            end
                        end
                    end
                else
                    scream = "Modul TemplUtl fehlt";
                end
            end
            if sign then
                local story = flat( args[ 3 ] );
                local stuff = flat( args[ 2 ] );
                if args.TEXT then
                    if story then
                        if not scream then
                            scream = "Parameterkonflikt";
                        end
                    else
                        story = args.TEXT;
                    end
                end
                if args.TYP then
                    if story then
                        if not scream then
                            scream = "Parameterkonflikt";
                        end
                    else
                        story = args.TYP;
                    end
                end
                if args.NAME then
                    if stuff then
                        if not scream then
                            scream = "Parameterkonflikt";
                        end
                    else
                        stuff = args.NAME;
                    end
                end
                if not story then
                    story = "Literatur von und über";
                end
                if not stuff then
                    lucky, WLink = pcall( require, "Module:WLink" );
                    if type( WLink ) == "table" then
                        WLink = WLink.WLink();
                        stuff = WLink.getArticleBase();
                    else
                        scream = "Modul WLink fehlt";
                    end
                end
                if stuff then
                    story = string.format( "%s %s", story, stuff );
                    r     = URIutil.linkDNBopac( sign, story );
                    if r then
                        r = r .. [==[ im Katalog der [[Deutsche Nationalbibliothek|Deutschen Nationalbibliothek]]]]==];
                    else
                        scream = "GND falsch";
                    end
                end
            end
        else
            scream = "Modul URIutil fehlt";
        end
    end
    if scream then
        if not r then
            r = fault( scream );
        end
        if title  and  title.namespace == 0 then
            r = r .. string.format( Skate, "DNB-Portal", scream );
        end
    end
    return r;
end -- dnbPortal()



-- Export
local p = { };

p.test = function ( about, args )
    local r;
    if about == "dnbPortal" then
        r = dnbPortal( args );
    end
    return r;
end

p.dnbPortal = function ( frame )
    return dnbPortal( frame:getParent().args, frame );
end

return p