]> CyberLeo.Net >> Repos - SourceForge/phpwiki.git/blob - lib/plugin/Ploticus.php
support <!plugin-list !> pagelist data
[SourceForge/phpwiki.git] / lib / plugin / Ploticus.php
1 <?php // -*-php-*-
2 rcs_id('$Id: Ploticus.php,v 1.8 2004-09-22 15:23:56 rurban Exp $');
3 /*
4  Copyright 2004 $ThePhpWikiProgrammingTeam
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
19  along with PhpWiki; if not, write to the Free Software
20  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
21  */
22
23 /**
24  * The Ploticus plugin passes all its arguments to the ploticus 
25  * binary and displays the result as PNG, GIF, EPS, SVG or SWF.
26  * Ploticus is a free, GPL, non-interactive software package 
27  * for producing plots, charts, and graphics from data.
28  * See http://ploticus.sourceforge.net/doc/welcome.html
29  *
30  * @Author: Reini Urban
31  *
32  * Note: 
33  * - For windows you need either a gd library with GIF support or 
34  *   a ploticus with PNG support. This comes only with the cygwin build.
35  * - We support only images supported by GD so far (PNG most likely). 
36  *   No EPS, PS, SWF, SVG or SVGZ support due to limitations in WikiPluginCached.
37  *   This will be fixed soon.
38  *
39  * Usage:
40 <?plugin Ploticus device=png [ploticus options...]
41    multiline ploticus script ...
42 ?>
43  * or without any script: (not tested)
44 <?plugin Ploticus -prefab vbars data=myfile.dat delim=tab y=1 clickmapurl="http://mywiki.url/wiki/?pagename=@2" clickmaplabel="@3" -csmap ?>
45  *
46  * TODO: PloticusSql - create intermediate data from SQL. Similar to SqlResult, just in graphic form.
47  * For example to produce nice looking pagehit statistics or ratings statistics.
48  */
49
50 if (!defined("PLOTICUS_EXE"))
51   if (isWindows())
52     define('PLOTICUS_EXE','pl.exe');
53   else
54     define('PLOTICUS_EXE','/usr/local/bin/pl');
55
56 require_once "lib/WikiPluginCached.php"; 
57
58 class WikiPlugin_Ploticus
59 extends WikiPluginCached
60 {
61     /**
62      * Sets plugin type to MAP if -csmap (-map or -mapdemo or -csmapdemo not supported)
63      * or HTML if the imagetype is not supported by GD (EPS, SVG, SVGZ) (not yet)
64      * or IMG_INLINE if device = png, gif or jpeg
65      */
66     function getPluginType() {
67         if (!empty($this->_args['-csmap']))
68             return PLUGIN_CACHED_MAP; // not yet tested
69         elseif (in_array($this->decideImgType($this->_args['device']),
70                          array('','svg','swf','svgz','eps','ps','pdf','html')))
71             return PLUGIN_CACHED_IMG_ONDEMAND;
72         else
73             return PLUGIN_CACHED_IMG_INLINE;
74     }
75     function getName () {
76         return _("Ploticus");
77     }
78     function getDescription () {
79         return _("Ploticus image creation");
80     }
81     function managesValidators() {
82         return true;
83     }
84     function getVersion() {
85         return preg_replace("/[Revision: $]/", '',
86                             "\$Revision: 1.8 $");
87     }
88     function getDefaultArguments() {
89         return array(
90                      'device' => 'png', // png,gif,svgz,svg,...
91                      '-prefab' => '',
92                      '-csmap' => false,
93                      'data'    => false, // <!plugin-list !> support
94                      'alt'    => false,
95                      'help'   => false,
96                      );
97     }
98     function handle_plugin_args_cruft(&$argstr, &$args) {
99         $this->source = $argstr;
100     }
101     /**
102      * Sets the expire time to one day (so the image producing
103      * functions are called seldomly) or to about two minutes
104      * if a help screen is created.
105      */
106     function getExpire($dbi, $argarray, $request) {
107         if (!empty($argarray['help']))
108             return '+120'; // 2 minutes
109         return sprintf('+%d', 3*86000); // approx 3 days
110     }
111
112     /**
113      * Sets the imagetype according to user wishes and
114      * relies on WikiPluginCached to catch illegal image
115      * formats.
116      * (I feel unsure whether this option is reasonable in
117      *  this case, because png will definitely have the
118      *  best results.)
119      *
120      * @return string 'png', 'jpeg', 'gif'
121      */
122     function getImageType($dbi, $argarray, $request) {
123         return $argarray['device'];
124     }
125
126     /**
127      * This gives an alternative text description of
128      * the image.
129      */
130     function getAlt($dbi, $argstr, $request) {
131         return (!empty($this->_args['alt'])) ? $this->_args['alt']
132                                              : $this->getDescription();
133     }
134
135     /**
136      * Returns an image containing a usage description of the plugin.
137      *
138      * TODO: -csmap pointing to the Ploticus documentation at sf.net.
139      * @return string image handle
140      */
141     function helpImage() {
142         $def = $this->defaultArguments();
143         //$other_imgtypes = $GLOBALS['PLUGIN_CACHED_IMGTYPES'];
144         //unset ($other_imgtypes[$def['imgtype']]);
145         $helparr = array(
146             '<?plugin Ploticus ' .
147             'device'           => ' = "' . $def['device'] . "(default)|" . join('|',$GLOBALS['PLUGIN_CACHED_IMGTYPES']).'"',
148             'data'             => ' <!plugin-list !>: pagelist as input',
149             'alt'              => ' = "alternate text"',
150             '-csmap'           => ' bool: clickable map?',
151             'help'             => ' bool: displays this screen',
152             '...'              => ' all further lines below the first plugin line ',
153             ''                 => ' and inside the tags are the ploticus script.',
154             "\n  ?>"
155             );
156         $length = 0;
157         foreach($helparr as $alignright => $alignleft) {
158             $length = max($length, strlen($alignright));
159         }
160         $helptext ='';
161         foreach($helparr as $alignright => $alignleft) {
162             $helptext .= substr('                                                        '
163                                 . $alignright, -$length).$alignleft."\n";
164         }
165         return $this->text2img($helptext, 4, array(1, 0, 0),
166                                array(255, 255, 255));
167     }
168
169     function withShellCommand($script) {
170         $findme  = 'shell';
171         $pos = strpos($script, $findme); // uppercase?
172         if ($pos === false) 
173             return 0;
174         return 1;
175     }
176
177     function getImage($dbi, $argarray, $request) {
178         //extract($this->getArgs($argstr, $request));
179         //extract($argarray);
180         $source =& $this->source;
181         if (!empty($source)) {
182             if ($this->withShellCommand($source)) {
183                 $this->_errortext .= _("shell commands not allowed in Ploticus");
184                 return false;
185             }
186             if (is_array($argarray['data'])) { // support <!plugin-list !> pagelists
187                 $src = "#proc getdata\ndata:";
188                 $i = 0;
189                 foreach ($argarray['data'] as $data) {
190                     // hash or array?
191                     if (is_array($data))
192                         $src .= ("\t" . join(" ", $data) . "\n");
193                     else
194                         $src .= ("\t" . '"' . $data . '" ' . $i++ . "\n");
195                 }
196                 $src .= $source;
197                 $source = $src;
198             }
199             $tempfile = $this->tempnam('Ploticus');
200             unlink($tempfile);
201             $gif = $argarray['device'];
202             $args = " -stdin -$gif -o $tempfile.$gif";
203             if (!empty($argarray['-csmap'])) {
204                 $args .= " -csmap -mapfile $tempfile.map";
205                 $this->_mapfile = "$tempfile.map";
206             }
207             if (!empty($argarray['-prefab'])) {
208                 //TODO: check $_ENV['PLOTICUS_PREFABS'] and default directory
209                 $args .= (" -prefab " . $argarray['-prefab']);
210             }
211             $code = $this->filterThroughCmd($source, PLOTICUS_EXE . "$args");
212             //if (empty($code))
213             //    return $this->error(fmt("Couldn't start commandline '%s'", $commandLine));
214             if (! file_exists("$tempfile.$gif") ) {
215                 $this->_errortext .= sprintf(_("Ploticus error: Outputfile '%s' not created"), "$tempfile.$gif");
216                 return false;
217             }
218             $ImageCreateFromFunc = "ImageCreateFrom$gif";
219             $img = $ImageCreateFromFunc( "$tempfile.$gif" );
220             return $img;
221         } else {
222             return $this->error(fmt("empty source"));
223         }
224     }
225     
226     function getMap($dbi, $argarray, $request) {
227         $img = $this->getImage($dbi, $argarray, $request);
228         return array($this->_mapfile, $img);
229     }
230 };
231
232 // $Log: not supported by cvs2svn $
233 // Revision 1.7  2004/09/22 13:46:26  rurban
234 // centralize upload paths.
235 // major WikiPluginCached feature enhancement:
236 //   support _STATIC pages in uploads/ instead of dynamic getimg.php? subrequests.
237 //   mainly for debugging, cache problems and action=pdf
238 //
239 // Revision 1.6  2004/09/07 13:26:31  rurban
240 // new WikiPluginCached option debug=static and some more sf.net defaults for VisualWiki
241 //
242 // Revision 1.5  2004/06/28 16:35:12  rurban
243 // prevent from shell commands
244 //
245 // Revision 1.4  2004/06/19 10:06:38  rurban
246 // Moved lib/plugincache-config.php to config/*.ini
247 // use PLUGIN_CACHED_* constants instead of global $CacheParams
248 //
249 // Revision 1.3  2004/06/03 09:40:57  rurban
250 // WikiPluginCache improvements
251 //
252 // Revision 1.2  2004/06/02 19:37:07  rurban
253 // extended description
254 //
255 // Revision 1.1  2004/06/02 19:12:42  rurban
256 // new Ploticus plugin
257 //
258 //
259
260 // For emacs users
261 // Local Variables:
262 // mode: php
263 // tab-width: 8
264 // c-basic-offset: 4
265 // c-hanging-comment-ender-p: nil
266 // indent-tabs-mode: nil
267 // End:
268 ?>