Skip to content

Instantly share code, notes, and snippets.

@saerdnaer
Created March 21, 2010 12:52
Show Gist options
  • Save saerdnaer/339289 to your computer and use it in GitHub Desktop.
Save saerdnaer/339289 to your computer and use it in GitHub Desktop.
Pentabarf Fahrplan Parser in PHP generating output in MediaWiki markup style
#! /usr/bin/php
<?php
/*
Ein Pentabarf Fahrplan Parser in PHP, der eine Textdatei ausspuckt, die man dem pyWikipedia-Bot vorwerfen kann.
*/
$filename = 'schedule.en.xml.0.30';
$xml = simplexml_load_file($filename);
// some functions
function time2int($time_sting)
{
$time = explode(':', $time_sting);
return $time[0] + ($time[1]/60);
}
function int2time($time_int)
{
$hours = floor($time_int);
$minutes = ($time_int - $hours) * 60;
return sprintf('%d:%02d', $hours, $minutes);
}
function add_new_page($page_title, $page_text)
{
echo '{{-start-}}' . "\n'''", $page_title, "'''", "\n", $page_text, "\n{{-stop-}}\n";
}
function gen_event_page($event)
{
// Redirect for Event ID
add_new_page($event['id'], '#REDIRECT [[' . $event->title . ']]');
// Redirect for Event
add_new_page($event->tag, '#REDIRECT [[' . $event->title . ']]');
$text = '{{Event' . "\n";
$text .= '|id=' . $event['id'] . "\n";
$text .= '|day=' . $event->day['index'] . "\n";
$text .= '|start=' . $event->start . "\n";
$text .= '|duration=' . $event->duration . "\n";
$text .= '|room=' . $event->room . "\n";
$text .= '|tag=' . $event->tag . "\n";
$text .= '|title=' . $event->title . "\n";
$text .= '|subtitle=' . $event->subtitle . "\n";
$text .= '|track=' . $event->track . "\n";
$text .= '|type=' . $event->type . "\n";
$text .= '|language=' . $event->language . "\n";
$text .= '|persons=' . "\n";
foreach ($event->persons->person AS $person)
{
$text .= '{{Person|' . $person['id'] . '|'. $person . '}}' . "\n";
}
$text .= '}}' . "\n\n";
$text .= $event->abstract ."\n\n";
$text .= $event->description ."\n\n";
$text .= '==Links==' . "\n";
if ( !empty($events->links) )
{
foreach ($events->links AS $link)
{
$text .= '* [' . $link->href . ' ' . $link . ']' . "\n";
}
}
$text .= "\n\n"
;
$text .= '==Recordings==' . "\n";
$text .= '* Please add here as soon as they are available' . "\n";
add_new_page($event->title, $text);
}
// Falls Ausgabe als HTML erfolgen soll
// ?html an URL anhängen
if ( !empty($_GET['html']) )
{
echo '<pre>';
}
// read XML file
$fahrplan_overview = array();
$rooms = array();
$rooms_reverse = array();
foreach($xml->day AS $day)
{
foreach($day AS $room)
{
$room_name = trim((string) $room['name']);
if ( empty($rooms_reverse[$room_name]) )
{
$rooms[] = $room_name;
$rooms_reverse[$room_name] = count($rooms);
}
foreach($room AS $event)
{
// create event page
$event->day = $day;
gen_event_page($event);
// add entry for overview
$fahrplan_overview[(string) $day['index']]
[(string) $event->start]
[$rooms_reverse[(string) $room['name']]] = array(
(string) $event->duration,
'[[' . $event->title . ']]' . "<br />\n" . $event->subtitle . "\n\n" . $event->abstract
);
}
}
}
// create overview page
$rooms_count = count($rooms);
$timeslot_duration_time = (string) $xml->conference->timeslot_duration;
$timeslot_duration = time2int($timeslot_duration_time);
$text = '';
foreach ($fahrplan_overview AS $day_index => $day)
{
$text .= '==Day ' . $day_index . '==' . "\n";
$text .= '{| border="1" cellspacing="0" cellpadding="4" rules="all" style="margin:1em 1em 1em 0; border:1px solid; border-collapse:collapse; empty-cells:show;" width="100%"' . "\n";
$text .= '|--- ' . "\n";
$text .= '! Time' . "\n";
$start_time = key($day);
foreach ($rooms AS $row)
{
$text .= '! width="45%" | ' . $row . "\n";
}
ksort($day);
$start = time2int($start_time);
$room_busy = array();
for ($i = $start; ; $i += $timeslot_duration)
{
if ( $i == 24 )
{
$i = 0;
}
$time = int2time($i);
$text .= '|--- style="vertical-align:top;"' . "\n";
$text .= '! ' . int2time($i) . "\n";
for ($j = 1; $j <= $rooms_count; $j++)
{
// Vortrag für Slot vorhanden
if ( !empty($day[$time][$j]) )
{
if ( $day[$time][$j][0] != $timeslot_duration_time )
{
$duration = time2int($day[$time][$j][0]);
$text .= '|rowspan="' . ($duration / $timeslot_duration) . '"';
$room_busy[$j] = ($duration / $timeslot_duration) - 1;
}
$text .= '| ' . $day[$time][$j][1] . "\n";
}
// Vorheriger Vortrag reicht in momentanen Slot hinein -> keine Zelle
else if ( !empty($room_busy[$j]) )
{
$room_busy[$j]--;
}
// Vorheriger Vortrag reicht nicht in momentanen Slot hinein -> Leere Zelle
else
{
$text .= '| ' . "\n";
}
}
if ( $i >= 1.5 && $i <= 10 )
{
break;
}
}
$text .= '|}' . "\n\n";
}
add_new_page('Fahrplan Overview', $text);
// Falls Ausgabe als HTML erfolgen soll
if ( !empty($_GET['html']) )
{
echo '</pre>';
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment