-
-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathapi_convertURL.php
More file actions
116 lines (105 loc) · 3.89 KB
/
Copy pathapi_convertURL.php
File metadata and controls
116 lines (105 loc) · 3.89 KB
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
<?php
/**
* @file
*/
/**
*
*/
function api_convertURL_front() {
?>
<p><big>Converts an aph.gov.au Hansard URL into an OpenAustralia one, if possible.</big></p>
<h4>Arguments</h4>
<dl>
<dt>url (required)</dt>
<dd>The aph.gov.au 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');
print $q->field(0, 'source_url');
?>
</dd>
</dl>
<h4>Example Response</h4>
<code>{
gid : "uk.org.publicwhip/debate/2006-07-11a.1352.2",
url : "http://www.openaustralia.org/debates/?id=2006-07-11a.1311.0#g1352.2"
}</code>
<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 foo(r){if(r.url)window.location=r.url;};(function(){var s=document.createElement('script');s.setAttribute('src','http://openaustralia.org/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 OpenAustralia. (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 = [
'gid' => fix_gid_from_db($q->field(0, 'gid')),
'major' => $q->field(0, 'major'),
'htype' => $q->field(0, 'htype'),
'subsection_id' => $q->field(0, 'subsection_id'),
];
$db = new ParlDB();
$LISTURL = new URL($hansardmajors[$id_data['major']]['page_all']);
$fragment = '';
if ($id_data['htype'] == '11' || $id_data['htype'] == '10') {
$LISTURL->insert(['id' => $id_data['gid']]);
} else {
$parent_epobject_id = $id_data['subsection_id'];
$parent_gid = '';
$r = $db->query("SELECT gid
FROM hansard
WHERE epobject_id = '" . $db->escape($parent_epobject_id) . "'
");
if ($r->rows() > 0) {
$parent_gid = fix_gid_from_db($r->field(0, 'gid'));
}
if ($parent_gid != '') {
$LISTURL->insert(['id' => $parent_gid]);
$fragment = '#g' . gid_to_anchor($id_data['gid']);
}
}
return $LISTURL->generate('none') . $fragment;
}
/**
*
*/
function api_converturl_url_output($q) {
$gid = $q->field(0, 'gid');
$url = get_listurl($q);
$output = [
'gid' => $gid,
'url' => 'http://www.openaustralia.org' . $url
];
api_output($output);
}
/**
*
*/
function api_converturl_url($url) {
$db = new ParlDB();
$url_nohash = preg_replace('/#.*/', '', $url);
$q = $db->query('select gid,major,htype,subsection_id from hansard where source_url = "' . $db->escape($url) . '" order by gid limit 1');
if ($q->rows()) {
return api_converturl_url_output($q);
}
$q = $db->query('select gid,major,htype,subsection_id from hansard where source_url like "' . $db->escape($url_nohash) . '%" order by gid limit 1');
if ($q->rows()) {
return api_converturl_url_output($q);
}
$url_bound = str_replace('cmhansrd/cm', 'cmhansrd/vo', $url_nohash);
if ($url_bound != $url_nohash) {
$q = $db->query('select gid,major,htype,subsection_id from hansard where source_url like "' . $db->escape($url_bound) . '%" order by gid limit 1');
if ($q->rows()) {
return api_converturl_url_output($q);
}
}
api_error('Sorry, URL could not be converted');
}