aboutsummaryrefslogtreecommitdiffstats
path: root/rev.php
blob: 2b40a3cfb9e9e0d085b470f656c37fade6ab8cff (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
<?php
/**
 * Place this at the public root of your system and adapt $app_root
 * so it relates to the app branch root.
 *
 * Used for a continuous integration prototype, on various hosts.
 *
 * PHP version 5
 *
 * @license BSD-2-Clause
 * @author  Romain d'Alverny <rda at mageia.org>
*/

$app_root = __DIR__;

$vars = array(
    'app'     => $_SERVER['SERVER_NAME'] . ':' . $_SERVER['SERVER_PORT'],
    'svn'     => get_svn_info($app_root),
    'status'  => get_status($app_root),
);

header('Content-Type: application/json; charset=utf-8');
echo json_encode($vars);

//---

/**
 * TODO Return app status (tests, config, other?)
 *
 * @param string $app_root
 *
 * @return string
*/
function get_status($app_root)
{
    return 'OK';
}

/**
 * Return basic Subversion status info. See $keys array.
 *
 * @param string $app_root
 *
 * @return array
*/
function get_svn_info($app_root)
{
    exec(escapeshellcmd(sprintf('LC_ALL=C %s info %s', exec('which svn'), escapeshellarg($app_root))),
        $out, $ret);

    $vars = array();
    $keys = array(
        'URL',
        'Revision',
        'Last Changed Author',
        'Last Changed Rev',
        'Last Changed Date'
    );
    foreach ($out as $l) {
        $l = explode(':', trim($l));
        $k = trim(array_shift($l));
        if (in_array($k, $keys))
            $vars[strtolower(str_replace(' ', '_', $k))] = trim(implode(':', $l));
    }

    // remove scheme & user; keep it?
    $u = $vars['url'];
    $u = parse_url($u);
    $vars['url'] = sprintf('%s%s', $u['host'], $u['path']);

    return $vars;
}