activitystreamjsondocument.php 5.0 KB

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