3 * $Id: editor_plugin_src.js 201 2007-02-12 15:56:56Z spocke $
6 * @copyright Copyright © 2004-2007, Moxiecode Systems AB, All rights reserved.
9 class PSpellShell extends SpellChecker {
11 * Spellchecks an array of words.
13 * @param {String} $lang Language code like sv or en.
14 * @param {Array} $words Array of words to spellcheck.
15 * @return {Array} Array of misspelled words.
17 function &checkWords($lang, $words) {
18 $cmd = $this->_getCMD($lang);
20 if ($fh = fopen($this->_tmpfile, "w")) {
23 foreach($words as $key => $value)
24 fwrite($fh, "^" . $value . "\n");
28 $this->throwError("PSpell support was not found.");
30 $data = shell_exec($cmd);
31 @unlink($this->_tmpfile);
33 $returnData = array();
34 $dataArr = preg_split("/[\r\n]/", $data, -1, PREG_SPLIT_NO_EMPTY);
36 foreach ($dataArr as $dstr) {
40 if (strpos($dstr, "@") === 0)
43 preg_match("/\& ([^ ]+) .*/i", $dstr, $matches);
45 if (!empty($matches[1]))
46 $returnData[] = utf8_encode(trim($matches[1]));
53 * Returns suggestions of for a specific word.
55 * @param {String} $lang Language code like sv or en.
56 * @param {String} $word Specific word to get suggestions for.
57 * @return {Array} Array of suggestions for the specified word.
59 function &getSuggestions($lang, $word) {
60 $cmd = $this->_getCMD($lang);
62 if (function_exists("mb_convert_encoding"))
63 $word = mb_convert_encoding($word, "ISO-8859-1", mb_detect_encoding($word, "UTF-8"));
65 $word = utf8_encode($word);
67 if ($fh = fopen($this->_tmpfile, "w")) {
69 fwrite($fh, "^$word\n");
72 $this->throwError("Error opening tmp file.");
74 $data = shell_exec($cmd);
75 @unlink($this->_tmpfile);
77 $returnData = array();
78 $dataArr = preg_split("/\n/", $data, -1, PREG_SPLIT_NO_EMPTY);
80 foreach($dataArr as $dstr) {
84 if (strpos($dstr, "@") === 0)
87 preg_match("/\&[^:]+:(.*)/i", $dstr, $matches);
89 if (!empty($matches[1])) {
90 $words = array_slice(explode(',', $matches[1]), 0, 10);
92 for ($i=0; $i<count($words); $i++)
93 $words[$i] = trim($words[$i]);
102 function _getCMD($lang) {
103 $this->_tmpfile = tempnam($this->_config['PSpellShell.tmp'], "tinyspell");
105 if(preg_match("#win#i", php_uname()))
106 return $this->_config['PSpellShell.aspell'] . " -a --lang=". $lang . " --encoding=utf-8 -H < " . $this->_tmpfile . " 2>&1";
108 return "cat ". $this->_tmpfile ." | " . $this->_config['PSpellShell.aspell'] . " -a --encoding=utf-8 -H --lang=". $lang;