Search.php 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. <?php
  2. declare(strict_types = 1);
  3. // {{{ License
  4. // This file is part of GNU social - https://www.gnu.org/software/social
  5. //
  6. // GNU social is free software: you can redistribute it and/or modify
  7. // it under the terms of the GNU Affero General Public License as published by
  8. // the Free Software Foundation, either version 3 of the License, or
  9. // (at your option) any later version.
  10. //
  11. // GNU social is distributed in the hope that it will be useful,
  12. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. // GNU Affero General Public License for more details.
  15. //
  16. // You should have received a copy of the GNU Affero General Public License
  17. // along with GNU social. If not, see <http://www.gnu.org/licenses/>.
  18. // }}}
  19. namespace Component\Search\Controller;
  20. use App\Core\Form;
  21. use function App\Core\I18n\_m;
  22. use App\Util\Common;
  23. use App\Util\Exception\BugFoundException;
  24. use App\Util\Exception\RedirectException;
  25. use App\Util\Form\FormFields;
  26. use App\Util\Formatting;
  27. use Component\Feed\Feed;
  28. use Component\Feed\Util\FeedController;
  29. use Component\Search as Comp;
  30. use Symfony\Component\Form\Extension\Core\Type\CheckboxType;
  31. use Symfony\Component\Form\Extension\Core\Type\SubmitType;
  32. use Symfony\Component\Form\Extension\Core\Type\TextType;
  33. use Symfony\Component\HttpFoundation\Request;
  34. class Search extends FeedController
  35. {
  36. /**
  37. * Handle a search query
  38. */
  39. public function handle(Request $request)
  40. {
  41. $actor = Common::actor();
  42. $language = !\is_null($actor) ? $actor->getTopLanguage()->getLocale() : null;
  43. $q = $this->string('q');
  44. $data = Feed::query(query: $q, page: $this->int('p'), language: $language);
  45. $notes = $data['notes'];
  46. $actors = $data['actors'];
  47. $search_builder_form = Form::create([
  48. ['include_actors', CheckboxType::class, ['required' => false, 'data' => false, 'label' => _m('Include people/actors')]],
  49. ['include_actors_groups', CheckboxType::class, ['required' => false, 'data' => false, 'label' => _m('Include groups')]],
  50. ['include_actors_lists', CheckboxType::class, ['required' => false, 'data' => false, 'label' => _m('Include people lists')]],
  51. ['include_actors_people', CheckboxType::class, ['required' => false, 'data' => false, 'label' => _m('Include people')]],
  52. ['include_actors_businesses', CheckboxType::class, ['required' => false, 'data' => false, 'label' => _m('Include businesses')]],
  53. ['include_actors_organizations', CheckboxType::class, ['required' => false, 'data' => false, 'label' => _m('Include organizations')]],
  54. ['include_actors_bots', CheckboxType::class, ['required' => false, 'data' => false, 'label' => _m('Include bots')]],
  55. ['include_notes', CheckboxType::class, ['required' => false, 'data' => true, 'label' => _m('Include notes')]],
  56. ['include_notes_text', CheckboxType::class, ['required' => false, 'data' => true, 'label' => _m('Include text notes')]],
  57. ['include_notes_media', CheckboxType::class, ['required' => false, 'data' => true, 'label' => _m('Include media notes')]],
  58. ['include_notes_polls', CheckboxType::class, ['required' => false, 'data' => true, 'label' => _m('Include polls')]],
  59. ['include_notes_bookmarks', CheckboxType::class, ['required' => false, 'data' => true, 'label' => _m('Include bookmarks')]],
  60. /* note_langs */ FormFields::language($actor, context_actor: null, label: _m('Search for notes in these languages'), multiple: true, required: false, use_short_display: false, form_id: 'note_langs', use_no_selection: true),
  61. ['note_tags', TextType::class, ['required' => false, 'label' => _m('Include only notes with all the following tags')]],
  62. /* note_actor_langs */ FormFields::language($actor, context_actor: null, label: _m('Search for notes by people who know these languages'), multiple: true, required: false, use_short_display: false, form_id: 'note_actor_langs', use_no_selection: true),
  63. ['note_actor_tags', TextType::class, ['required' => false, 'label' => _m('Include only notes by people with all the following tags')]],
  64. /* actor_langs */ FormFields::language($actor, context_actor: null, label: _m('Search for people that know these languages'), multiple: true, required: false, use_short_display: false, form_id: 'actor_langs', use_no_selection: true),
  65. ['actor_tags', TextType::class, ['required' => false, 'label' => _m('Include only people with all the following tags')]],
  66. [$form_name = 'search_builder', SubmitType::class, ['label' => _m('Search')]],
  67. ]);
  68. if ('POST' === $request->getMethod() && $request->request->has($form_name)) {
  69. $search_builder_form->handleRequest($request);
  70. if ($search_builder_form->isSubmitted() && $search_builder_form->isValid()) {
  71. $data = $search_builder_form->getData();
  72. $query = [];
  73. $include_notes_query = [];
  74. $include_actors_query = [];
  75. $exception = new BugFoundException('Search builder form seems to have new fields the code did not expect');
  76. foreach ($data as $key => $value) {
  77. if (!\is_null($value) && !empty($value)) {
  78. if (str_contains($key, 'tags')) {
  79. $query[] = "{$key}:#{$value}";
  80. } elseif (str_contains($key, 'lang')) {
  81. if (!\in_array('null', $value)) {
  82. $langs = implode(',', $value);
  83. $query[] = "{$key}:{$langs}";
  84. }
  85. } elseif (str_contains($key, 'include')) {
  86. if (str_contains($key, 'notes')) {
  87. if ($key === 'include_notes') {
  88. if (!$data[$key]) {
  89. $include_notes_query = null;
  90. }
  91. } elseif ($data[$key] && !\is_null($include_notes_query)) {
  92. $include_notes_query[] = Formatting::removePrefix($key, 'include_notes_');
  93. }
  94. } elseif (str_contains($key, 'actors')) {
  95. if ($key === 'include_actors') {
  96. if (!$data[$key]) {
  97. $include_actors_query = null;
  98. }
  99. } elseif ($data[$key] && !\is_null($include_actors_query)) {
  100. $include_actors_query[] = Formatting::removePrefix($key, 'include_actors_');
  101. }
  102. } else {
  103. throw $exception;
  104. }
  105. } else {
  106. throw $exception;
  107. }
  108. }
  109. }
  110. if (!\is_null($include_notes_query) && !empty($include_notes_query)) {
  111. $query[] = 'note-types:' . implode(',', $include_notes_query);
  112. }
  113. if (!\is_null($include_actors_query) && !empty($include_actors_query)) {
  114. $query[] = 'actor-types:' . implode(',', $include_actors_query);
  115. }
  116. $query = implode(' ', $query);
  117. throw new RedirectException('search', ['q' => $query]);
  118. }
  119. }
  120. return [
  121. '_template' => 'search/show.html.twig',
  122. 'actor' => $actor,
  123. 'search_form' => Comp\Search::searchForm($request, query: $q, add_subscribe: !\is_null($actor)),
  124. 'search_builder_form' => $search_builder_form->createView(),
  125. 'notes' => $notes ?? [],
  126. 'actors' => $actors ?? [],
  127. 'page' => 1, // TODO paginate
  128. ];
  129. }
  130. }