PageRenderTime 30ms CodeModel.GetById 1ms RepoModel.GetById 0ms app.codeStats 1ms

/titania/search.php

http://github.com/phpbb/customisation-db
PHP | 334 lines | 209 code | 59 blank | 66 comment | 39 complexity | ddce3a49b4fdd2e944300bf3e7304304 MD5 | raw file
Possible License(s): AGPL-1.0
  1. <?php
  2. /**
  3. *
  4. * @package Titania
  5. * @copyright (c) 2008 phpBB Customisation Database Team
  6. * @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License, version 2
  7. *
  8. */
  9. /**
  10. * @ignore
  11. */
  12. define('IN_TITANIA', true);
  13. if (!defined('TITANIA_ROOT')) define('TITANIA_ROOT', './');
  14. if (!defined('PHP_EXT')) define('PHP_EXT', substr(strrchr(__FILE__, '.'), 1));
  15. require TITANIA_ROOT . 'common.' . PHP_EXT;
  16. phpbb::$user->add_lang('search');
  17. titania::add_lang('search');
  18. // Available Search Types
  19. $search_types = array(
  20. TITANIA_CONTRIB => 'CONTRIBUTION_NAME_DESCRIPTION',
  21. TITANIA_FAQ => 'CONTRIB_FAQ',
  22. );
  23. if (titania::$config->support_in_titania)
  24. {
  25. $search_types = array_merge($search_types, array(TITANIA_SUPPORT => 'CONTRIB_SUPPORT'));
  26. }
  27. $mode = request_var('mode', '');
  28. $keywords = utf8_normalize_nfc(request_var('keywords', '', true));
  29. $user_id = request_var('u', 0);
  30. $contrib_id = request_var('contrib', 0);
  31. $search_fields = request_var('sf', '');
  32. $search_type = request_var('type', 0);
  33. $categories = request_var('c', array(0));
  34. $search_subcategories = request_var('sc', 0);
  35. $phpbb_versions = request_var('versions', array(''));
  36. // Display the advanced search page
  37. if (!$keywords && !$user_id && !$contrib_id && !isset($_POST['submit']))
  38. {
  39. if ($mode == 'find-contribution')
  40. {
  41. titania::_include('functions_posting', 'generate_category_select');
  42. titania::add_lang('contributions');
  43. phpbb::$template->assign_vars(array(
  44. 'S_SEARCH_ACTION' => titania_url::build_url('find-contribution'),
  45. ));
  46. // Display the list of phpBB versions available
  47. foreach (titania::$cache->get_phpbb_versions() as $version => $name)
  48. {
  49. $template->assign_block_vars('phpbb_versions', array(
  50. 'VERSION' => $name,
  51. ));
  52. }
  53. generate_category_select($categories, false, false);
  54. titania::page_header('SEARCH');
  55. titania::page_footer(true, 'find_contribution.html');
  56. }
  57. // Output search types
  58. foreach ($search_types as $value => $name)
  59. {
  60. phpbb::$template->assign_block_vars('types', array(
  61. 'NAME' => (isset(phpbb::$user->lang[$name])) ? phpbb::$user->lang[$name] : $name,
  62. 'VALUE' => $value,
  63. ));
  64. }
  65. phpbb::$template->assign_vars(array(
  66. 'S_SEARCH_ACTION' => titania_url::build_url('search'),
  67. ));
  68. titania::page_header('SEARCH');
  69. titania::page_footer(true, 'search_body.html');
  70. }
  71. // Add some POST stuff to the url
  72. if (isset($_POST['submit']))
  73. {
  74. $author = utf8_normalize_nfc(request_var('author', '', true));
  75. if ($author)
  76. {
  77. $sql = 'SELECT user_id FROM ' . USERS_TABLE . '
  78. WHERE username_clean = \'' . phpbb::$db->sql_escape(utf8_clean_string($author)) . '\'';
  79. phpbb::$db->sql_query($sql);
  80. $user_id = phpbb::$db->sql_fetchfield('user_id');
  81. phpbb::$db->sql_freeresult();
  82. if (!$user_id)
  83. {
  84. trigger_error('NO_USER');
  85. }
  86. titania_url::$params['u'] = $user_id;
  87. }
  88. $url_params = array(
  89. 'keywords' => $keywords,
  90. 'sf' => $search_fields,
  91. 'type' => $search_type,
  92. 'c' => $categories,
  93. 'sc' => $search_subcategories,
  94. 'versions' => $phpbb_versions,
  95. );
  96. foreach ($url_params as $name => $value)
  97. {
  98. if ($value)
  99. {
  100. titania_url::$params[$name] = $value;
  101. }
  102. }
  103. // Redirect if sent through POST so the parameters are in the URL (for easy copying/pasting to other users)
  104. redirect(titania_url::build_url(titania_url::$current_page, titania_url::$params));
  105. }
  106. // Setup the sort tool
  107. $sort = new titania_sort();
  108. $sort->set_defaults(phpbb::$config['posts_per_page']);
  109. $sort->request();
  110. // Setup the search tool and make sure it is working
  111. titania_search::initialize();
  112. if (titania_search::$do_not_index)
  113. {
  114. // Solr service is down
  115. trigger_error('SEARCH_UNAVAILABLE');
  116. }
  117. // Initialize the query
  118. $query = titania_search::create_find_query();
  119. // Query fields
  120. $query_fields = array();
  121. switch ($search_fields)
  122. {
  123. case 'titleonly' :
  124. $query_fields[] = 'title';
  125. break;
  126. case 'msgonly' :
  127. $query_fields[] = 'text';
  128. break;
  129. default:
  130. $query_fields[] = 'title';
  131. $query_fields[] = 'text';
  132. break;
  133. }
  134. // Keywords specified?
  135. if ($keywords)
  136. {
  137. titania_search::clean_keywords($keywords);
  138. $qb = new ezcSearchQueryBuilder();
  139. $qb->parseSearchQuery($query, $keywords, $query_fields);
  140. unset($qb);
  141. }
  142. // Author specified?
  143. if ($user_id)
  144. {
  145. $query->where($query->eq('author', $user_id));
  146. }
  147. // Contrib specified?
  148. if ($contrib_id)
  149. {
  150. $query->where($query->eq('parent_id', $contrib_id));
  151. }
  152. // Find contribution
  153. if ($mode == 'find-contribution')
  154. {
  155. if (sizeof($categories) == 1 && $categories[0] == 0)
  156. {
  157. // All
  158. $categories = array();
  159. }
  160. if (sizeof($categories) || sizeof($phpbb_versions))
  161. {
  162. // Prevent an error
  163. $contribs = array(0);
  164. // Grab the children
  165. if ($search_subcategories)
  166. {
  167. foreach ($categories as $category_id)
  168. {
  169. $categories = array_merge($categories, array_keys(titania::$cache->get_category_children($category_id)));
  170. }
  171. }
  172. // Build-a-query
  173. $prefix = ((sizeof($categories)) ? 'c' : 'v');
  174. $sql = 'SELECT DISTINCT(' . $prefix . '.contrib_id) FROM
  175. ' .((sizeof($categories)) ? TITANIA_CONTRIB_IN_CATEGORIES_TABLE . ' c' : '') . '
  176. ' .((sizeof($categories) && (sizeof($phpbb_versions))) ? ', ' : '') . '
  177. ' .((sizeof($phpbb_versions)) ? TITANIA_REVISIONS_PHPBB_TABLE . ' v' : '') . '
  178. WHERE
  179. ' . ((sizeof($categories)) ? phpbb::$db->sql_in_set('c.category_id', array_unique(array_map('intval', $categories))) : ''). '
  180. ' . ((sizeof($categories) && sizeof($phpbb_versions)) ? ' AND c.contrib_id = v.contrib_id AND ' : '');
  181. if (sizeof($phpbb_versions))
  182. {
  183. $or_sql = '';
  184. foreach ($phpbb_versions as $version)
  185. {
  186. // Skip invalid versions
  187. if (strlen($version) < 5 || $version[1] != '.' || $version[3] != '.')
  188. {
  189. continue;
  190. }
  191. // Add OR if not empty
  192. $or_sql .= (($or_sql) ? ' OR ' : '');
  193. $or_sql .= '(v.phpbb_version_branch = ' . (int) $version[0] . (int) $version[2] . ' AND v.phpbb_version_revision = \'' . phpbb::$db->sql_escape(substr($version, 4)) . '\')';
  194. }
  195. $sql .= ' (' . $or_sql . ') ';
  196. }
  197. $sql .= 'GROUP BY ' . $prefix . '.contrib_id';
  198. $result = phpbb::$db->sql_query($sql);
  199. while ($row = phpbb::$db->sql_fetchrow($result))
  200. {
  201. $contribs[] = $row['contrib_id'];
  202. }
  203. phpbb::$db->sql_freeresult($result);
  204. // Search in the set
  205. titania_search::in_set($query, 'id', $contribs);
  206. }
  207. $query->where($query->eq('type', TITANIA_CONTRIB));
  208. }
  209. else
  210. {
  211. // Search type
  212. if ($search_type)
  213. {
  214. $query->where($query->eq('type', $search_type));
  215. }
  216. }
  217. // Do the search
  218. $results = titania_search::custom_search($query, $sort);
  219. // Grab the users
  220. users_overlord::load_users($results['user_ids']);
  221. /*switch ($display)
  222. {
  223. case 'topics' :
  224. foreach ($results['documents'] as $document)
  225. {
  226. $url_base = $document->url;
  227. $url_params = '';
  228. if (substr($url_base, -1) != '/')
  229. {
  230. $url_params = substr($url_base, (strrpos($url_base, '/') + 1));
  231. $url_base = substr($url_base, 0, (strrpos($url_base, '/') + 1));
  232. }
  233. phpbb::$template->assign_block_vars('searchresults', array(
  234. 'TOPIC_TITLE' => censor_text($document->title),
  235. 'TOPIC_AUTHOR_FULL' => users_overlord::get_user($document->author, '_full'),
  236. 'FIRST_POST_TIME' => phpbb::$user->format_date($document->date),
  237. 'U_VIEW_TOPIC' => titania_url::build_url($url_base, $url_params),
  238. 'S_TOPIC_REPORTED' => ($document->reported) ? true : false,
  239. //'S_TOPIC_UNAPPROVED' => (!$document->approved) ? true : false,
  240. ));
  241. }
  242. break;
  243. default : */
  244. foreach ($results['documents'] as $document)
  245. {
  246. $url_base = $url_params = '';
  247. titania_url::split_base_params($url_base, $url_params, $document->url);
  248. phpbb::$template->assign_block_vars('searchresults', array(
  249. 'POST_SUBJECT' => censor_text($document->title),
  250. 'MESSAGE' => titania_generate_text_for_display($document->text, $document->text_uid, $document->text_bitfield, $document->text_options),
  251. 'POST_AUTHOR_FULL' => ($document->author) ? users_overlord::get_user($document->author, '_full') : false,
  252. 'POST_DATE' => ($document->date) ? phpbb::$user->format_date($document->date) : false,
  253. 'U_VIEW_POST' => titania_url::build_url($url_base, $url_params),
  254. 'S_POST_REPORTED' => ($document->reported) ? true : false,
  255. ));
  256. }
  257. /* break;
  258. }*/
  259. $sort->build_pagination(titania_url::$current_page, titania_url::$params);
  260. titania::page_header('SEARCH');
  261. phpbb::$template->assign_vars(array(
  262. 'SEARCH_WORDS' => $keywords,
  263. 'SEARCH_MATCHES' => ($sort->total == 1) ? sprintf(phpbb::$user->lang['FOUND_SEARCH_MATCH'], $sort->total) : sprintf(phpbb::$user->lang['FOUND_SEARCH_MATCHES'], $sort->total),
  264. 'U_SEARCH_WORDS' => titania_url::build_url(titania_url::$current_page, titania_url::$params),
  265. 'U_SEARCH' => titania_url::build_url((($mode == 'find-contribution') ? 'find-contribution' : 'search')),
  266. 'S_IN_SEARCH' => true,
  267. // 'S_SHOW_TOPICS' => ($display == 'topics') ? true : false,
  268. 'S_SEARCH_ACTION' => titania_url::$current_page_url,
  269. ));
  270. titania::page_footer(true, 'search_results.html');