Controller_Blame.class.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. <?php
  2. /**
  3. * Controller for displaying blame
  4. *
  5. * @author Christopher Han <xiphux@gmail.com>
  6. * @copyright Copyright (c) 2010 Christopher Han
  7. * @package GitPHP
  8. * @subpackage Controller
  9. */
  10. class GitPHP_Controller_Blame extends GitPHP_ControllerBase
  11. {
  12. /**
  13. * Initialize controller
  14. */
  15. public function Initialize()
  16. {
  17. parent::Initialize();
  18. if (empty($this->params['hashbase']))
  19. $this->params['hashbase'] = 'HEAD';
  20. if (!empty($this->params['output']) && ($this->params['output'] == 'js')) {
  21. $this->DisableLogging();
  22. }
  23. }
  24. /**
  25. * Gets the template for this controller
  26. *
  27. * @return string template filename
  28. */
  29. protected function GetTemplate()
  30. {
  31. if (isset($this->params['output']) && ($this->params['output'] == 'js')) {
  32. return 'blamedata.tpl';
  33. }
  34. return 'blame.tpl';
  35. }
  36. /**
  37. * Gets the cache key for this controller
  38. *
  39. * @return string cache key
  40. */
  41. protected function GetCacheKey()
  42. {
  43. return (isset($this->params['hashbase']) ? $this->params['hashbase'] : '') . '|' . (isset($this->params['hash']) ? $this->params['hash'] : '') . '|' . (isset($this->params['file']) ? sha1($this->params['file']) : '');
  44. }
  45. /**
  46. * Gets the name of this controller's action
  47. *
  48. * @param boolean $local true if caller wants the localized action name
  49. * @return string action name
  50. */
  51. public function GetName($local = false)
  52. {
  53. if ($local && $this->resource) {
  54. return $this->resource->translate('blame');
  55. }
  56. return 'blame';
  57. }
  58. /**
  59. * Loads data for this template
  60. */
  61. protected function LoadData()
  62. {
  63. $head = $this->GetProject()->GetHeadCommit();
  64. $this->tpl->assign('head', $head);
  65. $commit = $this->GetProject()->GetCommit($this->params['hashbase']);
  66. $this->tpl->assign('commit', $commit);
  67. if ((!isset($this->params['hash'])) && (isset($this->params['file']))) {
  68. $this->params['hash'] = $commit->GetTree()->PathToHash($this->params['file']);
  69. if (empty($this->params['hash']))
  70. throw new GitPHP_FileNotFoundException($this->params['file']);
  71. }
  72. $blob = $this->GetProject()->GetObjectManager()->GetBlob($this->params['hash']);
  73. if ($this->params['file'])
  74. $blob->SetPath($this->params['file']);
  75. $blob->SetCommit($commit);
  76. $this->tpl->assign('blob', $blob);
  77. $blame = new GitPHP_FileBlame($this->GetProject(), $commit, $this->params['file'], $this->exe);
  78. $this->tpl->assign('blame', $blame->GetBlame());
  79. if (isset($this->params['output']) && ($this->params['output'] == 'js')) {
  80. return;
  81. }
  82. $this->tpl->assign('tree', $commit->GetTree());
  83. if ($this->config->GetValue('geshi')) {
  84. include_once(GITPHP_GESHIDIR . "geshi.php");
  85. if (class_exists('GeSHi')) {
  86. $geshi = new GeSHi("",'php');
  87. if ($geshi) {
  88. $lang = GitPHP_Util::GeshiFilenameToLanguage($blob->GetName());
  89. if (empty($lang)) {
  90. $lang = $geshi->get_language_name_from_extension(substr(strrchr($blob->GetName(),'.'),1));
  91. }
  92. if (!empty($lang)) {
  93. $geshi->enable_classes();
  94. $geshi->enable_strict_mode(GESHI_MAYBE);
  95. $geshi->set_source($blob->GetData());
  96. $geshi->set_language($lang);
  97. $geshi->set_header_type(GESHI_HEADER_PRE_TABLE);
  98. $geshi->enable_line_numbers(GESHI_NORMAL_LINE_NUMBERS);
  99. $output = $geshi->parse_code();
  100. $bodystart = strpos($output, '<td');
  101. $bodyend = strrpos($output, '</tr>');
  102. if (($bodystart !== false) && ($bodyend !== false)) {
  103. $geshihead = substr($output, 0, $bodystart);
  104. $geshifoot = substr($output, $bodyend);
  105. $geshibody = substr($output, $bodystart, $bodyend-$bodystart);
  106. $this->tpl->assign('geshihead', $geshihead);
  107. $this->tpl->assign('geshibody', $geshibody);
  108. $this->tpl->assign('geshifoot', $geshifoot);
  109. $this->tpl->assign('geshicss', $geshi->get_stylesheet());
  110. $this->tpl->assign('geshi', true);
  111. }
  112. }
  113. }
  114. }
  115. }
  116. }
  117. }