]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - plugins/Irc/extlib/phergie/Phergie/Plugin/SpellCheck.php
Merge branch '0.9.x' of gitorious.org:statusnet/mainline into 1.0.x
[quix0rs-gnu-social.git] / plugins / Irc / extlib / phergie / Phergie / Plugin / SpellCheck.php
1 <?php
2 /**
3  * Phergie
4  *
5  * PHP version 5
6  *
7  * LICENSE
8  *
9  * This source file is subject to the new BSD license that is bundled
10  * with this package in the file LICENSE.
11  * It is also available through the world-wide-web at this URL:
12  * http://phergie.org/license
13  *
14  * @category  Phergie
15  * @package   Phergie_Plugin_TerryChay
16  * @author    Phergie Development Team <team@phergie.org>
17  * @copyright 2008-2010 Phergie Development Team (http://phergie.org)
18  * @license   http://phergie.org/license New BSD License
19  * @link      http://pear.phergie.org/package/Phergie_Plugin_TerryChay
20  */
21
22 /**
23  * Handles requests for checking spelling of specified words and returning
24  * either confirmation of correctly spelled words or potential correct
25  * spellings for misspelled words.
26  *
27  * @category Phergie
28  * @package  Phergie_Plugin_SpellCheck
29  * @author   Phergie Development Team <team@phergie.org>
30  * @license  http://phergie.org/license New BSD License
31  * @link     http://pear.phergie.org/package/Phergie_Plugin_TerryChay
32  * @uses     Phergie_Plugin_Command pear.phergie.org
33  * @uses     extension pspell
34  */
35 class Phergie_Plugin_SpellCheck extends Phergie_Plugin_Abstract
36 {
37     /**
38      * Spell check dictionary handler
39      *
40      * @var resource
41      */
42     protected $pspell;
43
44     /**
45      * Limit on the number of potential correct spellings returned
46      *
47      * @var int
48      */
49     protected $limit;
50
51     /**
52      * Check for dependencies.
53      *
54      * @return void
55      */
56     public function onLoad()
57     {
58         if (!extension_loaded('pspell')) {
59             $this->fail('pspell php extension is required');
60         }
61
62         if (!$this->getConfig('spellcheck.lang')) {
63             $this->fail('Setting spellcheck.lang must be filled-in');
64         }
65
66         $this->plugins->getPlugin('Command');
67
68         set_error_handler(array($this, 'loadDictionaryError'));
69         $this->pspell = pspell_new($this->getConfig('spellcheck.lang'));
70         restore_error_handler();
71
72         $this->limit = $this->getConfig('spellcheck.limit', 5);
73     }
74
75     /**
76      * Intercepts and handles requests for spell checks.
77      *
78      * @param string $word the string to perform checks against
79      *
80      * @return void
81      */
82     public function onCommandSpell($word)
83     {
84         $source = $this->event->getSource();
85         $target = $this->event->getNick();
86
87         $message  = $target . ': The word "' . $word;
88         $message .= '" seems to be spelled correctly.';
89         if (!pspell_check($this->pspell, $word)) {
90             $suggestions = pspell_suggest($this->pspell, $word);
91
92             $message  = $target;
93             $message .= ': I could not find any suggestions for "' . $word . '".';
94             if (!empty($suggestions)) {
95                 $suggestions = array_splice($suggestions, 0, $this->limit);
96                 $message     = $target . ': Suggestions for "';
97                 $message    .= $word . '": ' . implode(', ', $suggestions) . '.';
98             }
99         }
100
101         $this->doPrivmsg($source, $message);
102     }
103
104     /**
105      * Handle any errors from loading dictionary
106      *
107      * @param integer $errno   Error code
108      * @param string  $errstr  Error message
109      * @param string  $errfile File that errored
110      * @param integer $errline Line where the error happened
111      *
112      * @return void
113      */
114     protected function loadDictionaryError($errno, $errstr, $errfile, $errline)
115     {
116         $this->fail($errstr);
117     }
118 }