Modulo:Controllo Wikidata/sandboxUnitTest

-- Modulo che implementa il [[Template:Controllo Wikidata]]
 
require('strict')
 
local p = {}

-- Confronta due file multimediali su Commons senza tenere conto di eventuali underscore
local function compareCommonsMediaFile(name, name2)
	return name:gsub('_', ' ') == name2:gsub('_', ' ')
end

-- Confronta due URL senza tenere conto di un eventuale slash finale (solo se uno) e del protocollo http/https
local function compareURL(url1, url2)
	local ret = false
	url1, url2 = url1:match('^https?://(.*)$'), url2:match('^https?://(.*)$')
	if url1 and url2 then
		local n, n2 = select(2, url1:gsub('/', '')), select(2, url2:gsub('/', ''))
		if n <= 1 and n2 <= 1 then
			ret = url1:gsub('/$', '') == url2:gsub('/$', '')
		else
			ret = url1 == url2
		end
	end
	return ret
end

--[[
* La categoria senza wikitesto.
* @return string|nil
]]--
function p._category(wdprop, userval, catprefix, args)
	local mWikidata = require('Modulo:Wikidata')

	local wdval

	-- con id=no il primo parametro non è più trattato come l'ID di una proprietà Wikidata
	-- ma come una normale stringa da confrontare con il secondo parametro
	if args.id == 'no' then
		wdval = wdprop
	else
		wdval = mWikidata._getProperty( {
			wdprop,
			showunit  = args['unità'] ~= 'no',
			formatnum = args['unità'] ~= 'no',
			from      = args.from
		} )
	end

	local comparefunc = function(v1, v2) return v1 == v2 end
	local cat

	-- consente di definire funzioni di confronto per proprietà specifiche
	-- (oppure tramite eventuali parametri)
	if args.id ~= 'no' then
		if mWikidata._getDatatype({ args[1] }) == 'file multimediale su Commons' then
			comparefunc = compareCommonsMediaFile
		elseif args[1] == 'P856' then
			comparefunc = compareURL
		end
	end

	local genletto = { ms = 'letto', mp = 'letti', fs = 'letta', fp = 'lette' }

	if userval then
		if not wdval then
			cat = string.format('%s assente su Wikidata', catprefix)
		elseif args.uguale ~= 'no' and comparefunc(wdval, userval) then
			cat =  string.format('%s uguale a Wikidata', catprefix)
		elseif args.diff ~= 'no' then
			cat = string.format('%s differente da Wikidata', catprefix)
		end
	elseif wdval then
		cat = string.format('%s %s da Wikidata', catprefix, args.genere and genletto[args.genere] or 'letto')
	end
 
	return cat
end

--[[
* Per l'utilizzo da altro modulo.
* @return string
]]--
function p._main(args)
	local wdprop, userval, catprefix = args[1], args[2], args[3]

	-- namespace 0 obbligatorio salvo everyNS
	if mw.title.getCurrentTitle().namespace ~= 0 and args.everyNS ~= 'sì' then
		return ''
	end

	-- proprietà Wikidata e prefisso categoria obbligatori
	if args.id ~= 'no' and not wdprop or not catprefix then
		return ''
	end

	local category = p._category(wdprop, userval, catprefix, args)
	return category and string.format('[[Categoria:%s]]', category) or ''
end
 
--[[
* Entry-point di _main per il template {{Controllo Wikidata}}
]]--
function p.main(frame)
	local getArgs = require('Modulo:Arguments').getArgs
	return p._main(getArgs(frame, {parentOnly = true}))
end

--[[
* Entry-point di _category per unit test
]]--
function p.category(frame)
	local getArgs = require('Modulo:Arguments').getArgs
	local args = getArgs(frame)
	return p._category(args[1], args[2], args[3], args)
end
 
return p

Content Disclaimer

Informasi ini disarikan dari Wikipedia dan disajikan kembali untuk tujuan edukasi. Konten tersedia di bawah lisensi CC BY-SA 3.0. Kami tidak bertanggung jawab atas ketidakakuratan data yang bersumber dari kontribusi publik tersebut.

  1. The information displayed on this website is sourced in part or in whole from Wikipedia and has been adapted for the purpose of restating it. We strive to provide accurate and relevant information, however:
  2. There is no guarantee of absolute accuracy. Wikipedia is an open, collaborative project that can be edited by anyone, so information is subject to change.
  3. It is not intended to constitute professional advice. The content displayed is for informational and educational purposes only. For important decisions (e.g., medical, legal, or financial), please consult a professional.
  4. Content copyright. Wikipedia is licensed under the Creative Commons Attribution-ShareAlike License (CC BY-SA). This means that content may be reused with appropriate attribution and shared under a similar license.
  5. Responsible use. Any risk arising from the use of information from this website is entirely the responsibility of the user.