Module:Test

From Wikimedia Commons, the free media repository
Jump to navigation Jump to search
Lua

CodeDiscussionEditHistoryLinksLink count Subpages:DocumentationTestsResultsSandboxLive code All modules

This module is meant for testing your ideas. Any function could disappear or be changed at any moment so do not use any of them for things other than testing.

See also: Module:Sandbox

Code

local core  = require("Module:core")
local p = {}

function p.NO(frame)
	local entity = mw.wikibase.getEntity('Q577906')
	local data = {}
	local property = {P21='gender', P27='country', P106='occupation', P172='ethnicity'}
	for prop, field in pairs( property ) do
		if entity.claims and entity.claims[prop] then -- if we have wikidata item and item has the property
			data[field] = core.parseStatements(entity:getBestStatements( prop ), nil)
		end
	end
	return mw.text.jsonEncode(data)
end

function p.fallback(frame)
	return frame.args[1] .. ' :' .. mw.text.listToText(mw.language.getFallbacksFor( frame.args[1]),  ',', ',')
end

function p.namespaceName(frame)
	return mw.site.namespaces[frame.args[1]].name
end

function p.testRedirect(frame)
	return mw.wikibase.getEntity(frame.args[1]).id
end

function p.getContent(frame)
	return mw.title.getCurrentTitle():getContent()
end

function p.getFileInfo(frame)
	local title = mw.title.getCurrentTitle()
	return mw.title.new('Special:PageInfo/'..title.text,'File'):getContent()
end

function p.testRedirect2(frame)
	local id       = frame.args[1]
	local entity   = mw.wikibase.getEntity(id)
	local label0   =  entity:getLabel() or 'none'
	local label1,_ =  entity:getLabelWithLang('en') or 'none'
	local label2   =  mw.wikibase.getLabel(id) or 'none'
	local label3   =  mw.wikibase.getLabelByLang(id,'en') or 'none'
	local label4,_ =  mw.wikibase.getLabelWithLang(id) or 'none'
	return label0 ..' / '.. label1 ..' / '.. label2 ..' / '.. label3 ..' / '.. label4
end

function p.getLabelByLang(frame)
	local id = frame.args[1]
	return  mw.wikibase.getLabelByLang(id,'en') or 'none'
end

function p.getLabelByLang2(frame)
	return  mw.wikibase.getLabelByLang('Q1','en') or 'none'
end


function p.supportedLanguages(frame)
	L = mw.language.fetchLanguageNames()
	T = {}
	for lang,name in pairs(L) do
		table.insert(T, string.format('#%s: %s\n', lang, name))
	end
	return table.concat(T)
end

function p.userlang(frame)
	local _, lang = mw.wikibase.getLabelWithLang( 'Q2' )
	return lang
end

function p.languageName(frame)
	return mw.language.fetchLanguageName( frame.args.textLang, frame.args.userLang)
end

function p.labelAndLang(frame)
	label, language = mw.wikibase.getLabelWithLang( frame.args[1] )
	return language .. ":" .. label
end

function p.labelByLang(frame)

	label = mw.wikibase.getLabelByLang( frame.args[1], frame.args[2] )
	return label or 'None'
end

function p.descriptionByLang(frame)
	label = mw.wikibase.getDescriptionWithLang( frame.args[1], frame.args[2] )
	return label or 'None'
end

function p._enSitelink(item)
	return mw.wikibase.sitelink( item, 'enwiki' )
end

function p.enSitelink(frame)
	return mw.wikibase.sitelink( frame.args[1], 'enwiki' )
end

function p.isRedirect(frame)
	if mw.title.new( frame.args[1] ).isRedirect then 
		return 1
	else
		return 0
	end
end

function p.formatNum(frame)
	return mw.getLanguage(frame.args[1]):formatNum(12345678.9) .. ' / '.. mw.getLanguage(frame.args[1]):formatNum(12345678.9, { noCommafy = true })
end

function p.TypeTest(frame)
	local a = 'hello world'
	local b = nil
	local str = string.format(' type 1 = %s, type 2 = %s', type(a), type(b))
	return str
end

-- find string in a list of strings and return its index number ofr nil
local function tableFind(Table, Element)
	local i, Value
	for i, Value in ipairs(Table) do
		if Value == Element then
			return i
		end
	end
	return nil
end

function p.DateI18n(frame)
	local lang = 'ru'
	local case = 'YMD'
	local T = {}
	local tab = mw.ext.data.get('DateI18n.tab', lang)
	for _, row in pairs(tab.data) do
		local id, _, msg = unpack(row)
		T[id] = msg
	end
	return T[case] or 'missing'
end

function p.testJSON(frame)
	local str = frame.args[1]
	local str = str:gsub('\\','\\\\')
	local D   = mw.text.jsonDecode( str )
	return D["default"]
end

function p.collections(frame)
	local T = {}
	local tab = mw.ext.data.get('Completely indexed painting collections.tab', 'en')
	for iCol, field in pairs(tab.schema.fields) do -- go over field names
		if mw.ustring.lower(field.name) == 'collection_qid' then
			break
		end
	end
	return iCol
	--for iRow, row in pairs(tab.data) do
	--	T[iRow] = row[iCol]
	--end
	--return table.concat(T, ',')
end



-- ===============================================

function p.alternative_names(frame)
	local lang  = frame.args[2]
	local qCode = frame.args[1]
	local entity = mw.wikibase.getEntity(qCode)
	local Wikidata_label = require("Module:Wikidata label")  
	-- prepare fallback list of languages
	local langList = mw.language.getFallbacksFor(lang)
	table.insert(langList, 1, lang)
	
	-- get alternative names
	local alternative_names = ''
	for _, lng in ipairs(langList) do 
		local aliasTable = Wikidata_label._aliases(entity, lng)
		if #aliasTable>0 and #aliasTable<8 then -- skip aliases if more than 8 of them
			alternative_names = table.concat( aliasTable, '; ')
			break 
		end
	end
	return alternative_names
end


return p