Zum Inhalt springen

Modul:FileMedia

aus Wikipedia, der freien Enzyklopädie
Dies ist eine alte Version dieser Seite, zuletzt bearbeitet am 23. Oktober 2013 um 21:14 Uhr durch PerfektesChaos (Diskussion | Beiträge) (+ getTitle()). Sie kann sich erheblich von der aktuellen Version unterscheiden.
Vorlagenprogrammierung Diskussionen Lua Test Unterseiten
Modul Deutsch English

Modul: Dokumentation

Diese Seite enthält Code in der Programmiersprache Lua. Einbindungszahl Cirrus

Dies ist die (produktive) Mutterversion eines global benutzten Lua-Moduls.
Wenn die serial-Information nicht übereinstimmt, müsste eine Kopie hiervon in das lokale Wiki geschrieben werden.
Versionsbezeichnung auf WikiData: 2023-01-01

--[=[ 2013-10-23
FileMedia
]=]



local FileMedia = { }



FileMedia.getExtension = function ( ask )
    -- Retrieve "file name extension" from file name
    -- Precondition:
    --     ask  -- file name; string, or nil
    -- Postcondition:
    --     Returns string, or false
    local r = false
    if ask then
        local s = ask:match( ".%.(%w+)%s*$" )
        if s then
            r = s:lower()
        end
    end
    return r
end -- FileMedia.getExtension()



FileMedia.getTitle = function ( ask )
    -- Retrieve file page title from page name
    -- Precondition:
    --     ask  -- file name; string, or nil
    -- Postcondition:
    --     Returns string, or false if not a valid file
    local r = false
    if FileMedia.isType( ask, "file" ) then
        local space, s = ask:match( "^([^:]+):(.+)$" )
        if space then
            space = mw.site.namespaces[ mw.text.trim( space ) ]
            if space then
                space = space.canonicalName
                if space == "Media"  or  space == "File" then
                    r = mw.text.trim( s )
                end
            end
        else
            r = mw.text.trim( ask )
        end
    end
    return r
end -- FileMedia.getTitle()



FileMedia.getType = function ( ask )
    -- Retrieve supposed file type from "file name extension"
    -- Precondition:
    --     ask  -- file name; string, or nil
    -- Postcondition:
    --     Returns string, or false
    --             "audio"
    --             "data"
    --             "paged"
    --             "pixel"
    --             "program"
    --             "style"
    --             "vector"
    --             "video"
    local r = false
    if ask then
        local s = FileMedia.getExtension( ask )
        if s then
            local ext = mw.loadData( "Module:FileMedia/extension" )
            if ext then
                s = ext[ s ]
                if s then
                    r = s
                end
            end
        end
    end
    return r
end -- FileMedia.getType()



FileMedia.isFile = function ( ask )
    -- Is this a valid (existing) file title or file name?
    -- Precondition:
    --     ask  -- file title or file name; string, or nil
    -- Postcondition:
    --     Returns boolean
    -- Expensive function.
    local r = false
    local s = FileMedia.getTitle( ask )
    if s then
        r = mw.title.makeTitle( "File", s ).fileExists
    end
    return r
end -- FileMedia.isFile()



FileMedia.isType = function ( ask, against )
    -- Does file name match type expectation?
    -- Precondition:
    --     ask      -- file name; string, or nil
    --     against  -- expectation; generic or "file" or "image" or any
    -- Postcondition:
    --     Returns boolean
    local r = false
    local s = FileMedia.getType( ask )
    if s then
        local scope
        if against == "file" then
            scope = "audio paged pixel vector video"
        elseif against == "image" then
            scope = "paged pixel vector"
        end
        if scope then
            if FileMedia.getExtension( ask ) ~= "doc" then
                r = scope:match( s )
            end
        else
            r = ( s == against )
        end
    end
    return r
end -- FileMedia.isType()



-- Export
local p = { }

p.getTitle = function ( frame )
    -- Retrieve file page title from page name
    --     1  -- file name
    local lucky, r = pcall( FileMedia.getTitle,
                            frame.args[ 1 ] )
    return r or ""
end -- p.getTitle



p.getType = function ( frame )
    -- Retrieve supposed file type from "file name extension"
    --     1  -- file name
    local lucky, r = pcall( FileMedia.getType,
                            frame.args[ 1 ] )
    return r or ""
end -- p.getType



p.isFile = function ( frame )
    -- Is this a valid (existing) file title or file name?
    --     1  -- file name; with or without namespace
    -- Expensive function.
    local lucky, r = pcall( FileMedia.isFile,
                            frame.args[ 1 ] )
    return r and "1" or ""
end -- p.isFile



p.isType = function ( frame )
    -- Does file name match type expectation?
    --     1  -- file name
    --     2  -- expected
    local r        = false
    local supposed = frame.args[ 2 ]
    if supposed then
        supposed = supposed:match( "^%s*(%w+)%s*$" )
        if supposed then
            local lucky
            lucky, r = pcall( FileMedia.isType,
                              frame.args[ 1 ],
                              supposed:lower() )
        end
    end
    return r and "1" or ""
end -- p.isType



p.FileMedia = function ()
    return FileMedia
end -- p.FileMedia

return p