/data/vhost/matthew.theyworkforyou.dev.mysociety.org/theyworkforyou/www/docs/api/api_convertURL.php
<?php
function api_convertURL_front() {
?>
<p><big>Converts a parliament.uk Hansard URL into a TheyWorkForYou one, if possible.</big></p>
<h4>Arguments</h4>
<dl>
<dt>url (required)</dt>
<dd>The parliament.uk URL you wish to convert, e.g.
<?php $db = new ParlDB;
$q = $db->query('SELECT source_url FROM hansard WHERE major=1 AND hdate>"2006-07-01" ORDER BY RAND() LIMIT 1')->first();
print $q['source_url'];
?></dd>
</dl>
<h4>Example Response</h4>
<pre>{
gid : "uk.org.publicwhip/debate/2006-07-11a.1352.2",
url : "https://www.theyworkforyou.com/debates/?id=2006-07-11a.1311.0#g1352.2"
}</pre>
<h4>Example Use</h4>
<p>This probably counts as "AJAX", though it doesn't use XMLHTTP, asynchronicity, or XML, only cross-site JavaScript... It's definitely Web 2.1, at least.</p>
<ul><li><a href="javascript:function twfy_cb(r){if (r.url)window.location=r.url;};(function(d,s){s=d.createElement('script');s.src='https://www.theyworkforyou.com/api/convertURL?callback=twfy_cb&key=Gbr9QgCDzHExFzRwPWGAiUJ5&url='+encodeURIComponent(window.location);d.getElementsByTagName('head')[0].appendChild(s);})(document)">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></ul>
<?php
}
/* Very similar to function in hansardlist.php, but separated */
function get_listurl($q) {
global $hansardmajors;
$id_data = array(
'gid' => fix_gid_from_db($q['gid']),
'major' => $q['major'],
'htype' => $q['htype'],
'subsection_id' => $q['subsection_id'],
);
$db = new ParlDB;
$LISTURL = new \MySociety\TheyWorkForYou\Url($hansardmajors[$id_data['major']]['page_all']);
/data/vhost/matthew.theyworkforyou.dev.mysociety.org/theyworkforyou/www/docs/api/api_convertURL.php
<?php
function api_convertURL_front() {
?>
<p><big>Converts a parliament.uk Hansard URL into a TheyWorkForYou one, if possible.</big></p>
<h4>Arguments</h4>
<dl>
<dt>url (required)</dt>
<dd>The parliament.uk URL you wish to convert, e.g.
<?php $db = new ParlDB;
$q = $db->query('SELECT source_url FROM hansard WHERE major=1 AND hdate>"2006-07-01" ORDER BY RAND() LIMIT 1')->first();
print $q['source_url'];
?></dd>
</dl>
<h4>Example Response</h4>
<pre>{
gid : "uk.org.publicwhip/debate/2006-07-11a.1352.2",
url : "https://www.theyworkforyou.com/debates/?id=2006-07-11a.1311.0#g1352.2"
}</pre>
<h4>Example Use</h4>
<p>This probably counts as "AJAX", though it doesn't use XMLHTTP, asynchronicity, or XML, only cross-site JavaScript... It's definitely Web 2.1, at least.</p>
<ul><li><a href="javascript:function twfy_cb(r){if (r.url)window.location=r.url;};(function(d,s){s=d.createElement('script');s.src='https://www.theyworkforyou.com/api/convertURL?callback=twfy_cb&key=Gbr9QgCDzHExFzRwPWGAiUJ5&url='+encodeURIComponent(window.location);d.getElementsByTagName('head')[0].appendChild(s);})(document)">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></ul>
<?php
}
/* Very similar to function in hansardlist.php, but separated */
function get_listurl($q) {
global $hansardmajors;
$id_data = array(
'gid' => fix_gid_from_db($q['gid']),
'major' => $q['major'],
'htype' => $q['htype'],
'subsection_id' => $q['subsection_id'],
);
$db = new ParlDB;
$LISTURL = new \MySociety\TheyWorkForYou\Url($hansardmajors[$id_data['major']]['page_all']);
/data/vhost/matthew.theyworkforyou.dev.mysociety.org/theyworkforyou/www/docs/api/api_functions.php
// we only want to convert to iso if it's an actual API call
// so skip this if it's a documentation page
if (!get_http_var('docs')) {
// and then catch any errors in the conversion and just ignore
// them and return the unconverted results
$converted_out = @iconv('utf-8', 'iso-8859-1//TRANSLIT', $out);
if ($converted_out !== false) {
$out = $converted_out;
}
}
return $out;
}
# Call an API function
function api_call_user_func_or_error($function, $params, $error, $type) {
if (function_exists($function)) {
call_user_func_array($function, $params);
} elseif ($type == 'api') {
api_error($error);
} else {
print "<p style='color:#cc0000'>$error</p>";
}
}
# Used for testing for conditional responses
$cond_wkday_re = '(Sun|Mon|Tue|Wed|Thu|Fri|Sat)';
$cond_weekday_re = '(Sunday|Monday|Tuesday|Wednesday|Thursday|Friday|Saturday)';
$cond_month_re = '(Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec)';
$cond_month_map = array(
'Jan' => 1, 'Feb' => 2, 'Mar' => 3, 'Apr' => 4,
'May' => 5, 'Jun' => 6, 'Jul' => 7, 'Aug' => 8,
'Sep' => 9, 'Oct' => 10, 'Nov' => 11, 'Dec' => 12
);
$cond_date1_re = '(\d\d) ' . $cond_month_re . ' (\d\d\d\d)';
$cond_date2_re = '(\d\d)-' . $cond_month_re . '-(\d\d)';
/data/vhost/matthew.theyworkforyou.dev.mysociety.org/theyworkforyou/www/docs/api/index.php
}
}
} elseif (get_http_var('docs')) {
api_front_page();
} else {
$subscription = new MySociety\TheyWorkForYou\Subscription($THEUSER);
MySociety\TheyWorkForYou\Renderer::output('static/api-index', array(
'subscription' => $subscription->stripe,
));
}
function api_documentation_front($method, $explorer) {
global $PAGE, $this_page, $DATA, $THEUSER;
$this_page = 'api_doc_front';
$DATA->set_page_metadata($this_page, 'title', "$method function");
$PAGE->page_start();
$PAGE->stripe_start();
include_once 'api_'. $method . '.php';
print '<p align="center"><strong>https://www.theyworkforyou.com/api/' . $method . '</strong></p>';
api_call_user_func_or_error('api_' . $method . '_front', array(), 'No documentation yet', 'html');
if ($method != 'getQuota') {
api_documentation_explorer($method, $explorer);
}
$subscription = new MySociety\TheyWorkForYou\Subscription($THEUSER);
$sidebar = api_sidebar($subscription);
$PAGE->stripe_end(array($sidebar));
$PAGE->page_end();
}
function api_documentation_explorer($method, $explorer) {
global $methods;
?>
<h4>Explorer</h4>
<p>Try out this function without writing any code!</p>
<form method="get" action="?#output">
<p>
<?php foreach ($methods[$method]['parameters'] as $parameter) {
print $parameter . ': <input type="text" name="'.$parameter.'" value="';
if ($val = get_http_var($parameter))
/data/vhost/matthew.theyworkforyou.dev.mysociety.org/theyworkforyou/www/docs/api/index.php
break;
}
}
break;
}
}
if (!$match) {
api_log_call($key);
$msg = 'Unknown function "' . _htmlspecialchars($q_method) .
'". Possible functions are: ' .
join(', ', array_keys($methods));
if (get_http_var('output')) {
api_error($msg);
} else {
api_front_page($msg);
}
} else {
if (get_http_var('docs')) {
$explorer = ob_get_clean();
api_documentation_front($method, $explorer);
}
}
} elseif (get_http_var('docs')) {
api_front_page();
} else {
$subscription = new MySociety\TheyWorkForYou\Subscription($THEUSER);
MySociety\TheyWorkForYou\Renderer::output('static/api-index', array(
'subscription' => $subscription->stripe,
));
}
function api_documentation_front($method, $explorer) {
global $PAGE, $this_page, $DATA, $THEUSER;
$this_page = 'api_doc_front';
$DATA->set_page_metadata($this_page, 'title', "$method function");
$PAGE->page_start();
$PAGE->stripe_start();
include_once 'api_'. $method . '.php';
print '<p align="center"><strong>https://www.theyworkforyou.com/api/' . $method . '</strong></p>';
api_call_user_func_or_error('api_' . $method . '_front', array(), 'No documentation yet', 'html');