Module:Graph: Difference between revisions
From mcr
No edit summary |
No edit summary |
||
Line 26: | Line 26: | ||
end | end | ||
function p.getNodes( category, | function p.getNodes( category, depends_on_property, status_property ) | ||
local nodes = {}; | local nodes = {}; | ||
local result = mw.smw.getQueryResult( '[[Category:' .. category .. ']]|?' .. | local result = mw.smw.getQueryResult( '[[Category:' .. category .. ']]|?' .. depends_on_property .. '|?' .. status_property ) | ||
if result ~= nil then | if result ~= nil then | ||
for k1,v1 in pairs( result.results ) do | for k1,v1 in pairs( result.results ) do | ||
nodes[v1.fulltext] = {} | nodes[v1.fulltext] = {} | ||
nodes[v1.fulltext].displaytitle = p.cleanseDisplayTitle( v1.displaytitle ) | nodes[v1.fulltext].displaytitle = p.cleanseDisplayTitle( v1.displaytitle ) | ||
nodes[v1.fulltext].status = v1.printouts[status_property] | |||
nodes[v1.fulltext].dependencies = {} | nodes[v1.fulltext].dependencies = {} | ||
for k2,v2 in pairs ( v1.printouts[ | for k2,v2 in pairs ( v1.printouts[depends_on_property] ) do | ||
nodes[v1.fulltext].dependencies[v2.fulltext] = true | nodes[v1.fulltext].dependencies[v2.fulltext] = true | ||
end | end | ||
Line 46: | Line 47: | ||
end | end | ||
function p.drawGraph( category, | function p.drawGraph( category, depends_on_property, status_property, forward ) | ||
local nodes = p.getNodes( category, | local nodes = p.getNodes( category, depends_on_property, status_property ) | ||
local s = 'digraph ' | local s = 'digraph ' | ||
if ( forward ) then | if ( forward ) then | ||
Line 59: | Line 60: | ||
for k1,v1 in pairs( nodes ) do | for k1,v1 in pairs( nodes ) do | ||
s = s .. p.getPageName( k1 ) .. ' [URL="[[' .. k1 .. ']]", label=' .. v1.displaytitle .. ', color= | s = s .. p.getPageName( k1 ) .. ' [URL="[[' .. k1 .. ']]", label=' .. v1.displaytitle | ||
if ( v1.status == 'In Progress' ) then | |||
s = s .. ', color=orange, fontcolor=orange' | |||
elseif ( v1.status == 'Complete' ) then | |||
s = s .. ', color=green, fontcolor=green' | |||
end | |||
s = s .. '];\n' | |||
end | end | ||
for k1,v1 in pairs( nodes ) do | for k1,v1 in pairs( nodes ) do | ||
Line 75: | Line 82: | ||
function p.draw(frame) | function p.draw(frame) | ||
local s = p.drawGraph( frame.args[1], frame.args[2], true ) | local s = p.drawGraph( frame.args[1], frame.args[2], 'Status', true ) | ||
return frame:callParserFunction{ name = '#tag', args = { 'graphviz', format='png', s } } | return frame:callParserFunction{ name = '#tag', args = { 'graphviz', format='png', s } } | ||
end | end | ||
function p.reverse(frame) | function p.reverse(frame) | ||
local s = p.drawGraph( frame.args[1], frame.args[2], false ) | local s = p.drawGraph( frame.args[1], frame.args[2], 'Status', false ) | ||
return frame:callParserFunction{ name = '#tag', args = { 'graphviz', format='png', s } } | return frame:callParserFunction{ name = '#tag', args = { 'graphviz', format='png', s } } | ||
end | end | ||
return p | return p |
Revision as of 09:27, 2 January 2018
Documentation for this module may be created at Module:Graph/doc
local p = {}
function p.wrap( s, length )
s = mw.text.trim( s )
local oldindex = 1
local index = string.find( s, ' ' )
while ( index ) do
if ( index - oldindex >= length ) then
local temp = string.sub( s, 1, index - 1 ) .. '<BR/>' .. string.sub( s, index + 1 )
s = temp
oldindex = index + 4
end
index = string.find( s, ' ', index + 1)
end
return s
end
function p.cleanseDisplayTitle( displaytitle )
local s = displaytitle
s = string.gsub( s, '"', '' )
s = string.gsub( s, '%[', '' )
s = string.gsub( s, '%]', '' )
s = string.gsub( s, '&', 'and' )
s = p.wrap ( s, 15 )
return '<<TABLE BORDER="0"><TR><TD>' .. s .. '</TD></TR></TABLE>>'
end
function p.getNodes( category, depends_on_property, status_property )
local nodes = {};
local result = mw.smw.getQueryResult( '[[Category:' .. category .. ']]|?' .. depends_on_property .. '|?' .. status_property )
if result ~= nil then
for k1,v1 in pairs( result.results ) do
nodes[v1.fulltext] = {}
nodes[v1.fulltext].displaytitle = p.cleanseDisplayTitle( v1.displaytitle )
nodes[v1.fulltext].status = v1.printouts[status_property]
nodes[v1.fulltext].dependencies = {}
for k2,v2 in pairs ( v1.printouts[depends_on_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, ':' ) + 1 )
end
function p.drawGraph( category, depends_on_property, status_property, forward )
local nodes = p.getNodes( category, depends_on_property, status_property )
local s = 'digraph '
if ( forward ) then
s = s .. 'dependency_graph_forward'
else
s = s .. 'dependency_graph_reverse'
end
s = s .. ' {\n'
s = s .. 'rankdir=LR;\n'
s = s .. 'node [shape=box, fontsize=10, fontname="Arial"];\n'
for k1,v1 in pairs( nodes ) do
s = s .. p.getPageName( k1 ) .. ' [URL="[[' .. k1 .. ']]", label=' .. v1.displaytitle
if ( v1.status == 'In Progress' ) then
s = s .. ', color=orange, fontcolor=orange'
elseif ( v1.status == 'Complete' ) then
s = s .. ', color=green, fontcolor=green'
end
s = s .. '];\n'
end
for k1,v1 in pairs( nodes ) do
for k2, v2 in pairs ( v1.dependencies ) do
if ( forward ) then
s = s .. p.getPageName( k1 ) .. '->' .. p.getPageName( k2 ) .. '\n'
else
s = s .. p.getPageName( k2 ) .. '->' .. p.getPageName( k1 ) .. '\n'
end
end
end
s = s .. '}\n'
return s
end
function p.draw(frame)
local s = p.drawGraph( frame.args[1], frame.args[2], 'Status', true )
return frame:callParserFunction{ name = '#tag', args = { 'graphviz', format='png', s } }
end
function p.reverse(frame)
local s = p.drawGraph( frame.args[1], frame.args[2], 'Status', false )
return frame:callParserFunction{ name = '#tag', args = { 'graphviz', format='png', s } }
end
return p