Module:Signature

From BugSigDB

Documentation for this module may be created at Module:Signature/doc

local p = {}
local titleParts, getEntry

local sigReview = require( 'Module:Signature review' )

function p.reviewButton( frame, currentfullpagename )
	local invertedState = 'Reviewed'
	local invertedStateText = 'Mark reviewed'
	if mw.title.makeTitle( 'Review', currentfullpagename ).exists then
		if frame:expandTemplate{ title = 'getReviewValues', args = {'Review state'} } == 'Reviewed' then
		invertedState = 'Incorrect'
		invertedStateText = 'Flag for review'
	end
end
	
	return '<span class="with-pholder text-nowrap d-user-inline reviewer">' ..
		frame:callParserFunction(
			'#autoedit',
			'form=Review',
			'target=Review:' .. currentfullpagename,
			'link text=' .. invertedStateText,
			'link type=link',
			'Review[Subject]=' .. currentfullpagename,
			-- Important use current user over myvariables magic word due to caching.
			'Review[Reviewer]=current user',
			'Review[Review date]=now',
			'Review[Review state]=' .. invertedState
		) .. '<span class="pholder plainlinks" title="Reload the page to see the new status">' ..
	'[[' .. mw.title.getCurrentTitle().fullText .. '|' .. 'Your review change was submitted]]</span></span>'
end


function titleParts( text, count, first )
	local out = ""
	for i in mw.text.gsplit( text, '/', true ) do
		if count < 1 then
			return out
		end
		if first > 1 then
			first = first - 1
		else
			if out == '' then
				out = i
			else
				out = out .. '/' .. i
			end
			count = count - 1
		end
	end
	return out
end

function getEntry( name )
	if type( name ) == 'table' then
		return table.concat( name, ', ')
	end
	if name == nil then
		return ''
	end
	return name
end


function p.signature( frame )
	-- Unclear why this div is here since it would be empty anyways.
	local out = '<div class="d-none">'
	local t = mw.title.getCurrentTitle()
	local basePage = t.basePageTitle.prefixedText
	local currentfullpagename = mw.ext.VariablesLua.var( 'CURRENTFULLPAGENAME', t.fullText )
	local pargs = frame:getParent().args
	local basePageData = (mw.smw.ask{
		'[[' .. basePage .. ']]',
		'?Body site',
		'?Condition',
		'?Modification date'
	} or {{}})[1]
    -- The +sep= args don't work properly with named parameters.
	out = out .. frame:callParserFunction( '#set', { '',
			'Base page=' .. basePage,
			'Semantic Dependency=' .. basePage,
			'State=' .. ( frame:getParent().args['State'] or "Incomplete" ),
			'Related experiment=' .. titleParts( basePage, 1, 2 ),
			'Related study=' .. titleParts( basePage, 1, 1 ),
			'Original page name=' .. titleParts( currentfullpagename, 1, 3 ),
			'Page sort number=' .. frame:preprocess( "{{#replace:{{#titleparts:{{CURRENTFULLPAGENAME}}||-1}}|Signature |}}" ),
			'Related directly=' .. ( frame:getParent().args['NCBI'] or '' ),
			'+sep=,',
			'Body site=' .. getEntry( basePageData['Body site'] ),
			'+sep=,',
			'Condition=' .. getEntry( basePageData['Condition'] )
	} )

	if
		basePageData['Modification date'] == nil or
		(mw.smw.ask{ '[[' .. t.rootPageTitle.prefixedText .. ']]', '?Modification date' } or {{}})[1]['Modification date'] == nil
	then
		out = out .. '[[Category:Orphaned signatures]]'
	end
	if mw.ext.VariablesLua.var( 'inside_embed_loop' ) ~= 'yes' then
		out = out .. frame:preprocess( '{{DISPLAYTITLE:{{#getdisplaytitle:{{#titleparts:{{CURRENTFULLPAGENAME}}|1|1}}}}/{{#titleparts:{{CURRENTFULLPAGENAME}}|2|2}}}}' )
	end
	out = out .. '</div><div class="bg-light btn-group mt-3 study-btn study-btn-group">'
	out = out .. frame:callParserFunction(
		'#formlink',
		'form=Signature',
		'link text=Edit',
		'target=' .. currentfullpagename,
		'reload',
		'returnto=' .. t.fullText
	) .. '[' .. tostring( mw.uri.fullUrl( currentfullpagename, { action = 'history' } ) ) .. ' History]' ..
	'[' .. tostring( mw.uri.fullUrl( currentfullpagename, { action = 'delete' } ) ) .. ' Delete]'
	if frame:getParent().args['State'] == 'Complete' then
		out = out .. p.reviewButton( frame, currentfullpagename )
	end
	out = out .. '</div><div class="mb-2 p-3 alert bg-light signature">'
	out = out .. '<div class="float-right">' .. frame:expandTemplate{ title = 'Display state', args = { pargs['State'] or '' } } .. '</div>'
	-- FIXME underscore or no underscore
	out = out .. '<div>' .. frame:expandTemplate{ title = 'Curation', args = { Curator = pargs['Curator'] or '', Curated_date = pargs['Curated_date]'] or '' } } .. '</div></div>\n'
	-- Future todo might be to bring FieldTooltip into lua to batch the SMW queries.
	out = out .. '\'\'\'Source:\'\'\' ' .. frame:expandTemplate{ title = 'FieldTooltip', args = { 'Source' } } .. ' [[Source data::' .. ( pargs['Source'] or '' ) .. ']]\n\n' 
	out = out .. '\'\'\'Description:\'\'\' ' .. frame:expandTemplate{ title = 'FieldTooltip', args = { 'Description' } }
	frame:callParserFunction( '#set', 'Description=' .. ( pargs['Description'] or '' ) )
	out = out .. ( pargs['Description'] or '' ) .. '\n\n'
	out = out .. '\'\'\'Abundance in Group 1:\'\'\' ' .. frame:expandTemplate{ title = 'FieldTooltip', args = { 'Abundance in Group 1' } }
	if pargs['Abundance in Group 1'] then
		frame:callParserFunction( '#set', 'Abundance in Group 1=' .. pargs['Abundance in Group 1'] )
		out = out .. pargs['Abundance in Group 1'] .. ' abundance' .. frame:preprocess( '{{#show:{{#titleparts:{{CURRENTFULLPAGENAME}}|2}}|?Group 1 name|intro=&#10;in&#10;}}\n\n' )
	end
	
	out = out .. '<table class="datatable display sortable mb-4"><tr><th>\'\'\'NCBI\'\'\' ' ..
	    frame:expandTemplate{ title = 'FieldTooltip', args = { 'NCBI' } } ..
		'</th><th class="unsortable text-center">Quality Control</th><th class="unsortable text-center">Links</th></tr>'
	ncbiAlreadyDone = {}
	ncbiExport = {}
	ncbiExportIds = {}

	for ncbi in mw.text.gsplit( pargs['NCBI'] or '', ',') do
		ncbi = mw.text.trim( ncbi )
		if ncbiAlreadyDone[ncbi] == nil then
			if not string.match( ncbi, '%D' ) then
				ncbiAlreadyDone[ncbi] = true
				out = out .. '<tr><td>'
				-- Taxon name. FIXME this could probably be optimized.
				out = out .. frame:callParserFunction( '#tip-text', { 
					frame:callParserFunction( '#taxonomy', { ncbi, 'Taxon/ScientificName' } ),
					frame:expandTemplate{ title = 'Get taxolist', args = { ncbi } }
				} ) .. ' '
				table.insert( ncbiExport, mw.ext.VariablesLua.var( 'NCBI_val' ) )
				table.insert( ncbiExportIds, mw.ext.VariablesLua.var( 'NCBI_ids' ) )
				out = out .. '</td><td class="px-4 text-nowrap">'
				-- Taxon marks
				out = out .. '<span class="mark-span contaminant">'
				if mw.ext.VariablesLua.var( 'IsContaminant' ) == 'yes' then
					out = out .. frame:callParserFunction( '#tip-text', { '',  'This taxon (' .. ncbi ..') is a potential contaminant.' } )
				end
				out = out .. '</span><span class="mark-span contaminant-not-host">'
				if mw.ext.VariablesLua.var( 'IsContaminantNotHost' ) == 'yes' then
					out = out .. frame:callParserFunction( '#tip-text', { '' ,  'This taxon (' .. ncbi ..') is not host-associated.' } )
				end
				out = out .. '</span><span class="mark-span contaminant-prevalent">'
				if mw.ext.VariablesLua.var( 'IsPrevalentContaminant' ) == 'yes' then
					out = out .. frame:callParserFunction( '#tip-text', { '',  'This taxon (' .. ncbi ..') is prevalent in ' .. mw.ext.VariablesLua.var( 'IsPrevalentContaminantBodysite', '' ) .. '.' } )
				end
				out = out .. '</span></td><td class="px-4 text-nowrap">'
				-- Taxon URL
				out = out .. '[https://www.ncbi.nlm.nih.gov/Taxonomy/Browser/wwwtax.cgi?mode=Info&id=' .. ncbi ..
					' <span class="taxon-query-external"></span>]'
				-- Taxon lookup link
				out = out .. '&emsp;' .. frame:callParserFunction(
					'#queryformlink',
					'form=Taxon',
					'link text=<span class="taxon-query" title="' .. ncbi .. ' taxon lookup"></span>',
					'Taxon[NCBI]=' .. ncbi,
					'_run=1'
				) .. '&emsp;'
				out = out .. sigReview.getStatusReal( frame, currentfullpagename, ncbi )
				out = out .. '</td></tr>'
			else
				-- Not numeric
				out = out .. '[[Category:Pages with missing NCBI ID]]'
				-- Original template called unique on subparts, but i don't think that is needed.
				notnumeric = mw.text.split( ncbi, ';', true )
				ncbiAlreadyDone[ncbi] = true
				out = out .. '<tr><td class="text-warning">'
				-- Taxon name
				local taxonName = mw.text.encode( table.concat( notnumeric, ', ' ) )
				out = out .. '<span class="text-warning notnumeric" title="' .. taxonName .. '">'
				-- Lineage. FIXME, is this right, it seems like the Taxon would get repeated twice?
				out = out .. '<span class="nn-lineage">' .. taxonName .. '</span>'
				-- Taxon
				out = out .. '<span class="nn-taxon">' .. notnumeric[#notnumeric] .. '</span>'
				out = out .. '</span></td>'
				out = out .. '<td></td><td style="padding-left: 81px !important;">'
				-- Taxon sig review
				out = out .. frame:expandTemplate{ title = 'Get_signature_review_status', args = { currentfullpagename, notnumeric[#notnumeric] } }
				out = out .. '</td></tr>'
			end
		end
	end
	out = out .. '</table>'
	local ncbiInherits = {}
	local ncbiExportIdsIndiv = {}
	for _, exportLin in ipairs( ncbiExportIds ) do
		for indivExportLin in mw.text.gsplit( exportLin, '|' ) do
			if not ncbiAlreadyDone[indivExportLin] then
				table.insert( ncbiInherits, indivExportLin)	
			end
		end
	end

	mw.smw.set{
		-- Note, this is setting a single value with lots of commas, not multiple values
		NCBI_export = table.concat( ncbiExport, ',' ),
		NCBI_export_ids = table.concat( ncbiExportIds, ',' ),
		NCBI_export_ids_sc = table.concat( ncbiExportIds, ';' ),
		Inherits = ncbiInherits
	}

	
	out = out .. "\n\n\'\'\'Revision editor(s):\'\'\' " .. 
		frame:expandTemplate{ title = 'FieldTooltip', args = { 'Revision editor' } } .. ' ' ..
		frame:expandTemplate{ title = 'DisplayRevisionEditors' }
	out = out .. "[[Category:Signatures]]__NOTOC__ __NOEDITSECTION__"
	return out
end


return p