X-Git-Url: https://git.mxchange.org/?a=blobdiff_plain;f=classes%2FProfile_list.php;h=000e10b41f5fb7cc598c7c7a2d7ffa37378e98ed;hb=e6150323317fb5c37e54a1ac3637b0dfa87038c6;hp=df7bef520198447f345e09c4c6bc4cd4597349b0;hpb=ced00cf0bdd4f119e2126e2eba18a3df7ee45c11;p=quix0rs-gnu-social.git diff --git a/classes/Profile_list.php b/classes/Profile_list.php index df7bef5201..000e10b41f 100644 --- a/classes/Profile_list.php +++ b/classes/Profile_list.php @@ -21,20 +21,10 @@ * @license GNU Affero General Public License http://www.gnu.org/licenses/ */ -if (!defined('STATUSNET') && !defined('LACONICA')) { - exit(1); -} - -/** - * Table Definition for profile_list - */ -require_once INSTALLDIR.'/classes/Memcached_DataObject.php'; +if (!defined('GNUSOCIAL')) { exit(1); } -class Profile_list extends Memcached_DataObject +class Profile_list extends Managed_DataObject { - ###START_AUTOCODE - /* the code below is auto generated do not remove the above tag */ - public $__table = 'profile_list'; // table name public $id; // int(4) primary_key not_null public $tagger; // int(4) @@ -43,28 +33,44 @@ class Profile_list extends Memcached_DataObject public $private; // tinyint(1) public $created; // datetime not_null default_0000-00-00%2000%3A00%3A00 public $modified; // timestamp not_null default_CURRENT_TIMESTAMP - public $uri; // varchar(255) unique_key - public $mainpage; // varchar(255) + public $uri; // varchar(191) unique_key not 255 because utf8mb4 takes more space + public $mainpage; // varchar(191) not 255 because utf8mb4 takes more space public $tagged_count; // smallint public $subscriber_count; // smallint - /* Static get */ - function staticGet($k,$v=NULL) { return DB_DataObject::staticGet('Profile_list',$k,$v); } - - /* the code above is auto generated do not remove the tag below */ - ###END_AUTOCODE - - /** - * return a profile_list record, given its tag and tagger. - * - * @param array $kv ideally array('tag' => $tag, 'tagger' => $tagger) - * - * @return Profile_list a Profile_list object with the given tag and tagger. - */ - - function pkeyGet($kv) + public static function schemaDef() { - return Memcached_DataObject::pkeyGet('Profile_list', $kv); + return array( + 'fields' => array( + 'id' => array('type' => 'serial', 'not null' => true, 'description' => 'unique identifier'), + 'tagger' => array('type' => 'int', 'not null' => true, 'description' => 'user making the tag'), + 'tag' => array('type' => 'varchar', 'length' => 64, 'not null' => true, 'description' => 'people tag'), + 'description' => array('type' => 'text', 'description' => 'description of the people tag'), + 'private' => array('type' => 'int', 'size' => 'tiny', 'default' => 0, 'description' => 'is this tag private'), + + 'created' => array('type' => 'timestamp', 'not null' => true, 'description' => 'date the tag was added'), + 'modified' => array('type' => 'timestamp', 'not null' => true, 'description' => 'date the tag was modified'), + + 'uri' => array('type' => 'varchar', 'length' => 191, 'description' => 'universal identifier'), + 'mainpage' => array('type' => 'varchar', 'length' => 191, 'description' => 'page to link to'), + 'tagged_count' => array('type' => 'int', 'default' => 0, 'description' => 'number of people tagged with this tag by this user'), + 'subscriber_count' => array('type' => 'int', 'default' => 0, 'description' => 'number of subscribers to this tag'), + ), + 'primary key' => array('tagger', 'tag'), + 'unique keys' => array( + 'profile_list_id_key' => array('id') + ), + 'foreign keys' => array( + 'profile_list_tagger_fkey' => array('profile', array('tagger' => 'id')), + ), + 'indexes' => array( + 'profile_list_modified_idx' => array('modified'), + 'profile_list_tag_idx' => array('tag'), + 'profile_list_tagger_tag_idx' => array('tagger', 'tag'), + 'profile_list_tagged_count_idx' => array('tagged_count'), + 'profile_list_subscriber_count_idx' => array('subscriber_count'), + ), + ); } /** @@ -75,7 +81,7 @@ class Profile_list extends Memcached_DataObject function getTagger() { - return Profile::staticGet('id', $this->tagger); + return Profile::getByID($this->tagger); } /** @@ -126,7 +132,7 @@ class Profile_list extends Memcached_DataObject $url = $this->mainpage; } else { $url = common_local_url('showprofiletag', - array('tagger' => $this->getTagger()->nickname, + array('nickname' => $this->getTagger()->nickname, 'tag' => $this->tag)); } } @@ -165,57 +171,9 @@ class Profile_list extends Memcached_DataObject function getNotices($offset, $limit, $since_id=null, $max_id=null) { - $ids = Notice::stream(array($this, '_streamDirect'), - array(), - 'profile_tag:notice_ids:' . $this->id, - $offset, $limit, $since_id, $max_id); - - return Notice::getStreamByIds($ids); - } - - /** - * Query notices by users associated with this tag from the database. - * - * @param integer $offset offset - * @param integer $limit maximum no of results - * @param integer $since_id=null since this id - * @param integer $max_id=null maximum id in result - * - * @return array array of notice ids. - */ - - function _streamDirect($offset, $limit, $since_id, $max_id) - { - $inbox = new Profile_tag_inbox(); - - $inbox->profile_tag_id = $this->id; - - $inbox->selectAdd(); - $inbox->selectAdd('notice_id'); - - if ($since_id != 0) { - $inbox->whereAdd('notice_id > ' . $since_id); - } - - if ($max_id != 0) { - $inbox->whereAdd('notice_id <= ' . $max_id); - } - - $inbox->orderBy('notice_id DESC'); + $stream = new PeopletagNoticeStream($this); - if (!is_null($offset)) { - $inbox->limit($offset, $limit); - } - - $ids = array(); - - if ($inbox->find()) { - while ($inbox->fetch()) { - $ids[] = $inbox->notice_id; - } - } - - return $ids; + return $stream->getNotices($offset, $limit, $since_id, $max_id); } /** @@ -233,10 +191,12 @@ class Profile_list extends Memcached_DataObject function getSubscribers($offset=0, $limit=null, $since=0, $upto=0) { $subs = new Profile(); - $sub = new Profile_tag_subscription(); - $sub->profile_tag_id = $this->id; - $subs->joinAdd($sub); + $subs->joinAdd( + array('id', 'profile_tag_subscription:profile_id') + ); + $subs->whereAdd('profile_tag_subscription.profile_tag_id = ' . $this->id); + $subs->selectAdd('unix_timestamp(profile_tag_subscription.' . 'created) as "cursor"'); @@ -361,7 +321,7 @@ class Profile_list extends Memcached_DataObject * @return boolean success */ - function delete() + function delete($useWhere=false) { // force delete one item at a time. if (empty($this->id)) { @@ -374,41 +334,43 @@ class Profile_list extends Memcached_DataObject Profile_tag::cleanup($this); Profile_tag_subscription::cleanup($this); - return parent::delete(); + self::blow('profile:lists:%d', $this->tagger); + + return parent::delete($useWhere); } /** * Update a people tag gracefully * also change "tag" fields in profile_tag table * - * @param Profile_list $orig Object's original form + * @param Profile_list $dataObject Object's original form * * @return boolean success */ - function update($orig=null) + function update($dataObject=false) { - $result = true; - - if (!is_object($orig) && !$orig instanceof Profile_list) { - parent::update($orig); + if (!is_object($dataObject) && !$dataObject instanceof Profile_list) { + return parent::update($dataObject); } + $result = true; + // if original tag was different // check to see if the new tag already exists // if not, rename the tag correctly - if($orig->tag != $this->tag || $orig->tagger != $this->tagger) { + if($dataObject->tag != $this->tag || $dataObject->tagger != $this->tagger) { $existing = Profile_list::getByTaggerAndTag($this->tagger, $this->tag); if(!empty($existing)) { + // TRANS: Server exception. throw new ServerException(_('The tag you are trying to rename ' . 'to already exists.')); } // move the tag // XXX: allow OStatus plugin to send out profile tag - $result = Profile_tag::moveTag($orig, $this); + $result = Profile_tag::moveTag($dataObject, $this); } - parent::update($orig); - return $result; + return parent::update($dataObject); } /** @@ -431,18 +393,6 @@ class Profile_list extends Memcached_DataObject return $xs->getString(); } - /** - * return an xml string to represent this people tag - * as the subject of an activitystreams feed. - * - * @return string activitystreams subject - */ - - function asActivitySubject() - { - return $this->asActivityNoun('subject'); - } - /** * return an xml string to represent this people tag * as a noun in an activitystreams feed. @@ -469,18 +419,24 @@ class Profile_list extends Memcached_DataObject function taggedCount($recount=false) { - if (!$recount) { - return $this->tagged_count; - } + $keypart = sprintf('profile_list:tagged_count:%d:%s', + $this->tagger, + $this->tag); + + $count = self::cacheGet($keypart); + + if ($count === false) { + $tags = new Profile_tag(); - $tags = new Profile_tag(); - $tags->tag = $this->tag; - $tags->tagger = $this->tagger; - $orig = clone($this); - $this->tagged_count = (int) $tags->count('distinct tagged'); - $this->update($orig); + $tags->tag = $this->tag; + $tags->tagger = $this->tagger; - return $this->tagged_count; + $count = $tags->count('distinct tagged'); + + self::cacheSet($keypart, $count); + } + + return $count; } /** @@ -494,17 +450,38 @@ class Profile_list extends Memcached_DataObject function subscriberCount($recount=false) { - if ($recount) { - return $this->subscriber_count; + $keypart = sprintf('profile_list:subscriber_count:%d', + $this->id); + + $count = self::cacheGet($keypart); + + if ($count === false) { + + $sub = new Profile_tag_subscription(); + $sub->profile_tag_id = $this->id; + $count = (int) $sub->count('distinct profile_id'); + + self::cacheSet($keypart, $count); } - $sub = new Profile_tag_subscription(); - $sub->profile_tag_id = $this->id; - $orig = clone($this); - $this->subscriber_count = (int) $sub->count('distinct profile_id'); - $this->update($orig); + return $count; + } + + /** + * get the cached number of profiles subscribed to this + * people tag, re-count if the argument is true. + * + * @param boolean $recount whether to ignore cache + * + * @return integer count + */ - return $this->subscriber_count; + function blowNoticeStreamCache($all=false) + { + self::blow('profile_list:notice_ids:%d', $this->id); + if ($all) { + self::blow('profile_list:notice_ids:%d;last', $this->id); + } } /** @@ -599,8 +576,7 @@ class Profile_list extends Memcached_DataObject * * @return mixed Profile_list on success, false on fail */ - static function saveNew($fields) { - + static function saveNew(array $fields) { extract($fields); $ptag = new Profile_list(); @@ -608,10 +584,12 @@ class Profile_list extends Memcached_DataObject $ptag->query('BEGIN'); if (empty($tagger)) { + // TRANS: Server exception saving new tag without having a tagger specified. throw new Exception(_('No tagger specified.')); } if (empty($tag)) { + // TRANS: Server exception saving new tag without having a tag specified. throw new Exception(_('No tag specified.')); } @@ -649,6 +627,7 @@ class Profile_list extends Memcached_DataObject if (!$result) { common_log_db_error($ptag, 'INSERT', __FILE__); + // TRANS: Server exception saving new tag. throw new ServerException(_('Could not create profile tag.')); } @@ -658,15 +637,16 @@ class Profile_list extends Memcached_DataObject $result = $ptag->update($orig); if (!$result) { common_log_db_error($ptag, 'UPDATE', __FILE__); + // TRANS: Server exception saving new tag. throw new ServerException(_('Could not set profile tag URI.')); } } if (!isset($mainpage) || empty($mainpage)) { $orig = clone($ptag); - $user = User::staticGet('id', $ptag->tagger); + $user = User::getKV('id', $ptag->tagger); if(!empty($user)) { - $ptag->mainpage = common_local_url('showprofiletag', array('tag' => $ptag->tag, 'tagger' => $user->nickname)); + $ptag->mainpage = common_local_url('showprofiletag', array('tag' => $ptag->tag, 'nickname' => $user->getNickname())); } else { $ptag->mainpage = $uri; // assume this is a remote peopletag and the uri works } @@ -674,6 +654,7 @@ class Profile_list extends Memcached_DataObject $result = $ptag->update($orig); if (!$result) { common_log_db_error($ptag, 'UPDATE', __FILE__); + // TRANS: Server exception saving new tag. throw new ServerException(_('Could not set profile tag mainpage.')); } } @@ -706,7 +687,7 @@ class Profile_list extends Memcached_DataObject // XXX: This should be in Memcached_DataObject... eventually. - static function getAtCursor($fn, $args, $cursor, $count=20) + static function getAtCursor($fn, array $args, $cursor, $count=20) { $items = array(); @@ -879,7 +860,7 @@ class Profile_list extends Memcached_DataObject * @return Profile_list results */ - static function getByKeys($keys) { + static function getByKeys(array $keys) { $cache = Cache::instance(); if (!empty($cache)) { @@ -926,4 +907,13 @@ class Profile_list extends Memcached_DataObject return new ArrayWrapper($wrapped); } } + + function insert() + { + $result = parent::insert(); + if ($result) { + self::blow('profile:lists:%d', $this->tagger); + } + return $result; + } }