Several speed improvements (magiclink, caching, indexes)

This commit is contained in:
Michael 2021-02-17 18:59:19 +00:00
parent 251465f67a
commit 312c01a517
21 changed files with 119 additions and 86 deletions

View file

@ -1,6 +1,6 @@
-- ------------------------------------------
-- Friendica 2021.03-dev (Red Hot Poker)
-- DB_UPDATE_VERSION 1401
-- DB_UPDATE_VERSION 1402
-- ------------------------------------------
@ -1231,11 +1231,11 @@ CREATE TABLE IF NOT EXISTS `post-user` (
INDEX `uid_hidden` (`uid`,`hidden`),
INDEX `event-id` (`event-id`),
INDEX `uid_wall` (`uid`,`wall`),
INDEX `parent-uri-id` (`parent-uri-id`),
INDEX `parent-uri-id_uid` (`parent-uri-id`,`uid`),
INDEX `thr-parent-id` (`thr-parent-id`),
INDEX `external-id` (`external-id`),
INDEX `owner-id` (`owner-id`),
INDEX `author-id` (`author-id`),
INDEX `author-id_uid` (`author-id`,`uid`),
INDEX `causer-id` (`causer-id`),
INDEX `vid` (`vid`),
INDEX `uid_received` (`uid`,`received`),
@ -1615,6 +1615,7 @@ CREATE VIEW `post-view` AS SELECT
`post-user`.`unseen` AS `unseen`,
`post-user`.`deleted` AS `deleted`,
`post-user`.`origin` AS `origin`,
`post-thread-user`.`origin` AS `parent-origin`,
`post-thread-user`.`forum_mode` AS `forum_mode`,
`post-thread-user`.`mention` AS `mention`,
`post-user`.`global` AS `global`,

View file

@ -160,9 +160,6 @@ function localize_item(&$item)
return;
}
$Aname = $item['author-name'];
$Alink = $item['author-link'];
$obj = XML::parseString($xmlhead . $item['object']);
$Bname = $obj->title;
@ -177,9 +174,17 @@ function localize_item(&$item)
}
}
$A = '[url=' . Contact::magicLink($Alink) . ']' . $Aname . '[/url]';
$B = '[url=' . Contact::magicLink($Blink) . ']' . $Bname . '[/url]';
if ($Bphoto != "") {
$author = ['uid' => 0, 'id' => $item['author-id'],
'network' => $item['author-network'], 'url' => $item['author-link']];
$A = '[url=' . Contact::magicLinkByContact($author) . ']' . $item['author-name'] . '[/url]';
if (!empty($Blink)) {
$B = '[url=' . Contact::magicLink($Blink) . ']' . $Bname . '[/url]';
} else {
$B = '';
}
if ($Bphoto != "" && !empty($Blink)) {
$Bphoto = '[url=' . Contact::magicLink($Blink) . '][img=80x80]' . $Bphoto . '[/img][/url]';
}
@ -262,11 +267,10 @@ function localize_item(&$item)
}
// add sparkle links to appropriate permalinks
$author = ['uid' => 0, 'id' => $item['author-id'],
'network' => $item['author-network'], 'url' => $item['author-link']];
// Only create a redirection to a magic link when logged in
if (!empty($item['plink']) && Session::isAuthenticated()) {
$author = ['uid' => 0, 'id' => $item['author-id'],
'network' => $item['author-network'], 'url' => $item['author-link']];
$item['plink'] = Contact::magicLinkByContact($author, $item['plink']);
}
}
@ -763,7 +767,9 @@ function conversation_fetch_comments($thread_items, bool $pinned, array $activit
}
if (($row['gravity'] == GRAVITY_PARENT) && !empty($row['causer-id'])) {
$row['reshared'] = DI::l10n()->t('%s reshared this.', '<a href="'. htmlentities(Contact::magicLinkById($row['causer-id'])) .'">' . htmlentities($name) . '</a>');
$causer = ['uid' => 0, 'id' => $row['causer-id'],
'network' => $row['causer-network'], 'url' => $row['causer-link']];
$row['reshared'] = DI::l10n()->t('%s reshared this.', '<a href="'. htmlentities(Contact::magicLinkByContact($causer)) .'">' . htmlentities($name) . '</a>');
}
$row['direction'] = ['direction' => 3, 'title' => (empty($row['causer-id']) ? DI::l10n()->t('Reshared') : DI::l10n()->t('Reshared by %s', $name))];
break;
@ -903,7 +909,7 @@ function item_photo_menu($item) {
$sparkle = (strpos($profile_link, 'redir/') === 0);
$cid = 0;
$pcid = Contact::getIdForURL($item['author-link'], 0, false);
$pcid = $item['author-id'];
$network = '';
$rel = 0;
$condition = ['uid' => local_user(), 'nurl' => Strings::normaliseLink($item['author-link'])];

View file

@ -1454,7 +1454,9 @@ function photos_content(App $a)
continue;
}
$profile_url = Contact::magicLinkById($item['author-id']);
$author = ['uid' => 0, 'id' => $item['author-id'],
'network' => $item['author-network'], 'url' => $item['author-link']];
$profile_url = Contact::magicLinkByContact($author);
if (strpos($profile_url, 'redir/') === 0) {
$sparkle = ' sparkle';
} else {

View file

@ -59,7 +59,7 @@ function unfollow_content(App $a)
local_user(), Contact::SHARING, Contact::FRIEND, Strings::normaliseLink($url),
Strings::normaliseLink($url), $url];
$contact = DBA::selectFirst('contact', ['url', 'network', 'addr', 'name'], $condition);
$contact = DBA::selectFirst('contact', ['url', 'id', 'uid', 'network', 'addr', 'name'], $condition);
if (!DBA::isResult($contact)) {
notice(DI::l10n()->t("You aren't following this contact."));
@ -99,7 +99,7 @@ function unfollow_content(App $a)
'$submit' => DI::l10n()->t('Submit Request'),
'$cancel' => DI::l10n()->t('Cancel'),
'$url' => $contact['url'],
'$zrl' => Contact::magicLink($contact['url']),
'$zrl' => Contact::magicLinkByContact($contact),
'$url_label' => DI::l10n()->t('Profile URL'),
'$myaddr' => $self['url'],
'$request' => $request,

View file

@ -33,6 +33,9 @@ use Friendica\Util\Strings;
*/
class ContactSelector
{
static $serverdata = [];
static $server_url = [];
/**
* @param string $current current
* @param boolean $disabled optional, default false
@ -61,6 +64,21 @@ class ContactSelector
return $o;
}
private static function getServerForProfile(string $profile)
{
$server_url = self::getServerURLForProfile($profile);
if (!empty(self::$serverdata[$server_url])) {
return self::$serverdata[$server_url];
}
// Now query the GServer for the platform name
$gserver = DBA::selectFirst('gserver', ['platform', 'network'], ['nurl' => $server_url]);
self::$serverdata[$server_url] = $gserver;
return $gserver;
}
/**
* @param string $profile Profile URL
* @return string Server URL
@ -68,6 +86,10 @@ class ContactSelector
*/
private static function getServerURLForProfile($profile)
{
if (!empty(self::$server_url[$profile])) {
return self::$server_url[$profile];
}
$server_url = '';
// Fetch the server url from the contact table
@ -83,6 +105,8 @@ class ContactSelector
$server_url = Strings::normaliseLink(Network::unparseURL($parts));
}
self::$server_url[$profile] = $server_url;
return $server_url;
}
@ -123,24 +147,19 @@ class ContactSelector
$networkname = str_replace($search, $replace, $network);
if ((in_array($network, Protocol::FEDERATED)) && ($profile != "")) {
$server_url = self::getServerURLForProfile($profile);
$gserver = self::getServerForProfile($profile);
// Now query the GServer for the platform name
$gserver = DBA::selectFirst('gserver', ['platform', 'network'], ['nurl' => $server_url]);
if (!empty($gserver['platform'])) {
$platform = $gserver['platform'];
} elseif (!empty($gserver['network']) && ($gserver['network'] != Protocol::ACTIVITYPUB)) {
$platform = self::networkToName($gserver['network']);
}
if (DBA::isResult($gserver)) {
if (!empty($gserver['platform'])) {
$platform = $gserver['platform'];
} elseif (!empty($gserver['network']) && ($gserver['network'] != Protocol::ACTIVITYPUB)) {
$platform = self::networkToName($gserver['network']);
}
if (!empty($platform)) {
$networkname = $platform;
if (!empty($platform)) {
$networkname = $platform;
if ($network == Protocol::ACTIVITYPUB) {
$networkname .= ' (AP)';
}
if ($network == Protocol::ACTIVITYPUB) {
$networkname .= ' (AP)';
}
}
}
@ -192,12 +211,8 @@ class ContactSelector
$network_icon = str_replace($search, $replace, $network);
if ((in_array($network, Protocol::FEDERATED)) && ($profile != "")) {
$server_url = self::getServerURLForProfile($profile);
// Now query the GServer for the platform name
$gserver = DBA::selectFirst('gserver', ['platform'], ['nurl' => $server_url]);
if (DBA::isResult($gserver) && !empty($gserver['platform'])) {
$gserver = self::getServerForProfile($profile);
if (!empty($gserver['platform'])) {
$network_icon = $platform_icons[strtolower($gserver['platform'])] ?? $network_icon;
}
}

View file

@ -71,7 +71,7 @@ class ForumManager
$forumlist = [];
$fields = ['id', 'url', 'name', 'micro', 'thumb', 'avatar'];
$fields = ['id', 'url', 'name', 'micro', 'thumb', 'avatar', 'network', 'uid'];
$condition = [$condition_str, Protocol::DFRN, Protocol::ACTIVITYPUB, $uid];
$contacts = DBA::select('contact', $fields, $condition, $params);
if (!$contacts) {
@ -127,7 +127,7 @@ class ForumManager
$entry = [
'url' => $baseurl . '/' . $contact['id'],
'external_url' => Contact::magicLink($contact['url']),
'external_url' => Contact::magicLinkByContact($contact),
'name' => $contact['name'],
'cid' => $contact['id'],
'selected' => $selected,

View file

@ -837,7 +837,7 @@ class HTML
$redir = false;
if ($redirect) {
$url = Contact::magicLink($contact['url']);
$url = Contact::magicLinkByContact($contact);
if (strpos($url, 'redir/') === 0) {
$sparkle = ' sparkle';
}

View file

@ -22,6 +22,7 @@
namespace Friendica\Content;
use Friendica\Core\Addon;
use Friendica\Core\Cache\Duration;
use Friendica\Core\Protocol;
use Friendica\Core\Renderer;
use Friendica\Database\DBA;
@ -266,7 +267,7 @@ class Widget
$extra_sql = self::unavailableNetworks();
$r = DBA::p("SELECT DISTINCT(`network`) FROM `contact` WHERE `uid` = ? AND NOT `deleted` AND `network` != '' $extra_sql ORDER BY `network`",
$r = DBA::p("SELECT `network` FROM `contact` WHERE `uid` = ? AND NOT `deleted` AND `network` != '' $extra_sql GROUP BY `network` ORDER BY `network`",
local_user()
);
@ -395,7 +396,7 @@ class Widget
$entries = [];
foreach ($commonContacts as $contact) {
$entries[] = [
'url' => Contact::magicLink($contact['url']),
'url' => Contact::magicLinkByContact($contact),
'name' => $contact['name'],
'photo' => Contact::getThumb($contact),
];
@ -460,7 +461,13 @@ class Widget
$ret = [];
$dthen = Item::firstPostDate($uid, $wall);
$cachekey = 'Widget::postedByYear' . $uid . '-' . (int)$wall;
$dthen = DI::cache()->get($cachekey);
if (!empty($dthen)) {
$dthen = Item::firstPostDate($uid, $wall);
DI::cache()->set($cachekey, $dthen, Duration::HOUR);
}
if ($dthen) {
// Set the start and end date to the beginning of the month
$dnow = substr($dnow, 0, 8) . '01';

View file

@ -144,6 +144,7 @@ class ACL
'archive' => false,
'deleted' => false,
'pending' => false,
'network' => Protocol::FEDERATED,
'rel' => [Contact::FOLLOWER, Contact::FRIEND]
], $condition),
$params
@ -156,7 +157,7 @@ class ACL
$acl_forums = Contact::selectToArray($fields,
['uid' => $user_id, 'self' => false, 'blocked' => false, 'archive' => false, 'deleted' => false,
'pending' => false, 'contact-type' => Contact::TYPE_COMMUNITY], $params
'network' => Protocol::FEDERATED, 'pending' => false, 'contact-type' => Contact::TYPE_COMMUNITY], $params
);
$acl_contacts = array_merge($acl_forums, $acl_contacts);

View file

@ -905,7 +905,7 @@ class Contact
if (empty($contact['uid']) || ($contact['uid'] != $uid)) {
if ($uid == 0) {
$profile_link = self::magicLink($contact['url']);
$profile_link = self::magicLinkByContact($contact);
$menu = ['profile' => [DI::l10n()->t('View Profile'), $profile_link, true]];
return $menu;
@ -2684,11 +2684,11 @@ class Contact
return 'contact/' . $contact['id'] . '/conversations';
}
if ($contact['network'] != Protocol::DFRN) {
if (!empty($contact['network']) && $contact['network'] != Protocol::DFRN) {
return $destination;
}
if (!empty($contact['uid'])) {
if (!empty($contact['uid']) || empty($contact['network'])) {
return self::magicLink($contact['url'], $url);
}
@ -2819,22 +2819,22 @@ class Contact
}
/**
* Returns a random, global contact of the current node
* Returns a random, global contact array of the current node
*
* @return string The profile URL
* @return array The profile array
* @throws Exception
*/
public static function getRandomUrl()
public static function getRandomContact()
{
$r = DBA::selectFirst('contact', ['url'], [
$contact = DBA::selectFirst('contact', ['id', 'network', 'url', 'uid'], [
"`uid` = ? AND `network` = ? AND NOT `failed` AND `last-item` > ?",
0, Protocol::DFRN, DateTimeFormat::utc('now - 1 month'),
], ['order' => ['RAND()']]);
if (DBA::isResult($r)) {
return $r['url'];
if (DBA::isResult($contact)) {
return $contact;
}
return '';
return [];
}
}

View file

@ -930,7 +930,9 @@ class Event
$location = self::locationToArray($item['event-location']);
// Construct the profile link (magic-auth).
$profile_link = Contact::magicLinkById($item['author-id']);
$author = ['uid' => 0, 'id' => $item['author-id'],
'network' => $item['author-network'], 'url' => $item['author-link']];
$profile_link = Contact::magicLinkByContact($author);
$tpl = Renderer::getMarkupTemplate('event_stream_item.tpl');
$return = Renderer::replaceMacros($tpl, [

View file

@ -32,7 +32,6 @@ use Friendica\Core\System;
use Friendica\Model\Tag;
use Friendica\Core\Worker;
use Friendica\Database\DBA;
use Friendica\Database\DBStructure;
use Friendica\DI;
use Friendica\Model\Post;
use Friendica\Protocol\Activity;
@ -75,12 +74,12 @@ class Item
'uid', 'id', 'parent', 'guid', 'network', 'gravity',
'uri-id', 'uri', 'thr-parent-id', 'thr-parent', 'parent-uri-id', 'parent-uri',
'commented', 'created', 'edited', 'received', 'verb', 'object-type', 'postopts', 'plink',
'wall', 'private', 'starred', 'origin', 'title', 'body', 'language',
'wall', 'private', 'starred', 'origin', 'parent-origin', 'title', 'body', 'language',
'content-warning', 'location', 'coord', 'app', 'rendered-hash', 'rendered-html', 'object',
'allow_cid', 'allow_gid', 'deny_cid', 'deny_gid',
'author-id', 'author-link', 'author-name', 'author-avatar', 'author-network',
'owner-id', 'owner-link', 'owner-name', 'owner-avatar', 'owner-network',
'causer-id', 'causer-link', 'causer-name', 'causer-avatar', 'causer-contact-type',
'causer-id', 'causer-link', 'causer-name', 'causer-avatar', 'causer-contact-type', 'causer-network',
'contact-id', 'contact-uid', 'contact-link', 'contact-name', 'contact-avatar',
'writable', 'self', 'cid', 'alias',
'event-created', 'event-edited', 'event-start', 'event-finish',
@ -2196,7 +2195,8 @@ class Item
$params = ['order' => ['received' => false]];
$thread = Post::selectFirst(['received'], $condition, $params);
if (DBA::isResult($thread)) {
return substr(DateTimeFormat::local($thread['received']), 0, 10);
$postdate = substr(DateTimeFormat::local($thread['received']), 0, 10);
return $postdate;
}
return false;
}
@ -2673,7 +2673,9 @@ class Item
foreach (Post\Media::getByURIId($item['uri-id'], [Post\Media::DOCUMENT, Post\Media::TORRENT, Post\Media::UNKNOWN]) as $attachment) {
$mime = $attachment['mimetype'];
$the_url = Contact::magicLinkById($item['author-id'], $attachment['url']);
$author = ['uid' => 0, 'id' => $item['author-id'],
'network' => $item['author-network'], 'url' => $item['author-link']];
$the_url = Contact::magicLinkByContact($author, $attachment['url']);
if (strpos($mime, 'video') !== false) {
if (!$vhead) {

View file

@ -539,7 +539,7 @@ class Profile
$today = (((strtotime($rr['start'] . ' +00:00') < $now) && (strtotime($rr['finish'] . ' +00:00') > $now)) ? true : false);
$rr['link'] = Contact::magicLink($rr['url']);
$rr['link'] = Contact::magicLinkById($rr['cid']);
$rr['title'] = $rr['name'];
$rr['date'] = DI::l10n()->getDay(DateTimeFormat::convert($rr['start'], $a->timezone, 'UTC', $rr['adjust'] ? $bd_format : $bd_short)) . (($today) ? ' ' . DI::l10n()->t('[today]') : '');
$rr['startime'] = null;

View file

@ -407,7 +407,7 @@ class Tag
$searchpath = DI::baseUrl() . "/search?tag=";
$taglist = DBA::select('tag-view', ['type', 'name', 'url'],
$taglist = DBA::select('tag-view', ['type', 'name', 'url', 'cid'],
['uri-id' => $item['uri-id'], 'type' => [self::HASHTAG, self::MENTION, self::EXCLUSIVE_MENTION, self::IMPLICIT_MENTION]]);
while ($tag = DBA::fetch($taglist)) {
if ($tag['url'] == '') {
@ -428,7 +428,11 @@ class Tag
break;
case self::MENTION:
case self::EXCLUSIVE_MENTION:
if (!empty($tag['cid'])) {
$tag['url'] = Contact::magicLinkById($tag['cid']);
} else {
$tag['url'] = Contact::magicLink($tag['url']);
}
$return['mentions'][] = $prefix . '<a href="' . $tag['url'] . '" target="_blank" rel="noopener noreferrer">' . htmlspecialchars($tag['name']) . '</a>';
$return['tags'][] = $prefix . '<a href="' . $tag['url'] . '" target="_blank" rel="noopener noreferrer">' . htmlspecialchars($tag['name']) . '</a>';
break;

View file

@ -518,7 +518,7 @@ class Contact extends BaseModule
$relation_text = sprintf($relation_text, $contact['name']);
$url = Model\Contact::magicLink($contact['url']);
$url = Model\Contact::magicLinkByContact($contact);
if (strpos($url, 'redir/') === 0) {
$sparkle = ' class="sparkle" ';
} else {
@ -1076,7 +1076,7 @@ class Contact extends BaseModule
}
}
$url = Model\Contact::magicLink($contact['url']);
$url = Model\Contact::magicLinkByContact($contact);
if (strpos($url, 'redir/') === 0) {
$sparkle = ' class="sparkle" ';

View file

@ -94,7 +94,7 @@ class Hovercard extends BaseModule
'nick' => $contact['nick'],
'addr' => $contact['addr'] ?: $contact['url'],
'thumb' => Contact::getThumb($contact),
'url' => Contact::magicLink($contact['url']),
'url' => Contact::magicLinkByContact($contact),
'nurl' => $contact['nurl'],
'location' => $contact['location'],
'about' => $contact['about'],

View file

@ -47,7 +47,7 @@ class ItemBody extends BaseModule
throw new HTTPException\NotFoundException(DI::l10n()->t('Item not found.'));
}
$item = Post::selectFirst(['body'], ['uid' => local_user(), 'id' => $itemId]);
$item = Post::selectFirst(['body'], ['uid' => local_user(), 'uri-id' => $itemId]);
if (!empty($item)) {
if (DI::mode()->isAjax()) {

View file

@ -34,10 +34,10 @@ class RandomProfile extends BaseModule
{
$a = DI::app();
$contactUrl = Contact::getRandomUrl();
$contact = Contact::getRandomContact();
if ($contactUrl) {
$link = Contact::magicLink($contactUrl);
if (!empty($contact)) {
$link = Contact::magicLinkByContact($contact);
$a->redirect($link);
}

View file

@ -208,16 +208,9 @@ class Post
$dropping = true;
}
$origin = $item['origin'];
$origin = $item['origin'] || $item['parent-origin'];
if (!$origin) {
/// @todo This shouldn't be done as query here, but better during the data creation.
// it is now done here, since during the RC phase we shouldn't make to intense changes.
$parent = PostModel::selectFirst(['origin'], ['id' => $item['parent']]);
if (DBA::isResult($parent)) {
$origin = $parent['origin'];
}
} elseif ($item['pinned']) {
if ($item['pinned']) {
$pinned = DI::l10n()->t('pinned item');
}
@ -252,10 +245,9 @@ class Post
$profile_name = $item['author-link'];
}
$author = ['uid' => 0, 'id' => $item['author-id'],
'network' => $item['author-network'], 'url' => $item['author-link']];
if (Session::isAuthenticated()) {
$author = ['uid' => 0, 'id' => $item['author-id'],
'network' => $item['author-network'], 'url' => $item['author-link']];
$profile_link = Contact::magicLinkByContact($author);
} else {
$profile_link = $item['author-link'];
@ -1005,7 +997,7 @@ class Post
// This will have been stored in $a->page_contact by our calling page.
// Put this person as the wall owner of the wall-to-wall notice.
$this->owner_url = Contact::magicLink($a->page_contact['url']);
$this->owner_url = Contact::magicLinkByContact($a->page_contact);
$this->owner_photo = $a->page_contact['thumb'];
$this->owner_name = $a->page_contact['name'];
$this->wall_to_wall = true;

View file

@ -55,7 +55,7 @@
use Friendica\Database\DBA;
if (!defined('DB_UPDATE_VERSION')) {
define('DB_UPDATE_VERSION', 1401);
define('DB_UPDATE_VERSION', 1402);
}
return [
@ -1279,11 +1279,11 @@ return [
"uid_hidden" => ["uid", "hidden"],
"event-id" => ["event-id"],
"uid_wall" => ["uid", "wall"],
"parent-uri-id" => ["parent-uri-id"],
"parent-uri-id_uid" => ["parent-uri-id", "uid"],
"thr-parent-id" => ["thr-parent-id"],
"external-id" => ["external-id"],
"owner-id" => ["owner-id"],
"author-id" => ["author-id"],
"author-id_uid" => ["author-id", "uid"],
"causer-id" => ["causer-id"],
"vid" => ["vid"],
"uid_received" => ["uid", "received"],

View file

@ -68,6 +68,7 @@
"unseen" => ["post-user", "unseen"],
"deleted" => ["post-user", "deleted"],
"origin" => ["post-user", "origin"],
"parent-origin" => ["post-thread-user", "origin"],
"forum_mode" => ["post-thread-user", "forum_mode"],
"mention" => ["post-thread-user", "mention"],
"global" => ["post-user", "global"],