]> CyberLeo.Net >> Repos - SourceForge/phpwiki.git/blob - lib/plugin/VisualWiki.php
Fix Phpdoc
[SourceForge/phpwiki.git] / lib / plugin / VisualWiki.php
1 <?php
2
3 /*
4  * Copyright (C) 2002 Johannes Große (Johannes Gro&szlig;e)
5  *
6  * This file is 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 along
19  * with PhpWiki; if not, write to the Free Software Foundation, Inc.,
20  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 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.9
30  */
31 /* define('VISUALWIKI_ALLOWOPTIONS', true); */
32 if (!defined('VISUALWIKI_ALLOWOPTIONS'))
33     define('VISUALWIKI_ALLOWOPTIONS', false);
34
35 require_once 'lib/plugin/GraphViz.php';
36
37 class WikiPlugin_VisualWiki
38     extends WikiPlugin_GraphViz
39 {
40     /**
41      * Sets plugin type to map production
42      */
43     function getPluginType()
44     {
45         return ($GLOBALS['request']->getArg('debug')) ? PLUGIN_CACHED_IMG_ONDEMAND
46             : PLUGIN_CACHED_MAP;
47     }
48
49     /**
50      * Sets the plugin's name to VisualWiki. It can be called by
51      * <code>&lt;?plugin VisualWiki?&gt;</code>, now. This
52      * name must correspond to the filename and the class name.
53      */
54     function getName()
55     {
56         return "VisualWiki";
57     }
58
59     /**
60      * Sets textual description.
61      */
62     function getDescription()
63     {
64         return _("Visualizes the Wiki structure in a graph using the 'dot' commandline tool from graphviz.");
65     }
66
67     /**
68      * Returns default arguments. This is put into a separate
69      * function to allow its usage by both <code>getDefaultArguments</code>
70      * and <code>checkArguments</code>
71      */
72     function defaultarguments()
73     {
74         return array('imgtype' => 'png',
75             'width' => false, // was 5, scale it automatically
76             'height' => false, // was 7, scale it automatically
77             'colorby' => 'age', // sort by 'age' or 'revtime'
78             'fillnodes' => 'off',
79             'label' => 'name',
80             'shape' => 'ellipse',
81             'large_nb' => 5,
82             'recent_nb' => 5,
83             'refined_nb' => 15,
84             'backlink_nb' => 5,
85             'neighbour_list' => '',
86             'exclude_list' => '',
87             'include_list' => '',
88             'fontsize' => 9,
89             'debug' => false,
90             'help' => false);
91     }
92
93     /**
94      * Sets the default arguments. WikiPlugin also regards these as
95      * the allowed arguments. Since WikiPluginCached stores an image
96      * for each different set of parameters, there can be a lot of
97      * these (large) graphs if you allow different parameters.
98      * Set <code>VISUALWIKI_ALLOWOPTIONS</code> to <code>false</code>
99      * to allow no options to be set and use only the default parameters.
100      * This will need an disk space of about 20 Kbyte all the time.
101      */
102     function getDefaultArguments()
103     {
104         if (VISUALWIKI_ALLOWOPTIONS)
105             return $this->defaultarguments();
106         else
107             return array();
108     }
109
110     /**
111      * Substitutes each forbidden parameter value by the default value
112      * defined in <code>defaultarguments</code>.
113      */
114     function checkArguments(&$arg)
115     {
116         extract($arg);
117         $def = $this->defaultarguments();
118         if (($width < 3) || ($width > 15))
119             $arg['width'] = $def['width'];
120         if (($height < 3) || ($height > 20))
121             $arg['height'] = $def['height'];
122         if (($fontsize < 8) || ($fontsize > 24))
123             $arg['fontsize'] = $def['fontsize'];
124         if (!in_array($label, array('name', 'number')))
125             $arg['label'] = $def['label'];
126
127         if (!in_array($shape, array('ellipse', 'box', 'point', 'circle',
128             'plaintext'))
129         )
130             $arg['shape'] = $def['shape'];
131         if (!in_array($colorby, array('age', 'revtime')))
132             $arg['colorby'] = $def['colorby'];
133         if (!in_array($fillnodes, array('on', 'off')))
134             $arg['fillnodes'] = $def['fillnodes'];
135         if (($large_nb < 0) || ($large_nb > 50))
136             $arg['large_nb'] = $def['large_nb'];
137         if (($recent_nb < 0) || ($recent_nb > 50))
138             $arg['recent_nb'] = $def['recent_nb'];
139         if (($refined_nb < 0) || ($refined_nb > 50))
140             $arg['refined_nb'] = $def['refined_nb'];
141         if (($backlink_nb < 0) || ($backlink_nb > 50))
142             $arg['backlink_nb'] = $def['backlink_nb'];
143         // ToDo: check if "ImageCreateFrom$imgtype"() exists.
144         if (!in_array($imgtype, $GLOBALS['PLUGIN_CACHED_IMGTYPES']))
145             $arg['imgtype'] = $def['imgtype'];
146         if (empty($fontname))
147             $arg['fontname'] = VISUALWIKIFONT;
148     }
149
150     /**
151      * Checks options, creates help page if necessary, calls both
152      * database access and image map production functions.
153      * @param WikiDB $dbi
154      * @param array $argarray
155      * @param Request $request
156      * @return array($map,$html)
157      */
158     function getMap($dbi, $argarray, $request)
159     {
160         if (!VISUALWIKI_ALLOWOPTIONS)
161             $argarray = $this->defaultarguments();
162         $this->checkArguments($argarray);
163         $request->setArg('debug', $argarray['debug']);
164         //extract($argarray);
165         if ($argarray['help'])
166             return array($this->helpImage(), ' '); // FIXME
167         $this->createColors();
168         $this->extract_wikipages($dbi, $argarray);
169         /* ($dbi,  $large, $recent, $refined, $backlink,
170             $neighbour, $excludelist, $includelist, $color); */
171         $result = $this->invokeDot($argarray);
172         if (isa($result, 'HtmlElement'))
173             return array(false, $result);
174         else
175             return $result;
176         /* => ($width, $height, $color, $shape, $text); */
177     }
178
179     // ------------------------------------------------------------------------------------------
180
181     /**
182      * Returns an image containing a usage description of the plugin.
183      * @return string image handle
184      */
185     function helpImage()
186     {
187         $def = $this->defaultarguments();
188         $other_imgtypes = $GLOBALS['PLUGIN_CACHED_IMGTYPES'];
189         unset ($other_imgtypes[$def['imgtype']]);
190         $helparr = array(
191             '<<' . $this->getName() .
192                 ' img' => ' = "' . $def['imgtype'] . "(default)|" . join('|', $GLOBALS['PLUGIN_CACHED_IMGTYPES']) . '"',
193             'width' => ' = "width in inches"',
194             'height' => ' = "height in inches"',
195             'fontname' => ' = "font family"',
196             'fontsize' => ' = "fontsize in points"',
197             'colorby' => ' = "age|revtime|none"',
198             'fillnodes' => ' = "on|off"',
199             'shape' => ' = "ellipse(default)|box|circle|point"',
200             'label' => ' = "name|number"',
201             'large_nb' => ' = "number of largest pages to be selected"',
202             'recent_nb' => ' = "number of youngest pages"',
203             'refined_nb' => ' = "#pages with smallest time between revisions"',
204             'backlink_nb' => ' = "number of pages with most backlinks"',
205             'neighbour_list' => ' = "find pages linked from and to these pages"',
206             'exclude_list' => ' = "colon separated list of pages to be excluded"',
207             'include_list' => ' = "colon separated list"     >>'
208         );
209         $length = 0;
210         foreach ($helparr as $alignright => $alignleft) {
211             $length = max($length, strlen($alignright));
212         }
213         $helptext = '';
214         foreach ($helparr as $alignright => $alignleft) {
215             $helptext .= substr('                                                        '
216                 . $alignright, -$length) . $alignleft . "\n";
217         }
218         return $this->text2img($helptext, 4, array(1, 0, 0),
219             array(255, 255, 255));
220     }
221
222
223     /**
224      * Selects the first (smallest or biggest) WikiPages in
225      * a given category.
226      *
227      * @param int $number
228      * @param string $category
229      * @param bool $minimum
230      * @internal param int $number number of page names to be found
231      * @internal param string $category attribute of the pages which is used
232      *                           to compare them
233      * @internal param bool $minimum true finds smallest, false finds biggest
234      * @return array list of page names found to be the best
235      */
236     function findbest($number, $category, $minimum)
237     {
238         // select the $number best in the category '$category'
239         $pages = &$this->pages;
240         $names = &$this->names;
241
242         $selected = array();
243         $i = 0;
244         foreach ($names as $name) {
245             if ($i++ >= $number)
246                 break;
247             $selected[$name] = $pages[$name][$category];
248         }
249         //echo "<pre>$category "; var_dump($selected); "</pre>";
250         $compareto = $minimum ? 0x79999999 : -0x79999999;
251
252         $i = 0;
253         foreach ($names as $name) {
254             if ($i++ < $number)
255                 continue;
256             if ($minimum) {
257                 if (($crit = $pages[$name][$category]) < $compareto) {
258                     $selected[$name] = $crit;
259                     asort($selected, SORT_NUMERIC);
260                     array_pop($selected);
261                     $compareto = end($selected);
262                 }
263             } elseif (($crit = $pages[$name][$category]) > $compareto) {
264                 $selected[$name] = $crit;
265                 arsort($selected, SORT_NUMERIC);
266                 array_pop($selected);
267                 $compareto = end($selected);
268             }
269         }
270         //echo "<pre>$category "; var_dump($selected); "</pre>";
271
272         return array_keys($selected);
273     }
274
275
276     /**
277      * Extracts a subset of all pages from the wiki and find their
278      * connections to other pages. Also collects some page features
279      * like size, age, revision number which are used to find the
280      * most attractive pages.
281      *
282      * @param WikiDB $dbi
283      * @param $argarray
284      * @internal param \WikiDB $dbi database handle to access all Wiki pages
285      * @internal param int $LARGE number of largest pages which should
286      *                              be included
287      * @internal param int $RECENT number of the youngest pages to be included
288      * @internal param int $REFINED number of the pages with shortes revision
289      *                              interval
290      * @internal param int $BACKLINK number of the pages with most backlinks
291      * @internal param string $EXCLUDELIST colon ':' separated list of page names which
292      *                              should not be displayed (like PhpWiki, for
293      *                              example)
294      * @internal param string $INCLUDELIST colon separated list of pages which are
295      *                              always included (for example your own
296      *                              page :)
297      * @internal param string $COLOR 'age', 'revtime' or 'none'; Selects which
298      *                              page feature is used to determine the
299      *                              filling color of the nodes in the graph.
300      * @return void
301      */
302     function extract_wikipages($dbi, $argarray)
303     {
304         // $LARGE, $RECENT, $REFINED, $BACKLINK, $NEIGHBOUR,
305         // $EXCLUDELIST, $INCLUDELIST,$COLOR
306         $now = time();
307
308         extract($argarray);
309         // FIXME: gettextify?
310         $exclude_list = $exclude_list ? explode(':', $exclude_list) : array();
311         $include_list = $include_list ? explode(':', $include_list) : array();
312         $neighbour_list = $neighbour_list ? explode(':', $neighbour_list) : array();
313
314         // remove INCLUDED from EXCLUDED, includes override excludes.
315         if ($exclude_list and $include_list) {
316             $diff = array_diff($exclude_list, $include_list);
317             if ($diff)
318                 $exclude_list = $diff;
319         }
320
321         // collect all pages
322         $allpages = $dbi->getAllPages(false, false, false, $exclude_list);
323         $pages = &$this->pages;
324         $countpages = 0;
325         while ($page = $allpages->next()) {
326             $name = $page->getName();
327
328             // skip excluded pages
329             if (in_array($name, $exclude_list)) {
330                 $page->free();
331                 continue;
332             }
333
334             // false = get links from actual page
335             // true  = get links to actual page ("backlinks")
336             $backlinks = $page->getLinks(true);
337             unset($bconnection);
338             $bconnection = array();
339             while ($blink = $backlinks->next()) {
340                 array_push($bconnection, $blink->getName());
341             }
342             $backlinks->free();
343             unset($backlinks);
344
345             // include all neighbours of pages listed in $NEIGHBOUR
346             if (in_array($name, $neighbour_list)) {
347                 $ln = $page->getLinks(false);
348                 $con = array();
349                 while ($link = $ln->next()) {
350                     array_push($con, $link->getName());
351                 }
352                 $include_list = array_merge($include_list, $bconnection, $con);
353                 $ln->free();
354                 unset($l);
355                 unset($con);
356             }
357
358             unset($rev);
359             $rev = $page->getCurrentRevision();
360
361             $pages[$name] = array(
362                 'age' => $now - $rev->get('mtime'),
363                 'revnr' => $rev->getVersion(),
364                 'links' => array(),
365                 'backlink_nb' => count($bconnection),
366                 'backlinks' => $bconnection,
367                 'size' => 1000 // FIXME
368             );
369             $pages[$name]['revtime'] = $pages[$name]['age'] / ($pages[$name]['revnr']);
370
371             unset($page);
372         }
373         $allpages->free();
374         unset($allpages);
375         $this->names = array_keys($pages);
376
377         $countpages = count($pages);
378
379         // now select each page matching to given parameters
380         $all_selected = array_unique(array_merge(
381             $this->findbest($recent_nb, 'age', true),
382             $this->findbest($refined_nb, 'revtime', true),
383             $x = $this->findbest($backlink_nb, 'backlink_nb', false),
384 //          $this->findbest($large_nb,    'size',        false),
385             $include_list));
386
387         foreach ($all_selected as $name)
388             if (isset($pages[$name]))
389                 $newpages[$name] = $pages[$name];
390         unset($this->names);
391         unset($this->pages);
392         $this->pages = $newpages;
393         $pages = &$this->pages;
394         $this->names = array_keys($pages);
395         unset($newpages);
396         unset($all_selected);
397
398         $countpages = count($pages);
399
400         // remove dead links and collect links
401         reset($pages);
402         while (list($name, $page) = each($pages)) {
403             if (is_array($page['backlinks'])) {
404                 reset($page['backlinks']);
405                 while (list($index, $link) = each($page['backlinks'])) {
406                     if (!isset($pages[$link]) || $link == $name) {
407                         unset($pages[$name]['backlinks'][$index]);
408                     } else {
409                         array_push($pages[$link]['links'], $name);
410                         //array_push($this->everylink, array($link,$name));
411                     }
412                 }
413             }
414         }
415
416         if ($colorby == 'none')
417             return;
418         list($oldestname) = $this->findbest(1, $colorby, false);
419         $this->oldest = $pages[$oldestname][$colorby];
420         foreach ($this->names as $name)
421             $pages[$name]['color'] = $this->getColor($pages[$name][$colorby] / $this->oldest);
422     }
423
424     /**
425      * Creates the text file description of the graph needed to invoke
426      * <code>dot</code>.
427      *
428      * @param string $filename
429      * @param bool $argarray
430      * @internal param string $filename name of the dot file to be created
431      * @internal param float $width width of the output graph in inches
432      * @internal param float $height height of the graph in inches
433      * @internal param string $colorby color sceme beeing used ('age', 'revtime',
434      *                                                   'none')
435      * @internal param string $shape node shape; 'ellipse', 'box', 'circle', 'point'
436      * @internal param string $label 'name': label by name,
437      *                          'number': label by unique number
438      * @return boolean error status; true=ok; false=error
439      */
440     function createDotFile($filename, $argarray)
441     {
442         extract($argarray);
443         if (!$fp = fopen($filename, 'w'))
444             return false;
445
446         $fillstring = ($fillnodes == 'on') ? 'style=filled,' : '';
447
448         $ok = true;
449         $names = &$this->names;
450         $pages = &$this->pages;
451         if ($names)
452             $nametonumber = array_flip($names);
453
454         $dot = "digraph VisualWiki {\n" // }
455             . (!empty($fontpath) ? "    fontpath=\"$fontpath\"\n" : "");
456         if ($width and $height)
457             $dot .= "    size=\"$width,$height\";\n    ";
458
459
460         switch ($shape) {
461             case 'point':
462                 $dot .= "edge [arrowhead=none];\nnode [shape=$shape,fontname=$fontname,width=0.15,height=0.15,fontsize=$fontsize];\n";
463                 break;
464             case 'box':
465                 $dot .= "node [shape=$shape,fontname=$fontname,width=0.4,height=0.4,fontsize=$fontsize];\n";
466                 break;
467             case 'circle':
468                 $dot .= "node [shape=$shape,fontname=$fontname,width=0.25,height=0.25,fontsize=$fontsize];\n";
469                 break;
470             default :
471                 $dot .= "node [fontname=$fontname,shape=$shape,fontsize=$fontsize];\n";
472         }
473         $dot .= "\n";
474         $i = 0;
475         foreach ($names as $name) {
476
477             $url = rawurlencode($name);
478             // patch to allow Page/SubPage
479             $url = str_replace(urlencode(SUBPAGE_SEPARATOR), SUBPAGE_SEPARATOR, $url);
480             $nodename = ($label != 'name' ? $nametonumber[$name] + 1 : $name);
481
482             $dot .= "    \"$nodename\" [URL=\"$url\"";
483             if ($colorby != 'none') {
484                 $col = $pages[$name]['color'];
485                 $dot .= sprintf(',%scolor="#%02X%02X%02X"', $fillstring,
486                     $col[0], $col[1], $col[2]);
487             }
488             $dot .= "];\n";
489
490             if (!empty($pages[$name]['links'])) {
491                 unset($linkarray);
492                 if ($label != 'name')
493                     foreach ($pages[$name]['links'] as $linkname)
494                         $linkarray[] = $nametonumber[$linkname] + 1;
495                 else
496                     $linkarray = $pages[$name]['links'];
497                 $linkstring = join('"; "', $linkarray);
498
499                 $c = count($pages[$name]['links']);
500                 $dot .= "        \"$nodename\" -> "
501                     . ($c > 1 ? '{' : '')
502                     . "\"$linkstring\";"
503                     . ($c > 1 ? '}' : '')
504                     . "\n";
505             }
506         }
507         if ($colorby != 'none') {
508             $dot .= "\n    subgraph cluster_legend {\n"
509                 . "         node[fontname=$fontname,shape=box,width=0.4,height=0.4,fontsize=$fontsize];\n"
510                 . "         fillcolor=lightgrey;\n"
511                 . "         style=filled;\n"
512                 . "         fontname=$fontname;\n"
513                 . "         fontsize=$fontsize;\n"
514                 . "         label=\"" . gettext("Legend") . "\";\n";
515             $oldest = ceil($this->oldest / (24 * 3600));
516             $max = 5;
517             $legend = array();
518             for ($i = 0; $i < $max; $i++) {
519                 $time = floor($i / $max * $oldest);
520                 $name = '"' . $time . ' ' . _("days") . '"';
521                 $col = $this->getColor($i / $max);
522                 $dot .= sprintf('       %s [%scolor="#%02X%02X%02X"];',
523                     $name, $fillstring, $col[0], $col[1], $col[2])
524                     . "\n";
525                 $legend[] = $name;
526             }
527             $dot .= '        ' . join(' -> ', $legend)
528                 . ";\n    }\n";
529         }
530
531         // {
532         $dot .= "}\n";
533         $this->source = $dot;
534         // write a temp file
535         $ok = fwrite($fp, $dot);
536         $ok = fclose($fp) && $ok; // close anyway
537
538         return $ok;
539     }
540
541
542     /**
543      * static workaround on broken Cache or broken dot executable,
544      * called only if debug=static.
545      *
546      * @access private
547      * @param string $url
548      * @param WikiDB $dbi
549      * @param array $argarray
550      * @param  request  Request ???
551      * @internal param string $url url pointing to the image part of the map
552      * @internal param string $map &lt;area&gt; tags defining active
553      *                          regions in the map
554      * @internal param \WikiDB $dbi database abstraction class
555      * @internal param array $argarray complete (!) arguments to produce
556      *                          image. It is not necessary to call
557      *                          WikiPlugin->getArgs anymore.
558      * @return string html output
559      */
560     function embedImg($url, &$dbi, $argarray, &$request)
561     {
562         if (!VISUALWIKI_ALLOWOPTIONS)
563             $argarray = $this->defaultarguments();
564         $this->checkArguments($argarray);
565         //extract($argarray);
566         if ($argarray['help'])
567             return array($this->helpImage(), ' '); // FIXME
568         $this->createColors();
569         $this->extract_wikipages($dbi, $argarray);
570         list($imagehandle, $content['html']) = $this->invokeDot($argarray);
571         // write to uploads and produce static url
572         $file_dir = getUploadFilePath();
573         $upload_dir = getUploadDataPath();
574         $tmpfile = tempnam($file_dir, "VisualWiki") . "." . $argarray['imgtype'];
575         WikiPluginCached::writeImage($argarray['imgtype'], $imagehandle, $tmpfile);
576         ImageDestroy($imagehandle);
577         return WikiPluginCached::embedMap(1, $upload_dir . basename($tmpfile), $content['html'],
578             $dbi, $argarray, $request);
579     }
580
581     /**
582      * Prepares some rainbow colors for the nodes of the graph
583      * and stores them in an array which may be accessed with
584      * <code>getColor</code>.
585      */
586     function createColors()
587     {
588         $predefcolors = array(
589             array('red' => 255, 'green' => 0, 'blue' => 0),
590             array('red' => 255, 'green' => 255, 'blue' => 0),
591             array('red' => 0, 'green' => 255, 'blue' => 0),
592             array('red' => 0, 'green' => 255, 'blue' => 255),
593             array('red' => 0, 'green' => 0, 'blue' => 255),
594             array('red' => 100, 'green' => 100, 'blue' => 100)
595         );
596
597         $steps = 2;
598         $numberofcolors = count($predefcolors) * $steps;
599
600         $promille = -1;
601         foreach ($predefcolors as $color) {
602             if ($promille < 0) {
603                 $oldcolor = $color;
604                 $promille = 0;
605                 continue;
606             }
607             for ($i = 0; $i < $steps; $i++)
608                 $this->ColorTab[++$promille / $numberofcolors * 1000] = array(
609                     floor(interpolate($oldcolor['red'], $color['red'], $i / $steps)),
610                     floor(interpolate($oldcolor['green'], $color['green'], $i / $steps)),
611                     floor(interpolate($oldcolor['blue'], $color['blue'], $i / $steps))
612                 );
613             $oldcolor = $color;
614         }
615 //echo"<pre>";  var_dump($this->ColorTab); echo "</pre>";
616     }
617
618     /**
619      * Translates a value from 0.0 to 1.0 into rainbow color.
620      * red -&gt; orange -&gt; green -&gt; blue -&gt; gray
621      *
622      * @param float $promille
623      * @internal param float $promille value between 0.0 and 1.0
624      * @return array(red,green,blue)
625      */
626     function getColor($promille)
627     {
628         foreach ($this->ColorTab as $pro => $col) {
629             if ($promille * 1000 < $pro)
630                 return $col;
631         }
632         $lastcol = end($this->ColorTab);
633         return $lastcol;
634     }
635 }
636
637 /**
638  * Linear interpolates a value between two point a and b
639  * at a value pos.
640  * @param $a
641  * @param $b
642  * @param $pos
643  * @return float  interpolated value
644  */
645 function interpolate($a, $b, $pos)
646 {
647     return $a + ($b - $a) * $pos;
648 }
649
650 // Local Variables:
651 // mode: php
652 // tab-width: 8
653 // c-basic-offset: 4
654 // c-hanging-comment-ender-p: nil
655 // indent-tabs-mode: nil
656 // End: