#!/usr/bin/php-cgi -dcgi.force_redirect=0
<?php
# encoding: utf-8
# api: cgi
# type: output
# category: vcs
# title: changelog
# description: Outputs a NEWS-style timeline
# version: 0.2
# state: beta
# depends: php:sqlite
# config: -
#
# A variant of the hooks.th1 version, in PHP.
#



#-- database (== fossil repo)
function db($sql="", $params=[]) {
    static $db;
    if (empty($db)) {
        $db = new PDO("sqlite::memory:");
        $db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_WARNING);
        $db->query("ATTACH DATABASE '$_SERVER[FOSSIL_REPOSITORY]' AS 'repo'");
    }
    if ($params) {
        $stmt = $db->prepare($sql);
        $stmt->execute($params);
        return $stmt->fetchAll(PDO::FETCH_ASSOC);
    }
    else {
        return $db->query($sql);
    }
}


#-- output
header("Content-Type: text/plain");
$version="trunk";
print "$version (unreleased)\n";
$r = db("
     SELECT event.mtime, tag.tagname, MAX(tag.tagid), DATE(event.mtime) AS d,
            event.comment AS comment
     FROM event
      LEFT JOIN tagxref ON event.objid=tagxref.rid
      LEFT JOIN tag ON tagxref.tagid=tag.tagid
     WHERE type='ci'
     GROUP BY objid
     ORDER BY event.mtime DESC
     LIMIT 750
");
foreach ($r as $row) {
    if (preg_match('/^sym-.*?(\d+\.\d+.*)$/', $row["tagname"]. $uu)) {
        $version = substr($uu[1], 3, 100);
        print "\n$version ($r[d])\n";
     }
     $comment = trim(preg_replace("/\s+/", " ", $row["comment"]));
     print " * $comment\n";
}
print "\n\n";
