Module:GetRels: Difference between revisions

From Bodhicitta
((by SublimeText.Mediawiker))
((by SublimeText.Mediawiker))
Line 119: Line 119:
output = output .. '<div class="filterable"><i class="fa-sharp-duotone fa-solid fa-angles-right fa-fw" style="color: green;"></i> [[' .. page .. ']]</div>'
output = output .. '<div class="filterable"><i class="fa-sharp-duotone fa-solid fa-angles-right fa-fw" style="color: green;"></i> [[' .. page .. ']]</div>'
elseif dir == 'both' then
elseif dir == 'both' then
output = output .. '<div class="filterable"><i class="fa-sharp fa-solid fa-angle-left" style="color: blue;"></i><i class="fa-sharp fa-solid fa-angle-right" style="color: green;"></i> [[' .. pageTipagetle .. ']]</div>'
output = output .. '<div class="filterable"><i class="fa-sharp fa-solid fa-angle-left" style="color: blue;"></i><i class="fa-sharp fa-solid fa-angle-right" style="color: green;"></i> [[' .. page .. ']]</div>'
else
else
output = output .. '<div class="filterable">[[' .. pageTitle .. ']]</div>'
output = output .. '<div class="filterable">[[' .. page .. ']]</div>'
end
end
end
end

Revision as of 18:03, 9 September 2024

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

local p = {}

function p.main(frame)

	local debugStr = ''
	local output = '<div class="offwhite-bg rounded tsdwiki-depth-1>'

	-- get local pagename
	local pageA = mw.title.getCurrentTitle().subpageText

	-- store relationships and their directions in a table
	local relsDirection = {}
	local relsDirectionQueryData = mw.ext.externalData.getExternalData{
		url = 'https://research.tsadra.org/index.php?title=Special%3AAsk&q=' .. mw.uri.encode( '[[-Has subobject::Property:Relationship]]' ) .. '&po=RelPropName%0ARelPropDir&p%5Blimit%5D=108&p%5Bformat%5D=csv',
		data = { 
			RelPropName = 'RelPropName',
			RelPropDir = 'RelPropDir'
		},
		format = 'CSV with header'
	}
	for _, data in pairs( relsDirectionQueryData ) do relsDirection[data.RelPropName] = data.RelPropDir end

	local outgoingRelsQuery = mw.ext.externalData.getExternalData{
		url = 'https://research.tsadra.org/index.php?title=Special%3AAsk&q=' .. mw.uri.encode( '[[-Has subobject::' .. pageA .. ']][[RelatedTo::+]][[Relationship::+]][[RelationshipType::!ResourceToPerson]]' ) .. '&po=RelatedTo%0ARelationship&p%5Blimit%5D=10000&p%5Blink%5D=none&p%5Bmainlabel%5D=Page&p%5Bformat%5D=csv&p%5Bvaluesep%5D=;',
		data = {
			Page = 'Page',
			RelatedTo = 'RelatedTo',
			Relationship = 'Relationship'
		},
		format = 'CSV with header'
	}

	local incomingRelsQuery = mw.ext.externalData.getExternalData{
		url = 'https://research.tsadra.org/index.php?title=Special%3AAsk&q=' .. mw.uri.encode( '[[RelatedTo::' .. pageA .. ']][[Relationship::+]][[RelationshipType::!ResourceToPerson]]' ) .. '&po=RelatedTo%0ARelationship&p%5Blimit%5D=10000&p%5Blink%5D=none&p%5Bmainlabel%5D=Page&p%5Bformat%5D=csv&p%5Bvaluesep%5D=;',
		data = {
			Page = 'Page',
			RelatedTo = 'RelatedTo',
			Relationship = 'Relationship'
		},
		format = 'CSV with header'
	}

	if outgoingRelsQuery == nil and incomingRelsQuery == nil then
		return 'No relationships found for this item.'
	end

	local rel = ''
	local allRels = {}

	if outgoingRelsQuery == nil then else

		for _, data in ipairs( outgoingRelsQuery ) do

			local pageB = frame:preprocess( '{{PAGENAME:' .. data.Page .. '}}' )
			local rel = data.Relationship
			if type(allRels[rel]) == 'table' then else allRels[rel] = {} end

			if relsDirection[rel] == 'horizontal' then
				for page in string.gmatch(data.RelatedTo, "([^;]+)") do
					allRels[rel][page] = 'both'
				end
			else
				for page in string.gmatch(data.RelatedTo, "([^;]+)") do
					allRels[rel][page] = 'right'
				end
			end

		end

	end

	if incomingRelsQuery == nil then else

		for _, data in ipairs( incomingRelsQuery ) do

			local pageB = frame:preprocess( '{{PAGENAME:' .. data.Page .. '}}' )
			local rel = data.Relationship
			if type(allRels[rel]) == 'table' then else allRels[rel] = {} end
			local mediaCheckQuery = mw.ext.externalData.getExternalData{
				url = 'https://research.tsadra.org/index.php?title=Special%3AAsk&q=' .. mw.uri.encode( '[[' .. pageB .. ']][[MediaSeries::+]][[Volumenumber::>>1]]' ) .. '&p%5Bmainlabel%5D=wikipage&p%5Blimit%5D=108&p%5Bformat%5D=csv&p%5Bvaluesep%5D=;&p%5Bdefault%5D=',
				data = { wikipage = 'wikipage'},
				format = 'CSV with header'
			}

			local next = next
			if next(mediaCheckQuery) == nil or not mediaCheckQuery then

				if relsDirection[rel] == 'vertical' then
					allRels[rel][pageB] = 'left'
				else
					local relatedToTable = {}
					for page in string.gmatch(data.RelatedTo, "([^;]+)") do
						table.insert(relatedToTable, page)
					end
					for _, page in ipairs( relatedToTable ) do
						if (rel == "Is another edition of" and getVolNum(pageA) ~= 'no value' and getVolNum(page) ~= 'no value' and getVolNum(page) ~= getVolNum(pageA)) then
						elseif page == pageA then
							allRels[rel][pageB] = 'both'
						else
							allRels[rel][page] = 'both'
						end
					end
				end

			end

		end

	end

	checkIfNeedFilter(allRels)

	for rel, pages in pairs( allRels ) do
		output = output .. '<h1>' .. rel .. '</h1><div class="row">'
		for page, dir in pairs( pages ) do
			if dir == 'left' then
				output = output .. frame:expandTemplate{ title = 'TileQuery', args = { query = '[[' .. page .. ']]', classes = 'col-md-6' } }
			elseif dir == 'right' then
				output = output .. '<div class="filterable"><i class="fa-sharp-duotone fa-solid fa-angles-right fa-fw" style="color: green;"></i> [[' .. page .. ']]</div>'
			elseif dir == 'both' then
				output = output .. '<div class="filterable"><i class="fa-sharp fa-solid fa-angle-left" style="color: blue;"></i><i class="fa-sharp fa-solid fa-angle-right" style="color: green;"></i> [[' .. page .. ']]</div>'
			else
				output = output .. '<div class="filterable">[[' .. page .. ']]</div>'
			end
		end
		output = output .. '</div>'
	end

	output = output .. '</div>'

	-- output = debugStr .. '<br>' .. output

	return output

end

function checkIfNeedFilter(tableOfValues)
	local relCheck 
	for i, v in pairs(tableOfValues) do
		if #v > 10 then
			relCheck = true
		end
	end
	if #tableOfValues > 5 or relCheck then
		local filterRels = frame:callParserFunction{ 
			name = '#widget', args = {
				'FilterRelationships',
				id = 'Relationships',
				placeholder = 'Filter by relationships...'
			} 
		}
		local filterPages = frame:callParserFunction{ 
			name = '#widget', args = {
				'FilterRelationships',
				id = 'Pages',
				placeholder = 'Filter by pages found...'
			} 
		}
		output = output .. '<tr><td>' .. filterRels .. '</td><td>' .. filterPages .. '</td></tr>' 
	end
end

function getVolNum(page)
	local volNumQr = mw.ext.externalData.getExternalData{
		url = 'https://research.tsadra.org/index.php?title=Special%3AAsk&q=' .. mw.uri.encode( '[[' .. page .. ']][[Volumenumber::+]]' ) .. '&po=Volumenumber&p%5Bmainlabel%5D=wikipage&p%5Blimit%5D=108&p%5Bformat%5D=csv&p%5Bvaluesep%5D=;&p%5Bdefault%5D=',
		data = { 
			wikipage = 'wikipage',
			Volumenumber = 'Volumenumber'
		},
		format = 'CSV with header'
	}

	if volNumQr then 
		for _, data in pairs( volNumQr ) do 
			return data.Volumenumber 
		end 
	else
		return 'no value'
	end
end

return p