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

De Semantic MediaWiki - Sandbox

m (Mwjames a déplacé la page Module:SMW vers Module:Smw sans laisser de redirection)
Aucun résumé des modifications
 
(16 versions intermédiaires par le même utilisateur non affichées)
Ligne 1 : Ligne 1 :
-- Module:SMW
-- Module:SMW
local p = {}
local p = {}
local concat
local dump
local print
local getArgs = require('Module:Arguments').getArgs


-- Return results
-- Return results
function p.ask(frame)
function p.ask(frame)


     if not mw.ext.smw then
     if not mw.smw then
         return "mw.ext.smw module not found"
        return "mw.smw module not found"
    end
 
    if frame.args[1] == nil then
         return "no parameter found"
    end
 
    local queryResult = mw.smw.ask( frame.args )
 
    if queryResult == nil then
        return "(no values)"
    end
 
    if type( queryResult ) == "table" then
    return '<pre>' .. dump(queryResult) .. '</pre>'
    end
 
    return queryResult
end
 
 
function p.getQueryResult(frame)
 
    if not mw.smw then
        return "mw.smw module not found"
     end
     end


Ligne 12 : Ligne 41 :
         return "no parameter found"
         return "no parameter found"
     else
     else
         queryResult = mw.ext.smw.getQueryResult( frame.args[1] )
         queryResult = mw.smw.getQueryResult( frame.args )
     end
     end


Ligne 19 : Ligne 48 :
     end
     end


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


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


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


Ligne 32 : Ligne 61 :
         return "no parameter found"
         return "no parameter found"
     else
     else
         type = mw.ext.smw.getPropertyType( frame.args[1] )
         type = mw.smw.getPropertyType( frame.args[1] )
     end
     end


Ligne 40 : Ligne 69 :


     return type
     return type
end
-- set with return results
function p.set( frame )
    if not mw.smw then
        return "mw.smw module not found"
    end
    local result = mw.smw.set( frame.args )
    if result == true then
        return 'Your data was stored successfully'
    else
        return 'An error occurred during the storage process. Message reads ' .. result.error
    end
end
function p.subobject( frame )
if not mw.smw then
return "mw.smw module not found"
end
   
local args = getArgs(frame)
local subobjectId
    if args.subobjectId or args.SubobjectId then
        subobjectId = args.subobjectId or args.SubobjectId
        args.subobjectId = nil
args.SubobjectId = nil
    end
local result = mw.smw.subobject( args, subobjectId )
if result == true then
return 'Your data was stored successfully in a subobject\n<pre>' .. mw.dumpObject(args) .. '</pre>'
else
return 'An error occurred during the subobject storage process. Message reads ' .. result.error
end
end
function p.info( frame )
if not mw.smw then
return "mw.smw module not found"
end
if frame.args[1] == nil then
return "no parameter found"
end
return mw.smw.info( frame.args[1], frame.args[2] )
end
--- Concatenates a variable number of strings and numbers to one single string
-- ignores tables, bools, functions, and such and replaces them with the empty string
--
-- What is the benefit of using variable.concat instead of the .. operator?
-- Answer: .. throws an error, when trying to concat bools, tables, functions, etc.
-- This here handels them by converting them to an empty string
--
-- @param ... varaibles to concatenate
--
-- @return string
concat = function(...)
    local args = {...}
    if #args == 0 then
        error('you must supply at least one argument to \'concat\' (got none)')
    end
    local firstArg = table.remove(args, 1)
    if type(firstArg) == 'string' or type(firstArg) == 'number' then
        firstArg = print(firstArg)
    else
        firstArg = ''
    end
    if #args == 0 then
        return firstArg
    else
        return firstArg .. concat(unpack(args))
    end
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
dump = function(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 = concat(subtable, '\n', indent, k, ': ', 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 '' ) .. print(entity)
    elseif type( entity ) == 'string' then
        entity = mw.ustring.gsub(mw.ustring.gsub(entity, "\\'", "'"), "'", "\\'")
        return concat(omitType or '(string) ', '\'', entity, '\'')
    else
        -- number value expected
        return concat(omitType or '(' .. type( entity ) .. ') ', entity)
    end
end
--- This function prints a variable depending on its type:
-- * tables get concatenated by a comma
-- * bools get printed as true or false
-- * strings and numbers get simple returned as string
-- * functions and nils return as emtpy string
-- @return string
print = function(v)
    if type( v ) == 'table' then
        return table.concat(v, ',')
    elseif type( v ) == 'boolean' then
        return ( v and 'true' or 'false' )
    elseif type(v) == 'string' or type(v) == 'number' then
        return tostring(v)
    else
        return ''
    end
end
end


return p
return p

Dernière version du 19 janvier 2017 à 17:19

-- Module:SMW
local p = {}
local concat
local dump
local print
local getArgs = require('Module:Arguments').getArgs


-- 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"
    end

    local queryResult = mw.smw.ask( frame.args )

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

    if type( queryResult ) == "table" then
	    return '<pre>' .. dump(queryResult) .. '</pre>'
    end

    return queryResult
end


function p.getQueryResult(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.getPropertyType(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

-- set with return results
function p.set( frame )

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

    local result = mw.smw.set( frame.args )
    if result == true then
        return 'Your data was stored successfully'
    else
        return 'An error occurred during the storage process. Message reads ' .. result.error
    end
end

function p.subobject( frame )

	if not mw.smw then
		return "mw.smw module not found"
	end
    
	local args = getArgs(frame)
	local subobjectId
    if args.subobjectId or args.SubobjectId then
        subobjectId = args.subobjectId or args.SubobjectId
        args.subobjectId = nil
		args.SubobjectId = nil
    end
	local result = mw.smw.subobject( args, subobjectId )
	if result == true then
		return 'Your data was stored successfully in a subobject\n<pre>' .. mw.dumpObject(args) .. '</pre>'
	else
		return 'An error occurred during the subobject storage process. Message reads ' .. result.error
	end
end

function p.info( frame )

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

	if frame.args[1] == nil then
		return "no parameter found"
	end

	return mw.smw.info( frame.args[1], frame.args[2] )
end

--- Concatenates a variable number of strings and numbers to one single string
-- ignores tables, bools, functions, and such and replaces them with the empty string
--
-- What is the benefit of using variable.concat instead of the .. operator?
-- Answer: .. throws an error, when trying to concat bools, tables, functions, etc.
-- This here handels them by converting them to an empty string
--
-- @param ... varaibles to concatenate
--
-- @return string
concat = function(...)
    local args = {...}
    if #args == 0 then
        error('you must supply at least one argument to \'concat\' (got none)')
    end
    local firstArg = table.remove(args, 1)
    if type(firstArg) == 'string' or type(firstArg) == 'number' then
        firstArg = print(firstArg)
    else
        firstArg = ''
    end
    if #args == 0 then
        return firstArg
    else
        return firstArg .. concat(unpack(args))
    end
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
dump = function(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 = concat(subtable, '\n', indent, k, ': ', 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 '' ) .. print(entity)
    elseif type( entity ) == 'string' then
        entity = mw.ustring.gsub(mw.ustring.gsub(entity, "\\'", "'"), "'", "\\'")
        return concat(omitType or '(string) ', '\'', entity, '\'')
    else
        -- number value expected
        return concat(omitType or '(' .. type( entity ) .. ') ', entity)
    end
end

--- This function prints a variable depending on its type:
-- * tables get concatenated by a comma
-- * bools get printed as true or false
-- * strings and numbers get simple returned as string
-- * functions and nils return as emtpy string
-- @return string
print = function(v)
    if type( v ) == 'table' then
        return table.concat(v, ',')
    elseif type( v ) == 'boolean' then
        return ( v and 'true' or 'false' )
    elseif type(v) == 'string' or type(v) == 'number' then
        return tostring(v)
    else
        return ''
    end
end

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