« Module:Smw » : différence entre les versions

De Semantic MediaWiki - Sandbox

Aucun résumé des modifications
Aucun résumé des modifications
Ligne 12 : Ligne 12 :
         return "no parameter found"
         return "no parameter found"
     else
     else
         queryResult = mw.smw.getQueryResult( frame.args[1] )
         queryResult = mw.smw.getQueryResult( frame.args )
     end
     end


Ligne 19 : Ligne 19 :
     end
     end


     return queryResult
     return '<pre>' .. dump(queryResult) .. '</pre>'
end
end


Ligne 40 : Ligne 40 :


     return type
     return type
end
--- This dumps the variable (converts it into a string representation of itself)
--
-- @param entity mixed, value to dump
-- @param indent string, can bu used to set an indentation
-- @param omitType bool, set to true to omit the (<TYPE>) in front of the value
--
-- @return string
function dump(entity, indent, omitType)
    local entity = entity
    local indent = indent and indent or ''
    local omitType = omitType
    if type( entity ) == 'table' then
        local subtable
        if not omitType then
            subtable = '(table)[' .. #entity .. ']:'
        end
        indent = indent .. '\t'
        for k, v in pairs( entity ) do
            subtable = variable.concat(subtable, '\n', indent, k, ': ', variable.dump(v, indent, omitType))
        end
        return subtable
    elseif type( entity ) == 'nil' or type( entity ) == 'function' or type( entity ) == 'boolean' then
        return ( not omitType and '(' .. type(entity) .. ') ' or '' ) .. variable.print(entity)
    elseif type( entity ) == 'string' then
        entity = mw.ustring.gsub(mw.ustring.gsub(entity, "\\'", "'"), "'", "\\'")
        return variable.concat(omitType or '(string) ', '\'', entity, '\'')
    else
        -- number value expected
        return variable.concat(omitType or '(' .. type( entity ) .. ') ', entity)
    end
end
end


return p
return p

Version du 18 janvier 2017 à 14:49

-- Module:SMW
local p = {}

-- Return results
function p.ask(frame)

    if not mw.smw then
        return "mw.smw module not found"
    end

    if frame.args[1] == nil then
        return "no parameter found"
    else
        queryResult = mw.smw.getQueryResult( frame.args )
    end

    if queryResult == nil then
        return "(no values)"
    end

    return '<pre>' .. dump(queryResult) .. '</pre>'
end

-- Return property type
function p.type(frame)

    if not mw.smw then
        return "mw.smw module not found"
    end

    if frame.args[1] == nil then
        return "no parameter found"
    else
        type = mw.smw.getPropertyType( frame.args[1] )
    end

    if type == nil then
        return "(no values)"
    end

    return type
end

--- This dumps the variable (converts it into a string representation of itself)
--
-- @param entity mixed, value to dump
-- @param indent string, can bu used to set an indentation
-- @param omitType bool, set to true to omit the (<TYPE>) in front of the value
--
-- @return string
function dump(entity, indent, omitType)
    local entity = entity
    local indent = indent and indent or ''
    local omitType = omitType
    if type( entity ) == 'table' then
        local subtable
        if not omitType then
            subtable = '(table)[' .. #entity .. ']:'
        end
        indent = indent .. '\t'
        for k, v in pairs( entity ) do
            subtable = variable.concat(subtable, '\n', indent, k, ': ', variable.dump(v, indent, omitType))
        end
        return subtable
    elseif type( entity ) == 'nil' or type( entity ) == 'function' or type( entity ) == 'boolean' then
        return ( not omitType and '(' .. type(entity) .. ') ' or '' ) .. variable.print(entity)
    elseif type( entity ) == 'string' then
        entity = mw.ustring.gsub(mw.ustring.gsub(entity, "\\'", "'"), "'", "\\'")
        return variable.concat(omitType or '(string) ', '\'', entity, '\'')
    else
        -- number value expected
        return variable.concat(omitType or '(' .. type( entity ) .. ') ', entity)
    end
end

return p
Les cookies nous aident à fournir nos services. En utilisant nos services, vous acceptez notre utilisation de cookies.