git.gnu.io has moved to IP address 209.51.188.249 -- please double check where you are logging in.

Commit 99454be3 authored by Evan Prodromou's avatar Evan Prodromou

Move activity classes to their own files

Moved the various classes used by the Activity class to their own
files. There were >10 classes in the same file, with around 1500 lines
in the file. Just too big.

This change makes autoloading work for these classes, so also removed
the hard require in lib/common.php.
parent fb2b45c6
This diff is collapsed.
<?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/>.
*
* @category Feed
* @package StatusNet
* @author Evan Prodromou <evan@status.net>
* @author Zach Copley <zach@status.net>
* @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);
}
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;
return self::locationFromPoint($point);
}
return null;
}
// 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;
}
}
This diff is collapsed.
<?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/>.
*
* @category Feed
* @package StatusNet
* @author Evan Prodromou <evan@status.net>
* @author Zach Copley <zach@status.net>
* @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);
}
/**
* 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';
const CONTENT = 'content';
const SRC = 'src';
/**
* Get the permalink for an Activity object
*
* @param DOMElement $element A DOM element
*
* @return string related link, if any
*/
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
*/
static function getLink(DOMNode $element, $rel, $type=null)
{
$els = $element->childNodes;
foreach ($els as $link) {
if (!($link instanceof DOMElement)) {
continue;
}
if ($link->localName == self::LINK && $link->namespaceURI == self::ATOM) {
$linkRel = $link->getAttribute(self::REL);
$linkType = $link->getAttribute(self::TYPE);
if ($linkRel == $rel &&
(is_null($type) || $linkType == $type)) {
return $link->getAttribute(self::HREF);
}
}
}
return null;
}
static function getLinks(DOMNode $element, $rel, $type=null)
{
$els = $element->childNodes;
$out = array();
foreach ($els as $link) {
if ($link->localName == self::LINK && $link->namespaceURI == self::ATOM) {
$linkRel = $link->getAttribute(self::REL);
$linkType = $link->getAttribute(self::TYPE);
if ($linkRel == $rel &&
(is_null($type) || $linkType == $type)) {
$out[] = $link;
}
}
}
return $out;
}
/**
* 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
*/
static function child(DOMNode $element, $tag, $namespace=self::ATOM)
{
$els = $element->childNodes;
if (empty($els) || $els->length == 0) {
return null;
} else {
for ($i = 0; $i < $els->length; $i++) {
$el = $els->item($i);
if ($el->localName == $tag && $el->namespaceURI == $namespace) {
return $el;
}
}
}
}
/**
* 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
*/
static function childContent(DOMNode $element, $tag, $namespace=self::ATOM)
{
$el = self::child($element, $tag, $namespace);
if (empty($el)) {
return null;
} else {
return $el->textContent;
}
}
static function childHtmlContent(DOMNode $element, $tag, $namespace=self::ATOM)
{
$el = self::child($element, $tag, $namespace);
if (empty($el)) {
return null;
} else {
return self::textConstruct($el);
}
}
/**
* 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 -&lt; 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)
{
return self::childHtmlContent($element, self::CONTENT, self::ATOM);
}
static function textConstruct($el)
{
$src = $el->getAttribute(self::SRC);
if (!empty($src)) {
throw new ClientException(_("Can't handle remote content yet."));
}
$type = $el->getAttribute(self::TYPE);
// slavishly following http://atompub.org/rfc4287.html#rfc.section.4.1.3.3
if (empty($type) || $type == 'text') {
return $el->textContent;
} else if ($type == 'html') {
$text = $el->textContent;
return htmlspecialchars_decode($text, ENT_QUOTES);
} else if ($type == 'xhtml') {
$divEl = ActivityUtils::child($el, 'div', 'http://www.w3.org/1999/xhtml');
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);
} else if (in_array($type, array('text/xml', 'application/xml')) ||
preg_match('#(+|/)xml$#', $type)) {
throw new ClientException(_("Can't handle embedded XML content yet."));
} else if (strncasecmp($type, 'text/', 5)) {
return $el->textContent;
} else {
throw new ClientException(_("Can't handle embedded Base64 content yet."));
}
}
}
<?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/>.
*
* @category Feed
* @package StatusNet
* @author Evan Prodromou <evan@status.net>
* @author Zach Copley <zach@status.net>
* @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);
}
/**
* 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/
*/
class ActivityVerb
{
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';
// 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';
// For simple profile-update pings; no content to share.
const UPDATE_PROFILE = 'http://ostatus.org/schema/1.0/update-profile';
}
<?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/>.
*
* @category Feed
* @package StatusNet
* @author Evan Prodromou <evan@status.net>
* @author Zach Copley <zach@status.net>
* @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);
}
// XXX: Arg! This wouldn't be necessary if we used Avatars conistently
class AvatarLink
{
public $url;
public $type;
public $size;
public $width;
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);
}
}
}
static function fromAvatar($avatar)
{
if (empty($avatar)) {
return null;
}
$alink = new AvatarLink();
$alink->type = $avatar->mediatype;
$alink->height = $avatar->height;
$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;
}
}
......@@ -123,7 +123,6 @@ require_once INSTALLDIR.'/lib/util.php';
require_once INSTALLDIR.'/lib/action.php';
require_once INSTALLDIR.'/lib/mail.php';
require_once INSTALLDIR.'/lib/subs.php';
require_once INSTALLDIR.'/lib/activity.php';
require_once INSTALLDIR.'/lib/clientexception.php';
require_once INSTALLDIR.'/lib/serverexception.php';
......
<?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/>.
*
* @category Feed
* @package StatusNet
* @author Evan Prodromou <evan@status.net>
* @author Zach Copley <zach@status.net>
* @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);
}
class PoCo
{
const NS = 'http://portablecontacts.net/spec/1.0';
const USERNAME = 'preferredUsername';
const DISPLAYNAME = 'displayName';
const NOTE = 'note';
public $preferredUsername;
public $displayName;
public $note;
public $address;
public $urls = array();
function __construct($element = null)
{
if (empty($element)) {
return;
}
$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
);
$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));
}
return $urls;
}
private function _getAddress($element)
{
$addressEl = ActivityUtils::child(
$element,
PoCoAddress::ADDRESS,
PoCo::NS
);
if (!empty($addressEl)) {
$formatted = ActivityUtils::childContent(
$addressEl,
PoCoAddress::FORMATTED,
self::NS
);
if (!empty($formatted)) {
$address = new PoCoAddress();
$address->formatted = $formatted;
return $address;
}
}
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;
if (!empty($profile->homepage)) {
array_push(
$poco->urls,
new PoCoURL(
'homepage',
$profile->homepage,
true
)
);
}
return $poco;
}
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;
}
function getPrimaryURL()
{
foreach ($this->urls as $url) {
if ($url->primary) {
return $url;