Move handle_tag() to Content\Item::replaceTag

This commit is contained in:
Hypolite Petovan 2020-06-10 10:13:32 -04:00
parent be41095936
commit e7f50a05d1
2 changed files with 124 additions and 121 deletions

View file

@ -29,6 +29,7 @@
*/
use Friendica\App;
use Friendica\Content\Item as ItemHelper;
use Friendica\Content\Text\BBCode;
use Friendica\Core\Hook;
use Friendica\Core\Logger;
@ -395,7 +396,7 @@ function item_post(App $a) {
}
}
$success = handle_tag($body, $inform, local_user() ? local_user() : $profile_uid, $tag, $network);
$success = ItemHelper::replaceTag($body, $inform, local_user() ? local_user() : $profile_uid, $tag, $network);
if ($success['replaced']) {
$tagged[] = $tag;
}
@ -868,123 +869,3 @@ function item_content(App $a)
return $o;
}
/**
* This function removes the tag $tag from the text $body and replaces it with
* the appropriate link.
*
* @param App $a
* @param string $body the text to replace the tag in
* @param string $inform a comma-seperated string containing everybody to inform
* @param integer $profile_uid
* @param string $tag the tag to replace
* @param string $network The network of the post
*
* @return array|bool ['replaced' => $replaced, 'contact' => $contact];
* @throws ImagickException
* @throws HTTPException\InternalServerErrorException
*/
function handle_tag(&$body, &$inform, $profile_uid, $tag, $network = "")
{
$replaced = false;
//is it a person tag?
if (Tag::isType($tag, Tag::MENTION, Tag::IMPLICIT_MENTION, Tag::EXCLUSIVE_MENTION)) {
$tag_type = substr($tag, 0, 1);
//is it already replaced?
if (strpos($tag, '[url=')) {
// Checking for the alias that is used for OStatus
$pattern = "/[@!]\[url\=(.*?)\](.*?)\[\/url\]/ism";
if (preg_match($pattern, $tag, $matches)) {
$data = Contact::getDetailsByURL($matches[1]);
if ($data["alias"] != "") {
$newtag = '@[url=' . $data["alias"] . ']' . $data["nick"] . '[/url]';
}
}
return $replaced;
}
//get the person's name
$name = substr($tag, 1);
// Sometimes the tag detection doesn't seem to work right
// This is some workaround
$nameparts = explode(" ", $name);
$name = $nameparts[0];
// Try to detect the contact in various ways
if (strpos($name, 'http://')) {
// At first we have to ensure that the contact exists
Contact::getIdForURL($name);
// Now we should have something
$contact = Contact::getDetailsByURL($name);
} elseif (strpos($name, '@')) {
// This function automatically probes when no entry was found
$contact = Contact::getDetailsByAddr($name);
} else {
$contact = false;
$fields = ['id', 'url', 'nick', 'name', 'alias', 'network', 'forum', 'prv'];
if (strrpos($name, '+')) {
// Is it in format @nick+number?
$tagcid = intval(substr($name, strrpos($name, '+') + 1));
$contact = DBA::selectFirst('contact', $fields, ['id' => $tagcid, 'uid' => $profile_uid]);
}
// select someone by nick or attag in the current network
if (!DBA::isResult($contact) && ($network != "")) {
$condition = ["(`nick` = ? OR `attag` = ?) AND `network` = ? AND `uid` = ?",
$name, $name, $network, $profile_uid];
$contact = DBA::selectFirst('contact', $fields, $condition);
}
//select someone by name in the current network
if (!DBA::isResult($contact) && ($network != "")) {
$condition = ['name' => $name, 'network' => $network, 'uid' => $profile_uid];
$contact = DBA::selectFirst('contact', $fields, $condition);
}
// select someone by nick or attag in any network
if (!DBA::isResult($contact)) {
$condition = ["(`nick` = ? OR `attag` = ?) AND `uid` = ?", $name, $name, $profile_uid];
$contact = DBA::selectFirst('contact', $fields, $condition);
}
// select someone by name in any network
if (!DBA::isResult($contact)) {
$condition = ['name' => $name, 'uid' => $profile_uid];
$contact = DBA::selectFirst('contact', $fields, $condition);
}
}
// Check if $contact has been successfully loaded
if (DBA::isResult($contact)) {
if (strlen($inform) && (isset($contact["notify"]) || isset($contact["id"]))) {
$inform .= ',';
}
if (isset($contact["id"])) {
$inform .= 'cid:' . $contact["id"];
} elseif (isset($contact["notify"])) {
$inform .= $contact["notify"];
}
$profile = $contact["url"];
$newname = ($contact["name"] ?? '') ?: $contact["nick"];
}
//if there is an url for this persons profile
if (isset($profile) && ($newname != "")) {
$replaced = true;
// create profile link
$profile = str_replace(',', '%2c', $profile);
$newtag = $tag_type.'[url=' . $profile . ']' . $newname . '[/url]';
$body = str_replace($tag_type . $name, $newtag, $body);
}
}
return ['replaced' => $replaced, 'contact' => $contact];
}

View file

@ -21,7 +21,10 @@
namespace Friendica\Content;
use Friendica\Database\DBA;
use Friendica\Model\Contact;
use Friendica\Model\FileTag;
use Friendica\Model\Tag;
/**
* A content helper class for displaying items
@ -100,4 +103,123 @@ class Item
return [$categories, $folders];
}
/**
* This function removes the tag $tag from the text $body and replaces it with
* the appropriate link.
*
* @param string $body the text to replace the tag in
* @param string $inform a comma-seperated string containing everybody to inform
* @param integer $profile_uid the user id to replace the tag for (0 = anyone)
* @param string $tag the tag to replace
* @param string $network The network of the post
*
* @return array|bool ['replaced' => $replaced, 'contact' => $contact];
* @throws \Friendica\Network\HTTPException\InternalServerErrorException
* @throws \ImagickException
*/
public static function replaceTag(&$body, &$inform, $profile_uid, $tag, $network = '')
{
$replaced = false;
//is it a person tag?
if (Tag::isType($tag, Tag::MENTION, Tag::IMPLICIT_MENTION, Tag::EXCLUSIVE_MENTION)) {
$tag_type = substr($tag, 0, 1);
//is it already replaced?
if (strpos($tag, '[url=')) {
// Checking for the alias that is used for OStatus
$pattern = '/[@!]\[url\=(.*?)\](.*?)\[\/url\]/ism';
if (preg_match($pattern, $tag, $matches)) {
$data = Contact::getDetailsByURL($matches[1]);
if ($data['alias'] != '') {
$newtag = '@[url=' . $data['alias'] . ']' . $data['nick'] . '[/url]';
}
}
return $replaced;
}
//get the person's name
$name = substr($tag, 1);
// Sometimes the tag detection doesn't seem to work right
// This is some workaround
$nameparts = explode(' ', $name);
$name = $nameparts[0];
// Try to detect the contact in various ways
if (strpos($name, 'http://')) {
// At first we have to ensure that the contact exists
Contact::getIdForURL($name);
// Now we should have something
$contact = Contact::getDetailsByURL($name, $profile_uid);
} elseif (strpos($name, '@')) {
// This function automatically probes when no entry was found
$contact = Contact::getDetailsByAddr($name, $profile_uid);
} else {
$contact = false;
$fields = ['id', 'url', 'nick', 'name', 'alias', 'network', 'forum', 'prv'];
if (strrpos($name, '+')) {
// Is it in format @nick+number?
$tagcid = intval(substr($name, strrpos($name, '+') + 1));
$contact = DBA::selectFirst('contact', $fields, ['id' => $tagcid, 'uid' => $profile_uid]);
}
// select someone by nick or attag in the current network
if (!DBA::isResult($contact) && ($network != '')) {
$condition = ["(`nick` = ? OR `attag` = ?) AND `network` = ? AND `uid` = ?",
$name, $name, $network, $profile_uid];
$contact = DBA::selectFirst('contact', $fields, $condition);
}
//select someone by name in the current network
if (!DBA::isResult($contact) && ($network != '')) {
$condition = ['name' => $name, 'network' => $network, 'uid' => $profile_uid];
$contact = DBA::selectFirst('contact', $fields, $condition);
}
// select someone by nick or attag in any network
if (!DBA::isResult($contact)) {
$condition = ["(`nick` = ? OR `attag` = ?) AND `uid` = ?", $name, $name, $profile_uid];
$contact = DBA::selectFirst('contact', $fields, $condition);
}
// select someone by name in any network
if (!DBA::isResult($contact)) {
$condition = ['name' => $name, 'uid' => $profile_uid];
$contact = DBA::selectFirst('contact', $fields, $condition);
}
}
// Check if $contact has been successfully loaded
if (DBA::isResult($contact)) {
if (strlen($inform) && (isset($contact['notify']) || isset($contact['id']))) {
$inform .= ',';
}
if (isset($contact['id'])) {
$inform .= 'cid:' . $contact['id'];
} elseif (isset($contact['notify'])) {
$inform .= $contact['notify'];
}
$profile = $contact['url'];
$newname = ($contact['name'] ?? '') ?: $contact['nick'];
}
//if there is an url for this persons profile
if (isset($profile) && ($newname != '')) {
$replaced = true;
// create profile link
$profile = str_replace(',', '%2c', $profile);
$newtag = $tag_type.'[url=' . $profile . ']' . $newname . '[/url]';
$body = str_replace($tag_type . $name, $newtag, $body);
}
}
return ['replaced' => $replaced, 'contact' => $contact];
}
}