activitystreamjsondocument.php 8.0 KB

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