3 * $Id: editor_plugin_src.js 201 2007-02-12 15:56:56Z spocke $
5 * @package MCManager.includes
7 * @copyright Copyright © 2004-2007, Moxiecode Systems AB, All rights reserved.
10 class PSpellShell extends SpellChecker {
12 * Spellchecks an array of words.
14 * @param {String} $lang Language code like sv or en.
15 * @param {Array} $words Array of words to spellcheck.
16 * @return {Array} Array of misspelled words.
18 function &checkWords($lang, $words) {
19 $cmd = $this->_getCMD($lang);
21 if ($fh = fopen($this->_tmpfile, "w")) {
24 foreach($words as $key => $value)
25 fwrite($fh, "^" . $value . "\n");
29 $this->throwError("PSpell support was not found.");
31 $data = shell_exec($cmd);
32 @unlink($this->_tmpfile);
34 $returnData = array();
35 $dataArr = preg_split("/[\r\n]/", $data, -1, PREG_SPLIT_NO_EMPTY);
37 foreach ($dataArr as $dstr) {
41 if (strpos($dstr, "@") === 0)
44 preg_match("/\& ([^ ]+) .*/i", $dstr, $matches);
46 if (!empty($matches[1]))
47 $returnData[] = utf8_encode(trim($matches[1]));
54 * Returns suggestions of for a specific word.
56 * @param {String} $lang Language code like sv or en.
57 * @param {String} $word Specific word to get suggestions for.
58 * @return {Array} Array of suggestions for the specified word.
60 function &getSuggestions($lang, $word) {
61 $cmd = $this->_getCMD($lang);
63 if (function_exists("mb_convert_encoding"))
64 $word = mb_convert_encoding($word, "ISO-8859-1", mb_detect_encoding($word, "UTF-8"));
66 $word = utf8_encode($word);
68 if ($fh = fopen($this->_tmpfile, "w")) {
70 fwrite($fh, "^$word\n");
73 $this->throwError("Error opening tmp file.");
75 $data = shell_exec($cmd);
76 @unlink($this->_tmpfile);
78 $returnData = array();
79 $dataArr = preg_split("/\n/", $data, -1, PREG_SPLIT_NO_EMPTY);
81 foreach($dataArr as $dstr) {
85 if (strpos($dstr, "@") === 0)
88 preg_match("/\&[^:]+:(.*)/i", $dstr, $matches);
90 if (!empty($matches[1])) {
91 $words = array_slice(explode(',', $matches[1]), 0, 10);
93 for ($i=0; $i<count($words); $i++)
94 $words[$i] = trim($words[$i]);
103 function _getCMD($lang) {
104 $this->_tmpfile = tempnam($this->_config['PSpellShell.tmp'], "tinyspell");
106 if(preg_match("#win#i", php_uname()))
107 return $this->_config['PSpellShell.aspell'] . " -a --lang=". escapeshellarg($lang) . " --encoding=utf-8 -H < " . $this->_tmpfile . " 2>&1";
109 return "cat ". $this->_tmpfile ." | " . $this->_config['PSpellShell.aspell'] . " -a --encoding=utf-8 -H --lang=". escapeshellarg($lang);