Skip to content

Instantly share code, notes, and snippets.

@dafrancis
Created January 23, 2012 23:57
Show Gist options
  • Save dafrancis/1666598 to your computer and use it in GitHub Desktop.
Save dafrancis/1666598 to your computer and use it in GitHub Desktop.
FOSDEM Schedule Viewer. Parses the XML from the FOSDEM site to give a simple HTML interface (.htaccess is optional. This works without it as well!)
Options +FollowSymlinks
RewriteEngine on
RewriteBase /fosdem/
RewriteRule ^(.*) index.php?rewrit
<?php
/**
* ScheduleParser - Parses the FOSDEM timetable and outputs it in an "easy to read" format
*
* @package default
* @author Afal
**/
class ScheduleParser{
/**
* SimpleXML object containing the data from the fosdem website.
*/
private $xml;
private $levels;
private $isRewrite;
public function __construct(){
$this->xml = simplexml_load_file('http://fosdem.org/schedule/xml');
$this->levels = array('day','room','event');
$this->isRewrite = isset($_GET['rewrite']);
if($this->isRewrite){
$this->__rewrite();
}
}
private function get_event($room){
$event = $this->find_in_xml($room, 'event', 'id');
return sprintf('<h3>%s</h3><em>%s</em><p>%s</p><p><a href="%s">Back</a></p>', $event->title, $event->abstract, $event->description, $this->link_builder(array(), array('event')));
}
private function get_room($day){
$room = $this->find_in_xml($day, 'room', 'name');
$return = sprintf('<h3>%s</h3>', $room['name']);
if(isset($_GET['event']))
return $return.$this->get_event($room);
foreach($room->event as $event)
$return .= sprintf('<p><a href="%s">%s -&gt; %s</a></p>', $this->link_builder(array('event' => $event['id'])), $event->track, $event->title);
return $return.sprintf('<p><a href="%s">Back</a></p>', $this->link_builder(array(), array('room')));
}
private function get_day(){
$day = $this->find_in_xml($this->xml, 'day', 'index');
$return = sprintf('<h3>%s - %s</h3>', date('l', strtotime((string)$day['date'])), $day['date']);
if(isset($_GET['room']))
return $return.$this->get_room($day);
foreach($day->room as $room){
if(sizeof($room->children())){
$tracks = array();
foreach($room->event as $event)
if(!isset($tracks[(string)$event->track]))
$tracks[(string)$event->track] = true;
$return .= sprintf('<p><a href="%s">%s -&gt; %s</a></p>', $this->link_builder(array('room' => $room['name'])), implode(" and ",array_keys($tracks)), $room['name']);
}
}
return $return.sprintf('<p><a href="%s">Back</a></p>', $this->link_builder(array(), array('day')));
}
public function __toString(){
$conf = $this->xml->conference;
$return = sprintf('<h1>%s</h1><h2>%s</h2>', $conf->title, $conf->subtitle);
if(isset($_GET['day']) and $_GET['day'] != '')
return $return.$this->get_day();
//rest of the details???
foreach($this->xml->day as $day)
$return .= sprintf('<p><a href="%s">%s - %s</a></p>', $this->link_builder(array('day' => $day['index'])), date('l', strtotime((string)$day['date'])), $day['date']);
return $return;
}
/**
* find_in_xml - a simple function to make finding an item in a set easier
* e.g. event in a room
*
* @param $xml SimpleXML object in which to search in
* @param $get Element to find (The $_GET should be the same name)
* @param $comp The attribute to compare the $_GET to
* @return SimpleXML object of what is being found otherwise die()
* @author Afal
**/
private function find_in_xml($xml, $get, $comp){
foreach($xml->$get as $node)
if($node[$comp]==$_GET[$get])
return $node;
die(sprintf("<p>%s not found</p>", ucwords($get)));
}
/**
* link_builder - a function for making urls for links easier
*
* @param $add Array to add to $_GET
* @param $remove Array to remove from $_GET
* @return String representing the url
* @author Afal
**/
private function link_builder($add=array(), $remove=array()){
foreach($_GET as $k => $v)
if(!isset($add[$k]) and !in_array($k,$remove))
$add[$k] = $v;
$params = array();
foreach($add as $k => $v)
$params[] = "$k=$v";
if($this->isRewrite){
return $this->rewrite($add);
}
else{
return "index.php?".implode("&", $params);
}
}
private function rewrite($add){
$url = preg_replace("/index\.php/","",$_SERVER['SCRIPT_NAME']);
foreach($this->levels as $level){
if(isset($add[$level])){
$url .= $add[$level].'/';
}
}
return $url;
}
private function __rewrite(){
$requestURI = explode('/', $_SERVER['REQUEST_URI']);
$scriptName = explode('/',$_SERVER['SCRIPT_NAME']);
for($i= 0;$i < sizeof($scriptName);$i++){
if (in_array($requestURI[$i], $scriptName)){
unset($requestURI[$i]);
}
}
$command = array_values($requestURI);
$levels = $this->levels;
foreach($command as $comm){
if($comm=="") break;
$_GET[array_shift($levels)] = $comm;
}
}
}
echo new ScheduleParser();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment