Controller_GraphData.class.php 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. <?php
  2. /**
  3. * Controller for returning raw graph data
  4. *
  5. * @author Christopher Han <xiphux@gmail.com>
  6. * @copyright Copyright (c) 2012 Christopher Han
  7. * @package GitPHP
  8. * @subpackage Controller
  9. */
  10. class GitPHP_Controller_GraphData extends GitPHP_ControllerBase
  11. {
  12. /**
  13. * Initialize controller
  14. */
  15. public function Initialize()
  16. {
  17. parent::Initialize();
  18. if (!$this->config->GetValue('graphs')) {
  19. throw new Exception('Graphing has been disabled');
  20. }
  21. $this->preserveWhitespace = true;
  22. $this->DisableLogging();
  23. }
  24. /**
  25. * Gets the template for this controller
  26. *
  27. * @return string template filename
  28. */
  29. protected function GetTemplate()
  30. {
  31. return 'graphdata.tpl';
  32. }
  33. /**
  34. * Gets the cache key for this controller
  35. *
  36. * @return string cache key
  37. */
  38. protected function GetCacheKey()
  39. {
  40. return isset($this->params['graphtype']) ? $this->params['graphtype'] : '';
  41. }
  42. /**
  43. * Gets the name of this controller's action
  44. *
  45. * @param boolean $local true if caller wants the localized action name
  46. * @return string action name
  47. */
  48. public function GetName($local = false)
  49. {
  50. return 'graphdata';
  51. }
  52. /**
  53. * Loads headers for this template
  54. */
  55. protected function LoadHeaders()
  56. {
  57. $this->headers[] = 'Content-Type: application/json';
  58. }
  59. /**
  60. * Loads data for this template
  61. */
  62. protected function LoadData()
  63. {
  64. $head = $this->GetProject()->GetHeadCommit();
  65. $data = null;
  66. if ($this->params['graphtype'] == 'commitactivity') {
  67. $data = array();
  68. $commits = explode("\n", $this->exe->Execute($this->GetProject()->GetPath(), 'rev-list', array('--format=format:"%H %ct"', $head->GetHash())));
  69. foreach ($commits as $commit) {
  70. if (preg_match('/^([0-9a-fA-F]{40}) ([0-9]+)$/', $commit, $regs)) {
  71. $data[] = array('CommitEpoch' => (int)$regs[2]);
  72. }
  73. }
  74. } else if ($this->params['graphtype'] == 'languagedist') {
  75. $data = array();
  76. include_once(GITPHP_GESHIDIR . "geshi.php");
  77. $geshi = new GeSHi("",'php');
  78. $files = explode("\n", $this->exe->Execute($this->GetProject()->GetPath(), 'ls-tree', array('-r', '--name-only', $head->GetTree()->GetHash())));
  79. foreach ($files as $file) {
  80. $filename = GitPHP_Util::BaseName($file);
  81. $lang = GitPHP_Util::GeshiFilenameToLanguage($filename);
  82. if (empty($lang)) {
  83. $lang = $geshi->get_language_name_from_extension(substr(strrchr($filename, '.'), 1));
  84. if (empty($lang)) {
  85. $lang = 'Other';
  86. }
  87. }
  88. if (!empty($lang) && ($lang !== 'Other')) {
  89. $fulllang = $geshi->get_language_fullname($lang);
  90. if (!empty($fulllang))
  91. $lang = $fulllang;
  92. }
  93. if (isset($data[$lang])) {
  94. $data[$lang]++;
  95. } else {
  96. $data[$lang] = 1;
  97. }
  98. }
  99. }
  100. $this->tpl->assign('data', json_encode($data));
  101. }
  102. }