]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - classes/Profile_list.php
Merge remote-tracking branch 'mainline/1.0.x' into people_tags_rebase
[quix0rs-gnu-social.git] / classes / Profile_list.php
1 <?php
2 /**
3  * StatusNet - the distributed open-source microblogging tool
4  *
5  * This program is free software: you can redistribute it and/or modify
6  * it under the terms of the GNU Affero General Public License as published by
7  * the Free Software Foundation, either version 3 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.     See the
13  * GNU Affero General Public License for more details.
14  *
15  * You should have received a copy of the GNU Affero General Public License
16  * along with this program.     If not, see <http://www.gnu.org/licenses/>.
17  *
18  * @category Notices
19  * @package  StatusNet
20  * @author   Shashi Gowda <connect2shashi@gmail.com>
21  * @license  GNU Affero General Public License http://www.gnu.org/licenses/
22  */
23
24 if (!defined('STATUSNET') && !defined('LACONICA')) {
25     exit(1);
26 }
27
28 /**
29  * Table Definition for profile_list
30  */
31 require_once INSTALLDIR.'/classes/Memcached_DataObject.php';
32
33 class Profile_list extends Memcached_DataObject
34 {
35     ###START_AUTOCODE
36     /* the code below is auto generated do not remove the above tag */
37
38     public $__table = 'profile_list';                      // table name
39     public $id;                              // int(4)  primary_key not_null
40     public $tagger;                          // int(4)
41     public $tag;                             // varchar(64)
42     public $description;                     // text
43     public $private;                         // tinyint(1)
44     public $created;                         // datetime   not_null default_0000-00-00%2000%3A00%3A00
45     public $modified;                        // timestamp   not_null default_CURRENT_TIMESTAMP
46     public $uri;                             // varchar(255)  unique_key
47     public $mainpage;                        // varchar(255)
48     public $tagged_count;                    // smallint
49     public $subscriber_count;                // smallint
50
51     /* Static get */
52     function staticGet($k,$v=NULL) { return DB_DataObject::staticGet('Profile_list',$k,$v); }
53
54     /* the code above is auto generated do not remove the tag below */
55     ###END_AUTOCODE
56
57     /**
58      * return a profile_list record, given its tag and tagger.
59      *
60      * @param array $kv ideally array('tag' => $tag, 'tagger' => $tagger)
61      *
62      * @return Profile_list a Profile_list object with the given tag and tagger.
63      */
64
65     function pkeyGet($kv)
66     {
67         return Memcached_DataObject::pkeyGet('Profile_list', $kv);
68     }
69
70     /**
71      * get the tagger of this profile_list object
72      *
73      * @return Profile the tagger
74      */
75
76     function getTagger()
77     {
78         return Profile::staticGet('id', $this->tagger);
79     }
80
81     /**
82      * return a string to identify this
83      * profile_list in the user interface etc.
84      *
85      * @return String
86      */
87
88     function getBestName()
89     {
90         return $this->tag;
91     }
92
93     /**
94      * return a uri string for this profile_list
95      *
96      * @return String uri
97      */
98
99     function getUri()
100     {
101         $uri = null;
102         if (Event::handle('StartProfiletagGetUri', array($this, &$uri))) {
103             if (!empty($this->uri)) {
104                 $uri = $this->uri;
105             } else {
106                 $uri = common_local_url('profiletagbyid',
107                                         array('id' => $this->id, 'tagger_id' => $this->tagger));
108             }
109         }
110         Event::handle('EndProfiletagGetUri', array($this, &$uri));
111         return $uri;
112     }
113
114     /**
115      * return a url to the homepage of this item
116      *
117      * @return String home url
118      */
119
120     function homeUrl()
121     {
122         $url = null;
123         if (Event::handle('StartUserPeopletagHomeUrl', array($this, &$url))) {
124             // normally stored in mainpage, but older ones may be null
125             if (!empty($this->mainpage)) {
126                 $url = $this->mainpage;
127             } else {
128                 $url = common_local_url('showprofiletag',
129                                         array('tagger' => $this->getTagger()->nickname,
130                                               'tag'    => $this->tag));
131             }
132         }
133         Event::handle('EndUserPeopletagHomeUrl', array($this, &$url));
134         return $url;
135     }
136
137     /**
138      * return an immutable url for this object
139      *
140      * @return String permalink
141      */
142
143     function permalink()
144     {
145         $url = null;
146         if (Event::handle('StartProfiletagPermalink', array($this, &$url))) {
147             $url = common_local_url('profiletagbyid',
148                                     array('id' => $this->id));
149         }
150         Event::handle('EndProfiletagPermalink', array($this, &$url));
151         return $url;
152     }
153
154     /**
155      * Query notices by users associated with this tag,
156      * but first check the cache before hitting the DB.
157      *
158      * @param integer $offset   offset
159      * @param integer $limit    maximum no of results
160      * @param integer $since_id=null    since this id
161      * @param integer $max_id=null  maximum id in result
162      *
163      * @return Notice the query
164      */
165
166     function getNotices($offset, $limit, $since_id=null, $max_id=null)
167     {
168         $ids = Notice::stream(array($this, '_streamDirect'),
169                               array(),
170                               'profile_tag:notice_ids:' . $this->id,
171                               $offset, $limit, $since_id, $max_id);
172
173         return Notice::getStreamByIds($ids);
174     }
175
176     /**
177      * Query notices by users associated with this tag from the database.
178      *
179      * @param integer $offset   offset
180      * @param integer $limit    maximum no of results
181      * @param integer $since_id=null    since this id
182      * @param integer $max_id=null  maximum id in result
183      *
184      * @return array array of notice ids.
185      */
186
187     function _streamDirect($offset, $limit, $since_id, $max_id)
188     {
189         $inbox = new Profile_tag_inbox();
190
191         $inbox->profile_tag_id = $this->id;
192
193         $inbox->selectAdd();
194         $inbox->selectAdd('notice_id');
195
196         if ($since_id != 0) {
197             $inbox->whereAdd('notice_id > ' . $since_id);
198         }
199
200         if ($max_id != 0) {
201             $inbox->whereAdd('notice_id <= ' . $max_id);
202         }
203
204         $inbox->orderBy('notice_id DESC');
205
206         if (!is_null($offset)) {
207             $inbox->limit($offset, $limit);
208         }
209
210         $ids = array();
211
212         if ($inbox->find()) {
213             while ($inbox->fetch()) {
214                 $ids[] = $inbox->notice_id;
215             }
216         }
217
218         return $ids;
219     }
220
221     /**
222      * Get subscribers (local and remote) to this people tag
223      * Order by reverse chronology
224      *
225      * @param integer $offset   offset
226      * @param integer $limit    maximum no of results
227      * @param integer $since_id=null    since unix timestamp
228      * @param integer $upto=null  maximum unix timestamp when subscription was made
229      *
230      * @return Profile results
231      */
232
233     function getSubscribers($offset=0, $limit=null, $since=0, $upto=0)
234     {
235         $subs = new Profile();
236         $sub = new Profile_tag_subscription();
237         $sub->profile_tag_id = $this->id;
238
239         $subs->joinAdd($sub);
240         $subs->selectAdd('unix_timestamp(profile_tag_subscription.' .
241                          'created) as "cursor"');
242
243         if ($since != 0) {
244             $subs->whereAdd('cursor > ' . $since);
245         }
246
247         if ($upto != 0) {
248             $subs->whereAdd('cursor <= ' . $upto);
249         }
250
251         if ($limit != null) {
252             $subs->limit($offset, $limit);
253         }
254
255         $subs->orderBy('profile_tag_subscription.created DESC');
256         $subs->find();
257
258         return $subs;
259     }
260
261     /**
262      * Get all and only local subscribers to this people tag
263      * used for distributing notices to user inboxes.
264      *
265      * @return array ids of users
266      */
267
268     function getUserSubscribers()
269     {
270         // XXX: cache this
271
272         $user = new User();
273         if(common_config('db','quote_identifiers'))
274             $user_table = '"user"';
275         else $user_table = 'user';
276
277         $qry =
278           'SELECT id ' .
279           'FROM '. $user_table .' JOIN profile_tag_subscription '.
280           'ON '. $user_table .'.id = profile_tag_subscription.profile_id ' .
281           'WHERE profile_tag_subscription.profile_tag_id = %d ';
282
283         $user->query(sprintf($qry, $this->id));
284
285         $ids = array();
286
287         while ($user->fetch()) {
288             $ids[] = $user->id;
289         }
290
291         $user->free();
292
293         return $ids;
294     }
295
296     /**
297      * Check to see if a given profile has
298      * subscribed to this people tag's timeline
299      *
300      * @param mixed $id User or Profile object or integer id
301      *
302      * @return boolean subscription status
303      */
304
305     function hasSubscriber($id)
306     {
307         if (!is_numeric($id)) {
308             $id = $id->id;
309         }
310
311         $sub = Profile_tag_subscription::pkeyGet(array('profile_tag_id' => $this->id,
312                                                        'profile_id'     => $id));
313         return !empty($sub);
314     }
315
316     /**
317      * Get profiles tagged with this people tag,
318      * include modified timestamp as a "cursor" field
319      * order by descending order of modified time
320      *
321      * @param integer $offset   offset
322      * @param integer $limit    maximum no of results
323      * @param integer $since_id=null    since unix timestamp
324      * @param integer $upto=null  maximum unix timestamp when subscription was made
325      *
326      * @return Profile results
327      */
328
329     function getTagged($offset=0, $limit=null, $since=0, $upto=0)
330     {
331         $tagged = new Profile();
332         $tagged->joinAdd(array('id', 'profile_tag:tagged'));
333
334         #@fixme: postgres
335         $tagged->selectAdd('unix_timestamp(profile_tag.modified) as "cursor"');
336         $tagged->whereAdd('profile_tag.tagger = '.$this->tagger);
337         $tagged->whereAdd("profile_tag.tag = '{$this->tag}'");
338
339         if ($since != 0) {
340             $tagged->whereAdd('cursor > ' . $since);
341         }
342
343         if ($upto != 0) {
344             $tagged->whereAdd('cursor <= ' . $upto);
345         }
346
347         if ($limit != null) {
348             $tagged->limit($offset, $limit);
349         }
350
351         $tagged->orderBy('profile_tag.modified DESC');
352         $tagged->find();
353
354         return $tagged;
355     }
356
357     /**
358      * Gracefully delete one or many people tags
359      * along with their members and subscriptions data
360      *
361      * @return boolean success
362      */
363
364     function delete()
365     {
366         // force delete one item at a time.
367         if (empty($this->id)) {
368             $this->find();
369             while ($this->fetch()) {
370                 $this->delete();
371             }
372         }
373
374         Profile_tag::cleanup($this);
375         Profile_tag_subscription::cleanup($this);
376
377         return parent::delete();
378     }
379
380     /**
381      * Update a people tag gracefully
382      * also change "tag" fields in profile_tag table
383      *
384      * @param Profile_list $orig    Object's original form
385      *
386      * @return boolean success
387      */
388
389     function update($orig=null)
390     {
391         $result = true;
392
393         if (!is_object($orig) && !$orig instanceof Profile_list) {
394             parent::update($orig);
395         }
396
397         // if original tag was different
398         // check to see if the new tag already exists
399         // if not, rename the tag correctly
400         if($orig->tag != $this->tag || $orig->tagger != $this->tagger) {
401             $existing = Profile_list::getByTaggerAndTag($this->tagger, $this->tag);
402             if(!empty($existing)) {
403                 throw new ServerException(_('The tag you are trying to rename ' .
404                                             'to already exists.'));
405             }
406             // move the tag
407             // XXX: allow OStatus plugin to send out profile tag
408             $result = Profile_tag::moveTag($orig, $this);
409         }
410         parent::update($orig);
411         return $result;
412     }
413
414     /**
415      * return an xml string representing this people tag
416      * as the author of an atom feed
417      *
418      * @return string atom author element
419      */
420
421     function asAtomAuthor()
422     {
423         $xs = new XMLStringer(true);
424
425         $tagger = $this->getTagger();
426         $xs->elementStart('author');
427         $xs->element('name', null, '@' . $tagger->nickname . '/' . $this->tag);
428         $xs->element('uri', null, $this->permalink());
429         $xs->elementEnd('author');
430
431         return $xs->getString();
432     }
433
434     /**
435      * return an xml string to represent this people tag
436      * as the subject of an activitystreams feed.
437      *
438      * @return string activitystreams subject
439      */
440
441     function asActivitySubject()
442     {
443         return $this->asActivityNoun('subject');
444     }
445
446     /**
447      * return an xml string to represent this people tag
448      * as a noun in an activitystreams feed.
449      *
450      * @param string $element the xml tag
451      *
452      * @return string activitystreams noun
453      */
454
455     function asActivityNoun($element)
456     {
457         $noun = ActivityObject::fromPeopletag($this);
458         return $noun->asString('activity:' . $element);
459     }
460
461     /**
462      * get the cached number of profiles tagged with this
463      * people tag, re-count if the argument is true.
464      *
465      * @param boolean $recount  whether to ignore cache
466      *
467      * @return integer count
468      */
469
470     function taggedCount($recount=false)
471     {
472         if (!$recount) {
473             return $this->tagged_count;
474         }
475
476         $tags = new Profile_tag();
477         $tags->tag = $this->tag;
478         $tags->tagger = $this->tagger;
479         $orig = clone($this);
480         $this->tagged_count = (int) $tags->count('distinct tagged');
481         $this->update($orig);
482
483         return $this->tagged_count;
484     }
485
486     /**
487      * get the cached number of profiles subscribed to this
488      * people tag, re-count if the argument is true.
489      *
490      * @param boolean $recount  whether to ignore cache
491      *
492      * @return integer count
493      */
494
495     function subscriberCount($recount=false)
496     {
497         if ($recount) {
498             return $this->subscriber_count;
499         }
500
501         $sub = new Profile_tag_subscription();
502         $sub->profile_tag_id = $this->id;
503         $orig = clone($this);
504         $this->subscriber_count = (int) $sub->count('distinct profile_id');
505         $this->update($orig);
506
507         return $this->subscriber_count;
508     }
509
510     /**
511      * get the Profile_list object by the
512      * given tagger and with given tag
513      *
514      * @param integer $tagger   the id of the creator profile
515      * @param integer $tag      the tag
516      *
517      * @return integer count
518      */
519
520     static function getByTaggerAndTag($tagger, $tag)
521     {
522         $ptag = Profile_list::pkeyGet(array('tagger' => $tagger, 'tag' => $tag));
523         return $ptag;
524     }
525
526     /**
527      * create a profile_list record for a tag, tagger pair
528      * if it doesn't exist, return it.
529      *
530      * @param integer $tagger   the tagger
531      * @param string  $tag      the tag
532      * @param string  $description description
533      * @param boolean $private  protected or not
534      *
535      * @return Profile_list the people tag object
536      */
537
538     static function ensureTag($tagger, $tag, $description=null, $private=false)
539     {
540         $ptag = Profile_list::getByTaggerAndTag($tagger, $tag);
541
542         if(empty($ptag->id)) {
543             $args = array(
544                 'tag' => $tag,
545                 'tagger' => $tagger,
546                 'description' => $description,
547                 'private' => $private
548             );
549
550             $new_tag = Profile_list::saveNew($args);
551
552             return $new_tag;
553         }
554         return $ptag;
555     }
556
557     /**
558      * get the maximum number of characters
559      * that can be used in the description of
560      * a people tag.
561      *
562      * determined by $config['peopletag']['desclimit']
563      * if not set, falls back to $config['site']['textlimit']
564      *
565      * @return integer maximum number of characters
566      */
567
568     static function maxDescription()
569     {
570         $desclimit = common_config('peopletag', 'desclimit');
571         // null => use global limit (distinct from 0!)
572         if (is_null($desclimit)) {
573             $desclimit = common_config('site', 'textlimit');
574         }
575         return $desclimit;
576     }
577
578     /**
579      * check if the length of given text exceeds
580      * character limit.
581      *
582      * @param string $desc  the description
583      *
584      * @return boolean is the descripition too long?
585      */
586
587     static function descriptionTooLong($desc)
588     {
589         $desclimit = self::maxDescription();
590         return ($desclimit > 0 && !empty($desc) && (mb_strlen($desc) > $desclimit));
591     }
592
593     /**
594      * save a new people tag, this should be always used
595      * since it makes uri, homeurl, created and modified
596      * timestamps and performs checks.
597      *
598      * @param array $fields an array with fields and their values
599      *
600      * @return mixed Profile_list on success, false on fail
601      */
602     static function saveNew($fields) {
603
604         extract($fields);
605
606         $ptag = new Profile_list();
607
608         $ptag->query('BEGIN');
609
610         if (empty($tagger)) {
611             throw new Exception(_('No tagger specified.'));
612         }
613
614         if (empty($tag)) {
615             throw new Exception(_('No tag specified.'));
616         }
617
618         if (empty($mainpage)) {
619             $mainpage = null;
620         }
621
622         if (empty($uri)) {
623             // fill in later...
624             $uri = null;
625         }
626
627         if (empty($mainpage)) {
628             $mainpage = null;
629         }
630
631         if (empty($description)) {
632             $description = null;
633         }
634
635         if (empty($private)) {
636             $private = false;
637         }
638
639         $ptag->tagger      = $tagger;
640         $ptag->tag         = $tag;
641         $ptag->description = $description;
642         $ptag->private     = $private;
643         $ptag->uri         = $uri;
644         $ptag->mainpage    = $mainpage;
645         $ptag->created     = common_sql_now();
646         $ptag->modified    = common_sql_now();
647
648         $result = $ptag->insert();
649
650         if (!$result) {
651             common_log_db_error($ptag, 'INSERT', __FILE__);
652             throw new ServerException(_('Could not create profile tag.'));
653         }
654
655         if (!isset($uri) || empty($uri)) {
656             $orig = clone($ptag);
657             $ptag->uri = common_local_url('profiletagbyid', array('id' => $ptag->id, 'tagger_id' => $ptag->tagger));
658             $result = $ptag->update($orig);
659             if (!$result) {
660                 common_log_db_error($ptag, 'UPDATE', __FILE__);
661                 throw new ServerException(_('Could not set profile tag URI.'));
662             }
663         }
664
665         if (!isset($mainpage) || empty($mainpage)) {
666             $orig = clone($ptag);
667             $user = User::staticGet('id', $ptag->tagger);
668             if(!empty($user)) {
669                 $ptag->mainpage = common_local_url('showprofiletag', array('tag' => $ptag->tag, 'tagger' => $user->nickname));
670             } else {
671                 $ptag->mainpage = $uri; // assume this is a remote peopletag and the uri works
672             }
673
674             $result = $ptag->update($orig);
675             if (!$result) {
676                 common_log_db_error($ptag, 'UPDATE', __FILE__);
677                 throw new ServerException(_('Could not set profile tag mainpage.'));
678             }
679         }
680         return $ptag;
681     }
682
683     /**
684      * get all items at given cursor position for api
685      *
686      * @param callback $fn  a function that takes the following arguments in order:
687      *                      $offset, $limit, $since_id, $max_id
688      *                      and returns a Profile_list object after making the DB query
689      * @param array $args   arguments required for $fn
690      * @param integer $cursor   the cursor
691      * @param integer $count    max. number of results
692      *
693      * Algorithm:
694      * - if cursor is 0, return empty list
695      * - if cursor is -1, get first 21 items, next_cursor = 20th prev_cursor = 0
696      * - if cursor is +ve get 22 consecutive items before starting at cursor
697      *   - return items[1..20] if items[0] == cursor else return items[0..21]
698      *   - prev_cursor = items[1]
699      *   - next_cursor = id of the last item being returned
700      *
701      * - if cursor is -ve get 22 consecutive items after cursor starting at cursor
702      *   - return items[1..20]
703      *
704      * @returns array (array (mixed items), int next_cursor, int previous_cursor)
705      */
706
707      // XXX: This should be in Memcached_DataObject... eventually.
708
709     static function getAtCursor($fn, $args, $cursor, $count=20)
710     {
711         $items = array();
712
713         $since_id = 0;
714         $max_id = 0;
715         $next_cursor = 0;
716         $prev_cursor = 0;
717
718         if($cursor > 0) {
719             // if cursor is +ve fetch $count+2 items before cursor starting at cursor
720             $max_id = $cursor;
721             $fn_args = array_merge($args, array(0, $count+2, 0, $max_id));
722             $list = call_user_func_array($fn, $fn_args);
723             while($list->fetch()) {
724                 $items[] = clone($list);
725             }
726
727             if ((isset($items[0]->cursor) && $items[0]->cursor == $cursor) ||
728                 $items[0]->id == $cursor) {
729                 array_shift($items);
730                 $prev_cursor = isset($items[0]->cursor) ?
731                     -$items[0]->cursor : -$items[0]->id;
732             } else {
733                 if (count($items) > $count+1) {
734                     array_shift($items);
735                 }
736                 // this means the cursor item has been deleted, check to see if there are more
737                 $fn_args = array_merge($args, array(0, 1, $cursor));
738                 $more = call_user_func($fn, $fn_args);
739                 if (!$more->fetch() || empty($more)) {
740                     // no more items.
741                     $prev_cursor = 0;
742                 } else {
743                     $prev_cursor = isset($items[0]->cursor) ?
744                         -$items[0]->cursor : -$items[0]->id;
745                 }
746             }
747
748             if (count($items)==$count+1) {
749                 // this means there is a next page.
750                 $next = array_pop($items);
751                 $next_cursor = isset($next->cursor) ?
752                     $items[$count-1]->cursor : $items[$count-1]->id;
753             }
754
755         } else if($cursor < -1) {
756             // if cursor is -ve fetch $count+2 items created after -$cursor-1
757             $cursor = abs($cursor);
758             $since_id = $cursor-1;
759
760             $fn_args = array_merge($args, array(0, $count+2, $since_id));
761             $list = call_user_func_array($fn, $fn_args);
762             while($list->fetch()) {
763                 $items[] = clone($list);
764             }
765
766             $end = count($items)-1;
767             if ((isset($items[$end]->cursor) && $items[$end]->cursor == $cursor) ||
768                 $items[$end]->id == $cursor) {
769                 array_pop($items);
770                 $next_cursor = isset($items[$end-1]->cursor) ?
771                     $items[$end-1]->cursor : $items[$end-1]->id;
772             } else {
773                 $next_cursor = isset($items[$end]->cursor) ?
774                     $items[$end]->cursor : $items[$end]->id;
775                 if ($end > $count) array_pop($items); // excess item.
776
777                 // check if there are more items for next page
778                 $fn_args = array_merge($args, array(0, 1, 0, $cursor));
779                 $more = call_user_func_array($fn, $fn_args);
780                 if (!$more->fetch() || empty($more)) {
781                     $next_cursor = 0;
782                 }
783             }
784
785             if (count($items) == $count+1) {
786                 // this means there is a previous page.
787                 $prev = array_shift($items);
788                 $prev_cursor = isset($prev->cursor) ?
789                     -$items[0]->cursor : -$items[0]->id;
790             }
791         } else if($cursor == -1) {
792             $fn_args = array_merge($args, array(0, $count+1));
793             $list = call_user_func_array($fn, $fn_args);
794
795             while($list->fetch()) {
796                 $items[] = clone($list);
797             }
798
799             if (count($items)==$count+1) {
800                 $next = array_pop($items);
801                 if(isset($next->cursor)) {
802                     $next_cursor = $items[$count-1]->cursor;
803                 } else {
804                     $next_cursor = $items[$count-1]->id;
805                 }
806             }
807
808         }
809         return array($items, $next_cursor, $prev_cursor);
810     }
811
812     /**
813      * save a collection of people tags into the cache
814      *
815      * @param string $ckey  cache key
816      * @param Profile_list &$tag the results to store
817      * @param integer $offset   offset for slicing results
818      * @param integer $limit    maximum number of results
819      *
820      * @return boolean success
821      */
822
823     static function setCache($ckey, &$tag, $offset=0, $limit=null) {
824         $cache = Cache::instance();
825         if (empty($cache)) {
826             return false;
827         }
828         $str = '';
829         $tags = array();
830         while ($tag->fetch()) {
831             $str .= $tag->tagger . ':' . $tag->tag . ';';
832             $tags[] = clone($tag);
833         }
834         $str = substr($str, 0, -1);
835         if ($offset>=0 && !is_null($limit)) {
836             $tags = array_slice($tags, $offset, $limit);
837         }
838
839         $tag = new ArrayWrapper($tags);
840
841         return self::cacheSet($ckey, $str);
842     }
843
844     /**
845      * get people tags from the cache
846      *
847      * @param string $ckey  cache key
848      * @param integer $offset   offset for slicing
849      * @param integer $limit    limit
850      *
851      * @return Profile_list results
852      */
853
854     static function getCached($ckey, $offset=0, $limit=null) {
855
856         $keys_str = self::cacheGet($ckey);
857         if ($keys_str === false) {
858             return false;
859         }
860
861         $pairs = explode(';', $keys_str);
862         $keys = array();
863         foreach ($pairs as $pair) {
864             $keys[] = explode(':', $pair);
865         }
866
867         if ($offset>=0 && !is_null($limit)) {
868             $keys = array_slice($keys, $offset, $limit);
869         }
870         return self::getByKeys($keys);
871     }
872
873     /**
874      * get Profile_list objects from the database
875      * given their (tag, tagger) key pairs.
876      *
877      * @param array $keys   array of array(tagger, tag)
878      *
879      * @return Profile_list results
880      */
881
882     static function getByKeys($keys) {
883         $cache = Cache::instance();
884
885         if (!empty($cache)) {
886             $tags = array();
887
888             foreach ($keys as $key) {
889                 $t = Profile_list::getByTaggerAndTag($key[0], $key[1]);
890                 if (!empty($t)) {
891                     $tags[] = $t;
892                 }
893             }
894             return new ArrayWrapper($tags);
895         } else {
896             $tag = new Profile_list();
897             if (empty($keys)) {
898                 //if no IDs requested, just return the tag object
899                 return $tag;
900             }
901
902             $pairs = array();
903             foreach ($keys as $key) {
904                 $pairs[] = '(' . $key[0] . ', "' . $key[1] . '")';
905             }
906
907             $tag->whereAdd('(tagger, tag) in (' . implode(', ', $pairs) . ')');
908
909             $tag->find();
910
911             $temp = array();
912
913             while ($tag->fetch()) {
914                 $temp[$tag->tagger.'-'.$tag->tag] = clone($tag);
915             }
916
917             $wrapped = array();
918
919             foreach ($keys as $key) {
920                 $id = $key[0].'-'.$key[1];
921                 if (array_key_exists($id, $temp)) {
922                     $wrapped[] = $temp[$id];
923                 }
924             }
925
926             return new ArrayWrapper($wrapped);
927         }
928     }
929 }