Module:GetRels

From Bodhicitta
Revision as of 20:35, 8 April 2025 by Jeremi (talk | contribs) ((by SublimeText.Mediawiker))

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
	if pageA == "Bodhicaryāvatāra" then
		return ''
	else
		local DrlPagenameCheck = mw.ext.externalData.getExternalData{
			url = 'https://research.tsadra.org/index.php?title=Special%3AAsk&q=' .. mw.uri.encode( '[[' .. pageA .. ']]' ) .. '&p%5Bmainlabel%5D=Page&p%5Blimit%5D=1&p%5Bformat%5D=csv',
			data = { 
				pagename = 'Page'
			},
			format = 'CSV with header'
		}
		for _, data in pairs( DrlPagenameCheck ) do 
			if data.pagename then
				pageA = data.pagename
			end
		end
	end

	-- 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::<q>[[Category:BCA Project]]</q>]][[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( '[[-Has subobject::<q>[[Category:BCA Project]]</q>]][[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 ''
	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 pagesList = {}
			for page in string.gmatch(pages, "([^~]+)") do
				table.insert(pagesList, page)
			end
			pagesList = makeTblUnique(pagesList)
			local relName = ''
			if dir == 'left' then
				relName = getInverseRelName(rel)
			elseif dir == 'right' then
				relName = getRelName(rel)
			end

			output = output .. '<h4>' .. relName .. '</h4><div class="row">'
			for _, page in ipairs(pagesList) do
				local tileCheck = mw.smw.ask( '[[' .. page .. ']]' )
				if tileCheck then
					output = output .. frame:expandTemplate{ title = 'TileQueryNoDef', args = { 
						query = '[[' .. page .. ']]'
					} }
				else
					local pageLink = getPageLink(page)
					local NoExistTile = frame:preprocess( '<div class="col-md-6 mb-3 mb-md-4 filterable"><div class="tsdwiki-pop-off tsdwiki-tile tsdwiki-tile-library"><div class="tsdwiki-tile-library-content" style="flex: 0 0 100%; max-width: 100%;"><div class="h4 mb-2 p-0 clipped-2-lines border-0 text-140 line13 carnelian">' .. pageLink .. '</div><div class="text-muted clipped-3-lines unclamp-here carnelian"><b>PAGE MISSING!</b> Please click to create this library item or the redirect that is missing to direct it to the appropriate page.</div><div class="mt-1 mb-0 clipped-1-line carnelian font-italic text-right">Click to create <i class="fa-sharp-duotone fa-solid fa-rectangle-history-circle-plus"></i></div></div>{{NoExistTileLink|pageLink=' .. pageLink .. '}}</div></div>[[Category:Pages with red relationship tiles]]' )
					output = output .. NoExistTile
				end
			end
			output = output .. '</div><div class="clearfix"></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 ipairs( volNumQr ) do 
			return data.Volumenumber 
		end 
	else
		return 'no value'
	end
end

function makeTblUnique(indexedTable)
    local uniqueTbl = {}
    for _, value in ipairs(indexedTable) do
        uniqueTbl[value] = true
    end
    indexedTable = {}
    for value,_ in pairs(uniqueTbl) do
        table.insert(indexedTable, value)
    end
    return indexedTable
end

function getPageLink(page)
	local classQr = mw.ext.externalData.getExternalData{
		url = 'https://research.tsadra.org/index.php?title=Special%3AAsk&q=' .. mw.uri.encode( '[[' .. page .. ']][[Classification::+]]' ) .. '&po=Classification%0ADisplay%20title%20of&p%5Bmainlabel%5D=wikipage&p%5Blink%5D=none&p%5Blimit%5D=108&p%5Bformat%5D=csv&p%5Bvaluesep%5D=;',
		data = { 
			wikipage = 'wikipage',
			Classification = 'Classification',
			Title = 'Display title of'
		},
		format = 'CSV with header'
	}

	local classEquivalence = {
		['Articles'] = 'Articles' ,
		['Books'] = 'Books' ,
		['Classical Works'] = 'Texts' ,
		['Dissertations'] = 'Books' ,
		['Multimedia'] = 'Media' ,
		['Periodical Issues'] = 'Books' ,
		['Sanskrit Publications'] = 'Books' ,
		['Tibetan Publications'] = 'Books'
	}

	if classQr then 
		local plOutput = ''
		for _, data in ipairs( classQr ) do
			local pagename = ''
			if data.Title == '' or data.Title == nil then
				pagename = data.wikipage
			else
				pagename = string.gsub( data.Title, '%s?%(RKTS%w?%s?%d*%)', '' )
			end
			if classEquivalence[data.Classification] then
				plOutput = classEquivalence[data.Classification] .. '/' .. pagename
			else
				plOutput = data.Classification .. '/' .. pagename
			end
		end
		return plOutput
	else
		return 'no value'
	end
end

return p