File: WordPress.ml

package info (click to toggle)
xmlrpc-light 0.6.1-5
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye
  • size: 1,308 kB
  • sloc: ml: 3,183; makefile: 54; sh: 1
file content (746 lines) | stat: -rw-r--r-- 27,638 bytes parent folder | download | duplicates (4)
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
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
(*
 * XmlRpc Light, a small XmlRpc library based on Xml Light and Ocamlnet
 * Copyright (C) 2007-2009 Dave Benjamin (dave@ramenlabs.com)
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *)

exception Type_error of string
exception Unknown_field of string

let strict = ref false

let warn exn =
  if !strict
  then raise exn
  else prerr_endline (Printexc.to_string exn)

let map_array f = function
  | `Array items -> List.map f items
  | other -> raise (Type_error (XmlRpc.dump other))

let iter_struct f = function
  | `Struct pairs -> List.iter f pairs
  | other -> raise (Type_error (XmlRpc.dump other))

let int_value = function
  | `Int n -> n
  | `String s -> int_of_string s
  | other -> raise (Type_error (XmlRpc.dump other))

module Blog = struct
  type t = { mutable is_admin : bool;
             mutable url : string;
             mutable blog_id : int;
             mutable blog_name : string;
             mutable xmlrpc : string; }

  let make () =
    {is_admin=false;
     url="";
     blog_id=0;
     blog_name="";
     xmlrpc=""}

  let of_xmlrpc value =
    let result = make () in
    iter_struct
      (function
         | ("isAdmin", `Boolean v) -> result.is_admin <- v
         | ("url", `String v) -> result.url <- v
         | ("blogid", `String v) -> result.blog_id <- int_of_string v
         | ("blogid", `Int v) -> result.blog_id <- v
         | ("blogName", `String v) -> result.blog_name <- v
         | ("xmlrpc", `String v) -> result.xmlrpc <- v
         | (field, _) -> warn (Unknown_field field))
      value;
    result
end

module Category = struct
  type t = { mutable category_id : int;
             mutable parent_id : int;
             mutable description : string;
             mutable category_name : string;
             mutable html_url : string;
             mutable rss_url : string; }

  let make () =
    {category_id=0;
     parent_id=0;
     description="";
     category_name="";
     html_url="";
     rss_url=""} 

  let of_xmlrpc value =
    let result = make () in
    iter_struct
      (function
         | ("categoryId", `String v) -> result.category_id <- int_of_string v
         | ("categoryId", `Int v) -> result.category_id <- v
         | ("parentId", `String v) -> result.parent_id <- int_of_string v
         | ("parentId", `Int v) -> result.parent_id <- v
         | ("description", `String v) -> result.description <- v
         | ("categoryName", `String v) -> result.category_name <- v
         | ("htmlUrl", `String v) -> result.html_url <- v
         | ("rssUrl", `String v) -> result.rss_url <- v
         | (field, _) -> warn (Unknown_field field))
      value;
    result
end

module CommentCount = struct
  type t = { mutable approved : int;
             mutable awaiting_moderation : int;
             mutable spam : int;
             mutable total_comments : int; }

  let make () =
    {approved=0;
     awaiting_moderation=0;
     spam=0;
     total_comments=0}

  let of_xmlrpc value =
    let result = make () in
    iter_struct
      (function
         | ("approved", `String v) -> result.approved <- int_of_string v
         | ("approved", `Int v) -> result.approved <- v
         | ("awaiting_moderation", `Int v) -> result.awaiting_moderation <- v
         | ("spam", `Int v) -> result.spam <- v
         | ("total_comments", `Int v) -> result.total_comments <- v
         | (field, _) -> warn (Unknown_field field))
      value;
    result
end

module Comment = struct
  type t = { mutable date_created : XmlRpcDateTime.t;
             mutable user_id : int;
             mutable comment_id : int;
             mutable parent : int;
             mutable status : string;
             mutable content : string;
             mutable link : string;
             mutable post_id : int;
             mutable post_title : string;
             mutable author : string;
             mutable author_url : string;
             mutable author_email : string;
             mutable author_ip : string;
             mutable typ : string }

  let make () =
    {date_created=(0,0,0,0,0,0,0);
     user_id=0;
     comment_id=0;
     parent=0;
     status="";
     content="";
     link="";
     post_id=0;
     post_title="";
     author="";
     author_url="";
     author_email="";
     author_ip="";
     typ=""}

  let of_xmlrpc value =
    let result = make () in
    iter_struct
      (function
         | ("date_created_gmt", `DateTime v) -> result.date_created <- v
         | ("user_id", `String v) -> result.user_id <- int_of_string v
         | ("user_id", `Int v) -> result.user_id <- v
         | ("comment_id", `String v) -> result.comment_id <- int_of_string v
         | ("comment_id", `Int v) -> result.comment_id <- v
         | ("parent", `String v) -> result.parent <- int_of_string v
         | ("parent", `Int v) -> result.parent <- v
         | ("status", `String v) -> result.status <- v
         | ("content", `String v) -> result.content <- v
         | ("link", `String v) -> result.link <- v
         | ("post_id", `String v) -> result.post_id <- int_of_string v
         | ("post_id", `Int v) -> result.post_id <- v
         | ("post_title", `String v) -> result.post_title <- v
         | ("author", `String v) -> result.author <- v
         | ("author_url", `String v) -> result.author_url <- v
         | ("author_email", `String v) -> result.author_email <- v
         | ("author_ip", `String v) -> result.author_ip <- v
         | ("type", `String v) -> result.typ <- v
         | (field, _) -> warn (Unknown_field field))
      value;
    result

  let to_xmlrpc comment =
    `Struct ["date_created_gmt", `DateTime (XmlRpcDateTime.set_tz_offset 0
                                              comment.date_created);
             "user_id", `Int comment.user_id;
             "comment_id", `Int comment.comment_id;
             "parent", `Int comment.parent;
             "status", `String comment.status;
             "content", `String comment.content;
             "link", `String comment.link;
             "post_id", `Int comment.post_id;
             "post_title", `String comment.post_title;
             "author", `String comment.author;
             "author_url", `String comment.author_url;
             "author_email", `String comment.author_email;
             "author_ip", `String comment.author_ip;
             "type", `String comment.typ]
end

module CustomField = struct
  type t = { mutable id : int option;
             mutable key : string option;
             mutable value : string }

  let make () =
    {id=None;
     key=None;
     value=""}

  let of_xmlrpc value =
    let result = make () in
    iter_struct
      (function
         | ("id", `String v) -> result.id <- Some (int_of_string v)
         | ("id", `Int v) -> result.id <- Some v
         | ("key", `String v) -> result.key <- Some v
         | ("value", `String v) -> result.value <- v
         | (field, _) -> warn (Unknown_field field))
      value;
    result

  let to_xmlrpc field =
    match field with
      | {id=None; key=None; value=value} ->
          `Struct ["value", `String value]
      | {id=Some id; key=None; value=value} ->
          `Struct ["id", `Int id; "value", `String value]
      | {id=None; key=Some key; value=value} ->
          `Struct ["key", `String key; "value", `String value]
      | {id=Some id; key=Some key; value=value} ->
          `Struct ["id", `Int id; "key", `String key; "value", `String value]
end

module Option = struct
  type t = { mutable desc : string;
             mutable readonly : bool;
             mutable value : string; }

  let make () =
    {desc="";
     readonly=false;
     value=""}

  let of_xmlrpc value =
    let result = make () in
    iter_struct
      (function
         | ("desc", `String v) -> result.desc <- v
         | ("readonly", `Boolean v) -> result.readonly <- v
         | ("value", `String v) -> result.value <- v
         | (field, _) -> warn (Unknown_field field))
      value;
    result
end

module User = struct
  type t = { mutable user_id : int;
             mutable user_login : string;
             mutable display_name : string;
             mutable user_email : string;
             mutable meta_value : string; }

  let make () =
    {user_id=0;
     user_login="";
     display_name="";
     user_email="";
     meta_value=""}

  let of_xmlrpc value =
    let result = make () in
    iter_struct
      (function
         | ("user_id", `String v) -> result.user_id <- int_of_string v
         | ("user_id", `Int v) -> result.user_id <- v
         | ("user_login", `String v) -> result.user_login <- v
         | ("display_name", `String v) -> result.display_name <- v
         | ("user_email", `String v) -> result.user_email <- v
         | ("meta_value", `String v) -> result.meta_value <- v
         | (field, _) -> warn (Unknown_field field))
      value;
    result
end

module PageListItem = struct
  type t = { mutable page_id : int;
             mutable page_title : string;
             mutable page_parent_id : int;
             mutable date_created : XmlRpcDateTime.t;
           }

  let make () =
    {page_id=0;
     page_title="";
     page_parent_id=0;
     date_created=(0,0,0,0,0,0,0)}

  let of_xmlrpc value =
    let result = make () in
    iter_struct
      (function
         | ("page_id", `String v) -> result.page_id <- int_of_string v
         | ("page_id", `Int v) -> result.page_id <- v
         | ("page_title", `String v) -> result.page_title <- v
         | ("page_parent_id", `String v) -> result.page_parent_id <- int_of_string v
         | ("page_parent_id", `Int v) -> result.page_parent_id <- v
         | ("dateCreated", `DateTime v) -> result.date_created <- v
         | ("date_created_gmt", `DateTime v) -> result.date_created <- v
         | (field, _) -> warn (Unknown_field field))
      value;
    result
end

module Page = struct
  type t = { mutable date_created : XmlRpcDateTime.t;
             mutable user_id : int;
             mutable page_id : int;
             mutable page_status : string;
             mutable description : string;
             mutable title : string;
             mutable link : string;
             mutable permalink : string;
             mutable categories : string list;
             mutable excerpt : string;
             mutable text_more : string;
             mutable mt_allow_comments : bool;
             mutable mt_allow_pings : bool;
             mutable wp_slug : string;
             mutable wp_password : string;
             mutable wp_author : string;
             mutable wp_page_parent_id : int;
             mutable wp_page_parent_title : string;
             mutable wp_page_order : int;
             mutable wp_author_id : int;
             mutable wp_author_display_name : string;
             mutable custom_fields : CustomField.t list;
             mutable wp_page_template : string; }

  let make () =
    {date_created=(0,0,0,0,0,0,0);
     user_id=0;
     page_id=0;
     page_status="";
     description="";
     title="";
     link="";
     permalink="";
     categories=[];
     excerpt="";
     text_more="";
     mt_allow_comments=false;
     mt_allow_pings=false;
     wp_slug="";
     wp_password="";
     wp_author="";
     wp_page_parent_id=0;
     wp_page_parent_title="";
     wp_page_order=0;
     wp_author_id=0;
     wp_author_display_name="";
     custom_fields=[];
     wp_page_template=""}

  let of_xmlrpc value =
    let result = make () in
    iter_struct
      (function
         | ("dateCreated", `DateTime v) -> result.date_created <- v
         | ("date_created_gmt", `DateTime v) -> result.date_created <- v
         | ("userid", `String v) -> result.user_id <- int_of_string v
         | ("userid", `Int v) -> result.user_id <- v
         | ("page_id", `String v) -> result.page_id <- int_of_string v
         | ("page_id", `Int v) -> result.page_id <- v
         | ("page_status", `String v) -> result.page_status <- v
         | ("description", `String v) -> result.description <- v
         | ("title", `String v) -> result.title <- v
         | ("link", `String v) -> result.link <- v
         | ("permaLink", `String v) -> result.permalink <- v
         | ("categories", `Array v) ->
             result.categories <- List.map XmlRpc.dump v
         | ("excerpt", `String v) -> result.excerpt <- v
         | ("text_more", `String v) -> result.text_more <- v
         | ("mt_excerpt", `String v) -> result.excerpt <- v
         | ("mt_text_more", `String v) -> result.text_more <- v
         | ("mt_allow_comments", `Int v) -> result.mt_allow_comments <- v<>0
         | ("mt_allow_comments", `Boolean v) -> result.mt_allow_comments <- v
         | ("mt_allow_pings", `Int v) -> result.mt_allow_pings <- v<>0
         | ("mt_allow_pings", `Boolean v) -> result.mt_allow_pings <- v
         | ("wp_slug", `String v) -> result.wp_slug <- v
         | ("wp_password", `String v) -> result.wp_password <- v
         | ("wp_author", `String v) -> result.wp_author <- v
         | ("wp_author_display_name", `String v) ->
             result.wp_author_display_name <- v
         | ("wp_page_parent_id", `String v) ->
             result.wp_page_parent_id <- int_of_string v
         | ("wp_page_parent_id", `Int v) ->
             result.wp_page_parent_id <- v
         | ("wp_page_parent_title", `String v) ->
             result.wp_page_parent_title <- v
         | ("wp_page_order", `String v) ->
             result.wp_page_order <- int_of_string v
         | ("wp_page_order", `Int v) ->
             result.wp_page_order <- v
         | ("wp_author_id", `String v) ->
             result.wp_author_id <- int_of_string v
         | ("wp_author_id", `Int v) ->
             result.wp_author_id <- v
         | ("custom_fields", `Array v) ->
             result.custom_fields <- List.map CustomField.of_xmlrpc v
         | ("wp_page_template", `String v) ->
             result.wp_page_template <- v
         | (field, _) -> warn (Unknown_field field))
      value;
    result

  let to_xmlrpc page =
    `Struct ["userid", `Int page.user_id;
             "page_id", `Int page.page_id;
             "page_status", `String page.page_status;
             "wp_slug", `String page.wp_slug;
             "wp_password", `String page.wp_password;
             "wp_author", `String page.wp_author;
             "wp_author_display_name", `String page.wp_author_display_name;
             "wp_page_parent_id", `Int page.wp_page_parent_id;
             "wp_page_parent_title", `String page.wp_page_parent_title;
             "wp_page_order", `Int page.wp_page_order;
             "wp_author_id", `Int page.wp_author_id;
             "title", `String page.title;
             "description", `String page.description;
             "link", `String page.link;
             "permaLink", `String page.permalink;
             "mt_excerpt", `String page.excerpt;
             "mt_text_more", `String page.text_more;
             "mt_allow_comments", `Boolean page.mt_allow_comments;
             "mt_allow_pings", `Boolean page.mt_allow_pings;
             "dateCreated", `DateTime page.date_created;
             "date_created_gmt", `DateTime (XmlRpcDateTime.set_tz_offset 0
                                              page.date_created);
             "categories", `Array (List.map
                                     (fun s -> `String s)
                                     page.categories);
             "custom_fields", `Array (List.map
                                        CustomField.to_xmlrpc
                                        page.custom_fields);
             "wp_page_template", `String page.wp_page_template]
end

module Post = struct
  type t = { mutable user_id : int;
             mutable post_id : int;
             mutable post_status : string;
             mutable date_created : XmlRpcDateTime.t;
             mutable description : string;
             mutable title : string;
             mutable link : string;
             mutable permalink : string;
             mutable categories : string list;
             mutable excerpt : string;
             mutable text_more : string;
             mutable mt_allow_comments : bool;
             mutable mt_allow_pings : bool;
             mutable mt_keywords : string;
             mutable wp_slug : string;
             mutable wp_password : string;
             mutable wp_author_id : int;
             mutable wp_author_display_name : string;
             mutable custom_fields : CustomField.t list; }

  let make () =
    {date_created=(0,0,0,0,0,0,0);
     user_id=0;
     post_id=0;
     post_status="";
     description="";
     title="";
     link="";
     permalink="";
     categories=[];
     excerpt="";
     text_more="";
     mt_allow_comments=false;
     mt_allow_pings=false;
     mt_keywords="";
     wp_slug="";
     wp_password="";
     wp_author_id=0;
     wp_author_display_name="";
     custom_fields=[]}

  let of_xmlrpc value =
    let result = make () in
    iter_struct
      (function
         | ("dateCreated", `DateTime v) -> result.date_created <- v
         | ("date_created_gmt", `DateTime v) -> result.date_created <- v
         | ("userid", `String v) -> result.user_id <- int_of_string v
         | ("userid", `Int v) -> result.user_id <- v
         | ("postid", `String v) -> result.post_id <- int_of_string v
         | ("postid", `Int v) -> result.post_id <- v
         | ("post_status", `String v) -> result.post_status <- v
         | ("description", `String v) -> result.description <- v
         | ("title", `String v) -> result.title <- v
         | ("link", `String v) -> result.link <- v
         | ("permaLink", `String v) -> result.permalink <- v
         | ("categories", `Array v) ->
             result.categories <- List.map XmlRpc.dump v
         | ("mt_excerpt", `String v) -> result.excerpt <- v
         | ("mt_text_more", `String v) -> result.text_more <- v
         | ("mt_allow_comments", `Int v) -> result.mt_allow_comments <- v<>0
         | ("mt_allow_comments", `Boolean v) -> result.mt_allow_comments <- v
         | ("mt_allow_pings", `Int v) -> result.mt_allow_pings <- v<>0
         | ("mt_allow_pings", `Boolean v) -> result.mt_allow_pings <- v
         | ("mt_keywords", `String v) -> result.mt_keywords <- v
         | ("wp_slug", `String v) -> result.wp_slug <- v
         | ("wp_password", `String v) -> result.wp_password <- v
         | ("wp_author_id", `String v) -> result.wp_author_id <- int_of_string v
         | ("wp_author_id", `Int v) -> result.wp_author_id <- v
         | ("wp_author_display_name", `String v) ->
             result.wp_author_display_name <- v
         | ("custom_fields", `Array v) ->
             result.custom_fields <- List.map CustomField.of_xmlrpc v
         | (field, _) -> warn (Unknown_field field))
      value;
    result

  let to_xmlrpc post =
    `Struct ["dateCreated", `DateTime post.date_created;
             "date_created_gmt", `DateTime (XmlRpcDateTime.set_tz_offset 0
                                              post.date_created);
             "userid", `Int post.user_id;
             "postid", `Int post.post_id;
             "post_status", `String post.post_status;
             "description", `String post.description;
             "title", `String post.title;
             "link", `String post.link;
             "permaLink", `String post.permalink;
             "categories", `Array (List.map
                                     (fun s -> `String s)
                                     post.categories);
             "mt_excerpt", `String post.excerpt;
             "mt_text_more", `String post.text_more;
             "mt_allow_comments", `Boolean post.mt_allow_comments;
             "mt_allow_pings", `Boolean post.mt_allow_pings;
             "mt_keywords", `String post.mt_keywords;
             "wp_slug", `String post.wp_slug;
             "wp_password", `String post.wp_password;
             "wp_author_id", `Int post.wp_author_id;
             "wp_author_display_name", `String post.wp_author_display_name;
             "custom_fields", `Array (List.map
                                        CustomField.to_xmlrpc
                                        post.custom_fields)];
end

class api ~url ~blog_id ~username ~password =
object (self)
  val rpc = new XmlRpc.client url
  val std_args = [`Int blog_id; `String username; `String password]
  val blog_id = blog_id
  val username = username
  val password = password

  method rpc = rpc

  method get_page page_id =
    Page.of_xmlrpc (rpc#call "wp.getPage"
                      [`Int blog_id;
                       `Int page_id;
                       `String username;
                       `String password])

  method get_pages () =
    map_array Page.of_xmlrpc (rpc#call "wp.getPages" std_args)

  method get_page_list () =
    map_array PageListItem.of_xmlrpc (rpc#call "wp.getPageList" std_args)

  method get_page_status_list () =
    match rpc#call "wp.getPageStatusList" std_args with
      | `Struct pairs -> List.map (fun (k, v) -> (k, XmlRpc.dump v)) pairs
      | other -> raise (Type_error (XmlRpc.dump other))

  method get_page_templates () =
    match rpc#call "wp.getPageTemplates" std_args with
      | `Struct pairs -> List.map (fun (k, v) -> (k, XmlRpc.dump v)) pairs
      | other -> raise (Type_error (XmlRpc.dump other))

  method new_page content publish =
    int_value
      (rpc#call "wp.newPage"
         (std_args @ [Page.to_xmlrpc content; `Boolean publish]))

  method edit_page page_id content publish =
    ignore
      (rpc#call "wp.editPage" [`Int blog_id;
                               `Int page_id;
                               `String username;
                               `String password;
                               Page.to_xmlrpc content;
                               `Boolean publish])

  method delete_page page_id =
    ignore
      (rpc#call "wp.deletePage" (std_args @ [`Int page_id]))

  method get_post post_id =
    Post.of_xmlrpc (rpc#call "metaWeblog.getPost"
                      [`Int post_id;
                       `String username;
                       `String password])

  method get_recent_posts num_posts =
    map_array Post.of_xmlrpc (rpc#call "metaWeblog.getRecentPosts"
                                (std_args @ [`Int num_posts]))

  method get_post_status_list () =
    match rpc#call "wp.getPostStatusList" std_args with
      | `Struct pairs -> List.map (fun (k, v) -> (k, XmlRpc.dump v)) pairs
      | other -> raise (Type_error (XmlRpc.dump other))

  method new_post content publish =
    int_value
      (rpc#call "metaWeblog.newPost"
         (std_args @ [Post.to_xmlrpc content; `Boolean publish]))

  method edit_post post_id content publish =
    ignore
      (rpc#call "metaWeblog.editPost" [`Int post_id;
                                       `String username;
                                       `String password;
                                       Post.to_xmlrpc content;
                                       `Boolean publish])

  method delete_post post_id =
    ignore
      (rpc#call "metaWeblog.deletePost" [`Int blog_id;
                                         `Int post_id;
                                         `String username;
                                         `String password;
                                         `Boolean false])

  method get_authors () =
    map_array User.of_xmlrpc (rpc#call "wp.getAuthors" std_args)

  method get_blogs () =
    map_array Blog.of_xmlrpc (rpc#call "wp.getUsersBlogs" [`String username;
                                                           `String password])

  method get_comment_count post_id =
    CommentCount.of_xmlrpc
      (rpc#call "wp.getCommentCount" (std_args @ [`Int post_id]))

  method get_comment_status_list () =
    match rpc#call "wp.getCommentStatusList" std_args with
      | `Struct pairs -> List.map (fun (k, v) -> (k, XmlRpc.dump v)) pairs
      | other -> raise (Type_error (XmlRpc.dump other))

  method get_comment comment_id =
    Comment.of_xmlrpc
      (rpc#call "wp.getComment" (std_args @ [`Int comment_id]))

  method get_comments ?(status="") ?(post_id=0) ?(offset=0) ?(number=10) () =
    map_array Comment.of_xmlrpc
      (rpc#call "wp.getComments"
         (std_args @ [`Struct ["status", `String status;
                               "post_id", `Int post_id;
                               "offset", `Int offset;
                               "number", `Int number]]))

  method new_comment comment =
    int_value
      (rpc#call "wp.newComment"
         (std_args @ [`Int comment.Comment.post_id;
                      Comment.to_xmlrpc comment]))

  method edit_comment comment_id comment =
    ignore
      (rpc#call "wp.editComment"
         (std_args @ [`Int comment_id;
                      Comment.to_xmlrpc comment]))

  method delete_comment comment_id =
    ignore
      (rpc#call "wp.deleteComment" (std_args @ [`Int comment_id]))

  method get_categories () =
    map_array Category.of_xmlrpc (rpc#call "wp.getCategories" std_args)

  method new_category ~name ~slug ~parent_id ~description =
    int_value
      (rpc#call "wp.newCategory"
         (std_args @ [`Struct ["name", `String name;
                               "slug", `String slug;
                               "parent_id", `Int parent_id;
                               "description", `String description]]))

  method suggest_categories category max_results =
    rpc#call "wp.suggestCategories"
      (std_args @ [`String category; `Int max_results])

  method get_options names =
    let result =
      rpc#call
        "wp.getOptions"
        (std_args @ [`Array (List.map (fun s -> `String s) names)]) in
    match result with
      | `Struct pairs ->
          List.map (fun (name, opt) -> (name, Option.of_xmlrpc opt)) pairs
      | `Array [] -> []
      | other -> raise (Type_error (XmlRpc.dump other))

  method set_options options =
    let result =
      rpc#call
        "wp.setOptions"
        (std_args @ [`Struct (List.map (fun (name, value) ->
                                          (name, `String value)) options)]) in
    match result with
      | `Struct pairs ->
          List.map (fun (name, opt) -> (name, Option.of_xmlrpc opt)) pairs
      | `Array [] -> []
      | other -> raise (Type_error (XmlRpc.dump other))

  method upload_file ~name ~typ ~bits ~overwrite =
    let value =
      rpc#call "wp.uploadFile"
        (std_args @ [`Struct ["name", `String name;
                              "type", `String typ;
                              "bits", `Binary bits;
                              "overwrite", `Boolean overwrite]]) in
    let file, url, typ = ref "", ref "", ref "" in
    iter_struct
      (function
         | ("file", `String v) -> file := v
         | ("url", `String v) -> url := v
         | ("type", `String v) -> typ := v
         | (field, _) -> warn (Unknown_field field))
      value;
    (!file, !url, !typ)
end