123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596 |
- <?php
- define('INSTALLDIR', realpath(dirname(__FILE__) . '/../../..'));
- $longoptions = array('skip=', 'count=');
- $helptext = <<<END_OF_HELP
- testfeed.php [options] http://example.com/atom-feed-url
- Pull an Atom feed and run items in it as though they were live WebSub updates.
- Mainly intended for testing funky feed formats.
- --skip=N Ignore the first N items in the feed.
- --count=N Only process up to N items from the feed, after skipping.
- END_OF_HELP;
- require_once INSTALLDIR.'/scripts/commandline.inc';
- $validate = new Validate();
- if (empty($args[0]) || !$validate->uri($args[0])) {
- print "$helptext";
- exit(1);
- }
- $feedurl = $args[0];
- $skip = have_option('skip') ? intval(get_option_value('skip')) : 0;
- $count = have_option('count') ? intval(get_option_value('count')) : 0;
- $sub = FeedSub::getKV('uri', $feedurl);
- if (!$sub) {
- print "Feed $feedurl is not subscribed.\n";
- exit(1);
- }
- try {
- $xml = HTTPClient::quickGet($feedurl, 'application/atom+xml');
- } catch (Exception $e) {
- echo sprintf("Could not fetch feedurl %s (%d).\n", $e->getMessage(), $e->getCode());
- exit(1);
- }
- $feed = new DOMDocument();
- if (!$feed->loadXML($xml)) {
- print "Bad XML.\n";
- exit(1);
- }
- if ($skip || $count) {
- $entries = $feed->getElementsByTagNameNS(ActivityUtils::ATOM, 'entry');
- $remove = array();
- for ($i = 0; $i < $skip && $i < $entries->length; $i++) {
- $item = $entries->item($i);
- if ($item) {
- $remove[] = $item;
- }
- }
- if ($count) {
- for ($i = $skip + $count; $i < $entries->length; $i++) {
- $item = $entries->item($i);
- if ($item) {
- $remove[] = $item;
- }
- }
- }
- foreach ($remove as $item) {
- $item->parentNode->removeChild($item);
- }
- }
- echo "Calling event StartFeedSubReceive\n";
- Event::handle('StartFeedSubReceive', array($sub, $feed));
|