testfeed.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. #!/usr/bin/env php
  2. <?php
  3. // This file is part of GNU social - https://www.gnu.org/software/social
  4. //
  5. // GNU social is free software: you can redistribute it and/or modify
  6. // it under the terms of the GNU Affero General Public License as published by
  7. // the Free Software Foundation, either version 3 of the License, or
  8. // (at your option) any later version.
  9. //
  10. // GNU social is distributed in the hope that it will be useful,
  11. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. // GNU Affero General Public License for more details.
  14. //
  15. // You should have received a copy of the GNU Affero General Public License
  16. // along with GNU social. If not, see <http://www.gnu.org/licenses/>.
  17. /**
  18. * @package GNUsocial
  19. * @copyright 2010 StatusNet, Inc.
  20. * @license https://www.gnu.org/licenses/agpl.html GNU AGPL v3 or later
  21. */
  22. define('INSTALLDIR', dirname(__DIR__, 3));
  23. define('PUBLICDIR', INSTALLDIR . DIRECTORY_SEPARATOR . 'public');
  24. $longoptions = array('skip=', 'count=');
  25. $helptext = <<<END_OF_HELP
  26. testfeed.php [options] http://example.com/atom-feed-url
  27. Pull an Atom feed and run items in it as though they were live WebSub updates.
  28. Mainly intended for testing funky feed formats.
  29. --skip=N Ignore the first N items in the feed.
  30. --count=N Only process up to N items from the feed, after skipping.
  31. END_OF_HELP;
  32. require_once INSTALLDIR.'/scripts/commandline.inc';
  33. $validate = new Validate();
  34. if (empty($args[0]) || !$validate->uri($args[0])) {
  35. print "$helptext";
  36. exit(1);
  37. }
  38. $feedurl = $args[0];
  39. $skip = have_option('skip') ? intval(get_option_value('skip')) : 0;
  40. $count = have_option('count') ? intval(get_option_value('count')) : 0;
  41. $sub = FeedSub::getKV('uri', $feedurl);
  42. if (!$sub) {
  43. print "Feed $feedurl is not subscribed.\n";
  44. exit(1);
  45. }
  46. // XXX: This could maybe be replaced with $sub->importFeed()
  47. // Fetch the URL
  48. try {
  49. $xml = HTTPClient::quickGet($feedurl, 'application/atom+xml');
  50. } catch (Exception $e) {
  51. echo sprintf("Could not fetch feedurl %s (%d).\n", $e->getMessage(), $e->getCode());
  52. exit(1);
  53. }
  54. $feed = new DOMDocument();
  55. if (!$feed->loadXML($xml)) {
  56. print "Bad XML.\n";
  57. exit(1);
  58. }
  59. if ($skip || $count) {
  60. $entries = $feed->getElementsByTagNameNS(ActivityUtils::ATOM, 'entry');
  61. $remove = array();
  62. for ($i = 0; $i < $skip && $i < $entries->length; $i++) {
  63. $item = $entries->item($i);
  64. if ($item) {
  65. $remove[] = $item;
  66. }
  67. }
  68. if ($count) {
  69. for ($i = $skip + $count; $i < $entries->length; $i++) {
  70. $item = $entries->item($i);
  71. if ($item) {
  72. $remove[] = $item;
  73. }
  74. }
  75. }
  76. foreach ($remove as $item) {
  77. $item->parentNode->removeChild($item);
  78. }
  79. }
  80. echo "Calling event StartFeedSubReceive\n";
  81. Event::handle('StartFeedSubReceive', array($sub, $feed));