2021-09-14 13:40:50 +01:00
|
|
|
<?php
|
|
|
|
|
2021-10-10 09:26:18 +01:00
|
|
|
declare(strict_types = 1);
|
|
|
|
|
2021-09-14 13:40:50 +01:00
|
|
|
// {{{ License
|
|
|
|
|
|
|
|
// This file is part of GNU social - https://www.gnu.org/software/social
|
|
|
|
//
|
|
|
|
// GNU social is free software: you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU Affero General Public License as published by
|
|
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
|
|
// (at your option) any later version.
|
|
|
|
//
|
|
|
|
// GNU social is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU Affero General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU Affero General Public License
|
|
|
|
// along with GNU social. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
// }}}
|
|
|
|
|
|
|
|
namespace Component\Tag;
|
|
|
|
|
2021-09-20 16:16:42 +01:00
|
|
|
use App\Core\Cache;
|
2021-09-14 13:40:50 +01:00
|
|
|
use App\Core\DB\DB;
|
|
|
|
use App\Core\Event;
|
|
|
|
use App\Core\Modules\Component;
|
2021-09-20 13:08:17 +01:00
|
|
|
use App\Core\Router\Router;
|
2021-09-20 16:16:42 +01:00
|
|
|
use App\Entity\Note;
|
2021-09-14 13:40:50 +01:00
|
|
|
use App\Entity\NoteTag;
|
2021-09-20 13:08:17 +01:00
|
|
|
use App\Util\Formatting;
|
|
|
|
use App\Util\HTML;
|
2021-09-27 10:39:58 +01:00
|
|
|
use Doctrine\Common\Collections\ExpressionBuilder;
|
|
|
|
use Doctrine\ORM\Query\Expr;
|
|
|
|
use Doctrine\ORM\QueryBuilder;
|
2021-09-14 13:40:50 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Component responsible for extracting tags from posted notes, as well as normalizing them
|
|
|
|
*
|
2021-09-20 13:08:17 +01:00
|
|
|
* @author Hugo Sales <hugo@hsal.es>
|
2021-09-14 13:40:50 +01:00
|
|
|
* @copyright 2021 Free Software Foundation, Inc http://www.fsf.org
|
|
|
|
* @license https://www.gnu.org/licenses/agpl.html GNU AGPL v3 or later
|
|
|
|
*/
|
|
|
|
class Tag extends Component
|
|
|
|
{
|
2021-10-10 09:26:18 +01:00
|
|
|
public const MAX_TAG_LENGTH = 64;
|
|
|
|
public const TAG_REGEX = '/(^|\\s)(#[\\pL\\pN_\\-\\.]{1,64})/u'; // Brion Vibber 2011-02-23 v2:classes/Notice.php:367 function saveTags
|
|
|
|
public const TAG_SLUG_REGEX = '[A-Za-z0-9]{1,64}';
|
2021-09-20 13:08:17 +01:00
|
|
|
|
|
|
|
public function onAddRoute($r): bool
|
|
|
|
{
|
2021-10-10 09:26:18 +01:00
|
|
|
$r->connect('tag', '/tag/{tag<' . self::TAG_SLUG_REGEX . '>}', [Controller\Tag::class, 'tag']);
|
2021-09-20 13:08:17 +01:00
|
|
|
return Event::next;
|
|
|
|
}
|
2021-09-14 13:40:50 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Process note by extracting any tags present
|
|
|
|
*/
|
2021-09-20 16:16:42 +01:00
|
|
|
public function onProcessNoteContent(Note $note, string $content)
|
2021-09-14 13:40:50 +01:00
|
|
|
{
|
|
|
|
$matched_tags = [];
|
|
|
|
$processed_tags = false;
|
2021-10-10 09:26:18 +01:00
|
|
|
preg_match_all(self::TAG_REGEX, $content, $matched_tags, \PREG_SET_ORDER);
|
2021-09-14 13:40:50 +01:00
|
|
|
foreach ($matched_tags as $match) {
|
2021-09-20 16:16:42 +01:00
|
|
|
$tag = $match[2];
|
|
|
|
$canonical_tag = self::canonicalTag($tag);
|
|
|
|
DB::persist(NoteTag::create(['tag' => $tag, 'canonical' => $canonical_tag, 'note_id' => $note->getId()]));
|
|
|
|
Cache::pushList("tag-{$canonical_tag}", $note);
|
2021-09-14 13:40:50 +01:00
|
|
|
$processed_tags = true;
|
|
|
|
}
|
|
|
|
if ($processed_tags) {
|
|
|
|
DB::flush();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-20 13:08:17 +01:00
|
|
|
public function onRenderContent(string &$text)
|
2021-09-14 13:40:50 +01:00
|
|
|
{
|
2021-09-20 13:08:17 +01:00
|
|
|
$text = preg_replace_callback(self::TAG_REGEX, fn ($m) => $m[1] . $this->tagLink($m[2]), $text);
|
|
|
|
}
|
|
|
|
|
|
|
|
private function tagLink(string $tag): string
|
|
|
|
{
|
2021-09-20 16:16:42 +01:00
|
|
|
$canonical = self::canonicalTag($tag);
|
2021-09-20 13:08:17 +01:00
|
|
|
$url = Router::url('tag', ['tag' => $canonical]);
|
|
|
|
return HTML::html(['a' => ['attrs' => ['href' => $url, 'title' => $tag, 'rel' => 'tag'], $tag]], options: ['indent' => false]);
|
|
|
|
}
|
|
|
|
|
2021-09-20 16:16:42 +01:00
|
|
|
public static function canonicalTag(string $tag): string
|
2021-09-20 13:08:17 +01:00
|
|
|
{
|
2021-10-10 09:26:18 +01:00
|
|
|
return mb_substr(Formatting::slugify($tag), 0, self::MAX_TAG_LENGTH);
|
2021-09-14 13:40:50 +01:00
|
|
|
}
|
2021-09-27 10:39:58 +01:00
|
|
|
|
2021-10-10 05:44:10 +01:00
|
|
|
/**
|
|
|
|
* Populate $note_expr with an expression to match a tag, if the term looks like a tag
|
|
|
|
*
|
|
|
|
* $term /^(note|tag|people|actor)/ means we want to match only either a note or an actor
|
|
|
|
*/
|
|
|
|
public function onSearchCreateExpression(ExpressionBuilder $eb, string $term, &$note_expr, &$actor_expr)
|
2021-09-27 10:39:58 +01:00
|
|
|
{
|
2021-10-10 05:44:10 +01:00
|
|
|
$search_term = str_contains($term, ':#') ? explode(':', $term)[1] : $term;
|
|
|
|
$temp_note_expr = $eb->eq('note_tag.tag', $search_term);
|
|
|
|
$temp_actor_expr = $eb->eq('actor_tag.tag', $search_term);
|
|
|
|
if (Formatting::startsWith($term, ['note', 'tag'])) {
|
|
|
|
$note_expr = $temp_note_expr;
|
2021-09-27 10:39:58 +01:00
|
|
|
} else {
|
2021-10-10 05:44:10 +01:00
|
|
|
if (Formatting::startsWith($term, ['people', 'actor'])) {
|
|
|
|
$actor_expr = $temp_actor_expr;
|
|
|
|
} else {
|
|
|
|
$note_expr = $temp_note_expr;
|
|
|
|
$actor_expr = $temp_actor_expr;
|
|
|
|
return Event::next;
|
|
|
|
}
|
2021-09-27 10:39:58 +01:00
|
|
|
}
|
2021-10-10 05:44:10 +01:00
|
|
|
return Event::stop;
|
2021-09-27 10:39:58 +01:00
|
|
|
}
|
|
|
|
|
2021-10-10 05:44:10 +01:00
|
|
|
public function onSeachQueryAddJoins(QueryBuilder &$note_qb, QueryBuilder &$actor_qb)
|
2021-09-27 10:39:58 +01:00
|
|
|
{
|
2021-10-10 05:44:10 +01:00
|
|
|
$note_qb->join('App\Entity\NoteTag', 'note_tag', Expr\Join::WITH, 'note_tag.note_id = note.id');
|
|
|
|
$actor_qb->join('App\Entity\ActorTag', 'actor_tag', Expr\Join::WITH, 'actor_tag.tagger = actor.id');
|
2021-09-27 10:39:58 +01:00
|
|
|
}
|
2021-09-14 13:40:50 +01:00
|
|
|
}
|