Break out highlighting
Break out highlighting

<?php <?php
/* /*
* display.git_search.php * display.git_search.php
* gitphp: A PHP git repository browser * gitphp: A PHP git repository browser
* Component: Display - search * Component: Display - search
* *
* Copyright (C) 2009 Christopher Han <xiphux@gmail.com> * Copyright (C) 2009 Christopher Han <xiphux@gmail.com>
*/ */
   
require_once('defs.constants.php'); require_once('defs.constants.php');
  require_once('util.highlight.php');
require_once('gitutil.git_read_commit.php'); require_once('gitutil.git_read_commit.php');
require_once('gitutil.git_rev_list.php'); require_once('gitutil.git_rev_list.php');
   
function git_search($projectroot, $project, $hash, $search, $searchtype, $page = 0) function git_search($projectroot, $project, $hash, $search, $searchtype, $page = 0)
{ {
global $tpl,$gitphp_conf; global $tpl,$gitphp_conf;
   
if (!$gitphp_conf['search']) { if (!$gitphp_conf['search']) {
$tpl->clear_all_assign(); $tpl->clear_all_assign();
$tpl->assign("message","Search has been disabled"); $tpl->assign("message","Search has been disabled");
$tpl->display("message.tpl"); $tpl->display("message.tpl");
return; return;
} }
   
if (!isset($search) || (strlen($search) < 2)) { if (!isset($search) || (strlen($search) < 2)) {
$tpl->clear_all_assign(); $tpl->clear_all_assign();
$tpl->assign("error",TRUE); $tpl->assign("error",TRUE);
$tpl->assign("message","You must enter search text of at least 2 characters"); $tpl->assign("message","You must enter search text of at least 2 characters");
$tpl->display("message.tpl"); $tpl->display("message.tpl");
return; return;
} }
if (!isset($hash)) { if (!isset($hash)) {
//$hash = git_read_head($projectroot . $project); //$hash = git_read_head($projectroot . $project);
$hash = "HEAD"; $hash = "HEAD";
} }
   
$co = git_read_commit($projectroot . $project, $hash); $co = git_read_commit($projectroot . $project, $hash);
   
$revlist = explode("\n",trim(git_rev_list($projectroot . $project, $hash, 101, ($page * 100), FALSE, FALSE, $searchtype, $search))); $revlist = explode("\n",trim(git_rev_list($projectroot . $project, $hash, 101, ($page * 100), FALSE, FALSE, $searchtype, $search)));
   
if (count($revlist) < 1 || (strlen($revlist[0]) < 1)) { if (count($revlist) < 1 || (strlen($revlist[0]) < 1)) {
$tpl->clear_all_assign(); $tpl->clear_all_assign();
$tpl->assign("message","No matches for '" . $search . "'."); $tpl->assign("message","No matches for '" . $search . "'.");
$tpl->display("message.tpl"); $tpl->display("message.tpl");
return; return;
} }
   
$tpl->clear_all_assign(); $tpl->clear_all_assign();
$tpl->assign("project",$project); $tpl->assign("project",$project);
$tpl->assign("hash",$hash); $tpl->assign("hash",$hash);
$tpl->assign("treehash",$co['tree']); $tpl->assign("treehash",$co['tree']);
$tpl->display("search_nav.tpl"); $tpl->display("search_nav.tpl");
   
$tpl->assign("search",$search); $tpl->assign("search",$search);
$tpl->assign("searchtype",$searchtype); $tpl->assign("searchtype",$searchtype);
if ($page > 0) { if ($page > 0) {
$tpl->assign("firstlink",TRUE); $tpl->assign("firstlink",TRUE);
$tpl->assign("prevlink",TRUE); $tpl->assign("prevlink",TRUE);
if ($page > 1) if ($page > 1)
$tpl->assign("prevpage",$page-1); $tpl->assign("prevpage",$page-1);
} }
if (count($revlist) > 100) { if (count($revlist) > 100) {
$tpl->assign("nextlink",TRUE); $tpl->assign("nextlink",TRUE);
$tpl->assign("nextpage",$page+1); $tpl->assign("nextpage",$page+1);
} }
$tpl->display("search_pagenav.tpl"); $tpl->display("search_pagenav.tpl");
   
$tpl->assign("title",$co['title']); $tpl->assign("title",$co['title']);
$tpl->display("search_header.tpl"); $tpl->display("search_header.tpl");
   
$alternate = FALSE; $alternate = FALSE;
$commitcount = min(100,count($revlist)); $commitcount = min(100,count($revlist));
for ($i = 0; $i < $commitcount; $i++) { for ($i = 0; $i < $commitcount; $i++) {
$tpl->clear_all_assign(); $tpl->clear_all_assign();
$commit = $revlist[$i]; $commit = $revlist[$i];
if (strlen(trim($commit)) > 0) { if (strlen(trim($commit)) > 0) {
$co2 = git_read_commit($projectroot . $project, $commit); $co2 = git_read_commit($projectroot . $project, $commit);
if ($alternate) if ($alternate)
$tpl->assign("class","dark"); $tpl->assign("class","dark");
else else
$tpl->assign("class","light"); $tpl->assign("class","light");
$alternate = !$alternate; $alternate = !$alternate;
$tpl->assign("project",$project); $tpl->assign("project",$project);
$tpl->assign("commit",$commit); $tpl->assign("commit",$commit);
$tpl->assign("agestringage",$co2['age_string_age']); $tpl->assign("agestringage",$co2['age_string_age']);
$tpl->assign("agestringdate",$co2['age_string_date']); $tpl->assign("agestringdate",$co2['age_string_date']);
$tpl->assign("authorname",$co2['author_name']); $tpl->assign("authorname",$co2['author_name']);
$tpl->assign("title_short",$co2['title_short']); $tpl->assign("title_short",$co2['title_short']);
if (strlen($co2['title_short']) < strlen($co2['title'])) if (strlen($co2['title_short']) < strlen($co2['title']))
$tpl->assign("title",$co2['title']); $tpl->assign("title",$co2['title']);
$tpl->assign("committree",$co2['tree']); $tpl->assign("committree",$co2['tree']);
$matches = array(); $matches = array();
foreach ($co2['comment'] as $comline) { foreach ($co2['comment'] as $comline) {
if (eregi("(.*)(" . quotemeta($search) . ")(.*)",$comline,$regs)) { $hl = highlight($comline, $search, "searchmatch", GITPHP_TRIM_LENGTH);
$linelen = strlen($regs[0]); if ($hl && (strlen($hl) > 0))
if ($linelen > GITPHP_TRIM_LENGTH) { $matches[] = $hl;
$matchlen = strlen($regs[2]);  
$remain = floor((GITPHP_TRIM_LENGTH - $matchlen) / 2);  
$leftlen = strlen($regs[1]);  
$rightlen = strlen($regs[3]);  
if ($leftlen > $remain) {  
$leftremain = $remain;  
if ($rightlen < $remain)  
$leftremain += ($remain - $rightlen);  
$regs[1] = "..." . substr($regs[1], ($leftlen - ($leftremain - 3)));  
}  
if ($rightlen > $remain) {  
$rightremain = $remain;  
if ($leftlen < $remain)  
$rightremain += ($remain - $leftlen);  
$regs[3] = substr($regs[3],0,$rightremain-3) . "...";  
}  
}  
$matches[] = $regs[1] . "<span class=\"searchmatch\">" . $regs[2] . "</span>" . $regs[3];  
}  
} }
$tpl->assign("matches",$matches); $tpl->assign("matches",$matches);
$tpl->display("search_item.tpl"); $tpl->display("search_item.tpl");
} }
} }
   
$tpl->clear_all_assign(); $tpl->clear_all_assign();
$tpl->assign("project",$project); $tpl->assign("project",$project);
$tpl->assign("hash",$hash); $tpl->assign("hash",$hash);
$tpl->assign("search",$search); $tpl->assign("search",$search);
$tpl->assign("searchtype",$searchtype); $tpl->assign("searchtype",$searchtype);
if (count($revlist) > 100) { if (count($revlist) > 100) {
$tpl->assign("nextlink",TRUE); $tpl->assign("nextlink",TRUE);
$tpl->assign("nextpage",$page+1); $tpl->assign("nextpage",$page+1);
} }
$tpl->display("search_footer.tpl"); $tpl->display("search_footer.tpl");
} }
   
?> ?>
   
  <?php
  /*
  * util.highlight.php
  * gitphp: A PHP git repository browser
  * Component: Utility - highlight a string
  *
  * Copyright (C) 2008 Christopher Han <xiphux@gmail.com>
  */
 
  function highlight($haystack, $needle, $highlightclass, $trimlen = NULL)
  {
  if (eregi("(.*)(" . quotemeta($needle) . ")(.*)",$haystack,$regs)) {
  if (isset($trimlen) && ($trimlen > 0)) {
  $linelen = strlen($regs[0]);
  if ($linelen > $trimlen) {
  $matchlen = strlen($regs[2]);
  $remain = floor(($trimlen - $matchlen) / 2);
  $leftlen = strlen($regs[1]);
  $rightlen = strlen($regs[3]);
  if ($leftlen > $remain) {
  $leftremain = $remain;
  if ($rightlen < $remain)
  $leftremain += ($remain - $rightlen);
  $regs[1] = "..." . substr($regs[1], ($leftlen - ($leftremain - 3)));
  }
  if ($rightlen > $remain) {
  $rightremain = $remain;
  if ($leftlen < $remain)
  $rightremain += ($remain - $leftlen);
  $regs[3] = substr($regs[3],0,$rightremain-3) . "...";
  }
  }
  }
  $ret = $regs[1] . "<span";
  if ($highlightclass)
  $ret .= " class=\"" . $highlightclass . "\"";
  $ret .= ">" . $regs[2] . "</span>" . $regs[3];
  return $ret;
  }
 
  return false;
  }
 
  ?>
 
comments