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