You are here: Foswiki>Tasks Web>Item14361 (19 Feb 2024, MarcSCHAEFER)Edit Attach

Item14361: GraphVizPlugin: not clear how external images may be used

pencil
Priority: Normal
Current State: Being Worked On
Released In: n/a
Target Release: n/a
Applies To: Extension
Component: GraphvizPlugin
Branches:
Reported By: JohnKnutson
Waiting For: BramVanOosterhout, MichaelDaum
Last Change By: MarcSCHAEFER

You can create a graphviz diagram using a definition like the following:

<graphviz>
digraph "G"
{
   req0021 [ shape=plaintext,label=<
<table style="rounded" border="1" cellborder="0" cellpadding="0" cellspacing="0" color="#b7c8dc">
  <tr><td gradientangle="90" bgcolor="#f3f8ff:#d4e7fe" width="20"><img scale="false" src="stream-development-badge.png"/></td><td gradientangle="90" bgcolor="#f3f8ff:#d4e7fe" width="80" align="left" valign="middle">req0021</td></tr>
</table>
> ]
}
</graphviz>

This doesn't work as dot can't find stream-development-badge.png. I haven't bothered attaching it because it doesn't matter either way. The first time you save a topic on a wiki with GraphVizPlugin installed and enabled, you'll get a text box with dot error output in it about the missing png file, but if you reload that topic again it will render the graph without the image. Not sure why the behavior differs. Mostly I'd just like to know how to correctly set the path to the png so that the plugin will properly render the graph, assuming the png is attached to the topic.

I thought, based on a look at the plugin code, that maybe it was running dot in the pub/topic directory, but if that were the case it should have found the png file.

-- JohnKnutson - 29 Mar 2017

Note that the above graph definition works fine if you run dot on it directly (without the graphviz tags obviously) so long as you have the png files in the directory.

-- JohnKnutson - 29 Mar 2017

I changed my mind, I'm attaching both the embedded image and the results from running dot on the command-line.

-- JohnKnutson - 29 Mar 2017

Thinking about it, my guess as to why the error messages only appear on topic save is that dot actually does generate a graph (With warnings, maybe not errors) so that cached image is used on reload.

After experimenting a bit, it appears dot is being run in the root directory on the web host, but while I get no errors specifying the full path name (not an ideal approach), I also didn't get the image in the output as shown below.

-- JohnKnutson - 29 Mar 2017

Diagnosis and possible solutions

The graph will be produced with the default type (type="svg"). Creating SVG graphs that contain images has a few problems.

Issue 1 type="svg" inline="off"

With the default setting for the inline parameter (inline="off") the generated .svg file will be attached to the topic. The .svg file will contain references to the images like:
<image xlink:href="Sun_Workstation.png" width="76px" height="64px" preserveAspectRatio="xMinYMin meet" x="34.832" y="-67.3553"/>
So the referred images (like: Sun_Workstation.png) must be in the same directory as the generated .svg file. They will be if they also are attached to the topic as required in the documentation. However... The attached .svg file is referred from the topic with:
<img src='/pub/Sandbox/TryGraphViz/graphviz_864ef280e712908e105c1d4ea7dfd7d4.svg' class='graphviz' />
It is an img tag and referred with the src=... attribute. The src attribute will include the html but apparently it will not trigger the requests for the referred images. As a consequence, the images are not displayed. To display the images the .svg file must be referred through an <object> tag, like:
<object data='/pub/Sandbox/TryGraphViz/graphviz_864ef280e712908e105c1d4ea7dfd7d4.svg' class='graphviz' />

Issue 2 type="svg" inline="on"

If the inline parameter is specified as inline="on" a different problem arises. The generated code is now inserted in the topic. The references to the images remain as:
<image xlink:href="Sun_Workstation.png" width="76px" height="64px" preserveAspectRatio="xMinYMin meet" x="34.832" y="-67.3553"/>
Those images will be retrieved from /data/Sandbox where they clearly will not be found. The correct reference is /pub/Sandbox/TryGraphViz/Sun_Workstation.png like:
<image xlink:href="/pub/Sandbox/TryGraphViz/Sun_Workstation.png" width="76px" height="64px" preserveAspectRatio="xMinYMin meet" x="34.832" y="-67.3553"/>

I fixed this with a hack:
bram@FortKnox:~/devwiki/GraphvizPlugin$ git diff master..svgenhance -- lib/Foswiki/Plugins/GraphvizPlugin/Core.pm
diff --git a/lib/Foswiki/Plugins/GraphvizPlugin/Core.pm b/lib/Foswiki/Plugins/GraphvizPlugin/Core.pm
index b83c138..bfa0fa5 100644
--- a/lib/Foswiki/Plugins/GraphvizPlugin/Core.pm
+++ b/lib/Foswiki/Plugins/GraphvizPlugin/Core.pm
@@ -197,8 +197,11 @@ sub GRAPHVIZ {
       my $svg = Foswiki::Func::readFile($outfilePath, 1);
       $svg =~ s/<\?xml .*?>/<!-- -->/g;
       $svg =~ s/<!DOCTYPE.*dtd">/<!-- -->/gs;
+      
+      my $pubPath = Foswiki::Func::getPubUrlPath( $theWeb, $theTopic );
+      $svg =~ s!xlink:href="!href="$pubPath/!gs;
+      
       $result = $this->{svgFormat};
-
       $result =~ s/\$svg/$svg/;
       $result =~ s/width="[^"]*"/width="$width"/ if defined $width;
       $result =~ s/height="[^"]*"/height="$height"/ if defined $height;
bram@FortKnox:~/devwiki/GraphvizPlugin$ 
 
But I wonder whether the rendering of type="svg" would not be more in line with the wiki architecture if we honoured the library=... parameter. So instead of:
+      $svg =~ s!xlink:href="!href="$pubPath/!gs;
one could do:
+      $svg =~ s!xlink:href="!href="$library/!gs;

Comments???

  • Image for dot to embed:
    stream-development-badge.png

  • Results when running dot on the command line:
    foswiki-plugin-bug-ex.gv.png

-- BramVanOosterhout - 17 Feb 2024

I have added a Pull request to Github: G foswiki / GraphvizPlugin

Fix for type="png", and also type="svg" when inline="on" (including support of library= parameter)

The idea is that dot is run in the pub directory for the current topic, or in the pub directory of the library= parameter if this is set. This fixes the type="png" generation.

In addition, an adaptation of the above patch from Bram is done on the inline version of the type="svg" so to fix paths.

Note that presumably, if you use PublishPlugin, then only the PNG form will work.

Examples:
%GRAPHVIZ{ "digraph G { Workstation [image="Laptop.jpg"]; }"
   type="png"
}%

%GRAPHVIZ{ "digraph G { Workstation [image="Laptop.jpg"]; }" library="%SANDBOXWEB%/%TOPIC%"
   type="png"
}%

%GRAPHVIZ{ "digraph G { Workstation [image="Laptop.jpg"]; }"
   type="svg" inline="on"
}%

%GRAPHVIZ{ "digraph G { Workstation [image="Laptop.jpg"]; }" library="%SANDBOXWEB%/%TOPIC%"
   type="svg" inline="on"
}%

and even the bizarre version:
<graphviz inline="on">
digraph G {
   req0021 [ shape=plaintext,label=<
<table style="rounded" border="1" cellborder="0" cellpadding="0" cellspacing="0" color="#b7c8dc">
  <tr><td gradientangle="90" bgcolor="#f3f8ff:#d4e7fe" width="20"><img scale="false" src="Laptop.jpg"/></td><td gradientangle="90" bgcolor="#f3f8ff:#d4e7fe" width="80" align="left" valign="middle">req0021</td></tr>
</table>
> ]
}
</graphviz>

Still won't work: (SVG, not inline thus not patched)
%GRAPHVIZ{ "digraph G { Workstation [image="Laptop.jpg"]; }"
   type="svg"
}%

%GRAPHVIZ{ "digraph G { Workstation [image="Laptop.jpg"]; }" library="%SANDBOXWEB%/%TOPIC%"
   type="svg"
}%

-- MarcSCHAEFER - 18 Feb 2024

The name library is used a bit differently in graphviz language. Better would be something like imagepath .... This actually exists already as a node attribute as far as I read the docs: https://graphviz.org/docs/attrs/imagepath/ And we need to make sure that people don't create a security problem that way, e.g. specify ing /etc as a library or image path and then load passwd...

-- MichaelDaum - 19 Feb 2024

For the library GRAPHIZ macro argument: You are right: if I remember well, the code first converts the library= argument into a Wiki + document, and then in the patch I convert a Wiki + document into a path. I don't think you can easily control that path then.

However, if there is any way to do bad things in dot itself (in the end, dot is run), then yes!

-- MarcSCHAEFER - 19 Feb 2024
 

ItemTemplate edit

Summary GraphVizPlugin: not clear how external images may be used
ReportedBy JohnKnutson
Codebase 1.1.9
SVN Range
AppliesTo Extension
Component GraphvizPlugin
Priority Normal
CurrentState Being Worked On
WaitingFor BramVanOosterhout, MichaelDaum
Checkins
TargetRelease n/a
ReleasedIn n/a
CheckinsOnBranches
trunkCheckins
masterCheckins
ItemBranchCheckins
Release02x01Checkins
Release02x00Checkins
Release01x01Checkins
I Attachment Action Size Date Who Comment
foswiki-plugin-bug-ex.gv.pngpng foswiki-plugin-bug-ex.gv.png manage 3 K 29 Mar 2017 - 22:24 JohnKnutson Results when running dot on the command line
graphviz-plugin-2024-02-18.patchpatch graphviz-plugin-2024-02-18.patch manage 1 K 18 Feb 2024 - 12:44 MarcSCHAEFER Patch for fixing up URLs in graphviz
stream-development-badge.pngpng stream-development-badge.png manage 889 bytes 29 Mar 2017 - 22:24 JohnKnutson Image for dot to embed
Topic revision: r8 - 19 Feb 2024, MarcSCHAEFER
The copyright of the content on this website is held by the contributing authors, except where stated elsewhere. See Copyright Statement. Creative Commons License    Legal Imprint    Privacy Policy