Module:GetRels: Difference between revisions

From Bodhicitta
((by SublimeText.Mediawiker))
((by SublimeText.Mediawiker))
Line 3: Line 3:
function p.main(frame)
function p.main(frame)


local pArgs = frame:getParent().args
local debugStr = ''


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


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


local horizRelsQueryData = mw.ext.externalData.getExternalData{
local outgoingRelsQuery = mw.ext.externalData.getExternalData{
url = 'https://research.tsadra.org/index.php?title=Special%3AAsk&q=' .. mw.uri.encode( '[[-Has subobject::Property:Relationship]][[RelPropDir::horizontal]]' ) .. '&po=RelPropName&p%5Blimit%5D=108&p%5Bformat%5D=csv',
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',
data = { RelPropName = 'RelPropName'},
data = {
Page = 'Page',
RelatedTo = 'RelatedTo',
Relationship = 'Relationship'
},
format = 'CSV with header'
format = 'CSV with header'
}
}
local horizRels = {}
    for _, data in pairs( horizRelsQueryData ) do
    relsDirection[data.RelPropName] = 'horizontal'
    end


local queryResult = mw.ext.externalData.getExternalData{
local incomingRelsQuery = mw.ext.externalData.getExternalData{
url = 'https://research.tsadra.org/index.php?title=Special%3AAsk&q=' .. mw.uri.encode( '[[-Has subobject::' .. mw.title.getCurrentTitle().subpageText .. ']][[RelatedTo::+]][[RelationshipType::!ResourceToPerson]]OR[[RelatedTo::' .. mw.title.getCurrentTitle().subpageText .. ']][[RelationshipType::!ResourceToPerson]]' ) .. '&po=RelatedTo%0ARelationship&p%5Blimit%5D=10000&p%5Bmainlabel%5D=pageB&p%5Bformat%5D=csv',
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',
data = {
data = {
pageB = 'pageB',
Page = 'Page',
RelatedTo = 'RelatedTo',
RelatedTo = 'RelatedTo',
Relationship = 'Relationship'
Relationship = 'Relationship'
Line 37: Line 40:
}
}


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


if queryResult == nil then return "No results!" end
local rel = ''
local allRels = {}


    output = output .. '<h1>Relationships</h1><div id="RelationshipsContent" class="alternating-bg border">'
if outgoingRelsQuery == nil then else


for _, data in pairs( outgoingRelsQuery ) do


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


    for num, data in ipairs( queryResult ) do
if relsDirection[rel] == 'horizontal' then
if type(data.RelatedTo) == "table" then
for _, page in ipairs( data.RelatedTo ) do
allRels[rel][page] = 'both'
end
else
allRels[rel][data.RelatedTo] = 'both'
end
elseif type(data.RelatedTo) == "table" then
for _, page in ipairs( data.RelatedTo ) do
allRels[rel][page] = 'right'
end
else
allRels[rel][data.RelatedTo] = 'right'
end


    local pageB = frame:preprocess( '{{PAGENAME:' .. data.pageB .. '}}' )
end
    local pageBEnc = mw.uri.anchorEncode( pageB )
    local pageAEnc = mw.uri.anchorEncode( pageA )


    if pageBEnc == pageAEnc then
end


    local relatedToPages = ''
local setSubobjects = {}
    if data.RelatedTo == nil then
    relatedToPages = '<li class="filterable"><span style="color:red;">No item specified!</span></li>'
    else
    if type(data.RelatedTo) == "table" then
    for num, value in ipairs( data.RelatedTo ) do
    relatedToPages = relatedToPages .. '<li class="filterable">[[' .. value .. ']]</li>'
    end
else
relatedToPages = '<li class="filterable">[[' .. data.RelatedTo .. ']]</li>'
end
end


    if OutRels[data.Relationship] then
if incomingRelsQuery == nil then else
    OutRels[data.Relationship] = OutRels[data.Relationship] .. relatedToPages
    else
    if data.Relationship == nil then
    OutRels['<span style="color:red;">Relationship not set!</span>'] = relatedToPages
    else
    OutRels[data.Relationship] = relatedToPages
    end
    end


    else
for _, data in pairs( incomingRelsQuery ) do


        local mediaCheckQuery = mw.ext.externalData.getExternalData{
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%5Bdefault%5D=',
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%5Bdefault%5D=',
data = { wikipage = 'wikipage'},
data = { wikipage = 'wikipage'},
format = 'CSV with header'
format = 'CSV with header'
}
}
if not mediaCheckQuery then


if mediaCheckQuery == nil or #mediaCheckQuery == 0 then
if type(data.RelatedTo) == "table" and relsDirection[rel] == 'vertical' then
allRels[rel][pageB] = 'left'
elseif type(data.RelatedTo) == "table" then
for _, page in ipairs( 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
allRels[rel][pageB] = 'both'
setSubobjects[rel] = pageB
else
allRels[rel][page] = 'both'
setSubobjects[rel] = page
end
end
elseif relsDirection[rel] == 'horizontal' then
if (rel == "Is another edition of" and getVolNum(pageA) ~= 'no value' and getVolNum(pageB) ~= 'no value' and getVolNum(pageB) ~= getVolNum(pageA)) then
else
allRels[rel][pageB] = 'both'
setSubobjects[rel] = pageB
end
else
allRels[rel][pageB] = 'left'
end


    local rel = ''
end
    if data.Relationship == '' or data.Relationship == nil or data.Relationship == 0 then
    rel = '<span style="color: red; font-weight: bold;">Relationship not properly set!</span>'
    else
    rel = data.Relationship
    end


    local pageBTitle = mw.ext.displaytitle.get(pageB)
end


    if InRels[rel] then
end
    InRels[rel] = InRels[rel] .. '<li class="filterable">[[' .. pageB .. '|' .. pageBTitle .. ']]</li>'
    else
    InRels[rel] = '<li class="filterable">[[' .. pageB .. '|' .. pageBTitle .. ']]</li>'
    end


    if relsDirection[rel] == 'horizontal' then
local output = '<table id="RelationshipsContent" class="smwtable-clean th-top-right th-nowrap border w-100">'


   
checkIfNeedFilter(allRels)
local inferredQR = mw.ext.externalData.getExternalData{
url = 'https://research.tsadra.org/index.php?title=Special%3AAsk&q=' .. mw.uri.encode( '[[-Has subobject::!' .. pageA .. ']][[RelatedTo::' .. pageB .. ']][[Relationship::' .. rel .. ']]OR[[-Has subobject::' .. pageB .. ']][[Relationship::' .. rel .. ']]' , PATH ) .. '&po=RelatedTo&p%5Bmainlabel%5D=InferredPage&p%5Blimit%5D=10000&p%5Blink%5D=none&p%5Bformat%5D=csv&p%5Bvaluesep%5D=;',
data = { InferredPage = 'InferredPage', RelatedTo = 'RelatedTo' },
format = 'CSV with header'
}


if inferredQR then
for rel, pages in pairs( allRels ) do
output = output .. '<tr><th>' .. rel .. '</th><td><div class="sort-ascending">'
for page, dir in pairs( pages ) do
local pageTitle
if mw.ext.displaytitle.get(page) then
pageTitle = page .. '|' .. mw.ext.displaytitle.get(page)
else
pageTitle = page
end
if dir == 'left' then
output = output .. '<div class="filterable"><i class="fa-sharp-duotone fa-solid fa-angles-left fa-fw" style="color: blue;"></i> [[' .. pageTitle .. ']]</div>'
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> [[' .. pageTitle .. ']]</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> [[' .. pageTitle .. ']]</div>'
else
output = output .. '<div class="filterable">[[' .. pageTitle .. ']]</div>'
end
end
output = output .. '</div></td></tr>'
end


    for _, infData in ipairs( inferredQR ) do
output = output .. '</table>'


        if infData.RelatedTo == pageB then
-- output = debugStr .. '<br>' .. output


        local pageC = frame:preprocess( '{{PAGENAME:' .. infData.InferredPage .. '}}' )
return output


        local pageCTitle = mw.ext.displaytitle.get(pageC)
end


        if InRels[rel] then
function checkIfNeedFilter(tableOfValues)
    InRels[rel] = InRels[rel] .. '<li class="filterable">[[' .. pageC .. '|' .. pageCTitle .. ']]</li>'
local relCheck
    else
for i, v in pairs(tableOfValues) do
    InRels[rel] = '<li class="filterable">[[' .. pageC .. '|' .. pageCTitle .. ']]</li>'
if #v > 10 then
    end
relCheck = true
 
end
            elseif type(infData.RelatedTo) == "table" then
 
    for _, pageC in ipairs( infData.RelatedTo ) do
 
    if pageC ~= pageA then
 
    local pageCTitle = mw.ext.displaytitle.get(pageC)
 
        if InRels[rel] then
    InRels[rel] = InRels[rel] .. '<li class="filterable">[[' .. pageC .. '|' .. pageCTitle .. ']]</li>'
    else
    InRels[rel] = '<li class="filterable">[[' .. pageC .. '|' .. pageCTitle .. ']]</li>'
    end
    end
    end
    else
    for pageC in string.gmatch(infData.RelatedTo, "([^;]+)") do
    if pageC ~= pageA then
 
    local pageCTitle = mw.ext.displaytitle.get(pageC)
 
        if InRels[rel] then
    InRels[rel] = InRels[rel] .. '<li class="filterable">[[' .. pageC .. '|' .. pageCTitle .. ']]</li>'
    else
    InRels[rel] = '<li class="filterable">[[' .. pageC .. '|' .. pageCTitle .. ']]</li>'
    end
    end
    end
    end
    end
    end
end
end
    end
    end
 
    for rel, vals in pairs( OutRels ) do
    output = output .. '<div class="row m-0"><div class="col-10 col-md-4 p-2 font-weight-bold">' .. rel .. '</div><div class="col-2 col-md-1 p-2 text-center"><i class="fa-duotone fa-arrow-right" style="color: green;"></i></div><div class="col-md-7 p-2"><ol style="margin: 0 1.3em;" class="sort-ascending">' .. vals .. '</ol></div></div>'
end
end
 
if #tableOfValues > 5 or relCheck then
    for rel, vals in pairs( InRels ) do
local filterRels = frame:callParserFunction{
    if relsDirection[rel] == 'horizontal' then
name = '#widget', args = {
output = output .. '<div class="row m-0"><div class="col-10 col-md-4 p-2 font-weight-bold">' .. rel .. '</div><div class="col-2 col-md-1 p-2 text-center"><i class="fa-duotone fa-arrow-left" style="color: blue;"></i></div><div class="col-md-7 p-2"><ol style="margin: 0 1.3em;" class="sort-ascending">' .. vals .. '</ol></div></div>'
'FilterRelationships',
else
id = 'Relationships',
output = output .. '<div class="row m-0"><div class="col-10 col-md-4 p-2 font-weight-bold">' .. rel .. ' <i>this work</i></div><div class="col-2 col-md-1 p-2 text-center"><i class="fa-duotone fa-arrow-left" style="color: blue;"></i></div><div class="col-md-7 p-2"><ol style="margin: 0 1.3em;" class="sort-ascending">' .. vals .. '</ol></div></div>'
placeholder = 'Filter by relationships...'
end
}
}
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
end


    output = output .. '</div>'
function getVolNum(page)
 
local volNumQr = mw.ext.externalData.getExternalData{
    return output
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%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
end


return p
return p

Revision as of 09:09, 7 August 2024

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

local p = {}

function p.main(frame)

	local debugStr = ''

	-- 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',
		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',
		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 pairs( 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
				if type(data.RelatedTo) == "table" then
					for _, page in ipairs( data.RelatedTo ) do
						allRels[rel][page] = 'both'
					end
				else
					allRels[rel][data.RelatedTo] = 'both'
				end
			elseif type(data.RelatedTo) == "table" then
				for _, page in ipairs( data.RelatedTo ) do
					allRels[rel][page] = 'right'
				end
			else
				allRels[rel][data.RelatedTo] = 'right'
			end

		end

	end

	local setSubobjects = {}

	if incomingRelsQuery == nil then else

		for _, data in pairs( 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%5Bdefault%5D=',
				data = { wikipage = 'wikipage'},
				format = 'CSV with header'
			}
			
			if not mediaCheckQuery then

				if type(data.RelatedTo) == "table" and relsDirection[rel] == 'vertical' then
					allRels[rel][pageB] = 'left'
				elseif type(data.RelatedTo) == "table" then
					for _, page in ipairs( 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
							allRels[rel][pageB] = 'both'
							setSubobjects[rel] = pageB
						else
							allRels[rel][page] = 'both'
							setSubobjects[rel] = page
						end
					end
				elseif relsDirection[rel] == 'horizontal' then
					if (rel == "Is another edition of" and getVolNum(pageA) ~= 'no value' and getVolNum(pageB) ~= 'no value' and getVolNum(pageB) ~= getVolNum(pageA)) then
					else
						allRels[rel][pageB] = 'both'
						setSubobjects[rel] = pageB
					end
				else
					allRels[rel][pageB] = 'left'
				end

			end

		end

	end

	local output = '<table id="RelationshipsContent" class="smwtable-clean th-top-right th-nowrap border w-100">'

	checkIfNeedFilter(allRels)

	for rel, pages in pairs( allRels ) do
		output = output .. '<tr><th>' .. rel .. '</th><td><div class="sort-ascending">'
		for page, dir in pairs( pages ) do
			local pageTitle
			if mw.ext.displaytitle.get(page) then
				pageTitle = page .. '|' .. mw.ext.displaytitle.get(page)
			else
				pageTitle = page
			end
			if dir == 'left' then
				output = output .. '<div class="filterable"><i class="fa-sharp-duotone fa-solid fa-angles-left fa-fw" style="color: blue;"></i> [[' .. pageTitle .. ']]</div>'
			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> [[' .. pageTitle .. ']]</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> [[' .. pageTitle .. ']]</div>'
			else
				output = output .. '<div class="filterable">[[' .. pageTitle .. ']]</div>'
			end
		end
		output = output .. '</div></td></tr>'
	end

	output = output .. '</table>'

	-- 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%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