2 rcs_id('$Id: VisualWiki.php,v 1.8 2004-01-26 09:18:00 rurban Exp $');
4 Copyright (C) 2002 Johannes Große (Johannes Große)
6 This file is (not yet) part of PhpWiki.
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.
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.
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
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
31 define('VISUALWIKI_ALLOWOPTIONS', true);
33 if (PHP_OS == "Darwin") { // Mac OS X
34 $dotbin = '/sw/bin/dot'; // graphviz via Fink
35 //$dotbin = '/usr/local/bin/dot';
37 // Name of the Truetypefont - at least LucidaSansRegular.ttf is always present on OS X
38 define('VISUALWIKIFONT', 'LucidaSansRegular');
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/";
44 elseif (isWindows()) {
46 define('VISUALWIKIFONT', 'Arial');
48 $dotbin = '/usr/local/bin/dot';
50 // Name of the Truetypefont - Helvetica is probably easier to read
51 //define('VISUALWIKIFONT', 'Helvetica');
52 //define('VISUALWIKIFONT', 'Times');
53 define('VISUALWIKIFONT', 'Arial');
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/";
60 if (!defined('VISUALWIKI_ALLOWOPTIONS'))
61 define('VISUALWIKI_ALLOWOPTIONS', false);
63 require_once "lib/WikiPluginCached.php";
65 class WikiPlugin_VisualWiki
66 extends WikiPluginCached
69 * Sets plugin type to map production
71 function getPluginType() {
72 return PLUGIN_CACHED_MAP;
76 * Sets the plugin's name to VisualWiki. It can be called by
77 * <code><?plugin VisualWiki?></code>, now. This
78 * name must correspond to the filename and the class name.
84 function getVersion() {
85 return preg_replace("/[Revision: $]/", '',
90 * Sets textual description.
92 function getDescription() {
93 return _("Visualizes the Wiki structure in a graph using the 'dot' commandline tool from graphviz.");
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>
101 function defaultarguments() {
102 return array('imgtype' => 'png',
105 'colorby' => 'age', // sort by 'age' or 'revtime'
106 'fillnodes' => 'off',
108 'shape' => 'ellipse',
113 'neighbour_list' => '',
114 'exclude_list' => '',
115 'include_list' => '',
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.
129 function getDefaultArguments() {
130 if (VISUALWIKI_ALLOWOPTIONS)
131 return $this->defaultarguments();
137 * Substitutes each forbidden parameter value by the default value
138 * defined in <code>defaultarguments</code>.
140 function checkArguments(&$arg) {
142 $def = $this->defaultarguments();
144 if (($width < 3) || ($width > 15))
145 $arg['width'] = $def['width'];
147 if (($height < 3) || ($height > 20))
148 $arg['height'] = $def['height'];
150 if (($fontsize < 8) || ($fontsize > 24))
151 $arg['fontsize'] = $def['fontsize'];
153 if (!in_array($label, array('name', 'number')))
154 $arg['label'] = $def['label'];
156 if (!in_array($shape, array('ellipse', 'box', 'point', 'circle',
158 $arg['shape'] = $def['shape'];
160 if (!in_array($colorby, array('age', 'revtime')))
161 $arg['colorby'] = $def['colorby'];
163 if (!in_array($fillnodes, array('on', 'off')))
164 $arg['fillnodes'] = $def['fillnodes'];
166 if (($large_nb < 0) || ($large_nb > 50))
167 $arg['large_nb'] = $def['large_nb'];
169 if (($recent_nb < 0) || ($recent_nb > 50))
170 $arg['recent_nb'] = $def['recent_nb'];
172 if (($refined_nb < 0 ) || ( $refined_nb > 50))
173 $arg['refined_nb'] = $def['refined_nb'];
175 if (($backlink_nb < 0) || ($backlink_nb > 50))
176 $arg['backlink_nb'] = $def['backlink_nb'];
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;
186 * Checks options, creates help page if necessary, calls both
187 * database access and image map production functions.
188 * @return array($map,$html)
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); */
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.
210 function getExpire($dbi, $argarray, $request) {
211 if ($argarray['help'])
212 return '+120'; // 2 minutes
213 return sprintf('+%d', 3*86000); // approx 3 days
217 * Sets the imagetype according to user wishes and
218 * relies on WikiPluginCached to catch illegal image
220 * (I feel unsure whether this option is reasonable in
221 * this case, because png will definitely have the
224 * @return string 'png', 'gif', 'jpeg'
226 function getImageType($dbi, $argarray, $request) {
227 return $argarray['imgtype'];
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 <area>
234 * tags of the image map. Perhaps this will be removed.
237 function getAlt($dbi, $argstr, $request) {
238 return $this->getDescription();
241 // ------------------------------------------------------------------------------------------
244 * Returns an image containing a usage description of the plugin.
245 * @return string image handle
247 function helpImage() {
248 $def = $this->defaultarguments();
249 $other_imgtypes = $GLOBALS['CacheParams']['imgtypes'];
250 unset ($other_imgtypes[$def['imgtype']]);
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" ?>'
271 foreach($helparr as $alignright => $alignleft) {
272 $length = max($length, strlen($alignright));
275 foreach($helparr as $alignright => $alignleft) {
276 $helptext .= substr(' '
277 . $alignright, -$length).$alignleft."\n";
279 return $this->text2img($helptext, 4, array(1, 0, 0),
280 array(255, 255, 255));
285 * Selects the first (smallest or biggest) WikiPages in
288 * @param number integer number of page names to be found
289 * @param category string attribute of the pages which is used
291 * @param minimum boolean true finds smallest, false finds biggest
292 * @return array list of page names found to be the best
294 function findbest($number, $category, $minimum ) {
295 // select the $number best in the category '$category'
296 $pages = &$this->pages;
297 $names = &$this->names;
301 foreach($names as $name) {
304 $selected[$name] = $pages[$name][$category];
306 //echo "<pre>$category "; var_dump($selected); "</pre>";
307 $compareto = $minimum ? 0x79999999 : -0x79999999;
310 foreach ($names as $name) {
314 if (($crit = $pages[$name][$category]) < $compareto) {
315 $selected[$name] = $crit;
316 asort($selected, SORT_NUMERIC);
317 array_pop($selected);
318 $compareto = end($selected);
320 } elseif (($crit = $pages[$name][$category]) > $compareto) {
321 $selected[$name] = $crit;
322 arsort($selected, SORT_NUMERIC);
323 array_pop($selected);
324 $compareto = end($selected);
327 //echo "<pre>$category "; var_dump($selected); "</pre>";
329 return array_keys($selected);
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.
339 * @param dbi WikiDB database handle to access all Wiki pages
340 * @param LARGE integer number of largest pages which should
342 * @param RECENT integer number of the youngest pages to be included
343 * @param REFINED integer number of the pages with shortes revision
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
349 * @param INCLUDELIST string colon separated list of pages which are
350 * allways included (for example your own
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.
357 function extract_wikipages($dbi, $argarray) {
358 // $LARGE, $RECENT, $REFINED, $BACKLINK, $NEIGHBOUR,
359 // $EXCLUDELIST, $INCLUDELIST,$COLOR
363 // FIXME: gettextify?
364 $exclude_list = explode(':', $exclude_list);
365 $include_list = explode(':', $include_list);
366 $neighbour_list = explode(':', $neighbour_list);
368 // FIXME remove INCLUDED from EXCLUDED
371 $allpages = $dbi->getAllPages();
372 $pages = &$this->pages;
374 while ($page = $allpages->next()) {
375 $name = $page->getName();
377 // skip exluded pages
378 if (in_array($name, $exclude_list)) continue;
380 // false = get links from actual page
381 // true = get links to actual page ("backlinks")
382 $backlinks = $page->getLinks(true);
384 $bconnection = array();
385 while ($blink = $backlinks->next()) {
386 array_push($bconnection, $blink->getName());
390 // include all neighbours of pages listed in $NEIGHBOUR
391 if (in_array($name,$neighbour_list)) {
392 $l = $page->getLinks(false);
394 while ($link = $l->next()) {
395 array_push($con, $link->getName());
397 $include_list = array_merge($include_list, $bconnection, $con);
403 $currev = $page->getCurrentRevision();
405 $pages[$name] = array(
406 'age' => $now-$currev->get('mtime'),
407 'revnr' => $currev->getVersion(),
409 'backlink_nb' => count($bconnection),
410 'backlinks' => $bconnection,
411 'size' => 1000 // FIXME
413 $pages[$name]['revtime'] = $pages[$name]['age'] / ($pages[$name]['revnr']);
418 $this->names = array_keys($pages);
420 $countpages = count($pages);
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),
430 foreach($all_selected as $name)
431 if (isset($pages[$name]))
432 $newpages[$name] = $pages[$name];
435 $this->pages = $newpages;
436 $pages = &$this->pages;
437 $this->names = array_keys($pages);
439 unset($all_selected);
441 $countpages = count($pages);
443 // remove dead links and collect links
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]);
452 array_push($pages[$link]['links'],$name);
453 //array_push($this->everylink, array($link,$name));
459 if ($colorby == 'none')
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
468 * Creates the text file description of the graph needed to invoke
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',
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
481 function createDotFile($filename, $argarray) {
483 if (!$fp = fopen($filename, 'w'))
486 $fillstring = ($fillnodes == 'on') ? 'style=filled,' : '';
489 $names = &$this->names;
490 $pages = &$this->pages;
492 $nametonumber = array_flip($names);
494 $dot = "digraph VisualWiki {\n" // }
495 . (!empty($fontpath) ? " fontpath=\"$fontpath\"\n" : "")
496 . " size=\"$width,$height\";\n ";
500 $dot .= "edge [arrowhead=none];\nnode [shape=$shape,fontname=$fontname,width=0.15,height=0.15,fontsize=$fontsize];\n";
503 $dot .= "node [shape=$shape,fontname=$fontname,width=0.4,height=0.4,fontsize=$fontsize];\n";
506 $dot .= "node [shape=$shape,fontname=$fontname,width=0.25,height=0.25,fontsize=$fontsize];\n";
509 $dot .= "node [fontname=$fontname,shape=$shape,fontsize=$fontsize];\n" ;
513 foreach ($names as $name) {
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);
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]);
529 if (!empty($pages[$name]['links'])) {
531 if ($label != 'name')
532 foreach($pages[$name]['links'] as $linkname)
533 $linkarray[] = $nametonumber[$linkname] + 1;
535 $linkarray = $pages[$name]['links'];
536 $linkstring = join('"; "', $linkarray );
538 $c = count($pages[$name]['links']);
539 $dot .= " \"$nodename\" -> "
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"
551 . " fontname=$fontname;\n"
552 . " fontsize=$fontsize;\n"
553 . " label=\"".gettext("Legend")."\";\n";
554 $oldest= ceil($this->oldest / (24 * 3600));
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])
566 $dot .= ' '. join(' -> ', $legend)
574 $ok = fwrite($fp, $dot);
575 $ok = fclose($fp) && $ok; // close anyway
581 * Execute system command.
583 * @param cmd string command to be invoked
584 * @return boolean error status; true=ok; false=error
586 function execute($cmd) {
587 exec($cmd, $errortxt, $returnval);
588 return ($returnval == 0);
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.
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',
600 * @param shape string node shape; 'ellipse', 'box', 'circle', 'point'
601 * @param label string not used anymore
603 function invokeDot($argarray) {
605 $cacheparams = $GLOBALS['CacheParams'];
606 $tempfiles = tempnam($cacheparams['cache_dir'], 'VisualWiki');
607 $gif = $argarray['imgtype'];
608 $ImageCreateFromFunc = "ImageCreateFrom$gif";
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'));
621 $line = fgets($fp, 1000);
622 if (substr($line, 0, 1) == '#')
624 list($shape, $url, $e1, $e2, $e3, $e4) = sscanf($line,
625 "%s %s %d,%d %d,%d");
626 if ($shape != 'rect')
629 // dot sometimes gives not allways the right order so
630 // so we have to sort a bit
635 $map->pushContent(HTML::area(array(
637 'coords' => "$x1,$y1,$x2,$y2",
639 'title' => rawurldecode($url),
643 //trigger_error("url=".$url);
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);
656 return array($img, $map);
658 return array(false, false);
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>.
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)
677 $numberofcolors = count($predefcolors) * $steps;
680 foreach($predefcolors as $color) {
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 ))
694 //echo"<pre>"; var_dump($this->ColorTab); echo "</pre>";
698 * Translates a value from 0.0 to 1.0 into rainbow color.
699 * red -> orange -> green -> blue -> gray
701 * @param promille float value between 0.0 and 1.0
702 * @return array(red,green,blue)
704 function getColor($promille) {
705 foreach( $this->ColorTab as $pro => $col ) {
706 if ($promille*1000 < $pro)
709 $lastcol = end($this->ColorTab);
712 } // WikiPlugin_VisualWiki
715 * Linear interpolates a value between two point a and b
717 * @return float interpolated value
720 function interpolate($a, $b, $pos) {
721 return $a + ($b - $a) * $pos;
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'.)
730 // Revision 1.6 2003/01/18 22:11:45 carstenklapp
732 // Reformatting & tabs to spaces;
733 // Added copyleft, getVersion, getDescription, rcs_id.
740 // c-hanging-comment-ender-p: nil
741 // indent-tabs-mode: nil