display.git_search_files.php 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. <?php
  2. /*
  3. * display.git_search_files.php
  4. * gitphp: A PHP git repository browser
  5. * Component: Display - search in files
  6. *
  7. * Copyright (C) 2009 Christopher Han <xiphux@gmail.com>
  8. */
  9. require_once('defs.constants.php');
  10. require_once('util.highlight.php');
  11. require_once('gitutil.git_filesearch.php');
  12. require_once('gitutil.git_read_commit.php');
  13. require_once('display.git_message.php');
  14. function git_search_files($projectroot, $project, $hash, $search, $page = 0)
  15. {
  16. global $tpl,$gitphp_conf;
  17. $cachekey = sha1($project) . "|" . $hash . "|" . "filesearch" . "|" . sha1($search) . "|" . $page;
  18. if (!$tpl->is_cached('searchfiles.tpl', $cachekey)) {
  19. if (!($gitphp_conf['search'] && $gitphp_conf['filesearch'])) {
  20. git_message("File search has been disabled", TRUE, TRUE);
  21. return;
  22. }
  23. if (!isset($search) || (strlen($search) < 2)) {
  24. git_message("You must enter search text of at least 2 characters", TRUE, TRUE);
  25. return;
  26. }
  27. if (!isset($hash)) {
  28. //$hash = git_read_head($projectroot . $project);
  29. $hash = "HEAD";
  30. }
  31. $co = git_read_commit($projectroot . $project, $hash);
  32. $filesearch = git_filesearch($projectroot . $project, $hash, $search, false, ($page * 100), 101);
  33. if (count($filesearch) < 1) {
  34. git_message("No matches for '" . $search . "'.", FALSE, TRUE);
  35. return;
  36. }
  37. $tpl->assign("hash",$hash);
  38. $tpl->assign("treehash",$co['tree']);
  39. $tpl->assign("search",$search);
  40. $tpl->assign("searchtype","file");
  41. $tpl->assign("page",$page);
  42. $filesearchcount = count($filesearch);
  43. $tpl->assign("filesearchcount",$filesearchcount);
  44. $tpl->assign("title",$co['title']);
  45. $filesearchlines = array();
  46. $i = 0;
  47. foreach ($filesearch as $file => $data) {
  48. $filesearchline = array();
  49. $filesearchline["file"] = $file;
  50. if (strpos($file,"/") !== false) {
  51. $f = basename($file);
  52. $d = dirname($file);
  53. if ($d == "/")
  54. $d = "";
  55. $hlt = highlight($f, $search, "searchmatch");
  56. if ($hlt)
  57. $hlt = $d . "/" . $hlt;
  58. } else
  59. $hlt = highlight($file, $search, "searchmatch");
  60. if ($hlt)
  61. $filesearchline["filename"] = $hlt;
  62. else
  63. $filesearchline["filename"] = $file;
  64. $filesearchline["hash"] = $data['hash'];
  65. if ($data['type'] == "tree")
  66. $filesearchline["tree"] = TRUE;
  67. if (isset($data['lines'])) {
  68. $matches = array();
  69. foreach ($data['lines'] as $line) {
  70. $hlt = highlight($line,$search,"searchmatch",floor(GITPHP_TRIM_LENGTH*1.5),true);
  71. if ($hlt)
  72. $matches[] = $hlt;
  73. }
  74. if (count($matches) > 0)
  75. $filesearchline["matches"] = $matches;
  76. }
  77. $filesearchlines[] = $filesearchline;
  78. $i++;
  79. if ($i >= 100)
  80. break;
  81. }
  82. $tpl->assign("filesearchlines",$filesearchlines);
  83. }
  84. $tpl->display('searchfiles.tpl', $cachekey);
  85. }
  86. ?>