Module:GetRels: Difference between revisions

From Bodhicitta
((by SublimeText.Mediawiker))
Tag: Reverted
((by SublimeText.Mediawiker))
Tag: Reverted
Line 126: Line 126:


output = output .. '<h1>' .. relName .. '</h1><div class="row">'
output = output .. '<h1>' .. relName .. '</h1><div class="row">'
pages = string.gsub( pages, "'", "\'" )
pages = string.gsub( pages, "'", "\\'" )
for page in string.gmatch(pages, "([^;]+)") do
for page in string.gmatch(pages, "([^;]+)") do
output = output .. frame:expandTemplate{ title = 'RelationshipTile', args = { page } }
output = output .. frame:expandTemplate{ title = 'RelationshipTile', args = { page } }

Revision as of 21:38, 10 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 allRels[rel]['right'] then
				allRels[rel]['right'] = allRels[rel]['right'] .. ';' .. data.RelatedTo
			else
				allRels[rel]['right'] = data.RelatedTo
			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
					if allRels[rel]['left'] then
						allRels[rel]['left'] = allRels[rel]['left'] .. ';' .. pageB
					else
						allRels[rel]['left'] = pageB
					end
				else
					for page in string.gmatch(data.RelatedTo, "([^;]+)") 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
							if allRels[rel]['right'] then
								allRels[rel]['right'] = allRels[rel]['right'] .. ';' .. pageB
							else
								allRels[rel]['right'] = pageB
							end
						else
							if allRels[rel]['right'] then
								allRels[rel]['right'] = allRels[rel]['right'] .. ';' .. page
							else
								allRels[rel]['right'] = page
							end
						end

					end
				end

			end

		end

	end

	checkIfNeedFilter(allRels)

	for rel, dirs in pairs( allRels ) do
		for dir, pages in pairs( dirs ) do
			local relName = ''
			if dir == 'left' then
				relName = getInverseRelName(rel)
			elseif dir == 'right' then
				relName = getRelName(rel)
			end

			output = output .. '<h1>' .. relName .. '</h1><div class="row">'
			pages = string.gsub( pages, "'", "\\'" )
			for page in string.gmatch(pages, "([^;]+)") do
				output = output .. frame:expandTemplate{ title = 'RelationshipTile', args = { page } }
			end
			output = output .. '</div>'
	
		end
	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