]> CyberLeo.Net >> Repos - SourceForge/phpwiki.git/blob - lib/plugin/VisualWiki.php
Remove unused variables
[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         // now select each page matching to given parameters
371         $all_selected = array_unique(array_merge(
372             $this->findbest($recent_nb, 'age', true),
373             $this->findbest($refined_nb, 'revtime', true),
374             $x = $this->findbest($backlink_nb, 'backlink_nb', false),
375 //          $this->findbest($large_nb,    'size',        false),
376             $include_list));
377
378         foreach ($all_selected as $name)
379             if (isset($pages[$name]))
380                 $newpages[$name] = $pages[$name];
381         unset($this->names);
382         unset($this->pages);
383         $this->pages = $newpages;
384         $pages = &$this->pages;
385         $this->names = array_keys($pages);
386         unset($newpages);
387         unset($all_selected);
388
389         // remove dead links and collect links
390         reset($pages);
391         while (list($name, $page) = each($pages)) {
392             if (is_array($page['backlinks'])) {
393                 reset($page['backlinks']);
394                 while (list($index, $link) = each($page['backlinks'])) {
395                     if (!isset($pages[$link]) || $link == $name) {
396                         unset($pages[$name]['backlinks'][$index]);
397                     } else {
398                         array_push($pages[$link]['links'], $name);
399                         //array_push($this->everylink, array($link,$name));
400                     }
401                 }
402             }
403         }
404
405         if ($colorby == 'none')
406             return;
407         list($oldestname) = $this->findbest(1, $colorby, false);
408         $this->oldest = $pages[$oldestname][$colorby];
409         foreach ($this->names as $name)
410             $pages[$name]['color'] = $this->getColor($pages[$name][$colorby] / $this->oldest);
411     }
412
413     /**
414      * Creates the text file description of the graph needed to invoke
415      * <code>dot</code>.
416      *
417      * @param string $filename
418      * @param bool $argarray
419      * @internal param string $filename name of the dot file to be created
420      * @internal param float $width width of the output graph in inches
421      * @internal param float $height height of the graph in inches
422      * @internal param string $colorby color sceme beeing used ('age', 'revtime',
423      *                                                   'none')
424      * @internal param string $shape node shape; 'ellipse', 'box', 'circle', 'point'
425      * @internal param string $label 'name': label by name,
426      *                          'number': label by unique number
427      * @return boolean error status; true=ok; false=error
428      */
429     function createDotFile($filename, $argarray)
430     {
431         extract($argarray);
432         if (!$fp = fopen($filename, 'w'))
433             return false;
434
435         $fillstring = ($fillnodes == 'on') ? 'style=filled,' : '';
436
437         $names = &$this->names;
438         $pages = &$this->pages;
439         if ($names)
440             $nametonumber = array_flip($names);
441
442         $dot = "digraph VisualWiki {\n" // }
443             . (!empty($fontpath) ? "    fontpath=\"$fontpath\"\n" : "");
444         if ($width and $height)
445             $dot .= "    size=\"$width,$height\";\n    ";
446
447         switch ($shape) {
448             case 'point':
449                 $dot .= "edge [arrowhead=none];\nnode [shape=$shape,fontname=$fontname,width=0.15,height=0.15,fontsize=$fontsize];\n";
450                 break;
451             case 'box':
452                 $dot .= "node [shape=$shape,fontname=$fontname,width=0.4,height=0.4,fontsize=$fontsize];\n";
453                 break;
454             case 'circle':
455                 $dot .= "node [shape=$shape,fontname=$fontname,width=0.25,height=0.25,fontsize=$fontsize];\n";
456                 break;
457             default :
458                 $dot .= "node [fontname=$fontname,shape=$shape,fontsize=$fontsize];\n";
459         }
460         $dot .= "\n";
461         foreach ($names as $name) {
462
463             $url = rawurlencode($name);
464             // patch to allow Page/SubPage
465             $url = str_replace(urlencode(SUBPAGE_SEPARATOR), SUBPAGE_SEPARATOR, $url);
466             $nodename = ($label != 'name' ? $nametonumber[$name] + 1 : $name);
467
468             $dot .= "    \"$nodename\" [URL=\"$url\"";
469             if ($colorby != 'none') {
470                 $col = $pages[$name]['color'];
471                 $dot .= sprintf(',%scolor="#%02X%02X%02X"', $fillstring,
472                     $col[0], $col[1], $col[2]);
473             }
474             $dot .= "];\n";
475
476             if (!empty($pages[$name]['links'])) {
477                 unset($linkarray);
478                 if ($label != 'name')
479                     foreach ($pages[$name]['links'] as $linkname)
480                         $linkarray[] = $nametonumber[$linkname] + 1;
481                 else
482                     $linkarray = $pages[$name]['links'];
483                 $linkstring = join('"; "', $linkarray);
484
485                 $c = count($pages[$name]['links']);
486                 $dot .= "        \"$nodename\" -> "
487                     . ($c > 1 ? '{' : '')
488                     . "\"$linkstring\";"
489                     . ($c > 1 ? '}' : '')
490                     . "\n";
491             }
492         }
493         if ($colorby != 'none') {
494             $dot .= "\n    subgraph cluster_legend {\n"
495                 . "         node[fontname=$fontname,shape=box,width=0.4,height=0.4,fontsize=$fontsize];\n"
496                 . "         fillcolor=lightgrey;\n"
497                 . "         style=filled;\n"
498                 . "         fontname=$fontname;\n"
499                 . "         fontsize=$fontsize;\n"
500                 . "         label=\"" . gettext("Legend") . "\";\n";
501             $oldest = ceil($this->oldest / (24 * 3600));
502             $max = 5;
503             $legend = array();
504             for ($i = 0; $i < $max; $i++) {
505                 $time = floor($i / $max * $oldest);
506                 $name = '"' . $time . ' ' . _("days") . '"';
507                 $col = $this->getColor($i / $max);
508                 $dot .= sprintf('       %s [%scolor="#%02X%02X%02X"];',
509                     $name, $fillstring, $col[0], $col[1], $col[2])
510                     . "\n";
511                 $legend[] = $name;
512             }
513             $dot .= '        ' . join(' -> ', $legend)
514                 . ";\n    }\n";
515         }
516
517         // {
518         $dot .= "}\n";
519         $this->source = $dot;
520         // write a temp file
521         $ok = fwrite($fp, $dot);
522         $ok = fclose($fp) && $ok; // close anyway
523
524         return $ok;
525     }
526
527     /**
528      * static workaround on broken Cache or broken dot executable,
529      * called only if debug=static.
530      *
531      * @access private
532      * @param string $url
533      * @param WikiDB $dbi
534      * @param array $argarray
535      * @param  request  Request ???
536      * @internal param string $url url pointing to the image part of the map
537      * @internal param string $map &lt;area&gt; tags defining active
538      *                          regions in the map
539      * @internal param \WikiDB $dbi database abstraction class
540      * @internal param array $argarray complete (!) arguments to produce
541      *                          image. It is not necessary to call
542      *                          WikiPlugin->getArgs anymore.
543      * @return string html output
544      */
545     function embedImg($url, &$dbi, $argarray, &$request)
546     {
547         if (!VISUALWIKI_ALLOWOPTIONS)
548             $argarray = $this->defaultarguments();
549         $this->checkArguments($argarray);
550         //extract($argarray);
551         if ($argarray['help'])
552             return array($this->helpImage(), ' '); // FIXME
553         $this->createColors();
554         $this->extract_wikipages($dbi, $argarray);
555         list($imagehandle, $content['html']) = $this->invokeDot($argarray);
556         // write to uploads and produce static url
557         $file_dir = getUploadFilePath();
558         $upload_dir = getUploadDataPath();
559         $tmpfile = tempnam($file_dir, "VisualWiki") . "." . $argarray['imgtype'];
560         WikiPluginCached::writeImage($argarray['imgtype'], $imagehandle, $tmpfile);
561         ImageDestroy($imagehandle);
562         return WikiPluginCached::embedMap(1, $upload_dir . basename($tmpfile), $content['html'],
563             $dbi, $argarray, $request);
564     }
565
566     /**
567      * Prepares some rainbow colors for the nodes of the graph
568      * and stores them in an array which may be accessed with
569      * <code>getColor</code>.
570      */
571     function createColors()
572     {
573         $predefcolors = array(
574             array('red' => 255, 'green' => 0, 'blue' => 0),
575             array('red' => 255, 'green' => 255, 'blue' => 0),
576             array('red' => 0, 'green' => 255, 'blue' => 0),
577             array('red' => 0, 'green' => 255, 'blue' => 255),
578             array('red' => 0, 'green' => 0, 'blue' => 255),
579             array('red' => 100, 'green' => 100, 'blue' => 100)
580         );
581
582         $steps = 2;
583         $numberofcolors = count($predefcolors) * $steps;
584
585         $promille = -1;
586         foreach ($predefcolors as $color) {
587             if ($promille < 0) {
588                 $oldcolor = $color;
589                 $promille = 0;
590                 continue;
591             }
592             for ($i = 0; $i < $steps; $i++)
593                 $this->ColorTab[++$promille / $numberofcolors * 1000] = array(
594                     floor(interpolate($oldcolor['red'], $color['red'], $i / $steps)),
595                     floor(interpolate($oldcolor['green'], $color['green'], $i / $steps)),
596                     floor(interpolate($oldcolor['blue'], $color['blue'], $i / $steps))
597                 );
598             $oldcolor = $color;
599         }
600 //echo"<pre>";  var_dump($this->ColorTab); echo "</pre>";
601     }
602
603     /**
604      * Translates a value from 0.0 to 1.0 into rainbow color.
605      * red -&gt; orange -&gt; green -&gt; blue -&gt; gray
606      *
607      * @param float $promille
608      * @internal param float $promille value between 0.0 and 1.0
609      * @return array(red,green,blue)
610      */
611     function getColor($promille)
612     {
613         foreach ($this->ColorTab as $pro => $col) {
614             if ($promille * 1000 < $pro)
615                 return $col;
616         }
617         $lastcol = end($this->ColorTab);
618         return $lastcol;
619     }
620 }
621
622 /**
623  * Linear interpolates a value between two point a and b
624  * at a value pos.
625  * @param $a
626  * @param $b
627  * @param $pos
628  * @return float  interpolated value
629  */
630 function interpolate($a, $b, $pos)
631 {
632     return $a + ($b - $a) * $pos;
633 }
634
635 // Local Variables:
636 // mode: php
637 // tab-width: 8
638 // c-basic-offset: 4
639 // c-hanging-comment-ender-p: nil
640 // indent-tabs-mode: nil
641 // End: