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