]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - lib/activitystreamjsondocument.php
Reinstate profile_info in author/actor
[quix0rs-gnu-social.git] / lib / activitystreamjsondocument.php
1 <?php
2 /**
3  * StatusNet, the distributed open-source microblogging tool
4  *
5  * Class for serializing Activity Streams in JSON
6  *
7  * PHP version 5
8  *
9  * LICENCE: This program is free software: you can redistribute it and/or modify
10  * it under the terms of the GNU Affero General Public License as published by
11  * the Free Software Foundation, either version 3 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU Affero General Public License for more details.
18  *
19  * You should have received a copy of the GNU Affero General Public License
20  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
21  *
22  * @category  Feed
23  * @package   StatusNet
24  * @author    Zach Copley <zach@status.net>
25  * @copyright 2011 StatusNet, Inc.
26  * @license   http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
27  * @link      http://status.net/
28  */
29
30 if (!defined('STATUSNET'))
31 {
32     exit(1);
33 }
34
35 /**
36  * A class for generating JSON documents that represent an Activity Streams
37  *
38  * @category Feed
39  * @package  StatusNet
40  * @author   Zach Copley <zach@status.net>
41  * @license  http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
42  * @link     http://status.net/
43  */
44 class ActivityStreamJSONDocument
45 {
46
47     /* Top level array representing the document */
48     protected $doc = array();
49
50     /* The current authenticated user */
51     protected $cur = null;
52
53     /**
54      * Constructor
55      *
56      * @param User $cur the current authenticated user
57      */
58
59     function __construct($cur = null)
60     {
61
62         $this->cur = $cur;
63
64         /* Title of the JSON document */
65         $this->doc['title'] = null;
66
67         /* Array of activity items */
68         $this->doc['items'] = array();
69
70         /* Array of links associated with the document */
71         $this->doc['links'] = array();
72
73     }
74
75     /**
76      * Set the title of the document
77      *
78      * @param String $title the title
79      */
80
81     function setTitle($title)
82     {
83         $this->doc['title'] = $title;
84     }
85
86     /**
87      * Add more than one Item to the document
88      *
89      * @param mixed $notices an array of Notice objects or handle
90      *
91      */
92
93     function addItemsFromNotices($notices)
94     {
95         if (is_array($notices)) {
96             foreach ($notices as $notice) {
97                 $this->addItemFromNotice($notice);
98             }
99         } else {
100             while ($notices->fetch()) {
101                 $this->addItemFromNotice($notices);
102             }
103         }
104     }
105
106     /**
107      * Add a single Notice to the document
108      *
109      * @param Notice $notice a Notice to add
110      */
111
112     function addItemFromNotice($notice)
113     {
114         $cur = empty($this->cur) ? common_current_user() : $this->cur;
115
116         $act          = $notice->asActivity($cur);
117         $act->extra[] = $notice->noticeInfo($cur);
118
119         array_push($this->doc['items'], $act->asArray());
120     }
121
122     /**
123      * Add a link to the JSON document
124      *
125      * @param string $url the URL for the link
126      * @param string $rel the link relationship
127      */
128     function addLink($url = null, $rel = null, $mediaType = null)
129     {
130         $link = new ActivityStreamsLink($url, $rel, $mediaType);
131         $this->doc['link'][] = $link->asArray();
132     }
133
134     /*
135      * Return the entire document as a big string of JSON
136      *
137      * @return string encoded JSON output
138      */
139     function asString()
140     {
141         return json_encode(array_filter($this->doc));
142     }
143
144 }
145
146 /**
147  * A class for representing MediaLinks in JSON Activities
148  *
149  * @category Feed
150  * @package  StatusNet
151  * @author   Zach Copley <zach@status.net>
152  * @license  http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
153  * @link     http://status.net/
154  */
155
156 class ActivityStreamsMediaLink extends ActivityStreamsLink
157 {
158     private $linkDict;
159
160     function __construct(
161         $url       = null,
162         $width     = null,
163         $height    = null,
164         $mediaType = null,
165         $rel       = null,
166         $duration  = null
167     )
168     {
169         parent::__construct($url, $rel, $mediaType);
170         $this->linkDict = array(
171             'width'      => $width,
172             'height'     => $height,
173             'duration'   => $duration
174         );
175     }
176
177     function asArray()
178     {
179         return array_merge(
180             parent::asArray(),
181             array_filter($this->linkDict)
182         );
183     }
184 }
185
186 /**
187  * A class for representing links in JSON Activities
188  *
189  * @category Feed
190  * @package  StatusNet
191  * @author   Zach Copley <zach@status.net>
192  * @license  http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
193  * @link     http://status.net/
194  */
195
196 class ActivityStreamsLink
197 {
198     private $linkDict;
199
200     function __construct($url = null, $rel = null, $mediaType = null)
201     {
202         // links MUST have a URL
203         if (empty($url)) {
204             throw new Exception('Links must have a URL.');
205         }
206
207         $this->linkDict = array(
208             'url'   => $url,
209             'rel'   => $rel,      // extension
210             'type'  => $mediaType // extension
211         );
212     }
213
214     function asArray()
215     {
216         return array_filter($this->linkDict);
217     }
218 }