Commit d4df7fb8 authored by Rebecca Blundell's avatar Rebecca Blundell Committed by Rebecca Blundell

Bug 1840112: Tinymce - spellchecker changes

Add optional plugin for non-browser based spellchecker
as set in $CFG->spellcheckerengine

behatnotneeded

Change-Id: I14e567fda9fd5ab5d751917e9cbbe4e4b065ba9a
parent ada3e2cd
......@@ -10,7 +10,7 @@ Changes:
* Added all of the language packs (original commit: 259a86431ad1140e15288d39702cea885c08f3fe)
* Removed all .min.js and .dev.js files
* Added code to spellchecker/spellchecker.rpc to use $CFG->spellcheckerengine (original commit: a42e68b43e4a63db19569facae61898af1b86d3f) - not done
* Adding the 'aria-hidden="true"' to presentation icons (original commit: 4ac89c31a719a9f63be6555c4d1a094fd2b09d0e)- not done
* Bug 1605110: Modified the "table" plugin to style the table through CSS instead of HTML attributes (original commit: 63f91a7ea8bd60c102e257c05678cc6d55e21683) -not done
* Add TinyMCE imagebrowser plugin: plugins/imagebrowser/plugin.js
......@@ -18,32 +18,26 @@ Changes:
-can't find this.
* Removed files and folders
tinymce.jquery.js removed in v4.3.4
theme/inlite
theme/mobile
skins/lightgray
plugins/help
* Removed 'Powered by Tinymce' text from the modern theme - not found
tinymce.jquery.js removed in v4.3.4
theme/inlite
theme/mobile
skins/lightgray
plugins/help
min.js from plugin folders
skins/content
* Added
plugins/imagebrowser (originally added in commit 36bc9f7d26076d74ebc6cd0672441eed8ed9cd2d)
plugins/mathslate (originally added in commit 325cdfac3dfba40efbbad8cd306dcfb385674709)
plugins/tooltoggle (originally added in commit 2c490a70bfff677c0b95c0565d39162c8c9bc10d)
skins/light: not included in new versions of tinyMCE any more
download from https://github.com/instructure/tinymce-light-skin
v1.3.1
added langs
removed min.js from plugin folders
-plugins/imagebrowser (originally added in commit 36bc9f7d26076d74ebc6cd0672441eed8ed9cd2d)
This is old. The new one is a pay service.
-plugins/mathslate (originally added in commit 325cdfac3dfba40efbbad8cd306dcfb385674709)
-plugins/tooltoggle (originally added in commit 2c490a70bfff677c0b95c0565d39162c8c9bc10d)
-plugins/spellchecker to use $CFG->spellcheckerengine
if that is enabled instead of default browser spellchecker.
(original commit: a42e68b43e4a63db19569facae61898af1b86d3f)
removed
modern theme replaced by silver - fixed.
added tooltoggle back and changed function calls to new
imagebrowser. Added old one. New one is now a pay service
added mathslate from version 4, but need to test????
quickbars is new, what does it do?
removed skins/content
see if can make light skin work.
see what's happened with spellchecker. Add back in? replaced with sth else?
Version 4.0 (2014-04-16)
Initial version of the spellchecker for TinyMCE 4.0 supports Enchant and PSpell engines.
Add your dictionary files here for example en_GB.dic and en_GB.aff.
<?php
/**
* EnchantEngine.php
*
* Copyright, Moxiecode Systems AB
* Released under LGPL License.
*
* License: http://www.tinymce.com/license
* Contributing: http://www.tinymce.com/contributing
*/
class TinyMCE_SpellChecker_EnchantEngine extends TinyMCE_SpellChecker_Engine {
/**
* Spellchecks an array of words.
*
* @param String $lang Selected language code (like en_US or de_DE). Shortcodes like "en" and "de" work with enchant >= 1.4.1
* @param Array $words Array of words to check.
* @return Name/value object with arrays of suggestions.
*/
public function getSuggestions($lang, $words) {
$suggestions = array();
$enchant = enchant_broker_init();
$config = $this->getConfig();
if (isset($config["enchant_dicts_path"])) {
enchant_broker_set_dict_path($enchant, ENCHANT_MYSPELL, $config["enchant_dicts_path"]);
enchant_broker_set_dict_path($enchant, ENCHANT_ISPELL, $config["enchant_dicts_path"]);
}
if (!enchant_broker_describe($enchant)) {
throw new Exception("Enchant spellchecker not find any backends.");
}
$lang = $this->normalizeLangCode($enchant, $lang);
if (enchant_broker_dict_exists($enchant, $lang)) {
$dict = enchant_broker_request_dict($enchant, $lang);
foreach ($words as $word) {
if (!enchant_dict_check($dict, $word)) {
$suggs = enchant_dict_suggest($dict, $word);
if (!is_array($suggs)) {
$suggs = array();
}
$suggestions[$word] = $suggs;
}
}
enchant_broker_free_dict($dict);
enchant_broker_free($enchant);
} else {
enchant_broker_free($enchant);
throw new Exception("Enchant spellchecker could not find dictionary for language: " . $lang);
}
return $suggestions;
}
/**
* Return true/false if the engine is supported by the server.
*
* @return boolean True/false if the engine is supported.
*/
public function isSupported() {
return function_exists("enchant_broker_init");
}
private function normalizeLangCode($enchant, $lang) {
$variants = array(
"en" => array("en_US", "en_GB")
);
if (isset($variants[$lang])) {
array_unshift($variants, $lang);
foreach ($variants[$lang] as $variant) {
if (enchant_broker_dict_exists($enchant, $variant)) {
return $variant;
}
}
}
return $lang;
}
}
TinyMCE_Spellchecker_Engine::add("enchant", "TinyMCE_SpellChecker_EnchantEngine");
?>
<?php
/**
* Engine.php
*
* Copyright, Moxiecode Systems AB
* Released under LGPL License.
*
* License: http://www.tinymce.com/license
* Contributing: http://www.tinymce.com/contributing
*
* Base class for all spellcheckers this takes in the words to check
* spelling on and returns the suggestions.
*/
class TinyMCE_SpellChecker_Engine {
private static $engines = array();
private $config;
public function __constructor($config) {
$this->config = $config;
}
/**
* Spellchecks an array of words.
*
* @param String $lang Selected language code (like en_US or de_DE). Shortcodes like "en" and "de" work with enchant >= 1.4.1
* @param Array $words Array of words to check.
* @return Name/value object with arrays of suggestions.
*/
public function getSuggestions($lang, $words) {
return array();
}
/**
* Return true/false if the engine is supported by the server.
*
* @return boolean True/false if the engine is supported.
*/
public function isSupported() {
return true;
}
/**
* Sets the config array used to create the instance.
*
* @param Array $config Name/value array with config options.
*/
public function setConfig($config) {
$this->config = $config;
}
/**
* Returns the config array used to create the instance.
*
* @return Array Name/value array with config options.
*/
public function getConfig() {
return $this->config;
}
// Static methods
public static function processRequest($tinymceSpellcheckerConfig) {
$engine = self::get($tinymceSpellcheckerConfig["engine"]);
$engine = new $engine();
$engine->setConfig($tinymceSpellcheckerConfig);
header('Content-Type: application/json');
header('Content-Encoding: UTF-8');
header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");
header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");
header("Cache-Control: no-store, no-cache, must-revalidate");
header("Cache-Control: post-check=0, pre-check=0", false);
header("Pragma: no-cache");
$method = self::getParam("method", "spellcheck");
$lang = self::getParam("lang", "en_US");
$text = self::getParam("text");
if ($method == "spellcheck") {
try {
if (!$text) {
throw new Exception("Missing input parameter 'text'.");
}
if (!$engine->isSupported()) {
// log_debug("err");
throw new Exception("Current spellchecker isn't supported.");
}
$words = self::getWords($text);
echo json_encode((object) array(
"words" => (object) $engine->getSuggestions($lang, $words)
));
} catch (Exception $e) {
echo json_encode((object) array(
"error" => $e->getMessage()
));
}
} else {
echo json_encode((object) array(
"error" => "Invalid JSON input"
));
}
}
/**
* Returns an request value by name without magic quoting.
*
* @param String $name Name of parameter to get.
* @param String $default_value Default value to return if value not found.
* @return String request value by name without magic quoting or default value.
*/
public static function getParam($name, $default_value = false) {
if (isset($_POST[$name])) {
$req = $_POST;
} else if (isset($_GET[$name])) {
$req = $_GET;
} else {
return $default_value;
}
// Handle magic quotes
if (ini_get("magic_quotes_gpc")) {
if (is_array($req[$name])) {
$out = array();
foreach ($req[$name] as $name => $value) {
$out[stripslashes($name)] = stripslashes($value);
}
return $out;
}
return stripslashes($req[$name]);
}
return $req[$name];
}
public static function add($name, $className) {
self::$engines[$name] = $className;
}
public static function get($name) {
if (!isset(self::$engines[$name])) {
return null;
}
return self::$engines[$name];
}
public static function getWords($text) {
preg_match_all('(\w{3,})u', $text, $matches);
$words = $matches[0];
for ($i = count($words) - 1; $i >= 0; $i--) {
// Exclude words with numbers in them
if (preg_match('/[0-9]+/', $words[$i])) {
array_splice($words, $i, 1);
}
}
return $words;
}
}
?>
\ No newline at end of file
<?php
/**
* PSpellEngine.php
*
* Copyright, Moxiecode Systems AB
* Released under LGPL License.
*
* License: http://www.tinymce.com/license
* Contributing: http://www.tinymce.com/contributing
*/
class TinyMCE_SpellChecker_PSpellEngine extends TinyMCE_SpellChecker_Engine {
/**
* Spellchecks an array of words.
*
* @param String $lang Selected language code (like en_US or de_DE). Shortcodes like "en" and "de" work with enchant >= 1.4.1
* @param Array $words Array of words to check.
* @return Name/value object with arrays of suggestions.
*/
public function getSuggestions($lang, $words) {
$config = $this->getConfig();
switch ($config['PSpell.mode']) {
case "fast":
$mode = PSPELL_FAST;
break;
case "slow":
$mode = PSPELL_SLOW;
break;
default:
$mode = PSPELL_NORMAL;
}
// Setup PSpell link
$plink = pspell_new(
$lang,
$config['pspell.spelling'],
$config['pspell.jargon'],
$config['pspell.encoding'],
$mode
);
if (!$plink) {
throw new Exception("No PSpell link found opened.");
}
$outWords = array();
foreach ($words as $word) {
if (!pspell_check($plink, trim($word))) {
$outWords[] = utf8_encode($word);
}
}
return $outWords;
}
/**
* Return true/false if the engine is supported by the server.
*
* @return boolean True/false if the engine is supported.
*/
public function isSupported() {
return function_exists("pspell_new");
}
}
TinyMCE_Spellchecker_Engine::add("pspell", "TinyMCE_SpellChecker_PSpellEngine");
?>
<?php
/**
* spellcheck.php
*
* Copyright, Moxiecode Systems AB
* Released under LGPL License.
*
* License: http://www.tinymce.com/license
* Contributing: http://www.tinymce.com/contributing
*/
require('./includes/Engine.php');
require('./includes/EnchantEngine.php');
require('./includes/PSpellEngine.php');
$tinymceSpellCheckerConfig = array(
"engine" => "enchant", // enchant, pspell
// Enchant options
"enchant_dicts_path" => "./dicts",
// PSpell options
"pspell.mode" => "fast",
"pspell.spelling" => "",
"pspell.jargon" => "",
"pspell.encoding" => ""
);
TinyMCE_Spellchecker_Engine::processRequest($tinymceSpellCheckerConfig);
?>
\ No newline at end of file
......@@ -306,11 +306,11 @@ function smarty($javascript = array(), $headers = array(), $pagestrings = array(
if (get_config('tinymcespellcheckerengine')) {
$spellchecker = ',spellchecker';
$spellchecker_toolbar = '| spellchecker';
$spellchecker_config = "gecko_spellcheck : false, spellchecker_rpc_url : \"{$jsroot}tinymce/plugins/spellchecker/spellchecker.php\",";
$spellchecker_config = "spellchecker_rpc_url : \"{$jsroot}tinymce/plugins/spellchecker/spellchecker.php\",";
}
else {
$spellchecker = $spellchecker_toolbar = '';
$spellchecker_config = 'gecko_spellcheck : true,';
$spellchecker_config = 'browser_spellcheck : true,';
}
$mathslate = (get_config('mathjax')) ? 'mathslate' : '';
$mathslateplugin = !empty($mathslate) ? ',' . $mathslate : '';
......@@ -343,6 +343,7 @@ EOF;
if ($check[$key] == 'tinymce') {
$tinymceconfig = <<<EOF
theme: "silver",
contextmenu: false,
plugins: "tooltoggle,visualblocks,wordcount,link,lists,imagebrowser,table,emoticons{$spellchecker},paste,code,fullscreen,directionality,searchreplace,nonbreaking,charmap{$mathslateplugin},anchor",
skin: 'oxide',
toolbar1: {$toolbar[1]},
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment