atomnoticefeed.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. <?php
  2. /**
  3. * StatusNet, the distributed open-source microblogging tool
  4. *
  5. * Class for building an Atom feed from a collection of notices
  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 2010 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. * Class for creating a feed that represents a collection of notices. Builds the
  35. * feed in memory. Get the feed as a string with AtomNoticeFeed::getString().
  36. *
  37. * @category Feed
  38. * @package StatusNet
  39. * @author Zach Copley <zach@status.net>
  40. * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
  41. * @link http://status.net/
  42. */
  43. class AtomNoticeFeed extends Atom10Feed
  44. {
  45. var $cur;
  46. protected $scoped=null;
  47. /**
  48. * Constructor - adds a bunch of XML namespaces we need in our
  49. * notice-specific Atom feeds, and allows setting the current
  50. * authenticated user (useful for API methods).
  51. *
  52. * @param User $cur the current authenticated user (optional)
  53. * @param boolean $indent Whether to indent XML output
  54. *
  55. */
  56. function __construct($cur = null, $indent = true) {
  57. parent::__construct($indent);
  58. $this->cur = $cur ?: common_current_user();
  59. $this->scoped = !is_null($this->cur) ? $this->cur->getProfile() : null;
  60. // Feeds containing notice info use these namespaces
  61. $this->addNamespace(
  62. 'thr',
  63. 'http://purl.org/syndication/thread/1.0'
  64. );
  65. $this->addNamespace(
  66. 'georss',
  67. 'http://www.georss.org/georss'
  68. );
  69. $this->addNamespace(
  70. 'activity',
  71. 'http://activitystrea.ms/spec/1.0/'
  72. );
  73. $this->addNamespace(
  74. 'media',
  75. 'http://purl.org/syndication/atommedia'
  76. );
  77. $this->addNamespace(
  78. 'poco',
  79. 'http://portablecontacts.net/spec/1.0'
  80. );
  81. // XXX: What should the uri be?
  82. $this->addNamespace(
  83. 'ostatus',
  84. 'http://ostatus.org/schema/1.0'
  85. );
  86. $this->addNamespace(
  87. 'statusnet',
  88. 'http://status.net/schema/api/1/'
  89. );
  90. }
  91. /**
  92. * Add more than one Notice to the feed
  93. *
  94. * @param mixed $notices an array of Notice objects or handle
  95. *
  96. */
  97. function addEntryFromNotices($notices)
  98. {
  99. if (is_array($notices)) {
  100. foreach ($notices as $notice) {
  101. $this->addEntryFromNotice($notice);
  102. }
  103. } elseif ($notices instanceof Notice) {
  104. while ($notices->fetch()) {
  105. $this->addEntryFromNotice($notices);
  106. }
  107. } else {
  108. throw new ServerException('addEntryFromNotices got neither an array nor a Notice object');
  109. }
  110. }
  111. /**
  112. * Add a single Notice to the feed
  113. *
  114. * @param Notice $notice a Notice to add
  115. */
  116. function addEntryFromNotice(Notice $notice)
  117. {
  118. try {
  119. $source = $this->showSource();
  120. $author = $this->showAuthor();
  121. $this->addEntryRaw($notice->asAtomEntry(false, $source, $author, $this->scoped));
  122. } catch (Exception $e) {
  123. common_log(LOG_ERR, $e->getMessage());
  124. // we continue on exceptions
  125. }
  126. }
  127. function showSource()
  128. {
  129. return true;
  130. }
  131. function showAuthor()
  132. {
  133. return true;
  134. }
  135. }