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:
parent
1f35a9de8f
commit
b9c75476a4
|
@ -5,8 +5,6 @@
|
||||||
* Copyright © 2006 Moxiecode Systems AB
|
* Copyright © 2006 Moxiecode Systems AB
|
||||||
*/
|
*/
|
||||||
|
|
||||||
require_once("HttpClient.class.php");
|
|
||||||
|
|
||||||
class TinyGoogleSpell {
|
class TinyGoogleSpell {
|
||||||
var $lang;
|
var $lang;
|
||||||
|
|
||||||
|
@ -22,11 +20,21 @@ class TinyGoogleSpell {
|
||||||
$matches = $this->_getMatches($wordstr);
|
$matches = $this->_getMatches($wordstr);
|
||||||
|
|
||||||
for ($i=0; $i<count($matches); $i++)
|
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;
|
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.
|
// Returns array with suggestions or false if failed.
|
||||||
function getSuggestion($word) {
|
function getSuggestion($word) {
|
||||||
$sug = array();
|
$sug = array();
|
||||||
|
@ -34,37 +42,78 @@ class TinyGoogleSpell {
|
||||||
$matches = $this->_getMatches($word);
|
$matches = $this->_getMatches($word);
|
||||||
|
|
||||||
if (count($matches) > 0)
|
if (count($matches) > 0)
|
||||||
$sug = explode("\t", $matches[0][4]);
|
$sug = explode("\t", utf8_encode($this->unhtmlentities($matches[0][4])));
|
||||||
|
|
||||||
return $sug;
|
return $sug;
|
||||||
}
|
}
|
||||||
|
|
||||||
function _getMatches($word_list) {
|
function _xmlChars($string) {
|
||||||
$xml = "";
|
$trans = get_html_translation_table(HTML_ENTITIES, ENT_QUOTES);
|
||||||
|
|
||||||
// Setup HTTP Client
|
foreach ($trans as $k => $v)
|
||||||
$client = new HttpClient('www.google.com');
|
$trans[$k] = "&#".ord($k).";";
|
||||||
$client->setUserAgent('Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR');
|
|
||||||
$client->setHandleRedirects(false);
|
|
||||||
$client->setDebug(false);
|
|
||||||
|
|
||||||
// Setup XML request
|
return strtr($string, $trans);
|
||||||
$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>';
|
|
||||||
|
|
||||||
// Execute HTTP Post to Google
|
|
||||||
if (!$client->post('/tbproxy/spell?lang=' . $this->lang, $xml)) {
|
|
||||||
$this->errorMsg[] = 'An error occurred: ' . $client->getError();
|
|
||||||
return array();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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" ?><spellrequest textalreadyclipped="0" ignoredups="0" ignoredigits="1" ignoreallcaps="1"><text>' . $word_list . '</text></spellrequest>';
|
||||||
|
|
||||||
|
$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
|
// Grab and parse content
|
||||||
$xml = $client->getContent();
|
|
||||||
preg_match_all('/<c o="([^"]*)" l="([^"]*)" s="([^"]*)">([^<]*)<\/c>/', $xml, $matches, PREG_SET_ORDER);
|
preg_match_all('/<c o="([^"]*)" l="([^"]*)" s="([^"]*)">([^<]*)<\/c>/', $xml, $matches, PREG_SET_ORDER);
|
||||||
|
|
||||||
return $matches;
|
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
|
// Setup classname, should be the same as the name of the spellchecker class
|
||||||
|
|
|
@ -6,6 +6,7 @@
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
|
||||||
class TinyPspellShell {
|
class TinyPspellShell {
|
||||||
var $lang;
|
var $lang;
|
||||||
var $mode;
|
var $mode;
|
||||||
|
@ -27,7 +28,11 @@ class TinyPspellShell {
|
||||||
$this->errorMsg = array();
|
$this->errorMsg = array();
|
||||||
|
|
||||||
$this->tmpfile = tempnam($config['tinypspellshell.tmp'], "tinyspell");
|
$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.
|
// Returns array with bad words or false if failed.
|
||||||
|
@ -36,7 +41,6 @@ class TinyPspellShell {
|
||||||
fwrite($fh, "!\n");
|
fwrite($fh, "!\n");
|
||||||
foreach($wordArray as $key => $value)
|
foreach($wordArray as $key => $value)
|
||||||
fwrite($fh, "^" . $value . "\n");
|
fwrite($fh, "^" . $value . "\n");
|
||||||
|
|
||||||
fclose($fh);
|
fclose($fh);
|
||||||
} else {
|
} else {
|
||||||
$this->errorMsg[] = "PSpell not found.";
|
$this->errorMsg[] = "PSpell not found.";
|
||||||
|
@ -45,6 +49,7 @@ class TinyPspellShell {
|
||||||
|
|
||||||
$data = shell_exec($this->cmd);
|
$data = shell_exec($this->cmd);
|
||||||
@unlink($this->tmpfile);
|
@unlink($this->tmpfile);
|
||||||
|
|
||||||
$returnData = array();
|
$returnData = array();
|
||||||
$dataArr = preg_split("/\n/", $data, -1, PREG_SPLIT_NO_EMPTY);
|
$dataArr = preg_split("/\n/", $data, -1, PREG_SPLIT_NO_EMPTY);
|
||||||
|
|
||||||
|
@ -66,15 +71,22 @@ class TinyPspellShell {
|
||||||
|
|
||||||
// Returns array with suggestions or false if failed.
|
// Returns array with suggestions or false if failed.
|
||||||
function getSuggestion($word) {
|
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")) {
|
if ($fh = fopen($this->tmpfile, "w")) {
|
||||||
fwrite($fh, "!\n");
|
fwrite($fh, "!\n");
|
||||||
fwrite($fh, "^$word\n");
|
fwrite($fh, "^$word\n");
|
||||||
fclose($fh);
|
fclose($fh);
|
||||||
} else
|
} else
|
||||||
wp_die("Error opening tmp file.");
|
die("Error opening tmp file.");
|
||||||
|
|
||||||
$data = shell_exec($this->cmd);
|
$data = shell_exec($this->cmd);
|
||||||
|
|
||||||
@unlink($this->tmpfile);
|
@unlink($this->tmpfile);
|
||||||
|
|
||||||
$returnData = array();
|
$returnData = array();
|
||||||
$dataArr = preg_split("/\n/", $data, -1, PREG_SPLIT_NO_EMPTY);
|
$dataArr = preg_split("/\n/", $data, -1, PREG_SPLIT_NO_EMPTY);
|
||||||
|
|
||||||
|
@ -94,6 +106,13 @@ class TinyPspellShell {
|
||||||
}
|
}
|
||||||
return $returnData;
|
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
|
// Setup classname, should be the same as the name of the spellchecker class
|
||||||
|
|
|
@ -1,13 +1,14 @@
|
||||||
<?php
|
<?php
|
||||||
$spellCheckerConfig = array();
|
$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
|
// General settings
|
||||||
$spellCheckerConfig['enabled'] = true;
|
$spellCheckerConfig['enabled'] = true;
|
||||||
|
|
||||||
// Pspell shell specific settings
|
|
||||||
$spellCheckerConfig['tinypspellshell.aspell'] = '/usr/bin/aspell';
|
|
||||||
$spellCheckerConfig['tinypspellshell.tmp'] = '/tmp/tinyspell/0';
|
|
||||||
|
|
||||||
// Default settings
|
// Default settings
|
||||||
$spellCheckerConfig['default.language'] = 'en';
|
$spellCheckerConfig['default.language'] = 'en';
|
||||||
$spellCheckerConfig['default.mode'] = PSPELL_FAST;
|
$spellCheckerConfig['default.mode'] = PSPELL_FAST;
|
||||||
|
@ -17,13 +18,7 @@
|
||||||
$spellCheckerConfig['default.jargon'] = "";
|
$spellCheckerConfig['default.jargon'] = "";
|
||||||
$spellCheckerConfig['default.encoding'] = "";
|
$spellCheckerConfig['default.encoding'] = "";
|
||||||
|
|
||||||
// Spellchecker class use
|
// Pspell shell specific settings
|
||||||
if ( function_exists('pspell_new') )
|
$spellCheckerConfig['tinypspellshell.aspell'] = '/usr/bin/aspell';
|
||||||
require_once("classes/TinyPspell.class.php"); // Internal PHP version
|
$spellCheckerConfig['tinypspellshell.tmp'] = '/tmp';
|
||||||
|
|
||||||
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
|
|
||||||
?>
|
?>
|
||||||
|
|
|
@ -1,5 +1,4 @@
|
||||||
.mceItemHiddenSpellWord {
|
.mceItemHiddenSpellWord {
|
||||||
background: url('../images/wline.gif') repeat-x bottom left;
|
background: url('../images/wline.gif') repeat-x bottom left;
|
||||||
bo2rder-bottom: 1px dashed red;
|
|
||||||
cursor: default;
|
cursor: default;
|
||||||
}
|
}
|
||||||
|
|
|
@ -31,4 +31,5 @@
|
||||||
font-family: Arial, Verdana, Tahoma, Helvetica;
|
font-family: Arial, Verdana, Tahoma, Helvetica;
|
||||||
font-weight: bold;
|
font-weight: bold;
|
||||||
font-size: 11px;
|
font-size: 11px;
|
||||||
|
background-color: #FFF;
|
||||||
}
|
}
|
||||||
|
|
File diff suppressed because one or more lines are too long
|
@ -10,5 +10,6 @@ tinyMCE.addToLang('spellchecker',{
|
||||||
swait : 'Spellchecking, please wait...',
|
swait : 'Spellchecking, please wait...',
|
||||||
sug : 'Suggestions',
|
sug : 'Suggestions',
|
||||||
no_sug : 'No suggestions',
|
no_sug : 'No suggestions',
|
||||||
no_mpell : 'No misspellings found.'
|
no_mpell : 'No misspellings found.',
|
||||||
|
mpell_found : 'Found {$words} misspellings.'
|
||||||
});
|
});
|
||||||
|
|
|
@ -8,6 +8,9 @@
|
||||||
* @copyright Copyright © 2004-2006, Moxiecode Systems AB, All rights reserved.
|
* @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");
|
require_once("config.php");
|
||||||
|
|
||||||
$id = sanitize($_POST['id'], "loose");
|
$id = sanitize($_POST['id'], "loose");
|
||||||
|
@ -30,14 +33,14 @@
|
||||||
|
|
||||||
// Get input parameters.
|
// Get input parameters.
|
||||||
|
|
||||||
$check = $_POST['check'];
|
$check = urldecode($_REQUEST['check']);
|
||||||
$cmd = sanitize($_POST['cmd']);
|
$cmd = sanitize($_REQUEST['cmd']);
|
||||||
$lang = sanitize($_POST['lang'], "strict");
|
$lang = sanitize($_REQUEST['lang'], "strict");
|
||||||
$mode = sanitize($_POST['mode'], "strict");
|
$mode = sanitize($_REQUEST['mode'], "strict");
|
||||||
$spelling = sanitize($_POST['spelling'], "strict");
|
$spelling = sanitize($_REQUEST['spelling'], "strict");
|
||||||
$jargon = sanitize($_POST['jargon'], "strict");
|
$jargon = sanitize($_REQUEST['jargon'], "strict");
|
||||||
$encoding = sanitize($_POST['encoding'], "strict");
|
$encoding = sanitize($_REQUEST['encoding'], "strict");
|
||||||
$sg = sanitize($_POST['sg'], "bool");
|
$sg = sanitize($_REQUEST['sg'], "bool");
|
||||||
$words = array();
|
$words = array();
|
||||||
|
|
||||||
$validRequest = true;
|
$validRequest = true;
|
||||||
|
@ -90,9 +93,11 @@
|
||||||
$words = preg_split("/ |\n/", $check, -1, PREG_SPLIT_NO_EMPTY);
|
$words = preg_split("/ |\n/", $check, -1, PREG_SPLIT_NO_EMPTY);
|
||||||
$result = $tinyspell->checkWords($words);
|
$result = $tinyspell->checkWords($words);
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case "suggest":
|
case "suggest":
|
||||||
$result = $tinyspell->getSuggestion($check);
|
$result = $tinyspell->getSuggestion($check);
|
||||||
break;
|
break;
|
||||||
|
|
||||||
default:
|
default:
|
||||||
// Just use this for now.
|
// Just use this for now.
|
||||||
$tinyspell->errorMsg[] = "No command.";
|
$tinyspell->errorMsg[] = "No command.";
|
||||||
|
@ -109,19 +114,22 @@
|
||||||
switch($outputType) {
|
switch($outputType) {
|
||||||
case "xml":
|
case "xml":
|
||||||
header('Content-type: text/xml; charset=utf-8');
|
header('Content-type: text/xml; charset=utf-8');
|
||||||
echo '<?xml version="1.0" encoding="utf-8" ?>';
|
$body = '<?xml version="1.0" encoding="utf-8" ?>';
|
||||||
echo "\n";
|
$body .= "\n";
|
||||||
if (count($result) == 0)
|
|
||||||
echo '<res id="' . $id . '" cmd="'. $cmd .'" />';
|
|
||||||
else
|
|
||||||
echo '<res id="' . $id . '" cmd="'. $cmd .'">'. utf8_encode(implode(" ", $result)) .'</res>';
|
|
||||||
|
|
||||||
|
if (count($result) == 0)
|
||||||
|
$body .= '<res id="' . $id . '" cmd="'. $cmd .'" />';
|
||||||
|
else
|
||||||
|
$body .= '<res id="' . $id . '" cmd="'. $cmd .'">'. urlencode(implode(" ", $result)) .'</res>';
|
||||||
|
|
||||||
|
echo $body;
|
||||||
break;
|
break;
|
||||||
case "xmlerror";
|
case "xmlerror";
|
||||||
header('Content-type: text/xml; charset=utf-8');
|
header('Content-type: text/xml; charset=utf-8');
|
||||||
echo '<?xml version="1.0" encoding="utf-8" ?>';
|
$body = '<?xml version="1.0" encoding="utf-8" ?>';
|
||||||
echo "\n";
|
$body .= "\n";
|
||||||
echo '<res id="' . $id . '" cmd="'. $cmd .'" error="true" msg="'. implode(" ", $tinyspell->errorMsg) .'" />';
|
$body .= '<res id="' . $id . '" cmd="'. $cmd .'" error="true" msg="'. implode(" ", $tinyspell->errorMsg) .'" />';
|
||||||
|
echo $body;
|
||||||
break;
|
break;
|
||||||
case "html":
|
case "html":
|
||||||
var_dump($result);
|
var_dump($result);
|
||||||
|
@ -130,4 +138,5 @@
|
||||||
echo "Error";
|
echo "Error";
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
?>
|
?>
|
||||||
|
|
Loading…
Reference in New Issue