/
Api.php
215 lines (191 loc) · 6.02 KB
/
Api.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
<?php
/**
* Modifications to initial api made by ciss
**/
class Phergie_Plugin_Api extends Phergie_Plugin_Abstract
{
/**
* Database with the api documentation
*
* @var PDO
*/
protected $database;
//'/^(?:[\w\d]+|\*(?!\*))+$/'
private $queryTypes = array(
'help' => '#^help(\s*?(?<command>.+))?$#',
'property' => '#^(?<class>\w[\w\d]*?)::\$(?<property>\w[\w\d]*?)$#',
'method' => '#^(?<class>\w[\w\d]*?)::(?<method>[A-Za-z0-9]+)\s*?(\(\))?#',
'class' => '#^(?<class>\w[\w\d]*?)$#',
);
// Default message
private $unknown = 'Sorry, never heard of this.';
private $textFormats = array(
'n' => "\017",
'b' => "\002",
'i' => "\026",
'u' => "\037",
);
/**
* Check for dependencies.
*
* @return void
*/
public function onLoad()
{
if (!extension_loaded('PDO') || !extension_loaded('pdo_sqlite')) {
$this->fail('PDO and pdo_sqlite extensions must be installed');
}
else
{
$path = dirname(__FILE__);
try
{
$this->database = new PDO('sqlite:' . $path . '/Api/api.db');
}
catch (PDOException $e)
{
}
}
$this->getPluginHandler()->getPlugin('Command');
}
public function onCommandApi($args)
{
//TODO: Options regexp
if(preg_match('#^-(?<options>[\w]*?)\s+?(?<args>.*?)$#', $args, $matches)) {
$options = trim($matches['options']);
$args = trim($matches['args']);
}
//BUG: can not catch name like []
if(preg_match('#(?<args>^[^, ]++)\s*,?\s*(?:(?:@|at|to|oh)\s+|@|)(?<nick>[a-zA-Z0-9_-]+)\s*?$#', $args, $matches))
{
$args = $matches['args'];
$nick = $matches['nick'];
}
$args = trim($args);
$message = $this->unknown;
foreach($this->queryTypes as $name => $pattern)
{
if(preg_match($pattern, $args, $matches))
{
$functionName = 'query' . ucfirst($name);
if(!method_exists($this, $functionName))
continue;
$message = (empty($nick) ? '' : "$nick: ") . $this->$functionName($matches, (empty($options)) ? null : $options);
break;
}
}
$this->doPrivmsg($this->getEvent()->getSource(), $message);
}
private function queryMethod($args, $options = null)
{
$args['method'] .= '()';
//TODO: make this more elegant
$signature = false;
if($options !== null && strpos('s', $options) === 0 ) {
$query = $this->database->prepare("
SELECT
`d`.`me_signature`,
`d`.`me_link`
FROM
`doc_cl_methods` `s`
INNER JOIN `doc_cl_methods` `d` ON `d`.`me_class` = `s`.`me_definedby`
AND `d`.`me_name` = `s`.`me_name`
WHERE
`s`.`me_class` LIKE :class
AND `s`.`me_name` LIKE :method
");
$signature = true;
} else {
$query = $this->database->prepare("
SELECT
`me_class`,
`me_name`,
`me_description`,
`me_link`
FROM
`doc_cl_methods`
WHERE
`me_class` LIKE :class
AND `me_name` LIKE :method
");
}
$query->bindParam(':class', $args['class']);
$query->bindParam(':method', $args['method']);
$query->execute();
$results = $query->fetch(PDO::FETCH_ASSOC);
if(empty($results))
return 'Sorry, unknown method.';
extract($results);
extract($this->textFormats);
//TODO: fix the database for the \n in signature
return ($signature) ? $i.preg_replace('/\n/',' ',$me_signature).$n.' '.$me_link : "$b$me_class::$me_name$n $i$me_description$n $me_link";
}
private function queryProperty($args)
{
$query = $this->database->prepare("
SELECT
`pr_type`,
`pr_access`,
`pr_class`,
`pr_name`,
`pr_description`,
`pr_link`
FROM
`doc_cl_properties`
WHERE
`pr_class` LIKE :class
AND `pr_name` LIKE :property
");
$query->bindParam(':class', $args['class']);
$query->bindParam(':property', $args['property']);
$query->execute();
$results = $query->fetch(PDO::FETCH_ASSOC);
if(empty($results))
return 'Sorry, unknown property.';
extract($results);
return "$pr_access $pr_type $pr_class::\$$pr_name $pr_description $pr_link";
}
private function queryClass($args, $options = null)
{
$query = $this->database->prepare("
SELECT
`cl_package`,
`cl_name`,
`cl_description`,
`cl_link`
FROM
`doc_class`
WHERE
`cl_name` LIKE :class
");
$query->bindParam(':class', $args['class']);
$query->execute();
$results = $query->fetch(PDO::FETCH_ASSOC);
if(empty($results))
return 'Sorry, unknown class.';
extract($results);
return "$cl_package $cl_name $cl_description $cl_link";
return !empty($results)
? implode(' ', array_slice($results, 0, 2))
: 'Sorry, unknown class.';
}
private function queryHelp($args)
{
switch($command = trim($args['command']))
{
case 'class' : return 'For class info: api class';
case 'method' : return 'For method info: api class::method';
case 'property' : return 'For property info: api class::$property';
case 'api' :
case '' : return
"Hello, my name is yiibot. I am a Phergie ( http://phergie.org ) IRC bot programmed to help you with the Yii documentation."
." The main command to call me is: api. "
." For class info: api class, for method info: api class::method & for property info: api class::\$property"
." -- For reporting issues on my functionality visit: http://github.com/dmtrs/yiibot/issues or /msg tydeas."
."Authors of Api Plugin for Phergie bot: tydeas, ciss and thanks to rawtaz."
."Have a nice day :)";
default : return "Sorry, I've never heard of \"$command.\"";
}
}
}
?>