mirror of
https://git.jami.net/savoirfairelinux/jami-daemon.git
synced 2025-08-12 22:09:25 +08:00
290 lines
5.6 KiB
PHP
290 lines
5.6 KiB
PHP
<?php
|
|
|
|
/**
|
|
* Copyright (C) 2007 - Savoir-Faire Linux Inc.
|
|
* Author: Alexandre Bourget <alexandre.bourget@savoirfairelinux.com>
|
|
*
|
|
* LICENCE: GPLv3
|
|
*/
|
|
|
|
|
|
require_once('config.inc.php');
|
|
|
|
|
|
/**
|
|
* Helper functions to translate AsciiDoc files into HTML, display
|
|
* them and cache them.
|
|
*/
|
|
|
|
|
|
/**
|
|
* Retrieve page, compile it if new, cache it.
|
|
*
|
|
* @param string File name (including the extension) in the $PREFIX dir.
|
|
* @return HTML content
|
|
*/
|
|
function get_page($page, $compile = TRUE) {
|
|
// Get the latest HASH for that page.
|
|
$hash = get_git_hash($page);
|
|
|
|
if (!$hash) {
|
|
return "Page '$page' not found.<br />\n";
|
|
}
|
|
|
|
$cnt = get_cache_hash($hash);
|
|
|
|
if (!$cnt) {
|
|
if ($compile) {
|
|
// Compile it
|
|
$cnt = compile_page($hash, $page /* for ref only */);
|
|
|
|
put_cache_hash($hash, $cnt);
|
|
|
|
return $cnt;
|
|
}
|
|
else {
|
|
// Grab it as is.
|
|
$cnt = get_git_hash_content($hash);
|
|
|
|
put_cache_hash($hash, $cnt);
|
|
|
|
return $cnt;
|
|
}
|
|
}
|
|
|
|
return $cnt;
|
|
|
|
}
|
|
|
|
|
|
/**
|
|
* Show page
|
|
*
|
|
* @param string File name (including the ext.) in the $PREFIX dir.
|
|
*/
|
|
function show_page($page, $compile = TRUE) {
|
|
print get_page($page, $compile);
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
* 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) {
|
|
$fn = get_cache_hash_filename($hash);
|
|
|
|
if ($fn) {
|
|
return file_get_contents($fn);
|
|
}
|
|
return NULL;
|
|
}
|
|
|
|
|
|
/**
|
|
* Return the local filename (.cache) in the cache path if it exists
|
|
*
|
|
* @return string/null Filename of the locally cached file.
|
|
*/
|
|
function get_cache_hash_filename($hash) {
|
|
global $CACHE_PATH;
|
|
|
|
$fn = $CACHE_PATH.'/'.$hash.'.cache';
|
|
|
|
if (file_exists($fn)) {
|
|
return $fn;
|
|
}
|
|
|
|
return NULL;
|
|
}
|
|
|
|
/**
|
|
* Write content to cache (identified by $hash)
|
|
*/
|
|
function put_cache_hash($hash, $content) {
|
|
global $CACHE_PATH;
|
|
|
|
$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, $CACHE_PATH;
|
|
|
|
$output = '';
|
|
|
|
// Grab the asciidoc.conf file, and put it into cache.
|
|
// keep return the hash
|
|
$fnconf = bring_local_file('asciidoc.conf');
|
|
|
|
// -d book, so we can render H1s
|
|
// -a icons, enables the display of graphic icons in admonition blocks.
|
|
$p = popen("GIT_DIR=".$GIT_REPOS." git-show $hash | asciidoc -a icons -f \"".$fnconf."\" -d book --no-header-footer - 2>&1", 'r');
|
|
|
|
if (!$p) {
|
|
return "Unable to compile file: $page ($hash)\n";
|
|
}
|
|
|
|
while (!feof($p)) {
|
|
$output .= fread($p, 1024);
|
|
}
|
|
pclose($p);
|
|
|
|
return $output;
|
|
}
|
|
|
|
|
|
/**
|
|
* Bring a file in the .git repository into a local .cache file
|
|
* and return the filename (as $CACHE_PATH/hash.cache)
|
|
*
|
|
* This function will prepend the PREFIX to the given git filename ($gitfile)
|
|
*
|
|
* @param string Gitfile is a filename in the repository (without the PREFIX)
|
|
* @return string Filename of the local copy of that file (cache/lsakdjflakdj.cache)
|
|
*/
|
|
function bring_local_file($gitfile) {
|
|
|
|
$hash = get_git_hash($gitfile);
|
|
|
|
$fn = get_cache_hash_filename($hash);
|
|
|
|
if (!$fn) {
|
|
$cnt = get_git_hash_content($hash);
|
|
|
|
put_cache_hash($hash, $cnt);
|
|
|
|
// Might return an error, but that's all we can do.
|
|
return get_cache_hash_filename($hash);
|
|
}
|
|
|
|
return $fn;
|
|
}
|
|
|
|
|
|
/**
|
|
* Retrieve file from git's object-ocean
|
|
*/
|
|
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
|
|
*/
|
|
function get_git_hash_content($hash) {
|
|
global $GIT_REPOS;
|
|
|
|
$output = '';
|
|
|
|
$p = popen("GIT_DIR=".$GIT_REPOS." git-show $hash", 'r');
|
|
|
|
if (!$p) {
|
|
return "Unable to run git-show for hash: $hash\n";
|
|
}
|
|
|
|
while (!feof($p)) {
|
|
$output .= fread($p, 1024);
|
|
}
|
|
pclose($p);
|
|
|
|
return $output;
|
|
}
|
|
|
|
/**
|
|
* Get the file's SHA-1 hash, for the latest revision on USE_BRANCH
|
|
*
|
|
* Used for comparison of cached/to cache/cache filename.
|
|
*
|
|
* @param string Filename without the $PREFIX (ex: Features.txt,
|
|
* images/pouet.png) and optionally, a ":" separator, followed by
|
|
* a git tree-ish (commit, branch, tag), ex: Build.txt:tags/0.7.2
|
|
* @return string SHA-1 hash
|
|
*/
|
|
function get_git_hash($file) {
|
|
global $USE_BRANCH, $GIT_REPOS;
|
|
|
|
$branch = $USE_BRANCH;
|
|
|
|
$split = explode(":", $file);
|
|
if (count($split) > 1) {
|
|
$branch = $split[1];
|
|
}
|
|
|
|
$output = array();
|
|
|
|
$cmd = "cd $GIT_REPOS; git-ls-tree $branch \"".git_filename($split[0])."\"";
|
|
|
|
$string = exec($cmd, $output);
|
|
|
|
if (count($output)) {
|
|
$fields = explode(' ', $output[0]);
|
|
|
|
if ($fields[1] == 'blob') {
|
|
// Return the HASH
|
|
$subfields = explode("\t", $fields[2]);
|
|
return $subfields[0];
|
|
}
|
|
}
|
|
|
|
return NULL;
|
|
}
|
|
|
|
/**
|
|
* Get file name (parsed and clear for git-ls-tree)
|
|
*
|
|
* @param string Filename without the $PREFIX (ex: Features.txt, images/pouet.png)
|
|
* @return string Parsed file name, with $PREFIX prepended, and slashes cleaned up.
|
|
*/
|
|
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();
|
|
|
|
|
|
?>
|