123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122 |
- <?php
- /**
- * Controller for returning raw graph data
- *
- * @author Christopher Han <xiphux@gmail.com>
- * @copyright Copyright (c) 2012 Christopher Han
- * @package GitPHP
- * @subpackage Controller
- */
- class GitPHP_Controller_GraphData extends GitPHP_ControllerBase
- {
- /**
- * Initialize controller
- */
- public function Initialize()
- {
- parent::Initialize();
- if (!$this->config->GetValue('graphs')) {
- throw new Exception('Graphing has been disabled');
- }
- $this->preserveWhitespace = true;
- $this->DisableLogging();
- }
- /**
- * Gets the template for this controller
- *
- * @return string template filename
- */
- protected function GetTemplate()
- {
- return 'graphdata.tpl';
- }
- /**
- * Gets the cache key for this controller
- *
- * @return string cache key
- */
- protected function GetCacheKey()
- {
- return isset($this->params['graphtype']) ? $this->params['graphtype'] : '';
- }
- /**
- * Gets the name of this controller's action
- *
- * @param boolean $local true if caller wants the localized action name
- * @return string action name
- */
- public function GetName($local = false)
- {
- return 'graphdata';
- }
- /**
- * Loads headers for this template
- */
- protected function LoadHeaders()
- {
- $this->headers[] = 'Content-Type: application/json';
- }
- /**
- * Loads data for this template
- */
- protected function LoadData()
- {
- $head = $this->GetProject()->GetHeadCommit();
- $data = null;
- if ($this->params['graphtype'] == 'commitactivity') {
- $data = array();
- $commits = explode("\n", $this->exe->Execute($this->GetProject()->GetPath(), 'rev-list', array('--format=format:"%H %ct"', $head->GetHash())));
- foreach ($commits as $commit) {
- if (preg_match('/^([0-9a-fA-F]{40}) ([0-9]+)$/', $commit, $regs)) {
- $data[] = array('CommitEpoch' => (int)$regs[2]);
- }
- }
- } else if ($this->params['graphtype'] == 'languagedist') {
- $data = array();
- include_once(GITPHP_GESHIDIR . "geshi.php");
- $geshi = new GeSHi("",'php');
- $files = explode("\n", $this->exe->Execute($this->GetProject()->GetPath(), 'ls-tree', array('-r', '--name-only', $head->GetTree()->GetHash())));
- foreach ($files as $file) {
- $filename = GitPHP_Util::BaseName($file);
- $lang = GitPHP_Util::GeshiFilenameToLanguage($filename);
- if (empty($lang)) {
- $lang = $geshi->get_language_name_from_extension(substr(strrchr($filename, '.'), 1));
- if (empty($lang)) {
- $lang = 'Other';
- }
- }
- if (!empty($lang) && ($lang !== 'Other')) {
- $fulllang = $geshi->get_language_fullname($lang);
- if (!empty($fulllang))
- $lang = $fulllang;
- }
- if (isset($data[$lang])) {
- $data[$lang]++;
- } else {
- $data[$lang] = 1;
- }
- }
- }
- $this->tpl->assign('data', json_encode($data));
- }
- }
|