Mòdul:Edition

De Viquitexts

Mòdul que proporciona funcions d'accés a Wikidata complementàries a les proporcionades pel Mòdul:Wikidata.

Funcions:

badge

Retorna el badge (o insígnia) d'una edició, versió o traducció declarada a Wikidata. Cal passar per paràmetre l'identificador de Wikidata (qid). Sintaxi:

{{#invoke:edition|badge|qid}}

El valor retornat pot ser algun dels següents: Q20748091 (no revisat), Q20748092 (revisat), Q20748093 (validat), Q20748094 (problemàtic), o res.

{{#invoke:edition|badge|qid=Q105713908}} => Q20748092

obra

Retorna informació estàndard d'una edició, versió o traducció declarada a Wikidata. Cal passar per paràmetre l'identificador de Wikidata (qid). S'utilitza a {{OW}} i {{ED}}. Sintaxi:

{{#invoke:edition|obra|tipus=desambigua|qid=Q19525126}} => El sermó de Muntaner
{{#invoke:edition|obra|tipus=llibre||qid=Q123401416}} => Índex validat Descarregueu en format ePub Un casament en Picaña, per Francesc Palanca i Roca (1859)

--------------------------------------------------------------------------------
-- 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. Get wikitext for displaying an edition's badges from Wikidata.
-- Test: =p.badge({args={qid='Q19527599'}})
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 {{ED}}: =p.obra({args={tipus='desambigua', qid='Q123401416', etiqueta='El títol'}})
-- Prova {{OW}}: =p.obra({args={tipus='llibre', qid='Q123401416'}})
local function obra( args )

	local tipus = args['tipus']			--tipus de crida
	local qid = args['qid']				--id de Wikidata
	local etiqueta = args['etiqueta']	--etiqueta de l'enllaç intern

	local s = ''
	local CategoriaError = '[[Categoria:Pàgines amb errors diversos|'
	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 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	--[[Plantilla:ED]]
		local IconaDesambigua = ' [[File:Disambig.svg|15px|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 .. 'i]]'
			end	
		end
		if SenseWikidata == true then
			if etiqueta == nil or etiqueta == '' then
				s = s .. " ''sense títol''" .. CategoriaError .. 'e]]'
			else
				s = s .. " ''[[" .. etiqueta .. "]]''"
			end
		end	
	elseif tipus == 'llibre' then	--[[Plantilla:OW]]
		if qid == nil or qid == '' then
			s = s .. " qid no informat " .. CategoriaError .. 'q]]'
		else
			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
							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" .. CategoriaError .. 's]]'
					else
						--Si no hi ha sitelink, la pàg. no ha d'existir. Si es vol enllaç, afegir-lo a Wikidata.
						s = s .. " " .. "''" .. wTitol .. "''"
					end
				end
				local wTrad = Wikidata.claim{item=qid, property="P655", lang="ca", list=true, editicon=false}
				local wAutorList = Wikidata.claim{item=qid, property="P50", lang="ca", list=true, editicon=false}
				local AutorInformat = false
				if wTrad then
					if wAutorList then
						if mw.getCurrentFrame():preprocess("{{NAMESPACE}}") == 'Autor' then
							local AutorWS = Wikidata.getSiteLink{item=Wikidata.claim{item=qid, property="P50", lang="ca", list=false, editicon=false, formatting='raw'}}
							if AutorWS == "Autor:" .. mw.getCurrentFrame():preprocess("{{PAGENAME}}") then
								s = s .. ", traduït per " .. wTrad
							else
								AutorInformat = true
								s = s .. ", per " .. wAutorList
							end
						else	
							AutorInformat = true
							s = s .. ", per " .. wAutorList .. ", traduït per " .. wTrad
						end
					end
				end
				local wPrefaci= Wikidata.claim{item=qid, property="P2679", lang="ca", list=false, editicon=false, formatting='internallink'}
				if wPrefaci then
					if wAutorList then
						if mw.getCurrentFrame():preprocess("{{NAMESPACE}}") == 'Autor' then
							local PrefaciWS = Wikidata.getSiteLink{item=Wikidata.claim{item=qid, property="P2679", lang="ca", list=false, editicon=false, formatting='raw'}}
							if PrefaciWS == 'Autor:' .. mw.getCurrentFrame():preprocess("{{PAGENAME}}") then
								if AutorInformat == false then
									AutorInformat = true
									s = s .. ", per " .. wAutorList
								end	
							else
								s = s .. ", prologat per " .. wPrefaci
							end
						else	
							s = s .. ", prologat per " .. wPrefaci
						end
					end
				end
				local wEpileg = Wikidata.claim{item=qid, property="P2680", lang="ca", list=false, editicon=false}
				if wEpileg then
					local wAutor = Wikidata.claim{item=qid, property="P50", lang="ca", list=false, editicon=false, formatting='label'}
					if wAutor then
						if mw.getCurrentFrame():preprocess("{{NAMESPACE}}") == 'Autor' then
							if wAutor == mw.getCurrentFrame():preprocess("{{PAGENAME}}") then
								s = s .. ", epilogat per " .. wEpileg
							else
								AutorInformat = true
								s = s .. ", per " .. wAutor
							end
						else	
							s = s .. ", epilogat per " .. wEpileg
						end
					end
				end
				if mw.getCurrentFrame():preprocess("{{NAMESPACE}}") == 'Autor' then
				else	
					if wAutorList and AutorInformat == false then
						if wAutorList == 'valor desconegut' then
						else
							s = s .. ", per " .. wAutorList
						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 .. 'x]]'
			end
		end
	else	
		s = s .. 'Error tipus Edition'
	end

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