sflphone.funcs.php 3.55 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
<?php

/**
 * Copyright (C) 2007 - Savoir-Faire Linux Inc.
 * Author: Alexandre Bourget <alexandre.bourget@savoirfairelinux.com>
 *
 * LICENCE: GPL
 */


require_once('config.inc.php');


/**
 * Helper functions to translate AsciiDoc files into HTML, display
 * them and cache them.
 */


/**
21
 * Retrieve page, compile it if new, cache it.
22
 *
23
 * @param string File name (without the .txt) in the $PREFIX dir.
24 25
 * @return HTML content
 */
26
function get_page($page) {
27 28 29 30 31
  // Compile it
  
  // Get the latest HASH for that page.
  $hash = get_git_hash($page);

32 33 34 35
  if (!$hash) {
    return "Page '$page' not found.<br />\n";
  }

36 37 38 39 40 41 42 43 44 45 46 47 48 49 50
  $cnt = get_cache_hash($hash);

  if (!$cnt) {
    $cnt = compile_page($hash, $page /* for ref only */);

    put_cache_hash($hash, $cnt);

    return $cnt;
  }

  return $cnt;
  
}


51 52 53 54 55 56 57 58 59 60 61 62
/**
 * Show page
 *
 * @param string File name (without the .txt) in the $PREFIX dir.
 */
function show_page($page) {
  print get_page($page);

}



63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96
/**
 * Create the Cache dir if it doesn't exist.
 */
function check_cache_path() {
  global $CACHE_PATH;

  if (!file_exists($CACHE_PATH)) {
    mkdir($CACHE_PATH);
  }
}


/**
 * Check if the cache dir if this object was cached, if so, return it.
 *
 * @return mixed NULL or the content of the cache
 */
function get_cache_hash($hash) {
  global $CACHE_PATH;
  
  $fn = $CACHE_PATH.'/'.$hash.'.cache';

  if (file_exists($fn)) {
    return file_get_contents($fn);
  }

  return NULL;
}


/**
 * Write content to cache (identified by $hash)
 */
function put_cache_hash($hash, $content) {
97
  global $CACHE_PATH;
98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116
  
  $fn = $CACHE_PATH.'/'.$hash.'.cache';

  file_put_contents($fn, $content);

  return true;
}


/**
 * Just compile the page
 *
 * @return string Content of the compiled page.
 */
function compile_page($hash, $page) {
  global $GIT_REPOS;

  $output = '';

117
  $p = popen("GIT_DIR=".$GIT_REPOS." git-show $hash | asciidoc --no-header-footer -", 'r');
118 119 120 121 122 123

  if (!$p) {
    return "Unable to compile file: $page ($hash)\n";
  }

  while (!feof($p)) {
124
    $output .= fread($p, 1024);
125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153
  }
  pclose($p);

  return $output;
}


/**
 * Retrieve file from git's object-ocean
 *
 * UNUSED
 */
function get_git_file_content($file) {
  $hash = get_git_hash($file);

  $content = get_git_hash_content($hash);

  return $content;
}


/**
 * Retrieve hash's content from git's object-ocean
 *
 * UNUSED
 */
function get_git_hash_content($hash) {
  global $GIT_REPOS;

154 155
  $output = array();
  $content = exec("GIT_DIR=".$GIT_REPOS." git-show $hash", $output);
156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171

  return $content;
}

/**
 * Get the file's SHA-1 hash, for the latest revision on USE_BRANCH
 *
 * Used for comparison of cached/to cache/cache filename.
 *
 * @return string SHA-1 hash
 */
function get_git_hash($file) {
  global $USE_BRANCH, $GIT_REPOS;

  $output = array();

172 173 174
  $cmd = "cd $GIT_REPOS; git-ls-tree $USE_BRANCH \"".git_filename($file).".txt\"";

  $string = exec($cmd, $output);
175 176 177 178 179 180

  if (count($output)) {
    $fields = explode(' ', $output[0]);

    if ($fields[1] == 'blob') {
      // Return the HASH
181 182
      $subfields = explode("\t", $fields[2]);
      return $subfields[0];
183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217
    }
  }

  return NULL;
}

/**
 * Get file name (parsed and clear for git-ls-tree)
 *
 * @return string Parsed file name
 */
function git_filename($file) {
  global $PREFIX;
  // remove all '//', for '/', remove the trailing '/' at the beginning
  // add the PREFIX

  $out = $PREFIX . '/' . $file;

  $out = str_replace('//', '/', $out);
  $out = str_replace('//', '/', $out); // In case there are 3 in a row

  $out = ltrim($out, '/');

  return $out;
}




/** Check cache path right away, at each run. */

check_cache_path();


?>