Created
July 27, 2011 08:31
-
-
Save nickdunn/1108922 to your computer and use it in GitHub Desktop.
Symphony RDF content negotiation (hacky as crap)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
require_once(CORE . '/class.frontend.php'); | |
Class extension_seme4_semantics extends Extension { | |
public function about() { | |
return array('name' => 'Seme4 Semantics', | |
'version' => '1.1', | |
'release-date' => '2010-03-01', | |
'author' => array('name' => 'Nick Dunn', | |
'website' => 'http://nick-dunn.co.uk/', | |
'email' => '[email protected]'), | |
'description' => 'Allows content negotiation for URIs and serving of HTML and RDF versions.' | |
); | |
} | |
public function getSubscribedDelegates() { | |
return array( | |
array( | |
'page' => '/frontend/', | |
'delegate' => 'FrontendInitialised', | |
'callback' => 'frontendInitialised' | |
) | |
); | |
} | |
/*------------------------------------------------------------------------- | |
Caching | |
-------------------------------------------------------------------------*/ | |
public function frontendInitialised(&$context) { | |
$uri = getCurrentPage(); | |
$uri = trim($uri, '/'); | |
// is the request for a specific render resource (HTML or RDF) | |
if (preg_match('/(rdf|html)$/', $uri)) { | |
$extension = end(explode('.', $uri)); | |
} | |
// translate URL into a possible RDF file name | |
$rdf_file = preg_replace('/\//', '-', $uri); | |
// remove file extension from end of URI | |
if ($extension) $rdf_file = preg_replace('/.' . $extension . '$/', '', $rdf_file); | |
$rdf_file = trim($rdf_file, '-'); | |
$rdf_file .= '.rdf'; | |
$rdf_file_path = WORKSPACE . '/rdf/' . $rdf_file; | |
// does the client accept application/rdf+xml? | |
$accepts_rdf = preg_match('/application\/rdf\+xml/', $_SERVER['HTTP_ACCEPT']); | |
// if the RDF file exists, this URI has been semanticised... | |
if (file_exists($rdf_file_path)) { | |
// no extension (this is the resource identifier URI), so perform content negotiation | |
if (!$extension) { | |
$resource_type = 'html'; | |
if ($accepts_rdf) $resource_type = 'rdf'; | |
header('Location: ' . URL . '/' . $uri . '.' . $resource_type, true, 303); | |
} | |
// this is not a resource identifier URI, but a rendering, so serve correct content | |
else { | |
switch ($extension) { | |
// read RDF file | |
case 'rdf': | |
$rdf_data = fopen($rdf_file_path, 'r'); | |
header('Content-Type: application/rdf+xml'); | |
echo fread($rdf_data, filesize($rdf_file_path)); | |
fclose($rdf_data); | |
exit(); | |
break; | |
// manipulate URL and allow Symphony to serve | |
case 'html': | |
$context['page'] = preg_replace('/.' . $extension . '$/', '', $uri); | |
break; | |
} | |
} | |
} | |
// no RDF exists | |
else { | |
// but client is expecting RDF | |
if ($accepts_rdf) header("HTTP/1.1 406 Not acceptable"); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment