]> CyberLeo.Net >> Repos - SourceForge/phpwiki.git/blob - lib/plugin/GraphViz.php
fix syntax error
[SourceForge/phpwiki.git] / lib / plugin / GraphViz.php
1 <?php // -*-php-*-
2 rcs_id('$Id: GraphViz.php,v 1.2 2004-12-14 21:34:22 rurban Exp $');
3 /*
4  Copyright 2004 $ThePhpWikiProgrammingTeam
5
6  This file is part of PhpWiki.
7
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.
12
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.
17
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
21  */
22
23 /**
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.
26  *
27  * @Author: Reini Urban
28  *
29  * Note: 
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.
32  *
33  * Usage:
34 <?plugin GraphViz [options...]
35    multiline dot script ...
36 ?>
37
38  * See also: VisualWiki, which also uses dot and WikiPluginCached.
39  *
40  * TODO: 
41  * - neato binary ?
42  * - expand embedded <!plugin-list pagelist !> within the digraph script.
43  */
44
45 if (!defined("GRAPHVIZ_EXE"))
46   if (isWindows())
47     define('GRAPHVIZ_EXE','dot.exe');
48   else
49     define('GRAPHVIZ_EXE','/usr/local/bin/dot');
50
51 require_once "lib/WikiPluginCached.php"; 
52
53 class WikiPlugin_GraphViz
54 extends WikiPluginCached
55 {
56     /**
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
60      */
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']) {
70                 case 'svg':
71                 case 'svgz':
72                    return PLUGIN_CACHED_STATIC | PLUGIN_CACHED_SVG_PNG;
73                 case 'swf':
74                    return PLUGIN_CACHED_STATIC | PLUGIN_CACHED_SWF;
75                 default: 
76                    return PLUGIN_CACHED_STATIC | PLUGIN_CACHED_HTML;
77             }
78         }
79         else
80             return PLUGIN_CACHED_IMG_INLINE; // normal cached libgd image handles
81     }
82     function getName () {
83         return _("GraphViz");
84     }
85     function getDescription () {
86         return _("GraphViz image or imagemap creation of directed graphs");
87     }
88     function managesValidators() {
89         return true;
90     }
91     function getVersion() {
92         return preg_replace("/[Revision: $]/", '',
93                             "\$Revision: 1.2 $");
94     }
95     function getDefaultArguments() {
96         return array(
97                      'imgtype' => 'png', // png,gif,svgz,svg,...
98                      'alt'     => false,
99                      //'data'    => false, // <!plugin-list !> support
100                      'help'    => false,
101                      );
102     }
103     function handle_plugin_args_cruft(&$argstr, &$args) {
104         $this->source = $argstr;
105     }
106     /**
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.
110      */
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
115     }
116
117     /**
118      * Sets the imagetype according to user wishes and
119      * relies on WikiPluginCached to catch illegal image
120      * formats.
121      * @return string 'png', 'jpeg', 'gif'
122      */
123     function getImageType($dbi, $argarray, $request) {
124         return $argarray['imgtype'];
125     }
126
127     /**
128      * This gives an alternative text description of
129      * the image.
130      */
131     function getAlt($dbi, $argstr, $request) {
132         return (!empty($this->_args['alt'])) ? $this->_args['alt']
133                                              : $this->getDescription();
134     }
135
136     /**
137      * Returns an image containing a usage description of the plugin.
138      *
139      * TODO: *map features.
140      * @return string image handle
141      */
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"));
148         $helparr = array(
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.',
156             "\n  ?>"
157             );
158         $length = 0;
159         foreach($helparr as $alignright => $alignleft) {
160             $length = max($length, strlen($alignright));
161         }
162         $helptext ='';
163         foreach($helparr as $alignright => $alignleft) {
164             $helptext .= substr('                                                        '
165                                 . $alignright, -$length).$alignleft."\n";
166         }
167         return $this->text2img($helptext, 4, array(1, 0, 0),
168                                array(255, 255, 255));
169     }
170
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
177             /*
178             if (is_array($argarray['data'])) { // support <!plugin-list !> pagelists
179                 $src = ""; //#proc getdata\ndata:";
180                 $i = 0;
181                 foreach ($argarray['data'] as $data) {
182                     // hash or array?
183                     if (is_array($data))
184                         $src .= ("\t" . join(" ", $data) . "\n");
185                     else
186                         $src .= ("\t" . '"' . $data . '" ' . $i++ . "\n");
187                 }
188                 $src .= $source;
189                 $source = $src;
190             }
191             */
192             $tempfile = $this->tempnam('Graphviz');
193             unlink($tempfile);
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");
199             //if (empty($code))
200             //    return $this->error(fmt("Couldn't start commandline '%s'", $commandLine));
201             sleep(1);
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");
206                 return false;
207             }
208             $ImageCreateFromFunc = "ImageCreateFrom$gif";
209             if (function_exists($ImageCreateFromFunc))
210                 return $ImageCreateFromFunc( "$tempfile.$gif" );
211             return "$tempfile.$gif";
212         } else {
213             return $this->error(fmt("empty source"));
214         }
215     }
216     
217     // which argument must be set to 'png', for the fallback image when svg will fail on the client.
218     // type: SVG_PNG
219     function pngArg() {
220         return 'imgtype';
221     }
222     
223     function getMap($dbi, $argarray, $request) {
224         $img = $this->getImage($dbi, $argarray, $request);
225         return array($this->_mapfile, $img);
226     }
227 };
228
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
232 //
233
234 // For emacs users
235 // Local Variables:
236 // mode: php
237 // tab-width: 8
238 // c-basic-offset: 4
239 // c-hanging-comment-ender-p: nil
240 // indent-tabs-mode: nil
241 // End:
242 ?>