]> CyberLeo.Net >> Repos - SourceForge/phpwiki.git/blob - lib/plugin/VisualWiki.php
loadsave: check if the dumped file will be accessible from outside.
[SourceForge/phpwiki.git] / lib / plugin / VisualWiki.php
1 <?php // -*-php-*-
2 rcs_id('$Id: VisualWiki.php,v 1.17 2004-10-14 19:19:34 rurban Exp $');
3 /*
4  Copyright (C) 2002 Johannes Große (Johannes Gro&szlig;e)
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  * Produces graphical site map of PhpWiki
25  * Example for an image map creating plugin. It produces a graphical
26  * sitemap of PhpWiki by calling the <code>dot</code> commandline tool
27  * from graphviz (http://www.graphviz.org).
28  * @author Johannes Große
29  * @version 0.8
30  */
31 define('VISUALWIKI_ALLOWOPTIONS', true);
32 global $dotbin;
33 if (PHP_OS == "Darwin") { // Mac OS X
34     $dotbin = '/sw/bin/dot'; // graphviz via Fink
35     //$dotbin = '/usr/local/bin/dot';
36
37     // Name of the Truetypefont - at least LucidaSansRegular.ttf is always present on OS X
38     define('VISUALWIKIFONT', 'LucidaSansRegular');
39
40     // The default font paths do not find your fonts, set the path here:
41     $fontpath = "/System/Library/Frameworks/JavaVM.framework/Versions/1.3.1/Home/lib/fonts/";
42     //$fontpath = "/usr/X11R6/lib/X11/fonts/TTF/";
43 }
44 elseif (isWindows()) {
45   $dotbin = 'dot';
46   define('VISUALWIKIFONT', 'Arial');
47 } elseif ($_SERVER["SERVER_NAME"] == 'phpwiki.sourceforge.net') { // sf.net hack
48     $dotbin = '/home/groups/p/ph/phpwiki/bin/dot';
49     define('VISUALWIKIFONT', 'luximr'); 
50 } else { // other os
51     $dotbin = '/usr/local/bin/dot';
52     // Name of the Truetypefont - Helvetica is probably easier to read
53     define('VISUALWIKIFONT', 'Helvetica');
54     //define('VISUALWIKIFONT', 'Times');
55     //define('VISUALWIKIFONT', 'Arial');
56
57     // The default font paths do not find your fonts, set the path here:
58     //$fontpath = "/usr/X11R6/lib/X11/fonts/TTF/";
59     //$fontpath = "/usr/share/fonts/default/TrueType/";
60 }
61
62 if (!defined('VISUALWIKI_ALLOWOPTIONS'))
63     define('VISUALWIKI_ALLOWOPTIONS', false);
64
65 require_once "lib/WikiPluginCached.php";
66
67 class WikiPlugin_VisualWiki
68 extends WikiPluginCached
69 {
70     /**
71      * Sets plugin type to map production
72      */
73     function getPluginType() {
74         return ($GLOBALS['request']->getArg('debug')) ? PLUGIN_CACHED_IMG_ONDEMAND : PLUGIN_CACHED_MAP;
75     }
76
77     /**
78      * Sets the plugin's name to VisualWiki. It can be called by
79      * <code>&lt;?plugin VisualWiki?&gt;</code>, now. This
80      * name must correspond to the filename and the class name.
81      */
82     function getName() {
83         return "VisualWiki";
84     }
85
86     function getVersion() {
87         return preg_replace("/[Revision: $]/", '',
88                             "\$Revision: 1.17 $");
89     }
90
91     /**
92      * Sets textual description.
93      */
94     function getDescription() {
95         return _("Visualizes the Wiki structure in a graph using the 'dot' commandline tool from graphviz.");
96     }
97
98     /**
99      * Returns default arguments. This is put into a separate
100      * function to allow its usage by both <code>getDefaultArguments</code>
101      * and <code>checkArguments</code>
102      */
103     function defaultarguments() {
104         return array('imgtype'        => 'png',
105                      'width'          => false, // was 5, scale it automatically
106                      'height'         => false, // was 7, scale it automatically
107                      'colorby'        => 'age', // sort by 'age' or 'revtime'
108                      'fillnodes'      => 'off',
109                      'label'          => 'name',
110                      'shape'          => 'ellipse',
111                      'large_nb'       => 5,
112                      'recent_nb'      => 5,
113                      'refined_nb'     => 15,
114                      'backlink_nb'    => 5,
115                      'neighbour_list' => '',
116                      'exclude_list'   => '',
117                      'include_list'   => '',
118                      'fontsize'       => 9,
119                      'debug'          => false,
120                      'help'           => false );
121     }
122
123     /**
124      * Sets the default arguments. WikiPlugin also regards these as
125      * the allowed arguments. Since WikiPluginCached stores an image
126      * for each different set of parameters, there can be a lot of
127      * these (large) graphs if you allow different parameters.
128      * Set <code>VISUALWIKI_ALLOWOPTIONS</code> to <code>false</code>
129      * to allow no options to be set and use only the default paramters.
130      * This will need an disk space of about 20 Kbyte all the time.
131      */
132     function getDefaultArguments() {
133         if (VISUALWIKI_ALLOWOPTIONS)
134             return $this->defaultarguments();
135         else
136             return array();
137     }
138
139     /**
140      * Substitutes each forbidden parameter value by the default value
141      * defined in <code>defaultarguments</code>.
142      */
143     function checkArguments(&$arg) {
144         extract($arg);
145         $def = $this->defaultarguments();
146
147         if (($width < 3) || ($width > 15))
148             $arg['width'] = $def['width'];
149
150         if (($height < 3) || ($height > 20))
151             $arg['height'] = $def['height'];
152
153         if (($fontsize < 8) || ($fontsize > 24))
154             $arg['fontsize'] = $def['fontsize'];
155
156         if (!in_array($label, array('name', 'number')))
157             $arg['label'] = $def['label'];
158
159         if (!in_array($shape, array('ellipse', 'box', 'point', 'circle',
160                                     'plaintext')))
161             $arg['shape'] = $def['shape'];
162
163         if (!in_array($colorby, array('age', 'revtime')))
164             $arg['colorby'] = $def['colorby'];
165
166         if (!in_array($fillnodes, array('on', 'off')))
167             $arg['fillnodes'] = $def['fillnodes'];
168
169         if (($large_nb < 0) || ($large_nb > 50))
170             $arg['large_nb'] = $def['large_nb'];
171
172         if (($recent_nb < 0)  || ($recent_nb > 50))
173             $arg['recent_nb'] = $def['recent_nb'];
174
175         if (($refined_nb < 0 ) || ( $refined_nb > 50))
176             $arg['refined_nb'] = $def['refined_nb'];
177
178         if (($backlink_nb < 0) || ($backlink_nb > 50))
179             $arg['backlink_nb'] = $def['backlink_nb'];
180
181         // ToDo: check if "ImageCreateFrom$imgtype"() exists.
182         if (!in_array($imgtype, $GLOBALS['PLUGIN_CACHED_IMGTYPES']))
183             $arg['imgtype'] = $def['imgtype'];
184         if (empty($fontname))
185             $arg['fontname'] = VISUALWIKIFONT;
186     }
187
188     /**
189      * Checks options, creates help page if necessary, calls both
190      * database access and image map production functions.
191      * @return array($map,$html)
192      */
193     function getMap($dbi, $argarray, $request) {
194         if (!VISUALWIKI_ALLOWOPTIONS)
195             $argarray = $this->defaultarguments();
196         $this->checkArguments($argarray);
197         $request->setArg('debug',$argarray['debug']);
198         //extract($argarray);
199         if ($argarray['help'])
200             return array($this->helpImage(), ' '); // FIXME
201         $this->createColors();
202         $this->extract_wikipages($dbi, $argarray);
203         /* ($dbi,  $large, $recent, $refined, $backlink,
204             $neighbour, $excludelist, $includelist, $color); */
205         return $this->invokeDot($argarray);
206         /* ($width, $height, $color, $shape, $text); */
207     }
208
209     /**
210      * Sets the expire time to one day (so the image producing
211      * functions are called seldomly) or to about two minutes
212      * if a help screen is created.
213      */
214     function getExpire($dbi, $argarray, $request) {
215         if ($argarray['help'])
216             return '+120'; // 2 minutes
217         return sprintf('+%d', 3*86000); // approx 3 days
218     }
219
220     /**
221      * Sets the imagetype according to user wishes and
222      * relies on WikiPluginCached to catch illegal image
223      * formats.
224      * (I feel unsure whether this option is reasonable in
225      *  this case, because png will definitely have the
226      *  best results.)
227      *
228      * @return string 'png', 'gif', 'jpeg'
229      */
230     function getImageType($dbi, $argarray, $request) {
231         return $argarray['imgtype'];
232     }
233
234     /**
235      * This gives an alternative text description of
236      * the image map. I do not know whether it interferes
237      * with the <code>title</code> attributes in &lt;area&gt;
238      * tags of the image map. Perhaps this will be removed.
239      * @return string
240      */
241     function getAlt($dbi, $argstr, $request) {
242         return $this->getDescription();
243     }
244
245     // ------------------------------------------------------------------------------------------
246
247     /**
248      * Returns an image containing a usage description of the plugin.
249      * @return string image handle
250      */
251     function helpImage() {
252         $def = $this->defaultarguments();
253         $other_imgtypes = $GLOBALS['PLUGIN_CACHED_IMGTYPES'];
254         unset ($other_imgtypes[$def['imgtype']]);
255         $helparr = array(
256             '<?plugin '.$this->getName() .
257             ' img'             => ' = "' . $def['imgtype'] . "(default)|" . join('|',$GLOBALS['PLUGIN_CACHED_IMGTYPES']).'"',
258             'width'            => ' = "width in inches"',
259             'height'           => ' = "height in inches"',
260             'fontname'         => ' = "font family"',
261             'fontsize'         => ' = "fontsize in points"',
262             'colorby'          => ' = "age|revtime|none"',
263             'fillnodes'        => ' = "on|off"',
264             'shape'            => ' = "ellipse(default)|box|circle|point"',
265             'label'            => ' = "name|number"',
266             'large_nb'         => ' = "number of largest pages to be selected"',
267             'recent_nb'        => ' = "number of youngest pages"',
268             'refined_nb'       => ' = "#pages with smallest time between revisions"',
269             'backlink_nb'      => ' = "number of pages with most backlinks"',
270             'neighbour_list'   => ' = "find pages linked from and to these pages"',
271             'exclude_list'     => ' = "colon separated list of pages to be excluded"',
272             'include_list'     => ' = "colon separated list"     ?>'
273             );
274         $length = 0;
275         foreach($helparr as $alignright => $alignleft) {
276             $length = max($length, strlen($alignright));
277         }
278         $helptext ='';
279         foreach($helparr as $alignright => $alignleft) {
280             $helptext .= substr('                                                        '
281                                 . $alignright, -$length).$alignleft."\n";
282         }
283         return $this->text2img($helptext, 4, array(1, 0, 0),
284                                array(255, 255, 255));
285     }
286
287
288     /**
289      * Selects the first (smallest or biggest) WikiPages in
290      * a given category.
291      *
292      * @param  number   integer  number of page names to be found
293      * @param  category string   attribute of the pages which is used
294      *                           to compare them
295      * @param  minimum  boolean  true finds smallest, false finds biggest
296      * @return array             list of page names found to be the best
297      */
298     function findbest($number, $category, $minimum ) {
299         // select the $number best in the category '$category'
300         $pages = &$this->pages;
301         $names = &$this->names;
302
303         $selected = array();
304         $i = 0;
305         foreach($names as $name) {
306             if ($i++>=$number)
307                 break;
308             $selected[$name] = $pages[$name][$category];
309         }
310         //echo "<pre>$category "; var_dump($selected); "</pre>";
311         $compareto = $minimum ? 0x79999999 : -0x79999999;
312
313         $i = 0;
314         foreach ($names as $name) {
315             if ($i++<$number)
316                 continue;
317             if ($minimum) {
318                 if (($crit = $pages[$name][$category]) < $compareto) {
319                     $selected[$name] = $crit;
320                     asort($selected, SORT_NUMERIC);
321                     array_pop($selected);
322                     $compareto = end($selected);
323                 }
324             } elseif (($crit = $pages[$name][$category]) > $compareto)  {
325                 $selected[$name] = $crit;
326                 arsort($selected, SORT_NUMERIC);
327                 array_pop($selected);
328                 $compareto = end($selected);
329             }
330         }
331         //echo "<pre>$category "; var_dump($selected); "</pre>";
332
333         return array_keys($selected);
334     }
335
336
337     /**
338     * Extracts a subset of all pages from the wiki and find their
339     * connections to other pages. Also collects some page features
340     * like size, age, revision number which are used to find the
341     * most attractive pages.
342     *
343     * @param  dbi         WikiDB   database handle to access all Wiki pages
344     * @param  LARGE       integer  number of largest pages which should
345     *                              be included
346     * @param  RECENT      integer  number of the youngest pages to be included
347     * @param  REFINED     integer  number of the pages with shortes revision
348     *                              interval
349     * @param  BACKLINK    integer  number of the pages with most backlinks
350     * @param  EXCLUDELIST string   colon ':' separated list of page names which
351     *                              should not be displayed (like PhpWiki, for
352     *                              example)
353     * @param  INCLUDELIST string   colon separated list of pages which are
354     *                              allways included (for example your own
355     *                              page :)
356     * @param  COLOR       string   'age', 'revtime' or 'none'; Selects which
357     *                              page feature is used to determine the
358     *                              filling color of the nodes in the graph.
359     * @return void
360     */
361     function extract_wikipages($dbi, $argarray) {
362         // $LARGE, $RECENT, $REFINED, $BACKLINK, $NEIGHBOUR,
363         // $EXCLUDELIST, $INCLUDELIST,$COLOR
364         $now = time();
365
366         extract($argarray);
367         // FIXME: gettextify?
368         $exclude_list   = explode(':', $exclude_list);
369         $include_list   = explode(':', $include_list);
370         $neighbour_list = explode(':', $neighbour_list);
371
372         // FIXME remove INCLUDED from EXCLUDED
373
374         // collect all pages
375         $allpages = $dbi->getAllPages();
376         $pages = &$this->pages;
377         $countpages = 0;
378         while ($page = $allpages->next()) {
379             $name = $page->getName();
380
381             // skip exluded pages
382             if (in_array($name, $exclude_list)) {
383                 $page->free();  
384                 continue;
385             }
386
387             // false = get links from actual page
388             // true  = get links to actual page ("backlinks")
389             $backlinks = $page->getLinks(true);
390             unset($bconnection);
391             $bconnection = array();
392             while ($blink = $backlinks->next()) {
393                 array_push($bconnection, $blink->getName());
394             }
395             $backlinks->free();
396             unset($backlinks);
397
398             // include all neighbours of pages listed in $NEIGHBOUR
399             if (in_array($name,$neighbour_list)) {
400                 $l = $page->getLinks(false);
401                 $con = array();
402                 while ($link = $l->next()) {
403                     array_push($con, $link->getName());
404                 }
405                 $include_list = array_merge($include_list, $bconnection, $con);
406                 $l->free();
407                 unset($l);
408                 unset($con);
409             }
410
411             unset($currev);
412             $currev = $page->getCurrentRevision();
413
414             $pages[$name] = array(
415                 'age'         => $now-$currev->get('mtime'),
416                 'revnr'       => $currev->getVersion(),
417                 'links'       => array(),
418                 'backlink_nb' => count($bconnection),
419                 'backlinks'   => $bconnection,
420                 'size'        => 1000 // FIXME
421                 );
422             $pages[$name]['revtime'] = $pages[$name]['age'] / ($pages[$name]['revnr']);
423
424             unset($page);
425         }
426         $allpages->free();
427         unset($allpages);
428         $this->names = array_keys($pages);
429
430         $countpages = count($pages);
431
432         // now select each page matching to given parameters
433         $all_selected = array_unique(array_merge(
434             $this->findbest($recent_nb,   'age',         true),
435             $this->findbest($refined_nb,  'revtime',     true),
436             $x = $this->findbest($backlink_nb, 'backlink_nb', false),
437 //          $this->findbest($large_nb,    'size',        false),
438             $include_list));
439
440         foreach($all_selected as $name)
441             if (isset($pages[$name]))
442                 $newpages[$name] = $pages[$name];
443         unset($this->names);
444         unset($this->pages);
445         $this->pages = $newpages;
446         $pages = &$this->pages;
447         $this->names = array_keys($pages);
448         unset($newpages);
449         unset($all_selected);
450
451         $countpages = count($pages);
452
453         // remove dead links and collect links
454         reset($pages);
455         while( list($name,$page) = each($pages) ) {
456             if (is_array($page['backlinks'])) {
457                 reset($page['backlinks']);
458                 while ( list($index, $link) = each( $page['backlinks'] ) ) {
459                     if ( !isset($pages[$link]) || $link == $name ) {
460                         unset($pages[$name]['backlinks'][$index]);
461                     } else {
462                         array_push($pages[$link]['links'],$name);
463                         //array_push($this->everylink, array($link,$name));
464                     }
465                 }
466             }
467         }
468
469         if ($colorby == 'none')
470             return;
471         list($oldestname) = $this->findbest(1, $colorby, false);
472         $this->oldest = $pages[$oldestname][$colorby];
473         foreach($this->names as $name)
474             $pages[$name]['color'] = $this->getColor($pages[$name][$colorby] / $this->oldest);
475     } // extract_wikipages
476
477     /**
478      * Creates the text file description of the graph needed to invoke
479      * <code>dot</code>.
480      *
481      * @param filename  string  name of the dot file to be created
482      * @param width     float   width of the output graph in inches
483      * @param height    float   height of the graph in inches
484      * @param colorby   string  color sceme beeing used ('age', 'revtime',
485      *                                                   'none')
486      * @param shape     string  node shape; 'ellipse', 'box', 'circle', 'point'
487      * @param label     string  'name': label by name,
488      *                          'number': label by unique number
489      * @return boolean          error status; true=ok; false=error
490      */
491     function createDotFile($filename, $argarray) {
492         extract($argarray);
493         if (!$fp = fopen($filename, 'w'))
494             return false;
495
496         $fillstring = ($fillnodes == 'on') ? 'style=filled,' : '';
497
498         $ok = true;
499         $names = &$this->names;
500         $pages = &$this->pages;
501         if ($names)
502             $nametonumber = array_flip($names);
503
504         $dot = "digraph VisualWiki {\n" // }
505             . (!empty($fontpath) ? "    fontpath=\"$fontpath\"\n" : "");
506         if ($width and $height)
507             $dot .= "    size=\"$width,$height\";\n    ";
508
509
510         switch ($shape) {
511         case 'point':
512             $dot .= "edge [arrowhead=none];\nnode [shape=$shape,fontname=$fontname,width=0.15,height=0.15,fontsize=$fontsize];\n";
513             break;
514         case 'box':
515             $dot .= "node [shape=$shape,fontname=$fontname,width=0.4,height=0.4,fontsize=$fontsize];\n";
516             break;
517         case 'circle':
518             $dot .= "node [shape=$shape,fontname=$fontname,width=0.25,height=0.25,fontsize=$fontsize];\n";
519             break;
520         default :
521             $dot .= "node [fontname=$fontname,shape=$shape,fontsize=$fontsize];\n" ;
522         }
523         $dot .= "\n";
524         $i = 0;
525         foreach ($names as $name) {
526
527             $url = rawurlencode($name);
528             // patch to allow Page/SubPage
529             $url = preg_replace('/' . urlencode(SUBPAGE_SEPARATOR) . '/',
530                                 SUBPAGE_SEPARATOR, $url);
531             $nodename = ($label != 'name' ? $nametonumber[$name] + 1 : $name);
532
533             $dot .= "    \"$nodename\" [URL=\"$url\"";
534             if ($colorby != 'none') {
535                 $col = $pages[$name]['color'];
536                 $dot .= sprintf(',%scolor="#%02X%02X%02X"', $fillstring,
537                                 $col[0], $col[1], $col[2]);
538             }
539             $dot .= "];\n";
540
541             if (!empty($pages[$name]['links'])) {
542                 unset($linkarray);
543                 if ($label != 'name')
544                     foreach($pages[$name]['links'] as $linkname)
545                         $linkarray[] = $nametonumber[$linkname] + 1;
546                 else
547                     $linkarray = $pages[$name]['links'];
548                 $linkstring = join('"; "', $linkarray );
549
550                 $c = count($pages[$name]['links']);
551                 $dot .= "        \"$nodename\" -> "
552                      . ($c>1?'{':'')
553                      . "\"$linkstring\";"
554                      . ($c>1?'}':'')
555                      . "\n";
556             }
557         }
558         if ($colorby != 'none') {
559             $dot .= "\n    subgraph cluster_legend {\n"
560                  . "         node[fontname=$fontname,shape=box,width=0.4,height=0.4,fontsize=$fontsize];\n"
561                  . "         fillcolor=lightgrey;\n"
562                  . "         style=filled;\n"
563                  . "         fontname=$fontname;\n"
564                  . "         fontsize=$fontsize;\n"
565                  . "         label=\"".gettext("Legend")."\";\n";
566             $oldest= ceil($this->oldest / (24 * 3600));
567             $max = 5;
568             $legend = array();
569             for($i = 0; $i < $max; $i++) {
570                 $time = floor($i / $max * $oldest);
571                 $name = '"' . $time .' '. _("days") .'"';
572                 $col = $this->getColor($i/$max);
573                 $dot .= sprintf('       %s [%scolor="#%02X%02X%02X"];',
574                                 $name, $fillstring, $col[0], $col[1], $col[2])
575                     . "\n";
576                 $legend[] = $name;
577             }
578             $dot .= '        '. join(' -> ', $legend)
579                  . ";\n    }\n";
580
581         }
582
583         // {
584         $dot .= "}\n";
585
586         $ok = fwrite($fp, $dot);
587         $ok = fclose($fp) && $ok;  // close anyway
588
589         return $ok;
590     }
591
592     /**
593      * Execute system command.
594      *
595      * @param  cmd string   command to be invoked
596      * @return     boolean  error status; true=ok; false=error
597      */
598     function execute($cmd, $until = false) {
599         // cmd must redirect stderr to stdout though!
600         $errstr = exec($cmd); //, $outarr, $returnval); // normally 127
601         //$errstr = join('',$outarr);
602         $ok = empty($errstr);
603         if (!$ok) {
604             trigger_error("\n".$cmd." failed: $errstr", E_USER_WARNING);
605         } elseif ($GLOBALS['request']->getArg('debug'))
606             trigger_error("\n".$cmd.": success\n", E_USER_NOTICE);
607         if (!isWindows()) {
608             if ($until) {
609                 $loop = 100000;
610                 while (!file_exists($until) and $loop > 0) {
611                     $loop -= 100;
612                     usleep(100);
613                 }
614             } else {
615                 usleep(5000);
616             }
617         }
618         if ($until)
619             return file_exists($until);
620         return $ok;
621     }
622
623     /**
624      * Produces a dot file, calls dot twice to obtain an image and a
625      * text description of active areas for hyperlinking and returns
626      * an image and an html map.
627      *
628      * @param width     float   width of the output graph in inches
629      * @param height    float   height of the graph in inches
630      * @param colorby   string  color sceme beeing used ('age', 'revtime',
631      *                                                   'none')
632      * @param shape     string  node shape; 'ellipse', 'box', 'circle', 'point'
633      * @param label     string  not used anymore
634      */
635     function invokeDot($argarray) {
636         global $dotbin;
637         $tempfiles = $this->tempnam('VisualWiki');
638         $gif = $argarray['imgtype'];
639         $ImageCreateFromFunc = "ImageCreateFrom$gif";
640         $outfile = $tempfiles.".".$gif;
641         $debug = $GLOBALS['request']->getArg('debug');
642         if ($debug) {
643             $tempdir = dirname($tempfiles);
644             $tempout = $tempdir . "/.debug";
645         }
646         $ok = $tempfiles
647             && $this->createDotFile($tempfiles.'.dot',$argarray)
648             // && $this->filterThroughCmd('',"$dotbin -T$gif $tempfiles.dot -o $outfile")
649             // && $this->filterThroughCmd('',"$dotbin -Timap $tempfiles.dot -o ".$tempfiles.".map")
650             && $this->execute("$dotbin -T$gif $tempfiles.dot -o $outfile" . 
651                               ($debug ? " > $tempout 2>&1" : " 2>&1"), $outfile)
652             && $this->execute("$dotbin -Timap $tempfiles.dot -o ".$tempfiles.".map" . 
653                               ($debug ? " > $tempout 2>&1" : " 2>&1"), $tempfiles.".map")
654             && file_exists( $outfile )
655             && file_exists( $tempfiles.'.map' )
656             && ($img = $ImageCreateFromFunc($outfile))
657             && ($fp = fopen($tempfiles.'.map', 'r'));
658
659         $map = HTML();
660         if ($debug == 'static') {
661             // workaround for misconfigured WikiPluginCached (sf.net) or dot.
662             // present a static png and map file.
663             if (file_exists($outfile) and filesize($outfile) > 900)
664                 $img = $outfile;
665             else
666                 $img = $tempdir . "/VisualWiki.".$gif;
667             if (file_exists( $tempfiles.".map") and filesize($tempfiles.".map") > 20)
668                 $map = $tempfiles.".map";
669             else
670                 $map = $tempdir . "/VisualWiki.map";
671             $img = $ImageCreateFromFunc($img);
672             $fp = fopen($map, 'r');
673             $map = HTML();      
674             $ok = true;
675         }
676         if ($ok and $fp) {
677             while (!feof($fp)) {
678                 $line = fgets($fp, 1000);
679                 if (substr($line, 0, 1) == '#')
680                     continue;
681                 list($shape, $url, $e1, $e2, $e3, $e4) = sscanf($line,
682                                                                 "%s %s %d,%d %d,%d");
683                 if ($shape != 'rect')
684                     continue;
685
686                 // dot sometimes gives not allways the right order so
687                 // so we have to sort a bit
688                 $x1 = min($e1, $e3);
689                 $x2 = max($e1, $e3);
690                 $y1 = min($e2, $e4);
691                 $y2 = max($e2, $e4);
692                 $map->pushContent(HTML::area(array(
693                             'shape'  => 'rect',
694                             'coords' => "$x1,$y1,$x2,$y2",
695                             'href'   => $url,
696                             'title'  => rawurldecode($url),
697                             'alt' => $url)));
698             }
699             fclose($fp);
700             //trigger_error("url=".$url);
701         } else {
702             $this->_errortext = 
703                 ("$outfile: ".(file_exists($outfile) ? filesize($outfile):'missing')."\n".
704                  "$tempfiles.map: ".(file_exists("$tempfiles.map") ? filesize("$tempfiles.map"):'missing'));
705             $this->_errortext .= ("\ncmd-line: $dotbin -T$gif $tempfiles.dot -o $outfile");
706             trigger_error($this->_errortext, E_USER_WARNING);
707             return array(false, false);
708         }
709
710         // clean up tempfiles
711         if ($ok and !$argarray['debug'])
712             foreach (array('',".$gif",'.map','.dot') as $ext) {
713                 if (file_exists($tempfiles.$ext))
714                     unlink($tempfiles.$ext);
715             }
716
717         if ($ok)
718             return array($img, $map);
719         else
720             return array(false, false);
721     } // invokeDot
722
723
724     /** 
725      * static workaround on broken Cache or broken dot executable, 
726      * called only if debug=static.
727      *
728      * @access private
729      * @param  url      string  url pointing to the image part of the map
730      * @param  map      string  &lt;area&gt; tags defining active
731      *                          regions in the map
732      * @param  dbi      WikiDB  database abstraction class
733      * @param  argarray array   complete (!) arguments to produce 
734      *                          image. It is not necessary to call 
735      *                          WikiPlugin->getArgs anymore.
736      * @param  request  Request ??? 
737      * @return          string  html output
738      */
739     function embedImg($url,&$dbi,$argarray,&$request) {
740         if (!VISUALWIKI_ALLOWOPTIONS)
741             $argarray = $this->defaultarguments();
742         $this->checkArguments($argarray);
743         //extract($argarray);
744         if ($argarray['help'])
745             return array($this->helpImage(), ' '); // FIXME
746         $this->createColors();
747         $this->extract_wikipages($dbi, $argarray);
748         list($imagehandle, $content['html']) = $this->invokeDot($argarray);
749         // write to uploads and produce static url
750         $file_dir = defined('PHPWIKI_DIR') ? 
751             PHPWIKI_DIR . "/uploads" : "uploads";
752         $upload_dir = SERVER_URL . ((substr(DATA_PATH,0,1)=='/') ? '' : "/") . DATA_PATH . '/uploads/';
753         $tmpfile = tempnam($file_dir,"VisualWiki").".".$argarray['imgtype'];
754         WikiPluginCached::writeImage($argarray['imgtype'], $imagehandle, $tmpfile);             
755         ImageDestroy($imagehandle);
756         return WikiPluginCached::embedMap(1,$upload_dir.basename($tmpfile),$content['html'],$dbi,$argarray,$request);
757     }
758
759
760     /**
761      * Prepares some rainbow colors for the nodes of the graph
762      * and stores them in an array which may be accessed with
763      * <code>getColor</code>.
764      */
765     function createColors() {
766         $predefcolors = array(
767              array('red' => 255, 'green' =>   0, 'blue' =>   0),
768              array('red' => 255, 'green' => 255, 'blue' =>   0),
769              array('red' =>   0, 'green' => 255, 'blue' =>   0),
770              array('red' =>   0, 'green' => 255, 'blue' => 255),
771              array('red' =>   0, 'green' =>   0, 'blue' => 255),
772              array('red' => 100, 'green' => 100, 'blue' => 100)
773              );
774
775         $steps = 2;
776         $numberofcolors = count($predefcolors) * $steps;
777
778         $promille = -1;
779         foreach($predefcolors as $color) {
780             if ($promille < 0) {
781                 $oldcolor = $color;
782                 $promille = 0;
783                 continue;
784             }
785             for ($i = 0; $i < $steps; $i++)
786                 $this->ColorTab[++$promille / $numberofcolors * 1000] = array(
787                     floor(interpolate( $oldcolor['red'],   $color['red'],   $i/$steps )),
788                     floor(interpolate( $oldcolor['green'], $color['green'], $i/$steps )),
789                     floor(interpolate( $oldcolor['blue'],  $color['blue'],  $i/$steps ))
790                 );
791             $oldcolor = $color;
792         }
793 //echo"<pre>";  var_dump($this->ColorTab); echo "</pre>";
794     }
795
796     /**
797      * Translates a value from 0.0 to 1.0 into rainbow color.
798      * red -&gt; orange -&gt; green -&gt; blue -&gt; gray
799      *
800      * @param promille float value between 0.0 and 1.0
801      * @return array(red,green,blue)
802      */
803     function getColor($promille) {
804         foreach( $this->ColorTab as $pro => $col ) {
805             if ($promille*1000 < $pro)
806                 return $col;
807         }
808         $lastcol = end($this->ColorTab);
809         return $lastcol;
810     } // getColor
811 } // WikiPlugin_VisualWiki
812
813 /**
814  * Linear interpolates a value between two point a and b
815  * at a value pos.
816  * @return float  interpolated value
817  */
818
819 function interpolate($a, $b, $pos) {
820     return $a + ($b - $a) * $pos;
821 }
822
823 // $Log: not supported by cvs2svn $
824 // Revision 1.16  2004/10/12 15:34:47  rurban
825 // redirect stderr to display the failing msg
826 //
827 // Revision 1.15  2004/09/08 13:38:00  rurban
828 // improve loadfile stability by using markup=2 as default for undefined markup-style.
829 // use more refs for huge objects.
830 // fix debug=static issue in WikiPluginCached
831 //
832 // Revision 1.14  2004/09/07 13:26:31  rurban
833 // new WikiPluginCached option debug=static and some more sf.net defaults for VisualWiki
834 //
835 // Revision 1.13  2004/09/06 12:13:00  rurban
836 // provide sf.net default dotbin
837 //
838 // Revision 1.12  2004/09/06 12:08:50  rurban
839 // memory_limit on unix workaround
840 // VisualWiki: default autosize image
841 //
842 // Revision 1.11  2004/09/06 10:10:27  rurban
843 // fixed syntax error
844 //
845 // Revision 1.10  2004/06/19 10:06:38  rurban
846 // Moved lib/plugincache-config.php to config/*.ini
847 // use PLUGIN_CACHED_* constants instead of global $CacheParams
848 //
849 // Revision 1.9  2004/06/03 09:40:57  rurban
850 // WikiPluginCache improvements
851 //
852 // Revision 1.8  2004/01/26 09:18:00  rurban
853 // * changed stored pref representation as before.
854 //   the array of objects is 1) bigger and 2)
855 //   less portable. If we would import packed pref
856 //   objects and the object definition was changed, PHP would fail.
857 //   This doesn't happen with an simple array of non-default values.
858 // * use $prefs->retrieve and $prefs->store methods, where retrieve
859 //   understands the interim format of array of objects also.
860 // * simplified $prefs->get() and fixed $prefs->set()
861 // * added $user->_userid and class '_WikiUser' portability functions
862 // * fixed $user object ->_level upgrading, mostly using sessions.
863 //   this fixes yesterdays problems with loosing authorization level.
864 // * fixed WikiUserNew::checkPass to return the _level
865 // * fixed WikiUserNew::isSignedIn
866 // * added explodePageList to class PageList, support sortby arg
867 // * fixed UserPreferences for WikiUserNew
868 // * fixed WikiPlugin for empty defaults array
869 // * UnfoldSubpages: added pagename arg, renamed pages arg,
870 //   removed sort arg, support sortby arg
871 //
872 // Revision 1.7  2003/03/03 13:57:31  carstenklapp
873 // Added fontpath (see PhpWiki:VisualWiki), tries to be smart about which OS.
874 // (This plugin still doesn't work for me on OS X, but at least image files
875 // are actually being created now in '/tmp/cache'.)
876 //
877 // Revision 1.6  2003/01/18 22:11:45  carstenklapp
878 // Code cleanup:
879 // Reformatting & tabs to spaces;
880 // Added copyleft, getVersion, getDescription, rcs_id.
881 //
882
883 // Local Variables:
884 // mode: php
885 // tab-width: 8
886 // c-basic-offset: 4
887 // c-hanging-comment-ender-p: nil
888 // indent-tabs-mode: nil
889 // End:
890 ?>