4 Copyright 2004 $ThePhpWikiProgrammingTeam
6 This file is part of PhpWiki.
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.
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.
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
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
30 * @Author: Reini Urban
33 * - For windows you need either a gd library with GIF support or
34 * a ploticus with PNG support. This comes e.g. 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 yet, due to limitations in WikiPluginCached.
37 * This will be fixed soon.
40 <?plugin Ploticus device=png [ploticus options...]
41 multiline ploticus script ...
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 ?>
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 * Ploticus has its own sql support within #getproc data, but this would expose security information.
51 if (!defined("PLOTICUS_EXE"))
53 define('PLOTICUS_EXE','pl.exe');
55 define('PLOTICUS_EXE','/usr/local/bin/pl');
56 //TODO: check $_ENV['PLOTICUS_PREFABS'] and default directory
58 require_once "lib/WikiPluginCached.php";
60 class WikiPlugin_Ploticus
61 extends WikiPluginCached
64 * Sets plugin type to MAP if -csmap (-map or -mapdemo or -csmapdemo not supported)
65 * or HTML if the imagetype is not supported by GD (EPS, SVG, SVGZ) (not yet)
66 * or IMG_INLINE if device = png, gif or jpeg
68 function getPluginType() {
69 if (!empty($this->_args['-csmap']))
70 return PLUGIN_CACHED_MAP; // not yet tested
71 // produce these on-demand so far, uncached.
72 // will get better support in WikiPluginCached soon.
73 // FIXME: html also? what about ''?
74 $type = $this->decideImgType($this->_args['device']);
75 if ($type == $this->_args['device'])
76 return PLUGIN_CACHED_IMG_INLINE;
77 $device = strtolower($this->_args['device']);
78 if (in_array($device, array('svg','swf','svgz','eps','ps','pdf','html'))) {
79 switch ($this->_args['device']) {
82 return PLUGIN_CACHED_STATIC | PLUGIN_CACHED_SVG_PNG;
84 return PLUGIN_CACHED_STATIC | PLUGIN_CACHED_SWF;
86 return PLUGIN_CACHED_STATIC | PLUGIN_CACHED_HTML;
90 return PLUGIN_CACHED_IMG_INLINE; // normal cached libgd image handles
95 function getDescription () {
96 return _("Ploticus image creation");
98 function managesValidators() {
101 function getVersion() {
102 return preg_replace("/[Revision: $]/", '',
105 function getDefaultArguments() {
107 'device' => 'png', // png,gif,svgz,svg,...
110 'data' => false, // <!plugin-list !> support
115 function handle_plugin_args_cruft(&$argstr, &$args) {
116 $this->source = $argstr;
119 * Sets the expire time to one day (so the image producing
120 * functions are called seldomly) or to about two minutes
121 * if a help screen is created.
123 function getExpire($dbi, $argarray, $request) {
124 if (!empty($argarray['help']))
125 return '+120'; // 2 minutes
126 return sprintf('+%d', 3*86000); // approx 3 days
130 * Sets the imagetype according to user wishes and
131 * relies on WikiPluginCached to catch illegal image
133 * (I feel unsure whether this option is reasonable in
134 * this case, because png will definitely have the
137 * @return string 'png', 'jpeg', 'gif'
139 function getImageType($dbi, $argarray, $request) {
140 return $argarray['device'];
144 * This gives an alternative text description of
147 function getAlt($dbi, $argstr, $request) {
148 return (!empty($this->_args['alt'])) ? $this->_args['alt']
149 : $this->getDescription();
153 * Returns an image containing a usage description of the plugin.
155 * TODO: -csmap pointing to the Ploticus documentation at sf.net.
156 * @return string image handle
158 function helpImage() {
159 $def = $this->defaultArguments();
160 //$other_imgtypes = $GLOBALS['PLUGIN_CACHED_IMGTYPES'];
161 //unset ($other_imgtypes[$def['imgtype']]);
163 '<?plugin Ploticus ' .
164 'device' => ' = "' . $def['device'] . "(default)|"
165 . join('|',$GLOBALS['PLUGIN_CACHED_IMGTYPES']).'"',
166 'data' => ' <!plugin-list !>: pagelist as input',
167 'alt' => ' = "alternate text"',
168 '-csmap' => ' bool: clickable map?',
169 'help' => ' bool: displays this screen',
170 '...' => ' all further lines below the first plugin line ',
171 '' => ' and inside the tags are the ploticus script.',
175 foreach($helparr as $alignright => $alignleft) {
176 $length = max($length, strlen($alignright));
179 foreach($helparr as $alignright => $alignleft) {
180 $helptext .= substr(' '
181 . $alignright, -$length).$alignleft."\n";
183 return $this->text2img($helptext, 4, array(1, 0, 0),
184 array(255, 255, 255));
187 function withShellCommand($script) {
189 $pos = strpos($script, $findme); // uppercase?
195 function getImage($dbi, $argarray, $request) {
196 //extract($this->getArgs($argstr, $request));
197 //extract($argarray);
198 $source =& $this->source;
199 if (!empty($source)) {
200 if ($this->withShellCommand($source)) {
201 $this->_errortext .= _("shell commands not allowed in Ploticus");
204 if (is_array($argarray['data'])) { // support <!plugin-list !> pagelists
205 $src = "#proc getdata\ndata:";
207 foreach ($argarray['data'] as $data) {
210 $src .= ("\t" . join(" ", $data) . "\n");
212 $src .= ("\t" . '"' . $data . '" ' . $i++ . "\n");
217 $tempfile = $this->tempnam('Ploticus','plo');
219 $gif = $argarray['device'];
220 $args = "-$gif -o $tempfile.$gif";
221 if (!empty($argarray['-csmap'])) {
222 $args .= " -csmap -mapfile $tempfile.map";
223 $this->_mapfile = "$tempfile.map";
225 if (!empty($argarray['-prefab'])) {
226 //check $_ENV['PLOTICUS_PREFABS'] and default directory
227 global $HTTP_ENV_VARS;
228 if (empty($HTTP_ENV_VARS['PLOTICUS_PREFABS'])) {
229 if (file_exists("/usr/share/ploticus"))
230 $HTTP_ENV_VARS['PLOTICUS_PREFABS'] = "/usr/share/ploticus";
231 elseif (defined('PLOTICUS_PREFABS'))
232 $HTTP_ENV_VARS['PLOTICUS_PREFABS'] = constant('PLOTICUS_PREFABS');
234 $args .= (" -prefab " . $argarray['-prefab']);
237 $fp = fopen("$tempfile.plo", "w");
238 fwrite ($fp, $source);
240 $code = $this->execute(PLOTICUS_EXE . " $tempfile.plo $args", $tempfile.".$gif");
242 $code = $this->filterThroughCmd($source, PLOTICUS_EXE . " -stdin $args");
246 // return $this->error(fmt("Couldn't start commandline '%s'", $commandLine));
247 if (! file_exists($tempfile.".$gif") ) {
248 $this->_errortext .= sprintf(_("%s error: outputfile '%s' not created"),
249 "Ploticus", "$tempfile.$gif");
251 $this->_errortext .= ("\ncmd-line: " .PLOTICUS_EXE . " $tempfile.plo $args");
253 $this->_errortext .= ("\ncmd-line: cat script | ".PLOTICUS_EXE . " $args");
254 @unlink("$tempfile.pl");
255 @unlink("$tempfile");
258 $ImageCreateFromFunc = "ImageCreateFrom$gif";
259 if (function_exists($ImageCreateFromFunc)) {
260 $handle = $ImageCreateFromFunc( "$tempfile.$gif" );
262 @unlink("$tempfile.$gif");
263 @unlink("$tempfile.plo");
264 @unlink("$tempfile");
268 return "$tempfile.$gif";
270 return $this->error(fmt("empty source"));
274 // which argument must be set to 'png', for the fallback image when svg will fail on the client.
280 function getMap($dbi, $argarray, $request) {
281 $img = $this->getImage($dbi, $argarray, $request);
282 return array($this->_mapfile, $img);
291 // c-hanging-comment-ender-p: nil
292 // indent-tabs-mode: nil