1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace JarvisPHP\Plugins\InformationOn_plugin; |
4
|
|
|
|
5
|
|
|
use JarvisPHP\Core\JarvisSession; |
6
|
|
|
use JarvisPHP\Core\JarvisPHP; |
7
|
|
|
use JarvisPHP\Core\JarvisLanguage; |
8
|
|
|
use JarvisPHP\Core\JarvisTTS; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* A simple InformationOn plugin that uses Wikipedia Opensearch API |
12
|
|
|
* @author Stefano Bianchini |
13
|
|
|
* @website http://www.stefanobianchini.net |
14
|
|
|
* Based on Wiki_plugin of UgoRaffaele https://github.com/UgoRaffaele/ |
15
|
|
|
*/ |
16
|
|
|
class InformationOn_plugin implements \JarvisPHP\Core\JarvisPluginInterface{ |
17
|
|
|
/** |
18
|
|
|
* Priority of plugin |
19
|
|
|
* @var int |
20
|
|
|
*/ |
21
|
|
|
var $priority = 10; |
|
|
|
|
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* the behaviour of plugin |
25
|
|
|
* @param string $command |
26
|
|
|
*/ |
27
|
|
|
function answer($command) { |
|
|
|
|
28
|
|
|
//Extract search term |
29
|
|
|
preg_match(JarvisLanguage::translate('preg_match_activate_plugin',get_called_class()), $command, $matches); |
30
|
|
|
$search_term = $matches[2]; |
31
|
|
|
$wiki_query_url = _WIKI_URL . "?action=opensearch&search=" . urlencode($search_term) . "&format=xml&limit=5"; |
32
|
|
|
$xml = simplexml_load_string(file_get_contents($wiki_query_url)); |
33
|
|
|
$item_array = $xml->Section->Item; |
34
|
|
|
$answer = JarvisLanguage::translate('nothing_found',get_called_class()); |
35
|
|
|
//Have i got results? |
36
|
|
|
if(count($item_array)>0) { |
37
|
|
|
foreach ($item_array as $item) { |
38
|
|
|
if (strlen($item->Description) > 0) { |
39
|
|
|
$answer = sprintf(JarvisLanguage::translate('search_result_is',get_called_class()), $item->Description); |
40
|
|
|
break; |
41
|
|
|
} |
42
|
|
|
} |
43
|
|
|
} |
44
|
|
|
//Making response |
45
|
|
|
$response = new \JarvisPHP\Core\JarvisResponse($answer, JarvisTTS::speak($answer), JarvisPHP::getRealClassName(get_called_class()), true); |
|
|
|
|
46
|
|
|
$response->send(); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* Get plugin's priority |
51
|
|
|
* @return int |
52
|
|
|
*/ |
53
|
|
|
function getPriority() { |
|
|
|
|
54
|
|
|
return $this->priority; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* Is it the right plugin for the command? |
59
|
|
|
* @param string $command |
60
|
|
|
* @return boolean |
61
|
|
|
*/ |
62
|
|
|
function isLikely($command) { |
|
|
|
|
63
|
|
|
return preg_match(JarvisLanguage::translate('preg_match_activate_plugin',get_called_class()), $command); |
|
|
|
|
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* Does the plugin need a session? |
68
|
|
|
* @return boolean |
69
|
|
|
*/ |
70
|
|
|
function hasSession() { |
|
|
|
|
71
|
|
|
return false; |
72
|
|
|
} |
73
|
|
|
} |
74
|
|
|
|
The PSR-2 coding standard requires that all properties in a class have their visibility explicitly declared. If you declare a property using
the property is implicitly global.
To learn more about the PSR-2, please see the PHP-FIG site on the PSR-2.