123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164 |
- <?php
- /**
- * Controller for running a search
- *
- * @author Christopher Han <xiphux@gmail.com>
- * @copyright Copyright (c) 2010 Christopher Han
- * @package GitPHP
- * @subpackage Controller
- */
- class GitPHP_Controller_Search extends GitPHP_ControllerBase
- {
- /**
- * Commit search type
- *
- * @var string
- */
- const CommitSearch = 'commit';
- /**
- * Author search type
- *
- * @var string
- */
- const AuthorSearch = 'author';
- /**
- * Committer search type
- *
- * @var string
- */
- const CommitterSearch = 'committer';
- /**
- * File search type
- *
- * @var string
- */
- const FileSearch = 'file';
- /**
- * Initialize controller
- */
- public function Initialize()
- {
- parent::Initialize();
- if (!$this->config->GetValue('search')) {
- throw new GitPHP_SearchDisabledException();
- }
- if (empty($this->params['hash']))
- $this->params['hash'] = 'HEAD';
- if (empty($this->params['page']))
- $this->params['page'] = 0;
- if (!isset($this->params['searchtype']))
- $this->params['searchtype'] = GitPHP_Controller_Search::CommitSearch;
- if ($this->params['searchtype'] == GitPHP_Controller_Search::FileSearch) {
- if (!$this->config->GetValue('filesearch')) {
- throw new GitPHP_SearchDisabledException(true);
- }
- }
- 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)) {
- throw new GitPHP_InvalidSearchTypeException();
- }
- if ((!isset($this->params['search'])) || (strlen($this->params['search']) < 2)) {
- throw new GitPHP_SearchLengthException(2);
- }
- }
- /**
- * Gets the template for this controller
- *
- * @return string template filename
- */
- protected function GetTemplate()
- {
- if ($this->params['searchtype'] == GitPHP_Controller_Search::FileSearch) {
- return 'searchfiles.tpl';
- }
- return 'search.tpl';
- }
- /**
- * Gets the cache key for this controller
- *
- * @return string cache key
- */
- protected function GetCacheKey()
- {
- 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);
- }
- /**
- * 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)
- {
- if ($local && $this->resource) {
- return $this->resource->translate('search');
- }
- return 'search';
- }
- /**
- * Loads data for this template
- */
- protected function LoadData()
- {
- $co = $this->GetProject()->GetCommit($this->params['hash']);
- if (!$co) {
- return;
- }
- $this->tpl->assign('commit', $co);
- $skip = $this->params['page'] * 100;
- $results = null;
- switch ($this->params['searchtype']) {
- case GitPHP_Controller_Search::AuthorSearch:
- $results = new GitPHP_CommitSearch($this->GetProject(), GitPHP_CommitSearch::AuthorType, $this->params['search'], $this->exe, $co, 101, $skip);
- break;
- case GitPHP_Controller_Search::CommitterSearch:
- $results = new GitPHP_CommitSearch($this->GetProject(), GitPHP_CommitSearch::CommitterType, $this->params['search'], $this->exe, $co, 101, $skip);
- break;
- case GitPHP_Controller_Search::CommitSearch:
- $results = new GitPHP_CommitSearch($this->GetProject(), GitPHP_CommitSearch::CommitType, $this->params['search'], $this->exe, $co, 101, $skip);
- break;
- case GitPHP_Controller_Search::FileSearch:
- $results = new GitPHP_FileSearch($this->GetProject(), $co->GetTree(), $this->params['search'], $this->exe, 101, $skip);
- break;
- }
- if ($results->GetCount() > 0) {
- $this->tpl->assign('results', $results);
- }
- if ($results->GetCount() > 100) {
- $this->tpl->assign('hasmore', true);
- $results->SetLimit(100);
- }
- $this->tpl->assign('tree', $co->GetTree());
- $this->tpl->assign('page', $this->params['page']);
- }
-
- }
|