]> CyberLeo.Net >> Repos - SourceForge/phpwiki.git/blob - lib/plugin/SpellCheck.php
Use HTML 5 DOCTYPE; force CHARSET=utf-8
[SourceForge/phpwiki.git] / lib / plugin / SpellCheck.php
1 <?php
2
3 /**
4  * Copyright 2006,2007 $ThePhpWikiProgrammingTeam
5  *
6  * This file is part of PhpWiki.
7  *
8  * PhpWiki is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 2 of the License, or
11  * (at your option) any later version.
12  *
13  * PhpWiki is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License along
19  * with PhpWiki; if not, write to the Free Software Foundation, Inc.,
20  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21  */
22
23 /**
24  * SpellCheck is a plugin, used inside the editpage.tmpl (on save or preview)
25  * This could be a userpref option always highlighting words in preview
26  * or it could be an extra button in edit.
27  *
28  * The pspell extension is better, because it's easier to store corrections.
29  * Enchant looks more promising, because it supports multiple speller backends.
30  *
31  * Currently we do use aspell (via pspell or cmdline) in ispell mode.
32  * Maybe enchant later.
33  * cmdline preparation:
34 do autosplit wikiwords && sed s,^,\^^, $pagename | aspell --lang=$LANG -a
35 or
36 sed s,^,\^^, $pagename | aspell --lang=$LANG -a -C
37 =>
38 & phpwiki 62 30: pipework, paprika, Popek, Phip, Pike, Viki, pike, ...
39  */
40
41 // Those settings should really be defined in config.ini, not here.
42 if (!function_exists('pspell_new_config')) {
43     // old pipe interface:
44     if (!defined('ASPELL_EXE'))
45         define('ASPELL_EXE', 'aspell');
46     //define('ASPELL_EXE','/usr/local/bin/aspell');
47     //define('ASPELL_EXE','/home/groups/p/ph/phpwiki/bin/aspell');
48     if (!defined('ASPELL_DATA_DIR'))
49         if (isWindows())
50             define('ASPELL_DATA_DIR', 'c:\cygwin\usr\share\aspell');
51         else
52             define('ASPELL_DATA_DIR', '/usr/share/aspell');
53     //define('ASPELL_DATA_DIR','/home/groups/p/ph/phpwiki/share/highlight');
54 } else {
55     // new library interface through the pspell extension:
56     // "/var/dictionaries/custom.pws"
57     if (!defined('PSPELL_PWL'))
58         define('PSPELL_PWL', ''); // phpwiki-special wordlist
59     // "/var/dictionaries/custom.repl"
60     if (!defined('PSPELL_REPL'))
61         define('PSPELL_REPL', ''); // phpwiki-special replacement list (persistent replacements)
62 }
63
64 class WikiPlugin_SpellCheck
65     extends WikiPlugin
66 {
67     function getName()
68     {
69         return _("Spell Checker");
70     }
71
72     function getDescription()
73     {
74         return _("Check the spelling of a page and make suggestions.");
75     }
76
77     function managesValidators()
78     {
79         return true;
80     }
81
82     function getDefaultArguments()
83     {
84         return array('pagename' => '[]', // button or preview highlight?
85         );
86     }
87
88     function pspell_check($text, $lang = false)
89     {
90         if ($lang) $lang = $GLOBALS['LANG'];
91         $words = preg_split('/[\W]+?/', $text);
92
93         $misspelled = $return = array();
94         $pspell_config = pspell_config_create($lang, "", "", 'UTF-8',
95             PSPELL_NORMAL | PSPELL_RUN_TOGETHER);
96         //pspell_config_runtogether($pspell_config, true);
97         if (PSPELL_PWL)
98             pspell_config_personal($pspell_config, PSPELL_PWL);
99         if (PSPELL_REPL)
100             pspell_config_repl($pspell_config, PSPELL_REPL);
101         $pspell = pspell_new_config($pspell_config);
102
103         foreach ($words as $value) {
104             // SplitPagename $value
105             if (!pspell_check($pspell, $value)) {
106                 $misspelled[] = $value;
107             }
108         }
109         foreach ($misspelled as $value) {
110             $return[$value] = pspell_suggest($pspell, $value);
111         }
112         return $return;
113     }
114
115     function pspell_correct($text, $corrections)
116     {
117         ;
118     }
119
120     function run($dbi, $argstr, &$request, $basepage)
121     {
122         extract($this->getArgs($argstr, $request));
123         $page = $dbi->getPage($pagename);
124         $current = $page->getCurrentRevision();
125         $source = $current->getPackedContent();
126
127         if (empty($source))
128             return $this->error(fmt("empty source"));
129         if ($basepage == _("SpellCheck"))
130             return $this->error(fmt("Cannot SpellCheck myself"));
131         $lang = $page->get('lang');
132         if (empty($lang)) $lang = $GLOBALS['LANG'];
133         $html = HTML();
134         if (!function_exists('pspell_new_config')) {
135             // use the aspell commandline interface
136             include_once 'lib/WikiPluginCached.php';
137             $args = "";
138             $source = preg_replace("/^/m", "^", $source);
139             if (ASPELL_DATA_DIR)
140                 $args .= " --data-dir=" . ASPELL_DATA_DIR;
141             // MAYBE TODO: do we have the language dictionary?
142             $args .= " --lang=" . $lang;
143             // use -C or autosplit wikiwords in the text
144             $commandLine = ASPELL_EXE . " -a -C $args ";
145             $cache = new WikiPluginCached;
146             $code = $cache->filterThroughCmd($source, $commandLine);
147             if (empty($code))
148                 return $this->error(fmt("Couldn't start commandline ā€œ%sā€", $commandLine));
149             $sugg = array();
150             foreach (preg_split("/\n/", $code) as $line) {
151                 if (preg_match("/^& (\w+) \d+ \d+: (.+)$/", $line, $m)) {
152                     $sugg[$m[1]] = preg_split("/, /", $m[2]);
153                 }
154             }
155             /*$pre = HTML::pre(HTML::raw($code));
156             $html->pushContent($pre);*/
157         } else {
158             $sugg = pspell_check($source, $lang);
159         }
160         //$html->pushContent(HTML::hr(),HTML::h1(_("Spellcheck")));
161         $page = $request->getPage();
162         if ($version) {
163             $revision = $page->getRevision($version);
164             if (!$revision)
165                 NoSuchRevision($request, $page, $version);
166         } else {
167             $revision = $page->getCurrentRevision();
168         }
169         $GLOBALS['request']->setArg('suggestions', $sugg);
170         include_once 'lib/BlockParser.php';
171         $ori_html = TransformText($revision, $revision->get('markup'), $page);
172         $GLOBALS['request']->setArg('suggestions', false);
173
174         $html->pushContent($ori_html, HTML::hr(), HTML::h1(_("SpellCheck result")));
175
176         $list = HTML::ul();
177         foreach ($sugg as $word => $suggs) {
178             $w = HTML::span(array('class' => 'spell-wrong'), $word);
179             // TODO: optional replace-link. jscript or request button with word replace.
180             $r = HTML();
181             foreach ($suggs as $s) {
182                 $r->pushContent(HTML::a(array('class' => 'spell-sugg',
183                         'href' => "javascript:do_replace('$word','$s')"),
184                     $s), ", ");
185             }
186             $list->pushContent(HTML::li($w, _(": "), $r));
187         }
188         $html->pushContent($list);
189         return $html;
190     }
191 }
192
193 // Local Variables:
194 // mode: php
195 // tab-width: 8
196 // c-basic-offset: 4
197 // c-hanging-comment-ender-p: nil
198 // indent-tabs-mode: nil
199 // End: