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