Update to tinyMCE spellchecker 1.0.3.1

git-svn-id: http://svn.automattic.com/wordpress/trunk@4985 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This commit is contained in:
ryan 2007-03-07 04:26:26 +00:00
parent 1f35a9de8f
commit b9c75476a4
8 changed files with 131 additions and 630 deletions

View File

@ -1,12 +1,10 @@
<?php
/* *
/* *
* Tiny Spelling Interface for TinyMCE Spell Checking.
*
* Copyright © 2006 Moxiecode Systems AB
*/
require_once("HttpClient.class.php");
class TinyGoogleSpell {
var $lang;
@ -22,11 +20,21 @@ class TinyGoogleSpell {
$matches = $this->_getMatches($wordstr);
for ($i=0; $i<count($matches); $i++)
$words[] = substr($wordstr, $matches[$i][1], $matches[$i][2]);
$words[] = $this->unhtmlentities(mb_substr($wordstr, $matches[$i][1], $matches[$i][2], "UTF-8"));
return $words;
}
function unhtmlentities($string) {
$string = preg_replace('~&#x([0-9a-f]+);~ei', 'chr(hexdec("\\1"))', $string);
$string = preg_replace('~&#([0-9]+);~e', 'chr(\\1)', $string);
$trans_tbl = get_html_translation_table(HTML_ENTITIES);
$trans_tbl = array_flip($trans_tbl);
return strtr($string, $trans_tbl);
}
// Returns array with suggestions or false if failed.
function getSuggestion($word) {
$sug = array();
@ -34,37 +42,78 @@ class TinyGoogleSpell {
$matches = $this->_getMatches($word);
if (count($matches) > 0)
$sug = explode("\t", $matches[0][4]);
$sug = explode("\t", utf8_encode($this->unhtmlentities($matches[0][4])));
return $sug;
}
function _getMatches($word_list) {
$xml = "";
function _xmlChars($string) {
$trans = get_html_translation_table(HTML_ENTITIES, ENT_QUOTES);
foreach ($trans as $k => $v)
$trans[$k] = "&#".ord($k).";";
// Setup HTTP Client
$client = new HttpClient('www.google.com');
$client->setUserAgent('Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR');
$client->setHandleRedirects(false);
$client->setDebug(false);
return strtr($string, $trans);
}
function _getMatches($word_list) {
$server = "www.google.com";
$port = 443;
$path = "/tbproxy/spell?lang=" . $this->lang . "&hl=en";
$host = "www.google.com";
$url = "https://" . $server;
// Setup XML request
$xml .= '<?xml version="1.0" encoding="utf-8" ?>';
$xml .= '<spellrequest textalreadyclipped="0" ignoredups="0" ignoredigits="1" ignoreallcaps="1">';
$xml .= '<text>' . htmlentities($word_list) . '</text></spellrequest>';
$xml = '<?xml version="1.0" encoding="utf-8" ?><spellrequest textalreadyclipped="0" ignoredups="0" ignoredigits="1" ignoreallcaps="1"><text>' . $word_list . '</text></spellrequest>';
// Execute HTTP Post to Google
if (!$client->post('/tbproxy/spell?lang=' . $this->lang, $xml)) {
$this->errorMsg[] = 'An error occurred: ' . $client->getError();
return array();
$header = "POST ".$path." HTTP/1.0 \r\n";
$header .= "MIME-Version: 1.0 \r\n";
$header .= "Content-type: application/PTI26 \r\n";
$header .= "Content-length: ".strlen($xml)." \r\n";
$header .= "Content-transfer-encoding: text \r\n";
$header .= "Request-number: 1 \r\n";
$header .= "Document-type: Request \r\n";
$header .= "Interface-Version: Test 1.4 \r\n";
$header .= "Connection: close \r\n\r\n";
$header .= $xml;
//$this->_debugData($xml);
// Use raw sockets
$fp = fsockopen("ssl://" . $server, $port, $errno, $errstr, 30);
if ($fp) {
// Send request
fwrite($fp, $header);
// Read response
$xml = "";
while (!feof($fp))
$xml .= fgets($fp, 128);
fclose($fp);
} else {
// Use curl
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $header);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
$xml = curl_exec($ch);
curl_close($ch);
}
//$this->_debugData($xml);
// Grab and parse content
$xml = $client->getContent();
preg_match_all('/<c o="([^"]*)" l="([^"]*)" s="([^"]*)">([^<]*)<\/c>/', $xml, $matches, PREG_SET_ORDER);
return $matches;
}
function _debugData($data) {
$fh = @fopen("debug.log", 'a+');
@fwrite($fh, $data);
@fclose($fh);
}
}
// Setup classname, should be the same as the name of the spellchecker class

View File

@ -6,6 +6,7 @@
*
*/
class TinyPspellShell {
var $lang;
var $mode;
@ -27,7 +28,11 @@ class TinyPspellShell {
$this->errorMsg = array();
$this->tmpfile = tempnam($config['tinypspellshell.tmp'], "tinyspell");
$this->cmd = "cat ". $this->tmpfile ." | " . $config['tinypspellshell.aspell'] . " -a --lang=". $this->lang;
if(preg_match("#win#i",php_uname()))
$this->cmd = $config['tinypspellshell.aspell'] . " -a --lang=". $this->lang." --encoding=utf-8 -H < $this->tmpfile 2>&1";
else
$this->cmd = "cat ". $this->tmpfile ." | " . $config['tinypspellshell.aspell'] . " -a --encoding=utf-8 -H --lang=". $this->lang;
}
// Returns array with bad words or false if failed.
@ -36,7 +41,6 @@ class TinyPspellShell {
fwrite($fh, "!\n");
foreach($wordArray as $key => $value)
fwrite($fh, "^" . $value . "\n");
fclose($fh);
} else {
$this->errorMsg[] = "PSpell not found.";
@ -44,7 +48,8 @@ class TinyPspellShell {
}
$data = shell_exec($this->cmd);
@unlink($this->tmpfile);
@unlink($this->tmpfile);
$returnData = array();
$dataArr = preg_split("/\n/", $data, -1, PREG_SPLIT_NO_EMPTY);
@ -66,15 +71,22 @@ class TinyPspellShell {
// Returns array with suggestions or false if failed.
function getSuggestion($word) {
if (function_exists("mb_convert_encoding"))
$word = mb_convert_encoding($word, "ISO-8859-1", mb_detect_encoding($word, "UTF-8"));
else
$word = utf8_encode($word);
if ($fh = fopen($this->tmpfile, "w")) {
fwrite($fh, "!\n");
fwrite($fh, "^$word\n");
fclose($fh);
} else
wp_die("Error opening tmp file.");
die("Error opening tmp file.");
$data = shell_exec($this->cmd);
@unlink($this->tmpfile);
@unlink($this->tmpfile);
$returnData = array();
$dataArr = preg_split("/\n/", $data, -1, PREG_SPLIT_NO_EMPTY);
@ -94,9 +106,16 @@ class TinyPspellShell {
}
return $returnData;
}
function _debugData($data) {
$fh = @fopen("debug.log", 'a+');
@fwrite($fh, $data);
@fclose($fh);
}
}
// Setup classname, should be the same as the name of the spellchecker class
$spellCheckerConfig['class'] = "TinyPspellShell";
?>
?>

View File

@ -1,13 +1,14 @@
<?php
$spellCheckerConfig = array();
// Spellchecker class use
// require_once("classes/TinyPspellShell.class.php"); // Command line pspell
require_once("classes/TinyGoogleSpell.class.php"); // Google web service
// require_once("classes/TinyPspell.class.php"); // Internal PHP version
// General settings
$spellCheckerConfig['enabled'] = true;
// Pspell shell specific settings
$spellCheckerConfig['tinypspellshell.aspell'] = '/usr/bin/aspell';
$spellCheckerConfig['tinypspellshell.tmp'] = '/tmp/tinyspell/0';
// Default settings
$spellCheckerConfig['default.language'] = 'en';
$spellCheckerConfig['default.mode'] = PSPELL_FAST;
@ -17,13 +18,7 @@
$spellCheckerConfig['default.jargon'] = "";
$spellCheckerConfig['default.encoding'] = "";
// Spellchecker class use
if ( function_exists('pspell_new') )
require_once("classes/TinyPspell.class.php"); // Internal PHP version
elseif ( file_exists($spellCheckerConfig['tinypspellshell.aspell']) )
require_once("classes/TinyPspellShell.class.php"); // Command line pspell
else
require_once("classes/TinyGoogleSpell.class.php"); // Google web service
// Pspell shell specific settings
$spellCheckerConfig['tinypspellshell.aspell'] = '/usr/bin/aspell';
$spellCheckerConfig['tinypspellshell.tmp'] = '/tmp';
?>

View File

@ -1,5 +1,4 @@
.mceItemHiddenSpellWord {
background: url('../images/wline.gif') repeat-x bottom left;
bo2rder-bottom: 1px dashed red;
cursor: default;
}

View File

@ -31,4 +31,5 @@
font-family: Arial, Verdana, Tahoma, Helvetica;
font-weight: bold;
font-size: 11px;
background-color: #FFF;
}

File diff suppressed because one or more lines are too long

View File

@ -10,5 +10,6 @@ tinyMCE.addToLang('spellchecker',{
swait : 'Spellchecking, please wait...',
sug : 'Suggestions',
no_sug : 'No suggestions',
no_mpell : 'No misspellings found.'
no_mpell : 'No misspellings found.',
mpell_found : 'Found {$words} misspellings.'
});

View File

@ -8,6 +8,9 @@
* @copyright Copyright © 2004-2006, Moxiecode Systems AB, All rights reserved.
*/
// Ignore the Notice errors for now.
error_reporting(E_ALL ^ E_NOTICE);
require_once("config.php");
$id = sanitize($_POST['id'], "loose");
@ -30,14 +33,14 @@
// Get input parameters.
$check = $_POST['check'];
$cmd = sanitize($_POST['cmd']);
$lang = sanitize($_POST['lang'], "strict");
$mode = sanitize($_POST['mode'], "strict");
$spelling = sanitize($_POST['spelling'], "strict");
$jargon = sanitize($_POST['jargon'], "strict");
$encoding = sanitize($_POST['encoding'], "strict");
$sg = sanitize($_POST['sg'], "bool");
$check = urldecode($_REQUEST['check']);
$cmd = sanitize($_REQUEST['cmd']);
$lang = sanitize($_REQUEST['lang'], "strict");
$mode = sanitize($_REQUEST['mode'], "strict");
$spelling = sanitize($_REQUEST['spelling'], "strict");
$jargon = sanitize($_REQUEST['jargon'], "strict");
$encoding = sanitize($_REQUEST['encoding'], "strict");
$sg = sanitize($_REQUEST['sg'], "bool");
$words = array();
$validRequest = true;
@ -90,9 +93,11 @@
$words = preg_split("/ |\n/", $check, -1, PREG_SPLIT_NO_EMPTY);
$result = $tinyspell->checkWords($words);
break;
case "suggest":
$result = $tinyspell->getSuggestion($check);
break;
default:
// Just use this for now.
$tinyspell->errorMsg[] = "No command.";
@ -109,19 +114,22 @@
switch($outputType) {
case "xml":
header('Content-type: text/xml; charset=utf-8');
echo '<?xml version="1.0" encoding="utf-8" ?>';
echo "\n";
$body = '<?xml version="1.0" encoding="utf-8" ?>';
$body .= "\n";
if (count($result) == 0)
echo '<res id="' . $id . '" cmd="'. $cmd .'" />';
$body .= '<res id="' . $id . '" cmd="'. $cmd .'" />';
else
echo '<res id="' . $id . '" cmd="'. $cmd .'">'. utf8_encode(implode(" ", $result)) .'</res>';
$body .= '<res id="' . $id . '" cmd="'. $cmd .'">'. urlencode(implode(" ", $result)) .'</res>';
echo $body;
break;
case "xmlerror";
header('Content-type: text/xml; charset=utf-8');
echo '<?xml version="1.0" encoding="utf-8" ?>';
echo "\n";
echo '<res id="' . $id . '" cmd="'. $cmd .'" error="true" msg="'. implode(" ", $tinyspell->errorMsg) .'" />';
$body = '<?xml version="1.0" encoding="utf-8" ?>';
$body .= "\n";
$body .= '<res id="' . $id . '" cmd="'. $cmd .'" error="true" msg="'. implode(" ", $tinyspell->errorMsg) .'" />';
echo $body;
break;
case "html":
var_dump($result);
@ -130,4 +138,5 @@
echo "Error";
break;
}
?>
?>