PageRenderTime 46ms CodeModel.GetById 0ms RepoModel.GetById 0ms app.codeStats 1ms

/twfy/www/docs/api/index.php

https://github.com/bruno/openaustralia-app
PHP | 320 lines | 289 code | 15 blank | 16 comment | 30 complexity | 4595d3cc27db85e7203f3949bc8ac271 MD5 | raw file
  1. <?
  2. include_once '../../includes/easyparliament/init.php';
  3. include_once '../../includes/postcode.inc';
  4. include_once 'api_functions.php';
  5. # XXX: Need to override error handling! XXX
  6. $methods = array(
  7. 'convertURL' => array(
  8. 'parameters' => array('url'),
  9. 'required' => true,
  10. 'help' => 'Converts a parliament.uk Hansard URL into a TheyWorkForYou one, if possible',
  11. ),
  12. 'getConstituency' => array(
  13. 'new' => true,
  14. 'parameters' => array('postcode'),
  15. 'required' => true,
  16. 'help' => 'Searches for a constituency',
  17. ),
  18. 'getConstituencies' => array(
  19. 'parameters' => array('date', 'search', 'latitude', 'longitude', 'distance'),
  20. 'required' => false,
  21. 'help' => 'Returns list of constituencies',
  22. ),
  23. 'getMP' => array(
  24. 'new' => true,
  25. 'parameters' => array('id', 'constituency', 'postcode', 'always_return', 'extra'),
  26. 'required' => true,
  27. 'help' => 'Returns main details for an MP'
  28. ),
  29. 'getMPInfo' => array(
  30. 'parameters' => array('id'),
  31. 'required' => true,
  32. 'help' => 'Returns extra information for a person'
  33. ),
  34. 'getMPs' => array(
  35. 'parameters' => array('party', 'date', 'search'),
  36. 'required' => false,
  37. 'help' => 'Returns list of MPs',
  38. ),
  39. 'getLord' => array(
  40. 'parameters' => array('id'),
  41. 'required' => true,
  42. 'help' => 'Returns details for a Lord'
  43. ),
  44. 'getLords' => array(
  45. 'parameters' => array('date', 'party', 'search'),
  46. 'required' => false,
  47. 'help' => 'Returns list of Lords',
  48. ),
  49. 'getGeometry' => array(
  50. 'new' => true,
  51. 'parameters' => array('name'),
  52. 'required' => false,
  53. 'help' => 'Returns centre, bounding box of constituencies'
  54. ),
  55. /* 'getBoundary' => array(
  56. 'parameters' => array('name'),
  57. 'required' => true,
  58. 'help' => 'Returns boundary polygon of constituency'
  59. ),
  60. */
  61. 'getCommittee' => array(
  62. 'new' => true,
  63. 'parameters' => array('name', 'date'),
  64. 'required' => true,
  65. 'help' => 'Returns members of Select Committee',
  66. ),
  67. 'getDebates' => array(
  68. 'new' => true,
  69. 'parameters' => array('type', 'date', 'search', 'person', 'gid', 'year', 'order', 'page', 'num'),
  70. 'required' => true,
  71. 'help' => 'Returns Debates (either Commons, Westminhall Hall, or Lords)',
  72. ),
  73. 'getWrans' => array(
  74. 'parameters' => array('date', 'search', 'person', 'gid', 'year', 'order', 'page', 'num'),
  75. 'required' => true,
  76. 'help' => 'Returns Written Answers',
  77. ),
  78. 'getWMS' => array(
  79. 'parameters' => array('date', 'search', 'person', 'gid', 'year', 'order', 'page', 'num'),
  80. 'required' => true,
  81. 'help' => 'Returns Written Ministerial Statements',
  82. ),
  83. 'getHansard' => array(
  84. 'parameters' => array('search', 'person', 'order', 'page', 'num'),
  85. 'required' => true,
  86. 'help' => 'Returns any of the above',
  87. ),
  88. 'getComments' => array(
  89. 'new' => true,
  90. 'parameters' => array('search', 'page', 'num', 'pid'),
  91. 'required' => false,
  92. 'help' => 'Returns comments'
  93. ),
  94. 'postComment' => array(
  95. 'parameters' => array('user_id', 'gid?'),
  96. 'working' => false,
  97. 'required' => true,
  98. 'help' => 'Posts a comment - needs authentication!'
  99. ),
  100. );
  101. if ($q_method = get_http_var('method')) {
  102. $match = 0;
  103. foreach ($methods as $method => $data) {
  104. if (strtolower($q_method) == strtolower($method)) {
  105. $match++;
  106. if (get_http_var('docs')) {
  107. $_GET['verbose'] = 1;
  108. ob_start();
  109. }
  110. foreach ($data['parameters'] as $parameter) {
  111. if ($q_param = trim(get_http_var($parameter))) {
  112. $match++;
  113. include_once 'api_' . $method . '.php';
  114. api_call_user_func_or_error('api_' . $method . '_' . $parameter, array($q_param), 'API call not yet functional', 'api');
  115. break;
  116. }
  117. }
  118. if ($match == 1 && (get_http_var('output') || !get_http_var('docs'))) {
  119. if ($data['required']) {
  120. api_error('No parameter provided to function "' .
  121. htmlspecialchars($q_method) .
  122. '". Possible choices are: ' .
  123. join(', ', $data['parameters']) );
  124. } else {
  125. include_once 'api_' . $method . '.php';
  126. api_call_user_func_or_error('api_' . $method, null, 'API call not yet functional', 'api');
  127. break;
  128. }
  129. }
  130. break;
  131. }
  132. }
  133. if (!$match) {
  134. api_front_page('Unknown function "' . htmlspecialchars($q_method) .
  135. '". Possible functions are: ' .
  136. join(', ', array_keys($methods)) );
  137. } else {
  138. if (get_http_var('docs')) {
  139. $explorer = ob_get_clean();
  140. api_documentation_front($method, $explorer);
  141. }
  142. }
  143. } else {
  144. api_front_page();
  145. }
  146. function api_documentation_front($method, $explorer) {
  147. global $PAGE, $this_page, $DATA, $methods;
  148. $this_page = 'api_doc_front';
  149. $DATA->set_page_metadata($this_page, 'title', "$method function");
  150. $PAGE->page_start();
  151. $PAGE->stripe_start();
  152. include_once 'api_' . $method . '.php';
  153. print '<p align="center"><strong>http://www.theyworkforyou.com/api/' . $method . '</strong></p>';
  154. api_call_user_func_or_error('api_' . $method . '_front', null, 'No documentation yet', 'html');
  155. ?>
  156. <h4>Explorer</h4>
  157. <p>Try out this function without writing any code!</p>
  158. <form method="get" action="?#output">
  159. <p>
  160. <? foreach ($methods[$method]['parameters'] as $parameter) {
  161. print $parameter . ': <input type="text" name="'.$parameter.'" value="';
  162. if ($val = get_http_var($parameter))
  163. print htmlspecialchars($val);
  164. print '" size="30"><br>';
  165. }
  166. ?>
  167. Output:
  168. <input id="output_js" type="radio" name="output" value="js"<? if (get_http_var('output')=='js' || !get_http_var('output')) print ' checked'?>>
  169. <label for="output_js">JS</label>
  170. <input id="output_xml" type="radio" name="output" value="xml"<? if (get_http_var('output')=='xml') print ' checked'?>>
  171. <label for="output_xml">XML</label>
  172. <input id="output_php" type="radio" name="output" value="php"<? if (get_http_var('output')=='php') print ' checked'?>>
  173. <label for="output_php">Serialised PHP</label>
  174. <input id="output_rabx" type="radio" name="output" value="rabx"<? if (get_http_var('output')=='rabx') print ' checked'?>>
  175. <label for="output_rabx">RABX</label>
  176. <input type="submit" value="Go">
  177. </p>
  178. </form>
  179. <?
  180. if ($explorer) {
  181. $qs = array();
  182. foreach ($methods[$method]['parameters'] as $parameter) {
  183. if (get_http_var($parameter))
  184. $qs[] = htmlspecialchars(rawurlencode($parameter) . '=' . urlencode(get_http_var($parameter)));
  185. }
  186. print '<h4><a name="output"></a>Output</h4>';
  187. print '<p>URL for this: <strong>http://www.theyworkforyou.com/api/';
  188. print $method . '?' . join('&amp;', $qs) . '&amp;output='.get_http_var('output').'</strong></p>';
  189. print '<pre>' . htmlspecialchars($explorer) . '</pre>';
  190. }
  191. $sidebar = api_sidebar();
  192. $PAGE->stripe_end(array($sidebar));
  193. $PAGE->page_end();
  194. }
  195. function api_front_page($error = '') {
  196. global $PAGE, $methods, $this_page;
  197. $this_page = 'api_front';
  198. $PAGE->page_start();
  199. $PAGE->stripe_start();
  200. if ($error) {
  201. print "<p style='color: #cc0000'>$error</p>";
  202. }
  203. ?>
  204. <p>Welcome to TheyWorkForYou's API section, where you can learn how to query our database for information.</p>
  205. <h3>Overview</h3>
  206. <p>All requests take a number of parameters. <em>output</em> is optional, and defaults to <kbd>js</kbd>.</p>
  207. <p align="center"><strong>http://www.theyworkforyou.com/api/<em>function</em>?output=<em>output</em>&<em>other_variables</em></strong></p>
  208. <p>The current version of the API is <em>1.0.0</em>. If we make changes
  209. to the API,
  210. we'll increase the version number and make it an argument so you can still
  211. use the old version.</p>
  212. <h3>Outputs</h3>
  213. <p>The <em>output</em> argument can take any of the following values:
  214. <ul>
  215. <li><strong>xml</strong>. XML. The root element is twfy.</li>
  216. <li><strong>php</strong>. Serialized PHP, that can be turned back into useful information with the unserialize() command. Quite useful in Python as well, using <a href="http://hurring.com/code/python/serialize/">PHPUnserialize</a>.</li>
  217. <li><strong>js</strong>. A JavaScript object. You can provide a callback
  218. function with the <em>callback</em> variable, and then that function will be
  219. called with the data as its argument.</li>
  220. <li><strong>rabx</strong>. "RPC over Anything But XML".</li>
  221. </ul>
  222. <h3>Licensing</h3>
  223. <p>To use parliamentary material yourself (that's data returned from
  224. getDebates, getWrans, and getWMS), you will need to get a
  225. <a href="http://www.opsi.gov.uk/click-use/parliamentary-licence-information/index.htm">Parliamentary Licence</a> from the Office of Public Sector
  226. Information.
  227. <? /* All Ordnance Survey data (returned by getGeometry and getBoundary) is
  228. covered by the <acronym title="Pan-Government Agreement">PGA</acronym>
  229. under the <a href="http://www.dca.gov.uk/">Department for Constitutional
  230. Affairs</a>; you will have to get your own licence to re-use them. */ ?>
  231. Our own data - lists of MPs, Lords, constituencies and so on - is available
  232. under the <a href="http://creativecommons.org/licenses/by-sa/2.5/">Creative
  233. Commons Attribution-ShareAlike license version 2.5</a>.
  234. <p>Low volume, non-commercial use of the API service itself is free. Please
  235. <a href="/contact">contact us</a> for commercial use, or if you are about
  236. to use the service on a large scale.
  237. <h3>Bindings</h3>
  238. <p>These help you interface with the API more easily in a particular language:</p>
  239. <ul>
  240. <li><a href="http://codefluency.com/2006/11/21/tfwy-1-0-0-released">Ruby</a> (thanks to Bruce Williams and Martin Owen)</li>
  241. <li><a href="http://search.cpan.org/~sden/WebService-TWFY-API-0.01/lib/WebService/TWFY/API.pm">Perl</a> (thanks to Spiros Denaxas)</li>
  242. </ul>
  243. <p>If anyone wishes to write bindings for the API in any language, please
  244. do so, let us know and we'll link to it here. You might want to
  245. <a href="https://secure.mysociety.org/admin/lists/mailman/listinfo/developers-public">join our public developer mailing list</a>
  246. to discuss things.</p>
  247. <h3>Examples</h3>
  248. <ul>
  249. <li><a href="http://www.dracos.co.uk/work/theyworkforyou/api/postcode/">Postcode to constituency lookup, with no server side code</a> - use this to add constituency or MP lookup to a form on your website.
  250. <li><a href="http://www.dracos.co.uk/work/theyworkforyou/api/map/">Map showing location of all 646 constituencies, with no server side code</a> - example code using JavaScript and Google Maps.
  251. <li><a href="javascript:function foo(r){if(r.twfy.url)window.location=r.twfy.url;};(function(){var s=document.createElement('script');s.setAttribute('src','http://theyworkforyou.com/api/convertURL?callback=foo&url='+encodeURIComponent(window.location));s.setAttribute('type','text/javascript');document.getElementsByTagName('head')[0].appendChild(s);})()">Hansard prettifier</a> - drag this bookmarklet to your bookmarks bar, or bookmark it. Then if you ever find yourself on the official site, clicking this will try and take you to the equivalent page on TheyWorkForYou. (Tested in IE, Firefox, Opera.)</li>
  252. <li><a href="http://www.dracos.co.uk/work/theyworkforyou/api/fabfarts/">Matthew's MP Fab Farts</a> - every technology has the capacity to be used for fun.
  253. <li><a href="telnet://seagrass.goatchurch.org.uk:646/">Francis' MP Fight telnet text adventure</a> (<s>and <a href="http://caesious.beasts.org/~chris/scripts/mpfight">Chris' web version</a></s>) - battle your way to Sedgefield!
  254. <li><a href="http://www.straw-dogs.co.uk/10/15/your-mp-google-desktop-gadget/">Your MP - Google Desktop Gadget</a> - with GPL source code
  255. </ul>
  256. <?
  257. $sidebar = api_sidebar();
  258. $PAGE->stripe_end(array($sidebar));
  259. $PAGE->page_end();
  260. }
  261. function api_sidebar() {
  262. global $methods;
  263. $sidebar = '<div class="block"><h4>API Functions</h4> <div class="blockbody"><ul>';
  264. foreach ($methods as $method => $data){
  265. $sidebar .= '<li';
  266. if (isset($data['new']))
  267. $sidebar .= ' style="border-top: solid 1px #999999;"';
  268. $sidebar .= '>';
  269. if (!isset($data['working']) || $data['working'])
  270. $sidebar .= '<a href="' . WEBPATH . 'api/docs/' . $method . '">';
  271. $sidebar .= $method;
  272. if (!isset($data['working']) || $data['working'])
  273. $sidebar .= '</a>';
  274. else
  275. $sidebar .= ' - <em>not written yet</em>';
  276. # if ($data['required'])
  277. # $sidebar .= ' (parameter required)';
  278. # else
  279. # $sidebar .= ' (parameter optional)';
  280. $sidebar .= '<br>' . $data['help'];
  281. # $sidebar .= '<ul>';
  282. # foreach ($data['parameters'] as $parameter) {
  283. # $sidebar .= '<li>' . $parameter . '</li>';
  284. # }
  285. # $sidebar .= '</ul>';
  286. $sidebar .= '</li>';
  287. }
  288. $sidebar .= '</ul></div></div>';
  289. $sidebar = array(
  290. 'type' => 'html',
  291. 'content' => $sidebar
  292. );
  293. return $sidebar;
  294. }
  295. ?>