From 7e645f9a6a8e0cba89cd2058643f7f419a79eaa2 Mon Sep 17 00:00:00 2001 From: DougMac Date: Sat, 17 Aug 2013 10:50:26 +0100 Subject: [PATCH 1/2] Fixes: U4-2060 Spell Checker Broke After Google Update The Google spell check at https://www.google.com/tbproxy/spell no longer works. This updates to a newer Google Api (again undocumented) --- .../plugins/tinymce3/GoogleSpellChecker.cs | 65 ++++++++++--------- .../plugins/tinymce3/SpellCheckerResult.cs | 30 +++++++++ 2 files changed, 66 insertions(+), 29 deletions(-) diff --git a/src/Umbraco.Web/umbraco.presentation/umbraco/plugins/tinymce3/GoogleSpellChecker.cs b/src/Umbraco.Web/umbraco.presentation/umbraco/plugins/tinymce3/GoogleSpellChecker.cs index 42bf22e5fe..edaa5950c4 100644 --- a/src/Umbraco.Web/umbraco.presentation/umbraco/plugins/tinymce3/GoogleSpellChecker.cs +++ b/src/Umbraco.Web/umbraco.presentation/umbraco/plugins/tinymce3/GoogleSpellChecker.cs @@ -5,18 +5,27 @@ using System.Web.Script.Serialization; using System.Net; using System.Text; using System.Xml; +using System.Collections.Generic; // NB: This class was moved out of the client tinymce folder to aid with upgrades // but we'll keep the old namespace to make things easier for now (MB) namespace umbraco.presentation.umbraco_client.tinymce3.plugins.spellchecker { public class GoogleSpellChecker : SpellChecker, IHttpHandler - { + { private static string SendRequest(string lang, string data) { string googleResponse; - string requestUriString = string.Format("https://www.google.com:443/tbproxy/spell?lang={0}&hl={0}", lang); - string s = string.Format("{0}", HttpContext.Current.Server.UrlEncode(data)); + string requestUriString = "https://www.googleapis.com:443/rpc"; + var requestData = new Dictionary(); + var requestParams = new Dictionary(); + requestParams.Add("language", lang); + requestParams.Add("text", data); + requestParams.Add("key", "AIzaSyCLlKc60a3z7lo8deV-hAyDU7rHYgL4HZg"); + requestData.Add("method", "spelling.check"); + requestData.Add("apiVersion", "v2"); + requestData.Add("params", requestParams); + string jsonString = new JavaScriptSerializer().Serialize(requestData); StreamReader reader = null; HttpWebResponse response = null; Stream requestStream = null; @@ -25,15 +34,10 @@ namespace umbraco.presentation.umbraco_client.tinymce3.plugins.spellchecker HttpWebRequest request = (HttpWebRequest)WebRequest.Create(requestUriString); request.KeepAlive = false; request.Method = "POST"; - request.ContentType = "application/PTI26"; - request.ContentLength = s.Length; - WebHeaderCollection headers = request.Headers; - headers.Add("MIME-Version: 1.0"); - headers.Add("Request-number: 1"); - headers.Add("Document-type: Request"); - headers.Add("Interface-Version: Test 1.4"); + request.ContentType = "application/json"; + request.ContentLength = jsonString.Length; requestStream = request.GetRequestStream(); - byte[] bytes = new ASCIIEncoding().GetBytes(s); + byte[] bytes = new ASCIIEncoding().GetBytes(jsonString); requestStream.Write(bytes, 0, bytes.Length); response = (HttpWebResponse)request.GetResponse(); reader = new StreamReader(response.GetResponseStream()); @@ -64,18 +68,20 @@ namespace umbraco.presentation.umbraco_client.tinymce3.plugins.spellchecker /// The words to be checked. /// public override SpellCheckerResult CheckWords(string language, string[] words) - { - XmlDocument document = new XmlDocument(); + { string data = string.Join(" ", words); //turn them into a space-separated string as that's what google takes - string xml = SendRequest(language, data); - document.LoadXml(xml); + string json = SendRequest(language, data); + var jsonRes = new JavaScriptSerializer().Deserialize(json); - var res = new SpellCheckerResult(); - foreach (XmlNode node in document.SelectNodes("//c")) //go through each of the incorrectly spelt words + var res = new SpellCheckerResult(); + // Get list of misspelled words + if (jsonRes.result != null && jsonRes.result.spellingCheckResponse != null) { - XmlElement element = (XmlElement)node; - res.result.Add(data.Substring(Convert.ToInt32(element.GetAttribute("o")), Convert.ToInt32(element.GetAttribute("l")))); - } + foreach (var misspelling in jsonRes.result.spellingCheckResponse.misspellings) + { + res.result.Add(data.Substring(misspelling.charStart, misspelling.charLength)); + } + } return res; } @@ -87,22 +93,23 @@ namespace umbraco.presentation.umbraco_client.tinymce3.plugins.spellchecker /// The word that is misspelt. /// public override SpellCheckerResult GetSuggestions(string language, string word) - { - XmlDocument document = new XmlDocument(); - string xml = SendRequest(language, word); - document.LoadXml(xml); + { + string json = SendRequest(language, word); + var jsonRes = new JavaScriptSerializer().Deserialize(json); + var res = new SpellCheckerResult(); - foreach (XmlNode node in document.SelectNodes("//c")) //select each incorrectly spelt work + // Get list of suggestions + if (jsonRes.result != null && jsonRes.result.spellingCheckResponse != null) { - XmlElement element = (XmlElement)node; - foreach (string s in element.InnerText.Split(new char[] { '\t' })) //they are tab-separated for suggestions + foreach (var misspelling in jsonRes.result.spellingCheckResponse.misspellings) { - if (!string.IsNullOrEmpty(s)) + foreach (var suggestion in misspelling.suggestions) { - res.result.Add(s); + res.result.Add(suggestion.suggestion); } } } + return res; } diff --git a/src/Umbraco.Web/umbraco.presentation/umbraco/plugins/tinymce3/SpellCheckerResult.cs b/src/Umbraco.Web/umbraco.presentation/umbraco/plugins/tinymce3/SpellCheckerResult.cs index 1372629cf8..c6d16e903c 100644 --- a/src/Umbraco.Web/umbraco.presentation/umbraco/plugins/tinymce3/SpellCheckerResult.cs +++ b/src/Umbraco.Web/umbraco.presentation/umbraco/plugins/tinymce3/SpellCheckerResult.cs @@ -32,4 +32,34 @@ namespace umbraco.presentation.umbraco_client.tinymce3.plugins.spellchecker /// The error. public string error { get; set; } } + + /// + /// Object used to deserialise the Google Json response + /// + public class JsonSpellCheckerResult + { + public GoogleResponseResult result { get; set; } + + public class GoogleResponseResult + { + public GoogleResponseSpellingCheckResponse spellingCheckResponse { get; set; } + + } + public class GoogleResponseSpellingCheckResponse + { + public List misspellings { get; set; } + + } + public class GoogleResponseMisspelling + { + public int charStart { get; set; } + public int charLength { get; set; } + public List suggestions { get; set; } + + } + public class GoogleResponseSuggestion + { + public string suggestion { get; set; } + } + } } From 0218e39664dbd0adce3bf18898a5e7ce0d16138f Mon Sep 17 00:00:00 2001 From: DougMac Date: Mon, 26 Aug 2013 22:50:53 +0100 Subject: [PATCH 2/2] Fixes: U4-2060 Spell Checker Broke After Google Update --- .../plugins/tinymce3/GoogleSpellChecker.cs | 20 +++++++++---------- .../plugins/tinymce3/SpellCheckerResult.cs | 2 +- 2 files changed, 11 insertions(+), 11 deletions(-) diff --git a/src/Umbraco.Web/umbraco.presentation/umbraco/plugins/tinymce3/GoogleSpellChecker.cs b/src/Umbraco.Web/umbraco.presentation/umbraco/plugins/tinymce3/GoogleSpellChecker.cs index edaa5950c4..ef3a6376b0 100644 --- a/src/Umbraco.Web/umbraco.presentation/umbraco/plugins/tinymce3/GoogleSpellChecker.cs +++ b/src/Umbraco.Web/umbraco.presentation/umbraco/plugins/tinymce3/GoogleSpellChecker.cs @@ -12,7 +12,7 @@ using System.Collections.Generic; namespace umbraco.presentation.umbraco_client.tinymce3.plugins.spellchecker { public class GoogleSpellChecker : SpellChecker, IHttpHandler - { + { private static string SendRequest(string lang, string data) { string googleResponse; @@ -25,7 +25,7 @@ namespace umbraco.presentation.umbraco_client.tinymce3.plugins.spellchecker requestData.Add("method", "spelling.check"); requestData.Add("apiVersion", "v2"); requestData.Add("params", requestParams); - string jsonString = new JavaScriptSerializer().Serialize(requestData); + string jsonString = new JavaScriptSerializer().Serialize(requestData); StreamReader reader = null; HttpWebResponse response = null; Stream requestStream = null; @@ -35,7 +35,7 @@ namespace umbraco.presentation.umbraco_client.tinymce3.plugins.spellchecker request.KeepAlive = false; request.Method = "POST"; request.ContentType = "application/json"; - request.ContentLength = jsonString.Length; + request.ContentLength = jsonString.Length; requestStream = request.GetRequestStream(); byte[] bytes = new ASCIIEncoding().GetBytes(jsonString); requestStream.Write(bytes, 0, bytes.Length); @@ -68,20 +68,20 @@ namespace umbraco.presentation.umbraco_client.tinymce3.plugins.spellchecker /// The words to be checked. /// public override SpellCheckerResult CheckWords(string language, string[] words) - { + { string data = string.Join(" ", words); //turn them into a space-separated string as that's what google takes string json = SendRequest(language, data); var jsonRes = new JavaScriptSerializer().Deserialize(json); - var res = new SpellCheckerResult(); + var res = new SpellCheckerResult(); // Get list of misspelled words if (jsonRes.result != null && jsonRes.result.spellingCheckResponse != null) { foreach (var misspelling in jsonRes.result.spellingCheckResponse.misspellings) - { + { res.result.Add(data.Substring(misspelling.charStart, misspelling.charLength)); } - } + } return res; } @@ -93,12 +93,12 @@ namespace umbraco.presentation.umbraco_client.tinymce3.plugins.spellchecker /// The word that is misspelt. /// public override SpellCheckerResult GetSuggestions(string language, string word) - { + { string json = SendRequest(language, word); var jsonRes = new JavaScriptSerializer().Deserialize(json); var res = new SpellCheckerResult(); - // Get list of suggestions + // Get list of suggestions if (jsonRes.result != null && jsonRes.result.spellingCheckResponse != null) { foreach (var misspelling in jsonRes.result.spellingCheckResponse.misspellings) @@ -138,7 +138,7 @@ namespace umbraco.presentation.umbraco_client.tinymce3.plugins.spellchecker suggestions = new SpellCheckerResult(); break; } - + suggestions.id = input.Id; JavaScriptSerializer ser = new JavaScriptSerializer(); var res = ser.Serialize(suggestions); diff --git a/src/Umbraco.Web/umbraco.presentation/umbraco/plugins/tinymce3/SpellCheckerResult.cs b/src/Umbraco.Web/umbraco.presentation/umbraco/plugins/tinymce3/SpellCheckerResult.cs index c6d16e903c..fc5f7a0bfd 100644 --- a/src/Umbraco.Web/umbraco.presentation/umbraco/plugins/tinymce3/SpellCheckerResult.cs +++ b/src/Umbraco.Web/umbraco.presentation/umbraco/plugins/tinymce3/SpellCheckerResult.cs @@ -60,6 +60,6 @@ namespace umbraco.presentation.umbraco_client.tinymce3.plugins.spellchecker public class GoogleResponseSuggestion { public string suggestion { get; set; } - } + } } }