From b0a66b068a06902cdd5e3b145c2fad7a90b6da4a Mon Sep 17 00:00:00 2001 From: rurban Date: Tue, 7 Sep 2004 13:26:31 +0000 Subject: [PATCH] new WikiPluginCached option debug=static and some more sf.net defaults for VisualWiki git-svn-id: svn://svn.code.sf.net/p/phpwiki/code/trunk@3940 96ab9672-09ca-45d6-a79d-3d69d39ca109 --- lib/WikiPluginCached.php | 85 +++++++++++++++++++-- lib/plugin/Ploticus.php | 70 ++---------------- lib/plugin/VisualWiki.php | 152 ++++++++++++++++++++++++++++---------- 3 files changed, 200 insertions(+), 107 deletions(-) diff --git a/lib/WikiPluginCached.php b/lib/WikiPluginCached.php index 10dfa9327..c976c11de 100644 --- a/lib/WikiPluginCached.php +++ b/lib/WikiPluginCached.php @@ -1,4 +1,4 @@ - $key ), $map ), @@ -814,8 +815,12 @@ class WikiPluginCached extends WikiPlugin 'width' => 600, 'height' => 350 ); - $charx = ImageFontWidth($fontnr); - $chary = ImageFontHeight($fontnr); + if (function_exists('ImageFontWidth')) { + $charx = ImageFontWidth($fontnr); + $chary = ImageFontHeight($fontnr); + } else { + $charx = 10; $chary = 10; + } $marginx = $charx; $marginy = floor($chary/2); @@ -862,10 +867,78 @@ class WikiPluginCached extends WikiPlugin return $im; } // text2img + function newFilterThroughCmd($input, $commandLine) { + $descriptorspec = array( + 0 => array("pipe", "r"), // stdin is a pipe that the child will read from + 1 => array("pipe", "w"), // stdout is a pipe that the child will write to + 2 => array("pipe", "w"), // stdout is a pipe that the child will write to + ); + + $process = proc_open("$commandLine", $descriptorspec, $pipes); + if (is_resource($process)) { + // $pipes now looks like this: + // 0 => writeable handle connected to child stdin + // 1 => readable handle connected to child stdout + // 2 => readable handle connected to child stderr + fwrite($pipes[0], $input); + fclose($pipes[0]); + $buf = ""; + while(!feof($pipes[1])) { + $buf .= fgets($pipes[1], 1024); + } + fclose($pipes[1]); + $stderr = ''; + while(!feof($pipes[2])) { + $stderr .= fgets($pipes[2], 1024); + } + fclose($pipes[2]); + // It is important that you close any pipes before calling + // proc_close in order to avoid a deadlock + $return_value = proc_close($process); + if (empty($buf)) printXML($this->error($stderr)); + return $buf; + } + } + + /* PHP versions < 4.3 + * TODO: via temp file looks more promising + */ + function OldFilterThroughCmd($input, $commandLine) { + $input = str_replace ("\\", "\\\\", $input); + $input = str_replace ("\"", "\\\"", $input); + $input = str_replace ("\$", "\\\$", $input); + $input = str_replace ("`", "\`", $input); + $input = str_replace ("'", "\'", $input); + //$input = str_replace (";", "\;", $input); + + $pipe = popen("echo \"$input\"|$commandLine", 'r'); + if (!$pipe) { + print "pipe failed."; + return ""; + } + $output = ''; + while (!feof($pipe)) { + $output .= fread($pipe, 1024); + } + pclose($pipe); + return $output; + } + + // run "echo $source | $commandLine" and return result + function filterThroughCmd($source, $commandLine) { + if (check_php_version(4,3,0)) + return $this->newFilterThroughCmd($source, $commandLine); + else + return $this->oldFilterThroughCmd($source, $commandLine); + } + } // WikiPluginCached // $Log: not supported by cvs2svn $ +// Revision 1.11 2004/09/06 09:12:46 rurban +// improve pear handling with silent fallback to ours +// // For emacs users // Local Variables: diff --git a/lib/plugin/Ploticus.php b/lib/plugin/Ploticus.php index 3964fa349..38ce845ec 100644 --- a/lib/plugin/Ploticus.php +++ b/lib/plugin/Ploticus.php @@ -1,5 +1,5 @@ text2img($helptext, 4, array(1, 0, 0), array(255, 255, 255)); } - - function newFilterThroughCmd($input, $commandLine) { - $descriptorspec = array( - 0 => array("pipe", "r"), // stdin is a pipe that the child will read from - 1 => array("pipe", "w"), // stdout is a pipe that the child will write to - 2 => array("pipe", "w"), // stdout is a pipe that the child will write to - ); - - $process = proc_open("$commandLine", $descriptorspec, $pipes); - if (is_resource($process)) { - // $pipes now looks like this: - // 0 => writeable handle connected to child stdin - // 1 => readable handle connected to child stdout - // 2 => readable handle connected to child stderr - fwrite($pipes[0], $input); - fclose($pipes[0]); - $buf = ""; - while(!feof($pipes[1])) { - $buf .= fgets($pipes[1], 1024); - } - fclose($pipes[1]); - $stderr = ''; - while(!feof($pipes[2])) { - $stderr .= fgets($pipes[2], 1024); - } - fclose($pipes[2]); - // It is important that you close any pipes before calling - // proc_close in order to avoid a deadlock - $return_value = proc_close($process); - if (empty($buf)) printXML($this->error($stderr)); - return $buf; - } - } - - /* PHP versions < 4.3 - * TODO: via temp file looks more promising - */ - function OldFilterThroughCmd($input, $commandLine) { - $input = str_replace ("\\", "\\\\", $input); - $input = str_replace ("\"", "\\\"", $input); - $input = str_replace ("\$", "\\\$", $input); - $input = str_replace ("`", "\`", $input); - $input = str_replace ("'", "\'", $input); - //$input = str_replace (";", "\;", $input); - - $pipe = popen("echo \"$input\"|$commandLine", 'r'); - if (!$pipe) { - print "pipe failed."; - return ""; - } - $output = ''; - while (!feof($pipe)) { - $output .= fread($pipe, 1024); - } - pclose($pipe); - return $output; - } function withShellCommand($script) { $findme = 'shell'; @@ -243,11 +186,7 @@ extends WikiPluginCached $args .= " -csmap -mapfile $tempfiles.map"; $this->_mapfile = "$tempfiles.map"; } - $commandLine = PLOTICUS_EXE . "$args"; - if (check_php_version(4,3,0)) - $code = $this->newFilterThroughCmd($source, $commandLine); - else - $code = $this->oldFilterThroughCmd($source, $commandLine); + $code = $this->filterThroughCmd($source, PLOTICUS_EXE . "$args"); //if (empty($code)) // return $this->error(fmt("Couldn't start commandline '%s'", $commandLine)); if (! file_exists("$tempfiles.$gif") ) { @@ -269,6 +208,9 @@ extends WikiPluginCached }; // $Log: not supported by cvs2svn $ +// Revision 1.5 2004/06/28 16:35:12 rurban +// prevent from shell commands +// // Revision 1.4 2004/06/19 10:06:38 rurban // Moved lib/plugincache-config.php to config/*.ini // use PLUGIN_CACHED_* constants instead of global $CacheParams diff --git a/lib/plugin/VisualWiki.php b/lib/plugin/VisualWiki.php index 03eaf4580..b7d5d1db7 100644 --- a/lib/plugin/VisualWiki.php +++ b/lib/plugin/VisualWiki.php @@ -1,5 +1,5 @@ getArg('debug')) ? PLUGIN_CACHED_IMG_ONDEMAND : PLUGIN_CACHED_MAP; } /** @@ -86,7 +87,7 @@ extends WikiPluginCached function getVersion() { return preg_replace("/[Revision: $]/", '', - "\$Revision: 1.13 $"); + "\$Revision: 1.14 $"); } /** @@ -498,14 +499,15 @@ extends WikiPluginCached $ok = true; $names = &$this->names; $pages = &$this->pages; - - $nametonumber = array_flip($names); + if ($names) + $nametonumber = array_flip($names); $dot = "digraph VisualWiki {\n" // } . (!empty($fontpath) ? " fontpath=\"$fontpath\"\n" : ""); if ($width and $height) $dot .= " size=\"$width,$height\";\n "; + switch ($shape) { case 'point': $dot .= "edge [arrowhead=none];\nnode [shape=$shape,fontname=$fontname,width=0.15,height=0.15,fontsize=$fontsize];\n"; @@ -594,14 +596,28 @@ extends WikiPluginCached * @param cmd string command to be invoked * @return boolean error status; true=ok; false=error */ - function execute($cmd) { - exec($cmd, $outarr, $returnval); // normally 127 - $errstr = join('',$outarr); - if (!empty($errstr)) - trigger_error($cmd.": ".$errstr, E_USER_WARNING); - if (!isWindows()) - usleep(1000); - return empty($errstr); + function execute($cmd, $until = false) { + $errstr = exec($cmd); //, $outarr, $returnval); // normally 127 + // $errstr = join('',$outarr); + $ok = empty($errstr); + if (!$ok) { + trigger_error("\n".$cmd." failed: $errstr", E_USER_WARNING); + } elseif ($GLOBALS['request']->getArg('debug')) + trigger_error("\n".$cmd.": success\n", E_USER_NOTICE); + if (!isWindows()) { + if ($until) { + $loop = 100000; + while (!file_exists($until) and $loop > 0) { + $loop -= 100; + usleep(100); + } + } else { + usleep(5000); + } + } + if ($until) + return file_exists($until); + return $ok; } /** @@ -622,17 +638,41 @@ extends WikiPluginCached $tempfiles = $this->tempnam('VisualWiki'); $gif = $argarray['imgtype']; $ImageCreateFromFunc = "ImageCreateFrom$gif"; - $ok = $tempfiles + $outfile = $tempfiles.".".$gif; + $debug = $GLOBALS['request']->getArg('debug'); + if ($debug) { + $tempdir = dirname($tempfiles); + $tempout = $tempdir . "/.debug"; + } + $ok = $tempfiles && $this->createDotFile($tempfiles.'.dot',$argarray) - && $this->execute("$dotbin -T$gif $tempfiles.dot -o $tempfiles.$gif") - && $this->execute("$dotbin -Timap $tempfiles.dot -o $tempfiles.map") - && file_exists( "$tempfiles.$gif" ) + // && $this->filterThroughCmd('',"$dotbin -T$gif $tempfiles.dot -o $outfile") + // && $this->filterThroughCmd('',"$dotbin -Timap $tempfiles.dot -o ".$tempfiles.".map") + && $this->execute("$dotbin -T$gif $tempfiles.dot -o $outfile" . ($debug ? " > $tempout 2>&1" : ""), $outfile) + && $this->execute("$dotbin -Timap $tempfiles.dot -o ".$tempfiles.".map" . ($debug ? " > $tempout 2>&1" : ""), $tempfiles.".map") + && file_exists( $outfile ) && file_exists( $tempfiles.'.map' ) - && ($img = $ImageCreateFromFunc( "$tempfiles.$gif" )) - && ($fp = fopen($tempfiles.'.map','r')); + && ($img = $ImageCreateFromFunc($outfile)) + && ($fp = fopen($tempfiles.'.map', 'r')); $map = HTML(); - if ($ok) { + if ($debug == 'static') { + // workaround for misconfigured WikiPluginCached (sf.net) or dot. + // present a static png and map file. + if (file_exists($outfile) and filesize($outfile) > 900) + $img = $outfile; + else + $img = $tempdir . "/VisualWiki.".$gif; + if (file_exists( $tempfiles.".map") and filesize($tempfiles.".map") > 20) + $map = $tempfiles.".map"; + else + $map = $tempdir . "/VisualWiki.map"; + $img = $ImageCreateFromFunc($img); + $fp = fopen($map, 'r'); + $map = HTML(); + $ok = true; + } + if ($ok and $fp) { while (!feof($fp)) { $line = fgets($fp, 1000); if (substr($line, 0, 1) == '#') @@ -654,22 +694,21 @@ extends WikiPluginCached 'href' => $url, 'title' => rawurldecode($url), 'alt' => $url))); - } + } fclose($fp); -//trigger_error("url=".$url); + //trigger_error("url=".$url); } else { trigger_error(" -$tempfiles.$gif: ".(file_exists("$tempfiles.$gif") ? filesize("$tempfiles.$gif"):'missing')." -$tempfiles.map: ".(file_exists("$tempfiles.map") ? filesize("$tempfiles.map"):'missing')." -",E_USER_WARNING); +$outfile: ".(file_exists($outfile) ? filesize($outfile):'missing')." +$tempfiles.map: ".(file_exists("$tempfiles.map") ? filesize("$tempfiles.map"):'missing'), E_USER_WARNING); } // clean up tempfiles if ($ok and !$argarray['debug']) - foreach (array('',".$gif",".map",".dot") as $ext) { - if (file_exists($tempfiles.$ext)) - unlink($tempfiles.$ext); - } + foreach (array('',".$gif",'.map','.dot') as $ext) { + if (file_exists($tempfiles.$ext)) + unlink($tempfiles.$ext); + } if ($ok) return array($img, $map); @@ -677,6 +716,42 @@ $tempfiles.map: ".(file_exists("$tempfiles.map") ? filesize("$tempfiles.map"):'m return array(false, false); } // invokeDot + + /** + * static workaround on broken Cache, called only if debug=static + * + * @access private + * @param url string url pointing to the image part of the map + * @param map string <area> tags defining active + * regions in the map + * @param dbi WikiDB database abstraction class + * @param argarray array complete (!) arguments to produce + * image. It is not necessary to call + * WikiPlugin->getArgs anymore. + * @param request Request ??? + * @return string html output + */ + function embedImg($url,&$dbi,$argarray,&$request) { + if (!VISUALWIKI_ALLOWOPTIONS) + $argarray = $this->defaultarguments(); + $this->checkArguments($argarray); + //extract($argarray); + if ($argarray['help']) + return array($this->helpImage(), ' '); // FIXME + $this->createColors(); + $this->extract_wikipages($dbi, $argarray); + list($imagehandle, $content['html']) = $this->invokeDot($argarray); + // write to uploads and produce static url + $file_dir = defined('PHPWIKI_DIR') ? + PHPWIKI_DIR . "/uploads" : "uploads"; + $upload_dir = SERVER_URL . ((substr(DATA_PATH,0,1)=='/') ? '' : "/") . DATA_PATH . '/uploads/'; + $tmpfile = tempnam($file_dir,"VisualWiki").".".$argarray['imgtype']; + WikiPluginCached::writeImage($argarray['imgtype'], $imagehandle, $tmpfile); + ImageDestroy($imagehandle); + return WikiPluginCached::embedMap(1,$upload_dir.basename($tmpfile),$content['html'],$dbi,$argarray,$request); + } + + /** * Prepares some rainbow colors for the nodes of the graph * and stores them in an array which may be accessed with @@ -741,6 +816,9 @@ function interpolate($a, $b, $pos) { } // $Log: not supported by cvs2svn $ +// Revision 1.13 2004/09/06 12:13:00 rurban +// provide sf.net default dotbin +// // Revision 1.12 2004/09/06 12:08:50 rurban // memory_limit on unix workaround // VisualWiki: default autosize image -- 2.45.0