[BUGFIX] RTE: Words containing umlauts not added to personal dictionary
authorStanislas Rolland <typo3@sjbr.ca>
Thu, 18 Oct 2012 15:05:13 +0000 (11:05 -0400)
committerStanislas Rolland <typo3@sjbr.ca>
Thu, 18 Oct 2012 17:59:44 +0000 (19:59 +0200)
Problem: The spell checker must analyze the dictionary file and may
have to update the charset of the personal dictionary. In doing so,
it is looking for the wrong file when the dictionary in use is a
regional/variety dictionary.

Change-Id: Ia2b57c0ead7c90a6d40e9db4d396e6c7ded72e5c
Resolves: #29685
Releases: 4.5, 4.6, 4.7, 6.0
Reviewed-on: http://review.typo3.org/15790
Reviewed-by: Stanislas Rolland
Tested-by: Stanislas Rolland
typo3/sysext/rtehtmlarea/Classes/Controller/SpellCheckingController.php
typo3/sysext/rtehtmlarea/htmlarea/plugins/SpellChecker/spell-checker.js

index ed52984..5877659 100644 (file)
@@ -263,20 +263,26 @@ class SpellCheckingController {
                                                fwrite($filehandle, $cmd, strlen($cmd));
                                        }
                                        $cmd = '#' . LF;
-                                       fwrite($filehandle, $cmd, strlen($cmd));
-                                       // Assemble the Aspell command
-                                       $AspellCommand = (TYPO3_OS === 'WIN' ? 'type ' : 'cat ') . escapeshellarg($tmpFileName) . ' | ' . $this->AspellDirectory . ' -a --mode=none' . ($this->personalDictionaryPath ? ' --home-dir=' . escapeshellarg($this->personalDictionaryPath) : '') . ' --lang=' . escapeshellarg($this->dictionary) . ' --encoding=' . escapeshellarg($mainDictionaryCharacterSet) . ' 2>&1';
-                                       $AspellAnswer = shell_exec($AspellCommand);
-                                       // Close and delete the temporary file
-                                       fclose($filehandle);
-                                       \TYPO3\CMS\Core\Utility\GeneralUtility::unlink_tempfile($tmpFileName);
-                                       echo 'Personal word list was updated.';
+                                       $result = fwrite($filehandle, $cmd, strlen($cmd));
+                                       if ($result === FALSE) {
+                                               \TYPO3\CMS\Core\Utility\GeneralUtility::sysLog('SpellChecker tempfile write error: ' . $tmpFileName, $this->extKey, \TYPO3\CMS\Core\Utility\GeneralUtility::SYSLOG_SEVERITY_ERROR);
+                                       } else {
+                                               // Assemble the Aspell command
+                                               $aspellCommand = ((TYPO3_OS === 'WIN') ? 'type ' : 'cat ') . escapeshellarg($tmpFileName) . ' | '
+                                                       . $this->AspellDirectory
+                                                       . ' -a --mode=none'
+                                                       . ($this->personalDictionaryPath ? ' --home-dir=' . escapeshellarg($this->personalDictionaryPath) : '')
+                                                       . ' --lang=' . escapeshellarg($this->dictionary)
+                                                       . ' --encoding=' . escapeshellarg($mainDictionaryCharacterSet)
+                                                       . ' 2>&1';
+                                               $aspellResult = shell_exec($aspellCommand);
+                                               // Close and delete the temporary file
+                                               fclose($filehandle);
+                                               \TYPO3\CMS\Core\Utility\GeneralUtility::unlink_tempfile($tmpFileName);
+                                       }
                                } else {
                                        \TYPO3\CMS\Core\Utility\GeneralUtility::sysLog('SpellChecker tempfile open error: ' . $tmpFileName, $this->extKey, \TYPO3\CMS\Core\Utility\GeneralUtility::SYSLOG_SEVERITY_ERROR);
-                                       echo 'SpellChecker tempfile open error.';
                                }
-                       } else {
-                               echo 'Nothing to add to the personal word list.';
                        }
                        flush();
                        die;
@@ -351,7 +357,15 @@ var selectedDictionary = "' . $this->dictionary . '";
         * @return string path to the main dictionary
         */
        protected function setMainDictionaryPath() {
-               $this->mainDictionaryPath = trim(shell_exec($this->AspellDirectory . ' config dict-dir'));
+               $this->mainDictionaryPath = '';
+               $aspellCommand = $this->AspellDirectory . ' config dict-dir';
+               $aspellResult = shell_exec($aspellCommand);
+               if ($aspellResult) {
+                       $this->mainDictionaryPath = trim($aspellResult);
+               }
+               if (!$aspellResult || !$this->mainDictionaryPath) {
+                       \TYPO3\CMS\Core\Utility\GeneralUtility::sysLog('SpellChecker main dictionary path retrieval error: ' . $aspellCommand, $this->extKey, \TYPO3\CMS\Core\Utility\GeneralUtility::SYSLOG_SEVERITY_ERROR);
+               }
                return $this->mainDictionaryPath;
        }
 
@@ -362,20 +376,36 @@ var selectedDictionary = "' . $this->dictionary . '";
         */
        protected function getMainDictionaryCharacterSet() {
                $characterSet = '';
-               // Read the options of the dictionary
-               $dictionaryHandle = fopen($this->mainDictionaryPath . '/' . $this->dictionary . '.dat', 'rb');
-               $dictionaryContent = fread($dictionaryHandle, 500);
-               fclose($dictionaryHandle);
-               // Get the line that contains the character set option
-               $dictionaryContent = preg_split('/charset\\s*/', $dictionaryContent, 2);
-               if ($dictionaryContent[1]) {
-                       // Isolate the character set
-                       $dictionaryContent = \TYPO3\CMS\Core\Utility\GeneralUtility::trimExplode(LF, $dictionaryContent[1]);
-                       $characterSet = $dictionaryContent[0];
+               if ($this->mainDictionaryPath) {
+                       // Keep only the first part of the dictionary name
+                       $mainDictionary = preg_split('/[-_]/', $this->dictionary, 2);
+                       // Read the options of the dictionary
+                       $dictionaryFileName = $this->mainDictionaryPath . '/' . $mainDictionary[0] . '.dat';
+                       $dictionaryHandle = fopen($dictionaryFileName, 'rb');
+                       if (!$dictionaryHandle) {
+                               \TYPO3\CMS\Core\Utility\GeneralUtility::sysLog('SpellChecker main dictionary open error: ' . $dictionaryFileName, $this->extKey, \TYPO3\CMS\Core\Utility\GeneralUtility::SYSLOG_SEVERITY_ERROR);
+                       } else {
+                               $dictionaryContent = fread($dictionaryHandle, 500);
+                               if ($dictionaryContent === FALSE) {
+                                       \TYPO3\CMS\Core\Utility\GeneralUtility::sysLog('SpellChecker main dictionary read error: ' . $dictionaryFileName, $this->extKey, \TYPO3\CMS\Core\Utility\GeneralUtility::SYSLOG_SEVERITY_ERROR);
+                               } else {
+                                       fclose($dictionaryHandle);
+                                       // Get the line that contains the character set option
+                                       $dictionaryContent = preg_split('/charset\s*/', $dictionaryContent, 2);
+                                       if ($dictionaryContent[1]) {
+                                               // Isolate the character set
+                                               $dictionaryContent = \TYPO3\CMS\Core\Utility\GeneralUtility::trimExplode(LF, $dictionaryContent[1]);
+                                               $characterSet = $dictionaryContent[0];
+                                               // Fix Aspell character set oddity (i.e. iso8859-1)
+                                               $characterSet = str_replace('iso', 'iso-', $characterSet);
+                                               $characterSet = str_replace('--', '-', $characterSet);
+                                       }
+                                       if (!$characterSet) {
+                                               \TYPO3\CMS\Core\Utility\GeneralUtility::sysLog('SpellChecker main dictionary character set retrieval error: ' . $dictionaryContent[1], $this->extKey, \TYPO3\CMS\Core\Utility\GeneralUtility::SYSLOG_SEVERITY_ERROR);
+                                       }
+                               }
+                       }
                }
-               // Fix Aspell character set oddity (i.e. iso8859-1)
-               $characterSet = str_replace('iso', 'iso-', $characterSet);
-               $characterSet = str_replace('--', '-', $characterSet);
                return $characterSet;
        }
 
@@ -417,16 +447,29 @@ var selectedDictionary = "' . $this->dictionary . '";
         * @return void
         */
        protected function fixPersonalDictionaryCharacterSet() {
-               // Fix the options of the personl word list and of the replacement pairs files
-               $fileNames = array();
-               $fileNames[0] = $this->personalDictionaryPath . '/' . '.aspell.' . $this->dictionary . '.pws';
-               $fileNames[1] = $this->personalDictionaryPath . '/' . '.aspell.' . $this->dictionary . '.prepl';
-               foreach ($fileNames as $fileName) {
-                       if (file_exists($fileName)) {
-                               $fileContent = file_get_contents($fileName);
-                               $fileContent = explode(LF, $fileContent);
-                               if (strpos($fileContent[0], 'utf-8') === FALSE) {
-                                       $fileContent[0] .= ' utf-8';
+               if ($this->personalDictionaryPath) {
+                       // Fix the options of the personl word list and of the replacement pairs files
+                       // Aspell creates such files only for the main dictionary
+                       $fileNames = array();
+                       $mainDictionary = preg_split('/[-_]/', $this->dictionary, 2);
+                       $fileNames[0] = $this->personalDictionaryPath . '/' . '.aspell.' . $mainDictionary[0] . '.pws';
+                       $fileNames[1] = $this->personalDictionaryPath . '/' . '.aspell.' . $mainDictionary[0] . '.prepl';
+                       foreach ($fileNames as $fileName) {
+                               if (file_exists($fileName)) {
+                                       $fileContent = file_get_contents($fileName);
+                                       if ($fileContent === FALSE) {
+                                               \TYPO3\CMS\Core\Utility\GeneralUtility::sysLog('SpellChecker personal word list read error: ' . $fileName, $this->extKey, \TYPO3\CMS\Core\Utility\GeneralUtility::SYSLOG_SEVERITY_ERROR);
+                                       } else {
+                                               $fileContent = explode(LF, $fileContent);
+                                               if (strpos($fileContent[0], 'utf-8') === FALSE) {
+                                                       $fileContent[0] .= ' utf-8';
+                                                       $fileContent = implode(LF, $fileContent);
+                                                       $result = file_put_contents($fileName, $fileContent);
+                                                       if ($result === FALSE) {
+                                                               \TYPO3\CMS\Core\Utility\GeneralUtility::sysLog('SpellChecker personal word list write error: ' . $fileName, $this->extKey, \TYPO3\CMS\Core\Utility\GeneralUtility::SYSLOG_SEVERITY_ERROR);
+                                                       }
+                                               }
+                                       }
                                }
                                $fileContent = implode(LF, $fileContent);
                                file_put_contents($fileName, $fileContent);
index 3d4601f..3dedaf2 100644 (file)
@@ -412,7 +412,7 @@ HTMLArea.SpellChecker = Ext.extend(HTMLArea.Plugin, {
                                cmd: 'learn',
                                enablePersonalDicts: this.enablePersonalDicts,
                                userUid: this.userUid,
-                               dictionary: this.contentISOLanguage,
+                               dictionary: this.dialog.find('itemId', 'dictionary')[0].getValue(),
                                pspell_charset: this.contentCharset,
                                pspell_mode: this.spellCheckerMode
                        };