1<?php
2
3// recursive function to establish best sidebar file to be used
4function getSidebarFN($ns, $file) {
5
6  // check for wiki page = $ns:$file (or $file where no namespace)
7  $nsFile = ($ns) ? "$ns:$file" : $file;
8  if (file_exists(wikiFN($nsFile))) return wikiFN($nsFile);
9
10  // remove deepest namespace level and call function recursively
11
12  // no namespace left, exit with no file found
13  if (!$ns) return '';
14
15  $i = strrpos($ns, ":");
16  $ns = ($i) ? substr($ns, 0, $i) : false;
17  return getSidebarFN($ns, $file);
18}
19
20/**
21 * Returns xhtml for a virtual local sidebar file
22 * based on instructions from a master file
23 *
24 * Uses and creates a cachefile
25 *
26 * @author Jan Decaluwe <jan@jandecaluwe.com>
27 * by adapting the p_cached_xhtml function in dokuwiki
28 *
29 */
30function p_sidebar_cached_xhtml($localFile, $masterFile){
31  global $conf;
32  $cache  = getCacheName($localFile.$_SERVER['HTTP_HOST'].$_SERVER['SERVER_PORT'],'.xhtml');
33  $purge  = $conf['cachedir'].'/purgefile';
34  // check if cache can be used
35  $cachetime = @filemtime($cache); // 0 if not exists
36
37  if( @file_exists($masterFile)                                       // does the source exist
38      && $cachetime > @filemtime($masterFile)                         // cache is fresh
39      && ((time() - $cachetime) < $conf['cachetime'])                 // and is cachefile young enough
40      && !isset($_REQUEST['purge'])                                   // no purge param was set
41      && ($cachetime > @filemtime($purge))                            // and newer than the purgefile
42      && ($cachetime > @filemtime(DOKU_INC.'conf/dokuwiki.php'))      // newer than the config file
43      && ($cachetime > @filemtime(DOKU_INC.'conf/local.php'))         // newer than the local config file
44      && ($cachetime > @filemtime(DOKU_INC.'inc/parser/xhtml.php'))   // newer than the renderer
45      && ($cachetime > @filemtime(DOKU_INC.'inc/parser/parser.php'))  // newer than the parser
46      && ($cachetime > @filemtime(DOKU_INC.'inc/parser/handler.php')))// newer than the handler
47  {
48    //well then use the cache
49    $parsed = io_readfile($cache);
50    $parsed .= "\n<!-- cachefile $cache used -->\n";
51  }else{
52    $parsed = p_render('xhtml', p_cached_instructions($masterFile),$info); //try to use cached instructions
53
54    if($info['cache']){
55      io_saveFile($cache,$parsed); //save cachefile
56      $parsed .= "\n<!-- no cachefile used, but created -->\n";
57    }else{
58      @unlink($cache); //try to delete cachefile
59      $parsed .= "\n<!-- no cachefile used, caching forbidden -->\n";
60    }
61  }
62
63  return $parsed;
64}
65
66function html_sidebar() {
67  global $ID;
68  global $ACT;
69
70  if ($ACT != 'show') return '';
71
72  // determine master sidebar file
73  $masterFile = getSidebarFN(getNS($ID), 'sidebar');
74
75  // open sidebar <div>
76  echo("<div id='sidebar'>");
77
78  // determine what to display
79  if ($masterFile) {
80    // virtual hidden local sidebar filename
81    $fn = wikiFN($ID.'_sidebar');
82    $localFile = dirname($fn).'/_'.basename($fn);
83    print p_sidebar_cached_xhtml($localFile, $masterFile);
84  }
85  else {
86    html_index('.');
87  }
88
89  // close sidebar <div>
90  echo("</div>");
91}
92
93?>