-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWikiclient.php
More file actions
253 lines (211 loc) · 6.61 KB
/
Wikiclient.php
File metadata and controls
253 lines (211 loc) · 6.61 KB
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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
<?php
/*
This is a PHP client interface to the wikifier wikiserver.
Create a client instance with
$client = new Wikiclient($path_to_socket, $wiki_name, $wiki_password);
Then, use one of the public functions to send a request. These functions return the
result in the form of: array($response_message, $options) where
$response_message is the command or message reply and
$options is an associative array of message options
*/
class Wikiclient {
public $connected = false;
public $wiki_name;
public $session_id;
public $login_again_cb;
private $wiki_pass;
private $path;
private $sock;
private $username;
private $password;
public function __construct($path, $wiki_name, $wiki_pass, $session_id) {
$this->path = $path;
$this->wiki_name = $wiki_name;
$this->wiki_pass = $wiki_pass;
$this->session_id = $session_id;
}
// connect to unix listener.
private function connect($n = 1, $is_login = false) {
$this->sock = fsockopen('unix://'.$this->path, 0, $errno, $errstr, 10);
if (!$this->sock) {
if ($n == 5) return;
$this->connect($n + 1, $is_login);
}
// send anonymous login info.
$auth = array('wiki', array(
'name' => $this->wiki_name,
'password' => $this->wiki_pass
));
if (fwrite($this->sock, json_encode($auth)."\n"))
$this->connected = true;
else return;
// send session ID.
if (isset($this->session_id) && !$is_login) {
$auth2 = array('resume', array(
'session_id' => $this->session_id
));
fwrite($this->sock, json_encode($auth2)."\n");
}
return $this->connected;
}
// send a command/message.
function command($command, $opts = array()) {
if ($command != 'wiki' && !$this->connected)
$this->connect(1, $command == 'login');
$opts['close'] = true;
// send request
$req = array($command, $opts);
if (!fwrite($this->sock, json_encode($req)."\n")) {
if (isset($this->session_id)) {
unset($this->session_id);
if ($this->login_again_cb)
$this->login_again_cb->__invoke();
}
return null;
}
$data = '';
// read until the server sends EOF.
while (!feof($this->sock)) {
$data .= fgets($this->sock, 128);
}
fclose($this->sock);
unset($this->sock);
$this->connected = false;
// decode JSON.
$res = json_decode(trim($data));
$res[1]->response = $res[0];
$res = $res[1];
// check if the session expired.
if (isset($res->login_again)) {
unset($this->session_id);
if ($this->login_again_cb)
$this->login_again_cb->__invoke();
return;
}
return $res;
}
// send login for write access.
function login($username, $password, $session_id) {
return $this->command('login', array(
'username' => $username,
'password' => $password,
'session_id' => $session_id
));
}
/*********** PUBLIC READ METHODS ***********/
// send a page request.
function page($name) {
return $this->command('page', array( 'name' => $name ));
}
// send a page code request.
function page_code($name, $display_page = false) {
return $this->command('page_code', array(
'name' => $name,
'display_page' => $display_page
));
}
// send a page list request.
function page_list($sort = 'm-') {
return $this->command('page_list', array(
'sort' => $sort
));
}
// send a model code request.
function model_code($name, $display_model = false) {
return $this->command('model_code', array(
'name' => $name,
'display_model' => $display_model
));
}
// send a model list request
function model_list($sort = 'm-') {
return $this->command('model_list', array(
'sort' => $sort
));
}
// send an image request.
function image($name, $width = 0, $height = 0, $scale = 0) {
return $this->command('image', array(
'name' => $name,
'width' => $width,
'height' => $height,
'scale' => $scale
));
}
// send an image list request
function image_list($sort = 'm-') {
return $this->command('image_list', array(
'sort' => $sort
));
}
// send a category posts request.
function cat_posts($name) {
return $this->command('cat_posts', array(
'name' => $name
));
}
// send a category list request
function cat_list($sort = 'm-') {
return $this->command('cat_list', array(
'sort' => $sort
));
}
// check connection
function ping() {
return $this->command('ping');
}
// get last 100 log lines
function logs() {
return $this->command('logs');
}
/*********** PUBLIC WRITE METHODS ***********/
function page_save($name, $content, $message = null) {
return $this->command('page_save', array(
'name' => $name,
'content' => $content,
'message' => $message
));
}
function page_del($name) {
return $this->command('page_del', array(
'name' => $name
));
}
function page_move($name, $new_name) {
return $this->command('page_move', array(
'name' => $name,
'new_name' => $new_name
));
}
function page_revs($name) {
return $this->command('page_revs', array(
'name' => $name
));
}
function page_diff($name, $from, $to) {
return $this->command('page_diff', array(
'name' => $name,
'from' => $from,
'to' => $to
));
}
function model_save($name, $content, $message = null) {
return $this->command('model_save', array(
'name' => $name,
'content' => $content,
'message' => $message
));
}
function model_del($name) {
return $this->command('model_del', array(
'name' => $name
));
}
function model_move($name, $new_name) {
return $this->command('model_move', array(
'name' => $name,
'new_name' => $new_name
));
}
}
?>