Language.php 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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\Language;
  20. use App\Core\Event;
  21. use App\Core\Modules\Component;
  22. use App\Core\Router;
  23. use App\Entity\Actor;
  24. use App\Entity\Note;
  25. use App\Util\Formatting;
  26. use App\Util\Functional as GSF;
  27. use Component\Language\Controller as C;
  28. use Component\Language\Entity\ActorLanguage;
  29. use Doctrine\Common\Collections\ExpressionBuilder;
  30. use Doctrine\ORM\Query\Expr;
  31. use Doctrine\ORM\QueryBuilder;
  32. use EventResult;
  33. use Functional as F;
  34. use Symfony\Component\HttpFoundation\Request;
  35. class Language extends Component
  36. {
  37. public function onAddRoute(Router $r): EventResult
  38. {
  39. $r->connect('settings_sort_languages', '/settings/sort_languages', [C\Language::class, 'sortLanguages']);
  40. return Event::next;
  41. }
  42. /**
  43. * @param Note[] $notes
  44. */
  45. public function onFilterNoteList(?Actor $actor, array &$notes, Request $request): EventResult
  46. {
  47. if (\is_null($actor)) {
  48. return Event::next;
  49. }
  50. $notes = F\select(
  51. $notes,
  52. fn (Note $n) => \is_null($n->getLanguageId()) || \in_array($n->getLanguageId(), ActorLanguage::getActorRelatedLanguagesIds($actor)),
  53. );
  54. return Event::next;
  55. }
  56. /**
  57. * Populate $note_expr or $actor_expr with an expression to match a language
  58. *
  59. * @param mixed $note_expr
  60. * @param mixed $actor_expr
  61. */
  62. public function onCollectionQueryCreateExpression(ExpressionBuilder $eb, string $term, ?string $locale, ?Actor $actor, &$note_expr, &$actor_expr): EventResult
  63. {
  64. $search_term = str_contains($term, ':') ? explode(':', $term)[1] : $term;
  65. $temp_note_expr = null;
  66. $temp_note_actor_expr = null;
  67. $temp_actor_expr = null;
  68. if (str_contains($search_term, ',')) {
  69. foreach ([
  70. ['note_language.locale', &$temp_note_expr],
  71. ['note_actor_language.locale', &$temp_note_actor_expr],
  72. ['language.locale', &$temp_note_actor_expr],
  73. ] as $tmp) {
  74. [$column, &$var] = $tmp;
  75. $exprs = [];
  76. foreach (explode(',', $search_term) as $s) {
  77. $exprs[] = $eb->startsWith($column, $s);
  78. }
  79. $var = $eb->orX(...$exprs);
  80. }
  81. } else {
  82. $temp_note_expr = $eb->startsWith('note_language.locale', $search_term);
  83. $temp_note_actor_expr = $eb->startsWith('note_actor_language.locale', $search_term);
  84. $temp_actor_expr = $eb->startsWith('language.locale', $search_term);
  85. }
  86. if (Formatting::startsWith($term, ['lang:', 'language:'])) {
  87. $note_expr = $temp_note_expr;
  88. $actor_expr = $temp_actor_expr;
  89. return Event::stop;
  90. } elseif (Formatting::startsWith($term, GSF::cartesianProduct([['note', 'post'], ['lang', 'language'], [':']], separator: ['-', '_']))) {
  91. $note_expr = $temp_note_expr;
  92. return Event::stop;
  93. } elseif (Formatting::startsWith($term, GSF::cartesianProduct([['note', 'post'], ['author', 'actor', 'people', 'person'], ['lang', 'language'], [':']], separator: ['-', '_']))) {
  94. $note_expr = $temp_note_actor_expr;
  95. return Event::stop;
  96. } elseif (Formatting::startsWith($term, GSF::cartesianProduct([['actor', 'people', 'person'], ['lang', 'language'], [':']], separator: ['-', '_']))) {
  97. $actor_expr = $temp_actor_expr;
  98. return Event::stop;
  99. }
  100. return Event::next;
  101. }
  102. public function onCollectionQueryAddJoins(QueryBuilder &$note_qb, QueryBuilder &$actor_qb): EventResult
  103. {
  104. $note_aliases = $note_qb->getAllAliases();
  105. if (!\in_array('note_language', $note_aliases)) {
  106. $note_qb->leftJoin('Component\Language\Entity\Language', 'note_language', Expr\Join::WITH, 'note.language_id = note_language.id');
  107. }
  108. if (!\in_array('actor_language', $note_aliases)) {
  109. $note_qb->leftJoin('Component\Language\Entity\ActorLanguage', 'actor_language', Expr\Join::WITH, 'note.actor_id = actor_language.actor_id');
  110. }
  111. if (!\in_array('note_actor_language', $note_aliases)) {
  112. $note_qb->leftJoin('Component\Language\Entity\Language', 'note_actor_language', Expr\Join::WITH, 'note_actor_language.id = actor_language.language_id');
  113. }
  114. $actor_aliases = $actor_qb->getAllAliases();
  115. if (!\in_array('actor_language', $actor_aliases)) {
  116. $actor_qb->leftJoin('Component\Language\Entity\ActorLanguage', 'actor_language', Expr\Join::WITH, 'actor.id = actor_language.actor_id');
  117. }
  118. if (!\in_array('language', $actor_aliases)) {
  119. $actor_qb->leftJoin('Component\Language\Entity\Language', 'language', Expr\Join::WITH, 'actor_language.language_id = language.id');
  120. }
  121. return Event::next;
  122. }
  123. }