]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - lib/activitystreamjsondocument.php
lib/ping.php - Fix PHP 7.3 Warning switch continue -> break
[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('GNUSOCIAL')) { exit(1); }
31
32 /**
33  * A class for generating JSON documents that represent an Activity Streams
34  *
35  * @category Feed
36  * @package  StatusNet
37  * @author   Zach Copley <zach@status.net>
38  * @license  http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
39  * @link     http://status.net/
40  */
41 class ActivityStreamJSONDocument extends JSONActivityCollection
42 {
43     // Note: Lot of AS folks think the content type should be:
44     // 'application/stream+json; charset=utf-8', but this is more
45     // useful at the moment, because some programs actually understand
46     // it.
47     const CONTENT_TYPE = 'application/json; charset=utf-8';
48
49     /* Top level array representing the document */
50     protected $doc = array();
51
52     /* The current authenticated user */
53     protected $cur;
54     protected $scoped = null;
55
56     /* Title of the document */
57     protected $title;
58
59     /* Links associated with this document */
60     protected $links;
61
62     /* Count of items in this document */
63     // XXX This is cryptically referred to in the spec: "The Stream serialization MAY contain a count property."
64     protected $count;
65
66     /**
67      * Constructor
68      *
69      * @param User $cur the current authenticated user
70      */
71
72     function __construct($cur = null, $title = null, array $items=[], $links = null, $url = null)
73     {
74         parent::__construct($items, $url);
75
76         $this->cur = $cur ?: common_current_user();
77         $this->scoped = !is_null($this->cur) ? $this->cur->getProfile() : null;
78
79         /* Title of the JSON document */
80         $this->title = $title;
81
82         if (!empty($items)) {
83             $this->count = count($this->items);
84         }
85
86         /* Array of links associated with the document */
87         $this->links = empty($links) ? array() : $items;
88
89         /* URL of a document, this document? containing a list of all the items in the stream */
90         if (!empty($this->url)) {
91             $this->url = $this->url;
92         }
93     }
94
95     /**
96      * Set the title of the document
97      *
98      * @param String $title the title
99      */
100
101     function setTitle($title)
102     {
103         $this->title = $title;
104     }
105
106     function setUrl($url)
107     {
108         $this->url = $url;
109     }
110
111
112     /**
113      * Add more than one Item to the document
114      *
115      * @param mixed $notices an array of Notice objects or handle
116      *
117      */
118
119     function addItemsFromNotices($notices)
120     {
121         if (is_array($notices)) {
122             foreach ($notices as $notice) {
123                 $this->addItemFromNotice($notice);
124             }
125         } else {
126             while ($notices->fetch()) {
127                 $this->addItemFromNotice($notices);
128             }
129         }
130     }
131
132     /**
133      * Add a single Notice to the document
134      *
135      * @param Notice $notice a Notice to add
136      */
137
138     function addItemFromNotice($notice)
139     {
140         $act          = $notice->asActivity($this->scoped);
141         $act->extra[] = $notice->noticeInfo($this->scoped);
142         array_push($this->items, $act->asArray());
143         $this->count++;
144     }
145
146     /**
147      * Add a link to the JSON document
148      *
149      * @param string $url the URL for the link
150      * @param string $rel the link relationship
151      */
152     function addLink($url = null, $rel = null, $mediaType = null)
153     {
154         $link = new ActivityStreamsLink($url, $rel, $mediaType);
155         array_push($this->links, $link->asArray());
156     }
157
158     /*
159      * Return the entire document as a big string of JSON
160      *
161      * @return string encoded JSON output
162      */
163     function asString()
164     {
165         $this->doc['generator'] = 'GNU social ' . GNUSOCIAL_VERSION; // extension
166         $this->doc['title'] = $this->title;
167         $this->doc['url']   = $this->url;
168         $this->doc['totalItems'] = $this->count;
169         $this->doc['items'] = $this->items;
170         $this->doc['links'] = $this->links; // extension
171         return json_encode(array_filter($this->doc)); // filter out empty elements
172     }
173
174 }