]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - lib/activitystreamjsondocument.php
Merge branch '0.9.x' into json-activities
[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         array_push($this->doc['items'], $act->asArray());
119     }
120
121     /**
122      * Add a link to the JSON document
123      *
124      * @param string $url the URL for the link
125      * @param string $rel the link relationship
126      */
127     function addLink($url = null, $rel = null, $mediaType = null)
128     {
129         $link = new ActivityStreamsLink($url, $rel, $mediaType);
130         $this->doc['link'][] = $link->asArray();
131     }
132
133     /*
134      * Return the entire document as a big string of JSON
135      *
136      * @return string encoded JSON output
137      */
138     function asString()
139     {
140         return json_encode(array_filter($this->doc));
141     }
142
143 }
144
145 /**
146  * A class for representing MediaLinks in JSON Activities
147  *
148  * @category Feed
149  * @package  StatusNet
150  * @author   Zach Copley <zach@status.net>
151  * @license  http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
152  * @link     http://status.net/
153  */
154
155 class ActivityStreamsMediaLink extends ActivityStreamsLink
156 {
157     private $linkDict;
158
159     function __construct(
160         $url       = null,
161         $width     = null,
162         $height    = null,
163         $mediaType = null,
164         $rel       = null,
165         $duration  = null
166     )
167     {
168         parent::__construct($url, $rel, $mediaType);
169         $this->linkDict = array(
170             'width'      => $width,
171             'height'     => $height,
172             'duration'   => $duration
173         );
174     }
175
176     function asArray()
177     {
178         return array_merge(
179             parent::asArray(),
180             array_filter($this->linkDict)
181         );
182     }
183 }
184
185 /**
186  * A class for representing links in JSON Activities
187  *
188  * @category Feed
189  * @package  StatusNet
190  * @author   Zach Copley <zach@status.net>
191  * @license  http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
192  * @link     http://status.net/
193  */
194
195 class ActivityStreamsLink
196 {
197     private $linkDict;
198
199     function __construct($url = null, $rel = null, $mediaType = null)
200     {
201         // links MUST have a URL
202         if (empty($url)) {
203             throw new Exception('Links must have a URL.');
204         }
205
206         $this->linkDict = array(
207             'url'   => $url,
208             'rel'   => $rel,      // extension
209             'type'  => $mediaType // extension
210         );
211     }
212
213     function asArray()
214     {
215         return array_filter($this->linkDict);
216     }
217 }