Module:Graph: Difference between revisions
From mcr
No edit summary |
No edit summary |
||
Line 1: | Line 1: | ||
local p = {} | local p = {} | ||
function p.getNodes(category, property) | function p.getNodes( category, property ) | ||
local nodes = {}; | local nodes = {}; | ||
local result = mw.smw.getQueryResult( '[[Category:' .. category .. ']]|?' .. property ) | local result = mw.smw.getQueryResult( '[[Category:' .. category .. ']]|?' .. property ) | ||
Line 9: | Line 9: | ||
nodes[v1.fulltext].displaytitle = v1.displaytitle | nodes[v1.fulltext].displaytitle = v1.displaytitle | ||
nodes[v1.fulltext].dependencies = {} | nodes[v1.fulltext].dependencies = {} | ||
for k2,v2 in pairs (v1.printouts[property]) do | for k2,v2 in pairs ( v1.printouts[property] ) do | ||
nodes[v1.fulltext].dependencies[v2.fulltext] = true | nodes[v1.fulltext].dependencies[v2.fulltext] = true | ||
end | end | ||
Line 15: | Line 15: | ||
end | end | ||
return nodes | return nodes | ||
end | |||
function p.getPageName( title ) | |||
return string.sub( title, string.find( title, ':' ) ) | |||
end | end | ||
Line 26: | Line 30: | ||
-- Mollusca->X1 | -- Mollusca->X1 | ||
for k1,v1 in pairs( nodes ) do | for k1,v1 in pairs( nodes ) do | ||
s = s .. p.getPageName( k1 ) .. ' [URL="[[' .. k1 .. '|' .. v1.displaytitle .. ']]"];\n' | |||
end | end | ||
for k1,v1 in pairs( nodes ) do | for k1,v1 in pairs( nodes ) do | ||
for k2, v2 in pairs ( v1.dependencies ) do | for k2, v2 in pairs ( v1.dependencies ) do | ||
s = s .. p.getPageName( k1 ) .. '->' .. p.getPageName( k2 ) .. '\n' | |||
end | end | ||
end | end | ||
Line 36: | Line 40: | ||
--s = s .. 'A->B\n' | --s = s .. 'A->B\n' | ||
--s = s .. 'A->C\n' | --s = s .. 'A->C\n' | ||
s = s .. '}\n' | s = s .. '}\n' | ||
mw.logObject(s) | mw.logObject(s) |
Revision as of 12:42, 24 December 2017
Documentation for this module may be created at Module:Graph/doc
local p = {}
function p.getNodes( category, property )
local nodes = {};
local result = mw.smw.getQueryResult( '[[Category:' .. category .. ']]|?' .. property )
if result ~= nil then
for k1,v1 in pairs( result.results ) do
nodes[v1.fulltext] = {}
nodes[v1.fulltext].displaytitle = v1.displaytitle
nodes[v1.fulltext].dependencies = {}
for k2,v2 in pairs ( v1.printouts[property] ) do
nodes[v1.fulltext].dependencies[v2.fulltext] = true
end
end
end
return nodes
end
function p.getPageName( title )
return string.sub( title, string.find( title, ':' ) )
end
function p.draw(frame)
local nodes = p.getNodes( frame.args[1], frame.args[2] )
local s = 'digraph dependency_graph {\n'
s = s .. 'rankdir=LR;\n'
s = s .. 'node [shape=plaintext,fontname="Arial"];\n'
-- Mollusca [URL="[[Phab:T107595|hello world]]"];
-- Mollusca->X1
for k1,v1 in pairs( nodes ) do
s = s .. p.getPageName( k1 ) .. ' [URL="[[' .. k1 .. '|' .. v1.displaytitle .. ']]"];\n'
end
for k1,v1 in pairs( nodes ) do
for k2, v2 in pairs ( v1.dependencies ) do
s = s .. p.getPageName( k1 ) .. '->' .. p.getPageName( k2 ) .. '\n'
end
end
--s = s .. 'A->B\n'
--s = s .. 'A->C\n'
s = s .. '}\n'
mw.logObject(s)
return frame:callParserFunction{ name = '#tag', args = { 'graphviz', format='png', s } }
end
return p