Add specific exception for disabled search
Add specific exception for disabled search

<?php <?php
/** /**
* Controller for displaying a message page * Controller for displaying a message page
* *
* @author Christopher Han <xiphux@gmail.com> * @author Christopher Han <xiphux@gmail.com>
* @copyright Copyright (c) 2010 Christopher Han * @copyright Copyright (c) 2010 Christopher Han
* @package GitPHP * @package GitPHP
* @subpackage Controller * @subpackage Controller
*/ */
class GitPHP_Controller_Message extends GitPHP_ControllerBase class GitPHP_Controller_Message extends GitPHP_ControllerBase
{ {
/** /**
* Constructor * Constructor
*/ */
public function __construct() public function __construct()
{ {
$this->config = GitPHP_Config::GetInstance(); $this->config = GitPHP_Config::GetInstance();
   
if (GitPHP_Resource::Instantiated() && (GitPHP_Resource::GetLocale() != 'en_US')); if (GitPHP_Resource::Instantiated() && (GitPHP_Resource::GetLocale() != 'en_US'));
$this->resource = GitPHP_Resource::GetInstance(); $this->resource = GitPHP_Resource::GetInstance();
   
$this->InitializeGitExe(false); $this->InitializeGitExe(false);
   
try { try {
$this->InitializeProjectList(); $this->InitializeProjectList();
} catch (Exception $e) { } catch (Exception $e) {
} }
   
try { try {
$this->InitializeSmarty(); $this->InitializeSmarty();
} catch (Exception $e) { } catch (Exception $e) {
} }
   
if (isset($_GET['p']) && $this->projectList) { if (isset($_GET['p']) && $this->projectList) {
$project = $this->projectList->GetProject(str_replace(chr(0), '', $_GET['p'])); $project = $this->projectList->GetProject(str_replace(chr(0), '', $_GET['p']));
if ($project) { if ($project) {
$this->project = $project->GetProject(); $this->project = $project->GetProject();
} }
} }
   
if (isset($_GET['s'])) if (isset($_GET['s']))
$this->params['search'] = $_GET['s']; $this->params['search'] = $_GET['s'];
if (isset($_GET['st'])) if (isset($_GET['st']))
$this->params['searchtype'] = $_GET['st']; $this->params['searchtype'] = $_GET['st'];
   
$this->ReadQuery(); $this->ReadQuery();
} }
   
/** /**
* Gets the template for this controller * Gets the template for this controller
* *
* @return string template filename * @return string template filename
*/ */
protected function GetTemplate() protected function GetTemplate()
{ {
if ($this->project) if ($this->project)
return 'projectmessage.tpl'; return 'projectmessage.tpl';
return 'message.tpl'; return 'message.tpl';
} }
   
/** /**
* Gets the cache key for this controller * Gets the cache key for this controller
* *
* @return string cache key * @return string cache key
*/ */
protected function GetCacheKey() protected function GetCacheKey()
{ {
return sha1(serialize($this->params['exception'])); return sha1(serialize($this->params['exception']));
} }
   
/** /**
* Gets the name of this controller's action * Gets the name of this controller's action
* *
* @param boolean $local true if caller wants the localized action name * @param boolean $local true if caller wants the localized action name
* @return string action name * @return string action name
*/ */
public function GetName($local = false) public function GetName($local = false)
{ {
// This isn't a real controller // This isn't a real controller
return ''; return '';
} }
   
/** /**
* Read query into parameters * Read query into parameters
*/ */
protected function ReadQuery() protected function ReadQuery()
{ {
if (isset($_GET['h'])) if (isset($_GET['h']))
$this->params['hash'] = $_GET['h']; $this->params['hash'] = $_GET['h'];
else else
$this->params['hash'] = 'HEAD'; $this->params['hash'] = 'HEAD';
} }
   
/** /**
* Loads headers for this template * Loads headers for this template
*/ */
protected function LoadHeaders() protected function LoadHeaders()
{ {
if (($this->params['exception'] instanceof GitPHP_MessageException) && ($this->params['exception']->StatusCode)) { if (($this->params['exception'] instanceof GitPHP_MessageException) && ($this->params['exception']->StatusCode)) {
$partialHeader = $this->StatusCodeHeader($this->params['exception']->StatusCode); $partialHeader = $this->StatusCodeHeader($this->params['exception']->StatusCode);
if (!empty($partialHeader)) { if (!empty($partialHeader)) {
if (substr(php_sapi_name(), 0, 8) == 'cgi-fcgi') { if (substr(php_sapi_name(), 0, 8) == 'cgi-fcgi') {
/* /*
* FastCGI requires a different header * FastCGI requires a different header
*/ */
$this->headers[] = 'Status: ' . $partialHeader; $this->headers[] = 'Status: ' . $partialHeader;
} else { } else {
$this->headers[] = 'HTTP/1.1 ' . $partialHeader; $this->headers[] = 'HTTP/1.1 ' . $partialHeader;
} }
} }
} }
} }
   
/** /**
* Loads data for this template * Loads data for this template
*/ */
protected function LoadData() protected function LoadData()
{ {
$this->tpl->assign('message', $this->ExceptionToMessage($this->params['exception'])); $this->tpl->assign('message', $this->ExceptionToMessage($this->params['exception']));
if (($this->params['exception'] instanceof GitPHP_MessageException) && ($this->params['exception']->Error)) { if (($this->params['exception'] instanceof GitPHP_MessageException) && ($this->params['exception']->Error)) {
$this->tpl->assign('error', true); $this->tpl->assign('error', true);
} }
if ($this->project) { if ($this->project) {
try { try {
$co = $this->GetProject()->GetCommit($this->params['hash']); $co = $this->GetProject()->GetCommit($this->params['hash']);
if ($co) { if ($co) {
$this->tpl->assign('commit', $co); $this->tpl->assign('commit', $co);
} }
} catch (Exception $e) { } catch (Exception $e) {
} }
} }
} }
   
/** /**
* Gets the user-displayed message for an exception * Gets the user-displayed message for an exception
* *
* @param Exception $exception exception * @param Exception $exception exception
* @return string message * @return string message
*/ */
private function ExceptionToMessage($exception) private function ExceptionToMessage($exception)
{ {
if (!$exception) if (!$exception)
return; return;
   
if ($exception instanceof GitPHP_InvalidProjectParameterException) { if ($exception instanceof GitPHP_InvalidProjectParameterException) {
if ($this->resource) if ($this->resource)
return sprintf($this->resource->translate('Invalid project %1$s'), $exception->Project); return sprintf($this->resource->translate('Invalid project %1$s'), $exception->Project);
return sprintf('Invalid project %1$s', $exception->Project); return sprintf('Invalid project %1$s', $exception->Project);
} }
if ($exception instanceof GitPHP_MissingProjectParameterException) { if ($exception instanceof GitPHP_MissingProjectParameterException) {
if ($this->resource) if ($this->resource)
return $this->resource->translate('Project is required'); return $this->resource->translate('Project is required');
return 'Project is required'; return 'Project is required';
} }
   
  if ($exception instanceof GitPHP_SearchDisabledException) {
  if ($exception->FileSearch) {
  if ($this->resource)
  return $this->resource->translate('File search has been disabled');
  return 'File search has been disabled';
  } else {
  if ($this->resource)
  return $this->resource->translate('Search has been disabled');
  return 'Search has been disabled';
  }
  }
   
return $exception->getMessage(); return $exception->getMessage();
} }
   
/** /**
* Gets the header for an HTTP status code * Gets the header for an HTTP status code
* *
* @param integer $code status code * @param integer $code status code
* @return string header * @return string header
*/ */
private function StatusCodeHeader($code) private function StatusCodeHeader($code)
{ {
switch ($code) { switch ($code) {
case 500: case 500:
return '500 Internal Server Error'; return '500 Internal Server Error';
} }
} }
   
} }
   
<?php <?php
/** /**
* Controller for running a search * Controller for running a search
* *
* @author Christopher Han <xiphux@gmail.com> * @author Christopher Han <xiphux@gmail.com>
* @copyright Copyright (c) 2010 Christopher Han * @copyright Copyright (c) 2010 Christopher Han
* @package GitPHP * @package GitPHP
* @subpackage Controller * @subpackage Controller
*/ */
class GitPHP_Controller_Search extends GitPHP_ControllerBase class GitPHP_Controller_Search extends GitPHP_ControllerBase
{ {
   
/** /**
* Commit search type * Commit search type
* *
* @const * @const
*/ */
const CommitSearch = 'commit'; const CommitSearch = 'commit';
   
/** /**
* Author search type * Author search type
* *
* @const * @const
*/ */
const AuthorSearch = 'author'; const AuthorSearch = 'author';
   
/** /**
* Committer search type * Committer search type
* *
* @const * @const
*/ */
const CommitterSearch = 'committer'; const CommitterSearch = 'committer';
   
/** /**
* File search type * File search type
* *
* @const * @const
*/ */
const FileSearch = 'file'; const FileSearch = 'file';
   
/** /**
* Constructor * Constructor
*/ */
public function __construct() public function __construct()
{ {
parent::__construct(); parent::__construct();
   
if (!$this->config->GetValue('search')) { if (!$this->config->GetValue('search')) {
throw new GitPHP_MessageException(__('Search has been disabled'), true); throw new GitPHP_SearchDisabledException();
} }
} }
   
/** /**
* Gets the template for this controller * Gets the template for this controller
* *
* @return string template filename * @return string template filename
*/ */
protected function GetTemplate() protected function GetTemplate()
{ {
if ($this->params['searchtype'] == GitPHP_Controller_Search::FileSearch) { if ($this->params['searchtype'] == GitPHP_Controller_Search::FileSearch) {
return 'searchfiles.tpl'; return 'searchfiles.tpl';
} }
return 'search.tpl'; return 'search.tpl';
} }
   
/** /**
* Gets the cache key for this controller * Gets the cache key for this controller
* *
* @return string cache key * @return string cache key
*/ */
protected function GetCacheKey() 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); 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 * Gets the name of this controller's action
* *
* @param boolean $local true if caller wants the localized action name * @param boolean $local true if caller wants the localized action name
* @return string action name * @return string action name
*/ */
public function GetName($local = false) public function GetName($local = false)
{ {
if ($local && $this->resource) { if ($local && $this->resource) {
return $this->resource->translate('search'); return $this->resource->translate('search');
} }
return 'search'; return 'search';
} }
   
/** /**
* Read query into parameters * Read query into parameters
*/ */
protected function ReadQuery() protected function ReadQuery()
{ {
if (!isset($this->params['searchtype'])) if (!isset($this->params['searchtype']))
$this->params['searchtype'] = GitPHP_Controller_Search::CommitSearch; $this->params['searchtype'] = GitPHP_Controller_Search::CommitSearch;
   
if ($this->params['searchtype'] == GitPHP_Controller_Search::FileSearch) { if ($this->params['searchtype'] == GitPHP_Controller_Search::FileSearch) {
if (!$this->config->GetValue('filesearch')) { if (!$this->config->GetValue('filesearch')) {
throw new GitPHP_MessageException(__('File search has been disabled'), true); 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)) { 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_MessageException(__('Invalid search type'), true); throw new GitPHP_MessageException(__('Invalid search type'), true);
} }
   
if ((!isset($this->params['search'])) || (strlen($this->params['search']) < 2)) { if ((!isset($this->params['search'])) || (strlen($this->params['search']) < 2)) {
throw new GitPHP_MessageException(sprintf(__n('You must enter search text of at least %1$d character', 'You must enter search text of at least %1$d characters', 2), 2), true); throw new GitPHP_MessageException(sprintf(__n('You must enter search text of at least %1$d character', 'You must enter search text of at least %1$d characters', 2), 2), true);
} }
   
if (isset($_GET['h'])) if (isset($_GET['h']))
$this->params['hash'] = $_GET['h']; $this->params['hash'] = $_GET['h'];
else else
$this->params['hash'] = 'HEAD'; $this->params['hash'] = 'HEAD';
if (isset($_GET['pg'])) if (isset($_GET['pg']))
$this->params['page'] = $_GET['pg']; $this->params['page'] = $_GET['pg'];
else else
$this->params['page'] = 0; $this->params['page'] = 0;
} }
   
/** /**
* Loads data for this template * Loads data for this template
*/ */
protected function LoadData() protected function LoadData()
{ {
$co = $this->GetProject()->GetCommit($this->params['hash']); $co = $this->GetProject()->GetCommit($this->params['hash']);
   
if (!$co) { if (!$co) {
return; return;
} }
   
$this->tpl->assign('commit', $co); $this->tpl->assign('commit', $co);
   
$skip = $this->params['page'] * 100; $skip = $this->params['page'] * 100;
   
$results = null; $results = null;
   
switch ($this->params['searchtype']) { switch ($this->params['searchtype']) {
   
case GitPHP_Controller_Search::AuthorSearch: case GitPHP_Controller_Search::AuthorSearch:
$results = new GitPHP_CommitSearch($this->GetProject(), GitPHP_CommitSearch::AuthorType, $this->params['search'], $this->exe, $co, 101, $skip); $results = new GitPHP_CommitSearch($this->GetProject(), GitPHP_CommitSearch::AuthorType, $this->params['search'], $this->exe, $co, 101, $skip);
break; break;
   
case GitPHP_Controller_Search::CommitterSearch: case GitPHP_Controller_Search::CommitterSearch:
$results = new GitPHP_CommitSearch($this->GetProject(), GitPHP_CommitSearch::CommitterType, $this->params['search'], $this->exe, $co, 101, $skip); $results = new GitPHP_CommitSearch($this->GetProject(), GitPHP_CommitSearch::CommitterType, $this->params['search'], $this->exe, $co, 101, $skip);
break; break;
   
case GitPHP_Controller_Search::CommitSearch: case GitPHP_Controller_Search::CommitSearch:
$results = new GitPHP_CommitSearch($this->GetProject(), GitPHP_CommitSearch::CommitType, $this->params['search'], $this->exe, $co, 101, $skip); $results = new GitPHP_CommitSearch($this->GetProject(), GitPHP_CommitSearch::CommitType, $this->params['search'], $this->exe, $co, 101, $skip);
break; break;
   
case GitPHP_Controller_Search::FileSearch: case GitPHP_Controller_Search::FileSearch:
$results = new GitPHP_FileSearch($this->GetProject(), $co->GetTree(), $this->params['search'], $this->exe, 101, $skip); $results = new GitPHP_FileSearch($this->GetProject(), $co->GetTree(), $this->params['search'], $this->exe, 101, $skip);
break; break;
} }
   
if ($results->GetCount() > 0) { if ($results->GetCount() > 0) {
$this->tpl->assign('results', $results); $this->tpl->assign('results', $results);
} }
   
if ($results->GetCount() > 100) { if ($results->GetCount() > 100) {
$this->tpl->assign('hasmore', true); $this->tpl->assign('hasmore', true);
$results->SetLimit(100); $results->SetLimit(100);
} }
   
$this->tpl->assign('tree', $co->GetTree()); $this->tpl->assign('tree', $co->GetTree());
   
$this->tpl->assign('page', $this->params['page']); $this->tpl->assign('page', $this->params['page']);
   
} }
} }
   
  <?php
  /**
  * Custom exception when trying to perform a disabled search
  *
  * @author Christopher Han <xiphux@gmail.com>
  * @copyright Copyright (c) 2012 Christopher Han
  * @package GitPHP
  * @subpackage Exception
  */
  class GitPHP_SearchDisabledException extends GitPHP_MessageException
  {
  /**
  * Whether this exception is for file searching
  *
  * @var boolean
  */
  public $FileSearch;
 
  /**
  * Constructor
  *
  * @param boolean $fileSearch true if filesearch
  * @param string $message message
  * @param int $code code
  */
  public function __construct($fileSearch = false, $message = '', $code = 0)
  {
  $this->FileSearch = $fileSearch;
  if (empty($message)) {
  if ($fileSearch)
  $message = 'File search has been disabled';
  else
  $message = 'Search has been disabled';
  }
  parent::__construct($message, true, 200, $code);
  }
  }
 
comments