]> scripts.mit.edu Git - autoinstalls/wordpress.git/blob - wp-includes/js/tinymce/plugins/spellchecker/classes/PSpellShell.php
Wordpress 2.7.1-scripts
[autoinstalls/wordpress.git] / wp-includes / js / tinymce / plugins / spellchecker / classes / PSpellShell.php
1 <?php
2 /**
3  * $Id: editor_plugin_src.js 201 2007-02-12 15:56:56Z spocke $
4  *
5  * @package MCManager.includes
6  * @author Moxiecode
7  * @copyright Copyright © 2004-2007, Moxiecode Systems AB, All rights reserved.
8  */
9
10 class PSpellShell extends SpellChecker {
11         /**
12          * Spellchecks an array of words.
13          *
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.
17          */
18         function &checkWords($lang, $words) {
19                 $cmd = $this->_getCMD($lang);
20
21                 if ($fh = fopen($this->_tmpfile, "w")) {
22                         fwrite($fh, "!\n");
23
24                         foreach($words as $key => $value)
25                                 fwrite($fh, "^" . $value . "\n");
26
27                         fclose($fh);
28                 } else
29                         $this->throwError("PSpell support was not found.");
30
31                 $data = shell_exec($cmd);
32                 @unlink($this->_tmpfile);
33
34                 $returnData = array();
35                 $dataArr = preg_split("/[\r\n]/", $data, -1, PREG_SPLIT_NO_EMPTY);
36
37                 foreach ($dataArr as $dstr) {
38                         $matches = array();
39
40                         // Skip this line.
41                         if (strpos($dstr, "@") === 0)
42                                 continue;
43
44                         preg_match("/\& ([^ ]+) .*/i", $dstr, $matches);
45
46                         if (!empty($matches[1]))
47                                 $returnData[] = utf8_encode(trim($matches[1]));
48                 }
49
50                 return $returnData;
51         }
52
53         /**
54          * Returns suggestions of for a specific word.
55          *
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.
59          */
60         function &getSuggestions($lang, $word) {
61                 $cmd = $this->_getCMD($lang);
62
63         if (function_exists("mb_convert_encoding"))
64             $word = mb_convert_encoding($word, "ISO-8859-1", mb_detect_encoding($word, "UTF-8"));
65         else
66             $word = utf8_encode($word);
67
68                 if ($fh = fopen($this->_tmpfile, "w")) {
69                         fwrite($fh, "!\n");
70                         fwrite($fh, "^$word\n");
71                         fclose($fh);
72                 } else
73                         $this->throwError("Error opening tmp file.");
74
75                 $data = shell_exec($cmd);
76                 @unlink($this->_tmpfile);
77
78                 $returnData = array();
79                 $dataArr = preg_split("/\n/", $data, -1, PREG_SPLIT_NO_EMPTY);
80
81                 foreach($dataArr as $dstr) {
82                         $matches = array();
83
84                         // Skip this line.
85                         if (strpos($dstr, "@") === 0)
86                                 continue;
87
88                         preg_match("/\&[^:]+:(.*)/i", $dstr, $matches);
89
90                         if (!empty($matches[1])) {
91                                 $words = array_slice(explode(',', $matches[1]), 0, 10);
92
93                                 for ($i=0; $i<count($words); $i++)
94                                         $words[$i] = trim($words[$i]);
95
96                                 return $words;
97                         }
98                 }
99
100                 return array();
101         }
102
103         function _getCMD($lang) {
104                 $this->_tmpfile = tempnam($this->_config['PSpellShell.tmp'], "tinyspell");
105
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";
108
109                 return "cat ". $this->_tmpfile ." | " . $this->_config['PSpellShell.aspell'] . " -a --encoding=utf-8 -H --lang=". escapeshellarg($lang);
110         }
111 }
112
113 ?>