activitystreamjsondocument.php 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  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. }