Some code adjustements and performance improvements to the DFRN feed.

This commit is contained in:
Michael Vogel 2016-10-21 23:04:04 +00:00
parent 501c45def5
commit a8bef370d3
4 changed files with 43 additions and 47 deletions

View file

@ -33,8 +33,8 @@ class Config {
global $a; global $a;
$r = q("SELECT `v`, `k` FROM `config` WHERE `cat` = '%s' ORDER BY `cat`, `k`, `id`", dbesc($family)); $r = q("SELECT `v`, `k` FROM `config` WHERE `cat` = '%s' ORDER BY `cat`, `k`, `id`", dbesc($family));
if(count($r)) { if (count($r)) {
foreach($r as $rr) { foreach ($r as $rr) {
$k = $rr['k']; $k = $rr['k'];
if ($family === 'config') { if ($family === 'config') {
$a->config[$k] = $rr['v']; $a->config[$k] = $rr['v'];
@ -70,20 +70,20 @@ class Config {
* If true the config is loaded from the db and not from the cache (default: false) * If true the config is loaded from the db and not from the cache (default: false)
* @return mixed Stored value or null if it does not exist * @return mixed Stored value or null if it does not exist
*/ */
public static function get($family, $key, $default_value=null, $refresh = false) { public static function get($family, $key, $default_value = null, $refresh = false) {
global $a; global $a;
if(! $refresh) { if (!$refresh) {
// Looking if the whole family isn't set // Looking if the whole family isn't set
if(isset($a->config[$family])) { if (isset($a->config[$family])) {
if($a->config[$family] === '!<unset>!') { if ($a->config[$family] === '!<unset>!') {
return $default_value; return $default_value;
} }
} }
if(isset($a->config[$family][$key])) { if (isset($a->config[$family][$key])) {
if($a->config[$family][$key] === '!<unset>!') { if ($a->config[$family][$key] === '!<unset>!') {
return $default_value; return $default_value;
} }
return $a->config[$family][$key]; return $a->config[$family][$key];
@ -94,7 +94,7 @@ class Config {
dbesc($family), dbesc($family),
dbesc($key) dbesc($key)
); );
if(count($ret)) { if (count($ret)) {
// manage array value // manage array value
$val = (preg_match("|^a:[0-9]+:{.*}$|s", $ret[0]['v'])?unserialize( $ret[0]['v']):$ret[0]['v']); $val = (preg_match("|^a:[0-9]+:{.*}$|s", $ret[0]['v'])?unserialize( $ret[0]['v']):$ret[0]['v']);
$a->config[$family][$key] = $val; $a->config[$family][$key] = $val;
@ -123,13 +123,13 @@ class Config {
* The value to store * The value to store
* @return mixed Stored $value or false if the database update failed * @return mixed Stored $value or false if the database update failed
*/ */
public static function set($family,$key,$value) { public static function set($family, $key, $value) {
global $a; global $a;
$a->config[$family][$key] = $value; $a->config[$family][$key] = $value;
// manage array value // manage array value
$dbvalue = (is_array($value)?serialize($value):$value); $dbvalue = (is_array($value) ? serialize($value):$value);
$dbvalue = (is_bool($dbvalue) ? intval($dbvalue) : $dbvalue); $dbvalue = (is_bool($dbvalue) ? intval($dbvalue) : $dbvalue);
// The "INSERT" command is very cost intense. It saves performance to do it this way. // The "INSERT" command is very cost intense. It saves performance to do it this way.
@ -138,25 +138,21 @@ class Config {
dbesc($key) dbesc($key)
); );
// It would be better to use the dbm class. if (!$ret) {
// But there is an autoloader issue that I don't know how to fix:
// "Class 'Friendica\Core\dbm' not found"
//if (!dbm::is_result($ret))
if (!$ret)
$ret = q("INSERT INTO `config` (`cat`, `k`, `v`) VALUES ('%s', '%s', '%s') ON DUPLICATE KEY UPDATE `v` = '%s'", $ret = q("INSERT INTO `config` (`cat`, `k`, `v`) VALUES ('%s', '%s', '%s') ON DUPLICATE KEY UPDATE `v` = '%s'",
dbesc($family), dbesc($family),
dbesc($key), dbesc($key),
dbesc($dbvalue), dbesc($dbvalue),
dbesc($dbvalue) dbesc($dbvalue)
); );
elseif ($ret[0]['v'] != $dbvalue) } elseif ($ret[0]['v'] != $dbvalue) {
$ret = q("UPDATE `config` SET `v` = '%s' WHERE `cat` = '%s' AND `k` = '%s'", $ret = q("UPDATE `config` SET `v` = '%s' WHERE `cat` = '%s' AND `k` = '%s'",
dbesc($dbvalue), dbesc($dbvalue),
dbesc($family), dbesc($family),
dbesc($key) dbesc($key)
); );
}
if($ret) if ($ret)
return $value; return $value;
return $ret; return $ret;
@ -174,10 +170,10 @@ class Config {
* The configuration key to delete * The configuration key to delete
* @return mixed * @return mixed
*/ */
public static function delete($family,$key) { public static function delete($family, $key) {
global $a; global $a;
if(x($a->config[$family],$key)) if (x($a->config[$family],$key))
unset($a->config[$family][$key]); unset($a->config[$family][$key]);
$ret = q("DELETE FROM `config` WHERE `cat` = '%s' AND `k` = '%s'", $ret = q("DELETE FROM `config` WHERE `cat` = '%s' AND `k` = '%s'",
dbesc($family), dbesc($family),

View file

@ -27,14 +27,14 @@ class PConfig {
* The category of the configuration value * The category of the configuration value
* @return void * @return void
*/ */
public static function load($uid,$family) { public static function load($uid, $family) {
global $a; global $a;
$r = q("SELECT `v`,`k` FROM `pconfig` WHERE `cat` = '%s' AND `uid` = %d ORDER BY `cat`, `k`, `id`", $r = q("SELECT `v`,`k` FROM `pconfig` WHERE `cat` = '%s' AND `uid` = %d ORDER BY `cat`, `k`, `id`",
dbesc($family), dbesc($family),
intval($uid) intval($uid)
); );
if(count($r)) { if (count($r)) {
foreach($r as $rr) { foreach ($r as $rr) {
$k = $rr['k']; $k = $rr['k'];
$a->config[$uid][$family][$k] = $rr['v']; $a->config[$uid][$family][$k] = $rr['v'];
} }
@ -67,16 +67,16 @@ class PConfig {
global $a; global $a;
if(! $refresh) { if (!$refresh) {
// Looking if the whole family isn't set // Looking if the whole family isn't set
if(isset($a->config[$uid][$family])) { if (isset($a->config[$uid][$family])) {
if($a->config[$uid][$family] === '!<unset>!') { if ($a->config[$uid][$family] === '!<unset>!') {
return $default_value; return $default_value;
} }
} }
if(isset($a->config[$uid][$family][$key])) { if (isset($a->config[$uid][$family][$key])) {
if($a->config[$uid][$family][$key] === '!<unset>!') { if ($a->config[$uid][$family][$key] === '!<unset>!') {
return $default_value; return $default_value;
} }
return $a->config[$uid][$family][$key]; return $a->config[$uid][$family][$key];
@ -89,13 +89,12 @@ class PConfig {
dbesc($key) dbesc($key)
); );
if(count($ret)) { if (count($ret)) {
$val = (preg_match("|^a:[0-9]+:{.*}$|s", $ret[0]['v'])?unserialize( $ret[0]['v']):$ret[0]['v']); $val = (preg_match("|^a:[0-9]+:{.*}$|s", $ret[0]['v'])?unserialize( $ret[0]['v']):$ret[0]['v']);
$a->config[$uid][$family][$key] = $val; $a->config[$uid][$family][$key] = $val;
return $val; return $val;
} } else {
else {
$a->config[$uid][$family][$key] = '!<unset>!'; $a->config[$uid][$family][$key] = '!<unset>!';
} }
return $default_value; return $default_value;
@ -124,7 +123,7 @@ class PConfig {
global $a; global $a;
// manage array value // manage array value
$dbvalue = (is_array($value)?serialize($value):$value); $dbvalue = (is_array($value) ? serialize($value):$value);
$a->config[$uid][$family][$key] = $value; $a->config[$uid][$family][$key] = $value;
@ -154,8 +153,9 @@ class PConfig {
dbesc($key) dbesc($key)
); );
if($ret) if ($ret)
return $value; return $value;
return $ret; return $ret;
} }
@ -175,7 +175,7 @@ class PConfig {
public static function delete($uid,$family,$key) { public static function delete($uid,$family,$key) {
global $a; global $a;
if(x($a->config[$uid][$family],$key)) if (x($a->config[$uid][$family],$key))
unset($a->config[$uid][$family][$key]); unset($a->config[$uid][$family][$key]);
$ret = q("DELETE FROM `pconfig` WHERE `uid` = %d AND `cat` = '%s' AND `k` = '%s'", $ret = q("DELETE FROM `pconfig` WHERE `uid` = %d AND `cat` = '%s' AND `k` = '%s'",
intval($uid), intval($uid),

View file

@ -1,4 +1,8 @@
<?php <?php
/**
* @file include/dbclean.php
* @brief The script is called from time to time to clean the database entries and remove orphaned data.
*/
require_once("boot.php"); require_once("boot.php");
global $a, $db; global $a, $db;
@ -19,6 +23,9 @@ load_config('system');
remove_orphans(); remove_orphans();
killme(); killme();
/**
* @brief Remove orphaned database entries
*/
function remove_orphans() { function remove_orphans() {
global $db; global $db;

View file

@ -112,7 +112,6 @@ class dfrn {
$owner_nick = $owner['nickname']; $owner_nick = $owner['nickname'];
$sql_post_table = ""; $sql_post_table = "";
$visibility = "";
if(! $public_feed) { if(! $public_feed) {
@ -171,9 +170,6 @@ class dfrn {
else else
$sort = 'ASC'; $sort = 'ASC';
$date_field = "`changed`";
$sql_order = "`item`.`parent` ".$sort.", `item`.`created` ASC";
if(! strlen($last_update)) if(! strlen($last_update))
$last_update = 'now -30 days'; $last_update = 'now -30 days';
@ -190,22 +186,19 @@ class dfrn {
$check_date = datetime_convert('UTC','UTC',$last_update,'Y-m-d H:i:s'); $check_date = datetime_convert('UTC','UTC',$last_update,'Y-m-d H:i:s');
// AND ( `item`.`edited` > '%s' OR `item`.`changed` > '%s' )
// dbesc($check_date),
$r = q("SELECT `item`.*, `item`.`id` AS `item_id`, $r = q("SELECT `item`.*, `item`.`id` AS `item_id`,
`contact`.`name`, `contact`.`network`, `contact`.`photo`, `contact`.`url`, `contact`.`name`, `contact`.`network`, `contact`.`photo`, `contact`.`url`,
`contact`.`name-date`, `contact`.`uri-date`, `contact`.`avatar-date`, `contact`.`name-date`, `contact`.`uri-date`, `contact`.`avatar-date`,
`contact`.`thumb`, `contact`.`dfrn-id`, `contact`.`self`, `contact`.`thumb`, `contact`.`dfrn-id`, `contact`.`self`,
`sign`.`signed_text`, `sign`.`signature`, `sign`.`signer` `sign`.`signed_text`, `sign`.`signature`, `sign`.`signer`
FROM `item` $sql_post_table FROM `item` USE INDEX (`uid_wall_changed`, `uid_type_changed`) $sql_post_table
STRAIGHT_JOIN `contact` ON `contact`.`id` = `item`.`contact-id` STRAIGHT_JOIN `contact` ON `contact`.`id` = `item`.`contact-id`
AND `contact`.`blocked` = 0 AND `contact`.`pending` = 0 AND NOT `contact`.`blocked`
LEFT JOIN `sign` ON `sign`.`iid` = `item`.`id` LEFT JOIN `sign` ON `sign`.`iid` = `item`.`id`
WHERE `item`.`uid` = %d AND `item`.`visible` = 1 and `item`.`moderated` = 0 AND `item`.`parent` != 0 WHERE `item`.`uid` = %d AND `item`.`visible` AND NOT `item`.`moderated` AND `item`.`parent` != 0
AND ((`item`.`wall` = 1) $visibility) AND `item`.$date_field > '%s' AND `item`.`wall` AND `item`.`changed` > '%s'
$sql_extra $sql_extra
ORDER BY $sql_order LIMIT 0, 300", ORDER BY `item`.`parent` ".$sort.", `item`.`created` ASC LIMIT 0, 300",
intval($owner_id), intval($owner_id),
dbesc($check_date), dbesc($check_date),
dbesc($sort) dbesc($sort)