Pelzini

This is the code documentation for the Pelzini project

source of /viewer/search_functions.php

Back-end functions behind search
Allows for searching outside of search.php
  1. <?php
  2. /*
  3. Copyright 2008 Josh Heidenreich
  4.  
  5. This file is part of Pelzini.
  6.  
  7. Pelzini is free software: you can redistribute it and/or modify
  8. it under the terms of the GNU General Public License as published by
  9. the Free Software Foundation, either version 3 of the License, or
  10. (at your option) any later version.
  11.  
  12. Pelzini is distributed in the hope that it will be useful,
  13. but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. GNU General Public License for more details.
  16.  
  17. You should have received a copy of the GNU General Public License
  18. along with Pelzini. If not, see <http://www.gnu.org/licenses/>.
  19. */
  20.  
  21.  
  22. /**
  23.  * Back-end functions behind search
  24.  * Allows for searching outside of search.php
  25.  *
  26.  * @package Viewer
  27.  * @author Josh Heidenreich
  28.  * @since 0.1
  29.  * @tag i18n-done
  30.  **/
  31.  
  32.  
  33. /**
  34.  * Does a source search.
  35.  *
  36.  * @return boolean True if some results were found, false if nothing was found
  37.  **/
  38. function search_source($query, $case_sensitive = false, $path_contains = null)
  39. {
  40. global $project;
  41.  
  42. // Determine the match string
  43. // #ITEM# will be replaced in the specific search query
  44. // for for classes, #ITEM# will become classes.name
  45. $this_match_string = "files.source LIKE '%" . db_escape($query) . "%'";
  46. if ($case_sensitive) $this_match_string = "BINARY files.source LIKE '%" . db_escape($query) . "%'";
  47.  
  48. $extra_where = '1';
  49. if (!empty($path_contains)) $extra_where = "files.name LIKE '%" . db_escape($path_contains) . "%'";
  50.  
  51. $q = "SELECT files.id, files.name AS filename, files.source
  52. FROM files
  53. WHERE {$this_match_string}
  54. AND files.projectid = {$project['id']}
  55. AND {$extra_where}
  56. ORDER BY files.name";
  57. $res = db_query ($q);
  58. $num_files = db_num_rows ($res);
  59.  
  60. if ($num_files != 0) {
  61. echo '<h3>', str(STR_SOURCE_CODE_RESULT, 'num', $num_files), '</h3>';
  62.  
  63. $regex_search = htmlspecialchars($query);
  64. $regex_search = '/(' . preg_quote($regex_search, '/'). ')/';
  65. if (! $case_sensitive) $regex_search .= 'i';
  66. $url_keyword = urlencode($query);
  67.  
  68. $num_lines = 0;
  69. $alt = false;
  70. echo '<div class="list">';
  71. while ($row = db_fetch_assoc ($res)) {
  72. $row['filename'] = htmlspecialchars($row['filename']);
  73.  
  74. $class = 'item';
  75. if ($alt) $class .= '-alt';
  76.  
  77. echo "<div class=\"{$class}\">";
  78. echo "<img src=\"assets/icon_remove.png\" alt=\"\" title=\"Hide this result\" onclick=\"hide_content(event)\" class=\"showhide\">";
  79. echo "<p>";
  80. echo "<strong><a href=\"file?name={$row['filename']}\">{$row['filename']}</a></strong> &nbsp; ";
  81. echo "<small><a href=\"file_source?name={$row['filename']}&keyword={$url_keyword}\">Highlighted file source</a></small>";
  82. echo "</p>\n";
  83.  
  84. // Finds the lines, and highlights the term
  85. echo "<p class=\"content\">";
  86. $lines = explode("\n", $row['source']);
  87. foreach ($lines as $num => $line) {
  88. if (stripos($line, $query) !== false) {
  89. $num++;
  90. $line = htmlspecialchars($line);
  91. $line = preg_replace($regex_search, "<span class=\"highlight\">\$1</span>", $line);
  92.  
  93. $source_url = "file_source?name={$row['filename']}&highlight={$num}";
  94. if ($num > 5) $source_url .= '#src-lines-' . ($num - 5);
  95.  
  96. $num_lines++;
  97.  
  98. echo "Line <a href=\"{$source_url}\">{$num}</a>: <code>{$line}</code><br>";
  99. }
  100. }
  101. echo "</p>";
  102. echo "</div>";
  103.  
  104. $alt = ! $alt;
  105. }
  106.  
  107. echo "<div class=\"summary\">";
  108. echo '<p>', str(STR_NUM_SOURCE_RESULTS, 'lines', $num_lines, 'files', $num_files), '</p>';
  109. echo "</div>";
  110.  
  111. echo '</div>';
  112.  
  113.  
  114. return true;
  115.  
  116. } else {
  117. return false;
  118. }
  119. }
  120.  
  121.  
  122. ?>
  123.