]> CyberLeo.Net >> Repos - SourceForge/phpwiki.git/blob - lib/plugin/VisualWiki.php
getName should not translate
[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     function getDescription()
47     {
48         return _("Visualizes the Wiki structure in a graph using the 'dot' commandline tool from graphviz.");
49     }
50
51     function defaultArguments()
52     {
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'
57             'fillnodes' => 'off',
58             'label' => 'name',
59             'shape' => 'ellipse',
60             'large_nb' => 5,
61             'recent_nb' => 5,
62             'refined_nb' => 15,
63             'backlink_nb' => 5,
64             'neighbour_list' => '',
65             'exclude_list' => '',
66             'include_list' => '',
67             'fontsize' => 9,
68             'debug' => false,
69             'help' => false);
70     }
71
72     /**
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.
80      */
81     function getDefaultArguments()
82     {
83         if (VISUALWIKI_ALLOWOPTIONS)
84             return $this->defaultarguments();
85         else
86             return array();
87     }
88
89     /**
90      * Substitutes each forbidden parameter value by the default value
91      * defined in <code>defaultarguments</code>.
92      */
93     function checkArguments(&$arg)
94     {
95         extract($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'];
105
106         if (!in_array($shape, array('ellipse', 'box', 'point', 'circle',
107             'plaintext'))
108         )
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;
127     }
128
129     /**
130      * Checks options, creates help page if necessary, calls both
131      * database access and image map production functions.
132      * @param WikiDB $dbi
133      * @param array $argarray
134      * @param Request $request
135      * @return array($map,$html)
136      */
137     function getMap($dbi, $argarray, $request)
138     {
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);
153         else
154             return $result;
155         /* => ($width, $height, $color, $shape, $text); */
156     }
157
158     /**
159      * Returns an image containing a usage description of the plugin.
160      * @return string image handle
161      */
162     function helpImage()
163     {
164         $def = $this->defaultarguments();
165         $other_imgtypes = $GLOBALS['PLUGIN_CACHED_IMGTYPES'];
166         unset ($other_imgtypes[$def['imgtype']]);
167         $helparr = array(
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"     >>'
185         );
186         $length = 0;
187         foreach ($helparr as $alignright => $alignleft) {
188             $length = max($length, strlen($alignright));
189         }
190         $helptext = '';
191         foreach ($helparr as $alignright => $alignleft) {
192             $helptext .= substr('                                                        '
193                 . $alignright, -$length) . $alignleft . "\n";
194         }
195         return $this->text2img($helptext, 4, array(1, 0, 0),
196             array(255, 255, 255));
197     }
198
199     /**
200      * Selects the first (smallest or biggest) WikiPages in
201      * a given category.
202      *
203      * @param int $number
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
208      *                           to compare them
209      * @internal param bool $minimum true finds smallest, false finds biggest
210      * @return array list of page names found to be the best
211      */
212     function findbest($number, $category, $minimum)
213     {
214         // select the $number best in the category '$category'
215         $pages = &$this->pages;
216         $names = &$this->names;
217
218         $selected = array();
219         $i = 0;
220         foreach ($names as $name) {
221             if ($i++ >= $number)
222                 break;
223             $selected[$name] = $pages[$name][$category];
224         }
225         //echo "<pre>$category "; var_dump($selected); "</pre>";
226         $compareto = $minimum ? 0x79999999 : -0x79999999;
227
228         $i = 0;
229         foreach ($names as $name) {
230             if ($i++ < $number)
231                 continue;
232             if ($minimum) {
233                 if (($crit = $pages[$name][$category]) < $compareto) {
234                     $selected[$name] = $crit;
235                     asort($selected, SORT_NUMERIC);
236                     array_pop($selected);
237                     $compareto = end($selected);
238                 }
239             } elseif (($crit = $pages[$name][$category]) > $compareto) {
240                 $selected[$name] = $crit;
241                 arsort($selected, SORT_NUMERIC);
242                 array_pop($selected);
243                 $compareto = end($selected);
244             }
245         }
246         //echo "<pre>$category "; var_dump($selected); "</pre>";
247
248         return array_keys($selected);
249     }
250
251     /**
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.
256      *
257      * @param WikiDB $dbi
258      * @param $argarray
259      * @internal param \WikiDB $dbi database handle to access all Wiki pages
260      * @internal param int $LARGE number of largest pages which should
261      *                              be included
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
264      *                              interval
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
268      *                              example)
269      * @internal param string $INCLUDELIST colon separated list of pages which are
270      *                              always included (for example your own
271      *                              page :)
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.
275      * @return void
276      */
277     function extract_wikipages($dbi, $argarray)
278     {
279         // $LARGE, $RECENT, $REFINED, $BACKLINK, $NEIGHBOUR,
280         // $EXCLUDELIST, $INCLUDELIST,$COLOR
281         $now = time();
282
283         extract($argarray);
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();
288
289         // remove INCLUDED from EXCLUDED, includes override excludes.
290         if ($exclude_list and $include_list) {
291             $diff = array_diff($exclude_list, $include_list);
292             if ($diff)
293                 $exclude_list = $diff;
294         }
295
296         // collect all pages
297         $allpages = $dbi->getAllPages(false, false, false, $exclude_list);
298         $pages = &$this->pages;
299         $countpages = 0;
300         while ($page = $allpages->next()) {
301             $name = $page->getName();
302
303             // skip excluded pages
304             if (in_array($name, $exclude_list)) {
305                 $page->free();
306                 continue;
307             }
308
309             // false = get links from actual page
310             // true  = get links to actual page ("backlinks")
311             $backlinks = $page->getLinks(true);
312             unset($bconnection);
313             $bconnection = array();
314             while ($blink = $backlinks->next()) {
315                 array_push($bconnection, $blink->getName());
316             }
317             $backlinks->free();
318             unset($backlinks);
319
320             // include all neighbours of pages listed in $NEIGHBOUR
321             if (in_array($name, $neighbour_list)) {
322                 $ln = $page->getLinks(false);
323                 $con = array();
324                 while ($link = $ln->next()) {
325                     array_push($con, $link->getName());
326                 }
327                 $include_list = array_merge($include_list, $bconnection, $con);
328                 $ln->free();
329                 unset($l);
330                 unset($con);
331             }
332
333             unset($rev);
334             $rev = $page->getCurrentRevision();
335
336             $pages[$name] = array(
337                 'age' => $now - $rev->get('mtime'),
338                 'revnr' => $rev->getVersion(),
339                 'links' => array(),
340                 'backlink_nb' => count($bconnection),
341                 'backlinks' => $bconnection,
342                 'size' => 1000 // FIXME
343             );
344             $pages[$name]['revtime'] = $pages[$name]['age'] / ($pages[$name]['revnr']);
345
346             unset($page);
347         }
348         $allpages->free();
349         unset($allpages);
350         $this->names = array_keys($pages);
351
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),
358             $include_list));
359
360         foreach ($all_selected as $name)
361             if (isset($pages[$name]))
362                 $newpages[$name] = $pages[$name];
363         unset($this->names);
364         unset($this->pages);
365         $this->pages = $newpages;
366         $pages = &$this->pages;
367         $this->names = array_keys($pages);
368         unset($newpages);
369         unset($all_selected);
370
371         // remove dead links and collect links
372         reset($pages);
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]);
379                     } else {
380                         array_push($pages[$link]['links'], $name);
381                         //array_push($this->everylink, array($link,$name));
382                     }
383                 }
384             }
385         }
386
387         if ($colorby == 'none')
388             return;
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);
393     }
394
395     /**
396      * Creates the text file description of the graph needed to invoke
397      * <code>dot</code>.
398      *
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',
405      *                                                   'none')
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
410      */
411     function createDotFile($filename, $argarray)
412     {
413         extract($argarray);
414         if (!$fp = fopen($filename, 'w'))
415             return false;
416
417         $fillstring = ($fillnodes == 'on') ? 'style=filled,' : '';
418
419         $names = &$this->names;
420         $pages = &$this->pages;
421         if ($names)
422             $nametonumber = array_flip($names);
423
424         $dot = "digraph VisualWiki {\n" // }
425             . (!empty($fontpath) ? "    fontpath=\"$fontpath\"\n" : "");
426         if ($width and $height)
427             $dot .= "    size=\"$width,$height\";\n    ";
428
429         switch ($shape) {
430             case 'point':
431                 $dot .= "edge [arrowhead=none];\nnode [shape=$shape,fontname=$fontname,width=0.15,height=0.15,fontsize=$fontsize];\n";
432                 break;
433             case 'box':
434                 $dot .= "node [shape=$shape,fontname=$fontname,width=0.4,height=0.4,fontsize=$fontsize];\n";
435                 break;
436             case 'circle':
437                 $dot .= "node [shape=$shape,fontname=$fontname,width=0.25,height=0.25,fontsize=$fontsize];\n";
438                 break;
439             default :
440                 $dot .= "node [fontname=$fontname,shape=$shape,fontsize=$fontsize];\n";
441         }
442         $dot .= "\n";
443         foreach ($names as $name) {
444
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);
449
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]);
455             }
456             $dot .= "];\n";
457
458             if (!empty($pages[$name]['links'])) {
459                 unset($linkarray);
460                 if ($label != 'name')
461                     foreach ($pages[$name]['links'] as $linkname)
462                         $linkarray[] = $nametonumber[$linkname] + 1;
463                 else
464                     $linkarray = $pages[$name]['links'];
465                 $linkstring = join('"; "', $linkarray);
466
467                 $c = count($pages[$name]['links']);
468                 $dot .= "        \"$nodename\" -> "
469                     . ($c > 1 ? '{' : '')
470                     . "\"$linkstring\";"
471                     . ($c > 1 ? '}' : '')
472                     . "\n";
473             }
474         }
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"
479                 . "         style=filled;\n"
480                 . "         fontname=$fontname;\n"
481                 . "         fontsize=$fontsize;\n"
482                 . "         label=\"" . gettext("Legend") . "\";\n";
483             $oldest = ceil($this->oldest / (24 * 3600));
484             $max = 5;
485             $legend = array();
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])
492                     . "\n";
493                 $legend[] = $name;
494             }
495             $dot .= '        ' . join(' -> ', $legend)
496                 . ";\n    }\n";
497         }
498
499         // {
500         $dot .= "}\n";
501         $this->source = $dot;
502         // write a temp file
503         $ok = fwrite($fp, $dot);
504         $ok = fclose($fp) && $ok; // close anyway
505
506         return $ok;
507     }
508
509     /**
510      * static workaround on broken Cache or broken dot executable,
511      * called only if debug=static.
512      *
513      * @access private
514      * @param string $url
515      * @param WikiDB $dbi
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 &lt;area&gt; tags defining active
520      *                          regions in the map
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
526      */
527     function embedImg($url, &$dbi, $argarray, &$request)
528     {
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);
546     }
547
548     /**
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>.
552      */
553     function createColors()
554     {
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)
562         );
563
564         $steps = 2;
565         $numberofcolors = count($predefcolors) * $steps;
566
567         $promille = -1;
568         foreach ($predefcolors as $color) {
569             if ($promille < 0) {
570                 $oldcolor = $color;
571                 $promille = 0;
572                 continue;
573             }
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))
579                 );
580             $oldcolor = $color;
581         }
582 //echo"<pre>";  var_dump($this->ColorTab); echo "</pre>";
583     }
584
585     /**
586      * Translates a value from 0.0 to 1.0 into rainbow color.
587      * red -&gt; orange -&gt; green -&gt; blue -&gt; gray
588      *
589      * @param float $promille
590      * @internal param float $promille value between 0.0 and 1.0
591      * @return array(red,green,blue)
592      */
593     function getColor($promille)
594     {
595         foreach ($this->ColorTab as $pro => $col) {
596             if ($promille * 1000 < $pro)
597                 return $col;
598         }
599         $lastcol = end($this->ColorTab);
600         return $lastcol;
601     }
602 }
603
604 /**
605  * Linear interpolates a value between two point a and b
606  * at a value pos.
607  * @param $a
608  * @param $b
609  * @param $pos
610  * @return float  interpolated value
611  */
612 function interpolate($a, $b, $pos)
613 {
614     return $a + ($b - $a) * $pos;
615 }
616
617 // Local Variables:
618 // mode: php
619 // tab-width: 8
620 // c-basic-offset: 4
621 // c-hanging-comment-ender-p: nil
622 // indent-tabs-mode: nil
623 // End: