-
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
e002613
commit 6870e57
Showing
14 changed files
with
266 additions
and
5 deletions.
There are no files selected for viewing
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
<?php | ||
|
||
namespace App\migrations; | ||
|
||
use App\models; | ||
use App\services; | ||
|
||
class Migration202409200001AddTagsToLinks | ||
{ | ||
public function migrate(): bool | ||
{ | ||
$database = \Minz\Database::get(); | ||
|
||
$database->exec(<<<'SQL' | ||
ALTER TABLE links | ||
ADD COLUMN tags JSON NOT NULL DEFAULT '[]'; | ||
SQL); | ||
|
||
$database = \Minz\Database::get(); | ||
$statement = $database->query(<<<'SQL' | ||
SELECT l.* FROM links l, messages m | ||
WHERE l.id = m.link_id | ||
AND m.content LIKE '%#%' | ||
SQL); | ||
|
||
$links = models\Link::fromDatabaseRows($statement->fetchAll()); | ||
|
||
foreach ($links as $link) { | ||
services\LinkTags::refresh($link); | ||
} | ||
|
||
return true; | ||
} | ||
|
||
public function rollback(): bool | ||
{ | ||
$database = \Minz\Database::get(); | ||
|
||
$database->exec(<<<'SQL' | ||
ALTER TABLE links | ||
DROP COLUMN tags; | ||
SQL); | ||
|
||
return true; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
<?php | ||
|
||
namespace App\services; | ||
|
||
use App\models; | ||
|
||
/** | ||
* @author Marien Fressinaud <dev@marienfressinaud.fr> | ||
* @license http://www.gnu.org/licenses/agpl-3.0.en.html AGPL | ||
*/ | ||
class LinkTags | ||
{ | ||
public const TAG_REGEX = '/(?:^|[^\pL\pN_])#(?P<tag>[\pL\pN_]+)/u'; | ||
|
||
public static function refresh(models\Link $link): void | ||
{ | ||
$messages = $link->messages(); | ||
|
||
$tags = []; | ||
|
||
foreach ($messages as $message) { | ||
$message_tags = self::extractTags($message->content); | ||
$tags = array_merge($tags, $message_tags); | ||
} | ||
|
||
$tags = array_unique($tags); | ||
|
||
$link->tags = $tags; | ||
|
||
$link->save(); | ||
} | ||
|
||
/** | ||
* @return string[] | ||
*/ | ||
public static function extractTags(string $content): array | ||
{ | ||
$result = preg_match_all(self::TAG_REGEX, $content, $matches); | ||
|
||
if ($result === false) { | ||
return []; | ||
} | ||
|
||
return $matches['tag']; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.