Ir al contenido

Módulo:Enlaces

De Wikisource, la biblioteca libre.
Documentación del módulo


Este módulo está pensado para simplificar la adición de enlaces a proyectos que salen tanto de los parámetros en cualquier plantilla (siempre que usen nombres genéricos como |wikipedia= o |wikcionario=) como de Wikidata.
Esta documentación está transcluida desde Módulo:Enlaces/doc.
Los editores pueden experimentar en la zona de pruebas
Por favor, añade las categorías a la subpágina de documentación.
(subpáginas - enlaces)

--The sister project linking module
local p = {}
local Wikidata = require('Módulo:Wikidata')
local function getsize(tab)
	local count = 0
	for _,_ in pairs(tab) do
		count = count + 1
	end
	return count
end
local aliases = {
	['commons'] 	 = 'commonswiki',
	['commonscat'] 	 = 'commonswiki',
    ['wikilibros']   = 'eswikibooks',
    ['wikibooks']    = 'eswikibooks',
	['wikinoticias'] = 'eswikinews',
	['wikinews'] = 'eswikinews',
	['wikipedia']    = 'eswiki',
	['wikiquote']    = 'eswikiquote',
	['wikicitas']    = 'eswikiquote',
	['wikispecies']  = 'specieswiki',
	['wikiespecies']  = 'specieswiki',
	['wikiversity'] = 'eswikiversity',
	['wikiversidad'] = 'eswikiversity',
	['desambiguación'] = 'desambiguacion',
	['wiktionary'] = 'eswiktionary',
	['wikcionario'] = 'eswiktionary',
	['wikiviajes'] = 'eswikivoyage',
	['wikivoyage'] = 'eswikivoyage'
}

p.relevants = { --{['name']='',['image']='',['text']='',['prefix']='',}
		['commonswiki']   = { ['name']   = 'Wikimedia Commons',
						  	  ['image']  = 'Commons-logo.svg|link=c:',
							  ['text']   = 'multimedia',
							  ['prefix'] = 'c:',
			                  ['title']  = '',
		},
		['eswikibooks']   = { ['name']   = 'Wikibooks',
			                  ['image']  = 'Wikibooks-logo.svg|link=wikibooks:',
			                  ['text']   = 'libro',
			                  ['prefix'] = 'wikibooks:es:',
			                  ['title']  = '',},
		['eswikinews']    = { ['name']   = 'Wikinews',
							  ['image']  = 'Wikinews-logo.svg|link=wikinews:', 
							  ['text']   = 'noticias',
							  ['prefix'] = 'wikinews:es:',
			                  ['title']  = '',},
		['eswiki']        = { ['name']   = 'Wikipedia',
			                  ['image']  = 'Wikipedia-logo-simple.png|link=w:',
			                  ['text']   = 'artículo enciclopédico',
			                  ['prefix'] = 'w:es:',
			                  ['title']  = '',},
		['eswiktionary']   = { ['name']   = 'Wiktionary',
			                  ['image']  = 'Wiktionary-logo.svg|link=wikt:',
			                  ['text']   = 'diccionario',
			                  ['prefix'] = 'wikt:es:',
			                  ['title']  = '',},
		['eswikiquote']   = { ['name']   = 'Wikiquote',
			                  ['image']  = 'Wikiquote-logo.svg|link=wikiquote:',
			                  ['text']   = 'citas',
			                  ['prefix'] = 'wikiquote:es:',
			                  ['title']  = '',},
		['specieswiki']   = { ['name']   = 'Wikispecies',
			                  ['image']  = 'Wikispecies-logo.svg|link=species:',
			                  ['text']   = 'taxonomía',
			                  ['prefix'] = 'species:',
			                  ['title']  = '',},
		['eswikiversity'] = { ['name']   = 'Wikiversity',
			                  ['image']  = 'Wikiversity-logo.svg|link=wikiversity:',
			                  ['text']   = 'clase',
			                  ['prefix'] = 'wikiversity:es:',
			                  ['title']  = '',},
		['eswikivoyage']	=  { ['name']   = 'Wikivoyage',
			                  ['image']  = 'Wikivoyage-logo.svg|link=Wikivoyage:',
			                  ['text']   = 'clase',
			                  ['prefix'] = 'wikivoyage:es:',
			                  ['title']  = '',},
		['wikidata']      = { ['name']   = 'Wikidata',
			                  ['image']  = 'Wikidata-logo.svg|link=d:',
			                  ['text']   = 'metadatos',
			                  ['prefix'] = 'd:',
			                  ['title']  = '',},
		['desambiguacion']= { ['name']   = 'Desambiguación',
			                  ['image']  = 'Disambiguation.svg',
			                  ['text']   = 'títulos similares',
			                  ['prefix'] = '',
			                  ['title']  = '',},
}


-- toma los enlaces directamente desde los parámetros de la plantilla. Ejemplo, |Wikipedia= en {{encabezado}}
function p.getlinksfromargs( args, relevantsList )
	local lang = mw.language.getContentLanguage()
	local output = relevantsList

	for key, value in pairs( args ) do
		if value ~='' then
			key = lang:lcfirst(tostring(key))
			
			if (aliases[key]) then 
				key=aliases[key] --si hay alias, usarlo
			end 
			
			if (output[key]) then
				output[key]["title"] = value
			end
		end
	end

	return output
end
function p.getlinksfromwikidata(relevantsList, QID )
	local lang = mw.language.getContentLanguage()
	local output = relevantsList
	local Entidad  = mw.wikibase.getEntityObject(QID)
	if Entidad then
		output["wikidata"]["title"]=Entidad.id
		if Entidad["sitelinks"] and getsize(Entidad["sitelinks"]) > 1 then
			for k, v in pairs(Entidad["sitelinks"]) do
				if output[k] then
					output[k]["title"]=v["title"]
				end
			end
		elseif Entidad["claims"] and Entidad["claims"]["P629"] then
			-- edición de...
			local P629 = Entidad["claims"]["P629"][1]["mainsnak"]["datavalue"]["value"]["id"]
			local newEntidad = mw.wikibase.getEntityObject(P629)
			newEntidad["sitelinks"] = newEntidad["sitelinks"] or {} --inicializar en caso de obras originales sin sitelinks
			for k, v in pairs(newEntidad["sitelinks"]) do
				if output[k] then	
					output[k]["title"]=v["title"]
				end
			end
		end
		-- categoría en commons
		if output['commonswiki']['title'] == '' and Entidad["claims"] and Entidad["claims"]["P373"] then
			output['commonswiki']['title'] = 'Category:'..Entidad["claims"]["P373"][1]["mainsnak"]["datavalue"]["value"]
		end
	end
	return output
end

--todos los enlaces 
function p.all(args)
	local QID
	if args.wikidata and args.wikidata ~= '' then
		QID = args.wikidata
	else
		QID = mw.wikibase.getEntityIdForCurrentPage()
	end
	-- Wikidata y luego args, en ese orden
	local output = p.getlinksfromwikidata(p.relevants, QID)
	output = p.getlinksfromargs( args, output )
	return output
end

function p.formattedlinks (relevantList)
	
	local linklist = {}
	
	for k, v in pairs(relevantList) do
		if v.title ~= '' then
			local link = '<span style="white-space:nowrap;">'
			if v.image then
				link = link..'[[File:'.. v.image ..'|16px]]&nbsp;'
			end
			link = link..'[[' .. v.prefix .. v.title .. '|' .. v.text .. ']]</span>' 
			table.insert(linklist, link)
		end
	end
	
	local noofwords = table.maxn( linklist )
	local output
	if (noofwords == 0) then
		output = ''
	elseif ( noofwords == 1 ) then
		output = linklist[1] .. '.'
	else
		output = table.concat( linklist, ' &nbsp; ', 1, noofwords ) .. '.'
	end
	return ( output )
end
--funcion para {{biocitas}}
function p.formattedbiocitas (relevantList, order)
	
	local linklist = {}
	
	for _, x in pairs(order) do
		local v = relevantList[x] 
		if v and v["title"] ~= '' then
			link = '<div class="bc-enlace">[[File:'..v['image']..
			'|16px]]<span style=\'display:inline-block; width:.5em;\'>&nbsp;</span>[[' ..
			v['prefix'] .. v['title'] .. '|' .. v['text'] .. ']]</div>' 
			table.insert(linklist, link)
		end
	end
	
	local noofwords = table.maxn( linklist )
	local output
	if (noofwords == 0) then
		output = ''
	else
		output = table.concat( linklist, '\n', 1, noofwords )
	end

	return ( output )
end
-- The main function to run the module
function p.main( frame )
	local output = ''
	local links = p.all(frame)
	
	output = p.formattedlinks(links)
	
	return output
end

return p