diff options
author | andi <andi@splitbrain.org> | 2005-06-10 01:01:43 +0200 |
---|---|---|
committer | andi <andi@splitbrain.org> | 2005-06-10 01:01:43 +0200 |
commit | d8681a9e775f904d35aa033a84ef569e1df09ac1 (patch) | |
tree | 633cb70df6dd4a1e92a89f0436719dfffced2750 | |
parent | e1b80655c063a49d8762da43baee81198a5da1ff (diff) | |
download | rpg-d8681a9e775f904d35aa033a84ef569e1df09ac1.tar.gz rpg-d8681a9e775f904d35aa033a84ef569e1df09ac1.tar.bz2 |
Spellchecker fix for large files
darcs-hash:20050609230143-9977f-1b424e3f23bb4939d7636c5ef8f4d9516942f205.gz
-rw-r--r-- | inc/aspell.php | 39 | ||||
-rw-r--r-- | lib/exe/spellcheck.php | 14 |
2 files changed, 36 insertions, 17 deletions
diff --git a/inc/aspell.php b/inc/aspell.php index 9e83436a8..8267fb839 100644 --- a/inc/aspell.php +++ b/inc/aspell.php @@ -27,11 +27,14 @@ if(!defined('ASPELL_BIN')) define('ASPELL_BIN','aspell'); +// different spelling modes supported by aspell if(!defined('PSPELL_FAST')) define(PSPELL_FAST,1); # Fast mode (least number of suggestions) if(!defined('PSPELL_NORMAL')) define(PSPELL_NORMAL,2); # Normal mode (more suggestions) if(!defined('PSPELL_BAD_SPELLERS')) define(PSPELL_BAD_SPELLERS,3); # Slow mode (a lot of suggestions) if(!defined('ASPELL_ULTRA')) define(ASPELL_ULTRA,4); # Ultra fast mode (not available in Pspell!) + + /** * You can define PSPELL_COMP to use this class as drop in replacement * for the pspell extension @@ -111,17 +114,17 @@ class Aspell{ function _prepareArgs(){ $this->args = ''; - if($this->language){ + if($this->language != null){ $this->args .= ' --lang='.escapeshellarg($this->language); }else{ return false; // no lang no spell } - if($this->jargon){ + if($this->jargon != null){ $this->args .= ' --jargon='.escapeshellarg($this->jargon); } - if($this->encoding){ + if($this->encoding != null){ $this->args .= ' --encoding='.escapeshellarg($this->encoding); } @@ -148,15 +151,14 @@ class Aspell{ * This opens a bidirectional pipe to the aspell binary, writes * the given text to STDIN and returns STDOUT and STDERR * - * You have full access to aspell's pipe mode here - this means you need - * quote your lines yourself read the aspell manual for more info + * You can give an array of special commands to be executed first + * as $specials parameter. Data lines are escaped automatically * * @author Andreas Gohr <andi@splitbrain.org> * @link http://aspell.sf.net/man-html/Through-A-Pipe.html */ - function runAspell($text,&$out,&$err){ + function runAspell($text,&$out,&$err,$specials=null){ if(empty($text)) return true; - //prepare file descriptors $descspec = array( 0 => array('pipe', 'r'), // stdin is a pipe that the child will read from @@ -166,22 +168,37 @@ class Aspell{ $process = proc_open(ASPELL_BIN.' -a'.$this->args, $descspec, $pipes); if ($process) { - //write to stdin - fwrite($pipes[0],$text); + // write specials if given + if(is_array($specials)){ + foreach($specials as $s) fwrite($pipes[0],"$s\n"); + } + + // write line and read answer + $data = explode("\n",$text); + foreach($data as $line){ + fwrite($pipes[0],"^$line\n"); // aspell uses ^ to escape the line + fflush($pipes[0]); + do{ + $r = fgets($pipes[1],8192); + $out .= $r; + if(feof($pipes[1])) break; + }while($r != "\n"); + } fclose($pipes[0]); - //read stdout + // read remaining stdout (shouldn't be any) while (!feof($pipes[1])) { $out .= fread($pipes[1], 8192); } fclose($pipes[1]); - //read stderr + // read stderr while (!feof($pipes[2])) { $err .= fread($pipes[2], 8192); } fclose($pipes[2]); + // close process if(proc_close($process) != 0){ //something went wrong $err = "Aspell returned an error: $err"; diff --git a/lib/exe/spellcheck.php b/lib/exe/spellcheck.php index df4f134f4..ffa0e2886 100644 --- a/lib/exe/spellcheck.php +++ b/lib/exe/spellcheck.php @@ -48,13 +48,14 @@ if(!count($_POST) && $HTTP_RAW_POST_DATA){ if(!defined('DOKU_INC')) define('DOKU_INC',realpath(dirname(__FILE__).'/../../').'/'); require_once (DOKU_INC.'inc/init.php'); +session_write_close(); require_once (DOKU_INC.'inc/utf8.php'); require_once (DOKU_INC.'inc/aspell.php'); -header('Content-Type: text/html; charset=utf-8'); +header('Content-Type: text/plain; charset=utf-8'); //create spell object -$spell = new Aspell($conf['lang'],'null','utf-8'); +$spell = new Aspell($conf['lang'],null,'utf-8'); $spell->setMode(PSPELL_FAST); //call the requested function @@ -78,15 +79,15 @@ function spell_check() { // we need the text as array later $data = explode("\n",$string); - //prepare for aspell (add ^ to prevent commands) - $string = '^'.join("\n^",$data); - // keep some words from being checked (use blanks to preserve the offset) FIXME doesn't work yet + // keep some words from being checked (use blanks to preserve the offset) + // FIXME doesn't work yet... however with the used html mode of aspell this isn't really needed /* $string = preg_replace('!<\?(code|del|file)( \+)?>!e','spellclean(\\1)',$string); */ // $string = preg_replace('!()!e','spellclean(\\1)',$string); // run aspell in terse sgml mode - if(!$spell->runAspell("!\n+sgml\n".$string,$out,$err)){ + if(!$spell->runAspell($string,$out,$err,array('!','+html'))){ + //if(!$spell->runAspell($string,$out,$err)){ print '2'; //to indicate an error print "An error occured while trying to run the spellchecker:\n"; print $err; @@ -98,6 +99,7 @@ function spell_check() { $rcnt = count($lines)-1; // aspell result count $lcnt = count($data)+1; // original line counter + for($i=$rcnt; $i>=0; $i--){ $line = trim($lines[$i]); if($line[0] == '@') continue; // comment |