Module:Graph
From mcr
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, people )
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 .. people .. '</TD></TR></TABLE>>'
end
function p.getNodes( category, depends_on_property, status_property,
people_property, tag_property )
local nodes = {};
local result = mw.smw.getQueryResult( '[[Category:' .. category .. ']]|?' ..
depends_on_property .. '|?' .. status_property .. '|?' ..
people_property .. '|?' .. tag_property )
if result ~= nil then
for k1,v1 in pairs( result.results ) do
nodes[v1.fulltext] = {}
local people = table.concat( v1.printouts[people_property], ', ' )
if ( #people > 0 ) then
people = '<BR/>(' .. people .. ')'
else
people = ''
end
nodes[v1.fulltext].displaytitle =
p.cleanseDisplayTitle( v1.displaytitle, people )
nodes[v1.fulltext].status = v1.printouts[status_property][1]
nodes[v1.fulltext].dependencies = {}
for k2,v2 in pairs ( v1.printouts[depends_on_property] ) do
nodes[v1.fulltext].dependencies[v2.fulltext] = true
end
nodes[v1.fulltext].tags = v1.printouts[tag_property]
end
end
return nodes
end
function p.getPageName( title )
return string.sub( title, string.find( title, ':' ) + 1 )
end
function p.contains( t, value )
for k,v in pairs ( t ) do
if v == value then
return true
end
end
return false
end
function p.drawGraph( category, depends_on_property, status_property,
people_property, tag_property, forward )
local nodes = p.getNodes( category, depends_on_property, status_property,
people_property, tag_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'
s = s .. 'edge [color=gray75];\n'
for k1,v1 in pairs( nodes ) do
s = s .. p.getPageName( k1 ) .. ' [URL="[[' .. k1 .. ']]", label=' .. v1.displaytitle
if ( v1.status == 'Planning' ) then
s = s .. ', color=darkgoldenrod, fontcolor=darkgoldenrod'
elseif ( v1.status == 'In Progress' ) then
s = s .. ', color=darkgreen, fontcolor=darkgreen'
elseif ( v1.status == 'Complete' ) then
s = s .. ', color=blue3, fontcolor=blue3'
elseif ( v1.status == 'Tracking Task' ) then
s = s .. ', color=gray50, fontcolor=gray50'
end
if p.contains( v1.tags, 'Deployment' ) then
s = s .. ', shape=cds'
elseif p.contains( v1.tags, 'Epic' ) then
s = s .. ', shape=box3d'
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.dependency_graph(frame)
local s = p.drawGraph( 'Phabricator Tasks', 'Depends On', 'Status', 'Person', 'Tag', true )
return frame:callParserFunction{ name = '#tag', args = { 'graphviz', format='png', s } }
end
function p.pert_chart(frame)
local s = p.drawGraph( 'Phabricator Tasks', 'Depends On', 'Status', 'Person', 'Tag', false )
return frame:callParserFunction{ name = '#tag', args = { 'graphviz', format='png', s } }
end
return p