PageRenderTime 97ms CodeModel.GetById 26ms RepoModel.GetById 1ms app.codeStats 0ms

/search.inc.php

https://github.com/adamfranco/segue-1.x
PHP | 466 lines | 346 code | 79 blank | 41 comment | 43 complexity | 1f03942a577a53b09a7ce71a07f26281 MD5 | raw file
  1. <? /* $Id$ */
  2. require_once("objects/objects.inc.php");
  3. //require_once(dirname(__FILE__)."/objects/String.class.php");
  4. $content = '';
  5. //ob_start();
  6. /******************************************************************************
  7. * This script is an adaptation of fullstory.php
  8. * this script is included in site.inc.php when search variable is set
  9. ******************************************************************************/
  10. if ($_REQUEST['action'] == 'viewsite')
  11. $action = 'viewsite';
  12. else
  13. $action = 'site';
  14. //if ($tmp = $_REQUEST['order']) {
  15. // $_SESSION['order'] = $_REQUEST['order'];
  16. //}
  17. /* if ($tmp2 = $_REQUEST['recent']) { */
  18. /* $_SESSION['recent'] = ($tmp2=='true')?true:false; */
  19. /* } */
  20. //printpre($_SESSION);
  21. //printpre($_REQUEST);
  22. $partialstatus = 1;
  23. $siteObj =& new site($_REQUEST[site]);
  24. $sectionObj =& new section($_REQUEST[site],$_REQUEST[section], $siteObj);
  25. $pageObj =& new page($_REQUEST[site],$_REQUEST[section],$_REQUEST[page], $sectionObj);
  26. $storyObj =& new story($_REQUEST[site],$_REQUEST[section],$_REQUEST[page],$_REQUEST[story], $pageObj);
  27. $getinfo = "site=".$siteObj->name."&amp;section=".$sectionObj->id."&amp;page=".$pageObj->id."&amp;story=".$storyObj->id."&amp;detail=".$storyObj->id;
  28. $getinfo2 = "site=".$siteObj->name."&amp;section=".$sectionObj->id."&amp;page=".$pageObj->id;
  29. $editsettingsurl = "&amp;site=".$siteObj->name."&amp;section=".$sectionObj->id."&amp;page=".$pageObj->id."&amp;action=edit_story&amp;edit_story=".$storyObj->id."&amp;detail=".$storyObj->id."&amp;step=4&amp;goback=discuss&amp;link=1";
  30. $storyObj->fetchFromDB();
  31. $storyObj->owningSiteObj->fetchFromDB();
  32. //$site_owner=slot::getOwner($story->owningSiteObj->name);
  33. $site_owner=$storyObj->owningSiteObj->owner;
  34. //$site_owner =$siteObj->owner;
  35. //print_r($story->owningSiteObj);
  36. //print $site_owner;
  37. //printpre($site_owner);
  38. if ($_REQUEST['search']) {
  39. $search = $_REQUEST['search'];
  40. // $pageResults = searchPages($search);
  41. $sectionResults = searchSections($_REQUEST['search'], $_REQUEST[site]);
  42. $pageResults = searchPages($_REQUEST['search'], $_REQUEST[site]);
  43. $contentResults = searchContent($_REQUEST['search'], $_REQUEST[site]);
  44. $discussResults = searchDiscussions($_REQUEST['search'], $_REQUEST[site]);
  45. /******************************************************************************
  46. * Print out search results
  47. ******************************************************************************/
  48. $totalResults = count($pageResults) + count($contentResults) + count($discussResults);
  49. // printc($totalResults." found");
  50. printc("<table width='100%' border='0' align='center' cellpadding='4' cellspacing='0'>");
  51. printc("\n\t<tr><td></td><td></td></tr>");
  52. printc("\n\t<tr><td colspan='2' align='left' class='title'>Found: '$search'</td></tr>");
  53. // printc("<tr><td>Site</td><td>Author</td></tr>");
  54. if (count($sectionResults) > 0) {
  55. printc("\n\t<tr><td class='title2' >Section titles containing: '$search'</td><td></td></tr>");
  56. foreach ($sectionResults as $result) {
  57. printContentItem($result, "section", $siteObj, $search);
  58. }
  59. }
  60. if (count($pageResults) > 0) {
  61. printc("\n\t<tr><td class='title2' >Pages titles containing: '$search'</td><td></td></tr>");
  62. foreach ($pageResults as $result) {
  63. printContentItem($result, "page", $siteObj, $search);
  64. }
  65. }
  66. if (count($contentResults) > 0) {
  67. printc("\n\t<tr><td class='title2' >'$search' in text</td><td></td></tr>");
  68. foreach ($contentResults as $result) {
  69. printContentItem($result, "content", $siteObj, $search);
  70. }
  71. }
  72. if (count($discussResults) > 0) {
  73. printc("\n\t<tr><td class='title2' >'$search' in discussions</td><td></td></tr>");
  74. foreach ($discussResults as $result) {
  75. printContentItem($result, "discussion", $siteObj);
  76. }
  77. }
  78. printc("</table>");
  79. }
  80. function printContentItem($result, $type, & $siteObj, $search="") {
  81. global $_full_uri, $site_owner;
  82. $foundSection =& new section($_REQUEST[site],$result['section_id'], $siteObj);
  83. $foundPage =& new page($_REQUEST[site],$result['section_id'], $result['page_id'], $foundSection);
  84. $foundContent =& new story($_REQUEST[site], $result['section_id'], $result['page_id'], $result['story_id'], $foundPage);
  85. if (($foundSection->canview() && $foundPage->canview() && $foundContent->canview()) || $_SESSION[auser] == $site_owner) {
  86. ob_start();
  87. print "\n\t<tr>";
  88. $record_tag_tstamp = timestamp2usdate($result[story_updated_tstamp]);
  89. //if ($record_tag_tstamp != '0000-00-00 00:00:00') {
  90. //$record_tag_tstamp =& TimeStamp::fromString($record_tag_tstamp);
  91. //$record_tag_time =& $record_tag_tstamp->asTime();
  92. //print "<td valign='top' class='listtext'>".$record_tag_tstamp->ymdString()."<br/>".$record_tag_time->string12(false)."</td>";
  93. // print "<td valign='top' class='listtext'>".$record_tag_tstamp."</td>";
  94. print "\n\t\t<td valign='top'><a href='".$_full_uri."/index.php?&amp;action=site&amp;site=".$_REQUEST[site];
  95. print "&amp;section=".$result['section_id'];
  96. print "&amp;page=".$result['page_id'];
  97. print "&amp;story=".$result['story_id'];
  98. print "&amp;detail=".$result['story_id'];
  99. if ($type == "discussion") {
  100. print "#".$result['discussion_id'];
  101. }
  102. print "'>";
  103. print stripslashes(urldecode($result['section_title']));
  104. print " > ".stripslashes(urldecode($result['page_title']));
  105. if ($result['story_title']) print " > ".stripslashes(urldecode($result['story_title']));
  106. if ($result['discussion_subject']) {
  107. print " > ".stripslashes(urldecode($result['discussion_subject']));
  108. print " (".$result['user_fname'].")";
  109. }
  110. print "</a>\n\t\t</td>";
  111. // print "<tr><td valign='top' class='contentinfo'>added by".$result['user_fname']." on ".$record_tag_tstamp."</td>";
  112. print "\n\t</tr>";
  113. print "\n\t\t<tr>";
  114. print "\n\t\t\t<td class='list' valign='top'>";
  115. if ($type == "content") {
  116. $content = stripslashes(urldecode($result['story_text_short']));
  117. $content .= stripslashes(urldecode($result['story_text_long']));
  118. } else if ($type == "discussion") {
  119. $content = stripslashes(urldecode($result['discussion_content']));
  120. }
  121. $wikiResolver =& WikiResolver::instance();
  122. $content = $wikiResolver->parseText($content, $_REQUEST[site], $result['section_id'], $result['page_id']);
  123. $content = find_abstract($content, $search);
  124. if ($type != "page" && $type != "section") {
  125. print $content;
  126. }
  127. print "\n\t\t</td>";
  128. print "\n\t</tr>";
  129. //}
  130. $contentItem = ob_get_clean();
  131. printc($contentItem);
  132. }
  133. }
  134. function searchContent ($search, $site) {
  135. $terms = explode(" ", $search);
  136. foreach ($terms as $key => $term) {
  137. $terms[$key] = " LIKE ('%".addslashes($term)."%')";
  138. }
  139. $limit = 10;
  140. $query = "
  141. SELECT
  142. story_id, page_id, section_id, story_updated_tstamp,
  143. slot_name, page_title, section_title, story_title,
  144. story_text_short, story_text_long, user_fname, user_uname
  145. FROM
  146. slot
  147. INNER JOIN
  148. section
  149. ON section.FK_site = slot.FK_site
  150. INNER JOIN
  151. page
  152. ON FK_section = section_id
  153. INNER JOIN
  154. story
  155. ON FK_page = page_id
  156. INNER JOIN
  157. user
  158. ON story.FK_createdby = user_id
  159. WHERE
  160. slot_name = '".addslashes($site)."'
  161. AND (
  162. ";
  163. // Stories
  164. $query .= "\n\n(";
  165. foreach ($terms as $i => $term) {
  166. if ($i > 0) {
  167. $query .= "\n AND ";
  168. }
  169. $query .= "\n\t(";
  170. $query .= "story_text_short ".$term;
  171. $query .= "\n\tOR story_text_long ".$term;
  172. $query .= "\n\tOR story_title ".$term;
  173. $query .= ")";
  174. }
  175. $query .= "\n)";
  176. $query .= "
  177. )
  178. Order BY
  179. story_updated_tstamp DESC
  180. LIMIT 0, $limit
  181. ";
  182. //printpre($query);
  183. $r = db_query($query);
  184. if (db_num_rows($r) > 0) {
  185. $found_content = array();
  186. while ($a = db_fetch_assoc($r)) {
  187. $a[story_text_short] = stripslashes(urldecode($a['story_text_short']));
  188. $found_content[] = $a;
  189. }
  190. }
  191. return $found_content;
  192. }
  193. function searchDiscussions ($search, $site) {
  194. $terms = explode(" ", $search);
  195. foreach ($terms as $key => $term) {
  196. $terms[$key] = " LIKE ('%".addslashes($term)."%')";
  197. }
  198. $limit = 10;
  199. $query = "
  200. SELECT
  201. discussion_content, discussion_tstamp, discussion_subject, discussion_id, user_fname,
  202. story_id, story_title, page_title, section_title, page_id, section_id,
  203. FK_author, user_uname
  204. FROM
  205. slot
  206. INNER JOIN
  207. section
  208. ON section.FK_site = slot.FK_site
  209. INNER JOIN
  210. page
  211. ON FK_section = section_id
  212. INNER JOIN
  213. story
  214. ON FK_page = page_id
  215. INNER JOIN
  216. discussion
  217. ON story_id = FK_story
  218. INNER JOIN
  219. user
  220. ON discussion.FK_author = user_id
  221. WHERE
  222. slot_name = '".addslashes($site)."'
  223. AND (
  224. ";
  225. // Stories
  226. $query .= "\n\n(";
  227. foreach ($terms as $i => $term) {
  228. if ($i > 0) {
  229. $query .= "\n AND ";
  230. }
  231. $query .= "\n\t(";
  232. $query .= "discussion_content ".$term;
  233. $query .= "\n\tOR discussion_subject ".$term;
  234. $query .= "\n\tOR user_fname ".$term;
  235. $query .= ")";
  236. }
  237. $query .= "\n)";
  238. $query .= "
  239. )
  240. Order BY
  241. discussion_tstamp DESC
  242. LIMIT 0, $limit
  243. ";
  244. //printpre($query);
  245. $r = db_query($query);
  246. if (db_num_rows($r) > 0) {
  247. $found_discussions = array();
  248. while ($a = db_fetch_assoc($r)) {
  249. $a[story_text_short] = stripslashes(urldecode($a['story_text_short']));
  250. $found_discussions[] = $a;
  251. }
  252. }
  253. return $found_discussions;
  254. }
  255. function searchPages ($search, $site) {
  256. $terms = explode(" ", $search);
  257. foreach ($terms as $key => $term) {
  258. $terms[$key] = " LIKE ('%".addslashes($term)."%')";
  259. }
  260. $limit = 10;
  261. $query = "
  262. SELECT
  263. page_title, page_updated_tstamp, section_title, page_id, section_id
  264. FROM
  265. slot
  266. INNER JOIN
  267. section
  268. ON section.FK_site = slot.FK_site
  269. INNER JOIN
  270. page
  271. ON FK_section = section_id
  272. WHERE
  273. slot_name = '".addslashes($site)."'
  274. AND (
  275. ";
  276. // Stories
  277. $query .= "\n\n(";
  278. foreach ($terms as $i => $term) {
  279. if ($i > 0) {
  280. $query .= "\n AND ";
  281. }
  282. $query .= "\n\t(";
  283. $query .= "page_title ".$term;
  284. $query .= ")";
  285. }
  286. $query .= "\n)";
  287. $query .= "
  288. )
  289. Order BY
  290. page_updated_tstamp DESC
  291. LIMIT 0, $limit
  292. ";
  293. //printpre($query);
  294. $r = db_query($query);
  295. if (db_num_rows($r) > 0) {
  296. $found_pages = array();
  297. while ($a = db_fetch_assoc($r)) {
  298. $a[page_title] = stripslashes(urldecode($a['page_title']));
  299. $found_pages[] = $a;
  300. }
  301. }
  302. return $found_pages;
  303. }
  304. function searchSections ($search, $site) {
  305. $terms = explode(" ", $search);
  306. foreach ($terms as $key => $term) {
  307. $terms[$key] = " LIKE ('%".addslashes($term)."%')";
  308. }
  309. $limit = 10;
  310. $query = "
  311. SELECT
  312. section_title, section_updated_tstamp, section_title, section_id
  313. FROM
  314. slot
  315. INNER JOIN
  316. section
  317. ON section.FK_site = slot.FK_site
  318. WHERE
  319. slot_name = '".addslashes($site)."'
  320. AND (
  321. ";
  322. // Stories
  323. $query .= "\n\n(";
  324. foreach ($terms as $i => $term) {
  325. if ($i > 0) {
  326. $query .= "\n AND ";
  327. }
  328. $query .= "\n\t(";
  329. $query .= "section_title ".$term;
  330. $query .= ")";
  331. }
  332. $query .= "\n)";
  333. $query .= "
  334. )
  335. Order BY
  336. section_updated_tstamp DESC
  337. LIMIT 0, $limit
  338. ";
  339. //printpre($query);
  340. $r = db_query($query);
  341. if (db_num_rows($r) > 0) {
  342. $found_sections = array();
  343. while ($a = db_fetch_assoc($r)) {
  344. $a[section_title] = stripslashes(urldecode($a['section_title']));
  345. $found_sections[] = $a;
  346. }
  347. }
  348. return $found_sections;
  349. }
  350. function find_abstract ($content, $search, $numwords=25) {
  351. $content = eregi_replace("<br>", "..", $content);
  352. $content = eregi_replace("<br \/>", "..", $content);
  353. // $content = eregi_replace("\[\[linkpath\]\]", "...", $content);
  354. $search = " ".$search;
  355. $htmlstring =& HtmlString::withValue($content);
  356. $content = $htmlstring->stripTagsAndTrim(500);
  357. $lowercontent = strtolower($content);
  358. $searchstart = strpos($lowercontent, $search);
  359. if ($searchstart < 60) {
  360. $searchbegin = 0;
  361. $searchend = 150;
  362. } else if ($searchstart == 0) {
  363. $searchbegin = 0;
  364. $searchend = 150;
  365. } else {
  366. $searchbegin = $searchstart - 50;
  367. $searchend = $searchstart + 150;
  368. }
  369. $content = substr($content, $searchbegin, 500);
  370. $htmlstring =& HtmlString::withValue($content);
  371. $content = $htmlstring->stripTagsAndTrim(25);
  372. $clean = explode(" ", $content);
  373. $clean_content = "";
  374. foreach ($clean as $word) {
  375. if (strlen($word) > 50) $word = substr($word, 0, 50)."...";
  376. $clean_content .= $word." ";
  377. }
  378. $search_term = "<span class='foundtext'>".$search."</span>";
  379. $clean_content = eregi_replace($search, $search_term, $clean_content);
  380. if ($searchstart < 40) {
  381. $clean_content = $clean_content."...";
  382. } else {
  383. $clean_content = "...".$clean_content."...";
  384. }
  385. return $clean_content;
  386. }
  387. ?>