summaryrefslogtreecommitdiff
path: root/timeline/tlcacher.php
blob: a71fd758036276c6fa77715755d6ce0706c82f69 (plain)
    1 #!/usr/bin/php
    2 <?php
    3 
    4 /* Caches the timeline events into a sqlite db */
    5 
    6 require_once('DB.php');
    7 
    8 /**************** Configuration ***********************/
    9 
   10 // PEAR dns for the flyspray db and sqlite cache db,
   11 // ie 
   12 // $dsn = 'mysql://username:password@host/dbname';
   13 // $dsnc = 'sqlite:////path/to/database.db';
   14 require_once("tlcacher_config.php");
   15 
   16 // Url of the detailed task
   17 $task_url="https://crux.nu/bugs/?do=details&id=%s";
   18 
   19 // Gitweb url for commits
   20 $git_url = "http://crux.nu/gitweb/?p=%s.git;a=commitdiff;h=%s";
   21 // append ":branch" to the repo name to restrict logs to 'branch'
   22 #$git_repos = array("ports/core:2.2","ports/opt:2.2","ports/xorg:2.2", "tools/pkgutils", "tools/prt-get", "tools/prt-utils");
   23 $git_repos = array("ports/core:2.2","ports/opt:2.2","tools/webtools");
   24 $git_root = "/home/crux/scm";
   25 
   26 // Map git authors to wiki profiles
   27 $git_username_map = array(
   28     "Per Lidén" => "PerLiden",
   29 	"Matt Housh" => "jaeger",
   30 	"Juergen Daubert" => "JuergenDaubert",
   31 	"Johannes Winkelmann" => "JohannesWinkelmann",
   32 	"Simone Rota" => "SimoneRota" ,
   33 	"Jason Thomas Dolan" => "JasonThomasDolan",
   34 	"Jukka Heino" => "JukkaHeino",
   35 	"Tilman Sauerbeck" => "TilmanSauerbeck",
   36 	"Simon Gloßner" => "SimonGloßner",
   37 	"Nick Steeves" => "NickSteeves",
   38 	"Antti Nykänen" => "AnttiNykänen",
   39 );
   40 
   41 // Path of the recent changes pmwiki file
   42 $wiki_file = "/home/crux/public_html/wiki.d/Site.AllRecentChanges";
   43 
   44 // Event: cache_id, tstamp, type(icon), date, time, user, url, description, notes
   45 $events = array();
   46 
   47 /**************** Last cached events *******************/
   48 
   49 $dbc =& DB::connect($dsnc);
   50 if (DB::isError($dbc)) die("Cannot connect to S database");
   51 $dbc->setFetchMode(DB_FETCHMODE_ASSOC);
   52 $last_cached_sql = "select cache_id from events where event_type like 'task%' order by cache_id desc";
   53 $res =& $dbc->limitQuery($last_cached_sql,0,1);
   54 if (DB::isError($res)) die("Query error");
   55 if ($res->numRows() === 0) {
   56 	$last_task = 0;
   57 } else {
   58 	$row =& $res->fetchRow();
   59 	$last_task = $row['cache_id'];
   60 }
   61 $last_cached_sql = "select cache_id from events where event_type = 'wiki_changed' order by cache_id desc";
   62 $res =& $dbc->limitQuery($last_cached_sql,0,1);
   63 if (DB::isError($res)) die("Query error");
   64 if ($res->numRows() === 0) {
   65 	$last_wiki = 0;
   66 } else {
   67 	$row =& $res->fetchRow();
   68 	$last_wiki = $row['cache_id'];
   69 }
   70 
   71 function last_cached_git($repo) {
   72     global $dbc;
   73     $last_cached_sql = "select cache_id from events where event_type='git_commit_$repo' order by event_tstamp desc";
   74     $res =& $dbc->limitQuery($last_cached_sql,0,1);
   75     if (DB::isError($res)) die("Query error");
   76     if ($res->numRows() === 0) {
   77     	$last_git = "";
   78     } else {
   79     	$row =& $res->fetchRow();
   80     	$last_git = $row['cache_id'];
   81     }
   82     return $last_git;
   83 }
   84 
   85 /**************** Flyspray events ***********************/
   86 
   87 $db =& DB::connect($dsn);
   88 if (DB::isError($db)) die("Cannot connect to M database");
   89 $db->setFetchMode(DB_FETCHMODE_ASSOC);
   90 
   91 $sql = "select history_id, event_date, event_type, user_name, flyspray_history.task_id, item_summary, closure_comment
   92 	from flyspray_history 
   93 	join flyspray_users on flyspray_users.user_id = flyspray_history.user_id
   94 	join flyspray_tasks on flyspray_tasks.task_id = flyspray_history.task_id
   95 	where history_id > $last_task";
   96 
   97 $res =& $db->Query($sql);
   98 if (DB::isError($res)) die("Query error");
   99 
  100 while ($row =& $res->fetchRow()) {
  101     // $etype = $fs_events[$row['event_type']];
  102 	$etype = $row['event_type'];
  103 	$euser = $row['user_name'];
  104 	$etid = $row['task_id'];
  105 	$edate = $row['event_date'];
  106 	$cache_id = $row['history_id'];
  107 	$description = "";
  108 	$date = date("Y-m-d", $edate);
  109 	$time = date("H:i", $edate);
  110 	$url = sprintf($task_url,$etid);
  111 	switch ($etype) {
  112 		case "1": // new task
  113 			$icon = "task_opened";
  114 			$description = "New task [[$url|$etid]] opened by $euser";
  115 			$notes = $row['item_summary'];
  116 			break;
  117 		case "2": // task closed
  118 			$icon = "task_closed";
  119 			$description = "Task [[$url|$etid]] closed by $euser";
  120 			if ($row['closure_comment'] != "" && $row['closure_comment'] != 0) { // weird flyspray!
  121 				$notes = $row['closure_comment'];
  122 			} else {
  123 				$notes = "";
  124 			}
  125 			break;
  126 		case "3": // task edited : fields, comments, attachments, ownership, related tasks, etc.
  127 		case "4":
  128 		case "5":
  129 		case "6":
  130 		case "7":
  131 		case "8":
  132 		case "14":
  133 		case "15":
  134 		case "16":
  135 		case "22":
  136 		case "23":
  137 		case "24":
  138 		case "25":
  139 			$icon = "task_changed";
  140 			$description = "Task [[$url|$etid]] modified by $euser";
  141 			$notes = "";
  142 			break;
  143 	}
  144 	if ($description !== "") {
  145 		$events[] = array( 'cache_id' => $cache_id, 'tstamp' => $edate, 'icon' => $icon, 'date' => $date, 
  146 			'time' => $time, 'user' => $euser, 'url'=> $url, 'description' => $description, 'notes' => $notes,);
  147 	}
  148 }
  149 
  150 /****************** PmWiki events *********************/
  151 $lines = file($wiki_file);
  152 $chline = "";
  153 foreach ($lines as $line) {
  154 	if (substr($line,0,5) == "text=") {
  155 		$chline = substr($line,7);
  156 	}
  157 }
  158 if ($chline != "") {
  159 	$wikiedits = split('\*', $chline);
  160 	$icon = "wiki_changed";
  161 	foreach ($wikiedits as $ed) {
  162 		preg_match('/\[\[.*\]\] ./', $ed, $matches);
  163 		$page = $matches[0];
  164 		preg_match('/by \[\[.*\]\]/', $ed, $matches);
  165 		$user = $matches[0];
  166 		preg_match("/\=(.*)\=/s",$ed,$matches);
  167 		$notes = $matches[1];
  168 		preg_match("/\. \. \. (.*?) by/s",$ed,$matches);
  169 		$date = $matches[1];
  170 		$date = str_replace(", at", "", $date); // old entry format
  171 		$tstamp = strtotime($date);
  172 		preg_match('/(..\:..)/',$date,$matches);
  173 		$time = $matches[0];
  174 		$date = date("Y-m-d", $tstamp);
  175 		$action = "?action=diff#" . urlencode($date . " " . $time) . "|diff";
  176 		$page_diff = trim(str_replace("]]", $action."]]", $page));
  177 		preg_match('/\[\[(.*)\|diff\]\]/', $page_diff, $matches);
  178 		$url = $matches[1];
  179 		$url = "http://crux.nu/".str_replace(".","/", $url);
  180 		$description = "Wiki page $page edited $user ($page_diff)";
  181 		if ($tstamp > $last_wiki) {
  182 			$events[] = array( 'cache_id' => $tstamp, 'tstamp' => $tstamp, 'icon' => $icon, 'date' => $date, 
  183 				'time' => $time, 'user' => $user, 'url' => $url, 'description' => $description, 'notes' => $notes);
  184 		}
  185 	}
  186 }
  187 
  188 /******************* Git events ***********************/
  189 
  190 foreach ($git_repos as $repo) {
  191     if (strpos($repo, ":") !== FALSE) {
  192 		$tmp = explode(':', $repo);
  193     	$repo = $tmp[0];
  194 		$branch = $tmp[1];
  195 	}
  196     $pos = strpos($repo, "/");
  197     $reponame = substr($repo, $pos+1);
  198     $branch = "";
  199     $last_git = last_cached_git($reponame);
  200     $out = array();
  201     $res = 0;
  202     $done = array();
  203     exec("GIT_DIR=$git_root/$repo.git git log -n 1 $branch", $out, $res);
  204     $git_latest = trim(str_replace("commit ", "", $out[0]));
  205     unset($out);
  206     $res = 0;
  207     if ($git_latest != $last_git) {
  208         if ($last_git == "") {
  209     	    exec("GIT_DIR=$git_root/$repo.git git log $branch", $out, $res);
  210         } else {
  211     	    exec("GIT_DIR=$git_root/$repo.git git log $last_git..$git_latest $branch", $out, $res);
  212         }
  213         $last_git = $git_latest;
  214 	    foreach ($out as $line) {
  215 		    if (substr($line, 0, 7) == "commit ") {
  216                 $rev = substr($line, 7);
  217                 $url = sprintf($git_url, $repo, $rev);
  218                 $compact_rev = substr($rev,0,4)."..".substr($rev,-4,4);
  219                 $revurl = "[[$url|$compact_rev]]";
  220             } else if (substr($line, 0, 8) == "Author: ") {
  221                 $user = substr($line, 8);
  222                 $pos = strpos($user, "<");
  223                 if ($pos !== FALSE)
  224                     $user = trim(substr($user,0,$pos));
  225                 if (array_key_exists($user, $git_username_map)) {
  226 					$wikiname = $git_username_map[$user];
  227 					$user = "[[~" . $wikiname . "|" . $user . "]]";
  228 				}
  229                 $description = "$revurl committed by $user";
  230             } else if (substr($line, 0, 8) == "Date:   ") {
  231                 $date = substr($line, 8);
  232                 $tstamp = strtotime($date);
  233                 $date = date("Y-m-d", $tstamp);
  234                 $time = date("H:i", $tstamp);
  235             } else if (trim($line) != "" && !array_key_exists($rev, $done)) {
  236     			$icon = "git_commit_$reponame";
  237 	    		$notes = trim($line);
  238 		    	$events[] = array( 'cache_id' => $rev, 'tstamp' => $tstamp, 'icon' => $icon, 'date' => $date, 
  239 			        'time' => $time, 'user' => $user, 'url'=>$url, 'description' => $description, 'notes' => $notes);
  240                 $done[$rev] = 1;
  241 		    }
  242 	    }
  243     }
  244 }
  245 
  246 /*************** Finally, all events *********************/
  247 $sth = $dbc->prepare("insert into events values (NULL, ?, ?, ?, ?, ?, ?, ?, ?, ?)");
  248 foreach ($events as $evt) {
  249 	$res = $dbc->execute($sth, $evt);
  250 	if (DB::isError($res)) die ("Query error");
  251 }
  252 
  253 ?>

Generated by cgit