]> CyberLeo.Net >> Repos - SourceForge/phpwiki.git/blob - lib/plugin/VisualWiki.php
* changed stored pref representation as before.
[SourceForge/phpwiki.git] / lib / plugin / VisualWiki.php
1 <?php // -*-php-*-
2 rcs_id('$Id: VisualWiki.php,v 1.8 2004-01-26 09:18: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
50     // Name of the Truetypefont - Helvetica is probably easier to read
51     //define('VISUALWIKIFONT', 'Helvetica');
52     //define('VISUALWIKIFONT', 'Times');
53     define('VISUALWIKIFONT', 'Arial');
54
55     // The default font paths do not find your fonts, set the path here:
56     //$fontpath = "/usr/X11R6/lib/X11/fonts/TTF/";
57     //$fontpath = "/usr/share/fonts/default/TrueType/";
58 }
59
60 if (!defined('VISUALWIKI_ALLOWOPTIONS'))
61     define('VISUALWIKI_ALLOWOPTIONS', false);
62
63 require_once "lib/WikiPluginCached.php";
64
65 class WikiPlugin_VisualWiki
66 extends WikiPluginCached
67 {
68     /**
69      * Sets plugin type to map production
70      */
71     function getPluginType() {
72         return PLUGIN_CACHED_MAP;
73     }
74
75     /**
76      * Sets the plugin's name to VisualWiki. It can be called by
77      * <code>&lt;?plugin VisualWiki?&gt;</code>, now. This
78      * name must correspond to the filename and the class name.
79      */
80     function getName() {
81         return "VisualWiki";
82     }
83
84     function getVersion() {
85         return preg_replace("/[Revision: $]/", '',
86                             "\$Revision: 1.8 $");
87     }
88
89     /**
90      * Sets textual description.
91      */
92     function getDescription() {
93         return _("Visualizes the Wiki structure in a graph using the 'dot' commandline tool from graphviz.");
94     }
95
96     /**
97      * Returns default arguments. This is put into a separate
98      * function to allow its usage by both <code>getDefaultArguments</code>
99      * and <code>checkArguments</code>
100      */
101     function defaultarguments() {
102         return array('imgtype'        => 'png',
103                      'width'          => 5,
104                      'height'         => 7,
105                      'colorby'        => 'age', // sort by 'age' or 'revtime'
106                      'fillnodes'      => 'off',
107                      'label'          => 'name',
108                      'shape'          => 'ellipse',
109                      'large_nb'       => 5,
110                      'recent_nb'      => 5,
111                      'refined_nb'     => 15,
112                      'backlink_nb'    => 5,
113                      'neighbour_list' => '',
114                      'exclude_list'   => '',
115                      'include_list'   => '',
116                      'fontsize'       => 10,
117                      'help'           => false );
118     }
119
120     /**
121      * Sets the default arguments. WikiPlugin also regards these as
122      * the allowed arguments. Since WikiPluginCached stores an image
123      * for each different set of parameters, there can be a lot of
124      * these (large) graphs if you allow different parameters.
125      * Set <code>VISUALWIKI_ALLOWOPTIONS</code> to <code>false</code>
126      * to allow no options to be set and use only the default paramters.
127      * This will need an disk space of about 20 Kbyte all the time.
128      */
129     function getDefaultArguments() {
130         if (VISUALWIKI_ALLOWOPTIONS)
131             return $this->defaultarguments();
132         else
133             return array();
134     }
135
136     /**
137      * Substitutes each forbidden parameter value by the default value
138      * defined in <code>defaultarguments</code>.
139      */
140     function checkArguments(&$arg) {
141         extract($arg);
142         $def = $this->defaultarguments();
143
144         if (($width < 3) || ($width > 15))
145             $arg['width'] = $def['width'];
146
147         if (($height < 3) || ($height > 20))
148             $arg['height'] = $def['height'];
149
150         if (($fontsize < 8) || ($fontsize > 24))
151             $arg['fontsize'] = $def['fontsize'];
152
153         if (!in_array($label, array('name', 'number')))
154             $arg['label'] = $def['label'];
155
156         if (!in_array($shape, array('ellipse', 'box', 'point', 'circle',
157                                     'plaintext')))
158             $arg['shape'] = $def['shape'];
159
160         if (!in_array($colorby, array('age', 'revtime')))
161             $arg['colorby'] = $def['colorby'];
162
163         if (!in_array($fillnodes, array('on', 'off')))
164             $arg['fillnodes'] = $def['fillnodes'];
165
166         if (($large_nb < 0) || ($large_nb > 50))
167             $arg['large_nb'] = $def['large_nb'];
168
169         if (($recent_nb < 0)  || ($recent_nb > 50))
170             $arg['recent_nb'] = $def['recent_nb'];
171
172         if (($refined_nb < 0 ) || ( $refined_nb > 50))
173             $arg['refined_nb'] = $def['refined_nb'];
174
175         if (($backlink_nb < 0) || ($backlink_nb > 50))
176             $arg['backlink_nb'] = $def['backlink_nb'];
177
178         // ToDo: check if "ImageCreateFrom$imgtype"() exists.
179         if (!in_array($imgtype, $GLOBALS['CacheParams']['imgtypes']))
180             $arg['imgtype'] = $def['imgtype'];
181         if (empty($fontname))
182             $arg['fontname'] = VISUALWIKIFONT;
183     }
184
185     /**
186      * Checks options, creates help page if necessary, calls both
187      * database access and image map production functions.
188      * @return array($map,$html)
189      */
190     function getMap($dbi, $argarray, $request) {
191         if (!VISUALWIKI_ALLOWOPTIONS)
192             $argarray = $this->defaultarguments();
193         $this->checkArguments($argarray);
194         //extract($argarray);
195         if ($argarray['help'])
196             return array($this->helpImage(), ' '); // FIXME
197         $this->createColors();
198         $this->extract_wikipages($dbi, $argarray);
199         /* ($dbi,  $large, $recent, $refined, $backlink,
200             $neighbour, $excludelist, $includelist, $color); */
201         return $this->invokeDot($argarray);
202         /* ($width, $height, $color, $shape, $text); */
203     }
204
205     /**
206      * Sets the expire time to one day (so the image producing
207      * functions are called seldomly) or to about two minutes
208      * if a help screen is created.
209      */
210     function getExpire($dbi, $argarray, $request) {
211         if ($argarray['help'])
212             return '+120'; // 2 minutes
213         return sprintf('+%d', 3*86000); // approx 3 days
214     }
215
216     /**
217      * Sets the imagetype according to user wishes and
218      * relies on WikiPluginCached to catch illegal image
219      * formats.
220      * (I feel unsure whether this option is reasonable in
221      *  this case, because png will definitely have the
222      *  best results.)
223      *
224      * @return string 'png', 'gif', 'jpeg'
225      */
226     function getImageType($dbi, $argarray, $request) {
227         return $argarray['imgtype'];
228     }
229
230     /**
231      * This gives an alternative text description of
232      * the image map. I do not know whether it interferes
233      * with the <code>title</code> attributes in &lt;area&gt;
234      * tags of the image map. Perhaps this will be removed.
235      * @return string
236      */
237     function getAlt($dbi, $argstr, $request) {
238         return $this->getDescription();
239     }
240
241     // ------------------------------------------------------------------------------------------
242
243     /**
244      * Returns an image containing a usage description of the plugin.
245      * @return string image handle
246      */
247     function helpImage() {
248         $def = $this->defaultarguments();
249         $other_imgtypes = $GLOBALS['CacheParams']['imgtypes'];
250         unset ($other_imgtypes[$def['imgtype']]);
251         $helparr = array(
252             '<?plugin '.$this->getName() .
253             ' img'             => ' = "' . $def['imgtype'] . "(default)|" . join('|',$GLOBALS['CacheParams']['imgtypes']).'"',
254             'width'            => ' = "width in inches"',
255             'height'           => ' = "height in inches"',
256             'fontname'         => ' = "font family"',
257             'fontsize'         => ' = "fontsize in points"',
258             'colorby'          => ' = "age|revtime|none"',
259             'fillnodes'        => ' = "on|off"',
260             'shape'            => ' = "ellipse(default)|box|circle|point"',
261             'label'            => ' = "name|number"',
262             'large_nb'         => ' = "number of largest pages to be selected"',
263             'recent_nb'        => ' = "number of youngest pages"',
264             'refined_nb'       => ' = "#pages with smallest time between revisions"',
265             'backlink_nb'      => ' = "number of pages with most backlinks"',
266             'neighbour_list'   => ' = "find pages linked from and to these pages"',
267             'exclude_list'     => ' = "colon separated list of pages to be excluded"',
268             'include_list'     => ' = "colon separated list"     ?>'
269             );
270         $length = 0;
271         foreach($helparr as $alignright => $alignleft) {
272             $length = max($length, strlen($alignright));
273         }
274         $helptext ='';
275         foreach($helparr as $alignright => $alignleft) {
276             $helptext .= substr('                                                        '
277                                 . $alignright, -$length).$alignleft."\n";
278         }
279         return $this->text2img($helptext, 4, array(1, 0, 0),
280                                array(255, 255, 255));
281     }
282
283
284     /**
285      * Selects the first (smallest or biggest) WikiPages in
286      * a given category.
287      *
288      * @param  number   integer  number of page names to be found
289      * @param  category string   attribute of the pages which is used
290      *                           to compare them
291      * @param  minimum  boolean  true finds smallest, false finds biggest
292      * @return array             list of page names found to be the best
293      */
294     function findbest($number, $category, $minimum ) {
295         // select the $number best in the category '$category'
296         $pages = &$this->pages;
297         $names = &$this->names;
298
299         $selected = array();
300         $i = 0;
301         foreach($names as $name) {
302             if ($i++>=$number)
303                 break;
304             $selected[$name] = $pages[$name][$category];
305         }
306         //echo "<pre>$category "; var_dump($selected); "</pre>";
307         $compareto = $minimum ? 0x79999999 : -0x79999999;
308
309         $i = 0;
310         foreach ($names as $name) {
311             if ($i++<$number)
312                 continue;
313             if ($minimum) {
314                 if (($crit = $pages[$name][$category]) < $compareto) {
315                     $selected[$name] = $crit;
316                     asort($selected, SORT_NUMERIC);
317                     array_pop($selected);
318                     $compareto = end($selected);
319                 }
320             } elseif (($crit = $pages[$name][$category]) > $compareto)  {
321                 $selected[$name] = $crit;
322                 arsort($selected, SORT_NUMERIC);
323                 array_pop($selected);
324                 $compareto = end($selected);
325             }
326         }
327         //echo "<pre>$category "; var_dump($selected); "</pre>";
328
329         return array_keys($selected);
330     }
331
332
333     /**
334     * Extracts a subset of all pages from the wiki and find their
335     * connections to other pages. Also collects some page features
336     * like size, age, revision number which are used to find the
337     * most attractive pages.
338     *
339     * @param  dbi         WikiDB   database handle to access all Wiki pages
340     * @param  LARGE       integer  number of largest pages which should
341     *                              be included
342     * @param  RECENT      integer  number of the youngest pages to be included
343     * @param  REFINED     integer  number of the pages with shortes revision
344     *                              interval
345     * @param  BACKLINK    integer  number of the pages with most backlinks
346     * @param  EXCLUDELIST string   colon ':' separated list of page names which
347     *                              should not be displayed (like PhpWiki, for
348     *                              example)
349     * @param  INCLUDELIST string   colon separated list of pages which are
350     *                              allways included (for example your own
351     *                              page :)
352     * @param  COLOR       string   'age', 'revtime' or 'none'; Selects which
353     *                              page feature is used to determine the
354     *                              filling color of the nodes in the graph.
355     * @return void
356     */
357     function extract_wikipages($dbi, $argarray) {
358         // $LARGE, $RECENT, $REFINED, $BACKLINK, $NEIGHBOUR,
359         // $EXCLUDELIST, $INCLUDELIST,$COLOR
360         $now = time();
361
362         extract($argarray);
363         // FIXME: gettextify?
364         $exclude_list   = explode(':', $exclude_list);
365         $include_list   = explode(':', $include_list);
366         $neighbour_list = explode(':', $neighbour_list);
367
368         // FIXME remove INCLUDED from EXCLUDED
369
370         // collect all pages
371         $allpages = $dbi->getAllPages();
372         $pages = &$this->pages;
373         $countpages = 0;
374         while ($page = $allpages->next()) {
375             $name = $page->getName();
376
377             // skip exluded pages
378             if (in_array($name, $exclude_list)) continue;
379
380             // false = get links from actual page
381             // true  = get links to actual page ("backlinks")
382             $backlinks = $page->getLinks(true);
383             unset($bconnection);
384             $bconnection = array();
385             while ($blink = $backlinks->next()) {
386                 array_push($bconnection, $blink->getName());
387             }
388             unset($backlinks);
389
390             // include all neighbours of pages listed in $NEIGHBOUR
391             if (in_array($name,$neighbour_list)) {
392                 $l = $page->getLinks(false);
393                 $con = array();
394                 while ($link = $l->next()) {
395                     array_push($con, $link->getName());
396                 }
397                 $include_list = array_merge($include_list, $bconnection, $con);
398                 unset($l);
399                 unset($con);
400             }
401
402             unset($currev);
403             $currev = $page->getCurrentRevision();
404
405             $pages[$name] = array(
406                 'age'         => $now-$currev->get('mtime'),
407                 'revnr'       => $currev->getVersion(),
408                 'links'       => array(),
409                 'backlink_nb' => count($bconnection),
410                 'backlinks'   => $bconnection,
411                 'size'        => 1000 // FIXME
412                 );
413             $pages[$name]['revtime'] = $pages[$name]['age'] / ($pages[$name]['revnr']);
414
415             unset($page);
416         }
417         unset($allpages);
418         $this->names = array_keys($pages);
419
420         $countpages = count($pages);
421
422         // now select each page matching to given parameters
423         $all_selected = array_unique(array_merge(
424             $this->findbest($recent_nb,   'age',         true),
425             $this->findbest($refined_nb,  'revtime',     true),
426             $x = $this->findbest($backlink_nb, 'backlink_nb', false),
427 //            $this->findbest($large_nb,    'size',        false),
428             $include_list));
429
430         foreach($all_selected as $name)
431             if (isset($pages[$name]))
432                 $newpages[$name] = $pages[$name];
433         unset($this->names);
434         unset($this->pages);
435         $this->pages = $newpages;
436         $pages = &$this->pages;
437         $this->names = array_keys($pages);
438         unset($newpages);
439         unset($all_selected);
440
441         $countpages = count($pages);
442
443         // remove dead links and collect links
444         reset($pages);
445         while( list($name,$page) = each($pages) ) {
446             if (is_array($page['backlinks'])) {
447                 reset($page['backlinks']);
448                 while ( list($index, $link) = each( $page['backlinks'] ) ) {
449                     if ( !isset($pages[$link]) || $link == $name ) {
450                         unset($pages[$name]['backlinks'][$index]);
451                     } else {
452                         array_push($pages[$link]['links'],$name);
453                         //array_push($this->everylink, array($link,$name));
454                     }
455                 }
456             }
457         }
458
459         if ($colorby == 'none')
460             return;
461         list($oldestname) = $this->findbest(1, $colorby, false);
462         $this->oldest = $pages[$oldestname][$colorby];
463         foreach($this->names as $name)
464             $pages[$name]['color'] = $this->getColor($pages[$name][$colorby] / $this->oldest);
465     } // extract_wikipages
466
467     /**
468      * Creates the text file description of the graph needed to invoke
469      * <code>dot</code>.
470      *
471      * @param filename  string  name of the dot file to be created
472      * @param width     float   width of the output graph in inches
473      * @param height    float   height of the graph in inches
474      * @param colorby   string  color sceme beeing used ('age', 'revtime',
475      *                                                   'none')
476      * @param shape     string  node shape; 'ellipse', 'box', 'circle', 'point'
477      * @param label     string  'name': label by name,
478      *                          'number': label by unique number
479      * @return boolean          error status; true=ok; false=error
480      */
481     function createDotFile($filename, $argarray) {
482         extract($argarray);
483         if (!$fp = fopen($filename, 'w'))
484             return false;
485
486         $fillstring = ($fillnodes == 'on') ? 'style=filled,' : '';
487
488         $ok = true;
489         $names = &$this->names;
490         $pages = &$this->pages;
491
492         $nametonumber = array_flip($names);
493
494         $dot = "digraph VisualWiki {\n" // }
495              . (!empty($fontpath) ? "    fontpath=\"$fontpath\"\n" : "")
496              . "    size=\"$width,$height\";\n    ";
497
498         switch ($shape) {
499         case 'point':
500             $dot .= "edge [arrowhead=none];\nnode [shape=$shape,fontname=$fontname,width=0.15,height=0.15,fontsize=$fontsize];\n";
501             break;
502         case 'box':
503             $dot .= "node [shape=$shape,fontname=$fontname,width=0.4,height=0.4,fontsize=$fontsize];\n";
504             break;
505         case 'circle':
506             $dot .= "node [shape=$shape,fontname=$fontname,width=0.25,height=0.25,fontsize=$fontsize];\n";
507             break;
508         default :
509             $dot .= "node [fontname=$fontname,shape=$shape,fontsize=$fontsize];\n" ;
510         }
511         $dot .= "\n";
512         $i = 0;
513         foreach ($names as $name) {
514
515             $url = rawurlencode($name);
516             // patch to allow Page/SubPage
517             $url = preg_replace('/' . urlencode(SUBPAGE_SEPARATOR) . '/',
518                                 SUBPAGE_SEPARATOR, $url);
519             $nodename = ($label != 'name' ? $nametonumber[$name] + 1 : $name);
520
521             $dot .= "    \"$nodename\" [URL=\"$url\"";
522             if ($colorby != 'none') {
523                 $col = $pages[$name]['color'];
524                 $dot .= sprintf(',%scolor="#%02X%02X%02X"', $fillstring,
525                                 $col[0], $col[1], $col[2]);
526             }
527             $dot .= "];\n";
528
529             if (!empty($pages[$name]['links'])) {
530                 unset($linkarray);
531                 if ($label != 'name')
532                     foreach($pages[$name]['links'] as $linkname)
533                         $linkarray[] = $nametonumber[$linkname] + 1;
534                 else
535                     $linkarray = $pages[$name]['links'];
536                 $linkstring = join('"; "', $linkarray );
537
538                 $c = count($pages[$name]['links']);
539                 $dot .= "        \"$nodename\" -> "
540                      . ($c>1?'{':'')
541                      . "\"$linkstring\";"
542                      . ($c>1?'}':'')
543                      . "\n";
544             }
545         }
546         if ($colorby != 'none') {
547             $dot .= "\n    subgraph cluster_legend {\n"
548                  . "         node[fontname=$fontname,shape=box,width=0.4,height=0.4,fontsize=$fontsize];\n"
549                  . "         fillcolor=lightgrey;\n"
550                  . "         style=filled;\n"
551                  . "         fontname=$fontname;\n"
552                  . "         fontsize=$fontsize;\n"
553                  . "         label=\"".gettext("Legend")."\";\n";
554             $oldest= ceil($this->oldest / (24 * 3600));
555             $max = 5;
556             $legend = array();
557             for($i = 0; $i < $max; $i++) {
558                 $time = floor($i / $max * $oldest);
559                 $name = '"' . $time .' '. _("days") .'"';
560                 $col = $this->getColor($i/$max);
561                 $dot .= sprintf('       %s [%scolor="#%02X%02X%02X"];',
562                                 $name, $fillstring, $col[0], $col[1], $col[2])
563                     . "\n";
564                 $legend[] = $name;
565             }
566             $dot .= '        '. join(' -> ', $legend)
567                  . ";\n    }\n";
568
569         }
570
571         // {
572         $dot .= "}\n";
573
574         $ok = fwrite($fp, $dot);
575         $ok = fclose($fp) && $ok;  // close anyway
576
577         return $ok;
578     }
579
580     /**
581      * Execute system command.
582      *
583      * @param  cmd string   command to be invoked
584      * @return     boolean  error status; true=ok; false=error
585      */
586     function execute($cmd) {
587         exec($cmd, $errortxt, $returnval);
588         return ($returnval == 0);
589     }
590
591     /**
592      * Produces a dot file, calls dot twice to obtain an image and a
593      * text description of active areas for hyperlinking and returns
594      * an image and an html map.
595      *
596      * @param width     float   width of the output graph in inches
597      * @param height    float   height of the graph in inches
598      * @param colorby   string  color sceme beeing used ('age', 'revtime',
599      *                                                   'none')
600      * @param shape     string  node shape; 'ellipse', 'box', 'circle', 'point'
601      * @param label     string  not used anymore
602      */
603     function invokeDot($argarray) {
604         global $dotbin;
605         $cacheparams = $GLOBALS['CacheParams'];
606         $tempfiles = tempnam($cacheparams['cache_dir'], 'VisualWiki');
607         $gif = $argarray['imgtype'];
608         $ImageCreateFromFunc = "ImageCreateFrom$gif";
609         $ok =  $tempfiles
610             && $this->createDotFile($tempfiles.'.dot',$argarray)
611             && $this->execute("$dotbin -T$gif $tempfiles.dot -o $tempfiles.$gif")
612             && $this->execute("$dotbin -Timap $tempfiles.dot -o $tempfiles.map")
613             && file_exists( "$tempfiles.$gif" )
614             && file_exists( $tempfiles.'.map' )
615             && ($img = $ImageCreateFromFunc( "$tempfiles.$gif" ))
616             && ($fp = fopen($tempfiles.'.map','r'));
617
618         $map = HTML();
619         if ($ok) {
620             while (!feof($fp)) {
621                 $line = fgets($fp, 1000);
622                 if (substr($line, 0, 1) == '#')
623                     continue;
624                 list($shape, $url, $e1, $e2, $e3, $e4) = sscanf($line,
625                                                                 "%s %s %d,%d %d,%d");
626                 if ($shape != 'rect')
627                     continue;
628
629                 // dot sometimes gives not allways the right order so
630                 // so we have to sort a bit
631                 $x1 = min($e1, $e3);
632                 $x2 = max($e1, $e3);
633                 $y1 = min($e2, $e4);
634                 $y2 = max($e2, $e4);
635                 $map->pushContent(HTML::area(array(
636                             'shape'  => 'rect',
637                             'coords' => "$x1,$y1,$x2,$y2",
638                             'href'   => $url,
639                             'title'  => rawurldecode($url),
640                             'alt' => $url)));
641                 }
642             fclose($fp);
643 //trigger_error("url=".$url);
644         }
645
646         // clean up tempfiles
647         if ($ok && empty($_GET['debug']) && $tempfiles) {
648             //unlink($tempfiles);
649             //unlink("$tempfiles.$gif");
650             //unlink($tempfiles . '.map');
651             //unlink($tempfiles . '.dot');
652 //trigger_error($tempfiles);
653         }
654
655         if ($ok)
656             return array($img, $map);
657         else
658             return array(false, false);
659     } // invokeDot
660
661     /**
662      * Prepares some rainbow colors for the nodes of the graph
663      * and stores them in an array which may be accessed with
664      * <code>getColor</code>.
665      */
666     function createColors() {
667         $predefcolors = array(
668              array('red' => 255, 'green' =>   0, 'blue' =>   0),
669              array('red' => 255, 'green' => 255, 'blue' =>   0),
670              array('red' =>   0, 'green' => 255, 'blue' =>   0),
671              array('red' =>   0, 'green' => 255, 'blue' => 255),
672              array('red' =>   0, 'green' =>   0, 'blue' => 255),
673              array('red' => 100, 'green' => 100, 'blue' => 100)
674              );
675
676         $steps = 2;
677         $numberofcolors = count($predefcolors) * $steps;
678
679         $promille = -1;
680         foreach($predefcolors as $color) {
681             if ($promille < 0) {
682                 $oldcolor = $color;
683                 $promille = 0;
684                 continue;
685             }
686             for ($i = 0; $i < $steps; $i++)
687                 $this->ColorTab[++$promille / $numberofcolors * 1000] = array(
688                     floor(interpolate( $oldcolor['red'],   $color['red'],   $i/$steps )),
689                     floor(interpolate( $oldcolor['green'], $color['green'], $i/$steps )),
690                     floor(interpolate( $oldcolor['blue'],  $color['blue'],  $i/$steps ))
691                 );
692             $oldcolor = $color;
693         }
694 //echo"<pre>";  var_dump($this->ColorTab); echo "</pre>";
695     }
696
697     /**
698      * Translates a value from 0.0 to 1.0 into rainbow color.
699      * red -&gt; orange -&gt; green -&gt; blue -&gt; gray
700      *
701      * @param promille float value between 0.0 and 1.0
702      * @return array(red,green,blue)
703      */
704     function getColor($promille) {
705         foreach( $this->ColorTab as $pro => $col ) {
706             if ($promille*1000 < $pro)
707                 return $col;
708         }
709         $lastcol = end($this->ColorTab);
710         return $lastcol;
711     } // getColor
712 } // WikiPlugin_VisualWiki
713
714 /**
715  * Linear interpolates a value between two point a and b
716  * at a value pos.
717  * @return float  interpolated value
718  */
719
720 function interpolate($a, $b, $pos) {
721     return $a + ($b - $a) * $pos;
722 }
723
724 // $Log: not supported by cvs2svn $
725 // Revision 1.7  2003/03/03 13:57:31  carstenklapp
726 // Added fontpath (see PhpWiki:VisualWiki), tries to be smart about which OS.
727 // (This plugin still doesn't work for me on OS X, but at least image files
728 // are actually being created now in '/tmp/cache'.)
729 //
730 // Revision 1.6  2003/01/18 22:11:45  carstenklapp
731 // Code cleanup:
732 // Reformatting & tabs to spaces;
733 // Added copyleft, getVersion, getDescription, rcs_id.
734 //
735
736 // Local Variables:
737 // mode: php
738 // tab-width: 8
739 // c-basic-offset: 4
740 // c-hanging-comment-ender-p: nil
741 // indent-tabs-mode: nil
742 // End:
743 ?>