]> CyberLeo.Net >> Repos - SourceForge/phpwiki.git/blob - lib/plugin/VisualWiki.php
provide sf.net default dotbin
[SourceForge/phpwiki.git] / lib / plugin / VisualWiki.php
1 <?php // -*-php-*-
2 rcs_id('$Id: VisualWiki.php,v 1.13 2004-09-06 12:13:00 rurban Exp $');
3 /*
4  Copyright (C) 2002 Johannes Große (Johannes Gro&szlig;e)
5
6  This file is (not yet) 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 } else { // other os
48     $dotbin = '/usr/local/bin/dot';
49     if ($_SERVER["SERVER_NAME"] == 'phpwiki.sourceforge.net')
50         $dotbin = '/home/groups/p/ph/phpwiki/bin/dot';
51
52     // Name of the Truetypefont - Helvetica is probably easier to read
53     //define('VISUALWIKIFONT', 'Helvetica');
54     //define('VISUALWIKIFONT', 'Times');
55     //define('VISUALWIKIFONT', 'Arial');
56     define('VISUALWIKIFONT', 'luximr'); // sf.net, sf.net can only do gif
57
58     // The default font paths do not find your fonts, set the path here:
59     //$fontpath = "/usr/X11R6/lib/X11/fonts/TTF/";
60     //$fontpath = "/usr/share/fonts/default/TrueType/";
61 }
62
63 if (!defined('VISUALWIKI_ALLOWOPTIONS'))
64     define('VISUALWIKI_ALLOWOPTIONS', false);
65
66 require_once "lib/WikiPluginCached.php";
67
68 class WikiPlugin_VisualWiki
69 extends WikiPluginCached
70 {
71     /**
72      * Sets plugin type to map production
73      */
74     function getPluginType() {
75         return PLUGIN_CACHED_MAP;
76     }
77
78     /**
79      * Sets the plugin's name to VisualWiki. It can be called by
80      * <code>&lt;?plugin VisualWiki?&gt;</code>, now. This
81      * name must correspond to the filename and the class name.
82      */
83     function getName() {
84         return "VisualWiki";
85     }
86
87     function getVersion() {
88         return preg_replace("/[Revision: $]/", '',
89                             "\$Revision: 1.13 $");
90     }
91
92     /**
93      * Sets textual description.
94      */
95     function getDescription() {
96         return _("Visualizes the Wiki structure in a graph using the 'dot' commandline tool from graphviz.");
97     }
98
99     /**
100      * Returns default arguments. This is put into a separate
101      * function to allow its usage by both <code>getDefaultArguments</code>
102      * and <code>checkArguments</code>
103      */
104     function defaultarguments() {
105         return array('imgtype'        => 'png',
106                      'width'          => false, // was 5, scale it automatically
107                      'height'         => false, // was 7, scale it automatically
108                      'colorby'        => 'age', // sort by 'age' or 'revtime'
109                      'fillnodes'      => 'off',
110                      'label'          => 'name',
111                      'shape'          => 'ellipse',
112                      'large_nb'       => 5,
113                      'recent_nb'      => 5,
114                      'refined_nb'     => 15,
115                      'backlink_nb'    => 5,
116                      'neighbour_list' => '',
117                      'exclude_list'   => '',
118                      'include_list'   => '',
119                      'fontsize'       => 9,
120                      'debug'          => false,
121                      'help'           => false );
122     }
123
124     /**
125      * Sets the default arguments. WikiPlugin also regards these as
126      * the allowed arguments. Since WikiPluginCached stores an image
127      * for each different set of parameters, there can be a lot of
128      * these (large) graphs if you allow different parameters.
129      * Set <code>VISUALWIKI_ALLOWOPTIONS</code> to <code>false</code>
130      * to allow no options to be set and use only the default paramters.
131      * This will need an disk space of about 20 Kbyte all the time.
132      */
133     function getDefaultArguments() {
134         if (VISUALWIKI_ALLOWOPTIONS)
135             return $this->defaultarguments();
136         else
137             return array();
138     }
139
140     /**
141      * Substitutes each forbidden parameter value by the default value
142      * defined in <code>defaultarguments</code>.
143      */
144     function checkArguments(&$arg) {
145         extract($arg);
146         $def = $this->defaultarguments();
147
148         if (($width < 3) || ($width > 15))
149             $arg['width'] = $def['width'];
150
151         if (($height < 3) || ($height > 20))
152             $arg['height'] = $def['height'];
153
154         if (($fontsize < 8) || ($fontsize > 24))
155             $arg['fontsize'] = $def['fontsize'];
156
157         if (!in_array($label, array('name', 'number')))
158             $arg['label'] = $def['label'];
159
160         if (!in_array($shape, array('ellipse', 'box', 'point', 'circle',
161                                     'plaintext')))
162             $arg['shape'] = $def['shape'];
163
164         if (!in_array($colorby, array('age', 'revtime')))
165             $arg['colorby'] = $def['colorby'];
166
167         if (!in_array($fillnodes, array('on', 'off')))
168             $arg['fillnodes'] = $def['fillnodes'];
169
170         if (($large_nb < 0) || ($large_nb > 50))
171             $arg['large_nb'] = $def['large_nb'];
172
173         if (($recent_nb < 0)  || ($recent_nb > 50))
174             $arg['recent_nb'] = $def['recent_nb'];
175
176         if (($refined_nb < 0 ) || ( $refined_nb > 50))
177             $arg['refined_nb'] = $def['refined_nb'];
178
179         if (($backlink_nb < 0) || ($backlink_nb > 50))
180             $arg['backlink_nb'] = $def['backlink_nb'];
181
182         // ToDo: check if "ImageCreateFrom$imgtype"() exists.
183         if (!in_array($imgtype, $GLOBALS['PLUGIN_CACHED_IMGTYPES']))
184             $arg['imgtype'] = $def['imgtype'];
185         if (empty($fontname))
186             $arg['fontname'] = VISUALWIKIFONT;
187     }
188
189     /**
190      * Checks options, creates help page if necessary, calls both
191      * database access and image map production functions.
192      * @return array($map,$html)
193      */
194     function getMap($dbi, $argarray, $request) {
195         if (!VISUALWIKI_ALLOWOPTIONS)
196             $argarray = $this->defaultarguments();
197         $this->checkArguments($argarray);
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
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         switch ($shape) {
510         case 'point':
511             $dot .= "edge [arrowhead=none];\nnode [shape=$shape,fontname=$fontname,width=0.15,height=0.15,fontsize=$fontsize];\n";
512             break;
513         case 'box':
514             $dot .= "node [shape=$shape,fontname=$fontname,width=0.4,height=0.4,fontsize=$fontsize];\n";
515             break;
516         case 'circle':
517             $dot .= "node [shape=$shape,fontname=$fontname,width=0.25,height=0.25,fontsize=$fontsize];\n";
518             break;
519         default :
520             $dot .= "node [fontname=$fontname,shape=$shape,fontsize=$fontsize];\n" ;
521         }
522         $dot .= "\n";
523         $i = 0;
524         foreach ($names as $name) {
525
526             $url = rawurlencode($name);
527             // patch to allow Page/SubPage
528             $url = preg_replace('/' . urlencode(SUBPAGE_SEPARATOR) . '/',
529                                 SUBPAGE_SEPARATOR, $url);
530             $nodename = ($label != 'name' ? $nametonumber[$name] + 1 : $name);
531
532             $dot .= "    \"$nodename\" [URL=\"$url\"";
533             if ($colorby != 'none') {
534                 $col = $pages[$name]['color'];
535                 $dot .= sprintf(',%scolor="#%02X%02X%02X"', $fillstring,
536                                 $col[0], $col[1], $col[2]);
537             }
538             $dot .= "];\n";
539
540             if (!empty($pages[$name]['links'])) {
541                 unset($linkarray);
542                 if ($label != 'name')
543                     foreach($pages[$name]['links'] as $linkname)
544                         $linkarray[] = $nametonumber[$linkname] + 1;
545                 else
546                     $linkarray = $pages[$name]['links'];
547                 $linkstring = join('"; "', $linkarray );
548
549                 $c = count($pages[$name]['links']);
550                 $dot .= "        \"$nodename\" -> "
551                      . ($c>1?'{':'')
552                      . "\"$linkstring\";"
553                      . ($c>1?'}':'')
554                      . "\n";
555             }
556         }
557         if ($colorby != 'none') {
558             $dot .= "\n    subgraph cluster_legend {\n"
559                  . "         node[fontname=$fontname,shape=box,width=0.4,height=0.4,fontsize=$fontsize];\n"
560                  . "         fillcolor=lightgrey;\n"
561                  . "         style=filled;\n"
562                  . "         fontname=$fontname;\n"
563                  . "         fontsize=$fontsize;\n"
564                  . "         label=\"".gettext("Legend")."\";\n";
565             $oldest= ceil($this->oldest / (24 * 3600));
566             $max = 5;
567             $legend = array();
568             for($i = 0; $i < $max; $i++) {
569                 $time = floor($i / $max * $oldest);
570                 $name = '"' . $time .' '. _("days") .'"';
571                 $col = $this->getColor($i/$max);
572                 $dot .= sprintf('       %s [%scolor="#%02X%02X%02X"];',
573                                 $name, $fillstring, $col[0], $col[1], $col[2])
574                     . "\n";
575                 $legend[] = $name;
576             }
577             $dot .= '        '. join(' -> ', $legend)
578                  . ";\n    }\n";
579
580         }
581
582         // {
583         $dot .= "}\n";
584
585         $ok = fwrite($fp, $dot);
586         $ok = fclose($fp) && $ok;  // close anyway
587
588         return $ok;
589     }
590
591     /**
592      * Execute system command.
593      *
594      * @param  cmd string   command to be invoked
595      * @return     boolean  error status; true=ok; false=error
596      */
597     function execute($cmd) {
598         exec($cmd, $outarr, $returnval); // normally 127
599         $errstr = join('',$outarr);
600         if (!empty($errstr))
601             trigger_error($cmd.": ".$errstr, E_USER_WARNING);
602         if (!isWindows())
603             usleep(1000);
604         return empty($errstr);
605     }
606
607     /**
608      * Produces a dot file, calls dot twice to obtain an image and a
609      * text description of active areas for hyperlinking and returns
610      * an image and an html map.
611      *
612      * @param width     float   width of the output graph in inches
613      * @param height    float   height of the graph in inches
614      * @param colorby   string  color sceme beeing used ('age', 'revtime',
615      *                                                   'none')
616      * @param shape     string  node shape; 'ellipse', 'box', 'circle', 'point'
617      * @param label     string  not used anymore
618      */
619     function invokeDot($argarray) {
620         global $dotbin;
621         //$cacheparams = $GLOBALS['CacheParams'];
622         $tempfiles = $this->tempnam('VisualWiki');
623         $gif = $argarray['imgtype'];
624         $ImageCreateFromFunc = "ImageCreateFrom$gif";
625         $ok =  $tempfiles
626             && $this->createDotFile($tempfiles.'.dot',$argarray)
627             && $this->execute("$dotbin -T$gif $tempfiles.dot -o $tempfiles.$gif")
628             && $this->execute("$dotbin -Timap $tempfiles.dot -o $tempfiles.map")
629             && file_exists( "$tempfiles.$gif" )
630             && file_exists( $tempfiles.'.map' )
631             && ($img = $ImageCreateFromFunc( "$tempfiles.$gif" ))
632             && ($fp = fopen($tempfiles.'.map','r'));
633
634         $map = HTML();
635         if ($ok) {
636             while (!feof($fp)) {
637                 $line = fgets($fp, 1000);
638                 if (substr($line, 0, 1) == '#')
639                     continue;
640                 list($shape, $url, $e1, $e2, $e3, $e4) = sscanf($line,
641                                                                 "%s %s %d,%d %d,%d");
642                 if ($shape != 'rect')
643                     continue;
644
645                 // dot sometimes gives not allways the right order so
646                 // so we have to sort a bit
647                 $x1 = min($e1, $e3);
648                 $x2 = max($e1, $e3);
649                 $y1 = min($e2, $e4);
650                 $y2 = max($e2, $e4);
651                 $map->pushContent(HTML::area(array(
652                             'shape'  => 'rect',
653                             'coords' => "$x1,$y1,$x2,$y2",
654                             'href'   => $url,
655                             'title'  => rawurldecode($url),
656                             'alt' => $url)));
657                 }
658             fclose($fp);
659 //trigger_error("url=".$url);
660         } else {
661             trigger_error("
662 $tempfiles.$gif: ".(file_exists("$tempfiles.$gif") ? filesize("$tempfiles.$gif"):'missing')."
663 $tempfiles.map: ".(file_exists("$tempfiles.map") ? filesize("$tempfiles.map"):'missing')."
664 ",E_USER_WARNING);
665         }
666
667         // clean up tempfiles
668         if ($ok and !$argarray['debug'])
669         foreach (array('',".$gif",".map",".dot") as $ext) {
670             if (file_exists($tempfiles.$ext))
671                 unlink($tempfiles.$ext);
672         }
673
674         if ($ok)
675             return array($img, $map);
676         else
677             return array(false, false);
678     } // invokeDot
679
680     /**
681      * Prepares some rainbow colors for the nodes of the graph
682      * and stores them in an array which may be accessed with
683      * <code>getColor</code>.
684      */
685     function createColors() {
686         $predefcolors = array(
687              array('red' => 255, 'green' =>   0, 'blue' =>   0),
688              array('red' => 255, 'green' => 255, 'blue' =>   0),
689              array('red' =>   0, 'green' => 255, 'blue' =>   0),
690              array('red' =>   0, 'green' => 255, 'blue' => 255),
691              array('red' =>   0, 'green' =>   0, 'blue' => 255),
692              array('red' => 100, 'green' => 100, 'blue' => 100)
693              );
694
695         $steps = 2;
696         $numberofcolors = count($predefcolors) * $steps;
697
698         $promille = -1;
699         foreach($predefcolors as $color) {
700             if ($promille < 0) {
701                 $oldcolor = $color;
702                 $promille = 0;
703                 continue;
704             }
705             for ($i = 0; $i < $steps; $i++)
706                 $this->ColorTab[++$promille / $numberofcolors * 1000] = array(
707                     floor(interpolate( $oldcolor['red'],   $color['red'],   $i/$steps )),
708                     floor(interpolate( $oldcolor['green'], $color['green'], $i/$steps )),
709                     floor(interpolate( $oldcolor['blue'],  $color['blue'],  $i/$steps ))
710                 );
711             $oldcolor = $color;
712         }
713 //echo"<pre>";  var_dump($this->ColorTab); echo "</pre>";
714     }
715
716     /**
717      * Translates a value from 0.0 to 1.0 into rainbow color.
718      * red -&gt; orange -&gt; green -&gt; blue -&gt; gray
719      *
720      * @param promille float value between 0.0 and 1.0
721      * @return array(red,green,blue)
722      */
723     function getColor($promille) {
724         foreach( $this->ColorTab as $pro => $col ) {
725             if ($promille*1000 < $pro)
726                 return $col;
727         }
728         $lastcol = end($this->ColorTab);
729         return $lastcol;
730     } // getColor
731 } // WikiPlugin_VisualWiki
732
733 /**
734  * Linear interpolates a value between two point a and b
735  * at a value pos.
736  * @return float  interpolated value
737  */
738
739 function interpolate($a, $b, $pos) {
740     return $a + ($b - $a) * $pos;
741 }
742
743 // $Log: not supported by cvs2svn $
744 // Revision 1.12  2004/09/06 12:08:50  rurban
745 // memory_limit on unix workaround
746 // VisualWiki: default autosize image
747 //
748 // Revision 1.11  2004/09/06 10:10:27  rurban
749 // fixed syntax error
750 //
751 // Revision 1.10  2004/06/19 10:06:38  rurban
752 // Moved lib/plugincache-config.php to config/*.ini
753 // use PLUGIN_CACHED_* constants instead of global $CacheParams
754 //
755 // Revision 1.9  2004/06/03 09:40:57  rurban
756 // WikiPluginCache improvements
757 //
758 // Revision 1.8  2004/01/26 09:18:00  rurban
759 // * changed stored pref representation as before.
760 //   the array of objects is 1) bigger and 2)
761 //   less portable. If we would import packed pref
762 //   objects and the object definition was changed, PHP would fail.
763 //   This doesn't happen with an simple array of non-default values.
764 // * use $prefs->retrieve and $prefs->store methods, where retrieve
765 //   understands the interim format of array of objects also.
766 // * simplified $prefs->get() and fixed $prefs->set()
767 // * added $user->_userid and class '_WikiUser' portability functions
768 // * fixed $user object ->_level upgrading, mostly using sessions.
769 //   this fixes yesterdays problems with loosing authorization level.
770 // * fixed WikiUserNew::checkPass to return the _level
771 // * fixed WikiUserNew::isSignedIn
772 // * added explodePageList to class PageList, support sortby arg
773 // * fixed UserPreferences for WikiUserNew
774 // * fixed WikiPlugin for empty defaults array
775 // * UnfoldSubpages: added pagename arg, renamed pages arg,
776 //   removed sort arg, support sortby arg
777 //
778 // Revision 1.7  2003/03/03 13:57:31  carstenklapp
779 // Added fontpath (see PhpWiki:VisualWiki), tries to be smart about which OS.
780 // (This plugin still doesn't work for me on OS X, but at least image files
781 // are actually being created now in '/tmp/cache'.)
782 //
783 // Revision 1.6  2003/01/18 22:11:45  carstenklapp
784 // Code cleanup:
785 // Reformatting & tabs to spaces;
786 // Added copyleft, getVersion, getDescription, rcs_id.
787 //
788
789 // Local Variables:
790 // mode: php
791 // tab-width: 8
792 // c-basic-offset: 4
793 // c-hanging-comment-ender-p: nil
794 // indent-tabs-mode: nil
795 // End:
796 ?>