Module:Language distance

Lua
CodeDiscussionLinksLink count SubpagesDocumentationTestsResultsSandboxLive code All modules

This module is a test from Norwegian (bokmål) Wikipedia. It needs some cleanup to be more easily reusable.

Typical call is something like

local contentLanguage = mw.language.getContentLanguage()
local distance = require 'Module:Language distance'(contentLanguage:getCode())

The module lacks proper testing.

Code

---
local langdist = {}

---
local lang

---
-- This loads a subset of the total graph. It covers the most used parts of the
-- total graph, and it is a lot cheaper than doing all the database lookups.
local graph = mw.loadData('Module:Language distance/conf')

---
-- metatable for the export
local mt = {}

---
-- adjust the installation of the module
mt.__call = function (self, qid)
	lang = qid
	local countdown = 12
	while countdown ~= 0 and not graph[qid] do
		countdown = countdown - 1
		local entity = mw.wikibase.getEntity(qid)
		assert(entity, 'Can not traverse entity')
		local statements = entity:getBestStatements('P279')-- or {}
		--local found
		mw.logObject(statements)
		for _,claim in ipairs(statements) do
			claim = claim or {}
			local valid = claim['type'] == 'statement'
			valid = valid and claim['rank'] ~= 'deprecated'
			local mainsnak = claim.mainsnak or {}
			if valid and mainsnak then
				valid = valid and mainsnak
				mw.logObject(mainsnak)
				valid = valid and mainsnak.datatype == 'wikibase-item'
				valid = valid and mainsnak.snaktype == 'value'
			end
			
			mw.logObject(valid)
		end
		if not graph[qid] then
			break
		end
	end
	return self
end

---
-- get any unknown entry, which is basically everything
mt.__index = function (self, qid)
	return nil
end

---
-- assign to an unknown entry
mt.__newindex = function (self)
	error('Can not assign to the table')
	return nil
end

-- install the metatable
setmetatable(langdist, mt)

---
return langdist