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 pArgs = frame:getParent().args | ||
local pageA = mw.title.getCurrentTitle().subpageText | local pageA = mw.title.getCurrentTitle().subpageText | ||
local relsDirection = {} | local relsDirection = {} | ||
local verticRelsQueryData = mw.ext.externalData.getExternalData{ | local verticRelsQueryData = mw.ext.externalData.getExternalData{ | ||
| Line 20: | Line 17: | ||
end | end | ||
local horizRelsQueryData = mw.ext.externalData.getExternalData{ | local horizRelsQueryData = 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::Property:Relationship]][[RelPropDir::horizontal]]' ) .. '&po=RelPropName&p%5Blimit%5D=108&p%5Bformat%5D=csv', | ||
| Line 31: | Line 27: | ||
end | end | ||
local queryResult = mw.ext.externalData.getExternalData{ | local queryResult = 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( '[[-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', | ||
| Line 42: | Line 37: | ||
} | } | ||
local output = '' | local output = '' | ||
if queryResult == nil then return "No results!" end | if queryResult == nil then return "No results!" end | ||
output = output .. '<h1>Relationships</h1><div id="RelationshipsContent" class="alternating-bg border">' | output = output .. '<h1>Relationships</h1><div id="RelationshipsContent" class="alternating-bg border">' | ||
local InRels = {} | local InRels = {} | ||
local OutRels = {} | local OutRels = {} | ||
local InferredRels = {} | local InferredRels = {} | ||
for num, data in ipairs( queryResult ) do | for num, data in ipairs( queryResult ) do | ||
local pageB = frame:preprocess( '{{PAGENAME:' .. data.pageB .. '}}' ) | local pageB = frame:preprocess( '{{PAGENAME:' .. data.pageB .. '}}' ) | ||
local pageBEnc = mw.uri.anchorEncode( pageB ) | local pageBEnc = mw.uri.anchorEncode( pageB ) | ||
local pageAEnc = mw.uri.anchorEncode( pageA ) | local pageAEnc = mw.uri.anchorEncode( pageA ) | ||
if pageBEnc == pageAEnc then | if pageBEnc == pageAEnc then | ||
local relatedToPages = '' | local relatedToPages = '' | ||
if data.RelatedTo == nil then | if data.RelatedTo == nil then | ||
relatedToPages = '<li class="filterable"><span style="color:red;">No item specified!</span></li>' | relatedToPages = '<li class="filterable"><span style="color:red;">No item specified!</span></li>' | ||
| Line 101: | Line 69: | ||
end | end | ||
if OutRels[data.Relationship] then | if OutRels[data.Relationship] then | ||
OutRels[data.Relationship] = OutRels[data.Relationship] .. relatedToPages | OutRels[data.Relationship] = OutRels[data.Relationship] .. relatedToPages | ||
else | else | ||
if data.Relationship == nil then | if data.Relationship == nil then | ||
OutRels['<span style="color:red;">Relationship not set!</span>'] = relatedToPages | OutRels['<span style="color:red;">Relationship not set!</span>'] = relatedToPages | ||
| Line 115: | Line 81: | ||
else | else | ||
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'}, | ||
| Line 124: | Line 89: | ||
if mediaCheckQuery == nil or #mediaCheckQuery == 0 then | if mediaCheckQuery == nil or #mediaCheckQuery == 0 then | ||
local rel = '' | local rel = '' | ||
if data.Relationship == '' or data.Relationship == nil or data.Relationship == 0 then | 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>' | rel = '<span style="color: red; font-weight: bold;">Relationship not properly set!</span>' | ||
else | else | ||
| Line 134: | Line 96: | ||
end | end | ||
local pageBTitle = mw.ext.displaytitle.get(pageB) | local pageBTitle = mw.ext.displaytitle.get(pageB) | ||
if InRels[rel] then | if InRels[rel] then | ||
InRels[rel] = InRels[rel] .. '<li class="filterable">[[' .. pageB .. '|' .. pageBTitle .. ']]</li>' | InRels[rel] = InRels[rel] .. '<li class="filterable">[[' .. pageB .. '|' .. pageBTitle .. ']]</li>' | ||
| Line 144: | Line 104: | ||
end | end | ||
if relsDirection[rel] == 'horizontal' then | if relsDirection[rel] == 'horizontal' then | ||
local inferredQR = mw.ext.externalData.getExternalData{ | 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=;', | 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=;', | ||
| Line 156: | Line 113: | ||
} | } | ||
if inferredQR then | if inferredQR then | ||
for _, infData in ipairs( inferredQR ) do | for _, infData in ipairs( inferredQR ) do | ||
if infData.RelatedTo == pageB then | |||
local pageC = frame:preprocess( '{{PAGENAME:' .. infData.InferredPage .. '}}' ) | |||
local pageCTitle = mw.ext.displaytitle.get(pageC) | |||
if InRels[rel] then | |||
InRels[rel] = InRels[rel] .. '<li class="filterable">[[' .. pageC .. '|' .. pageCTitle .. ']]</li>' | InRels[rel] = InRels[rel] .. '<li class="filterable">[[' .. pageC .. '|' .. pageCTitle .. ']]</li>' | ||
else | else | ||
| Line 178: | Line 129: | ||
end | end | ||
elseif type(infData.RelatedTo) == "table" then | |||
for _, pageC in ipairs( infData.RelatedTo ) do | for _, pageC in ipairs( infData.RelatedTo ) do | ||
if pageC ~= pageA then | if pageC ~= pageA then | ||
local pageCTitle = mw.ext.displaytitle.get(pageC) | local pageCTitle = mw.ext.displaytitle.get(pageC) | ||
if InRels[rel] then | |||
InRels[rel] = InRels[rel] .. '<li class="filterable">[[' .. pageC .. '|' .. pageCTitle .. ']]</li>' | InRels[rel] = InRels[rel] .. '<li class="filterable">[[' .. pageC .. '|' .. pageCTitle .. ']]</li>' | ||
else | else | ||
| Line 203: | Line 148: | ||
if pageC ~= pageA then | if pageC ~= pageA then | ||
local pageCTitle = mw.ext.displaytitle.get(pageC) | local pageCTitle = mw.ext.displaytitle.get(pageC) | ||
if InRels[rel] then | |||
InRels[rel] = InRels[rel] .. '<li class="filterable">[[' .. pageC .. '|' .. pageCTitle .. ']]</li>' | InRels[rel] = InRels[rel] .. '<li class="filterable">[[' .. pageC .. '|' .. pageCTitle .. ']]</li>' | ||
else | else | ||
| Line 222: | Line 165: | ||
end | end | ||
for rel, vals in pairs( OutRels ) do | 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>' | 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 | ||
for rel, vals in pairs( InRels ) do | for rel, vals in pairs( InRels ) do | ||
if relsDirection[rel] == 'horizontal' then | if relsDirection[rel] == 'horizontal' then | ||
| Line 236: | Line 177: | ||
end | end | ||
output = output .. '</div>' | output = output .. '</div>' | ||
Revision as of 04:42, 7 August 2024
Documentation for this module may be created at Module:GetRels/doc
local p = {}
function p.main(frame)
local pArgs = frame:getParent().args
local pageA = mw.title.getCurrentTitle().subpageText
local relsDirection = {}
local verticRelsQueryData = 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',
data = { RelPropName = 'RelPropName'},
format = 'CSV with header'
}
for _, data in pairs( verticRelsQueryData ) do
relsDirection[data.RelPropName] = 'vertical'
end
local horizRelsQueryData = 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',
data = { RelPropName = 'RelPropName'},
format = 'CSV with header'
}
local horizRels = {}
for _, data in pairs( horizRelsQueryData ) do
relsDirection[data.RelPropName] = 'horizontal'
end
local queryResult = 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',
data = {
pageB = 'pageB',
RelatedTo = 'RelatedTo',
Relationship = 'Relationship'
},
format = 'CSV with header'
}
local output = ''
if queryResult == nil then return "No results!" end
output = output .. '<h1>Relationships</h1><div id="RelationshipsContent" class="alternating-bg border">'
local InRels = {}
local OutRels = {}
local InferredRels = {}
for num, data in ipairs( queryResult ) do
local pageB = frame:preprocess( '{{PAGENAME:' .. data.pageB .. '}}' )
local pageBEnc = mw.uri.anchorEncode( pageB )
local pageAEnc = mw.uri.anchorEncode( pageA )
if pageBEnc == pageAEnc then
local relatedToPages = ''
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
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
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 mediaCheckQuery == nil or #mediaCheckQuery == 0 then
local rel = ''
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)
if InRels[rel] then
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 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 _, infData in ipairs( inferredQR ) do
if infData.RelatedTo == pageB then
local pageC = frame:preprocess( '{{PAGENAME:' .. infData.InferredPage .. '}}' )
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
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
for rel, vals in pairs( InRels ) do
if relsDirection[rel] == 'horizontal' then
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>'
else
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>'
end
end
output = output .. '</div>'
return output
end
return p