2010-02-12 14:42:42 +09:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* StatusNet, the distributed open-source microblogging tool
|
|
|
|
*
|
|
|
|
* An activity
|
|
|
|
*
|
|
|
|
* PHP version 5
|
|
|
|
*
|
|
|
|
* LICENCE: This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU Affero General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU Affero General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Affero General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*
|
2010-02-23 10:10:50 +09:00
|
|
|
* @category Feed
|
2010-02-12 14:42:42 +09:00
|
|
|
* @package StatusNet
|
|
|
|
* @author Evan Prodromou <evan@status.net>
|
2010-02-23 10:10:50 +09:00
|
|
|
* @author Zach Copley <zach@status.net>
|
2010-02-12 14:42:42 +09:00
|
|
|
* @copyright 2010 StatusNet, Inc.
|
|
|
|
* @license http://www.fsf.org/licensing/licenses/agpl-3.0.html AGPLv3
|
|
|
|
* @link http://status.net/
|
|
|
|
*/
|
|
|
|
|
|
|
|
if (!defined('STATUSNET')) {
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
2010-02-23 09:46:54 +09:00
|
|
|
class PoCoURL
|
|
|
|
{
|
2010-02-24 15:35:48 +09:00
|
|
|
const URLS = 'urls';
|
2010-02-23 09:46:54 +09:00
|
|
|
const TYPE = 'type';
|
|
|
|
const VALUE = 'value';
|
|
|
|
const PRIMARY = 'primary';
|
|
|
|
|
|
|
|
public $type;
|
|
|
|
public $value;
|
|
|
|
public $primary;
|
|
|
|
|
|
|
|
function __construct($type, $value, $primary = false)
|
|
|
|
{
|
|
|
|
$this->type = $type;
|
|
|
|
$this->value = $value;
|
|
|
|
$this->primary = $primary;
|
|
|
|
}
|
|
|
|
|
|
|
|
function asString()
|
|
|
|
{
|
|
|
|
$xs = new XMLStringer(true);
|
|
|
|
$xs->elementStart('poco:urls');
|
|
|
|
$xs->element('poco:type', null, $this->type);
|
|
|
|
$xs->element('poco:value', null, $this->value);
|
2010-02-24 15:35:48 +09:00
|
|
|
if (!empty($this->primary)) {
|
2010-02-23 09:46:54 +09:00
|
|
|
$xs->element('poco:primary', null, 'true');
|
|
|
|
}
|
|
|
|
$xs->elementEnd('poco:urls');
|
|
|
|
return $xs->getString();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
class PoCoAddress
|
|
|
|
{
|
|
|
|
const ADDRESS = 'address';
|
|
|
|
const FORMATTED = 'formatted';
|
|
|
|
|
|
|
|
public $formatted;
|
|
|
|
|
2010-02-24 15:35:48 +09:00
|
|
|
// @todo Other address fields
|
2010-02-23 09:46:54 +09:00
|
|
|
|
|
|
|
function asString()
|
|
|
|
{
|
2010-02-24 15:35:48 +09:00
|
|
|
if (!empty($this->formatted)) {
|
|
|
|
$xs = new XMLStringer(true);
|
|
|
|
$xs->elementStart('poco:address');
|
2010-03-12 10:12:30 +09:00
|
|
|
$xs->element('poco:formatted', null, common_xml_safe_str($this->formatted));
|
2010-02-24 15:35:48 +09:00
|
|
|
$xs->elementEnd('poco:address');
|
|
|
|
return $xs->getString();
|
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
2010-02-23 09:46:54 +09:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-02-22 21:57:44 +09:00
|
|
|
class PoCo
|
|
|
|
{
|
|
|
|
const NS = 'http://portablecontacts.net/spec/1.0';
|
2010-02-23 09:46:54 +09:00
|
|
|
|
2010-02-24 15:35:48 +09:00
|
|
|
const USERNAME = 'preferredUsername';
|
|
|
|
const DISPLAYNAME = 'displayName';
|
|
|
|
const NOTE = 'note';
|
2010-02-23 09:46:54 +09:00
|
|
|
|
|
|
|
public $preferredUsername;
|
2010-02-24 15:35:48 +09:00
|
|
|
public $displayName;
|
2010-02-23 09:46:54 +09:00
|
|
|
public $note;
|
|
|
|
public $address;
|
|
|
|
public $urls = array();
|
|
|
|
|
2010-02-24 15:35:48 +09:00
|
|
|
function __construct($element = null)
|
2010-02-23 09:46:54 +09:00
|
|
|
{
|
2010-02-24 15:35:48 +09:00
|
|
|
if (empty($element)) {
|
|
|
|
return;
|
|
|
|
}
|
2010-02-23 09:46:54 +09:00
|
|
|
|
2010-02-24 15:35:48 +09:00
|
|
|
$this->preferredUsername = ActivityUtils::childContent(
|
|
|
|
$element,
|
|
|
|
self::USERNAME,
|
|
|
|
self::NS
|
|
|
|
);
|
|
|
|
|
|
|
|
$this->displayName = ActivityUtils::childContent(
|
|
|
|
$element,
|
|
|
|
self::DISPLAYNAME,
|
|
|
|
self::NS
|
|
|
|
);
|
|
|
|
|
|
|
|
$this->note = ActivityUtils::childContent(
|
|
|
|
$element,
|
|
|
|
self::NOTE,
|
|
|
|
self::NS
|
|
|
|
);
|
|
|
|
|
|
|
|
$this->address = $this->_getAddress($element);
|
|
|
|
$this->urls = $this->_getURLs($element);
|
|
|
|
}
|
|
|
|
|
|
|
|
private function _getURLs($element)
|
|
|
|
{
|
|
|
|
$urlEls = $element->getElementsByTagnameNS(self::NS, PoCoURL::URLS);
|
|
|
|
$urls = array();
|
|
|
|
|
|
|
|
foreach ($urlEls as $urlEl) {
|
|
|
|
|
|
|
|
$type = ActivityUtils::childContent(
|
|
|
|
$urlEl,
|
|
|
|
PoCoURL::TYPE,
|
|
|
|
PoCo::NS
|
|
|
|
);
|
|
|
|
|
|
|
|
$value = ActivityUtils::childContent(
|
|
|
|
$urlEl,
|
|
|
|
PoCoURL::VALUE,
|
|
|
|
PoCo::NS
|
|
|
|
);
|
|
|
|
|
|
|
|
$primary = ActivityUtils::childContent(
|
|
|
|
$urlEl,
|
|
|
|
PoCoURL::PRIMARY,
|
|
|
|
PoCo::NS
|
|
|
|
);
|
|
|
|
|
2010-02-25 09:51:24 +09:00
|
|
|
$isPrimary = false;
|
|
|
|
|
|
|
|
if (isset($primary) && $primary == 'true') {
|
|
|
|
$isPrimary = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// @todo check to make sure a primary hasn't already been added
|
|
|
|
|
|
|
|
array_push($urls, new PoCoURL($type, $value, $isPrimary));
|
2010-02-24 15:35:48 +09:00
|
|
|
}
|
|
|
|
return $urls;
|
|
|
|
}
|
|
|
|
|
|
|
|
private function _getAddress($element)
|
|
|
|
{
|
|
|
|
$addressEl = ActivityUtils::child(
|
|
|
|
$element,
|
|
|
|
PoCoAddress::ADDRESS,
|
|
|
|
PoCo::NS
|
|
|
|
);
|
|
|
|
|
2010-02-25 07:16:17 +09:00
|
|
|
if (!empty($addressEl)) {
|
|
|
|
$formatted = ActivityUtils::childContent(
|
|
|
|
$addressEl,
|
|
|
|
PoCoAddress::FORMATTED,
|
|
|
|
self::NS
|
|
|
|
);
|
2010-02-24 15:35:48 +09:00
|
|
|
|
2010-02-25 07:16:17 +09:00
|
|
|
if (!empty($formatted)) {
|
|
|
|
$address = new PoCoAddress();
|
|
|
|
$address->formatted = $formatted;
|
|
|
|
return $address;
|
|
|
|
}
|
2010-02-24 15:35:48 +09:00
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
function fromProfile($profile)
|
|
|
|
{
|
|
|
|
if (empty($profile)) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
$poco = new PoCo();
|
|
|
|
|
|
|
|
$poco->preferredUsername = $profile->nickname;
|
|
|
|
$poco->displayName = $profile->getBestName();
|
|
|
|
|
|
|
|
$poco->note = $profile->bio;
|
|
|
|
|
|
|
|
$paddy = new PoCoAddress();
|
|
|
|
$paddy->formatted = $profile->location;
|
|
|
|
$poco->address = $paddy;
|
2010-02-23 09:46:54 +09:00
|
|
|
|
|
|
|
if (!empty($profile->homepage)) {
|
|
|
|
array_push(
|
2010-02-24 15:35:48 +09:00
|
|
|
$poco->urls,
|
2010-02-23 09:46:54 +09:00
|
|
|
new PoCoURL(
|
|
|
|
'homepage',
|
|
|
|
$profile->homepage,
|
|
|
|
true
|
|
|
|
)
|
|
|
|
);
|
|
|
|
}
|
2010-02-24 15:35:48 +09:00
|
|
|
|
|
|
|
return $poco;
|
2010-02-23 09:46:54 +09:00
|
|
|
}
|
|
|
|
|
2010-02-26 06:34:43 +09:00
|
|
|
function fromGroup($group)
|
|
|
|
{
|
|
|
|
if (empty($group)) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
$poco = new PoCo();
|
|
|
|
|
|
|
|
$poco->preferredUsername = $group->nickname;
|
|
|
|
$poco->displayName = $group->getBestName();
|
|
|
|
|
|
|
|
$poco->note = $group->description;
|
|
|
|
|
|
|
|
$paddy = new PoCoAddress();
|
|
|
|
$paddy->formatted = $group->location;
|
|
|
|
$poco->address = $paddy;
|
|
|
|
|
|
|
|
if (!empty($group->homepage)) {
|
|
|
|
array_push(
|
|
|
|
$poco->urls,
|
|
|
|
new PoCoURL(
|
|
|
|
'homepage',
|
|
|
|
$group->homepage,
|
|
|
|
true
|
|
|
|
)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $poco;
|
|
|
|
}
|
|
|
|
|
2010-02-25 09:51:24 +09:00
|
|
|
function getPrimaryURL()
|
|
|
|
{
|
|
|
|
foreach ($this->urls as $url) {
|
|
|
|
if ($url->primary) {
|
|
|
|
return $url;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-02-23 09:46:54 +09:00
|
|
|
function asString()
|
|
|
|
{
|
|
|
|
$xs = new XMLStringer(true);
|
|
|
|
$xs->element(
|
|
|
|
'poco:preferredUsername',
|
|
|
|
null,
|
|
|
|
$this->preferredUsername
|
|
|
|
);
|
|
|
|
|
2010-02-24 07:26:34 +09:00
|
|
|
$xs->element(
|
|
|
|
'poco:displayName',
|
|
|
|
null,
|
|
|
|
$this->displayName
|
|
|
|
);
|
|
|
|
|
2010-02-23 09:46:54 +09:00
|
|
|
if (!empty($this->note)) {
|
2010-03-12 10:12:30 +09:00
|
|
|
$xs->element('poco:note', null, common_xml_safe_str($this->note));
|
2010-02-23 09:46:54 +09:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!empty($this->address)) {
|
|
|
|
$xs->raw($this->address->asString());
|
|
|
|
}
|
|
|
|
|
|
|
|
foreach ($this->urls as $url) {
|
|
|
|
$xs->raw($url->asString());
|
|
|
|
}
|
|
|
|
|
|
|
|
return $xs->getString();
|
|
|
|
}
|
2010-02-22 21:57:44 +09:00
|
|
|
}
|
|
|
|
|
2010-02-15 02:12:47 +09:00
|
|
|
/**
|
|
|
|
* Utilities for turning DOMish things into Activityish things
|
|
|
|
*
|
|
|
|
* Some common functions that I didn't have the bandwidth to try to factor
|
|
|
|
* into some kind of reasonable superclass, so just dumped here. Might
|
|
|
|
* be useful to have an ActivityObject parent class or something.
|
|
|
|
*
|
|
|
|
* @category OStatus
|
|
|
|
* @package StatusNet
|
|
|
|
* @author Evan Prodromou <evan@status.net>
|
|
|
|
* @copyright 2010 StatusNet, Inc.
|
|
|
|
* @license http://www.fsf.org/licensing/licenses/agpl-3.0.html AGPLv3
|
|
|
|
* @link http://status.net/
|
|
|
|
*/
|
|
|
|
|
|
|
|
class ActivityUtils
|
|
|
|
{
|
|
|
|
const ATOM = 'http://www.w3.org/2005/Atom';
|
|
|
|
|
|
|
|
const LINK = 'link';
|
|
|
|
const REL = 'rel';
|
|
|
|
const TYPE = 'type';
|
|
|
|
const HREF = 'href';
|
|
|
|
|
2010-02-22 00:50:51 +09:00
|
|
|
const CONTENT = 'content';
|
|
|
|
const SRC = 'src';
|
|
|
|
|
2010-02-15 02:12:47 +09:00
|
|
|
/**
|
|
|
|
* Get the permalink for an Activity object
|
|
|
|
*
|
|
|
|
* @param DOMElement $element A DOM element
|
|
|
|
*
|
|
|
|
* @return string related link, if any
|
|
|
|
*/
|
|
|
|
|
2010-02-17 02:49:54 +09:00
|
|
|
static function getPermalink($element)
|
|
|
|
{
|
|
|
|
return self::getLink($element, 'alternate', 'text/html');
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the permalink for an Activity object
|
|
|
|
*
|
|
|
|
* @param DOMElement $element A DOM element
|
|
|
|
*
|
|
|
|
* @return string related link, if any
|
|
|
|
*/
|
|
|
|
|
2010-02-25 07:16:17 +09:00
|
|
|
static function getLink(DOMNode $element, $rel, $type=null)
|
2010-02-15 02:12:47 +09:00
|
|
|
{
|
2010-03-03 08:38:52 +09:00
|
|
|
$els = $element->childNodes;
|
2010-02-15 02:12:47 +09:00
|
|
|
|
2010-03-03 08:38:52 +09:00
|
|
|
foreach ($els as $link) {
|
|
|
|
if ($link->localName == self::LINK && $link->namespaceURI == self::ATOM) {
|
2010-02-15 03:19:32 +09:00
|
|
|
|
2010-03-03 08:38:52 +09:00
|
|
|
$linkRel = $link->getAttribute(self::REL);
|
|
|
|
$linkType = $link->getAttribute(self::TYPE);
|
2010-02-15 03:19:32 +09:00
|
|
|
|
2010-03-03 08:38:52 +09:00
|
|
|
if ($linkRel == $rel &&
|
|
|
|
(is_null($type) || $linkType == $type)) {
|
|
|
|
return $link->getAttribute(self::HREF);
|
|
|
|
}
|
2010-02-15 02:12:47 +09:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
2010-02-17 02:49:54 +09:00
|
|
|
|
2010-02-26 11:51:44 +09:00
|
|
|
static function getLinks(DOMNode $element, $rel, $type=null)
|
|
|
|
{
|
2010-03-03 08:38:52 +09:00
|
|
|
$els = $element->childNodes;
|
2010-02-26 11:51:44 +09:00
|
|
|
$out = array();
|
|
|
|
|
2010-03-03 08:38:52 +09:00
|
|
|
foreach ($els as $link) {
|
|
|
|
if ($link->localName == self::LINK && $link->namespaceURI == self::ATOM) {
|
2010-02-26 11:51:44 +09:00
|
|
|
|
2010-03-03 08:38:52 +09:00
|
|
|
$linkRel = $link->getAttribute(self::REL);
|
|
|
|
$linkType = $link->getAttribute(self::TYPE);
|
2010-02-26 11:51:44 +09:00
|
|
|
|
2010-03-03 08:38:52 +09:00
|
|
|
if ($linkRel == $rel &&
|
|
|
|
(is_null($type) || $linkType == $type)) {
|
|
|
|
$out[] = $link;
|
|
|
|
}
|
2010-02-26 11:51:44 +09:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $out;
|
|
|
|
}
|
|
|
|
|
2010-02-17 02:49:54 +09:00
|
|
|
/**
|
|
|
|
* Gets the first child element with the given tag
|
|
|
|
*
|
|
|
|
* @param DOMElement $element element to pick at
|
|
|
|
* @param string $tag tag to look for
|
|
|
|
* @param string $namespace Namespace to look under
|
|
|
|
*
|
|
|
|
* @return DOMElement found element or null
|
|
|
|
*/
|
|
|
|
|
2010-02-25 07:16:17 +09:00
|
|
|
static function child(DOMNode $element, $tag, $namespace=self::ATOM)
|
2010-02-17 02:49:54 +09:00
|
|
|
{
|
2010-02-17 08:04:39 +09:00
|
|
|
$els = $element->childNodes;
|
2010-02-17 02:49:54 +09:00
|
|
|
if (empty($els) || $els->length == 0) {
|
|
|
|
return null;
|
|
|
|
} else {
|
2010-02-17 08:04:39 +09:00
|
|
|
for ($i = 0; $i < $els->length; $i++) {
|
|
|
|
$el = $els->item($i);
|
|
|
|
if ($el->localName == $tag && $el->namespaceURI == $namespace) {
|
|
|
|
return $el;
|
|
|
|
}
|
|
|
|
}
|
2010-02-17 02:49:54 +09:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Grab the text content of a DOM element child of the current element
|
|
|
|
*
|
|
|
|
* @param DOMElement $element Element whose children we examine
|
|
|
|
* @param string $tag Tag to look up
|
|
|
|
* @param string $namespace Namespace to use, defaults to Atom
|
|
|
|
*
|
|
|
|
* @return string content of the child
|
|
|
|
*/
|
|
|
|
|
2010-02-25 07:16:17 +09:00
|
|
|
static function childContent(DOMNode $element, $tag, $namespace=self::ATOM)
|
2010-02-17 02:49:54 +09:00
|
|
|
{
|
|
|
|
$el = self::child($element, $tag, $namespace);
|
|
|
|
|
|
|
|
if (empty($el)) {
|
|
|
|
return null;
|
|
|
|
} else {
|
|
|
|
return $el->textContent;
|
|
|
|
}
|
|
|
|
}
|
2010-02-22 00:50:51 +09:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the content of an atom:entry-like object
|
|
|
|
*
|
|
|
|
* @param DOMElement $element The element to examine.
|
|
|
|
*
|
|
|
|
* @return string unencoded HTML content of the element, like "This -< is <b>HTML</b>."
|
|
|
|
*
|
|
|
|
* @todo handle remote content
|
|
|
|
* @todo handle embedded XML mime types
|
|
|
|
* @todo handle base64-encoded non-XML and non-text mime types
|
|
|
|
*/
|
|
|
|
|
|
|
|
static function getContent($element)
|
|
|
|
{
|
|
|
|
$contentEl = ActivityUtils::child($element, self::CONTENT);
|
|
|
|
|
|
|
|
if (!empty($contentEl)) {
|
|
|
|
|
|
|
|
$src = $contentEl->getAttribute(self::SRC);
|
|
|
|
|
|
|
|
if (!empty($src)) {
|
|
|
|
throw new ClientException(_("Can't handle remote content yet."));
|
|
|
|
}
|
|
|
|
|
|
|
|
$type = $contentEl->getAttribute(self::TYPE);
|
|
|
|
|
|
|
|
// slavishly following http://atompub.org/rfc4287.html#rfc.section.4.1.3.3
|
|
|
|
|
2010-03-14 00:37:08 +09:00
|
|
|
if (empty($type) || $type == 'text') {
|
2010-02-22 00:50:51 +09:00
|
|
|
return $contentEl->textContent;
|
|
|
|
} else if ($type == 'html') {
|
|
|
|
$text = $contentEl->textContent;
|
|
|
|
return htmlspecialchars_decode($text, ENT_QUOTES);
|
|
|
|
} else if ($type == 'xhtml') {
|
2010-03-16 05:21:55 +09:00
|
|
|
$divEl = ActivityUtils::child($contentEl, 'div', 'http://www.w3.org/1999/xhtml');
|
2010-02-22 00:50:51 +09:00
|
|
|
if (empty($divEl)) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
$doc = $divEl->ownerDocument;
|
|
|
|
$text = '';
|
|
|
|
$children = $divEl->childNodes;
|
|
|
|
|
|
|
|
for ($i = 0; $i < $children->length; $i++) {
|
|
|
|
$child = $children->item($i);
|
|
|
|
$text .= $doc->saveXML($child);
|
|
|
|
}
|
|
|
|
return trim($text);
|
2010-03-14 00:37:08 +09:00
|
|
|
} else if (in_array($type, array('text/xml', 'application/xml')) ||
|
2010-02-22 00:50:51 +09:00
|
|
|
preg_match('#(+|/)xml$#', $type)) {
|
|
|
|
throw new ClientException(_("Can't handle embedded XML content yet."));
|
|
|
|
} else if (strncasecmp($type, 'text/', 5)) {
|
|
|
|
return $contentEl->textContent;
|
|
|
|
} else {
|
|
|
|
throw new ClientException(_("Can't handle embedded Base64 content yet."));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2010-02-15 02:12:47 +09:00
|
|
|
}
|
|
|
|
|
2010-02-26 09:06:49 +09:00
|
|
|
// XXX: Arg! This wouldn't be necessary if we used Avatars conistently
|
|
|
|
class AvatarLink
|
|
|
|
{
|
|
|
|
public $url;
|
|
|
|
public $type;
|
|
|
|
public $size;
|
|
|
|
public $width;
|
2010-02-26 11:51:44 +09:00
|
|
|
public $height;
|
|
|
|
|
|
|
|
function __construct($element=null)
|
|
|
|
{
|
|
|
|
if ($element) {
|
|
|
|
// @fixme use correct namespaces
|
|
|
|
$this->url = $element->getAttribute('href');
|
|
|
|
$this->type = $element->getAttribute('type');
|
|
|
|
$width = $element->getAttribute('media:width');
|
|
|
|
if ($width != null) {
|
|
|
|
$this->width = intval($width);
|
|
|
|
}
|
|
|
|
$height = $element->getAttribute('media:height');
|
|
|
|
if ($height != null) {
|
|
|
|
$this->height = intval($height);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2010-02-26 09:06:49 +09:00
|
|
|
|
|
|
|
static function fromAvatar($avatar)
|
|
|
|
{
|
|
|
|
if (empty($avatar)) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
$alink = new AvatarLink();
|
|
|
|
$alink->type = $avatar->mediatype;
|
2010-02-26 11:06:03 +09:00
|
|
|
$alink->height = $avatar->height;
|
2010-02-26 09:06:49 +09:00
|
|
|
$alink->width = $avatar->width;
|
|
|
|
$alink->url = $avatar->displayUrl();
|
|
|
|
return $alink;
|
|
|
|
}
|
|
|
|
|
|
|
|
static function fromFilename($filename, $size)
|
|
|
|
{
|
|
|
|
$alink = new AvatarLink();
|
|
|
|
$alink->url = $filename;
|
|
|
|
$alink->height = $size;
|
|
|
|
if (!empty($filename)) {
|
|
|
|
$alink->width = $size;
|
|
|
|
$alink->type = self::mediatype($filename);
|
|
|
|
} else {
|
|
|
|
$alink->url = User_group::defaultLogo($size);
|
|
|
|
$alink->type = 'image/png';
|
|
|
|
}
|
|
|
|
return $alink;
|
|
|
|
}
|
|
|
|
|
|
|
|
// yuck!
|
|
|
|
static function mediatype($filename) {
|
|
|
|
$ext = strtolower(end(explode('.', $filename)));
|
|
|
|
if ($ext == 'jpeg') {
|
|
|
|
$ext = 'jpg';
|
|
|
|
}
|
|
|
|
// hope we don't support any others
|
|
|
|
$types = array('png', 'gif', 'jpg', 'jpeg');
|
|
|
|
if (in_array($ext, $types)) {
|
|
|
|
return 'image/' . $ext;
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-02-15 02:12:47 +09:00
|
|
|
/**
|
|
|
|
* A noun-ish thing in the activity universe
|
|
|
|
*
|
|
|
|
* The activity streams spec talks about activity objects, while also having
|
|
|
|
* a tag activity:object, which is in fact an activity object. Aaaaaah!
|
|
|
|
*
|
|
|
|
* This is just a thing in the activity universe. Can be the subject, object,
|
|
|
|
* or indirect object (target!) of an activity verb. Rotten name, and I'm
|
|
|
|
* propagating it. *sigh*
|
|
|
|
*
|
|
|
|
* @category OStatus
|
|
|
|
* @package StatusNet
|
|
|
|
* @author Evan Prodromou <evan@status.net>
|
|
|
|
* @copyright 2010 StatusNet, Inc.
|
|
|
|
* @license http://www.fsf.org/licensing/licenses/agpl-3.0.html AGPLv3
|
|
|
|
* @link http://status.net/
|
|
|
|
*/
|
|
|
|
|
|
|
|
class ActivityObject
|
2010-02-12 14:42:42 +09:00
|
|
|
{
|
|
|
|
const ARTICLE = 'http://activitystrea.ms/schema/1.0/article';
|
|
|
|
const BLOGENTRY = 'http://activitystrea.ms/schema/1.0/blog-entry';
|
|
|
|
const NOTE = 'http://activitystrea.ms/schema/1.0/note';
|
|
|
|
const STATUS = 'http://activitystrea.ms/schema/1.0/status';
|
|
|
|
const FILE = 'http://activitystrea.ms/schema/1.0/file';
|
|
|
|
const PHOTO = 'http://activitystrea.ms/schema/1.0/photo';
|
|
|
|
const ALBUM = 'http://activitystrea.ms/schema/1.0/photo-album';
|
|
|
|
const PLAYLIST = 'http://activitystrea.ms/schema/1.0/playlist';
|
|
|
|
const VIDEO = 'http://activitystrea.ms/schema/1.0/video';
|
|
|
|
const AUDIO = 'http://activitystrea.ms/schema/1.0/audio';
|
|
|
|
const BOOKMARK = 'http://activitystrea.ms/schema/1.0/bookmark';
|
|
|
|
const PERSON = 'http://activitystrea.ms/schema/1.0/person';
|
|
|
|
const GROUP = 'http://activitystrea.ms/schema/1.0/group';
|
|
|
|
const PLACE = 'http://activitystrea.ms/schema/1.0/place';
|
2010-02-15 02:12:47 +09:00
|
|
|
const COMMENT = 'http://activitystrea.ms/schema/1.0/comment';
|
|
|
|
// ^^^^^^^^^^ tea!
|
|
|
|
|
|
|
|
// Atom elements we snarf
|
|
|
|
|
|
|
|
const TITLE = 'title';
|
|
|
|
const SUMMARY = 'summary';
|
|
|
|
const ID = 'id';
|
|
|
|
const SOURCE = 'source';
|
|
|
|
|
2010-02-15 03:19:32 +09:00
|
|
|
const NAME = 'name';
|
|
|
|
const URI = 'uri';
|
|
|
|
const EMAIL = 'email';
|
2010-02-12 14:42:42 +09:00
|
|
|
|
2010-02-17 02:49:54 +09:00
|
|
|
public $element;
|
2010-02-12 14:42:42 +09:00
|
|
|
public $type;
|
|
|
|
public $id;
|
|
|
|
public $title;
|
|
|
|
public $summary;
|
|
|
|
public $content;
|
2010-02-15 02:12:47 +09:00
|
|
|
public $link;
|
|
|
|
public $source;
|
2010-02-26 09:06:49 +09:00
|
|
|
public $avatarLinks = array();
|
2010-02-23 09:46:54 +09:00
|
|
|
public $geopoint;
|
2010-02-24 15:35:48 +09:00
|
|
|
public $poco;
|
|
|
|
public $displayName;
|
2010-02-15 02:12:47 +09:00
|
|
|
|
2010-02-21 23:15:57 +09:00
|
|
|
/**
|
2010-02-15 02:12:47 +09:00
|
|
|
* Constructor
|
|
|
|
*
|
|
|
|
* This probably needs to be refactored
|
|
|
|
* to generate a local class (ActivityPerson, ActivityFile, ...)
|
|
|
|
* based on the object type.
|
|
|
|
*
|
|
|
|
* @param DOMElement $element DOM thing to turn into an Activity thing
|
|
|
|
*/
|
|
|
|
|
2010-02-21 09:58:20 +09:00
|
|
|
function __construct($element = null)
|
2010-02-15 02:12:47 +09:00
|
|
|
{
|
2010-02-21 09:58:20 +09:00
|
|
|
if (empty($element)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2010-02-17 02:49:54 +09:00
|
|
|
$this->element = $element;
|
2010-02-15 02:12:47 +09:00
|
|
|
|
2010-02-25 09:51:24 +09:00
|
|
|
$this->geopoint = $this->_childContent(
|
|
|
|
$element,
|
|
|
|
ActivityContext::POINT,
|
|
|
|
ActivityContext::GEORSS
|
|
|
|
);
|
|
|
|
|
2010-02-15 02:12:47 +09:00
|
|
|
if ($element->tagName == 'author') {
|
2010-03-19 23:48:39 +09:00
|
|
|
$this->_fromAuthor($element);
|
|
|
|
} else if ($element->tagName == 'item') {
|
|
|
|
$this->_fromRssItem($element);
|
2010-02-15 02:12:47 +09:00
|
|
|
} else {
|
2010-03-19 23:48:39 +09:00
|
|
|
$this->_fromAtomEntry($element);
|
2010-02-15 02:12:47 +09:00
|
|
|
}
|
2010-02-17 10:49:49 +09:00
|
|
|
|
|
|
|
// Some per-type attributes...
|
|
|
|
if ($this->type == self::PERSON || $this->type == self::GROUP) {
|
|
|
|
$this->displayName = $this->title;
|
|
|
|
|
2010-03-15 01:57:24 +09:00
|
|
|
$photos = ActivityUtils::getLinks($element, 'photo');
|
|
|
|
if (count($photos)) {
|
|
|
|
foreach ($photos as $link) {
|
|
|
|
$this->avatarLinks[] = new AvatarLink($link);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
$avatars = ActivityUtils::getLinks($element, 'avatar');
|
|
|
|
foreach ($avatars as $link) {
|
|
|
|
$this->avatarLinks[] = new AvatarLink($link);
|
|
|
|
}
|
2010-02-26 11:51:44 +09:00
|
|
|
}
|
2010-02-24 15:35:48 +09:00
|
|
|
|
|
|
|
$this->poco = new PoCo($element);
|
2010-02-17 10:49:49 +09:00
|
|
|
}
|
2010-02-15 02:12:47 +09:00
|
|
|
}
|
|
|
|
|
2010-03-19 23:48:39 +09:00
|
|
|
private function _fromAuthor($element)
|
|
|
|
{
|
|
|
|
$this->type = self::PERSON; // XXX: is this fair?
|
|
|
|
$this->title = $this->_childContent($element, self::NAME);
|
|
|
|
$this->id = $this->_childContent($element, self::URI);
|
|
|
|
|
|
|
|
if (empty($this->id)) {
|
|
|
|
$email = $this->_childContent($element, self::EMAIL);
|
|
|
|
if (!empty($email)) {
|
|
|
|
// XXX: acct: ?
|
|
|
|
$this->id = 'mailto:'.$email;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private function _fromAtomEntry($element)
|
|
|
|
{
|
|
|
|
$this->type = $this->_childContent($element, Activity::OBJECTTYPE,
|
|
|
|
Activity::SPEC);
|
|
|
|
|
|
|
|
if (empty($this->type)) {
|
|
|
|
$this->type = ActivityObject::NOTE;
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->id = $this->_childContent($element, self::ID);
|
|
|
|
$this->title = $this->_childContent($element, self::TITLE);
|
|
|
|
$this->summary = $this->_childContent($element, self::SUMMARY);
|
|
|
|
|
|
|
|
$this->source = $this->_getSource($element);
|
|
|
|
|
|
|
|
$this->content = ActivityUtils::getContent($element);
|
|
|
|
|
|
|
|
$this->link = ActivityUtils::getPermalink($element);
|
|
|
|
}
|
|
|
|
|
|
|
|
// @fixme rationalize with Activity::_fromRssItem()
|
|
|
|
|
|
|
|
private function _fromRssItem($item)
|
|
|
|
{
|
|
|
|
$this->title = ActivityUtils::childContent($item, ActivityObject::TITLE, Activity::RSS);
|
|
|
|
|
|
|
|
$contentEl = ActivityUtils::child($item, ActivityUtils::CONTENT, Activity::CONTENTNS);
|
|
|
|
|
|
|
|
if (!empty($contentEl)) {
|
|
|
|
$this->content = htmlspecialchars_decode($contentEl->textContent, ENT_QUOTES);
|
|
|
|
} else {
|
|
|
|
$descriptionEl = ActivityUtils::child($item, Activity::DESCRIPTION, Activity::RSS);
|
|
|
|
if (!empty($descriptionEl)) {
|
|
|
|
$this->content = htmlspecialchars_decode($descriptionEl->textContent, ENT_QUOTES);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->link = ActivityUtils::childContent($item, ActivityUtils::LINK, Activity::RSS);
|
|
|
|
|
|
|
|
$guidEl = ActivityUtils::child($item, Activity::GUID, Activity::RSS);
|
|
|
|
|
|
|
|
if (!empty($guidEl)) {
|
|
|
|
$this->id = $guidEl->textContent;
|
|
|
|
|
|
|
|
if ($guidEl->hasAttribute('isPermaLink')) {
|
|
|
|
// overwrites <link>
|
|
|
|
$this->link = $this->id;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-02-17 02:49:54 +09:00
|
|
|
private function _childContent($element, $tag, $namespace=ActivityUtils::ATOM)
|
|
|
|
{
|
|
|
|
return ActivityUtils::childContent($element, $tag, $namespace);
|
|
|
|
}
|
2010-02-15 02:12:47 +09:00
|
|
|
|
2010-02-17 02:49:54 +09:00
|
|
|
// Try to get a unique id for the source feed
|
|
|
|
|
|
|
|
private function _getSource($element)
|
2010-02-15 02:12:47 +09:00
|
|
|
{
|
2010-02-17 02:49:54 +09:00
|
|
|
$sourceEl = ActivityUtils::child($element, 'source');
|
2010-02-15 02:12:47 +09:00
|
|
|
|
2010-02-17 02:49:54 +09:00
|
|
|
if (empty($sourceEl)) {
|
2010-02-15 02:12:47 +09:00
|
|
|
return null;
|
|
|
|
} else {
|
2010-02-17 02:49:54 +09:00
|
|
|
$href = ActivityUtils::getLink($sourceEl, 'self');
|
|
|
|
if (!empty($href)) {
|
|
|
|
return $href;
|
|
|
|
} else {
|
|
|
|
return ActivityUtils::childContent($sourceEl, 'id');
|
|
|
|
}
|
2010-02-15 02:12:47 +09:00
|
|
|
}
|
|
|
|
}
|
2010-02-21 09:58:20 +09:00
|
|
|
|
2010-03-19 09:08:19 +09:00
|
|
|
static function fromNotice(Notice $notice)
|
2010-02-21 09:58:20 +09:00
|
|
|
{
|
|
|
|
$object = new ActivityObject();
|
|
|
|
|
|
|
|
$object->type = ActivityObject::NOTE;
|
|
|
|
|
|
|
|
$object->id = $notice->uri;
|
|
|
|
$object->title = $notice->content;
|
|
|
|
$object->content = $notice->rendered;
|
|
|
|
$object->link = $notice->bestUrl();
|
|
|
|
|
|
|
|
return $object;
|
|
|
|
}
|
|
|
|
|
2010-03-19 09:08:19 +09:00
|
|
|
static function fromProfile(Profile $profile)
|
2010-02-22 00:50:51 +09:00
|
|
|
{
|
|
|
|
$object = new ActivityObject();
|
|
|
|
|
2010-02-23 09:46:54 +09:00
|
|
|
$object->type = ActivityObject::PERSON;
|
|
|
|
$object->id = $profile->getUri();
|
|
|
|
$object->title = $profile->getBestName();
|
|
|
|
$object->link = $profile->profileurl;
|
2010-02-26 09:06:49 +09:00
|
|
|
|
|
|
|
$orig = $profile->getOriginalAvatar();
|
|
|
|
|
|
|
|
if (!empty($orig)) {
|
|
|
|
$object->avatarLinks[] = AvatarLink::fromAvatar($orig);
|
|
|
|
}
|
|
|
|
|
|
|
|
$sizes = array(
|
|
|
|
AVATAR_PROFILE_SIZE,
|
|
|
|
AVATAR_STREAM_SIZE,
|
|
|
|
AVATAR_MINI_SIZE
|
|
|
|
);
|
|
|
|
|
|
|
|
foreach ($sizes as $size) {
|
|
|
|
|
|
|
|
$alink = null;
|
|
|
|
$avatar = $profile->getAvatar($size);
|
|
|
|
|
|
|
|
if (!empty($avatar)) {
|
|
|
|
$alink = AvatarLink::fromAvatar($avatar);
|
|
|
|
} else {
|
|
|
|
$alink = new AvatarLink();
|
|
|
|
$alink->type = 'image/png';
|
|
|
|
$alink->height = $size;
|
|
|
|
$alink->width = $size;
|
|
|
|
$alink->url = Avatar::defaultImage($size);
|
|
|
|
}
|
|
|
|
|
|
|
|
$object->avatarLinks[] = $alink;
|
2010-02-26 06:02:08 +09:00
|
|
|
}
|
2010-02-23 09:46:54 +09:00
|
|
|
|
|
|
|
if (isset($profile->lat) && isset($profile->lon)) {
|
2010-02-26 09:06:49 +09:00
|
|
|
$object->geopoint = (float)$profile->lat
|
|
|
|
. ' ' . (float)$profile->lon;
|
2010-02-23 09:46:54 +09:00
|
|
|
}
|
|
|
|
|
2010-02-24 15:35:48 +09:00
|
|
|
$object->poco = PoCo::fromProfile($profile);
|
2010-02-22 00:50:51 +09:00
|
|
|
|
|
|
|
return $object;
|
|
|
|
}
|
|
|
|
|
2010-02-26 06:34:43 +09:00
|
|
|
static function fromGroup($group)
|
|
|
|
{
|
|
|
|
$object = new ActivityObject();
|
|
|
|
|
|
|
|
$object->type = ActivityObject::GROUP;
|
|
|
|
$object->id = $group->getUri();
|
|
|
|
$object->title = $group->getBestName();
|
|
|
|
$object->link = $group->getUri();
|
2010-02-26 09:06:49 +09:00
|
|
|
|
|
|
|
$object->avatarLinks[] = AvatarLink::fromFilename(
|
|
|
|
$group->homepage_logo,
|
|
|
|
AVATAR_PROFILE_SIZE
|
|
|
|
);
|
|
|
|
|
|
|
|
$object->avatarLinks[] = AvatarLink::fromFilename(
|
|
|
|
$group->stream_logo,
|
|
|
|
AVATAR_STREAM_SIZE
|
|
|
|
);
|
|
|
|
|
|
|
|
$object->avatarLinks[] = AvatarLink::fromFilename(
|
|
|
|
$group->mini_logo,
|
|
|
|
AVATAR_MINI_SIZE
|
|
|
|
);
|
2010-02-26 06:34:43 +09:00
|
|
|
|
|
|
|
$object->poco = PoCo::fromGroup($group);
|
|
|
|
|
|
|
|
return $object;
|
|
|
|
}
|
|
|
|
|
2010-02-21 09:58:20 +09:00
|
|
|
function asString($tag='activity:object')
|
|
|
|
{
|
|
|
|
$xs = new XMLStringer(true);
|
|
|
|
|
|
|
|
$xs->elementStart($tag);
|
|
|
|
|
|
|
|
$xs->element('activity:object-type', null, $this->type);
|
|
|
|
|
|
|
|
$xs->element(self::ID, null, $this->id);
|
|
|
|
|
|
|
|
if (!empty($this->title)) {
|
2010-03-12 10:12:30 +09:00
|
|
|
$xs->element(
|
|
|
|
self::TITLE,
|
|
|
|
null,
|
|
|
|
common_xml_safe_str($this->title)
|
|
|
|
);
|
2010-02-21 09:58:20 +09:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!empty($this->summary)) {
|
2010-03-12 10:12:30 +09:00
|
|
|
$xs->element(
|
|
|
|
self::SUMMARY,
|
|
|
|
null,
|
|
|
|
common_xml_safe_str($this->summary)
|
|
|
|
);
|
2010-02-21 09:58:20 +09:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!empty($this->content)) {
|
|
|
|
// XXX: assuming HTML content here
|
2010-03-12 10:12:30 +09:00
|
|
|
$xs->element(
|
|
|
|
ActivityUtils::CONTENT,
|
|
|
|
array('type' => 'html'),
|
|
|
|
common_xml_safe_str($this->content)
|
|
|
|
);
|
2010-02-21 09:58:20 +09:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!empty($this->link)) {
|
2010-02-24 15:35:48 +09:00
|
|
|
$xs->element(
|
|
|
|
'link',
|
|
|
|
array(
|
|
|
|
'rel' => 'alternate',
|
|
|
|
'type' => 'text/html',
|
|
|
|
'href' => $this->link
|
|
|
|
),
|
|
|
|
null
|
|
|
|
);
|
2010-02-23 09:46:54 +09:00
|
|
|
}
|
|
|
|
|
2010-02-26 09:06:49 +09:00
|
|
|
if ($this->type == ActivityObject::PERSON
|
|
|
|
|| $this->type == ActivityObject::GROUP) {
|
|
|
|
|
|
|
|
foreach ($this->avatarLinks as $avatar) {
|
|
|
|
$xs->element(
|
|
|
|
'link', array(
|
|
|
|
'rel' => 'avatar',
|
|
|
|
'type' => $avatar->type,
|
|
|
|
'media:width' => $avatar->width,
|
|
|
|
'media:height' => $avatar->height,
|
|
|
|
'href' => $avatar->url
|
|
|
|
),
|
|
|
|
null
|
|
|
|
);
|
|
|
|
}
|
2010-02-26 06:34:43 +09:00
|
|
|
}
|
|
|
|
|
2010-02-23 09:46:54 +09:00
|
|
|
if (!empty($this->geopoint)) {
|
|
|
|
$xs->element(
|
|
|
|
'georss:point',
|
|
|
|
null,
|
|
|
|
$this->geopoint
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!empty($this->poco)) {
|
|
|
|
$xs->raw($this->poco->asString());
|
2010-02-21 09:58:20 +09:00
|
|
|
}
|
|
|
|
|
|
|
|
$xs->elementEnd($tag);
|
|
|
|
|
|
|
|
return $xs->getString();
|
|
|
|
}
|
2010-02-12 14:42:42 +09:00
|
|
|
}
|
|
|
|
|
2010-02-15 02:12:47 +09:00
|
|
|
/**
|
|
|
|
* Utility class to hold a bunch of constant defining default verb types
|
|
|
|
*
|
|
|
|
* @category OStatus
|
|
|
|
* @package StatusNet
|
|
|
|
* @author Evan Prodromou <evan@status.net>
|
|
|
|
* @copyright 2010 StatusNet, Inc.
|
|
|
|
* @license http://www.fsf.org/licensing/licenses/agpl-3.0.html AGPLv3
|
|
|
|
* @link http://status.net/
|
|
|
|
*/
|
2010-02-12 14:42:42 +09:00
|
|
|
|
2010-02-15 02:12:47 +09:00
|
|
|
class ActivityVerb
|
|
|
|
{
|
2010-02-12 14:42:42 +09:00
|
|
|
const POST = 'http://activitystrea.ms/schema/1.0/post';
|
|
|
|
const SHARE = 'http://activitystrea.ms/schema/1.0/share';
|
|
|
|
const SAVE = 'http://activitystrea.ms/schema/1.0/save';
|
|
|
|
const FAVORITE = 'http://activitystrea.ms/schema/1.0/favorite';
|
|
|
|
const PLAY = 'http://activitystrea.ms/schema/1.0/play';
|
|
|
|
const FOLLOW = 'http://activitystrea.ms/schema/1.0/follow';
|
|
|
|
const FRIEND = 'http://activitystrea.ms/schema/1.0/make-friend';
|
|
|
|
const JOIN = 'http://activitystrea.ms/schema/1.0/join';
|
|
|
|
const TAG = 'http://activitystrea.ms/schema/1.0/tag';
|
2010-02-19 03:20:48 +09:00
|
|
|
|
|
|
|
// Custom OStatus verbs for the flipside until they're standardized
|
|
|
|
const DELETE = 'http://ostatus.org/schema/1.0/unfollow';
|
|
|
|
const UNFAVORITE = 'http://ostatus.org/schema/1.0/unfavorite';
|
|
|
|
const UNFOLLOW = 'http://ostatus.org/schema/1.0/unfollow';
|
|
|
|
const LEAVE = 'http://ostatus.org/schema/1.0/leave';
|
2010-02-25 05:36:36 +09:00
|
|
|
|
|
|
|
// For simple profile-update pings; no content to share.
|
|
|
|
const UPDATE_PROFILE = 'http://ostatus.org/schema/1.0/update-profile';
|
2010-02-15 02:12:47 +09:00
|
|
|
}
|
|
|
|
|
2010-02-19 12:18:14 +09:00
|
|
|
class ActivityContext
|
|
|
|
{
|
|
|
|
public $replyToID;
|
|
|
|
public $replyToUrl;
|
|
|
|
public $location;
|
|
|
|
public $attention = array();
|
|
|
|
public $conversation;
|
|
|
|
|
|
|
|
const THR = 'http://purl.org/syndication/thread/1.0';
|
|
|
|
const GEORSS = 'http://www.georss.org/georss';
|
|
|
|
const OSTATUS = 'http://ostatus.org/schema/1.0';
|
|
|
|
|
|
|
|
const INREPLYTO = 'in-reply-to';
|
|
|
|
const REF = 'ref';
|
|
|
|
const HREF = 'href';
|
|
|
|
|
|
|
|
const POINT = 'point';
|
|
|
|
|
|
|
|
const ATTENTION = 'ostatus:attention';
|
|
|
|
const CONVERSATION = 'ostatus:conversation';
|
|
|
|
|
|
|
|
function __construct($element)
|
|
|
|
{
|
|
|
|
$replyToEl = ActivityUtils::child($element, self::INREPLYTO, self::THR);
|
|
|
|
|
|
|
|
if (!empty($replyToEl)) {
|
|
|
|
$this->replyToID = $replyToEl->getAttribute(self::REF);
|
|
|
|
$this->replyToUrl = $replyToEl->getAttribute(self::HREF);
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->location = $this->getLocation($element);
|
|
|
|
|
|
|
|
$this->conversation = ActivityUtils::getLink($element, self::CONVERSATION);
|
|
|
|
|
|
|
|
// Multiple attention links allowed
|
|
|
|
|
|
|
|
$links = $element->getElementsByTagNameNS(ActivityUtils::ATOM, ActivityUtils::LINK);
|
|
|
|
|
|
|
|
for ($i = 0; $i < $links->length; $i++) {
|
|
|
|
|
|
|
|
$link = $links->item($i);
|
|
|
|
|
|
|
|
$linkRel = $link->getAttribute(ActivityUtils::REL);
|
|
|
|
|
|
|
|
if ($linkRel == self::ATTENTION) {
|
|
|
|
$this->attention[] = $link->getAttribute(self::HREF);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Parse location given as a GeoRSS-simple point, if provided.
|
|
|
|
* http://www.georss.org/simple
|
|
|
|
*
|
|
|
|
* @param feed item $entry
|
|
|
|
* @return mixed Location or false
|
|
|
|
*/
|
|
|
|
function getLocation($dom)
|
|
|
|
{
|
|
|
|
$points = $dom->getElementsByTagNameNS(self::GEORSS, self::POINT);
|
|
|
|
|
|
|
|
for ($i = 0; $i < $points->length; $i++) {
|
|
|
|
$point = $points->item($i)->textContent;
|
2010-02-25 09:51:24 +09:00
|
|
|
return self::locationFromPoint($point);
|
2010-02-19 12:18:14 +09:00
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
2010-02-25 09:51:24 +09:00
|
|
|
|
|
|
|
// XXX: Move to ActivityUtils or Location?
|
|
|
|
static function locationFromPoint($point)
|
|
|
|
{
|
|
|
|
$point = str_replace(',', ' ', $point); // per spec "treat commas as whitespace"
|
|
|
|
$point = preg_replace('/\s+/', ' ', $point);
|
|
|
|
$point = trim($point);
|
|
|
|
$coords = explode(' ', $point);
|
|
|
|
if (count($coords) == 2) {
|
|
|
|
list($lat, $lon) = $coords;
|
|
|
|
if (is_numeric($lat) && is_numeric($lon)) {
|
|
|
|
common_log(LOG_INFO, "Looking up location for $lat $lon from georss point");
|
|
|
|
return Location::fromLatLon($lat, $lon);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
common_log(LOG_ERR, "Ignoring bogus georss:point value $point");
|
|
|
|
return null;
|
|
|
|
}
|
2010-02-19 12:18:14 +09:00
|
|
|
}
|
|
|
|
|
2010-02-15 02:12:47 +09:00
|
|
|
/**
|
|
|
|
* An activity in the ActivityStrea.ms world
|
|
|
|
*
|
|
|
|
* An activity is kind of like a sentence: someone did something
|
|
|
|
* to something else.
|
|
|
|
*
|
|
|
|
* 'someone' is the 'actor'; 'did something' is the verb;
|
|
|
|
* 'something else' is the object.
|
|
|
|
*
|
|
|
|
* @category OStatus
|
|
|
|
* @package StatusNet
|
|
|
|
* @author Evan Prodromou <evan@status.net>
|
|
|
|
* @copyright 2010 StatusNet, Inc.
|
|
|
|
* @license http://www.fsf.org/licensing/licenses/agpl-3.0.html AGPLv3
|
|
|
|
* @link http://status.net/
|
|
|
|
*/
|
|
|
|
|
|
|
|
class Activity
|
|
|
|
{
|
|
|
|
const SPEC = 'http://activitystrea.ms/spec/1.0/';
|
|
|
|
const SCHEMA = 'http://activitystrea.ms/schema/1.0/';
|
|
|
|
|
|
|
|
const VERB = 'verb';
|
|
|
|
const OBJECT = 'object';
|
|
|
|
const ACTOR = 'actor';
|
|
|
|
const SUBJECT = 'subject';
|
|
|
|
const OBJECTTYPE = 'object-type';
|
|
|
|
const CONTEXT = 'context';
|
|
|
|
const TARGET = 'target';
|
|
|
|
|
|
|
|
const ATOM = 'http://www.w3.org/2005/Atom';
|
2010-02-12 14:42:42 +09:00
|
|
|
|
2010-02-15 02:12:47 +09:00
|
|
|
const AUTHOR = 'author';
|
|
|
|
const PUBLISHED = 'published';
|
2010-02-15 03:19:32 +09:00
|
|
|
const UPDATED = 'updated';
|
2010-02-12 14:42:42 +09:00
|
|
|
|
2010-03-19 23:48:39 +09:00
|
|
|
const RSS = null; // no namespace!
|
|
|
|
|
|
|
|
const PUBDATE = 'pubDate';
|
|
|
|
const DESCRIPTION = 'description';
|
|
|
|
const GUID = 'guid';
|
|
|
|
const SELF = 'self';
|
|
|
|
const IMAGE = 'image';
|
|
|
|
const URL = 'url';
|
|
|
|
|
|
|
|
const DC = 'http://purl.org/dc/elements/1.1/';
|
|
|
|
|
|
|
|
const CREATOR = 'creator';
|
|
|
|
|
|
|
|
const CONTENTNS = 'http://purl.org/rss/1.0/modules/content/';
|
|
|
|
|
2010-02-15 02:12:47 +09:00
|
|
|
public $actor; // an ActivityObject
|
|
|
|
public $verb; // a string (the URL)
|
|
|
|
public $object; // an ActivityObject
|
|
|
|
public $target; // an ActivityObject
|
|
|
|
public $context; // an ActivityObject
|
|
|
|
public $time; // Time of the activity
|
|
|
|
public $link; // an ActivityObject
|
|
|
|
public $entry; // the source entry
|
|
|
|
public $feed; // the source feed
|
|
|
|
|
2010-02-22 00:50:51 +09:00
|
|
|
public $summary; // summary of activity
|
|
|
|
public $content; // HTML content of activity
|
|
|
|
public $id; // ID of the activity
|
|
|
|
public $title; // title of the activity
|
2010-02-26 04:26:33 +09:00
|
|
|
public $categories = array(); // list of AtomCategory objects
|
2010-03-03 09:30:09 +09:00
|
|
|
public $enclosures = array(); // list of enclosure URL references
|
2010-02-22 00:50:51 +09:00
|
|
|
|
2010-02-15 02:12:47 +09:00
|
|
|
/**
|
|
|
|
* Turns a regular old Atom <entry> into a magical activity
|
|
|
|
*
|
|
|
|
* @param DOMElement $entry Atom entry to poke at
|
|
|
|
* @param DOMElement $feed Atom feed, for context
|
|
|
|
*/
|
|
|
|
|
2010-02-22 00:50:51 +09:00
|
|
|
function __construct($entry = null, $feed = null)
|
2010-02-12 14:42:42 +09:00
|
|
|
{
|
2010-02-22 00:50:51 +09:00
|
|
|
if (is_null($entry)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2010-03-16 11:06:06 +09:00
|
|
|
// Insist on a feed's root DOMElement; don't allow a DOMDocument
|
2010-03-04 13:55:53 +09:00
|
|
|
if ($feed instanceof DOMDocument) {
|
2010-03-16 11:06:06 +09:00
|
|
|
throw new ClientException(
|
|
|
|
_("Expecting a root feed element but got a whole XML document.")
|
2010-03-04 13:55:53 +09:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2010-03-19 23:48:39 +09:00
|
|
|
$this->entry = $entry;
|
2010-02-15 02:12:47 +09:00
|
|
|
$this->feed = $feed;
|
|
|
|
|
2010-03-19 23:48:39 +09:00
|
|
|
if ($entry->namespaceURI == Activity::ATOM &&
|
|
|
|
$entry->localName == 'entry') {
|
|
|
|
$this->_fromAtomEntry($entry, $feed);
|
|
|
|
} else if ($entry->namespaceURI == Activity::RSS &&
|
|
|
|
$entry->localName == 'item') {
|
|
|
|
$this->_fromRssItem($entry, $feed);
|
|
|
|
} else {
|
|
|
|
throw new Exception("Unknown DOM element: {$entry->namespaceURI} {$entry->localName}");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function _fromAtomEntry($entry, $feed)
|
|
|
|
{
|
2010-02-15 02:12:47 +09:00
|
|
|
$pubEl = $this->_child($entry, self::PUBLISHED, self::ATOM);
|
|
|
|
|
|
|
|
if (!empty($pubEl)) {
|
|
|
|
$this->time = strtotime($pubEl->textContent);
|
|
|
|
} else {
|
|
|
|
// XXX technically an error; being liberal. Good idea...?
|
2010-02-15 03:19:32 +09:00
|
|
|
$updateEl = $this->_child($entry, self::UPDATED, self::ATOM);
|
|
|
|
if (!empty($updateEl)) {
|
|
|
|
$this->time = strtotime($updateEl->textContent);
|
|
|
|
} else {
|
|
|
|
$this->time = null;
|
|
|
|
}
|
2010-02-15 02:12:47 +09:00
|
|
|
}
|
|
|
|
|
2010-02-17 02:49:54 +09:00
|
|
|
$this->link = ActivityUtils::getPermalink($entry);
|
2010-02-15 02:12:47 +09:00
|
|
|
|
|
|
|
$verbEl = $this->_child($entry, self::VERB);
|
|
|
|
|
|
|
|
if (!empty($verbEl)) {
|
|
|
|
$this->verb = trim($verbEl->textContent);
|
|
|
|
} else {
|
|
|
|
$this->verb = ActivityVerb::POST;
|
|
|
|
// XXX: do other implied stuff here
|
|
|
|
}
|
|
|
|
|
|
|
|
$objectEl = $this->_child($entry, self::OBJECT);
|
|
|
|
|
|
|
|
if (!empty($objectEl)) {
|
|
|
|
$this->object = new ActivityObject($objectEl);
|
|
|
|
} else {
|
|
|
|
$this->object = new ActivityObject($entry);
|
|
|
|
}
|
|
|
|
|
|
|
|
$actorEl = $this->_child($entry, self::ACTOR);
|
|
|
|
|
|
|
|
if (!empty($actorEl)) {
|
|
|
|
|
|
|
|
$this->actor = new ActivityObject($actorEl);
|
|
|
|
|
|
|
|
} else if (!empty($feed) &&
|
|
|
|
$subjectEl = $this->_child($feed, self::SUBJECT)) {
|
|
|
|
|
|
|
|
$this->actor = new ActivityObject($subjectEl);
|
|
|
|
|
|
|
|
} else if ($authorEl = $this->_child($entry, self::AUTHOR, self::ATOM)) {
|
|
|
|
|
|
|
|
$this->actor = new ActivityObject($authorEl);
|
2010-02-15 03:19:32 +09:00
|
|
|
|
|
|
|
} else if (!empty($feed) && $authorEl = $this->_child($feed, self::AUTHOR,
|
|
|
|
self::ATOM)) {
|
|
|
|
|
|
|
|
$this->actor = new ActivityObject($authorEl);
|
2010-02-15 02:12:47 +09:00
|
|
|
}
|
|
|
|
|
|
|
|
$contextEl = $this->_child($entry, self::CONTEXT);
|
|
|
|
|
|
|
|
if (!empty($contextEl)) {
|
2010-02-19 12:18:14 +09:00
|
|
|
$this->context = new ActivityContext($contextEl);
|
|
|
|
} else {
|
|
|
|
$this->context = new ActivityContext($entry);
|
2010-02-15 02:12:47 +09:00
|
|
|
}
|
|
|
|
|
|
|
|
$targetEl = $this->_child($entry, self::TARGET);
|
|
|
|
|
|
|
|
if (!empty($targetEl)) {
|
|
|
|
$this->target = new ActivityObject($targetEl);
|
|
|
|
}
|
2010-02-22 00:50:51 +09:00
|
|
|
|
|
|
|
$this->summary = ActivityUtils::childContent($entry, 'summary');
|
|
|
|
$this->id = ActivityUtils::childContent($entry, 'id');
|
|
|
|
$this->content = ActivityUtils::getContent($entry);
|
2010-02-26 04:26:33 +09:00
|
|
|
|
|
|
|
$catEls = $entry->getElementsByTagNameNS(self::ATOM, 'category');
|
|
|
|
if ($catEls) {
|
|
|
|
for ($i = 0; $i < $catEls->length; $i++) {
|
|
|
|
$catEl = $catEls->item($i);
|
|
|
|
$this->categories[] = new AtomCategory($catEl);
|
|
|
|
}
|
|
|
|
}
|
2010-03-03 09:30:09 +09:00
|
|
|
|
|
|
|
foreach (ActivityUtils::getLinks($entry, 'enclosure') as $link) {
|
|
|
|
$this->enclosures[] = $link->getAttribute('href');
|
|
|
|
}
|
2010-02-12 14:42:42 +09:00
|
|
|
}
|
|
|
|
|
2010-03-19 23:48:39 +09:00
|
|
|
function _fromRssItem($item, $rss)
|
|
|
|
{
|
|
|
|
$verbEl = $this->_child($item, self::VERB);
|
|
|
|
|
|
|
|
if (!empty($verbEl)) {
|
|
|
|
$this->verb = trim($verbEl->textContent);
|
|
|
|
} else {
|
|
|
|
$this->verb = ActivityVerb::POST;
|
|
|
|
// XXX: do other implied stuff here
|
|
|
|
}
|
|
|
|
|
|
|
|
$pubDateEl = $this->_child($item, self::PUBDATE, self::RSS);
|
|
|
|
|
|
|
|
if (!empty($pubDateEl)) {
|
|
|
|
$this->time = strtotime($pubDateEl->textContent);
|
|
|
|
}
|
|
|
|
|
|
|
|
$authorEl = $this->_child($item, self::AUTHOR, self::RSS);
|
|
|
|
|
|
|
|
if (!empty($authorEl)) {
|
|
|
|
$this->actor = $this->_fromRssAuthor($authorEl);
|
|
|
|
} else {
|
|
|
|
$dcCreatorEl = $this->_child($item, self::CREATOR, self::DC);
|
|
|
|
if (!empty($dcCreatorEl)) {
|
|
|
|
$this->actor = $this->_fromDcCreator($dcCreatorEl);
|
|
|
|
} else if (!empty($rss)) {
|
|
|
|
$this->actor = $this->_fromRss($rss);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->title = ActivityUtils::childContent($item, ActivityObject::TITLE, self::RSS);
|
|
|
|
|
|
|
|
$contentEl = ActivityUtils::child($item, ActivityUtils::CONTENT, self::CONTENTNS);
|
|
|
|
|
|
|
|
if (!empty($contentEl)) {
|
|
|
|
$this->content = htmlspecialchars_decode($contentEl->textContent, ENT_QUOTES);
|
|
|
|
} else {
|
|
|
|
$descriptionEl = ActivityUtils::child($item, self::DESCRIPTION, self::RSS);
|
|
|
|
if (!empty($descriptionEl)) {
|
|
|
|
$this->content = htmlspecialchars_decode($descriptionEl->textContent, ENT_QUOTES);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->link = ActivityUtils::childContent($item, ActivityUtils::LINK, self::RSS);
|
|
|
|
|
|
|
|
// @fixme enclosures
|
|
|
|
// @fixme thumbnails... maybe
|
|
|
|
|
|
|
|
$guidEl = ActivityUtils::child($item, self::GUID, self::RSS);
|
|
|
|
|
|
|
|
if (!empty($guidEl)) {
|
|
|
|
$this->id = $guidEl->textContent;
|
|
|
|
|
|
|
|
if ($guidEl->hasAttribute('isPermaLink') && $guidEl->getAttribute('isPermaLink') != 'false') {
|
|
|
|
// overwrites <link>
|
|
|
|
$this->link = $this->id;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->object = new ActivityObject($item);
|
|
|
|
$this->context = new ActivityContext($item);
|
|
|
|
}
|
|
|
|
|
2010-02-15 02:12:47 +09:00
|
|
|
/**
|
|
|
|
* Returns an Atom <entry> based on this activity
|
|
|
|
*
|
|
|
|
* @return DOMElement Atom entry
|
|
|
|
*/
|
|
|
|
|
2010-02-12 14:42:42 +09:00
|
|
|
function toAtomEntry()
|
|
|
|
{
|
2010-02-15 02:12:47 +09:00
|
|
|
return null;
|
2010-02-12 14:42:42 +09:00
|
|
|
}
|
2010-02-15 02:12:47 +09:00
|
|
|
|
2010-02-22 00:50:51 +09:00
|
|
|
function asString($namespace=false)
|
|
|
|
{
|
|
|
|
$xs = new XMLStringer(true);
|
|
|
|
|
|
|
|
if ($namespace) {
|
|
|
|
$attrs = array('xmlns' => 'http://www.w3.org/2005/Atom',
|
|
|
|
'xmlns:activity' => 'http://activitystrea.ms/spec/1.0/',
|
2010-02-24 05:44:27 +09:00
|
|
|
'xmlns:georss' => 'http://www.georss.org/georss',
|
|
|
|
'xmlns:ostatus' => 'http://ostatus.org/schema/1.0',
|
2010-02-26 09:06:49 +09:00
|
|
|
'xmlns:poco' => 'http://portablecontacts.net/spec/1.0',
|
|
|
|
'xmlns:media' => 'http://purl.org/syndication/atommedia');
|
2010-02-22 00:50:51 +09:00
|
|
|
} else {
|
|
|
|
$attrs = array();
|
|
|
|
}
|
|
|
|
|
|
|
|
$xs->elementStart('entry', $attrs);
|
|
|
|
|
|
|
|
$xs->element('id', null, $this->id);
|
|
|
|
$xs->element('title', null, $this->title);
|
|
|
|
$xs->element('published', null, common_date_iso8601($this->time));
|
|
|
|
$xs->element('content', array('type' => 'html'), $this->content);
|
|
|
|
|
|
|
|
if (!empty($this->summary)) {
|
|
|
|
$xs->element('summary', null, $this->summary);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!empty($this->link)) {
|
|
|
|
$xs->element('link', array('rel' => 'alternate',
|
|
|
|
'type' => 'text/html'),
|
|
|
|
$this->link);
|
|
|
|
}
|
|
|
|
|
|
|
|
// XXX: add context
|
|
|
|
|
2010-02-25 02:36:31 +09:00
|
|
|
$xs->elementStart('author');
|
|
|
|
$xs->element('uri', array(), $this->actor->id);
|
|
|
|
if ($this->actor->title) {
|
|
|
|
$xs->element('name', array(), $this->actor->title);
|
|
|
|
}
|
|
|
|
$xs->elementEnd('author');
|
2010-02-22 07:00:05 +09:00
|
|
|
$xs->raw($this->actor->asString('activity:actor'));
|
2010-02-25 02:36:31 +09:00
|
|
|
|
2010-02-22 00:50:51 +09:00
|
|
|
$xs->element('activity:verb', null, $this->verb);
|
2010-02-25 02:36:31 +09:00
|
|
|
|
|
|
|
if ($this->object) {
|
|
|
|
$xs->raw($this->object->asString());
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($this->target) {
|
|
|
|
$xs->raw($this->target->asString('activity:target'));
|
|
|
|
}
|
2010-02-22 00:50:51 +09:00
|
|
|
|
2010-02-26 04:26:33 +09:00
|
|
|
foreach ($this->categories as $cat) {
|
|
|
|
$xs->raw($cat->asString());
|
|
|
|
}
|
|
|
|
|
2010-02-22 00:50:51 +09:00
|
|
|
$xs->elementEnd('entry');
|
|
|
|
|
|
|
|
return $xs->getString();
|
|
|
|
}
|
|
|
|
|
2010-03-19 23:48:39 +09:00
|
|
|
function _fromRssAuthor($el)
|
|
|
|
{
|
|
|
|
$text = $el->textContent;
|
|
|
|
|
|
|
|
if (preg_match('/^(.*?) \((.*)\)$/', $text, $match)) {
|
|
|
|
$email = $match[1];
|
|
|
|
$name = $match[2];
|
|
|
|
} else if (preg_match('/^(.*?) <(.*)>$/', $text, $match)) {
|
|
|
|
$name = $match[1];
|
|
|
|
$email = $match[2];
|
|
|
|
} else if (preg_match('/.*@.*/', $text)) {
|
|
|
|
$email = $text;
|
|
|
|
$name = null;
|
|
|
|
} else {
|
|
|
|
$name = $text;
|
|
|
|
$email = null;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Not really enough info
|
|
|
|
|
|
|
|
$actor = new ActivityObject();
|
|
|
|
|
|
|
|
$actor->element = $el;
|
|
|
|
|
|
|
|
$actor->type = ActivityObject::PERSON;
|
|
|
|
$actor->title = $name;
|
|
|
|
|
|
|
|
if (!empty($email)) {
|
|
|
|
$actor->id = 'mailto:'.$email;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $actor;
|
|
|
|
}
|
|
|
|
|
|
|
|
function _fromDcCreator($el)
|
|
|
|
{
|
|
|
|
// Not really enough info
|
|
|
|
|
|
|
|
$text = $el->textContent;
|
|
|
|
|
|
|
|
$actor = new ActivityObject();
|
|
|
|
|
|
|
|
$actor->element = $el;
|
|
|
|
|
|
|
|
$actor->title = $text;
|
|
|
|
$actor->type = ActivityObject::PERSON;
|
|
|
|
|
|
|
|
return $actor;
|
|
|
|
}
|
|
|
|
|
|
|
|
function _fromRss($el)
|
|
|
|
{
|
|
|
|
$actor = new ActivityObject();
|
|
|
|
|
|
|
|
$actor->element = $el;
|
|
|
|
|
|
|
|
$actor->type = ActivityObject::PERSON; // @fixme guess better
|
|
|
|
|
|
|
|
$actor->title = ActivityUtils::childContent($el, ActivityObject::TITLE, self::RSS);
|
|
|
|
$actor->link = ActivityUtils::childContent($el, ActivityUtils::LINK, self::RSS);
|
|
|
|
$actor->id = ActivityUtils::getLink($el, self::SELF);
|
|
|
|
|
|
|
|
$desc = ActivityUtils::childContent($el, self::DESCRIPTION, self::RSS);
|
|
|
|
|
|
|
|
if (!empty($desc)) {
|
|
|
|
$actor->content = htmlspecialchars_decode($desc, ENT_QUOTES);
|
|
|
|
}
|
|
|
|
|
|
|
|
$imageEl = ActivityUtils::child($el, self::IMAGE, self::RSS);
|
|
|
|
|
|
|
|
if (!empty($imageEl)) {
|
|
|
|
$actor->avatarLinks[] = ActivityUtils::childContent($imageEl, self::URL, self::RSS);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $actor;
|
|
|
|
}
|
|
|
|
|
2010-02-15 02:12:47 +09:00
|
|
|
private function _child($element, $tag, $namespace=self::SPEC)
|
|
|
|
{
|
2010-02-17 02:49:54 +09:00
|
|
|
return ActivityUtils::child($element, $tag, $namespace);
|
2010-02-15 02:12:47 +09:00
|
|
|
}
|
2010-02-26 04:26:33 +09:00
|
|
|
}
|
|
|
|
|
|
|
|
class AtomCategory
|
|
|
|
{
|
|
|
|
public $term;
|
|
|
|
public $scheme;
|
|
|
|
public $label;
|
|
|
|
|
|
|
|
function __construct($element=null)
|
|
|
|
{
|
|
|
|
if ($element && $element->attributes) {
|
|
|
|
$this->term = $this->extract($element, 'term');
|
|
|
|
$this->scheme = $this->extract($element, 'scheme');
|
|
|
|
$this->label = $this->extract($element, 'label');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
protected function extract($element, $attrib)
|
|
|
|
{
|
|
|
|
$node = $element->attributes->getNamedItemNS(Activity::ATOM, $attrib);
|
|
|
|
if ($node) {
|
|
|
|
return trim($node->textContent);
|
|
|
|
}
|
|
|
|
$node = $element->attributes->getNamedItem($attrib);
|
|
|
|
if ($node) {
|
|
|
|
return trim($node->textContent);
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
function asString()
|
|
|
|
{
|
|
|
|
$attribs = array();
|
|
|
|
if ($this->term !== null) {
|
|
|
|
$attribs['term'] = $this->term;
|
|
|
|
}
|
|
|
|
if ($this->scheme !== null) {
|
|
|
|
$attribs['scheme'] = $this->scheme;
|
|
|
|
}
|
|
|
|
if ($this->label !== null) {
|
|
|
|
$attribs['label'] = $this->label;
|
|
|
|
}
|
|
|
|
$xs = new XMLStringer();
|
|
|
|
$xs->element('category', $attribs);
|
|
|
|
return $xs->asString();
|
|
|
|
}
|
|
|
|
}
|