]> git.mxchange.org Git - quix0rs-gnu-social.git/blobdiff - classes/Profile_tag.php
The overloaded DB_DataObject function staticGet is now called getKV
[quix0rs-gnu-social.git] / classes / Profile_tag.php
index 17f5034ff946034d45f3e6491652918665dd3c00..4e0900aa63a2c21a9ee128970ec73aedeb7824da 100644 (file)
@@ -4,7 +4,7 @@
  */
 require_once INSTALLDIR.'/classes/Memcached_DataObject.php';
 
-class Profile_tag extends Memcached_DataObject
+class Profile_tag extends Managed_DataObject
 {
     ###START_AUTOCODE
     /* the code below is auto generated do not remove the above tag */
@@ -15,13 +15,33 @@ class Profile_tag extends Memcached_DataObject
     public $tag;                             // varchar(64)  primary_key not_null
     public $modified;                        // timestamp()   not_null default_CURRENT_TIMESTAMP
 
-    /* Static get */
-    function staticGet($k,$v=null)
-    { return Memcached_DataObject::staticGet('Profile_tag',$k,$v); }
-
     /* the code above is auto generated do not remove the tag below */
     ###END_AUTOCODE
 
+    public static function schemaDef()
+    {
+        return array(
+
+            'fields' => array(
+                'tagger' => array('type' => 'int', 'not null' => true, 'description' => 'user making the tag'),
+                'tagged' => array('type' => 'int', 'not null' => true, 'description' => 'profile tagged'),
+                'tag' => array('type' => 'varchar', 'length' => 64, 'not null' => true, 'description' => 'hash tag associated with this notice'),
+                'modified' => array('type' => 'timestamp', 'not null' => true, 'description' => 'date the tag was added'),
+            ),
+            'primary key' => array('tagger', 'tagged', 'tag'),
+            'foreign keys' => array(
+                'profile_tag_tagger_fkey' => array('profile', array('tagger' => 'id')),
+                'profile_tag_tagged_fkey' => array('profile', array('tagged' => 'id')),
+                'profile_tag_tag_fkey' => array('profile_list', array('tag' => 'tag')),
+            ),
+            'indexes' => array(
+                'profile_tag_modified_idx' => array('modified'),
+                'profile_tag_tagger_tag_idx' => array('tagger', 'tag'),
+                'profile_tag_tagged_idx' => array('tagged'),
+            ),
+        );
+    }
+
     function pkeyGet($kv) {
         return Memcached_DataObject::pkeyGet('Profile_tag', $kv);
     }
@@ -55,20 +75,17 @@ class Profile_tag extends Memcached_DataObject
             return $tags;
         }
 
-        $profile_tag = new Profile_tag();
-        $profile_list->tagger = $tagger;
-        $profile_tag->tagged = $tagged;
+        $qry = 'select profile_list.* from profile_list left join '.
+               'profile_tag on (profile_list.tag = profile_tag.tag and '.
+               'profile_list.tagger = profile_tag.tagger) where '.
+               'profile_tag.tagger = %d and profile_tag.tagged = %d ';
+        $qry = sprintf($qry, $tagger, $tagged);
 
-        $profile_list->selectAdd();
+        if (!$include_priv) {
+            $qry .= ' and profile_list.private = 0';
+        }
 
-        // only fetch id, tag, mainpage and
-        // private hoping this will be faster
-        $profile_list->selectAdd('profile_list.id, ' .
-                                 'profile_list.tag, ' .
-                                 'profile_list.mainpage, ' .
-                                 'profile_list.private');
-        $profile_list->joinAdd($profile_tag);
-        $profile_list->find();
+        $profile_list->query($qry);
 
         Profile_list::setCache($key, $profile_list);
 
@@ -78,23 +95,26 @@ class Profile_tag extends Memcached_DataObject
     static function getTagsArray($tagger, $tagged, $auth_user_id=null)
     {
         $ptag = new Profile_tag();
-        $ptag->tagger = $tagger;
-        $ptag->tagged = $tagged;
-
-        if ($tagger != $auth_user_id) {
-            $list = new Profile_list();
-            $list->private = false;
-            $ptag->joinAdd($list);
-            $ptag->selectAdd();
-            $ptag->selectAdd('profile_tag.tag');
+
+        $qry = sprintf('select profile_tag.tag '.
+                       'from profile_tag join profile_list '.
+                       ' on (profile_tag.tagger = profile_list.tagger ' .
+                       '     and profile_tag.tag = profile_list.tag) ' .
+                       'where profile_tag.tagger = %d ' .
+                       'and   profile_tag.tagged = %d ',
+                       $tagger, $tagged);
+
+        if ($auth_user_id != $tagger) {
+            $qry .= 'and profile_list.private = 0';
         }
 
         $tags = array();
-        $ptag->find();
+
+        $ptag->query($qry);
+
         while ($ptag->fetch()) {
             $tags[] = $ptag->tag;
         }
-        $ptag->free();
 
         return $tags;
     }
@@ -137,8 +157,8 @@ class Profile_tag extends Memcached_DataObject
             return $ptag;
         }
 
-        $tagger_profile = Profile::staticGet('id', $tagger);
-        $tagged_profile = Profile::staticGet('id', $tagged);
+        $tagger_profile = Profile::getKV('id', $tagger);
+        $tagged_profile = Profile::getKV('id', $tagged);
 
         if (Event::handle('StartTagProfile', array($tagger_profile, $tagged_profile, $tag))) {
 
@@ -167,10 +187,10 @@ class Profile_tag extends Memcached_DataObject
             $profile_list = Profile_list::ensureTag($tagger, $tag, $desc, $private);
 
             if ($profile_list->taggedCount() >= common_config('peopletag', 'maxpeople')) {
-                // TRANS: Client exception thrown trying to set one tag for more people than allowed.
-                throw new ClientException(sprintf(_('You already have %1$d or more people tagged %2$s, ' .
-                                                    'which is the maximum allowed number.' .
-                                                    'Try untagging others with the same tag first.'),
+                // TRANS: Client exception thrown when trying to add more people than allowed to a list.
+                throw new ClientException(sprintf(_('You already have %1$d or more people in list %2$s, ' .
+                                                    'which is the maximum allowed number. ' .
+                                                    'Try unlisting others first.'),
                                                     common_config('peopletag', 'maxpeople'), $tag));
                 return false;
             }
@@ -224,7 +244,9 @@ class Profile_tag extends Memcached_DataObject
             Event::handle('EndUntagProfile', array($orig));
             if ($result) {
                 $profile_list = Profile_list::pkeyGet(array('tag' => $tag, 'tagger' => $tagger));
-                $profile_list->taggedCount(true);
+                if (!empty($profile_list)) {
+                    $profile_list->taggedCount(true);
+                }
                 self::blowCaches($tagger, $tagged);
                 return true;
             }
@@ -258,8 +280,11 @@ class Profile_tag extends Memcached_DataObject
                'tag = "%s", tagger = "%s" ' .
                'WHERE tag = "%s" ' .
                'AND tagger = "%s"';
-        $result = $tags->query(sprintf($qry, $new->tag, $new->tagger,
-                                             $orig->tag, $orig->tagger));
+        $result = $tags->query(sprintf($qry,
+                                       $tags->escape($new->tag),
+                                       $tags->escape($new->tagger),
+                                       $tags->escape($orig->tag),
+                                       $tags->escape($orig->tagger)));
 
         if (!$result) {
             common_log_db_error($tags, 'UPDATE', __FILE__);
@@ -281,12 +306,34 @@ class Profile_tag extends Memcached_DataObject
         $profile->query('SELECT profile.* ' .
                         'FROM profile JOIN profile_tag ' .
                         'ON profile.id = profile_tag.tagged ' .
-                        'WHERE profile_tag.tagger = ' . $tagger . ' ' .
-                        'AND profile_tag.tag = "' . $tag . '" ');
+                        'WHERE profile_tag.tagger = ' . $profile->escape($tagger) . ' ' .
+                        'AND profile_tag.tag = "' . $profile->escape($tag) . '" ');
         $tagged = array();
         while ($profile->fetch()) {
             $tagged[] = clone($profile);
         }
         return true;
     }
+
+    function insert()
+    {
+        $result = parent::insert();
+        if ($result) {
+            self::blow('profile_list:tagged_count:%d:%s', 
+                       $this->tagger,
+                       $this->tag);
+        }
+        return $result;
+    }
+
+    function delete()
+    {
+        $result = parent::delete();
+        if ($result) {
+            self::blow('profile_list:tagged_count:%d:%s', 
+                       $this->tagger,
+                       $this->tag);
+        }
+        return $result;
+    }
 }