Changeset 254 in t29-www for lib/technikum29.php


Ignore:
Timestamp:
May 25, 2012, 7:58:22 AM (12 years ago)
Author:
sven
Message:

Weiterarbeit an t29v6 (5 Stunden):

  • News werden dynamisch als YAML aus einem Heredoc aus news.php rausgelesen und in das Menü eingepflegt (String-Ersetzung)
  • Caching-System: Jede generierte Seite wird nun gecacht
  • Objektorientierung: Der Library-Code ist pseudo-OOP
  • PHP 5.3-Abhängigkeit - keine create_function-Aufrufe mehr sondern Closures
File:
1 edited

Legend:

Unmodified
Added
Removed
  • lib/technikum29.php

    r252 r254  
    66 *
    77 **/
    8  
     8
     9if(defined('T29')) return false; // no nesting
     10define('T29', true);
     11
    912$lib = dirname(__FILE__);
    10 $lang = "de";
     13$lang = "de-v6";
    1114$root = "/"; # webroot
     15$file = substr($_SERVER['SCRIPT_FILENAME'], strlen($_SERVER['DOCUMENT_ROOT']));
    1216
    13 require "$lib/template.php";
     17$skip_cache = isset($_GET['skip_cache']);
     18$purge_cache = isset($_GET['purge_cache']);
    1419
    15 register_shutdown_function('print_footer');
    16 print_header();
     20$cache_dir = "$lib/../shared/cache";
    1721
    18 #include("$lib/menu.php");
     22# lightweight caching system
     23$test_programs = array(
     24        __FILE__,
     25        $_SERVER['SCRIPT_FILENAME'],
     26        "$lib/template.php",
     27        "$lib/menu.php",
     28        "$lib/../de-v6/hauptnavigation.xml",
     29        "$lib/../de-v6/sidebar.xml",
     30        "$lib/../de-v6/news.php",
     31);
    1932
    20 function t29dom_add_class($simplexml_element, $value) {
    21         $dom = dom_import_simplexml($simplexml_element);
    22         $simplexml_element['class'] =
    23                 ($dom->hasAttribute("class") ? ($simplexml_element['class'].' '):'').$value;
     33$cache_file = $cache_dir . $file;
     34$last_cache = @filemtime($cache_dir.$file);
     35$last_program = array_reduce(array_map(function($x){return @filemtime($x);}, $test_programs), 'max');
     36$cache_valid = $last_cache && $last_program < $last_cache;
     37
     38if(!$cache_valid || $skip_cache || $purge_cache) {
     39        // rebuild cache
     40        require "$lib/template.php";
     41        $tmpl = new t29Template($GLOBALS);
     42        $tmpl->create_cache();
     43} else {
     44        // use cache
     45        header("Last-Modified: ".gmdate("D, d M Y H:i:s", $last_cache)." GMT");
     46        //header("Etag: $etag");
     47
     48        if(@strtotime($_SERVER['HTTP_IF_MODIFIED_SINCE']) == $last_cache) {
     49                // client already has page cached locally
     50                header("HTTP/1.1 304 Not Modified");
     51        } else {
     52                readfile($cache_file);
     53        }
     54        exit;
    2455}
    2556
    26 function print_menu($file) {
    27         global $lib,$seiten_id;
    28         $xml = simplexml_load_file("$lib/../de-v6/$file.xml");
    29        
    30         // aktuelle Seite anmarkern und Hierarchie hochgehen
    31         // (<ul><li>bla<ul><li>bla<ul><li>hierbin ich <- hochgehen.)
    32         $current_a = $xml->xpath("//a[@seiten_id='$seiten_id']");
    33         if(count($current_a)) {
    34                 $current_li = $current_a[0]->xpath("parent::li");
    35                 t29dom_add_class($current_li[0], "current");
    36                 $ancestors = $current_li[0]->xpath("ancestor-or-self::li");
    37                 array_walk($ancestors, create_function('$i', 't29dom_add_class($i, "active");'));
    38         }
    39 
    40         // Seiten-IDs (ungueltiges HTML) ummoddeln
    41         $all_ids = $xml->xpath("//a[@seiten_id]");
    42         foreach($all_ids as $a) {
    43                 $a['id'] = "sidebar_link_".$a['seiten_id'];
    44                 // umweg ueber DOM um Node zu loeschen
    45                 $adom = dom_import_simplexml($a);
    46                 $adom->removeAttribute('seiten_id');
    47         }
    48        
    49         print $xml->ul->asXML();
    50 }
    51 
    52 function print_relations() {
    53         global $lib, $seiten_id;
    54        
    55         $sidebar = simplexml_load_file("$lib/../de-v6/sidebar.xml");
    56         $current_a = $sidebar->xpath("//a[@seiten_id='$seiten_id']");
    57         if(count($current_a)) {
    58                 $prev = $current_a[0]->xpath("preceding::a[@seiten_id][1]");
    59                 if(count($prev)) {
    60                         $a = $prev[0];
    61                         print "<li class='prev'><a href='$a[href]'>vorherige Seite <strong>$a</strong></a></li>";
    62                 }
    63                 $next = $current_a[0]->xpath("following::a[@seiten_id][1]");
    64                 if(count($next)) {
    65                         $a = $next[0];
    66                         print "<li class='next'><a href='$a[href]'>nächste Seite <strong>$a</strong></a></li>";
    67                 }
    68         } else {
    69                 print '<li class="start"><a href="#">Starte Führung <strong>Blabla</strong></a>';
    70         }
    71 }
     57// end of technikum29.php
Note: See TracChangeset for help on using the changeset viewer.
© 2008 - 2013 technikum29 • Sven Köppel • Some rights reserved
Powered by Trac
Expect where otherwise noted, content on this site is licensed under a Creative Commons 3.0 License