Módulo:Proofreadpage index template

Documentación del módulo


Uso

Módulo que construye la información visible de un Índice (Index).

El módulo está pensado para ser llamado por MediaWiki:Proofreadpage index template, pero puede ser probado con el siguiente código:

{{#invoke:Proofreadpage index template|fields|parámetros}}

  • fields es la función del módulo encargada de construir la página, según el resto de parámetros.
  • Parámetros:
  • Wikidata (obligatorio): es el identificador de Wikidata del Índice. Por ejemplo, «Q107297266». A partir de éste se capturan de Wikidata los datos del Índice "capturables", como Autor, Traductor, Titulo, etc. Más información en Ayuda:Índices.
-Si un campo está informado a mano se ignorará lo que se hubiera capturado de Wikidata.
-Si un campo no está informado a mano y se recupera algún dato de Wikidata, entonces se mostrará este último.
-Si un campo no está informado a mano y no se recupera nada de Wikidata, el campo no aparecerá.
  • NomPag (obligatorio): es el {{NOMPAGE}} de la página del Índice. Para hacer pruebas podemos poner el nombre del archivo, como «El Robinson suizo (1864).pdf». Se utiliza para mostrar la imagen del archivo así como el enlace para purgar las páginas.
  • NomPagE (obligatorio): es el {{NOMPAGEE}} de la página del Índice. Para hacer pruebas podemos poner el nombre del archivo, como «El_Robinson_suizo_(1864).pdf». Se utiliza para mostrar la imagen del archivo.

Ejemplo

  • Código mínimo de pruebas que captura datos de Wikidata: {{#invoke:Proofreadpage index template|fields|NomPag=El Robinson suizo (1864).pdf|NomPagE=El_Robinson_suizo_(1864).pdf|Paginas=test|Wikidata=Q107297266}}
Esta documentación está transcluida desde Módulo:Proofreadpage index template/doc.
Los editores pueden experimentar en la zona de pruebas de este módulo.
Por favor, añade las categorías e interwikis a la subpágina de documentación. Subpáginas de este módulo.

--------------------------------------------------------------------------------
-- Adaptado de https://en.wikisource.org/wiki/Module:Proofreadpage_index_template
--           y https://ca.wikisource.org/wiki/Module:Proofreadpage_index_template
-- This is a module to implement logic for [[MediaWiki:Proofreadpage index template]]
--------------------------------------------------------------------------------
-- Ejemplo (función p.fields):
-- {{#invoke:Proofreadpage index template|fields|NomPag=El Robinson suizo (1864).pdf|NomPagE=El_Robinson_suizo_(1864).pdf|Paginas=tabla|Wikidata=Q107297266}}
--------------------------------------------------------------------------------

local Wikidata = require('Module:Wikidata')
local getArgs = require('Module:Arguments').getArgs

local p = {} --p stands for package

-- mapping of field ID to field properties
local headings = {
	year		=	{	txt = 'Año',		},
	author		=	{	txt = 'Autor',		},
	editor		=	{	txt = 'Editor',		},
	editorial	=	{	txt = 'Editorial',	},
	source		=	{	txt = 'Fuente',		},
	illustrator =	{	txt = 'Ilustrador',	},
	printer 	=	{	txt = 'Imprenta',	},
	place		=	{	txt = 'Lugar',		},
	modernizar	=	{	txt = 'Modernización de la ortografía',	},
	country		=	{	txt = 'País',		},	--derechos
	quality 	=	{	txt = 'Progreso',	},
	serie		=	{	txt = 'Serie o colección',	},
	subtitle	=	{	txt = 'Subtítulo',	},
	title		=	{	txt = 'Título',		},
	translator	=	{	txt = 'Traductor',	},
	volum		=	{ 	txt = 'Volumen',	},
	wikidata	=	{	txt = 'Metadatos',	}
}

local function get_heading(id)
	if headings[id] then
		return headings[id]['txt']
	end
	error( "Can't find heading for ID: " .. id )
end

local function construct_image(content, nompage, qid, nompag)
	if nompage == nil or nompage == '' then error('nompage no puede ser nulo') end
	local s = ''

	--Miramos si en WD hay 1 o más P996, capturamos el que coincida, y le miramos el P4714 (nº pág. con título).
	local P4714 = ''
	if qid then
		local FileOnCommonsStmts = mw.wikibase.getAllStatements (qid, 'P996');
		if FileOnCommonsStmts then
			for _, stmt in pairs( FileOnCommonsStmts ) do
				local commonsFilename = stmt['mainsnak']['datavalue']['value']
				--decode: nompag podría venir con "'" en lugar de "'" (apóstrofe)
				if mw.text.decode(nompag) == commonsFilename then
					P4714 = Wikidata.claim{item=qid, property="P996", qualifier="P4714", lang="es", list=false}
				end
			end
		end
	end	

	s = s .. '<tr><td valign="top" id="ws-cover">'
	if content then
	else
		if P4714 == '' then
			content = '1'	--si no hay nada de nada
		else
			content = P4714
		end
		if content == nil then content = '1' end
	end
	if tonumber(content) == nil then
		s = s .. content
	else
		s = s .. '<span id="ws-cover" style="display:none; speak:none;">'
		s = s .. nompage .. "/" .. content .. "</span>"
		s = s .. '[[File:' .. nompage .. '|page=' .. content .. '|frameless]]'
	end
	s = s .. '</td>'
	s = s .. '<td valign="top"><table style="margin-left:0">'	--se cierran en construct_field_paginas
	return s
end

local function construct_field_paginas(content, nompage)

	if nompage == nil or nompage == '' then return "Error en construct_field_pagines (nompage)" end
	if content == nil or content == '' then return "Error en construct_field_pagines (content)" end
	local s = ''
	s = s .. '</table></td></tr>'
	s = s .. '<tr><td valign="top" colspan="2"><span class="plainlinks">'
	s = s .. '<b>[//es.wikisource.org/wiki/Index:' .. nompage .. '?action=purge Páginas]</b></span>'
	s = s .. ' ([[Ayuda:Nivel_de_las_páginas|Nivel]])\n'
	s = s .. '<div style="padding-left:0.5em; background-color:#F0F0F0;">\n' .. content .. '</div></td></tr>'
	s = s .. '</table></td>'
	return s
end

local function construct_field_sumario(content)
	local s = ''
	if content then
		s = '<td width="33%">' .. content .. '</td>'
	end
	return s
end
--------------------------------------------------------------------------------
-- 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 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.
-- Adaptado de https://ca.wikisource.org/wiki/Module:Edition
local function badge( args )
	local item = getItem( args )	--por defecto, id de la página
	if args.qid ~= nil then			--pero si pasamos un qid, id pasado
		item = mw.wikibase.getEntity(args.qid)
	end
	if not ( item and item.sitelinks and item.sitelinks.eswikisource and item.sitelinks.eswikisource.badges ) then
		return ''
	end
	local badges = item.sitelinks.eswikisource.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 corr.), Q20748092 (corr.), Q20748093 (val.), Q20748094 (err.)
			end
			out = out .. badgeOut
		end
	end
	return mw.getCurrentFrame():preprocess( out )
end

-- construct a basic "field" row
local function construct_field(id, content, qid, nompag)

	if id == nil then error( "Error en construct_field" ) end

	local CatError = "[[Categoría:Índices con discrepancias respecto a Wikidata"	
	local contentAux = content
	local siWDsiWS = false	--hay contenido en WD y en WS (se puede borrar)
	local noWDsiWS = false	--no hay contenido en WD pero sí en WS (no borrar hasta migrarlo a WD)

	if id == 'author' then
		if qid then
			local AutorP50 = Wikidata.claim{item=qid, property="P50", lang="es", list=true}
			if AutorP50 then
				if AutorP50 == 'valor desconocido' then
					AutorP50 = '[[:Categoría:Obras de autores anónimos|Anónimo]]'
				end
				if content then
					siWDsiWS = true
				else
					content = AutorP50
				end
			else
				if content then
					siWDsiWS = true
				else
					AutorP50 = Wikidata.claim{item=qid, property="P2093", lang="es", list=true}	-- p.ej. Q120785961
					if AutorP50 then
						content = AutorP50
					end	
				end
			end
			if not AutorP50 and content then noWDsiWS = true end
		end
	end

	if id == 'translator' then
		if qid then
			local wTraductor = Wikidata.claim{item=qid, property="P655", lang="es", list=true}
			if wTraductor then
				if content then
					siWDsiWS = true
				else
					content = wTraductor
				end
			end
			if not wTraductor and content then noWDsiWS = true end
		end
	end

	if id == 'title' then
		if qid then
			local wSiteLink = mw.wikibase.getSitelink(qid)
			local wTitulo = Wikidata.claim{item=qid, property="P1476", lang="es", list=false}
			if wSiteLink then
				if content then
					siWDsiWS = true
				else
					if wTitulo then
						content = "''[[" .. wSiteLink .. "|" .. wTitulo .. "]]''"
					else
						content = "''[[" .. wSiteLink .. "]]''"
					end	
				end
			else	
				if content then
					noWDsiWS = true
				else
					if wTitulo then content = "''[[" .. wTitulo .. "]]''" end
				end	
			end
		end
	end
	
	if id == 'subtitle' then
		if qid then
			local wSubtitulo = Wikidata.claim{item=qid, property="P1680", lang="es", list=false}
			if wSubtitulo then
				if content then
					siWDsiWS = true
				else
					content = wSubtitulo
				end
			end
			if not wSubtitulo and content then noWDsiWS = true end
		end
	end

	if id == 'illustrator' then
		if qid then
			local wIlustrator = Wikidata.claim{item=qid, property="P110", lang="es", list=true}
			if wIlustrator then
				if content then
					siWDsiWS = true
				else
					content = wIlustrator
				end
			end
			if not wIlustrator and content then noWDsiWS = true end
		end
	end

	if id == 'editorial' then
		if qid then
			local wPublisher = Wikidata.claim{item=qid, property="P123", lang="es", list=true, formatting='label'}
			if wPublisher then
				if content then
					siWDsiWS = true
				else
					content = wPublisher
				end
			end
			if not wPublisher and content then noWDsiWS = true end
		end
	end

	if id == 'printer' then
		if qid then
			local wImprenta = Wikidata.claim{item=qid, property="P872", lang="es", list=false, formatting='label'}
			if wImprenta then
				if content then
					siWDsiWS = true
				else
					content = wImprenta
				end
			end
			if not wImprenta and content then noWDsiWS = true end
		end
	end

	if id == 'editor' then
		if qid then
			local wEditor = Wikidata.claim{item=qid, property="P98", lang="es", list=false, formatting='label'}
			if wEditor then
				if content then
					siWDsiWS = true
				else
					content = wEditor
				end
			end
			if not wEditor and content then noWDsiWS = true end
		end
	end

	if id == 'volum' then
		if qid then
			local wVolumen = Wikidata.claim{item=qid, property="P478", lang="es", list=true}
			if wVolumen then
				if content then
					siWDsiWS = true
				else
					content = wVolumen
				end
			end
			if not wVolumen and content then noWDsiWS = true end
		end
	end

	if id == 'serie' then
	end

	if id == 'modernizar' then
		if content then
			if content == 'S' then
				content = '[[Wikisource:Modernización de textos|Ortografía antigua — El lector puede elegir modernizar la ortografía]]'
			else
				content = 'No modernizar'
			end
		end
	end

	if id == 'place' then
		if qid then
			local wPlace = Wikidata.claim{item=qid, property="P291", lang="es", list=true, formatting='label'}
			if wPlace then
				if content then
					siWDsiWS = true
				else
					content = wPlace
				end
			end
			if not wPlace and content then noWDsiWS = true end
		end
	end

	if id == 'country' then
		if qid then
			local wPais = Wikidata.claim{item=qid, property="P495", lang="es", list=false, formatting='label'}
			if wPais then
				if content then
					siWDsiWS = true
				else
					content = wPais
				end
			end
			if not wPais and content then noWDsiWS = true end
		end
	end

	if id == 'year' then
		if qid then
			local wData = Wikidata.claim{item=qid, property="P577", lang="es", list=false, formatting=" j xg Y"}
			if wData then
				if content then
					siWDsiWS = true
				else
					content = wData
				end
			end
			if not wData and content then noWDsiWS = true end
		end
	end

	if id == 'source' then
		if content == nil then
			if nompag == nil then
				content = content .. CatError .. "|Nompag]]"
			else	--todo: se podría buscar los identificadores de Wikidata P675 (GB), P724 (IA), etc.
				content = "[[:File:" .. nompag .. "|Archivo]]"
			end
		else
			noWDsiWS = true
		end
	end

	if id == 'wikidata' then
		if nompag == nil then
			return CatError .. "|Pagename]]"	
		end
		if content == nil then
			return "[[Categoría:Índices no conectados a Wikidata]]"
		end
		content = "[[File:Wikidata-logo.svg|20px|link=d:" .. contentAux .. "]] [[d:" .. contentAux .. "|" .. contentAux .. "]]"

		if mw.wikibase.entityExists(qid) == false then
			content = content .. CatError .. "|Qid]]"	
		end
		if Wikidata.tieneValorPropiedad{"P996", item=qid} == nil then
			local wTitulo = mw.title.getCurrentTitle()	--Nombre sin "Índice:"
			if wTitulo.text then
				local wArchivo = mw.title.new( wTitulo.text, 'File' )
				if wArchivo.exists then
					--Archivo no está en Commons pero sí localmente en Wikisource (casos excepcionales)
				else
					--No hay archivo local ni P996: hay que añadirlo (archivo en Commons) al ítem en Wikidata.
					content = content .. CatError .. "|Commons]]"	
				end
    		else
				content = content .. CatError .. "|Error]]"
			end
		else
			local FileMatch = ''
			local FileOnCommonsStmts = mw.wikibase.getAllStatements (qid, 'P996');
			for _, stmt in pairs( FileOnCommonsStmts ) do
				local commonsFilename = stmt['mainsnak']['datavalue']['value']
				if mw.text.decode(nompag) == commonsFilename then	--decode por si apóstrofe llega como "&#39;"
					FileMatch = commonsFilename
				end
			end
			if FileMatch == '' then
				content = content .. CatError .. "|Fichero]]"
			end
		end
	end	
	
	if id == 'quality' then

		if qid then
			local badges = badge(qid)
			if badges then
				if content then siWDsiWS = true end
				content = "C"
				if badges == "Q20748091" then content = "C" end
				if badges == "Q20748092" then content = "V" end
				if badges == "Q20748093" then content = "T" end
				if badges == "Q20748094" then content = "L" end
			else
				if content then noWDsiWS = true end
			end
			if contentAux then --tiene preferecia lo informado a mano
				content = contentAux
			end	
		end

		if content == nil then
			content = "[[Categoría:Índices no corregidos]][[:Categoría:Índices no corregidos|Por corregir]]"
		elseif content == 'C' then
			content = "[[Categoría:Índices no corregidos]][[:Categoría:Índices no corregidos|Por corregir]]"
		elseif content == 'V' then
			content = "[[Categoría:Índices corregidos]][[:Categoría:Índices corregidos|Corregido — Todas las páginas corregidas pero no todas validadas]]"
		elseif content == 'T' then
			content = "[[Categoría:Índices validados]][[:Categoría:Índices validados|Terminado — Todas las páginas validadas]]"
		elseif content == 'L' then
			content = "[[Categoría:Índices dañados]][[:Categoría:Índices dañados|Archivo fuente debe ser arreglado antes de corregirlo]]"
		else
			content = "[[Categoría:Índices no corregidos]][[:Categoría:Índices no corregidos|Por corregir]]"
		end
	end	
	
	if content == nil or content == '' then
		return ''
	end
	
	local title = get_heading( id )
	if siWDsiWS == true then
		content = content .. "[[Categoría:Índices con datos en Wikidata]]"
		title = title .. "<span style='color:red; font-size:75%;'>*</span>"
	end
	if noWDsiWS == true then
		content = content .. "[[Categoría:índices con datos para Wikidata]]"
		title = title .. "<span style='color:green; font-size:75%;'>*</span>"
	end
	
	local s = ''
	s = s .. '\n<tr>'
	s = s .. '<th valign="top" align="left"><b>' .. title .. '</b></th>\n'
	s = s .. '<td>' .. content .. '</td>\n'
	s = s .. '</tr>\n'

	return s
end

function p.fields(frame)
	
	local args = getArgs(frame)

	local s = ""

	s = s .. '<table width="100%"><tr><td valign="top"><table>'
	s = s .. construct_image(args['Imagen'], args['NomPagE'], args['Wikidata'], args['NomPag'])

	-- Tras la imagen, intentamos mostrar una fila por campo (si no hay contenido, no aparece)
	s = s .. construct_field('author', args['Autor'], args['Wikidata'])
	s = s .. construct_field('translator', args['Traductor'], args['Wikidata'])
	s = s .. construct_field('editor', args['Editor'], args['Wikidata'])
	s = s .. construct_field('title', args['Titulo'], args['Wikidata'])
	s = s .. construct_field('subtitle', args['Subtitulo'], args['Wikidata'])
	s = s .. construct_field('volum', args['Volumen'], args['Wikidata'])
	s = s .. construct_field('year', args['Ano'], args['Wikidata'])
	s = s .. construct_field('place', args['Lugar'], args['Wikidata'])
	s = s .. construct_field('country', args['derechos'], args['Wikidata'])
	s = s .. construct_field('printer', args['Imprenta'], args['Wikidata'])
	s = s .. construct_field('editorial', args['Editorial'], args['Wikidata'])
	s = s .. construct_field('illustrator', args['Ilustrador'], args['Wikidata'])
	s = s .. construct_field('source', args['Fuente'], args['Wikidata'], args['NomPag'])
	s = s .. construct_field('serie', args['Serie'], args['Wikidata'])	--a mano
	s = s .. construct_field('modernizar', args['Modernizacion'], args['Wikidata'])	--a mano
	s = s .. construct_field('wikidata', args['Wikidata'], args['Wikidata'], args['NomPag'])
	s = s .. construct_field('quality', args['Progreso'], args['Wikidata'])
	s = s .. '<tr><td colspan="2">[[Ayuda:Índices|Ayuda]]</td></tr>'
	s = s .. construct_field_paginas(args['Paginas'], args['NomPagE'])
	s = s .. construct_field_sumario(args['Comentarios'])
	s = s .. '</tr></table>'
	return s
	
end

return p