From WikiChip
Difference between revisions of "Module:wikichip"

 
(3 intermediate revisions by the same user not shown)
Line 1: Line 1:
 
local wikichip = {}
 
local wikichip = {}
local chip = {
 
  result = nil,
 
  valid = false
 
}
 
  
 
function wikichip:new(name)
 
function wikichip:new(name)
 +
local chip = {
 +
result = nil,
 +
valid = false
 +
}
 +
 
chip.result = mw.smw.getQueryResult(
 
chip.result = mw.smw.getQueryResult(
     '[[name::Core i3-7100]][[instance of::microprocessor]]' ..
+
     '[[name::' .. name .. ']][[instance of::microprocessor]]' ..
 
     '|?full page name' ..
 
     '|?full page name' ..
 
     '|?name' ..
 
     '|?name' ..
 
     '|?designer' ..
 
     '|?designer' ..
 +
    '|?manufacturer' ..
 
     '|limit=1|mainlabel=page'
 
     '|limit=1|mainlabel=page'
 
     )
 
     )
 +
   
 +
function chip.is_valid()
 +
  if not chip.result.results[1] then
 +
    return false
 +
  end
 +
  return true
 +
end
 +
 +
function chip.attr(attr)
 +
  return chip.result.results[1].printouts[attr]
 +
end
 +
 +
function chip.designer()   return chip.attr('designer')[1] end
 +
function chip.name()     return chip.attr('name')[1] end
 +
function chip.manufacturer() return chip.attr('manufacturer')[1] end
 +
 
 
      
 
      
 
     return chip
 
     return chip
end
 
 
function chip.is_valid()
 
  if not chip.result.results[1] then
 
    return false
 
  end
 
  return true
 
end
 
 
function chip.attr(attr)
 
  return chip.result.results[1].printouts[attr]
 
end
 
 
function chip.designer()
 
  return chip.attr('designer')[1]
 
 
end
 
end
  

Latest revision as of 03:57, 29 June 2017

Documentation for this module may be created at Module:wikichip/doc

local wikichip = {}

function wikichip:new(name)
	local chip = {
		result = nil,
		valid = false
	}

	chip.result = mw.smw.getQueryResult(
    	'[[name::' .. name .. ']][[instance of::microprocessor]]' ..
    	'|?full page name' ..
    	'|?name' ..
    	'|?designer' ..
    	'|?manufacturer' ..
    	'|limit=1|mainlabel=page'
    )
    
	function chip.is_valid()
	  if not chip.result.results[1] then
	    return false
	  end
	  return true
	end
	
	function chip.attr(attr)
	  return chip.result.results[1].printouts[attr]
	end
	
	function chip.designer()	  	return chip.attr('designer')[1]				end
	function chip.name()		    return chip.attr('name')[1]					end
	function chip.manufacturer() 	return chip.attr('manufacturer')[1]			end
   
    
    return chip
end


-- Because key,value pairs are stored arbitrarily in the tables, this
-- function sorts the keys and returns the key,value pairs in sorted order.
function sortedPairs(unsortedTable)
  -- Get all the keys from the table
  local keys = {}
  for key in pairs(unsortedTable) do
    keys[#keys+1] = key
  end
  
  -- Sort'em!
  table.sort(keys)
  
  local i = 0
  return function()
    i = i + 1
    if keys[i] then
      return keys[i], unsortedTable[keys[i]]
    end
  end
end

function print(s)
  mw.log(s)	
end

function wikichip.print_r ( t )  
    local print_r_cache={}
    local function sub_print_r(t,indent)
        if (print_r_cache[tostring(t)]) then
            print(indent.."*"..tostring(t))
        else
            print_r_cache[tostring(t)]=true
            if (type(t)=="table") then
                for pos,val in pairs(t) do
                    if (type(val)=="table") then
                        print(indent.."["..pos.."] => "..tostring(t).." {")
                        sub_print_r(val,indent..string.rep(" ",string.len(pos)+8))
                        print(indent..string.rep(" ",string.len(pos)+6).."}")
                    elseif (type(val)=="string") then
                        print(indent.."["..pos..'] => "'..val..'"')
                    else
                        print(indent.."["..pos.."] => "..tostring(val))
                    end
                end
            else
                print(indent..tostring(t))
            end
        end
    end
    if (type(t)=="table") then
        print(tostring(t).." {")
        sub_print_r(t,"  ")
        print("}")
    else
        sub_print_r(t,"  ")
    end
    print()
end

return wikichip