]> git.mxchange.org Git - friendica.git/blobdiff - include/Core/PConfig.php
More usage of dbm::is_result($r) instead of count($r):
[friendica.git] / include / Core / PConfig.php
index 975210cfd2d608ea09487257d54efc3da3208365..33ec93c0eb5e4c15c5dfaea24892d70171f1feff 100644 (file)
@@ -28,12 +28,12 @@ class PConfig {
         * @return void
         */
        public static function load($uid, $family) {
-               global $a;
+               $a = get_app();
                $r = q("SELECT `v`,`k` FROM `pconfig` WHERE `cat` = '%s' AND `uid` = %d ORDER BY `cat`, `k`, `id`",
                        dbesc($family),
                        intval($uid)
                );
-               if (count($r)) {
+               if (dbm::is_result($r)) {
                        foreach ($r as $rr) {
                                $k = $rr['k'];
                                $a->config[$uid][$family][$k] = $rr['v'];
@@ -65,7 +65,7 @@ class PConfig {
         */
        public static function get($uid, $family, $key, $default_value = null, $refresh = false) {
 
-               global $a;
+               $a = get_app();
 
                if (!$refresh) {
                        // Looking if the whole family isn't set
@@ -118,26 +118,22 @@ class PConfig {
         *  The value to store
         * @return mixed Stored $value or false
         */
-       public static function set($uid,$family,$key,$value) {
+       public static function set($uid, $family, $key, $value) {
 
-               global $a;
+               $a = get_app();
+
+               $stored = self::get($uid, $family, $key);
+
+               if ($stored == $value) {
+                       return true;
+               }
 
                // manage array value
                $dbvalue = (is_array($value) ? serialize($value):$value);
 
                $a->config[$uid][$family][$key] = $value;
 
-               // The "INSERT" command is very cost intense. It saves performance to do it this way.
-               $ret = q("SELECT `v` FROM `pconfig` WHERE `uid` = %d AND `cat` = '%s' AND `k` = '%s' ORDER BY `id` DESC LIMIT 1",
-                       intval($uid),
-                       dbesc($family),
-                       dbesc($key)
-               );
-
-               // It would be better to use the dbm class.
-               // My lacking knowdledge in autoloaders prohibits this.
-               // if (!dbm::is_result($ret))
-                if (!$ret)
+                if (is_null($stored)) {
                        $ret = q("INSERT INTO `pconfig` (`uid`, `cat`, `k`, `v`) VALUES (%d, '%s', '%s', '%s') ON DUPLICATE KEY UPDATE `v` = '%s'",
                                intval($uid),
                                dbesc($family),
@@ -145,17 +141,18 @@ class PConfig {
                                dbesc($dbvalue),
                                dbesc($dbvalue)
                        );
-               elseif ($ret[0]['v'] != $dbvalue)
+               } else {
                        $ret = q("UPDATE `pconfig` SET `v` = '%s' WHERE `uid` = %d AND `cat` = '%s' AND `k` = '%s'",
                                dbesc($dbvalue),
                                intval($uid),
                                dbesc($family),
                                dbesc($key)
                        );
+               }
 
-               if ($ret)
+               if ($ret) {
                        return $value;
-
+               }
                return $ret;
        }
 
@@ -174,14 +171,18 @@ class PConfig {
         */
        public static function delete($uid,$family,$key) {
 
-               global $a;
-               if (x($a->config[$uid][$family], $key))
+               $a = get_app();
+
+               if (x($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'",
                        intval($uid),
                        dbesc($family),
                        dbesc($key)
                );
+
                return $ret;
        }
 }