2 rcs_id('$Id: GraphViz.php,v 1.2 2004-12-14 21:34:22 rurban Exp $');
4 Copyright 2004 $ThePhpWikiProgrammingTeam
6 This file is part of PhpWiki.
8 PhpWiki is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
13 PhpWiki is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with PhpWiki; if not, write to the Free Software
20 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
24 * The GraphViz plugin passes all its arguments to the grapviz dot
25 * binary and displays the result as cached image (PNG,GIF,SVG) or imagemap.
27 * @Author: Reini Urban
30 * - We support only images supported by GD so far (PNG most likely).
31 * EPS, PS, SWF, SVG or SVGZ and imagemaps need to be tested.
34 <?plugin GraphViz [options...]
35 multiline dot script ...
38 * See also: VisualWiki, which also uses dot and WikiPluginCached.
42 * - expand embedded <!plugin-list pagelist !> within the digraph script.
45 if (!defined("GRAPHVIZ_EXE"))
47 define('GRAPHVIZ_EXE','dot.exe');
49 define('GRAPHVIZ_EXE','/usr/local/bin/dot');
51 require_once "lib/WikiPluginCached.php";
53 class WikiPlugin_GraphViz
54 extends WikiPluginCached
57 * Sets plugin type to MAP if -csmap (-map or -mapdemo or -csmapdemo not supported)
58 * or HTML if the imagetype is not supported by GD (EPS, SVG, SVGZ) (not yet)
59 * or IMG_INLINE if device = png, gif or jpeg
61 function getPluginType() {
62 if (!empty($this->_args['-csmap']))
63 return PLUGIN_CACHED_MAP; // not yet tested
64 $type = $this->decideImgType($this->_args['imgtype']);
65 if ($type == $this->_args['imgtype'])
66 return PLUGIN_CACHED_IMG_INLINE;
67 $device = strtolower($this->_args['imgtype']);
68 if (in_array($device, array('svg','swf','svgz','eps','ps'))) {
69 switch ($this->_args['device']) {
72 return PLUGIN_CACHED_STATIC | PLUGIN_CACHED_SVG_PNG;
74 return PLUGIN_CACHED_STATIC | PLUGIN_CACHED_SWF;
76 return PLUGIN_CACHED_STATIC | PLUGIN_CACHED_HTML;
80 return PLUGIN_CACHED_IMG_INLINE; // normal cached libgd image handles
85 function getDescription () {
86 return _("GraphViz image or imagemap creation of directed graphs");
88 function managesValidators() {
91 function getVersion() {
92 return preg_replace("/[Revision: $]/", '',
95 function getDefaultArguments() {
97 'imgtype' => 'png', // png,gif,svgz,svg,...
99 //'data' => false, // <!plugin-list !> support
103 function handle_plugin_args_cruft(&$argstr, &$args) {
104 $this->source = $argstr;
107 * Sets the expire time to one day (so the image producing
108 * functions are called seldomly) or to about two minutes
109 * if a help screen is created.
111 function getExpire($dbi, $argarray, $request) {
112 if (!empty($argarray['help']))
113 return '+120'; // 2 minutes
114 return sprintf('+%d', 3*86000); // approx 3 days
118 * Sets the imagetype according to user wishes and
119 * relies on WikiPluginCached to catch illegal image
121 * @return string 'png', 'jpeg', 'gif'
123 function getImageType($dbi, $argarray, $request) {
124 return $argarray['imgtype'];
128 * This gives an alternative text description of
131 function getAlt($dbi, $argstr, $request) {
132 return (!empty($this->_args['alt'])) ? $this->_args['alt']
133 : $this->getDescription();
137 * Returns an image containing a usage description of the plugin.
139 * TODO: *map features.
140 * @return string image handle
142 function helpImage() {
143 $def = $this->defaultArguments();
144 //$other_imgtypes = $GLOBALS['PLUGIN_CACHED_IMGTYPES'];
145 //unset ($other_imgtypes[$def['imgtype']]);
146 $imgtypes = $GLOBALS['PLUGIN_CACHED_IMGTYPES'];
147 $imgtypes = array_merge($imgtypes, array("svg", "svgz", "imap", "cmapx", "ismap", "cmap"));
149 '<?plugin GraphViz ' .
150 'imgtype' => ' = "' . $def['imgtype'] . "(default)|" . join('|',$imgtypes).'"',
151 'alt' => ' = "alternate text"',
152 //'data' => ' <!plugin-list !>: pagelist as input',
153 'help' => ' bool: displays this screen',
154 '...' => ' all further lines below the first plugin line ',
155 '' => ' and inside the tags are the dotty script.',
159 foreach($helparr as $alignright => $alignleft) {
160 $length = max($length, strlen($alignright));
163 foreach($helparr as $alignright => $alignleft) {
164 $helptext .= substr(' '
165 . $alignright, -$length).$alignleft."\n";
167 return $this->text2img($helptext, 4, array(1, 0, 0),
168 array(255, 255, 255));
171 function getImage($dbi, $argarray, $request) {
172 //extract($this->getArgs($argstr, $request));
173 //extract($argarray);
174 $source =& $this->source;
175 if (!empty($source)) {
176 //TODO: parse lines for <!plugin-list !> pagelists
178 if (is_array($argarray['data'])) { // support <!plugin-list !> pagelists
179 $src = ""; //#proc getdata\ndata:";
181 foreach ($argarray['data'] as $data) {
184 $src .= ("\t" . join(" ", $data) . "\n");
186 $src .= ("\t" . '"' . $data . '" ' . $i++ . "\n");
192 $tempfile = $this->tempnam('Graphviz');
194 $gif = $argarray['imgtype'];
195 $args = " -T$gif -o $tempfile.$gif";
196 if (in_array($gif, array("imap", "cmapx", "ismap", "cmap")))
197 $this->_mapfile = "$tempfile.map";
198 $code = $this->filterThroughCmd($source, GRAPHVIZ_EXE . "$args");
200 // return $this->error(fmt("Couldn't start commandline '%s'", $commandLine));
202 if (! file_exists("$tempfile.$gif") ) {
203 $this->_errortext .= sprintf(_("%s error: outputfile '%s' not created"),
204 "GraphViz", "$tempfile.$gif");
205 $this->_errortext .= ("\ncmd-line: cat script | " . GRAPHVIZ_EXE . "$args");
208 $ImageCreateFromFunc = "ImageCreateFrom$gif";
209 if (function_exists($ImageCreateFromFunc))
210 return $ImageCreateFromFunc( "$tempfile.$gif" );
211 return "$tempfile.$gif";
213 return $this->error(fmt("empty source"));
217 // which argument must be set to 'png', for the fallback image when svg will fail on the client.
223 function getMap($dbi, $argarray, $request) {
224 $img = $this->getImage($dbi, $argarray, $request);
225 return array($this->_mapfile, $img);
229 // $Log: not supported by cvs2svn $
230 // Revision 1.1 2004/12/13 14:45:33 rurban
231 // new generic GraphViz plugin: similar to Ploticus
239 // c-hanging-comment-ender-p: nil
240 // indent-tabs-mode: nil