]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - lib/activitystreamjsondocument.php
Merge commit 'refs/merge-requests/41' of https://gitorious.org/social/mainline into...
[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 extends JSONActivityCollection
45 {
46     // Note: Lot of AS folks think the content type should be:
47     // 'application/stream+json; charset=utf-8', but this is more
48     // useful at the moment, because some programs actually understand
49     // it.
50     const CONTENT_TYPE = 'application/json; charset=utf-8';
51
52     /* Top level array representing the document */
53     protected $doc = array();
54
55     /* The current authenticated user */
56     protected $cur;
57     protected $scoped = null;
58
59     /* Title of the document */
60     protected $title;
61
62     /* Links associated with this document */
63     protected $links;
64
65     /* Count of items in this document */
66     // XXX This is cryptically referred to in the spec: "The Stream serialization MAY contain a count property."
67     protected $count;
68
69     /**
70      * Constructor
71      *
72      * @param User $cur the current authenticated user
73      */
74
75     function __construct($cur = null, $title = null, $items = null, $links = null, $url = null)
76     {
77         parent::__construct($items, $url);
78
79         $this->cur = $cur ?: common_current_user();
80         $this->scoped = !is_null($this->cur) ? $this->cur->getProfile() : null;
81
82         /* Title of the JSON document */
83         $this->title = $title;
84
85         if (!empty($items)) {
86             $this->count = count($this->items);
87         }
88
89         /* Array of links associated with the document */
90         $this->links = empty($links) ? array() : $items;
91
92         /* URL of a document, this document? containing a list of all the items in the stream */
93         if (!empty($this->url)) {
94             $this->url = $this->url;
95         }
96     }
97
98     /**
99      * Set the title of the document
100      *
101      * @param String $title the title
102      */
103
104     function setTitle($title)
105     {
106         $this->title = $title;
107     }
108
109     function setUrl($url)
110     {
111         $this->url = $url;
112     }
113
114
115     /**
116      * Add more than one Item to the document
117      *
118      * @param mixed $notices an array of Notice objects or handle
119      *
120      */
121
122     function addItemsFromNotices($notices)
123     {
124         if (is_array($notices)) {
125             foreach ($notices as $notice) {
126                 $this->addItemFromNotice($notice);
127             }
128         } else {
129             while ($notices->fetch()) {
130                 $this->addItemFromNotice($notices);
131             }
132         }
133     }
134
135     /**
136      * Add a single Notice to the document
137      *
138      * @param Notice $notice a Notice to add
139      */
140
141     function addItemFromNotice($notice)
142     {
143         $act          = $notice->asActivity($this->scoped);
144         $act->extra[] = $notice->noticeInfo($this->scoped);
145         array_push($this->items, $act->asArray());
146         $this->count++;
147     }
148
149     /**
150      * Add a link to the JSON document
151      *
152      * @param string $url the URL for the link
153      * @param string $rel the link relationship
154      */
155     function addLink($url = null, $rel = null, $mediaType = null)
156     {
157         $link = new ActivityStreamsLink($url, $rel, $mediaType);
158         array_push($this->links, $link->asArray());
159     }
160
161     /*
162      * Return the entire document as a big string of JSON
163      *
164      * @return string encoded JSON output
165      */
166     function asString()
167     {
168         $this->doc['generator'] = 'GNU social ' . GNUSOCIAL_VERSION; // extension
169         $this->doc['title'] = $this->title;
170         $this->doc['url']   = $this->url;
171         $this->doc['totalItems'] = $this->count;
172         $this->doc['items'] = $this->items;
173         $this->doc['links'] = $this->links; // extension
174         return json_encode(array_filter($this->doc)); // filter out empty elements
175     }
176
177 }
178
179 /**
180  * A class for representing MediaLinks in JSON Activities
181  *
182  * @category Feed
183  * @package  StatusNet
184  * @author   Zach Copley <zach@status.net>
185  * @license  http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
186  * @link     http://status.net/
187  */
188
189 class ActivityStreamsMediaLink extends ActivityStreamsLink
190 {
191     private $linkDict;
192
193     function __construct(
194         $url       = null,
195         $width     = null,
196         $height    = null,
197         $mediaType = null, // extension
198         $rel       = null, // extension
199         $duration  = null
200     )
201     {
202         parent::__construct($url, $rel, $mediaType);
203         $this->linkDict = array(
204             'width'      => intval($width),
205             'height'     => intval($height),
206             'duration'   => intval($duration)
207         );
208     }
209
210     function asArray()
211     {
212         return array_merge(
213             parent::asArray(),
214             array_filter($this->linkDict)
215         );
216     }
217 }
218
219 /*
220  * Collection primarily as the root of an Activity Streams doc but can be used as the value
221  * of extension properties in a variety of situations.
222  *
223  * A valid Collection object serialization MUST contain at least the url or items properties.
224  */
225 class JSONActivityCollection {
226
227     /* Non-negative integer specifying the total number of activities within the stream */
228     protected $totalItems;
229
230     /* An array containing a listing of Objects of any object type */
231     protected $items;
232
233     /* IRI referencing a JSON document containing the full listing of objects in the collection */
234     protected $url;
235
236     /**
237      * Constructor
238      *
239      * @param array  $items       array of activity items
240      * @param string $url         url of a doc list all the objs in the collection
241      * @param int    $totalItems  total number of items in the collection
242      */
243     function __construct($items = null, $url = null)
244     {
245         $this->items      = empty($items) ? array() : $items;
246         $this->totalItems = count($items);
247         $this->url        = $url;
248     }
249
250     /**
251      * Get the total number of items in the collection
252      *
253      * @return int total the total
254      */
255     public function getTotalItems()
256     {
257         $this->totalItems = count($items);
258         return $this->totalItems;
259     }
260 }
261
262 /**
263  * A class for representing links in JSON Activities
264  *
265  * @category Feed
266  * @package  StatusNet
267  * @author   Zach Copley <zach@status.net>
268  * @license  http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
269  * @link     http://status.net/
270  */
271
272 class ActivityStreamsLink
273 {
274     private $linkDict;
275
276     function __construct($url = null, $rel = null, $mediaType = null)
277     {
278         // links MUST have a URL
279         if (empty($url)) {
280             throw new Exception('Links must have a URL.');
281         }
282
283         $this->linkDict = array(
284             'url'   => $url,
285             'rel'   => $rel,      // extension
286             'type'  => $mediaType // extension
287         );
288     }
289
290     function asArray()
291     {
292         return array_filter($this->linkDict);
293     }
294 }
295