. // }}} namespace Component\Tag; use App\Core\DB\DB; use App\Core\Event; use App\Core\Modules\Component; use App\Entity\NoteTag; /** * Component responsible for extracting tags from posted notes, as well as normalizing them * * @author Hugo Sales * @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 { const TAG_REGEX = '/(?:^|\s)#([\pL\pN_\-\.]{1,64})/u'; // Brion Vibber 2011-02-23 v2:classes/Notice.php:367 function saveTags /** * Process note by extracting any tags present */ public function onProcessNoteContent(int $note_id, string $content) { $matched_tags = []; $processed_tags = false; preg_match_all(self::TAG_REGEX, $content, $matched_tags, PREG_SET_ORDER); foreach ($matched_tags as $match) { DB::persist($tag = NoteTag::create(['tag' => $match[0], 'note_id' => $note_id])); $processed_tags = true; } if ($processed_tags) { DB::flush(); } } public function onAddRoute($r): bool { $r->connect('tag', '/tag/{tag' . self::TAG_REGEX . '}' , [Controller\Tag::class, 'tag']); return Event::next; } }