1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 | <?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']); } } |