4 * Copyright (C) 2002 Johannes Große
6 * This file is 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 along
19 * with PhpWiki; if not, write to the Free Software Foundation, Inc.,
20 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
24 * Produces graphical site map of PhpWiki
25 * @author Johannes Große
28 /* define('VISUALWIKI_ALLOWOPTIONS', true); */
29 if (!defined('VISUALWIKI_ALLOWOPTIONS'))
30 define('VISUALWIKI_ALLOWOPTIONS', false);
32 require_once 'lib/plugin/GraphViz.php';
34 class WikiPlugin_VisualWiki
35 extends WikiPlugin_GraphViz
38 * Sets plugin type to map production
40 function getPluginType()
42 return ($GLOBALS['request']->getArg('debug')) ? PLUGIN_CACHED_IMG_ONDEMAND
46 function getDescription()
48 return _("Visualizes the Wiki structure in a graph using the 'dot' commandline tool from graphviz.");
51 function defaultArguments()
53 return array('imgtype' => 'png',
54 'width' => false, // was 5, scale it automatically
55 'height' => false, // was 7, scale it automatically
56 'colorby' => 'age', // sort by 'age' or 'revtime'
64 'neighbour_list' => '',
73 * Sets the default arguments. WikiPlugin also regards these as
74 * the allowed arguments. Since WikiPluginCached stores an image
75 * for each different set of parameters, there can be a lot of
76 * these (large) graphs if you allow different parameters.
77 * Set <code>VISUALWIKI_ALLOWOPTIONS</code> to <code>false</code>
78 * to allow no options to be set and use only the default parameters.
79 * This will need an disk space of about 20 Kbyte all the time.
81 function getDefaultArguments()
83 if (VISUALWIKI_ALLOWOPTIONS)
84 return $this->defaultarguments();
90 * Substitutes each forbidden parameter value by the default value
91 * defined in <code>defaultarguments</code>.
93 function checkArguments(&$arg)
96 $def = $this->defaultarguments();
97 if (($width < 3) || ($width > 15))
98 $arg['width'] = $def['width'];
99 if (($height < 3) || ($height > 20))
100 $arg['height'] = $def['height'];
101 if (($fontsize < 8) || ($fontsize > 24))
102 $arg['fontsize'] = $def['fontsize'];
103 if (!in_array($label, array('name', 'number')))
104 $arg['label'] = $def['label'];
106 if (!in_array($shape, array('ellipse', 'box', 'point', 'circle',
109 $arg['shape'] = $def['shape'];
110 if (!in_array($colorby, array('age', 'revtime')))
111 $arg['colorby'] = $def['colorby'];
112 if (!in_array($fillnodes, array('on', 'off')))
113 $arg['fillnodes'] = $def['fillnodes'];
114 if (($large_nb < 0) || ($large_nb > 50))
115 $arg['large_nb'] = $def['large_nb'];
116 if (($recent_nb < 0) || ($recent_nb > 50))
117 $arg['recent_nb'] = $def['recent_nb'];
118 if (($refined_nb < 0) || ($refined_nb > 50))
119 $arg['refined_nb'] = $def['refined_nb'];
120 if (($backlink_nb < 0) || ($backlink_nb > 50))
121 $arg['backlink_nb'] = $def['backlink_nb'];
122 // ToDo: check if "ImageCreateFrom$imgtype"() exists.
123 if (!in_array($imgtype, $GLOBALS['PLUGIN_CACHED_IMGTYPES']))
124 $arg['imgtype'] = $def['imgtype'];
125 if (empty($fontname))
126 $arg['fontname'] = VISUALWIKIFONT;
130 * Checks options, creates help page if necessary, calls both
131 * database access and image map production functions.
133 * @param array $argarray
134 * @param Request $request
135 * @return array($map,$html)
137 function getMap($dbi, $argarray, $request)
139 if (!VISUALWIKI_ALLOWOPTIONS)
140 $argarray = $this->defaultarguments();
141 $this->checkArguments($argarray);
142 $request->setArg('debug', $argarray['debug']);
143 //extract($argarray);
144 if ($argarray['help'])
145 return array($this->helpImage(), ' '); // FIXME
146 $this->createColors();
147 $this->extract_wikipages($dbi, $argarray);
148 /* ($dbi, $large, $recent, $refined, $backlink,
149 $neighbour, $excludelist, $includelist, $color); */
150 $result = $this->invokeDot($argarray);
151 if (isa($result, 'HtmlElement'))
152 return array(false, $result);
155 /* => ($width, $height, $color, $shape, $text); */
159 * Returns an image containing a usage description of the plugin.
160 * @return string image handle
164 $def = $this->defaultarguments();
165 $other_imgtypes = $GLOBALS['PLUGIN_CACHED_IMGTYPES'];
166 unset ($other_imgtypes[$def['imgtype']]);
168 '<<' . $this->getName() .
169 ' img' => ' = "' . $def['imgtype'] . "(default)|" . join('|', $GLOBALS['PLUGIN_CACHED_IMGTYPES']) . '"',
170 'width' => ' = "width in inches"',
171 'height' => ' = "height in inches"',
172 'fontname' => ' = "font family"',
173 'fontsize' => ' = "fontsize in points"',
174 'colorby' => ' = "age|revtime|none"',
175 'fillnodes' => ' = "on|off"',
176 'shape' => ' = "ellipse(default)|box|circle|point"',
177 'label' => ' = "name|number"',
178 'large_nb' => ' = "number of largest pages to be selected"',
179 'recent_nb' => ' = "number of youngest pages"',
180 'refined_nb' => ' = "#pages with smallest time between revisions"',
181 'backlink_nb' => ' = "number of pages with most backlinks"',
182 'neighbour_list' => ' = "find pages linked from and to these pages"',
183 'exclude_list' => ' = "colon separated list of pages to be excluded"',
184 'include_list' => ' = "colon separated list" >>'
187 foreach ($helparr as $alignright => $alignleft) {
188 $length = max($length, strlen($alignright));
191 foreach ($helparr as $alignright => $alignleft) {
192 $helptext .= substr(' '
193 . $alignright, -$length) . $alignleft . "\n";
195 return $this->text2img($helptext, 4, array(1, 0, 0),
196 array(255, 255, 255));
200 * Selects the first (smallest or biggest) WikiPages in
204 * @param string $category
205 * @param bool $minimum
206 * @internal param int $number number of page names to be found
207 * @internal param string $category attribute of the pages which is used
209 * @internal param bool $minimum true finds smallest, false finds biggest
210 * @return array list of page names found to be the best
212 function findbest($number, $category, $minimum)
214 // select the $number best in the category '$category'
215 $pages = &$this->pages;
216 $names = &$this->names;
220 foreach ($names as $name) {
223 $selected[$name] = $pages[$name][$category];
225 //echo "<pre>$category "; var_dump($selected); "</pre>";
226 $compareto = $minimum ? 0x79999999 : -0x79999999;
229 foreach ($names as $name) {
233 if (($crit = $pages[$name][$category]) < $compareto) {
234 $selected[$name] = $crit;
235 asort($selected, SORT_NUMERIC);
236 array_pop($selected);
237 $compareto = end($selected);
239 } elseif (($crit = $pages[$name][$category]) > $compareto) {
240 $selected[$name] = $crit;
241 arsort($selected, SORT_NUMERIC);
242 array_pop($selected);
243 $compareto = end($selected);
246 //echo "<pre>$category "; var_dump($selected); "</pre>";
248 return array_keys($selected);
252 * Extracts a subset of all pages from the wiki and find their
253 * connections to other pages. Also collects some page features
254 * like size, age, revision number which are used to find the
255 * most attractive pages.
259 * @internal param \WikiDB $dbi database handle to access all Wiki pages
260 * @internal param int $LARGE number of largest pages which should
262 * @internal param int $RECENT number of the youngest pages to be included
263 * @internal param int $REFINED number of the pages with shortes revision
265 * @internal param int $BACKLINK number of the pages with most backlinks
266 * @internal param string $EXCLUDELIST colon ':' separated list of page names which
267 * should not be displayed (like PhpWiki, for
269 * @internal param string $INCLUDELIST colon separated list of pages which are
270 * always included (for example your own
272 * @internal param string $COLOR 'age', 'revtime' or 'none'; Selects which
273 * page feature is used to determine the
274 * filling color of the nodes in the graph.
277 function extract_wikipages($dbi, $argarray)
279 // $LARGE, $RECENT, $REFINED, $BACKLINK, $NEIGHBOUR,
280 // $EXCLUDELIST, $INCLUDELIST,$COLOR
284 // FIXME: gettextify?
285 $exclude_list = $exclude_list ? explode(':', $exclude_list) : array();
286 $include_list = $include_list ? explode(':', $include_list) : array();
287 $neighbour_list = $neighbour_list ? explode(':', $neighbour_list) : array();
289 // remove INCLUDED from EXCLUDED, includes override excludes.
290 if ($exclude_list and $include_list) {
291 $diff = array_diff($exclude_list, $include_list);
293 $exclude_list = $diff;
297 $allpages = $dbi->getAllPages(false, false, false, $exclude_list);
298 $pages = &$this->pages;
300 while ($page = $allpages->next()) {
301 $name = $page->getName();
303 // skip excluded pages
304 if (in_array($name, $exclude_list)) {
309 // false = get links from actual page
310 // true = get links to actual page ("backlinks")
311 $backlinks = $page->getLinks(true);
313 $bconnection = array();
314 while ($blink = $backlinks->next()) {
315 array_push($bconnection, $blink->getName());
320 // include all neighbours of pages listed in $NEIGHBOUR
321 if (in_array($name, $neighbour_list)) {
322 $ln = $page->getLinks(false);
324 while ($link = $ln->next()) {
325 array_push($con, $link->getName());
327 $include_list = array_merge($include_list, $bconnection, $con);
334 $rev = $page->getCurrentRevision();
336 $pages[$name] = array(
337 'age' => $now - $rev->get('mtime'),
338 'revnr' => $rev->getVersion(),
340 'backlink_nb' => count($bconnection),
341 'backlinks' => $bconnection,
342 'size' => 1000 // FIXME
344 $pages[$name]['revtime'] = $pages[$name]['age'] / ($pages[$name]['revnr']);
350 $this->names = array_keys($pages);
352 // now select each page matching to given parameters
353 $all_selected = array_unique(array_merge(
354 $this->findbest($recent_nb, 'age', true),
355 $this->findbest($refined_nb, 'revtime', true),
356 $x = $this->findbest($backlink_nb, 'backlink_nb', false),
357 // $this->findbest($large_nb, 'size', false),
360 foreach ($all_selected as $name)
361 if (isset($pages[$name]))
362 $newpages[$name] = $pages[$name];
365 $this->pages = $newpages;
366 $pages = &$this->pages;
367 $this->names = array_keys($pages);
369 unset($all_selected);
371 // remove dead links and collect links
373 while (list($name, $page) = each($pages)) {
374 if (is_array($page['backlinks'])) {
375 reset($page['backlinks']);
376 while (list($index, $link) = each($page['backlinks'])) {
377 if (!isset($pages[$link]) || $link == $name) {
378 unset($pages[$name]['backlinks'][$index]);
380 array_push($pages[$link]['links'], $name);
381 //array_push($this->everylink, array($link,$name));
387 if ($colorby == 'none')
389 list($oldestname) = $this->findbest(1, $colorby, false);
390 $this->oldest = $pages[$oldestname][$colorby];
391 foreach ($this->names as $name)
392 $pages[$name]['color'] = $this->getColor($pages[$name][$colorby] / $this->oldest);
396 * Creates the text file description of the graph needed to invoke
399 * @param string $filename
400 * @param bool $argarray
401 * @internal param string $filename name of the dot file to be created
402 * @internal param float $width width of the output graph in inches
403 * @internal param float $height height of the graph in inches
404 * @internal param string $colorby color sceme beeing used ('age', 'revtime',
406 * @internal param string $shape node shape; 'ellipse', 'box', 'circle', 'point'
407 * @internal param string $label 'name': label by name,
408 * 'number': label by unique number
409 * @return boolean error status; true=ok; false=error
411 function createDotFile($filename, $argarray)
414 if (!$fp = fopen($filename, 'w'))
417 $fillstring = ($fillnodes == 'on') ? 'style=filled,' : '';
419 $names = &$this->names;
420 $pages = &$this->pages;
422 $nametonumber = array_flip($names);
424 $dot = "digraph VisualWiki {\n" // }
425 . (!empty($fontpath) ? " fontpath=\"$fontpath\"\n" : "");
426 if ($width and $height)
427 $dot .= " size=\"$width,$height\";\n ";
431 $dot .= "edge [arrowhead=none];\nnode [shape=$shape,fontname=$fontname,width=0.15,height=0.15,fontsize=$fontsize];\n";
434 $dot .= "node [shape=$shape,fontname=$fontname,width=0.4,height=0.4,fontsize=$fontsize];\n";
437 $dot .= "node [shape=$shape,fontname=$fontname,width=0.25,height=0.25,fontsize=$fontsize];\n";
440 $dot .= "node [fontname=$fontname,shape=$shape,fontsize=$fontsize];\n";
443 foreach ($names as $name) {
445 $url = rawurlencode($name);
446 // patch to allow Page/SubPage
447 $url = str_replace(urlencode(SUBPAGE_SEPARATOR), SUBPAGE_SEPARATOR, $url);
448 $nodename = ($label != 'name' ? $nametonumber[$name] + 1 : $name);
450 $dot .= " \"$nodename\" [URL=\"$url\"";
451 if ($colorby != 'none') {
452 $col = $pages[$name]['color'];
453 $dot .= sprintf(',%scolor="#%02X%02X%02X"', $fillstring,
454 $col[0], $col[1], $col[2]);
458 if (!empty($pages[$name]['links'])) {
460 if ($label != 'name')
461 foreach ($pages[$name]['links'] as $linkname)
462 $linkarray[] = $nametonumber[$linkname] + 1;
464 $linkarray = $pages[$name]['links'];
465 $linkstring = join('"; "', $linkarray);
467 $c = count($pages[$name]['links']);
468 $dot .= " \"$nodename\" -> "
469 . ($c > 1 ? '{' : '')
471 . ($c > 1 ? '}' : '')
475 if ($colorby != 'none') {
476 $dot .= "\n subgraph cluster_legend {\n"
477 . " node[fontname=$fontname,shape=box,width=0.4,height=0.4,fontsize=$fontsize];\n"
478 . " fillcolor=lightgrey;\n"
480 . " fontname=$fontname;\n"
481 . " fontsize=$fontsize;\n"
482 . " label=\"" . gettext("Legend") . "\";\n";
483 $oldest = ceil($this->oldest / (24 * 3600));
486 for ($i = 0; $i < $max; $i++) {
487 $time = floor($i / $max * $oldest);
488 $name = '"' . $time . ' ' . _("days") . '"';
489 $col = $this->getColor($i / $max);
490 $dot .= sprintf(' %s [%scolor="#%02X%02X%02X"];',
491 $name, $fillstring, $col[0], $col[1], $col[2])
495 $dot .= ' ' . join(' -> ', $legend)
501 $this->source = $dot;
503 $ok = fwrite($fp, $dot);
504 $ok = fclose($fp) && $ok; // close anyway
510 * static workaround on broken Cache or broken dot executable,
511 * called only if debug=static.
516 * @param array $argarray
517 * @param request Request ???
518 * @internal param string $url url pointing to the image part of the map
519 * @internal param string $map <area> tags defining active
521 * @internal param \WikiDB $dbi database abstraction class
522 * @internal param array $argarray complete (!) arguments to produce
523 * image. It is not necessary to call
524 * WikiPlugin->getArgs anymore.
525 * @return string html output
527 function embedImg($url, &$dbi, $argarray, &$request)
529 if (!VISUALWIKI_ALLOWOPTIONS)
530 $argarray = $this->defaultarguments();
531 $this->checkArguments($argarray);
532 //extract($argarray);
533 if ($argarray['help'])
534 return array($this->helpImage(), ' '); // FIXME
535 $this->createColors();
536 $this->extract_wikipages($dbi, $argarray);
537 list($imagehandle, $content['html']) = $this->invokeDot($argarray);
538 // write to uploads and produce static url
539 $file_dir = getUploadFilePath();
540 $upload_dir = getUploadDataPath();
541 $tmpfile = tempnam($file_dir, "VisualWiki") . "." . $argarray['imgtype'];
542 WikiPluginCached::writeImage($argarray['imgtype'], $imagehandle, $tmpfile);
543 ImageDestroy($imagehandle);
544 return WikiPluginCached::embedMap(1, $upload_dir . basename($tmpfile), $content['html'],
545 $dbi, $argarray, $request);
549 * Prepares some rainbow colors for the nodes of the graph
550 * and stores them in an array which may be accessed with
551 * <code>getColor</code>.
553 function createColors()
555 $predefcolors = array(
556 array('red' => 255, 'green' => 0, 'blue' => 0),
557 array('red' => 255, 'green' => 255, 'blue' => 0),
558 array('red' => 0, 'green' => 255, 'blue' => 0),
559 array('red' => 0, 'green' => 255, 'blue' => 255),
560 array('red' => 0, 'green' => 0, 'blue' => 255),
561 array('red' => 100, 'green' => 100, 'blue' => 100)
565 $numberofcolors = count($predefcolors) * $steps;
568 foreach ($predefcolors as $color) {
574 for ($i = 0; $i < $steps; $i++)
575 $this->ColorTab[++$promille / $numberofcolors * 1000] = array(
576 floor(interpolate($oldcolor['red'], $color['red'], $i / $steps)),
577 floor(interpolate($oldcolor['green'], $color['green'], $i / $steps)),
578 floor(interpolate($oldcolor['blue'], $color['blue'], $i / $steps))
582 //echo"<pre>"; var_dump($this->ColorTab); echo "</pre>";
586 * Translates a value from 0.0 to 1.0 into rainbow color.
587 * red -> orange -> green -> blue -> gray
589 * @param float $promille
590 * @internal param float $promille value between 0.0 and 1.0
591 * @return array(red,green,blue)
593 function getColor($promille)
595 foreach ($this->ColorTab as $pro => $col) {
596 if ($promille * 1000 < $pro)
599 $lastcol = end($this->ColorTab);
605 * Linear interpolates a value between two point a and b
610 * @return float interpolated value
612 function interpolate($a, $b, $pos)
614 return $a + ($b - $a) * $pos;
621 // c-hanging-comment-ender-p: nil
622 // indent-tabs-mode: nil