Module:GetRels: Difference between revisions

From Bodhicitta
((by SublimeText.Mediawiker))
((by SublimeText.Mediawiker))
Line 115: Line 115:
for page, dir in pairs( pages ) do
for page, dir in pairs( pages ) do
if dir == 'left' then
if dir == 'left' then
output = output .. frame:expandTemplate{ title = 'RelationshipTiles', args = { page } }
output = output .. frame:expandTemplate{ title = 'RelationshipTile', args = { page } }
elseif dir == 'right' then
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>'
output = output .. '<div class="filterable"><i class="fa-sharp-duotone fa-solid fa-angles-right fa-fw" style="color: green;"></i> [[' .. page .. ']]</div>'

Revision as of 18:47, 9 September 2024

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

local p = {}

function p.main(frame)

	local debugStr = ''
	local output = ''

	-- 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>' .. getInverseRelName(rel) .. '</h1><div class="row">'
		for page, dir in pairs( pages ) do
			if dir == 'left' then
				output = output .. frame:expandTemplate{ title = 'RelationshipTile', args = { page } }
			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 = debugStr .. '<br>' .. output

	return output

end

function getRelName(rel)
    local names = {
		["Contains a full translation of"] = 'Translation of',
		["Contains a modern commentary of"] = 'Commentary of',
		["Contains a partial translation of"] = 'Partial translation of',
		["Contains a translated commentary of"] = 'Translated commentary of',
		["Contains a recension of"] = 'Recension of',
		["Contains a review of"] = 'Review of',
		["Contains a teaching based on"] = 'Teaching based on',
		["Contains a translation of"] = 'Translation of',
		["Contains polemical content related to"] = 'Polemical content on',
		["Contains scholarship related to"] = 'Scholarship on',
		["Has a title identical or similar to"] = 'Similar title',
		["Has other volumes"] = 'Other volumes',
		["Is a book based on this dissertation"] = 'Based on dissertation',
		["Is a chapter or a part of"] = 'Chapter or part of',
		["Is a commentary of"] = 'Commentary of',
		["Is a member of"] = 'Member of',
		["Is a reading of"] = 'Reading of',
		["Is a scholarly presentation about"] = 'Scholarly presentation about',
		["Is a teaching based on"] = 'Teaching on',
		["Is an interview about"] = 'Interview about',
		["Is another edition of"] = 'Other editions',
		["Is related to"] = 'Related',
		["Shares intertextuality with"] = 'Shares intertextuality'
    }
    if names[rel] then
 	   return names[rel]
 	else
 		return rel
 	end
end

function getInverseRelName(rel)
    local names = {
		["Contains a full translation of"] = 'Full translations',
		["Contains a modern commentary of"] = 'Commentaries',
		["Contains a partial translation of"] = 'Partial translations',
		["Contains a translated commentary of"] = 'Translated Commentaries',
		["Contains a recension of"] = 'Recensions',
		["Contains a review of"] = 'Reviews',
		["Contains a teaching based on"] = 'Teachings',
		["Contains a translation of"] = 'Translations',
		["Contains polemical content related to"] = 'Polemical content',
		["Contains scholarship related to"] = 'Scholarship',
		["Has a title identical or similar to"] = 'Similar title',
		["Has other volumes"] = 'Other volumes',
		["Is a book based on this dissertation"] = 'Based on dissertation',
		["Is a chapter or a part of"] = 'Contains chapter or part',
		["Is a commentary of"] = 'Commentaries',
		["Is a member of"] = 'Contains members',
		["Is a reading of"] = 'Readings',
		["Is a scholarly presentation about"] = 'Scholarly presentations',
		["Is a teaching based on"] = 'Teachings',
		["Is an interview about"] = 'Interviews',
		["Is another edition of"] = 'Other editions',
		["Is related to"] = 'Related',
		["Shares intertextuality with"] = 'Shares intertextuality'
    }
    if names[rel] then
 	   return names[rel]
 	else
 		return rel
 	end
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