Mòdul:Prova

De Viquitexts

La documentació d'ús d'aquest mòdul es pot crear a Mòdul:Prova/ús

--------------------------------------------------------------------------------
-- Adaptat de https://en.wikisource.org/wiki/Module:Edition
--------------------------------------------------------------------------------
require('strict')
local Wikidata = require('Module:Wikidata')
--------------------------------------------------------------------------------
-- Get an Item based on what's passed in the 'wikidata' or 'page' parameters of
-- the args, or the current page's ID otherwise.
local function getItem( args )
	local id = nil
	-- If args is a table with an appropriate element, use it.
	if type( args ) == 'table' then
		if args.wikidata ~= '' and args.wikidata ~= nil then
			id = args.wikidata
		elseif args.wikidata_id ~= '' and args.wikidata_id ~= nil then
			id = args.wikidata_id
		elseif args.page ~= '' and args.page ~= nil then
			local title = mw.title.new( args.page )
			id = mw.wikibase.getEntityIdForTitle( title.nsText .. title.text )
			-- If no entity for this page, maybe it's a subpage and we should look for the root page's entity.
			if id == nil then
				id = mw.wikibase.getEntityIdForTitle( title.nsText .. title.rootText )
			end
		end
	end
	if type( args ) == 'string' and args ~= '' then
		id = args
	end
	return mw.wikibase.getEntity( id )
end

--------------------------------------------------------------------------------
-- Exported method. Autor o autors d'un Llibre.
-- Prova 1 arg: =p.autor({args={qid='Q123401416'}})
local function autor( args )
	local qid = args['qid']
	if qid == nil then return '' end

	local AutorWD = ''
	AutorWD = Wikidata.claim{item=qid, property="P50", lang="ca", list=true, editicon=false}
	if not AutorWD then
		AutorWD = Wikidata.claim{item=qid, property="P2093", lang="ca", list=true, editicon=false}	-- p.ex. Q122228052
	end
	if not AutorWD then return '' end

	return AutorWD
	
end

--------------------------------------------------------------------------------
-- Exported method. Get wikitext for displaying an edition's badges from Wikidata.
-- Testing 0 arg: =p.badge({args={wikidata='Q19527599'}})
-- Testing 1 arg: =p.badge({args={wikidata='Q19527599',qid='Q105713908'}})
local function badge( args )
	local item = getItem( args )	--per defecte, id de la pàgina
	if args.qid ~= nil then			--però si passem un qid, id passat
		item = mw.wikibase.getEntity(args.qid)
	end
	if not ( item and item.sitelinks and item.sitelinks.cawikisource and item.sitelinks.cawikisource.badges ) then
		return ''
	end
	local badges = item.sitelinks.cawikisource.badges
	local out = ''
	for _, badge in pairs( badges ) do
		local badgeOut = ''
		local badgeItem = mw.wikibase.getEntity( badge )
		local wikisourceBadgeClass = 'Q75042035'
		if badgeItem.claims.P31[1].mainsnak.datavalue.value.id == wikisourceBadgeClass and badgeItem.claims.P18 ~= nil then
			if badge ~= nil then
				badgeOut = badgeOut .. badge --Q20748091 (no revisat), Q20748092 (revisat), Q20748093 (validat), Q20748094 (problemàtic)
			end
			out = out .. badgeOut
		end
	end
	return mw.getCurrentFrame():preprocess( out )
end

--------------------------------------------------------------------------------
-- Obra: estàndard "inline" de declaració d'una obra per a les pàgines d'Autor
-- Prova: =p.obra({args={tipus='desambigua', qid='Q123401416', etiqueta='El títol'}})
-- Prova: =p.obra({args={tipus='llibre', qid='Q123401416', volum='3'}})
local function obra( args )

	local tipus = args['tipus']			--tipus de crida
	local qid = args['qid']				--id de Wikidata
	local volum = args['volum']			--volum N (1 qid, N fitxers, N Llibres)
	local etiqueta = args['etiqueta']	--etiqueta de l'enllaç intern

	local s = ''
	local CategoriaError = '[[Categoria:Pàgines amb plantilla F que requereix manteniment]]'
	local SenseWikidata = false
	local IconaWikidata = '[[File:Wikidata-logo.svg|20px|link=d:'
	local Icona100 = '<span title="Índex validat">[[File:100%.svg|10px|alt=Índex validat|link=]]</span> '
	local Icona075 = '<span title="Índex no validat">[[File:75%.svg|10px|alt=Índex no validat|link=]]</span> '
--	local Icona050 = '<span title="Índex no revisat">[[File:50%.svg|10px|alt=Índex no revisat|link=]]</span> '
	local Icona025 = '<span title="Índex incomplet">[[File:25%.svg|10px|alt=Índex incomplet|link=]]</span> '
	local IconaErr = '<span title="Índex amb errors">[[File:Purple Exclamation.svg|10px|alt=Índex amb errors|link=]]</span> '
	local IconaLlibre = '[[File:Open book nae 02.svg|20px|link=:Llibre:'
	local FileMatch = ''
	local wSiteLink = ''
	local wTitol = ''
	local P996 = false
	local MultiVolum = false

	if tipus == nil or tipus == '' then return '' end

	if tipus == "desambigua" then
		local IconaDesambigua = ' [[File:Disambig.svg|17px|link=]]'
		if qid == nil or qid == '' then
			s = s .. IconaDesambigua
			SenseWikidata = true
		else
			if mw.wikibase.entityExists(qid) == true then
				s = s .. IconaWikidata .. qid .. ']]'
				s = s .. IconaDesambigua
				wSiteLink = Wikidata.getSiteLink{item=qid}
				if wSiteLink then
					if etiqueta == nil or etiqueta == '' then
						s = s .. " ''[[" .. wSiteLink .. "]]''"
					else
						s = s .. " ''[[" .. wSiteLink .. "|" .. etiqueta .. "]]''"
					end	
				else
					SenseWikidata = true
				end	
			else
				s = s .. 'Identificador inexistent' .. CategoriaError
			end	
		end
		if SenseWikidata == true then
			if etiqueta == nil or etiqueta == '' then
				s = s .. " ''sense títol''" .. CategoriaError
			else
				s = s .. " ''[[" .. etiqueta .. "]]''"
			end
		end	
	elseif tipus == 'llibre' then
		if qid == nil or qid == '' then
			s = s .. " qid no informat " .. CategoriaError
		else	--llibre, no qid
			if mw.wikibase.entityExists(qid) == true then
				s = s .. IconaWikidata .. qid .. ']]'
				local Insignia = badge(args)
				if Insignia == 'Q20748093' then			s = s .. Icona100
				elseif Insignia == 'Q20748092' then		s = s .. Icona075
				elseif Insignia == 'Q20748091' then		s = s .. Icona025
				elseif Insignia == 'Q20748094' then		s = s .. IconaErr end

				if Wikidata.validProperty{"P996", item=qid} == nil then
				else	--hi ha P996 (un o varis)
					local FileOnCommonsStmts = mw.wikibase.getAllStatements (qid, 'P996');
					for _, stmt in pairs( FileOnCommonsStmts ) do
						--Mirem per cada P996 si està creat el Llibre a Viquitexts (1 per edició)
						local commonsFilename = stmt['mainsnak']['datavalue']['value']
						local wLlibre = mw.title.new( commonsFilename, 'Index' )
						if wLlibre.exists then
							FileMatch = wLlibre.text
--							if P996 == true then MultiVolum = true end
							P996 = true
						end
					end
					if P996 == true then
						s = s .. IconaLlibre .. FileMatch .. ']] '
					end
				end

				wTitol = Wikidata.claim{item=qid, property="P1476", lang="ca", list=false, editicon=false}
				wSiteLink = Wikidata.getSiteLink{item=qid}
				if wSiteLink then
					if wTitol == nil or wTitol == '' then
						s = s .. mw.getCurrentFrame():preprocess("{{Epub|" .. wSiteLink .. "}} " .. "''[[" .. wSiteLink .. "]]''")
					else
						s = s .. mw.getCurrentFrame():preprocess("{{Epub|" .. wSiteLink .. "}} " .. "''[[" .. wSiteLink .. "|" .. wTitol .. "]]''")
					end
				else
					if wTitol == nil or wTitol == '' then
						s = s .. "error: no hi ha títol ni SiteLink"
					else
						local wPagina = mw.title.new( wTitol, '' )
						if wPagina.exists then
							s = s .. mw.getCurrentFrame():preprocess(" {{Epub|" .. wTitol .. "}} " .. "''[[" .. wTitol .. "]]''")
						else
							s = s .. " " .. "''[[" .. wTitol .. "]]''"
						end
					end
				end
				local wTrad = Wikidata.claim{item=qid, property="P655", lang="ca", list=false, editicon=false}
				if wTrad then
					local wAutor = Wikidata.claim{item=qid, property="P50", lang="ca", list=false, editicon=false, formatting='label'}
					if wAutor then
						if wAutor == mw.getCurrentFrame():preprocess("{{PAGENAME}}") then
							s = s .. ", traduït per " .. wTrad
						else
							s = s .. ", de " .. wAutor
						end	
					end
				end
				if P996 == false then
					local wPartDe = Wikidata.claim{item=qid, property="P361", lang="ca", list=false, editicon=false}
					if wPartDe then
						s = s .. ", part de ''" .. wPartDe .. "''"
					end
				end	
				local wData = Wikidata.claim{item=qid, property="P577", lang="ca", list=false, editicon=false, formatting="j xg Y"}
				if wData then
					s = s .. " (" .. wData .. ")"
				end
				local wTeParts = Wikidata.claim{item=qid, property="P527", lang="ca", list=true, editicon=false}
				if wTeParts then
					s = s .. ". Inclou: " .. wTeParts .. "."
				end
			else
				s = s .. " qid erroni " .. CategoriaError
			end
		end
	else	
		s = s .. 'Error tipus Edition'
	end

	return s
	
end

--------------------------------------------------------------------------------
-- Export all public functions.
return {
	autor = function( frame ) return autor( frame.args ) end;
	badge = function( frame ) return badge( frame.args ) end;
	obra = function( frame ) return obra( frame.args ) end;
}