Language.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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\RouteLoader;
  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 Functional as F;
  33. use Symfony\Component\HttpFoundation\Request;
  34. class Language extends Component
  35. {
  36. public function onAddRoute(RouteLoader $r): bool
  37. {
  38. $r->connect('settings_sort_languages', '/settings/sort_languages', [C\Language::class, 'sortLanguages']);
  39. return Event::next;
  40. }
  41. public function onFilterNoteList(?Actor $actor, array &$notes, Request $request): bool
  42. {
  43. if (\is_null($actor)) {
  44. return Event::next;
  45. }
  46. $notes = F\select(
  47. $notes,
  48. fn (Note $n) => \is_null($n->getLanguageId()) || \in_array($n->getLanguageId(), ActorLanguage::getActorRelatedLanguagesIds($actor)),
  49. );
  50. return Event::next;
  51. }
  52. /**
  53. * Populate $note_expr or $actor_expr with an expression to match a language
  54. */
  55. public function onCollectionQueryCreateExpression(ExpressionBuilder $eb, string $term, ?string $locale, ?Actor $actor, &$note_expr, &$actor_expr): bool
  56. {
  57. $search_term = str_contains($term, ':') ? explode(':', $term)[1] : $term;
  58. $temp_note_expr = null;
  59. $temp_note_actor_expr = null;
  60. $temp_actor_expr = null;
  61. if (str_contains($search_term, ',')) {
  62. foreach ([
  63. ['note_language.locale', &$temp_note_expr],
  64. ['note_actor_language.locale', &$temp_note_actor_expr],
  65. ['language.locale', &$temp_note_actor_expr],
  66. ] as $tmp) {
  67. [$column, &$var] = $tmp;
  68. $exprs = [];
  69. foreach (explode(',', $search_term) as $s) {
  70. $exprs[] = $eb->startsWith($column, $s);
  71. }
  72. $var = $eb->orX(...$exprs);
  73. }
  74. } else {
  75. $temp_note_expr = $eb->startsWith('note_language.locale', $search_term);
  76. $temp_note_actor_expr = $eb->startsWith('note_actor_language.locale', $search_term);
  77. $temp_actor_expr = $eb->startsWith('language.locale', $search_term);
  78. }
  79. if (Formatting::startsWith($term, ['lang:', 'language:'])) {
  80. $note_expr = $temp_note_expr;
  81. $actor_expr = $temp_actor_expr;
  82. return Event::stop;
  83. } elseif (Formatting::startsWith($term, GSF::cartesianProduct([['note', 'post'], ['lang', 'language'], [':']], separator: ['-', '_']))) {
  84. $note_expr = $temp_note_expr;
  85. return Event::stop;
  86. } elseif (Formatting::startsWith($term, GSF::cartesianProduct([['note', 'post'], ['author', 'actor', 'people', 'person'], ['lang', 'language'], [':']], separator: ['-', '_']))) {
  87. $note_expr = $temp_note_actor_expr;
  88. return Event::stop;
  89. } elseif (Formatting::startsWith($term, GSF::cartesianProduct([['actor', 'people', 'person'], ['lang', 'language'], [':']], separator: ['-', '_']))) {
  90. $actor_expr = $temp_actor_expr;
  91. return Event::stop;
  92. }
  93. return Event::next;
  94. }
  95. public function onCollectionQueryAddJoins(QueryBuilder &$note_qb, QueryBuilder &$actor_qb): bool
  96. {
  97. $note_qb->leftJoin('Component\Language\Entity\Language', 'note_language', Expr\Join::WITH, 'note.language_id = note_language.id')
  98. ->leftJoin('Component\Language\Entity\ActorLanguage', 'actor_language', Expr\Join::WITH, 'note.actor_id = actor_language.actor_id')
  99. ->leftJoin('Component\Language\Entity\Language', 'note_actor_language', Expr\Join::WITH, 'note_actor_language.id = actor_language.language_id');
  100. $actor_qb->leftJoin('Component\Language\Entity\ActorLanguage', 'actor_language', Expr\Join::WITH, 'actor.id = actor_language.actor_id')
  101. ->leftJoin('Component\Language\Entity\Language', 'language', Expr\Join::WITH, 'actor_language.language_id = language.id');
  102. return Event::next;
  103. }
  104. }