<?php
/**
 * DokuGource Plugin: extract gource log from the wiki
 *
 * @license    GPL 2 (http://www.gnu.org/licenses/gpl.html)
 * @author     Etienne Meleard <etienne.meleard@free.fr>
 * 
 * 2010/05/19 : Creation
 */

// must be run within Dokuwiki
if(!defined('DOKU_INC')) die();

if(!defined('DOKU_PLUGIN')) define('DOKU_PLUGIN',DOKU_INC.'lib/plugins/');
require_once(DOKU_PLUGIN.'action.php');

class action_plugin_dokugource extends DokuWiki_Action_Plugin {
	var $colors = array();
	var $log = array();
	
	// return some info
	function getInfo() {
		return confToHash(dirname(__FILE__).'/INFO');
	}
	
	/**
	 * Register plugin hooks
	 */
	function register(&$controller) {
		$controller->register_hook('IO_WIKIPAGE_WRITE', 'AFTER', $this, 'gourcecolor');
		$controller->register_hook('ACTION_ACT_PREPROCESS', 'BEFORE', $this, 'evalgourcerequest');
	}
	
	/**
	 * Get gourcecolor
	 * @param &$event dokuwiki event object
	 * @param $args handler arguments
	 */
    function gourcecolor(&$event, $args) {
		global $conf;
		global $ID;
		if(preg_match('`~~gourcecolor(<|:|>)#?([0-9a-z]{3}([0-9a-z]{3})?)~~`i', $event->data[0][1], $m)) {
			$c = strtoupper($m[2]);
			if(strlen($c) == 3) {
				$c = str_split($c);
				$c = $c[0].$c[0].$c[1].$c[1].$c[2].$c[2];
			}
			$ns = $event->data[1].':'.(($m[1] == '<') ? '' : $event->data[2]);
			$ls = preg_split('`\n+`', trim(@file_get_contents($conf['metadir'].'/gource.colors')));
			if($fp = fopen($conf['metadir'].'/gource.colors', 'w')) {
				foreach($ls as $l) {
					if(!$l) continue;
					if(array_shift(preg_split('`\s+`', $l, 2)) != $ns) fwrite($fp, $l."\n");
				}
				fwrite($fp, $ns.' '.$c."\n");
				fclose($fp);
			}
		}
    }
    
    /**
     * Eval if a gource log is requiered
	 * @param &$event dokuwiki event object
	 * @param $args handler arguments
     */
    function evalgourcerequest(&$event, $args) {
		global $conf;
		global $ID;
		
		// get autorisation
		if(!isset($_GET['dokugource_key']) || empty($_GET['dokugource_key'])) return;
		if(!preg_match('`^[a-z0-9]{8,64}$`i', $_GET['dokugource_key'])) die();
		$key = $_GET['dokugource_key'];
		
		$ns = $ID;
		if($ns) {
			$ns = str_replace('/', ':', trim($ns, '/:'));
			if(!@is_dir($conf['datadir'].'/'.str_replace(':', '/', $ns).'/')) {
				if(@is_file($conf['datadir'].'/'.str_replace(':', '/', $ns).'.txt')) {
					$ns = preg_replace('`^(.*)\:[^\:]+$`', '$1', $ns);
				}
			}
		}
		$ns = trim($ns, '/:');
		
		$auto = preg_split('`\n+`', @file_get_contents(DOKU_CONF.'/gource.cnf.php'));
		array_shift($auto);
		$autons = false;
		foreach($auto as $l) {
			if(preg_match('`^'.$key.'(\s+(.*))?$`', $l, $m)) {
				$autons = trim($m[2], '/:');
			}
		}
		if($autons === false) die();
		if(substr($ns, 0, strlen($autons)) != $autons) die();
		
		$from = (isset($_GET['dokugource_from']) && !empty($_GET['dokugource_from'])) ? (int)$_GET['dokugource_from'] : 0;
		
		$this->colors = array();
		foreach(preg_split('`\n+`', trim(@file_get_contents($conf['metadir'].'/gource.colors'))) as $l) {
			if(!$l) continue;
			$l = preg_split('`\s+`', $l, 2);
			$this->colors[$l[0]] = $l[1];
		}
		
		$strip = explode(':', $ns);
		array_pop($strip);
		$strip = count($strip) ? strlen(implode(':', $strip)) : 0;
		$this->crawl($conf['metadir'].($ns ? '/'.str_replace(':', '/', $ns) : ''), $strip, $from);
		sort($this->log);
		
		header('Content-Type: text/plain; charset=utf-8');
		echo trim(implode("\n", $this->log)."\nDone\n");
		exit();
    }
    
	function color($p) {
		$ns = preg_replace('`^(.*\:)[^\:]+$`', '$1', $p);
		if(isset($this->colors[$p])) return $this->colors[$p];
		if(isset($this->colors[$ns])) return $this->colors[$ns];
		
		$this->colors[$p] = sprintf('%02X%02X%02X', rand(0, 255), rand(0, 255), rand(0, 255));
		return $this->colors[$p];
	}
	
	function crawl($p, $strip, $from) {
		if(!@is_dir($p)) return;
		foreach(scandir($p) as $i) {
			if($i == '.' || $i == '..') continue;
			if(is_file($p.'/'.$i)) if(preg_match('`\.changes$`', $i)) {
				foreach(preg_split('`\n+`', trim(@file_get_contents($p.'/'.$i))) as $l) {
					if(!$l) continue;
					if(preg_match('`^\s*([0-9]+)\s+([^\s]+)\s+([a-z]{1,3})\s+([^\s]+)\s+([^\s]+)`i', $l, $m)) {
						if((int)$m[1] < $from) continue;
						if(preg_match('`(C|cc|sc)`i', $m[3])) {
							$m[3] = 'A';
						}elseif(preg_match('`(D|dc|hc)`i', $m[3])) {
							$m[3] = 'D';
						}else{
							$m[3] = 'M';
						}
						$id = str_replace(':', '/', $m[4]);
						if($strip) $id = trim(substr($id, $strip), '/:');
						$this->log[] = $m[1].'|'.$m[5].'|'.$m[3].'|'.$id.'|'.$this->color($m[4]);
					}
				}
			}
			
			if(is_dir($p.'/'.$i)) $this->crawl($p.'/'.$i, $strip, $from);
		}
	}
}
