1
0
Fork 0

Merge pull request #10482 from annando/converturiid

Some more "convertForUriId" replacements
This commit is contained in:
Hypolite Petovan 2021-07-10 17:45:34 -04:00 committed by GitHub
commit f6df07ec86
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
18 changed files with 97 additions and 147 deletions

View file

@ -1,6 +1,6 @@
-- ------------------------------------------
-- Friendica 2021.09-dev (Siberian Iris)
-- DB_UPDATE_VERSION 1426
-- DB_UPDATE_VERSION 1427
-- ------------------------------------------
@ -2291,6 +2291,7 @@ CREATE VIEW `owner-view` AS SELECT
`contact`.`dfrn-id` AS `dfrn-id`,
`contact`.`url` AS `url`,
`contact`.`nurl` AS `nurl`,
`contact`.`uri-id` AS `uri-id`,
`contact`.`addr` AS `addr`,
`contact`.`alias` AS `alias`,
`contact`.`pubkey` AS `pubkey`,

View file

@ -2525,12 +2525,12 @@ function api_format_messages($item, $recipient, $sender)
if (!empty($_GET['getText'])) {
$ret['title'] = $item['title'];
if ($_GET['getText'] == 'html') {
$ret['text'] = BBCode::convert($item['body'], false);
$ret['text'] = BBCode::convertForUriId($item['uri-id'], $item['body'], BBCode::API);
} elseif ($_GET['getText'] == 'plain') {
$ret['text'] = trim(HTML::toPlaintext(BBCode::convert(api_clean_plain_items($item['body']), false, BBCode::API, true), 0));
$ret['text'] = trim(HTML::toPlaintext(BBCode::convertForUriId($item['uri-id'], api_clean_plain_items($item['body']), BBCode::API), 0));
}
} else {
$ret['text'] = $item['title'] . "\n" . HTML::toPlaintext(BBCode::convert(api_clean_plain_items($item['body']), false, BBCode::API, true), 0);
$ret['text'] = $item['title'] . "\n" . HTML::toPlaintext(BBCode::convertForUriId($item['uri-id'], api_clean_plain_items($item['body']), BBCode::API), 0);
}
if (!empty($_GET['getUserObjects']) && $_GET['getUserObjects'] == 'false') {
unset($ret['sender']);
@ -2557,7 +2557,7 @@ function api_convert_item($item)
$attachments = api_get_attachments($body, $item['uri-id']);
// Workaround for ostatus messages where the title is identically to the body
$html = BBCode::convert(api_clean_plain_items($body), false, BBCode::API, true);
$html = BBCode::convertForUriId($item['uri-id'], api_clean_plain_items($body), BBCode::API);
$statusbody = trim(HTML::toPlaintext($html, 0));
// handle data: images
@ -2575,7 +2575,7 @@ function api_convert_item($item)
$statustext = mb_substr($statustext, 0, 1000) . "... \n" . ($item['plink'] ?? '');
}
$statushtml = BBCode::convert(BBCode::removeAttachment($body), false, BBCode::API, true);
$statushtml = BBCode::convertForUriId($item['uri-id'], BBCode::removeAttachment($body), BBCode::API);
// Workaround for clients with limited HTML parser functionality
$search = ["<br>", "<blockquote>", "</blockquote>",
@ -2589,7 +2589,7 @@ function api_convert_item($item)
$statushtml = str_replace($search, $replace, $statushtml);
if ($item['title'] != "") {
$statushtml = "<br><h4>" . BBCode::convert($item['title']) . "</h4><br>" . $statushtml;
$statushtml = "<br><h4>" . BBCode::convertForUriId($item['uri-id'], $item['title']) . "</h4><br>" . $statushtml;
}
do {
@ -2607,7 +2607,7 @@ function api_convert_item($item)
// feeds without body should contain the link
if ((($item['network'] ?? Protocol::PHANTOM) == Protocol::FEED) && (strlen($item['body']) == 0)) {
$statushtml .= BBCode::convert($item['plink']);
$statushtml .= BBCode::convertForUriId($item['uri-id'], $item['plink']);
}
return [
@ -3054,7 +3054,7 @@ function api_format_item($item, $type = "json", $status_user = null, $author_use
'external_url' => DI::baseUrl() . "/display/" . $item['guid'],
'friendica_activities' => api_format_items_activities($item, $type),
'friendica_title' => $item['title'],
'friendica_html' => BBCode::convert($item['body'], false)
'friendica_html' => BBCode::convertForUriId($item['uri-id'], $item['body'], BBCode::EXTERNAL)
];
if (count($converted["attachments"]) > 0) {

View file

@ -47,91 +47,6 @@ use Friendica\Util\Strings;
use Friendica\Util\Temporal;
use Friendica\Util\XML;
function item_extract_images($body) {
$saved_image = [];
$orig_body = $body;
$new_body = '';
$cnt = 0;
$img_start = strpos($orig_body, '[img');
$img_st_close = ($img_start !== false ? strpos(substr($orig_body, $img_start), ']') : false);
$img_end = ($img_start !== false ? strpos(substr($orig_body, $img_start), '[/img]') : false);
while (($img_st_close !== false) && ($img_end !== false)) {
$img_st_close++; // make it point to AFTER the closing bracket
$img_end += $img_start;
if (!strcmp(substr($orig_body, $img_start + $img_st_close, 5), 'data:')) {
// This is an embedded image
$saved_image[$cnt] = substr($orig_body, $img_start + $img_st_close, $img_end - ($img_start + $img_st_close));
$new_body = $new_body . substr($orig_body, 0, $img_start) . '[!#saved_image' . $cnt . '#!]';
$cnt++;
} else {
$new_body = $new_body . substr($orig_body, 0, $img_end + strlen('[/img]'));
}
$orig_body = substr($orig_body, $img_end + strlen('[/img]'));
if ($orig_body === false) {
// in case the body ends on a closing image tag
$orig_body = '';
}
$img_start = strpos($orig_body, '[img');
$img_st_close = ($img_start !== false ? strpos(substr($orig_body, $img_start), ']') : false);
$img_end = ($img_start !== false ? strpos(substr($orig_body, $img_start), '[/img]') : false);
}
$new_body = $new_body . $orig_body;
return ['body' => $new_body, 'images' => $saved_image];
}
function item_redir_and_replace_images($body, $images, $cid) {
$origbody = $body;
$newbody = '';
$cnt = 1;
$pos = BBCode::getTagPosition($origbody, 'url', 0);
while ($pos !== false && $cnt < 1000) {
$search = '/\[url\=(.*?)\]\[!#saved_image([0-9]*)#!\]\[\/url\]' . '/is';
$replace = '[url=' . DI::baseUrl() . '/redir/' . $cid
. '?url=' . '$1' . '][!#saved_image' . '$2' .'#!][/url]';
$newbody .= substr($origbody, 0, $pos['start']['open']);
$subject = substr($origbody, $pos['start']['open'], $pos['end']['close'] - $pos['start']['open']);
$origbody = substr($origbody, $pos['end']['close']);
if ($origbody === false) {
$origbody = '';
}
$subject = preg_replace($search, $replace, $subject);
$newbody .= $subject;
$cnt++;
// Isn't this supposed to use $cnt value for $occurrences? - @MrPetovan
$pos = BBCode::getTagPosition($origbody, 'url', 0);
}
$newbody .= $origbody;
$cnt = 0;
foreach ($images as $image) {
/*
* We're depending on the property of 'foreach' (specified on the PHP website) that
* it loops over the array starting from the first element and going sequentially
* to the last element.
*/
$newbody = str_replace('[!#saved_image' . $cnt . '#!]', '[img]' . $image . '[/img]', $newbody);
$cnt++;
}
return $newbody;
}
/**
* Render actions localized
*
@ -141,11 +56,6 @@ function item_redir_and_replace_images($body, $images, $cid) {
*/
function localize_item(&$item)
{
$extracted = item_extract_images($item['body']);
if ($extracted['images']) {
$item['body'] = item_redir_and_replace_images($extracted['body'], $extracted['images'], $item['contact-id']);
}
/// @todo The following functionality needs to be cleaned up.
if (!empty($item['verb'])) {
$activity = DI::activity();
@ -260,13 +170,6 @@ function localize_item(&$item)
}
}
// add zrl's to public images
$photo_pattern = "/\[url=(.*?)\/photos\/(.*?)\/image\/(.*?)\]\[img(.*?)\]h(.*?)\[\/img\]\[\/url\]/is";
if (preg_match($photo_pattern, $item['body'])) {
$photo_replace = '[url=' . Profile::zrl('$1' . '/photos/' . '$2' . '/image/' . '$3' , true) . '][img' . '$4' . ']h' . '$5' . '[/img][/url]';
$item['body'] = BBCode::pregReplaceInTag($photo_pattern, $photo_replace, 'url', $item['body']);
}
// add sparkle links to appropriate permalinks
// Only create a redirection to a magic link when logged in
if (!empty($item['plink']) && Session::isAuthenticated()) {

View file

@ -474,7 +474,7 @@ function notification($params)
if ($show_in_notification_page) {
$fields = [
'name' => $params['source_name'] ?? '',
'name_cache' => substr(strip_tags(BBCode::convert($params['source_name'])), 0, 255),
'name_cache' => substr(strip_tags(BBCode::convertForUriId($uri_id, $params['source_name'])), 0, 255),
'url' => $params['source_link'] ?? '',
'photo' => $params['source_photo'] ?? '',
'link' => $itemlink ?? '',

View file

@ -314,14 +314,9 @@ function message_content(App $a)
$sparkle = ' sparkle';
}
$extracted = item_extract_images($message['body']);
if ($extracted['images']) {
$message['body'] = item_redir_and_replace_images($extracted['body'], $extracted['images'], $message['contact-id']);
}
$from_name_e = $message['from-name'];
$subject_e = $message['title'];
$body_e = BBCode::convert($message['body']);
$body_e = BBCode::convertForUriId($message['uri-id'], $message['body']);
$to_name_e = $message['name'];
$contact = Contact::getByURL($message['from-url'], false, ['thumb', 'addr', 'id', 'avatar']);

View file

@ -100,6 +100,9 @@ class PostUpdate
if (!self::update1426()) {
return false;
}
if (!self::update1427()) {
return false;
}
return true;
}
@ -961,4 +964,51 @@ class PostUpdate
return false;
}
/**
* update the "uri-id" field in the event table
*
* @return bool "true" when the job is done
* @throws \Friendica\Network\HTTPException\InternalServerErrorException
* @throws \ImagickException
*/
private static function update1427()
{
// Was the script completed?
if (DI::config()->get("system", "post_update_version") >= 1427) {
return true;
}
$condition = ["`uri-id` IS NULL"];
Logger::info('Start', ['rest' => DBA::count('event', $condition)]);
$rows = 0;
$events = DBA::select('event', ['id', 'uri', 'guid'], $condition, ['limit' => 1000]);
if (DBA::errorNo() != 0) {
Logger::error('Database error', ['no' => DBA::errorNo(), 'message' => DBA::errorMessage()]);
return false;
}
while ($event = DBA::fetch($events)) {
if (!empty($event['guid'])) {
$uriid = ItemURI::insert(['uri' => $event['uri'], 'guid' => $event['guid']]);
} else {
$uriid = ItemURI::getIdByURI($event['uri']);
}
DBA::update('event', ['uri-id' => $uriid], ['id' => $event['id']]);
++$rows;
}
DBA::close($events);
Logger::info('Processed', ['rows' => $rows]);
if ($rows <= 100) {
DI::config()->set("system", "post_update_version", 1427);
Logger::info('Done');
return true;
}
return false;
}
}

View file

@ -589,10 +589,10 @@ class Event
$last_date = '';
$fmt = DI::l10n()->t('l, F j');
foreach ($event_result as $event) {
$item = Post::selectFirst(['plink', 'author-name', 'author-avatar', 'author-link', 'private'], ['id' => $event['itemid']]);
$item = Post::selectFirst(['plink', 'author-name', 'author-avatar', 'author-link', 'private', 'uri-id'], ['id' => $event['itemid']]);
if (!DBA::isResult($item)) {
// Using default values when no item had been found
$item = ['plink' => '', 'author-name' => '', 'author-avatar' => '', 'author-link' => '', 'private' => Item::PUBLIC];
$item = ['plink' => '', 'author-name' => '', 'author-avatar' => '', 'author-link' => '', 'private' => Item::PUBLIC, 'uri-id' => ($event['uri-id'] ?? 0)];
}
$event = array_merge($event, $item);

View file

@ -486,11 +486,11 @@ class Profile
}
if (isset($p['about'])) {
$p['about'] = BBCode::convert($p['about']);
$p['about'] = BBCode::convertForUriId($profile['uri-id'] ?? 0, $p['about']);
}
if (isset($p['address'])) {
$p['address'] = BBCode::convert($p['address']);
$p['address'] = BBCode::convertForUriId($profile['uri-id'] ?? 0, $p['address']);
}
$p['photo'] = Contact::getAvatarUrlForId($cid, ProxyUtils::SIZE_SMALL);
@ -670,13 +670,13 @@ class Profile
$istoday = true;
}
$title = strip_tags(html_entity_decode(BBCode::convert($rr['summary']), ENT_QUOTES, 'UTF-8'));
$title = strip_tags(html_entity_decode(BBCode::convertForUriId($rr['uri-id'], $rr['summary']), ENT_QUOTES, 'UTF-8'));
if (strlen($title) > 35) {
$title = substr($title, 0, 32) . '... ';
}
$description = substr(strip_tags(BBCode::convert($rr['desc'])), 0, 32) . '... ';
$description = substr(strip_tags(BBCode::convertForUriId($rr['uri-id'], $rr['desc'])), 0, 32) . '... ';
if (!$description) {
$description = DI::l10n()->t('[No description]');
}

View file

@ -55,7 +55,7 @@ class Index extends BaseApi
'cid' => $event['cid'],
'uri' => $event['uri'],
'name' => $event['summary'],
'desc' => BBCode::convert($event['desc']),
'desc' => BBCode::convertForUriId($event['uri-id'], $event['desc']),
'startTime' => $event['start'],
'endTime' => $event['finish'],
'type' => $event['type'],

View file

@ -169,7 +169,7 @@ class Profile extends BaseProfile
}
if ($a->profile['about']) {
$basic_fields += self::buildField('about', DI::l10n()->t('Description:'), BBCode::convert($a->profile['about']));
$basic_fields += self::buildField('about', DI::l10n()->t('Description:'), BBCode::convertForUriId($a->profile['uri-id'], $a->profile['about']));
}
if ($a->profile['xmpp']) {
@ -218,7 +218,7 @@ class Profile extends BaseProfile
$custom_fields += self::buildField(
'custom_' . $profile_field->order,
$profile_field->label,
BBCode::convert($profile_field->value),
BBCode::convertForUriId($a->profile['uri-id'], $profile_field->value),
'aprofile custom'
);
};

View file

@ -1345,8 +1345,7 @@ class Receiver
// Some AP software allow formatted text in post location, so we run all the text converters we have to boil
// down to HTML and then finally format to plaintext.
$location = Markdown::convert($location);
$location = BBCode::convert($location);
$location = HTML::toPlaintext($location);
$location = BBCode::toPlaintext($location);
}
$object_data['sc:identifier'] = JsonLD::fetchElement($object, 'sc:identifier', '@value');

View file

@ -345,7 +345,7 @@ class Transmitter
}
if (!empty($owner['about'])) {
$data['summary'] = BBCode::convert($owner['about'], false);
$data['summary'] = BBCode::convertForUriId($owner['uri-id'], $owner['about'], BBCode::EXTERNAL);
}
$data['url'] = $owner['url'];

View file

@ -765,12 +765,13 @@ class DFRN
* @param DOMDocument $doc XML document
* @param string $element Element name for the activity
* @param string $activity activity value
* @param int $uriid Uri-Id of the post
*
* @return \DOMElement XML activity object
* @throws \Friendica\Network\HTTPException\InternalServerErrorException
* @todo Find proper type-hints
*/
private static function createActivity(DOMDocument $doc, $element, $activity)
private static function createActivity(DOMDocument $doc, $element, $activity, $uriid)
{
if ($activity) {
$entry = $doc->createElement($element);
@ -817,7 +818,7 @@ class DFRN
}
}
if ($r->content) {
XML::addElement($doc, $entry, "content", BBCode::convert($r->content), ["type" => "html"]);
XML::addElement($doc, $entry, "content", BBCode::convertForUriId($uriid, $r->content, BBCode::EXTERNAL), ["type" => "html"]);
}
return $entry;
@ -918,7 +919,7 @@ class DFRN
$htmlbody = "[b]" . $item['title'] . "[/b]\n\n" . $htmlbody;
}
$htmlbody = BBCode::convertForUriId($item['uri-id'], $htmlbody, BBCode::OSTATUS);
$htmlbody = BBCode::convertForUriId($item['uri-id'], $htmlbody, BBCode::ACTIVITYPUB);
}
$author = self::addEntryAuthor($doc, "author", $item["author-link"], $item);
@ -1033,12 +1034,12 @@ class DFRN
XML::addElement($doc, $entry, "activity:object-type", Activity\ObjectType::COMMENT);
}
$actobj = self::createActivity($doc, "activity:object", $item['object']);
$actobj = self::createActivity($doc, "activity:object", $item['object'], $item['uri-id']);
if ($actobj) {
$entry->appendChild($actobj);
}
$actarg = self::createActivity($doc, "activity:target", $item['target']);
$actarg = self::createActivity($doc, "activity:target", $item['target'], $item['uri-id']);
if ($actarg) {
$entry->appendChild($actarg);
}

View file

@ -1109,7 +1109,7 @@ class Feed
$body = OStatus::formatPicturePost($item['body'], $item['uri-id']);
$body = BBCode::convertForUriId($item['uri-id'], $body, BBCode::OSTATUS, false);
$body = BBCode::convertForUriId($item['uri-id'], $body, BBCode::ACTIVITYPUB);
XML::addElement($doc, $entry, "content", $body, ["type" => "html"]);
@ -1186,7 +1186,7 @@ class Feed
private static function getTitle(array $item)
{
if ($item['title'] != '') {
return BBCode::convertForUriId($item['uri-id'], $item['title'], BBCode::OSTATUS);
return BBCode::convertForUriId($item['uri-id'], $item['title'], BBCode::ACTIVITYPUB);
}
// Fetch information about the post

View file

@ -1418,7 +1418,7 @@ class OStatus
XML::addElement($doc, $author, "name", $owner["nick"]);
XML::addElement($doc, $author, "email", $owner["addr"]);
if ($show_profile) {
XML::addElement($doc, $author, "summary", BBCode::convert($owner["about"], false, BBCode::OSTATUS));
XML::addElement($doc, $author, "summary", BBCode::convertForUriId($owner['uri-id'], $owner["about"], BBCode::OSTATUS));
}
$attributes = ["rel" => "alternate", "type" => "text/html", "href" => $owner["url"]];
@ -1445,7 +1445,7 @@ class OStatus
XML::addElement($doc, $author, "poco:preferredUsername", $owner["nick"]);
XML::addElement($doc, $author, "poco:displayName", $owner["name"]);
if ($show_profile) {
XML::addElement($doc, $author, "poco:note", BBCode::convert($owner["about"], false, BBCode::OSTATUS));
XML::addElement($doc, $author, "poco:note", BBCode::convertForUriId($owner['uri-id'], $owner["about"], BBCode::OSTATUS));
if (trim($owner["location"]) != "") {
$element = $doc->createElement("poco:address");

View file

@ -55,7 +55,7 @@
use Friendica\Database\DBA;
if (!defined('DB_UPDATE_VERSION')) {
define('DB_UPDATE_VERSION', 1426);
define('DB_UPDATE_VERSION', 1427);
}
return [

View file

@ -729,6 +729,7 @@
"dfrn-id" => ["contact", "dfrn-id"],
"url" => ["contact", "url"],
"nurl" => ["contact", "nurl"],
"uri-id" => ["contact", "uri-id"],
"addr" => ["contact", "addr"],
"alias" => ["contact", "alias"],
"pubkey" => ["contact", "pubkey"],

View file

@ -2192,9 +2192,9 @@ class ApiTest extends FixtureTest
public function testApiFormatMessages()
{
$result = api_format_messages(
['id' => 1, 'title' => 'item_title', 'body' => '[b]item_body[/b]'],
['id' => 2, 'screen_name' => 'recipient_name'],
['id' => 3, 'screen_name' => 'sender_name']
['id' => 1, 'uri-id' => 1, 'title' => 'item_title', 'body' => '[b]item_body[/b]'],
['id' => 2, 'uri-id' => 2, 'screen_name' => 'recipient_name'],
['id' => 3, 'uri-id' => 2, 'screen_name' => 'sender_name']
);
self::assertEquals('item_title' . "\n" . 'item_body', $result['text']);
self::assertEquals(1, $result['id']);
@ -2213,9 +2213,9 @@ class ApiTest extends FixtureTest
{
$_GET['getText'] = 'html';
$result = api_format_messages(
['id' => 1, 'title' => 'item_title', 'body' => '[b]item_body[/b]'],
['id' => 2, 'screen_name' => 'recipient_name'],
['id' => 3, 'screen_name' => 'sender_name']
['id' => 1, 'uri-id' => 1, 'title' => 'item_title', 'body' => '[b]item_body[/b]'],
['id' => 2, 'uri-id' => 2, 'screen_name' => 'recipient_name'],
['id' => 3, 'uri-id' => 3, 'screen_name' => 'sender_name']
);
self::assertEquals('item_title', $result['title']);
self::assertEquals('<strong>item_body</strong>', $result['text']);
@ -2230,9 +2230,9 @@ class ApiTest extends FixtureTest
{
$_GET['getText'] = 'plain';
$result = api_format_messages(
['id' => 1, 'title' => 'item_title', 'body' => '[b]item_body[/b]'],
['id' => 2, 'screen_name' => 'recipient_name'],
['id' => 3, 'screen_name' => 'sender_name']
['id' => 1, 'uri-id' => 1, 'title' => 'item_title', 'body' => '[b]item_body[/b]'],
['id' => 2, 'uri-id' => 2, 'screen_name' => 'recipient_name'],
['id' => 3, 'uri-id' => 3, 'screen_name' => 'sender_name']
);
self::assertEquals('item_title', $result['title']);
self::assertEquals('item_body', $result['text']);
@ -2247,9 +2247,9 @@ class ApiTest extends FixtureTest
{
$_GET['getUserObjects'] = 'false';
$result = api_format_messages(
['id' => 1, 'title' => 'item_title', 'body' => '[b]item_body[/b]'],
['id' => 2, 'screen_name' => 'recipient_name'],
['id' => 3, 'screen_name' => 'sender_name']
['id' => 1, 'uri-id' => 1, 'title' => 'item_title', 'body' => '[b]item_body[/b]'],
['id' => 2, 'uri-id' => 2, 'screen_name' => 'recipient_name'],
['id' => 3, 'uri-id' => 3, 'screen_name' => 'sender_name']
);
self::assertTrue(!isset($result['sender']));
self::assertTrue(!isset($result['recipient']));