Controller_Search.class.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. <?php
  2. /**
  3. * Controller for running a search
  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_Search extends GitPHP_ControllerBase
  11. {
  12. /**
  13. * Commit search type
  14. *
  15. * @var string
  16. */
  17. const CommitSearch = 'commit';
  18. /**
  19. * Author search type
  20. *
  21. * @var string
  22. */
  23. const AuthorSearch = 'author';
  24. /**
  25. * Committer search type
  26. *
  27. * @var string
  28. */
  29. const CommitterSearch = 'committer';
  30. /**
  31. * File search type
  32. *
  33. * @var string
  34. */
  35. const FileSearch = 'file';
  36. /**
  37. * Initialize controller
  38. */
  39. public function Initialize()
  40. {
  41. parent::Initialize();
  42. if (!$this->config->GetValue('search')) {
  43. throw new GitPHP_SearchDisabledException();
  44. }
  45. if (empty($this->params['hash']))
  46. $this->params['hash'] = 'HEAD';
  47. if (empty($this->params['page']))
  48. $this->params['page'] = 0;
  49. if (!isset($this->params['searchtype']))
  50. $this->params['searchtype'] = GitPHP_Controller_Search::CommitSearch;
  51. if ($this->params['searchtype'] == GitPHP_Controller_Search::FileSearch) {
  52. if (!$this->config->GetValue('filesearch')) {
  53. throw new GitPHP_SearchDisabledException(true);
  54. }
  55. }
  56. if (($this->params['searchtype'] !== GitPHP_Controller_Search::AuthorSearch) && ($this->params['searchtype'] !== GitPHP_Controller_Search::CommitterSearch) && ($this->params['searchtype'] !== GitPHP_Controller_Search::CommitSearch) && ($this->params['searchtype'] !== GitPHP_Controller_Search::FileSearch)) {
  57. throw new GitPHP_InvalidSearchTypeException();
  58. }
  59. if ((!isset($this->params['search'])) || (strlen($this->params['search']) < 2)) {
  60. throw new GitPHP_SearchLengthException(2);
  61. }
  62. }
  63. /**
  64. * Gets the template for this controller
  65. *
  66. * @return string template filename
  67. */
  68. protected function GetTemplate()
  69. {
  70. if ($this->params['searchtype'] == GitPHP_Controller_Search::FileSearch) {
  71. return 'searchfiles.tpl';
  72. }
  73. return 'search.tpl';
  74. }
  75. /**
  76. * Gets the cache key for this controller
  77. *
  78. * @return string cache key
  79. */
  80. protected function GetCacheKey()
  81. {
  82. return (isset($this->params['hash']) ? $this->params['hash'] : '') . '|' . (isset($this->params['searchtype']) ? sha1($this->params['searchtype']) : '') . '|' . (isset($this->params['search']) ? sha1($this->params['search']) : '') . '|' . (isset($this->params['page']) ? $this->params['page'] : 0);
  83. }
  84. /**
  85. * Gets the name of this controller's action
  86. *
  87. * @param boolean $local true if caller wants the localized action name
  88. * @return string action name
  89. */
  90. public function GetName($local = false)
  91. {
  92. if ($local && $this->resource) {
  93. return $this->resource->translate('search');
  94. }
  95. return 'search';
  96. }
  97. /**
  98. * Loads data for this template
  99. */
  100. protected function LoadData()
  101. {
  102. $co = $this->GetProject()->GetCommit($this->params['hash']);
  103. if (!$co) {
  104. return;
  105. }
  106. $this->tpl->assign('commit', $co);
  107. $skip = $this->params['page'] * 100;
  108. $results = null;
  109. switch ($this->params['searchtype']) {
  110. case GitPHP_Controller_Search::AuthorSearch:
  111. $results = new GitPHP_CommitSearch($this->GetProject(), GitPHP_CommitSearch::AuthorType, $this->params['search'], $this->exe, $co, 101, $skip);
  112. break;
  113. case GitPHP_Controller_Search::CommitterSearch:
  114. $results = new GitPHP_CommitSearch($this->GetProject(), GitPHP_CommitSearch::CommitterType, $this->params['search'], $this->exe, $co, 101, $skip);
  115. break;
  116. case GitPHP_Controller_Search::CommitSearch:
  117. $results = new GitPHP_CommitSearch($this->GetProject(), GitPHP_CommitSearch::CommitType, $this->params['search'], $this->exe, $co, 101, $skip);
  118. break;
  119. case GitPHP_Controller_Search::FileSearch:
  120. $results = new GitPHP_FileSearch($this->GetProject(), $co->GetTree(), $this->params['search'], $this->exe, 101, $skip);
  121. break;
  122. }
  123. if ($results->GetCount() > 0) {
  124. $this->tpl->assign('results', $results);
  125. }
  126. if ($results->GetCount() > 100) {
  127. $this->tpl->assign('hasmore', true);
  128. $results->SetLimit(100);
  129. }
  130. $this->tpl->assign('tree', $co->GetTree());
  131. $this->tpl->assign('page', $this->params['page']);
  132. }
  133. }