Pelzini

This is the code documentation for the Pelzini project

source of /viewer/controllers/search.php

Does a search of the database
  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.  * Does a search of the database
  23.  *
  24.  * @package Viewer
  25.  * @author Josh Heidenreich
  26.  * @since 0.1
  27.  * @tag i18n-done
  28.  **/
  29.  
  30. require_once 'functions.php';
  31. require_once 'search_functions.php';
  32.  
  33. $_GET['q'] = @trim($_GET['q']);
  34. $_GET['path'] = @trim($_GET['path']);
  35.  
  36. // class::method -> redirect to function page
  37. if (preg_match('/^([a-zA-Z0-9_]+)::([a-zA-Z0-9_]+)$/', $_GET['q'], $matches)) {
  38. $class = db_escape($matches[1]);
  39. $method = db_escape($matches[2]);
  40. $q = "SELECT functions.id
  41. FROM functions
  42. INNER JOIN classes ON functions.classid = classes.id
  43. WHERE classes.name = '{$class}'
  44. AND functions.name = '{$method}'
  45. AND functions.projectid = {$project['id']}
  46. LIMIT 2";
  47. $res = db_query($q);
  48. $num = db_num_rows($res);
  49. if ($num == 1) {
  50. $row = db_fetch_assoc($res);
  51. redirect('function?name=' . urlencode($matches[2]) . '&memberof=' . urlencode($matches[1]) . '&q=' . urlencode($_GET['q']));
  52. }
  53. }
  54.  
  55. $skin['page_name'] = str(STR_SEARCH_TITLE);
  56. require_once 'head.php';
  57.  
  58. if ($_GET['q'] == '' and $_GET['path'] == '') {
  59. echo '<p>You must specify a search term.</p>';
  60. require_once 'foot.php';
  61. }
  62.  
  63. $query = db_escape($_GET['q']);
  64. $_GET['advanced'] = (int) $_GET['advanced'];
  65. $results = false;
  66.  
  67. // Determine the match string
  68. // #ITEM# will be replaced in the specific search query
  69. // for for classes, #ITEM# will become classes.name
  70. $match_string = "#ITEM# '";
  71.  
  72. $extra_where = '1';
  73. if (!empty($_GET['path'])) {
  74. $path = db_escape($_GET['path']);
  75. $extra_where = "files.name LIKE '%{$path}%'";
  76. }
  77.  
  78.  
  79. echo "<img src=\"assets/icon_remove.png\" alt=\"\" title=\"Hide this result\" onclick=\"hide_content(event)\" class=\"showhide\">";
  80. echo "<span style=\"float: right;\">", str(STR_SHOW_HIDE_ALL), " &nbsp;</span>";
  81.  
  82. echo '<h2>', str(STR_SEARCH_TITLE), '</h2>';
  83. echo '<p>', str(STR_YOU_SEARCHED_FOR, 'term', htmlspecialchars($_GET['q'])), '</p>';
  84.  
  85. // classes
  86. if (@$_GET['advanced'] == 0 or @$_GET['classes'] == 'y') {
  87. $q = "SELECT classes.id, classes.name, classes.description, classes.extends, classes.abstract,
  88. files.name as filename, classes.fileid,
  89. IF(BINARY classes.name = '{$query}', 1, 0) +
  90. IF(classes.name LIKE '{$query}', 1, 0) +
  91. IF(classes.name LIKE '{$query}%', 1, 0) +
  92. 0 AS relevancy
  93. FROM classes
  94. INNER JOIN files ON classes.fileid = files.id
  95. WHERE classes.name LIKE '%{$query}%'
  96. AND classes.projectid = {$project['id']}
  97. AND {$extra_where}
  98. ORDER BY relevancy DESC, classes.name";
  99.  
  100. $res = db_query ($q);
  101. $num = db_num_rows ($res);
  102. if ($num != 0) {
  103. $results = true;
  104. echo '<h3>', str(STR_CLASSES_RESULT, 'num', $num), '</h3>';
  105.  
  106. $alt = false;
  107. echo '<div class="list">';
  108. while ($row = db_fetch_assoc ($res)) {
  109. $class = 'item';
  110. if ($alt) $class .= '-alt';
  111.  
  112. echo "<div class=\"{$class}\">";
  113. echo "<img src=\"assets/icon_remove.png\" alt=\"\" title=\"Hide this result\" onclick=\"hide_content(event)\" class=\"showhide\">";
  114. echo "<p><strong>", get_class_link($row['name'], $row['filename']), "</strong>";
  115.  
  116. if ($row['extends'] != null) {
  117. echo " <small>extends ", get_class_link($row['extends']), "</small>";
  118. }
  119.  
  120. if ($row['abstract'] == 1) {
  121. echo " <small>(abstract)</small>";
  122. }
  123.  
  124. echo "<div class=\"content\">";
  125. echo delink_inline($row['description']);
  126. echo "</div>";
  127. echo "</div>";
  128.  
  129. $alt = ! $alt;
  130. }
  131. echo '</div>';
  132. }
  133. }
  134.  
  135. // interfaces
  136. if (@$_GET['advanced'] == 0 or @$_GET['interfaces'] == 'y') {
  137. $q = "SELECT interfaces.id, interfaces.name, interfaces.description, interfaces.extends,
  138. files.name as filename, interfaces.fileid,
  139. IF(BINARY interfaces.name = '{$query}', 1, 0) +
  140. IF(interfaces.name LIKE '{$query}', 1, 0) +
  141. IF(interfaces.name LIKE '{$query}%', 1, 0) +
  142. 0 AS relevancy
  143. FROM interfaces
  144. INNER JOIN files ON interfaces.fileid = files.id
  145. WHERE interfaces.name LIKE '%{$query}%'
  146. AND interfaces.projectid = {$project['id']}
  147. AND {$extra_where}
  148. ORDER BY relevancy DESC, interfaces.name";
  149.  
  150. $res = db_query ($q);
  151. $num = db_num_rows ($res);
  152. if ($num != 0) {
  153. $results = true;
  154. echo '<h3>', str(STR_INTERFACES_RESULT, 'num', $num), '</h3>';
  155.  
  156. $alt = false;
  157. echo '<div class="list">';
  158. while ($row = db_fetch_assoc ($res)) {
  159. $class = 'item';
  160. if ($alt) $class .= '-alt';
  161.  
  162. echo "<div class=\"{$class}\">";
  163. echo "<img src=\"assets/icon_remove.png\" alt=\"\" title=\"Hide this result\" onclick=\"hide_content(event)\" class=\"showhide\">";
  164. echo "<p><strong>", get_interface_link($row['name'], $row['filename']), "</strong>";
  165.  
  166. if ($row['extends'] != null) {
  167. echo " <small>extends ", get_interface_link($row['extends']), "</small>";
  168. }
  169.  
  170. echo "<div class=\"content\">";
  171. echo delink_inline($row['description']);
  172. echo "</div>";
  173. echo "</div>";
  174.  
  175. $alt = ! $alt;
  176. }
  177. echo '</div>';
  178. }
  179. }
  180.  
  181. // functions
  182. if (@$_GET['advanced'] == 0 or @$_GET['functions'] == 'y') {
  183. $q = "SELECT functions.id, functions.name, functions.description, functions.classid, functions.linenum,
  184. files.name as filename, functions.fileid, classes.name as class,
  185. IF(BINARY functions.name = '{$query}', 1, 0) +
  186. IF(functions.name LIKE '{$query}', 1, 0) +
  187. IF(functions.name LIKE '{$query}%', 1, 0) +
  188. 0 AS relevancy
  189. FROM functions
  190. INNER JOIN files ON functions.fileid = files.id
  191. LEFT JOIN classes ON functions.classid = classes.id
  192. WHERE functions.name LIKE '%{$query}%'
  193. AND functions.projectid = {$project['id']}
  194. AND {$extra_where}
  195. ORDER BY relevancy DESC, functions.name";
  196. $res = db_query ($q);
  197. $num = db_num_rows ($res);
  198. if ($num != 0) {
  199. $results = true;
  200. echo '<h3>', str(STR_FUNCTIONS_RESULT, 'num', $num), '</h3>';
  201.  
  202. $alt = false;
  203. echo '<div class="list">';
  204. while ($row = db_fetch_assoc ($res)) {
  205. $class = 'item';
  206. if ($alt) $class .= '-alt';
  207.  
  208. echo "<div class=\"{$class}\">";
  209. echo "<img src=\"assets/icon_remove.png\" alt=\"\" title=\"Hide this result\" onclick=\"hide_content(event)\" class=\"showhide\">";
  210.  
  211. if ($row['class'] != null) {
  212. $link_text = $row['class'] . '::' . $row['name'];
  213. } else {
  214. $link_text = $row['name'];
  215. }
  216. echo "<p><strong>", get_function_link($row['class'], $row['name'], $link_text), "</strong>";
  217.  
  218. echo "<div class=\"content\">";
  219. echo delink_inline($row['description']);
  220. echo "</div>";
  221. echo "</div>";
  222.  
  223. $alt = ! $alt;
  224. }
  225. echo '</div>';
  226. }
  227. }
  228.  
  229.  
  230. // constants
  231. if (@$_GET['advanced'] == 0 or @$_GET['constants'] == 'y') {
  232. $q = "SELECT constants.name, constants.description, files.name as filename, constants.fileid, constants.value,
  233. IF(BINARY constants.name = '{$query}', 1, 0) +
  234. IF(constants.name LIKE '{$query}', 1, 0) +
  235. IF(constants.name LIKE '{$query}%', 1, 0) +
  236. 0 AS relevancy
  237. FROM constants
  238. INNER JOIN files ON constants.fileid = files.id
  239. WHERE constants.name LIKE '%{$query}%'
  240. AND constants.projectid = {$project['id']}
  241. AND {$extra_where}
  242. ORDER BY relevancy DESC, constants.name";
  243. $res = db_query ($q);
  244. $num = db_num_rows ($res);
  245. if ($num != 0) {
  246. $results = true;
  247. echo '<h3>', str(STR_CONSTANTS_RESULT, 'num', $num), '</h3>';
  248.  
  249. $alt = false;
  250. echo '<div class="list">';
  251. while ($row = db_fetch_assoc ($res)) {
  252. $row['name'] = htmlspecialchars($row['name']);
  253. $row['value'] = htmlspecialchars($row['value']);
  254.  
  255. $class = 'item';
  256. if ($alt) $class .= '-alt';
  257.  
  258. echo "<div class=\"{$class}\">";
  259. echo "<img src=\"assets/icon_remove.png\" alt=\"\" title=\"Hide this result\" onclick=\"hide_content(event)\" class=\"showhide\">";
  260. echo "<p><strong>{$row['name']}</strong>";
  261. echo " = <strong>{$row['value']}</strong>";
  262.  
  263. echo "<div class=\"content\">";
  264. echo delink_inline($row['description']);
  265. echo "</div>";
  266. echo "</div>";
  267.  
  268. $alt = ! $alt;
  269. }
  270. echo '</div>';
  271. }
  272. }
  273.  
  274.  
  275. // documents
  276. if (@$_GET['advanced'] == 0 or @$_GET['documents'] == 'y') {
  277. $q = "SELECT documents.name,
  278. IF(BINARY documents.name = '{$query}', 1, 0) +
  279. IF(documents.name LIKE '{$query}', 1, 0) +
  280. IF(documents.name LIKE '{$query}%', 1, 0) +
  281. 0 AS relevancy
  282. FROM documents
  283. WHERE documents.name LIKE '%{$query}%'
  284. AND documents.projectid = {$project['id']}
  285. ORDER BY relevancy DESC, documents.name";
  286. $res = db_query ($q);
  287. $num = db_num_rows ($res);
  288. if ($num != 0) {
  289. $results = true;
  290. echo '<h3>', str(STR_DOCUMENTS_RESULT, 'num', $num), '</h3>';
  291.  
  292. $alt = false;
  293. echo '<div class="list">';
  294. while ($row = db_fetch_assoc ($res)) {
  295. $row['name'] = htmlspecialchars($row['name']);
  296. $url = htmlspecialchars(urlencode($row['name']));
  297.  
  298. $class = 'item';
  299. if ($alt) $class .= '-alt';
  300.  
  301. echo "<div class=\"{$class}\">";
  302. echo "<a href=\"document?name={$url}\">{$row['name']}</a>";
  303. echo "</div>";
  304.  
  305. $alt = ! $alt;
  306. }
  307. echo '</div>';
  308. }
  309. }
  310.  
  311.  
  312. // source
  313. if (@$_GET['advanced'] == 0 or @$_GET['source'] == 'y') {
  314. $source_results = search_source(@$_GET['q'], false, @$_GET['path']);
  315. if ($source_results) {
  316. $results = true;
  317. }
  318. }
  319.  
  320.  
  321. // no results
  322. if (! $results) {
  323. echo "<p>Nothing was found!</p>";
  324. }
  325.  
  326.  
  327. require_once 'foot.php';
  328. ?>
  329.