Editing Module:Test

Jump to navigation Jump to search

Warning: You are not logged in. Your IP address will be publicly visible if you make any edits. If you log in or create an account, your edits will be attributed to your username, along with other benefits.

The edit can be undone. Please check the comparison below to verify that this is what you want to do, and then save the changes below to finish undoing the edit.

Latest revision Your text
Line 3: Line 3:
  
 
local function runTime()
 
local function runTime()
   return string.format("%i", os.clock() * 1000)
+
   local clock = string.format("%i", os.clock() * 1000)
 +
  return clock
 
end
 
end
  
Line 11: Line 12:
  
 
if mw then
 
if mw then
 
 
   log = mw.log
 
   log = mw.log
 
   logObject = mw.logObject
 
   logObject = mw.logObject
  
   local timeDataStart = runTime()
+
   local timeDataEnd = runTime()
 +
  log(string.format('@%ims, data loaded in %ims', timeDataEnd, timeDataEnd - timeDataStart))
 +
 
 +
  Data  = mw.loadData('Module:Test/data')
 
    
 
    
  Data  = mw.loadData('Module:Test/data')
 
 
 
   local timeDataEnd = runTime()
 
   local timeDataEnd = runTime()
 
   log(string.format('@%ims, data loaded in %ims', timeDataEnd, timeDataEnd - timeDataStart))
 
   log(string.format('@%ims, data loaded in %ims', timeDataEnd, timeDataEnd - timeDataStart))
 
+
 
 
   Util  = require("Module:Test/lib/util")
 
   Util  = require("Module:Test/lib/util")
 
   Search = require("Module:Test/lib/search")
 
   Search = require("Module:Test/lib/search")
Line 28: Line 29:
 
   log(string.format('@%ims, modules loaded', runTime()))
 
   log(string.format('@%ims, modules loaded', runTime()))
  
 +
  frame = mw and mw.getCurrentFrame()
 
else
 
else
 
+
   logStore = {}
   logDevStore = {}
 
  
 
   log = function(str)
 
   log = function(str)
     table.insert(logDevStore, str)
+
     table.insert(logStore, str)
 
   end
 
   end
  
Line 39: Line 40:
 
     if prefix then
 
     if prefix then
 
       assert(type(prefix) == "string")
 
       assert(type(prefix) == "string")
       table.insert(logDevStore, prefix .. " = " .. Inspect(obj))
+
       table.insert(logStore, prefix .. " = " .. Inspect(obj))
 
     else
 
     else
       table.insert(logDevStore, Inspect(obj))
+
       table.insert(logStore, Inspect(obj))
 
     end
 
     end
 
   end
 
   end
Line 63: Line 64:
  
 
   log(string.format('@%ims, modules loaded', runTime()))
 
   log(string.format('@%ims, modules loaded', runTime()))
 
 
end
 
end
  
Line 70: Line 70:
 
-----------------------
 
-----------------------
  
function DefInfo.vardefine(name, value, frame)
+
function DefInfo.vardefine(name, value)
 
   assert(name, "vardefine: missing argument #1 (variable to definePrefix)")
 
   assert(name, "vardefine: missing argument #1 (variable to definePrefix)")
 
   assert(type(name) == "string", string.format("vardefine: bad argument #1 (string expected, got %s)", type(name)))
 
   assert(type(name) == "string", string.format("vardefine: bad argument #1 (string expected, got %s)", type(name)))
 
   assert(value, "vardefine: missing argument #2 (value to assign)")
 
   assert(value, "vardefine: missing argument #2 (value to assign)")
 
   assert(type(value) == "string" or type(value) == "number" or type(value) =="boolean", string.format("vardefine: bad argument #2 (string, number or boolean expected, got %s)", type(value)))
 
   assert(type(value) == "string" or type(value) == "number" or type(value) =="boolean", string.format("vardefine: bad argument #2 (string, number or boolean expected, got %s)", type(value)))
  assert(frame, "vardefine: 'frame' missing")
 
 
   frame:callParserFunction('#vardefine', name, value)
 
   frame:callParserFunction('#vardefine', name, value)
 
end
 
end
Line 112: Line 111:
 
function DefInfo.getDef(defID, expandVF)
 
function DefInfo.getDef(defID, expandVF)
 
   if expandVF ~= false then expandVF = true end
 
   if expandVF ~= false then expandVF = true end
 
 
   local ignoreKeys = {"Abstract", "Name", "ParentName"}
 
   local ignoreKeys = {"Abstract", "Name", "ParentName"}
 
   local baseDef
 
   local baseDef
Line 153: Line 151:
 
end
 
end
  
local function definePrefixed(tbl, frame)
+
local function definePrefixed(tbl)
 
   for k,v in pairs(tbl) do
 
   for k,v in pairs(tbl) do
 
     if type(v) ~= 'table' then
 
     if type(v) ~= 'table' then
 
       local mt = getmetatable(tbl)
 
       local mt = getmetatable(tbl)
 
       log(string.format('%s = %s', mt[k], tostring(v)))
 
       log(string.format('%s = %s', mt[k], tostring(v)))
       if mw then DefInfo.vardefine(mt[k], v, frame) end
+
       if mw then DefInfo.vardefine(mt[k], v) end
 
     else
 
     else
       definePrefixed(v, frame)
+
       definePrefixed(v)
 
     end
 
     end
 
   end
 
   end
Line 168: Line 166:
 
-- public interface --
 
-- public interface --
 
----------------------
 
----------------------
 +
 +
function wiki.getDefName(frame)
 +
  local defName
 +
  local label = frame.args[1]
 +
 +
  if not label then
 +
    logObject(frame.args, string.format("getDefName @ %ims: missing argument #1 (label)\nframe.args", runTime()))
 +
    return nil
 +
  end
 +
 +
  for defID,def in pairs(Data) do
 +
    if string.upper(def.label or "") == string.upper(label) then
 +
      defName = def.defName
 +
    end
 +
  end
 +
 +
  if not defName then
 +
    logObject(frame.args, string.format("getDefName @ %ims: '%s' not found\nframe.args", runTime(), label))
 +
    return nil
 +
  end
 +
 +
  log(string.format("@%ims, getDefName: retrieved defName", runTime()))
 +
  return defName
 +
end
  
 
function wiki.count(frame)
 
function wiki.count(frame)
 
   local query = wiki.query(frame)
 
   local query = wiki.query(frame)
   if type(wiki.queried) == 'table' then -- WARNING: checks a variable that is set in wiki.query (ugly)
+
   if queried then
     return Util.table.count(wiki.queried)
+
     return #queried
 
   end
 
   end
 
end
 
end
Line 197: Line 219:
 
   end
 
   end
  
   local processedDef = def
+
   local prune = def
  
 
   for i,arg in ipairs(frame.args) do -- arguments
 
   for i,arg in ipairs(frame.args) do -- arguments
Line 203: Line 225:
  
 
     if i == argLen and frame.args["sibling"] then
 
     if i == argLen and frame.args["sibling"] then
       processedDef = Search.find({nil, frame.args["sibling"]} , processedDef)
+
       prune = Search.find({nil, frame.args["sibling"]} , prune)
       if not processedDef then
+
       if not prune then
 
         logObject(frame.args, string.format("query @ %ims: bad argument 'sibling' ('%s' not found')\nframe.args", runTime(), frame.args["sibling"]))
 
         logObject(frame.args, string.format("query @ %ims: bad argument 'sibling' ('%s' not found')\nframe.args", runTime(), frame.args["sibling"]))
 
         return nil
 
         return nil
 
       else
 
       else
         processedDef = Search.meta.parent.table[arg]
+
         prune = Search.meta.parent.table[arg]
         if not processedDef then
+
         if not prune then
 
           logObject(frame.args, string.format("query @ %ims: bad argument #%i ('%s' is not a sibling of '%s')", runTime(), i, arg, frame.args["sibling"]))
 
           logObject(frame.args, string.format("query @ %ims: bad argument #%i ('%s' is not a sibling of '%s')", runTime(), i, arg, frame.args["sibling"]))
 
           return nil
 
           return nil
Line 217: Line 239:
  
 
     if i < argLen or i == argLen and not frame.args["sibling"] then
 
     if i < argLen or i == argLen and not frame.args["sibling"] then
       processedDef = Search.find(arg, processedDef)
+
       prune = Search.find(arg, prune)
       if not processedDef then
+
       if not prune then
 
         logObject(frame.args, string.format("query @ %ims: bad argument #%i ('%s' not found)\nframe.args", runTime(), i, frame.args[i]))
 
         logObject(frame.args, string.format("query @ %ims: bad argument #%i ('%s' not found)\nframe.args", runTime(), i, frame.args[i]))
 
         return nil
 
         return nil
 
       else
 
       else
         if type(processedDef) ~= 'table' and i < argLen then
+
         if type(prune) ~= 'table' and i < argLen then
 
           log(string.format("query @ %ims: warning Def ['%s'] argument #%i ('%s' returns a value, all extra arguments ignored)", runTime(), def['label'], i, frame.args[i]))
 
           log(string.format("query @ %ims: warning Def ['%s'] argument #%i ('%s' returns a value, all extra arguments ignored)", runTime(), def['label'], i, frame.args[i]))
           return processedDef
+
           return prune
 
         end
 
         end
 
       end
 
       end
Line 231: Line 253:
 
   end -- for arguments
 
   end -- for arguments
  
   if type(processedDef) == "table" then
+
   if type(prune) == "table" then
 
     log(string.format("@%ims, query: table vardefined", runTime()))
 
     log(string.format("@%ims, query: table vardefined", runTime()))
     setPrefix(processedDef, frame.args[argLen])
+
     setPrefix(prune, frame.args[argLen])
     definePrefixed(processedDef, frame)
+
     definePrefixed(prune)
     wiki.queried = processedDef -- WARNING: sets a variable that is used in another function wiki.count (ugly)
+
     queried = prune
 
     return nil
 
     return nil
 
   end
 
   end
  
   log(string.format("@%ims, query: %s printed", runTime(), type(processedDef)))
+
   log(string.format("@%ims, query: %s printed", runTime(), type(prune)))
   return processedDef
+
   return prune
 +
 
 +
end
 +
-- {{User:Dr. Strangelove/Template:Infobox/sandbox|{{{movespeed|}}}|statBases|MoveSpeed|title=Some movin|SMWP=Move Speed Base}}
 +
-- {{#invoke:Test|print|{{SUBPAGENAME}}}}
 +
function wiki.print(frame)
 +
  local subpagename = frame.args[1]
 +
  local pFrame = frame:getParent()
 +
 
 +
  if not pFrame.args then return "no arguments passed" end
 +
  if not pFrame.args[1] then return "missing argument #1" end
 +
  if not pFrame.args.title then return "missing named argument 'title'" end
 +
 
 +
  local ibPropValue = pFrame.args[1]
 +
 
 +
  local qFrame = {
 +
    args = {}
 +
  }
 +
 
 +
  for i,v in ipairs(pFrame.args) do
 +
    if i > 1 then
 +
      table.insert(qFrame.args, v)
 +
    end
 +
  end
 +
 
 +
  qFrame.args.label = 'hare'
 +
 
 +
  mw.logObject(qFrame, 'qFrame')
 +
 
 +
  local q = wiki.query(qFrame)
 +
 
 +
  mw.logObject(q, 'q')
 +
  mw.logObject(pFrame.args.SMWP, 'pFrame.args.SMWP')
 +
  mw.logObject(ibPropValue, 'ibPropValue')
 +
 
 +
  if pFrame.args.SMWP and ibPropValue then
 +
    frame:callParserFunction('#set', pFrame.args.SMWP .. '=' .. ibPropValue)
 +
  end
 +
 
 +
  local sOutputText
 +
 
 +
  if ibPropValue == '' then
 +
    sOutputText = string.format(';%s\n:%s', pFrame.args.title, q)
 +
  else
 +
    sOutputText = string.format(';%s\n:%s', pFrame.args.title, ibPropValue)
 +
  end
 +
 
 +
  return sOutputText
 
end
 
end
  
------------------------------------
+
---------------------------------
-- simulate MediaWiki environment --
+
-- simulate module invocation  --
------------------------------------
+
---------------------------------
  
 
if not mw then
 
if not mw then
Line 254: Line 323:
 
   wiki.query(simframe)
 
   wiki.query(simframe)
 
end
 
end
 +
 +
----------------------------------------
 +
-- simulate wiki log while developing --
 +
----------------------------------------
  
 
if not mw then
 
if not mw then
 
   Util.hl("DefInfo log")
 
   Util.hl("DefInfo log")
   for _,v in ipairs(logDevStore) do
+
   for _,v in ipairs(logStore) do
 
     print(v)
 
     print(v)
 
   end
 
   end
 
end
 
end
 
------------
 
-- return --
 
------------
 
  
 
if mw then
 
if mw then

Please note that all contributions to RimWorld Wiki are considered to be released under the CC BY-SA 3.0 (see RimWorld Wiki:Copyrights for details). If you do not want your writing to be edited mercilessly and redistributed at will, then do not submit it here.
You are also promising us that you wrote this yourself, or copied it from a public domain or similar free resource. Do not submit copyrighted work without permission!

Cancel Editing help (opens in new window)