Módulo:Mw

Origem: Wikipédia, a enciclopédia livre.
-- Wrapper for Base and Frame Object Lua functions
--   https://www.mediawiki.org/wiki/Extension:Scribunto/Lua_reference_manual#Base_functions
--   https://www.mediawiki.org/wiki/Extension:Scribunto/Lua_reference_manual#Frame_object
--
-- Not implemented
--   mw.clone
--   mw.loadData
--   mw.dumpObject
--   mw.logObject
--   frame:callParserFunction
--   frame:expandTemplate
--   frame:extensionTag
--   frame:newChild
--   frame:newParserValue
--   frame:newTemplateParserValue

f = {}

function boolean( str )
  -- Interpret string as boolean
  -- * Returns False if nil, "false" or "0"; returns True otherwise
  return not (str == nil or str == "false" or str == "0")
end

function f.allToString(frame)
  -- mw.allToString( ... )
  ret = {}
  for i, j in pairs( frame.args or {} ) do
    table.insert( ret, i .. '=' .. j )
  end
  return table.concat( ret, '|' )
end

function f.allToStringEx(frame)
  -- mw.allToString( [separator, keyseparator, hidenumbers, useparent, ] ... )
  
  separator = frame.args['separator'] or '|'
  keyseparator = frame.args['keyseparator'] or '='
  hidenumbers = boolean(frame.args['hidenumbers'])
  useparent = boolean(frame.args['useparent'])
  
  if useparent then
  	thisframe = frame:getParent()
  else
  	thisframe = frame
  end
  
  ret = {}
  nextnum = 1
  for key, value in pairs( thisframe.args or {} ) do
  	if useparent or not (key == 'hidenumbers' or key == 'useparent' or key == 'separator' or key == 'keyseparator') then
	  	if hidenumbers and key == nextnum then
	  		txt = value
	  		nextnum = nextnum + 1
	  	else
	  		txt = key .. keyseparator .. value
	  	end
	    table.insert( ret, txt )
	end
  end
  return table.concat( ret, separator )
end

function f.getCurrentFrame(frame)
  -- mw.getCurrentFrame().args
  return mw.allToString(frame)
end

function f.incrementExpensiveFunctionCount(frame)
  -- mw.incrementExpensiveFunctionCount()
  if pcall(mw.incrementExpensiveFunctionCount) then
    return ''
  else
    return 'error'
  end
end

function f.isSubsting(frame)
  -- mw.isSubsting()
  if mw.isSubsting() then
    return 'true'
  else
    return 'false'
  end
end

function f.log(frame)
  -- mw.log( ... )
  mw.log( f.allToString(frame) )
  return ''
end

function f.getParent(frame)
  -- frame:getParent().args
  ret = {}
  for i, j in pairs( frame:getParent().args or {} ) do
    table.insert( ret, i .. '=' .. j )
  end
  return table.concat( ret, '|' )
end

function f.getTitle(frame)
  -- frame:getTitle()
  return frame:getTitle()
end

function f.preprocess(frame)
  -- frame:preprocess( ... )
  ret = mw.clone(frame.args)
  return frame:preprocess( table.concat(ret, '|') )
end

function f.getArgument(frame)
  -- frame:getArgument( arg )
  arg = frame.args['arg'] or frame.args[1]
  return frame:getArgument( arg ):expand()
end

function f.argumentPairs(frame)
  -- frame:argumentPairs()
  return f.getCurrentFrame(frame)
end

return f