/mambots/search/content.searchbot.php

https://github.com/doctorgrif/Joostina-1.2.1 · PHP · 156 lines · 128 code · 0 blank · 28 comment · 11 complexity · 2d48bd62f109b8fa947b857b9c59a366 MD5 · raw file

  1. <?php
  2. /**
  3. * @package Joostina
  4. * @copyright Àâòîðñêèå ïðàâà (C) 2008 Joostina team. Âñå ïðàâà çàùèùåíû.
  5. * @license Ëèöåíçèÿ http://www.gnu.org/licenses/gpl-2.0.htm GNU/GPL, èëè help/license.php
  6. * Joostina! - ñâîáîäíîå ïðîãðàììíîå îáåñïå÷åíèå ðàñïðîñòðàíÿåìîå ïî óñëîâèÿì ëèöåíçèè GNU/GPL
  7. * Äëÿ ïîëó÷åíèÿ èíôîðìàöèè î èñïîëüçóåìûõ ðàñøèðåíèÿõ è çàìå÷àíèé îá àâòîðñêîì ïðàâå, ñìîòðèòå ôàéë help/copyright.php.
  8. */
  9. // çàïðåò ïðÿìîãî äîñòóïà
  10. defined('_VALID_MOS') or die();
  11. $_MAMBOTS->registerFunction('onSearch', 'botSearchContent');
  12. /**
  13. * Content Search method
  14. * çàïðîñ sql äîëæåí âîçâðàòèòü ïîëÿ, èñïîëüçóþòñÿ â îáû÷íîé îïåðàöèè
  15. * îòîáðàæåíèÿ: href, title, section, created, text, browsernav
  16. * @param îïðåäåëÿåò öåëü ïîèñêà
  17. * @param ñîïîñòàâëÿåò ïàðàìåòðû: exact|any|all
  18. * @param îïðåäåëÿåò ïàðàìåòð ñîðòèðîâêè: newest|oldest|popular|alpha|category
  19. */
  20. function botSearchContent($text, $phrase = '', $ordering = '', $category = '') {
  21. global $database, $my, $_MAMBOTS;
  22. // check if param query has previously been processed
  23. if (!isset($_MAMBOTS->_search_mambot_params['content'])) {
  24. // load mambot params info
  25. $query = "SELECT params" . "\n FROM #__mambots" . "\n WHERE element = 'content.searchbot'" .
  26. "\n AND folder = 'search'";
  27. $database->setQuery($query);
  28. $database->loadObject($mambot);
  29. // save query to class variable
  30. $_MAMBOTS->_search_mambot_params['content'] = $mambot;
  31. }
  32. // pull query data from class variable
  33. $mambot = $_MAMBOTS->_search_mambot_params['content'];
  34. $botParams = new mosParameters($mambot->params);
  35. $limit = $botParams->def('search_limit', 50);
  36. $nonmenu = $botParams->def('nonmenu', 1);
  37. $nullDate = $database->getNullDate();
  38. $now = _CURRENT_SERVER_TIME;
  39. $text = trim($text);
  40. if ($text == '') {
  41. return array();
  42. }
  43. $wheres = array();
  44. switch ($phrase) {
  45. case 'exact':
  46. $wheres2 = array();
  47. $wheres2[] = "LOWER(a.title) LIKE LOWER('%$text%')";
  48. $wheres2[] = "LOWER(a.introtext) LIKE LOWER('%$text%')";
  49. $wheres2[] = "LOWER(a.fulltext) LIKE LOWER('%$text%')";
  50. $wheres2[] = "LOWER(a.metakey) LIKE LOWER('%$text%')";
  51. $wheres2[] = "LOWER(a.metadesc) LIKE LOWER('%$text%')";
  52. $where = '(' . implode(') OR (', $wheres2) . ')';
  53. break;
  54. case 'all':
  55. case 'any':
  56. default:
  57. $words = explode(' ', $text);
  58. $wheres = array();
  59. foreach ($words as $word) {
  60. $wheres2 = array();
  61. $wheres2[] = "LOWER(a.title) LIKE LOWER('%$word%')";
  62. $wheres2[] = "LOWER(a.introtext) LIKE LOWER('%$word%')";
  63. $wheres2[] = "LOWER(a.fulltext) LIKE LOWER('%$word%')";
  64. $wheres2[] = "LOWER(a.metakey) LIKE LOWER('%$word%')";
  65. $wheres2[] = "LOWER(a.metadesc) LIKE LOWER('%$word%')";
  66. $wheres[] = implode(' OR ', $wheres2);
  67. }
  68. $where = '(' . implode(($phrase == 'all' ? ') AND (' : ') OR ('), $wheres) . ')';
  69. break;
  70. }
  71. $morder = '';
  72. switch ($ordering) {
  73. case 'oldest':
  74. $order = 'a.created ASC';
  75. break;
  76. case 'popular':
  77. $order = 'a.hits DESC';
  78. break;
  79. case 'alpha':
  80. $order = 'a.title ASC';
  81. break;
  82. case 'category':
  83. $order = 'b.title ASC, a.title ASC';
  84. $morder = 'a.title ASC';
  85. break;
  86. case 'newest':
  87. default:
  88. $order = 'a.created DESC';
  89. break;
  90. }
  91. // search content items
  92. $query = "SELECT STRAIGHT_JOIN a.title AS title," . "\n a.created AS created," . "\n CONCAT(a.introtext, a.fulltext) AS text," .
  93. //"\n AND a.catid = ".$category."".
  94. "\n CONCAT_WS( '/', u.title, b.title ) AS section," . "\n CONCAT( 'index.php?option=com_content&task=view&id=', a.id ) AS href," .
  95. "\n '2' AS browsernav," . "\n 'content' AS type" . "\n, u.id AS sec_id, b.id as cat_id" .
  96. "\n FROM #__content AS a" . "\n INNER JOIN #__categories AS b ON b.id=a.catid" . "\n INNER JOIN #__sections AS u ON u.id = a.sectionid" .
  97. "\n WHERE ( $where )" . "\n AND a.state = 1" . "\n AND u.published = 1" . "\n AND b.published = 1" .
  98. "\n AND a.access <= " . (int) $my->gid . "\n AND b.access <= " . (int) $my->gid . "\n AND u.access <= " . (int)
  99. $my->gid . "\n AND ( a.publish_up = " . $database->Quote($nullDate) .
  100. " OR a.publish_up <= " . $database->Quote($now) . " )" . "\n AND ( a.publish_down = " .
  101. $database->Quote($nullDate) . " OR a.publish_down >= " . $database->Quote($now) .
  102. " )" . "\n GROUP BY a.id" . "\n ORDER BY $order";
  103. $database->setQuery($query, 0, $limit);
  104. $list = $database->loadObjectList();
  105. // search static content
  106. $query = "SELECT STRAIGHT_JOIN a.title AS title," . "\n a.created AS created," . "\n a.introtext AS text," .
  107. "\n " . $database->Quote(_STATIC_CONTENT) . " AS section," . "\n CONCAT( 'index.php?option=com_content&task=view&id=', a.id, '&Itemid=', m.id ) AS href," .
  108. "\n '2' AS browsernav," . "\n a.id" . "\n FROM #__content AS a" . "\n LEFT JOIN #__menu AS m ON m.componentid = a.id" .
  109. "\n WHERE ($where)" . "\n AND a.state = 1" . "\n AND a.access <= " . (int) $my->gid . "\n AND m.type = 'content_typed'" .
  110. "\n AND ( a.publish_up = " . $database->Quote($nullDate) . " OR a.publish_up <= " . $database->Quote($now) .
  111. " )" . "\n AND ( a.publish_down = " . $database->Quote($nullDate) .
  112. " OR a.publish_down >= " . $database->Quote($now) . " )" . "\n GROUP BY a.id" . "\n ORDER BY " . ($morder ?
  113. $morder : $order);
  114. $database->setQuery($query, 0, $limit);
  115. $list2 = $database->loadObjectList();
  116. // ïîèñê àðõèâíîãî ñîäåðæèìîãî
  117. $query = "SELECT STRAIGHT_JOIN a.title AS title," . "\n a.created AS created," . "\n a.introtext AS text," .
  118. "\n CONCAT_WS( '/', " . $database->Quote(_SEARCH_ARCHIVED) .
  119. ", u.title, b.title ) AS section," . "\n CONCAT('index.php?option=com_content&task=view&id=',a.id) AS href," .
  120. "\n '2' AS browsernav," . "\n 'content' AS type" . "\n FROM #__content AS a" . "\n INNER JOIN #__categories AS b ON b.id=a.catid" .
  121. "\n INNER JOIN #__sections AS u ON u.id = a.sectionid" . "\n WHERE ( $where )" . "\n AND a.state = -1" .
  122. "\n AND u.published = 1" . "\n AND b.published = 1" . "\n AND a.access <= " . (int) $my->gid .
  123. "\n AND b.access <= " . (int) $my->gid . "\n AND u.access <= " . (int) $my->gid . "\n AND ( a.publish_up = " .
  124. $database->Quote($nullDate) . " OR a.publish_up <= " . $database->Quote($now) . " )" .
  125. "\n AND ( a.publish_down = " . $database->Quote($nullDate) .
  126. " OR a.publish_down >= " . $database->Quote($now) . " )" . "\n ORDER BY $order";
  127. $database->setQuery($query, 0, $limit);
  128. $list3 = $database->loadObjectList();
  129. // check if search of nonmenu linked static content is allowed
  130. if ($nonmenu) {
  131. // collect ids of static content items linked to menu items
  132. // so they can be removed from query that follows
  133. $ids = null;
  134. if (count($list2)) {
  135. foreach ($list2 as $static) {
  136. $ids[] = (int) $static->id;
  137. }
  138. $ids = "a.id != " . implode(" OR a.id != ", $ids);
  139. }
  140. // search static content not connected to a menu
  141. $query = "SELECT STRAIGHT_JOIN a.title AS title," . "\n a.created AS created," . "\n a.introtext AS text," .
  142. "\n '2' as browsernav, " . $database->Quote(_STATIC_CONTENT) . " AS section," . "\n CONCAT( 'index.php?option=com_content&task=view&id=', a.id ) AS href," .
  143. "\n a.id" . "\n FROM #__content AS a" . "\n WHERE ($where)" . (($ids) ? "\n AND ( $ids )" :
  144. '') . "\n AND a.state = 1" . "\n AND a.access <= " . (int) $my->gid . "\n AND a.sectionid = 0" .
  145. "\n AND ( a.publish_up = " . $database->Quote($nullDate) . " OR a.publish_up <= " . $database->Quote($now) .
  146. " )" . "\n AND ( a.publish_down = " . $database->Quote($nullDate) .
  147. " OR a.publish_down >= " . $database->Quote($now) . " )" . "\n ORDER BY " . ($morder ? $morder :
  148. $order);
  149. $database->setQuery($query, 0, $limit);
  150. $list4 = $database->loadObjectList();
  151. } else {
  152. $list4 = array();
  153. }
  154. return array_merge($list, $list2, $list3, (array) $list4);
  155. }
  156. ?>