]> CyberLeo.Net >> Repos - SourceForge/phpwiki.git/blob - tests/xmlrpc/xmlrpc-introspect.php
Add svn:keywords
[SourceForge/phpwiki.git] / tests / xmlrpc / xmlrpc-introspect.php
1 <?php // $Id$
2
3 /*********************************
4 * system.describeMethods Support *
5 *********************************/
6
7 /* public function. format method description. 
8  * input = result from xmlrpc method "system.describeMethods" in a php array 
9  * result = html string suitable for display.
10  */
11 function format_describe_methods_result($response) {
12    global $xi_type_hash;
13    $unknown = "???";
14    $method_hash = array();
15    $xi_type_hash = array();
16
17    $typeList = $response[typeList];
18    $methodList = $response[methodList];
19
20    usort($typeList, "name_cmp");
21    usort($methodList, "name_cmp");
22
23    if ($methodList) {
24       $buf .= "<big>Methods\n<ul>";
25       foreach($methodList as $method) {
26          $name = $method[name];
27          $buf .= "<li><a href='#$name'>$name()</a>\n";
28          $method_hash[$name] = $method;
29       }
30       $buf .= "</ul></big>";
31    }
32    if ($typeList) {
33       $buf .= "<big>Types\n<ul>";
34       foreach($typeList as $type) {
35          $name = $type[name];
36          $buf .= "<li><a href='#$name'>$name</a>\n";
37          $xi_type_hash[$name] = $type;
38       }
39       $buf .= "</ul></big>";
40    }
41
42
43    $buf .= "<h2>Methods</h2><table bgcolor='#dddddd' align='center' border=1 width=100%>";
44    foreach($methodList as $method) {
45       $name = $method[name];
46       $author = $method[author] ? $method[author] : $unknown;
47       $version = $method[version] ? $method[version] : $unknown;
48       $desc = $method[purpose] ? $method[purpose] : $unknown;
49       $sigs = $method[signatures];
50       $buf .= "<tr bgcolor='#444444'><td align='center' class='title' colspan=3><font color='#EEEEEE'><a name='$name'><b><big>$name</big></b></a></font></td></tr>";
51       $buf .= "<tr>" . field("Signature(s)", get_sigs($method), 3) . "</tr>";
52       $buf .= "<tr>" . field("Author", $author, 2) . field("Version", $version) . "</tr>";
53       $buf .= "<tr>" . field("Purpose", $desc, 3) . "</tr>";
54
55       $exclude_list = array(
56                            "see",
57                            "signatures",
58                            "purpose",
59                            "version",
60                            "author",
61                            "name"
62                            );
63       foreach($method as $key => $val) {
64          if (!in_array($key, $exclude_list)) {
65             if (is_array($val)) {
66                $buf .= "<tr bgcolor='#aaaaaa'><td colspan=3>" . ucfirst($key) . "</td></tr>";
67
68                foreach($val as $key2 => $desc) {
69                   if (gettype($desc) === "string") {
70                      if (gettype($key2) !== "string") {
71                         $key2 = false;
72                      }
73
74                      $buf .= "<tr>" . field(ucfirst($key2), $desc, 3) . "</tr>";
75                   }
76                }
77             }
78             else {
79                $buf .= "<tr>" . field(ucfirst($key), $val, 3) . "</tr>";
80             }
81          }
82       }
83
84
85       if ($sigs) {
86          $bmultiple = count($sigs) > 1;
87          foreach($sigs as $sig) {
88             if ($bmultiple) {
89                $count ++;
90                $buf .= "<tr bgcolor='#888888'><td colspan=3><font color='white'><b>Signature $count:</b> " . get_sig($name, $sig) . "</font></td></tr>";
91             }
92
93             if ($sig[params]) {
94                $buf .= "<tr bgcolor='#aaaaaa'><td colspan=3>Parameters</td></tr>";
95
96                $buf .= "<tr><td colspan=3>";
97                $buf .= do_params($sig[params], false, false);
98                $buf .= "</td></tr>";
99             }
100
101             if ($sig[returns]) {
102                $buf .= "<tr bgcolor='#aaaaaa'><td colspan=3>Return Value(s)</td></tr>";
103                $buf .= "<tr><td colspan=3>";
104                $buf .= do_params($sig[returns], false, false);
105                $buf .= "</td></tr>";
106             }
107          }
108       }
109
110       if ($method[see]) {
111          $buf .= "<tr bgcolor='#aaaaaa'><td colspan=3>See Also</td></tr>";
112
113          foreach($method[see] as $name => $desc) {
114             if ($method_hash[$name]) {
115                $name = "<a href='#$name'>$name</a>";
116             }
117
118             $buf .= "<tr>" . field($name, $desc, 3) . "</tr>";
119          }
120       }
121    }
122    $buf .= "</table><H2>Types</H2><TABLE><table bgcolor='#dddddd' align='center' border=1 width=100%>";
123    $buf .= "<tr><td colspan=3>";
124
125    $buf .= do_params($typeList, true);
126
127    $buf .= "</td></tr>";
128    $buf .= "</TABLE>";
129
130    return $buf;
131 }
132
133 /*********************************************************************
134 * The following functions are non-public and may change at any time. *
135 *********************************************************************/
136
137 /* non public - format key/val pair in a <td></td> */
138 function field($title, $text, $columns=false, $width=false) {
139    if ($columns) {
140       $colspan = " colspan='$columns'";
141    }
142    if ($width) {
143       $width = " width='$width' ";
144    }
145    $colon = $title && $text ? ":" : "";
146    return "<td$colspan$width><b>$title$colon</b> $text</td>";
147 }
148
149 function is_xmlrpc_type($type) {
150    static $types = array("array", "struct", "mixed", "string", "int", "i4", "datetime", "base64", "boolean");
151    return in_array($type, $types);
152 }
153
154 function user_type($type) {
155    global $xi_type_hash;
156    return $xi_type_hash[$type] ? true : false;
157 }
158
159
160 function do_param($param, $bNewType=false, $bLinkUserTypes=true, $depth=0) {
161    /* guard against serious craziness */
162    if (++ $depth >= 24) {
163       return "<dt><b>max depth reached.  bailing out...</b></dt>";
164    }
165    $name = $param[name];
166    $type = $param[type];
167    $desc = $param[description];
168    $member = $param[member];
169    $opt = $param[optional];
170    $def = $param['default'];
171    $type_def = $param[type_def];
172
173    if ($type_def) {
174       $type_def = "<a href='#$type_def'>$type_def</a> ";
175    }
176    else {
177       $type_def = "";
178    }
179
180    $buf = "<dt>";
181
182    if ($bNewType) {
183       $anchor = "<a name='$name'></a>";
184    }
185
186    if (user_type($type)) {
187       if ($bLinkUserTypes) {
188          $type = "<a href='#$type'>$type</a>";
189       }
190       else {  
191          /* hack to display user values inline.  max depth check above. */
192          global $xi_type_hash;
193          $newtype = $xi_type_hash[$type];
194
195          $newtype[name] = $param[name];
196          $newtype[optional] = $param[optional];
197          if ($param[description]) {
198             $newtype[description] = $param[description];
199          }
200          $newtype[type_def] = $type;
201          $buf .= do_param($newtype, $bNewType, $bLinkUserTypes, $depth);
202          return $buf;
203       }
204    }
205
206    $buf .= "$type_def$type <i>$anchor$name</i>";
207    if ($opt || $def) {
208       $buf .= " (";
209       if ($opt) {
210          $buf .= "optional" . ($def ? ", " : "");
211       }
212       if ($def) {
213          $buf .= "default=$def";
214       }
215       $buf .= ")";
216    }
217
218    if ($desc) {
219       $buf .= " -- $desc";
220    }
221    if ($member) {
222       $buf .= do_params($member, $bNewType, $bLinkUserTypes, $depth);
223    }
224    $buf .= "</dt>\n";
225
226    return $buf;
227 }
228
229 /* non public - format params list recursively */
230 function do_params($params, $bNewType=false, $bLinkUserTypes=true, $depth=0) {
231    if ($params) {
232       $br = $bNewType ? "<br>" : "";
233       $buf = "\n<dl>\n";
234       foreach($params as $param) {
235          $buf .= do_param($param, $bNewType, $bLinkUserTypes, $depth);
236       }
237       $buf .= "</dl>$br\n";
238    }
239    return $buf;
240 }
241
242
243 function get_sig($method_name, $sig) {
244    $buf = "";
245    if ($method_name && $sig) {
246       $return = $sig[returns][0][type];
247       if (!$return) {
248          $return = "void";
249       }
250       $buf .= "$return ";
251
252       $buf .= "$method_name( ";
253       $first = true;
254
255       if ($sig[params]) {
256          foreach($sig[params] as $param) {
257             $type = $param[type];
258             $opt = $param[optional];
259             $def = $param["default"];
260             $name = $param["name"];
261             if (user_type($type)) {
262                $type = "<a href='#$type'>$type</a>";
263             }
264
265             if ($opt) {
266                $buf .= $first ? '[' : ' [';
267             }
268             if (!$first) {
269                $buf .= ', ';
270             }
271             $buf .= "$type";
272             if (name) {
273                $buf .= " $name";
274             }
275             if ($def) {
276                $buf .= " = $def";
277             }
278             if ($opt) {
279                $buf .= ']';
280             }
281             $first = false;
282          }
283       }
284
285       $buf .= " )";
286    }
287
288    return $buf;
289 }
290
291 /* non public. generate method signature from method description */
292 function get_sigs($method) {
293    $method_name = $method[name];
294
295    $buf = "";
296
297    if ($method[signatures]) {
298       foreach($method[signatures] as $sig) {
299          $buf .= "<li>" . get_sig($method_name, $sig);
300       }
301    }
302
303    return $buf;
304 }
305
306 function name_cmp($a, $b) {
307    return strcmp($a[name], $b[name]);
308 }
309
310
311 /*************************************
312 * END system.describeMethods Support *
313 *************************************/
314
315
316 ?>