Merge pull request #2878 from Hypolite/improvement/ping-performance

Improving ping.php performance
This commit is contained in:
Michael Vogel 2016-11-02 21:56:18 +01:00 committed by GitHub
commit 9dd3e9d19a
9 changed files with 166 additions and 78 deletions

View file

@ -38,7 +38,7 @@ define ( 'FRIENDICA_PLATFORM', 'Friendica');
define ( 'FRIENDICA_CODENAME', 'Asparagus'); define ( 'FRIENDICA_CODENAME', 'Asparagus');
define ( 'FRIENDICA_VERSION', '3.5.1-dev' ); define ( 'FRIENDICA_VERSION', '3.5.1-dev' );
define ( 'DFRN_PROTOCOL_VERSION', '2.23' ); define ( 'DFRN_PROTOCOL_VERSION', '2.23' );
define ( 'DB_UPDATE_VERSION', 1207 ); define ( 'DB_UPDATE_VERSION', 1208 );
/** /**
* @brief Constant with a HTML line break. * @brief Constant with a HTML line break.
@ -785,60 +785,100 @@ class App {
return($this->scheme); return($this->scheme);
} }
/**
* @brief Retrieves the Friendica instance base URL
*
* This function assembles the base URL from multiple parts:
* - Protocol is determined either by the request or a combination of
* system.ssl_policy and the $ssl parameter.
* - Host name is determined either by system.hostname or inferred from request
* - Path is inferred from SCRIPT_NAME
*
* Caches the result (depending on $ssl value) for performance.
*
* Note: $ssl parameter value doesn't directly correlate with the resulting protocol
*
* @param bool $ssl Whether to append http or https under SSL_POLICY_SELFSIGN
* @return string Friendica server base URL
*/
function get_baseurl($ssl = false) { function get_baseurl($ssl = false) {
// Is the function called statically? // Is the function called statically?
if (!is_object($this)) if (!is_object($this)) {
return(self::$a->get_baseurl($ssl)); return self::$a->get_baseurl($ssl);
}
// Arbitrary values, the resulting url protocol can be different
$cache_index = $ssl ? 'https' : 'http';
// Cached value found, nothing to process
if (isset($this->baseurl[$cache_index])) {
return $this->baseurl[$cache_index];
}
$scheme = $this->scheme; $scheme = $this->scheme;
if((x($this->config,'system')) && (x($this->config['system'],'ssl_policy'))) { if ((x($this->config, 'system')) && (x($this->config['system'], 'ssl_policy'))) {
if(intval($this->config['system']['ssl_policy']) === intval(SSL_POLICY_FULL)) if (intval($this->config['system']['ssl_policy']) === SSL_POLICY_FULL) {
$scheme = 'https'; $scheme = 'https';
}
// Basically, we have $ssl = true on any links which can only be seen by a logged in user // Basically, we have $ssl = true on any links which can only be seen by a logged in user
// (and also the login link). Anything seen by an outsider will have it turned off. // (and also the login link). Anything seen by an outsider will have it turned off.
if($this->config['system']['ssl_policy'] == SSL_POLICY_SELFSIGN) { if ($this->config['system']['ssl_policy'] == SSL_POLICY_SELFSIGN) {
if($ssl) if ($ssl) {
$scheme = 'https'; $scheme = 'https';
else } else {
$scheme = 'http'; $scheme = 'http';
}
} }
} }
if (get_config('config','hostname') != "") if (get_config('config', 'hostname') != '') {
$this->hostname = get_config('config','hostname'); $this->hostname = get_config('config', 'hostname');
}
$this->baseurl = $scheme . "://" . $this->hostname . ((isset($this->path) && strlen($this->path)) ? '/' . $this->path : '' ); $this->baseurl[$cache_index] = $scheme . "://" . $this->hostname . ((isset($this->path) && strlen($this->path)) ? '/' . $this->path : '' );
return $this->baseurl;
return $this->baseurl[$cache_index];
} }
/**
* @brief Initializes the baseurl components
*
* Clears the baseurl cache to prevent inconstistencies
*
* @param string $url
*/
function set_baseurl($url) { function set_baseurl($url) {
$parsed = @parse_url($url); $parsed = @parse_url($url);
$this->baseurl = $url; $this->baseurl = [];
if($parsed) { if($parsed) {
$this->scheme = $parsed['scheme']; $this->scheme = $parsed['scheme'];
$hostname = $parsed['host']; $hostname = $parsed['host'];
if(x($parsed,'port')) if (x($parsed, 'port')) {
$hostname .= ':' . $parsed['port']; $hostname .= ':' . $parsed['port'];
if(x($parsed,'path')) }
$this->path = trim($parsed['path'],'\\/'); if (x($parsed, 'path')) {
$this->path = trim($parsed['path'], '\\/');
}
if (file_exists(".htpreconfig.php")) if (file_exists(".htpreconfig.php")) {
@include(".htpreconfig.php"); @include(".htpreconfig.php");
}
if (get_config('config','hostname') != "") if (get_config('config', 'hostname') != '') {
$this->hostname = get_config('config','hostname'); $this->hostname = get_config('config', 'hostname');
}
if (!isset($this->hostname) OR ($this->hostname == "")) if (!isset($this->hostname) OR ($this->hostname == '')) {
$this->hostname = $hostname; $this->hostname = $hostname;
}
} }
} }
function get_hostname() { function get_hostname() {

View file

@ -655,6 +655,8 @@ CREATE TABLE IF NOT EXISTS `notify` (
`seen` tinyint(1) NOT NULL DEFAULT 0, `seen` tinyint(1) NOT NULL DEFAULT 0,
`verb` varchar(255) NOT NULL DEFAULT '', `verb` varchar(255) NOT NULL DEFAULT '',
`otype` varchar(16) NOT NULL DEFAULT '', `otype` varchar(16) NOT NULL DEFAULT '',
`name_cache` tinytext,
`msg_name` mediumtext,
PRIMARY KEY(`id`), PRIMARY KEY(`id`),
INDEX `uid` (`uid`) INDEX `uid` (`uid`)
) DEFAULT CHARSET=utf8mb4; ) DEFAULT CHARSET=utf8mb4;

View file

@ -1,22 +1,24 @@
Table notify Table notify
============ ============
| Field | Description | Type | Null | Key | Default | Extra | | Field | Description | Type | Null | Key | Default | Extra |
| ------ | --------------------------------- | ------------ | ---- | --- | ------------------- | --------------- | | ---------- | --------------------------------- | ------------ | ---- | --- | ------------------- | --------------- |
| id | sequential ID | int(11) | NO | PRI | NULL | auto_increment | | id | sequential ID | int(11) | NO | PRI | NULL | auto_increment |
| hash | | varchar(64) | NO | | | | | hash | | varchar(64) | NO | | | |
| type | | int(11) | NO | | 0 | | | type | | int(11) | NO | | 0 | |
| name | | varchar(255) | NO | | | | | name | | varchar(255) | NO | | | |
| url | | varchar(255) | NO | | | | | url | | varchar(255) | NO | | | |
| photo | | varchar(255) | NO | | | | | photo | | varchar(255) | NO | | | |
| date | | datetime | NO | | 0000-00-00 00:00:00 | | | date | | datetime | NO | | 0000-00-00 00:00:00 | |
| msg | | mediumtext | NO | | NULL | | | msg | | mediumtext | YES | | NULL | |
| uid | user.id of the owner of this data | int(11) | NO | MUL | 0 | | | uid | user.id of the owner of this data | int(11) | NO | MUL | 0 | |
| link | | varchar(255) | NO | | | | | link | | varchar(255) | NO | | | |
| parent | | int(11) | NO | | 0 | | | iid | item.id | int(11) | NO | | 0 | |
| seen | | tinyint(1) | NO | | 0 | | | parent | | int(11) | NO | | 0 | |
| verb | | varchar(255) | NO | | | | | seen | | tinyint(1) | NO | | 0 | |
| otype | | varchar(16) | NO | | | | | verb | | varchar(255) | NO | | | |
| iid | item.id | int(11) | NO | | 0 | | | otype | | varchar(16) | NO | | | |
| name_cache | Cached bbcode parsing of name | tinytext | YES | | NULL | |
| msg_cache | Cached bbcode parsing of msg | mediumtext | YES | | NULL | |
Return to [database documentation](help/database) Return to [database documentation](help/database)

View file

@ -325,15 +325,15 @@ function datetimesel($format, $min, $max, $default, $label, $id = 'datetimepicke
* Results relative to current timezone. * Results relative to current timezone.
* Limited to range of timestamps. * Limited to range of timestamps.
* *
* @param string $posted_date * @param string $posted_date MySQL-formatted date string (YYYY-MM-DD HH:MM:SS)
* @param string $format (optional) Parsed with sprintf() * @param string $format (optional) Parsed with sprintf()
* <tt>%1$d %2$s ago</tt>, e.g. 22 hours ago, 1 minute ago * <tt>%1$d %2$s ago</tt>, e.g. 22 hours ago, 1 minute ago
* *
* @return string with relative date * @return string with relative date
*/ */
function relative_date($posted_date,$format = null) { function relative_date($posted_date, $format = null) {
$localtime = datetime_convert('UTC',date_default_timezone_get(),$posted_date); $localtime = $posted_date . ' UTC';
$abs = strtotime($localtime); $abs = strtotime($localtime);
@ -347,13 +347,6 @@ function relative_date($posted_date,$format = null) {
return t('less than a second ago'); return t('less than a second ago');
} }
/*
$time_append = '';
if ($etime >= 86400) {
$time_append = ' ('.$localtime.')';
}
*/
$a = array( 12 * 30 * 24 * 60 * 60 => array( t('year'), t('years')), $a = array( 12 * 30 * 24 * 60 * 60 => array( t('year'), t('years')),
30 * 24 * 60 * 60 => array( t('month'), t('months')), 30 * 24 * 60 * 60 => array( t('month'), t('months')),
7 * 24 * 60 * 60 => array( t('week'), t('weeks')), 7 * 24 * 60 * 60 => array( t('week'), t('weeks')),
@ -368,10 +361,11 @@ function relative_date($posted_date,$format = null) {
if ($d >= 1) { if ($d >= 1) {
$r = round($d); $r = round($d);
// translators - e.g. 22 hours ago, 1 minute ago // translators - e.g. 22 hours ago, 1 minute ago
if(! $format) if (!$format) {
$format = t('%1$d %2$s ago'); $format = t('%1$d %2$s ago');
}
return sprintf( $format,$r, (($r == 1) ? $str[0] : $str[1])); return sprintf($format, $r, (($r == 1) ? $str[0] : $str[1]));
} }
} }
} }

View file

@ -1039,6 +1039,8 @@ function db_definition($charset) {
"seen" => array("type" => "tinyint(1)", "not null" => "1", "default" => "0"), "seen" => array("type" => "tinyint(1)", "not null" => "1", "default" => "0"),
"verb" => array("type" => "varchar(255)", "not null" => "1", "default" => ""), "verb" => array("type" => "varchar(255)", "not null" => "1", "default" => ""),
"otype" => array("type" => "varchar(16)", "not null" => "1", "default" => ""), "otype" => array("type" => "varchar(16)", "not null" => "1", "default" => ""),
"name_cache" => array("type" => "tinytext"),
"msg_cache" => array("type" => "mediumtext")
), ),
"indexes" => array( "indexes" => array(
"PRIMARY" => array("id"), "PRIMARY" => array("id"),

View file

@ -418,6 +418,7 @@ function notification($params) {
$datarray = array(); $datarray = array();
$datarray['hash'] = $hash; $datarray['hash'] = $hash;
$datarray['name'] = $params['source_name']; $datarray['name'] = $params['source_name'];
$datarray['name_cache'] = strip_tags(bbcode($params['source_name']));
$datarray['url'] = $params['source_link']; $datarray['url'] = $params['source_link'];
$datarray['photo'] = $params['source_photo']; $datarray['photo'] = $params['source_photo'];
$datarray['date'] = datetime_convert(); $datarray['date'] = datetime_convert();
@ -439,7 +440,7 @@ function notification($params) {
// create notification entry in DB // create notification entry in DB
$r = q("INSERT INTO `notify` (`hash`, `name`, `url`, `photo`, `date`, `uid`, `link`, `iid`, `parent`, `type`, `verb`, `otype`) $r = q("INSERT INTO `notify` (`hash`, `name`, `url`, `photo`, `date`, `uid`, `link`, `iid`, `parent`, `type`, `verb`, `otype`, `name_cache`)
values('%s', '%s', '%s', '%s', '%s', %d, '%s', %d, %d, %d, '%s', '%s')", values('%s', '%s', '%s', '%s', '%s', %d, '%s', %d, %d, %d, '%s', '%s')",
dbesc($datarray['hash']), dbesc($datarray['hash']),
dbesc($datarray['name']), dbesc($datarray['name']),
@ -452,7 +453,8 @@ function notification($params) {
intval($datarray['parent']), intval($datarray['parent']),
intval($datarray['type']), intval($datarray['type']),
dbesc($datarray['verb']), dbesc($datarray['verb']),
dbesc($datarray['otype']) dbesc($datarray['otype']),
dbesc($datarray["name_cache"])
); );
$r = q("SELECT `id` FROM `notify` WHERE `hash` = '%s' AND `uid` = %d LIMIT 1", $r = q("SELECT `id` FROM `notify` WHERE `hash` = '%s' AND `uid` = %d LIMIT 1",
@ -494,8 +496,10 @@ function notification($params) {
$itemlink = $a->get_baseurl().'/notify/view/'.$notify_id; $itemlink = $a->get_baseurl().'/notify/view/'.$notify_id;
$msg = replace_macros($epreamble, array('$itemlink' => $itemlink)); $msg = replace_macros($epreamble, array('$itemlink' => $itemlink));
$r = q("UPDATE `notify` SET `msg` = '%s' WHERE `id` = %d AND `uid` = %d", $msg_cache = format_notification_message($datarray['name_cache'], strip_tags(bbcode($msg)));
$r = q("UPDATE `notify` SET `msg` = '%s', `msg_cache` = '%s' WHERE `id` = %d AND `uid` = %d",
dbesc($msg), dbesc($msg),
dbesc($msg_cache),
intval($notify_id), intval($notify_id),
intval($params['uid']) intval($params['uid'])
); );
@ -778,4 +782,27 @@ function check_item_notification($itemid, $uid, $defaulttype = "") {
if (isset($params["type"])) if (isset($params["type"]))
notification($params); notification($params);
} }
?>
/**
* @brief Formats a notification message with the notification author
*
* Replace the name with {0} but ensure to make that only once. The {0} is used
* later and prints the name in bold.
*
* @param string $name
* @param string $message
* @return string Formatted message
*/
function format_notification_message($name, $message) {
if ($name != '') {
$pos = strpos($message, $name);
} else {
$pos = false;
}
if ($pos !== false) {
$message = substr_replace($message, '{0}', $pos, strlen($name));
}
return $message;
}

View file

@ -40,6 +40,19 @@ function ref_session_read($id) {
return ''; return '';
} }
/**
* @brief Standard PHP session write callback
*
* This callback updates the DB-stored session data and/or the expiration depending
* on the case. Uses the $session_expire global for existing session, 5 minutes
* for newly created session.
*
* @global bool $session_exists Whether a session with the given id already exists
* @global int $session_expire Session expiration delay in seconds
* @param string $id Session ID with format: [a-z0-9]{26}
* @param string $data Serialized session data
* @return boolean Returns false if parameters are missing, true otherwise
*/
function ref_session_write($id, $data) { function ref_session_write($id, $data) {
global $session_exists, $session_expire; global $session_exists, $session_expire;
@ -66,10 +79,11 @@ function ref_session_write($id, $data) {
SET `expire` = '%s' SET `expire` = '%s'
WHERE `expire` != '%s' AND `sid` = '%s'", WHERE `expire` != '%s' AND `sid` = '%s'",
dbesc($expire), dbesc($expire), dbesc($id)); dbesc($expire), dbesc($expire), dbesc($id));
} else } else {
$r = q("INSERT INTO `session` $r = q("INSERT INTO `session`
SET `sid` = '%s', `expire` = '%s', `data` = '%s'", SET `sid` = '%s', `expire` = '%s', `data` = '%s'",
dbesc($id), dbesc($default_expire), dbesc($data)); dbesc($id), dbesc($default_expire), dbesc($data));
}
return true; return true;
} }

View file

@ -344,6 +344,12 @@ function ping_init(&$a) {
killme(); killme();
} }
/**
* @brief Retrieves the notifications array for the given user ID
*
* @param int $uid User id
* @return array Associative array of notifications
*/
function ping_get_notifications($uid) { function ping_get_notifications($uid) {
$result = array(); $result = array();
@ -372,46 +378,47 @@ function ping_get_notifications($uid) {
$seensql = ""; $seensql = "";
$order = "DESC"; $order = "DESC";
$offset = 0; $offset = 0;
} elseif (!$r) } elseif (!$r) {
$quit = true; $quit = true;
else } else {
$offset += 50; $offset += 50;
}
foreach ($r AS $notification) { foreach ($r AS $notification) {
if (is_null($notification["visible"])) if (is_null($notification["visible"])) {
$notification["visible"] = true; $notification["visible"] = true;
}
if (is_null($notification["spam"])) if (is_null($notification["spam"])) {
$notification["spam"] = 0; $notification["spam"] = 0;
}
if (is_null($notification["deleted"])) if (is_null($notification["deleted"])) {
$notification["deleted"] = 0; $notification["deleted"] = 0;
}
$notification["message"] = strip_tags(bbcode($notification["msg"])); if ($notification["msg_cache"]) {
$notification["name"] = strip_tags(bbcode($notification["name"])); $notification["name"] = $notification["name_cache"];
$notification["message"] = $notification["msg_cache"];
} else {
$notification["name"] = strip_tags(bbcode($notification["name"]));
$notification["message"] = format_notification_message($notification["name"], strip_tags(bbcode($notification["msg"])));
// Replace the name with {0} but ensure to make that only once q("UPDATE `notify` SET `name_cache` = '%s', `msg_cache` = '%s' WHERE `id` = %d",
// The {0} is used later and prints the name in bold. dbesc($notification["name"]),
dbesc($notification["message"]),
intval($notification["id"])
);
}
if ($notification['name'] != "") $notification["href"] = $a->get_baseurl() . "/notify/view/" . $notification["id"];
$pos = strpos($notification["message"],$notification['name']);
else
$pos = false;
if ($pos !== false)
$notification["message"] = substr_replace($notification["message"],"{0}",$pos,strlen($notification["name"]));
$notification['href'] = $a->get_baseurl() . '/notify/view/' . $notification['id'];
if ($notification["visible"] AND !$notification["spam"] AND if ($notification["visible"] AND !$notification["spam"] AND
!$notification["deleted"] AND !is_array($result[$notification["parent"]])) { !$notification["deleted"] AND !is_array($result[$notification["parent"]])) {
$result[$notification["parent"]] = $notification; $result[$notification["parent"]] = $notification;
} }
} }
} while ((count($result) < 50) AND !$quit); } while ((count($result) < 50) AND !$quit);
return($result); return($result);
} }

View file

@ -1,6 +1,6 @@
<?php <?php
define('UPDATE_VERSION' , 1207); define('UPDATE_VERSION' , 1208);
/** /**
* *