ActivityUtils.php

Summary
ActivityUtils.php
ActivityUtilsClass with helper functions for Activities
ActivityUtilsHelper class for Activity objects and related stuff
getPermalinkGet the permalink for an Activity object
getLinkGet the permalink for an Activity object
getLinksReturns an array of links in the DOMNode and its children
childGets the first child element with the given tag
childrenGets all immediate child elements with the given tag
childContentGrab the text content of a DOM element child of the current element
childHtmlContentReturns the content of a child in HTML format, if it exists.
getContentGet the content of an atom:entry-like object
textConstructConstruct the body of an activity stream activity.
validateUriIs this a valid URI for remote profile/notice identification? 
getFeedAuthorGiven a feed as a parameter, determine the author
compareTypesCompares the action type of multiple objects and returns true if they are the same type specified and false if not.
compareVerbsA synonym of compareTypes
resolveUri
findLocalObjectFind an activity object from a given URI or URIs
checkAuthorshipCheck authorship by supplying a Profile as a default and letting plugins set it to something else if the activity’s author is actually someone else (like with a group or peopletag feed as handled in OStatus).
typeToTitle
verbToTitle

ActivityUtils

Class with helper functions for Activities

postActiv

the micro-blogging software

Copyright

Copyright © 2016-2017, Maiyannah Bishop

Derived from code copyright various sources

  • GNU Social © 2013-2016, Free Software Foundation, Inc
  • StatusNet © 2008-2012, StatusNet, Inc

License

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/.

https://www.gnu.org/licenses/agpl.html

About

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.

PHP version

Tested with PHP 5.6

File Authors

Web

ActivityUtils

Helper class for Activity objects and related stuff

Defines

  • ATOM - ‘http://www.w3.org/2005/Atom’;
  • LINK - ‘link’;
  • REL - ‘rel’;
  • TYPE - ‘type’;
  • HREF - ‘href’;
  • CONTENT - ‘content’;
  • SRC - ‘src’;
Summary
getPermalinkGet the permalink for an Activity object
getLinkGet the permalink for an Activity object
getLinksReturns an array of links in the DOMNode and its children
childGets the first child element with the given tag
childrenGets all immediate child elements with the given tag
childContentGrab the text content of a DOM element child of the current element
childHtmlContentReturns the content of a child in HTML format, if it exists.
getContentGet the content of an atom:entry-like object
textConstructConstruct the body of an activity stream activity.
validateUriIs this a valid URI for remote profile/notice identification? 
getFeedAuthorGiven a feed as a parameter, determine the author
compareTypesCompares the action type of multiple objects and returns true if they are the same type specified and false if not.
compareVerbsA synonym of compareTypes
resolveUri
findLocalObjectFind an activity object from a given URI or URIs
checkAuthorshipCheck authorship by supplying a Profile as a default and letting plugins set it to something else if the activity’s author is actually someone else (like with a group or peopletag feed as handled in OStatus).
typeToTitle
verbToTitle

getPermalink

static function getPermalink($element)

Get the permalink for an Activity object

@param DOMElement $element A DOM element

@return string related link, if any

getLink

static function getLink(DOMNode $element,  
 $rel,  
 $type = null)

Get the permalink for an Activity object

Parameters

  • DOMElement $element A DOM element

Returns

  • string related link, if any, NULL if not

getLinks

static function getLinks(DOMNode $element,  
 $rel,  
 $type = null)

Returns an array of links in the DOMNode and its children

Parameters

  • DOMNode element
  • rel
  • type

Returns

  • array of strings containing links

child

static function child(DOMNode $element,  
 $tag,  
 $namespace = self::ATOM)

Gets the first child element with the given tag

Parameters

  • DOMElement $element - element to pick at
  • string $tag - tag to look for
  • string $namespace - Namespace to look under

Returns

  • DOMElement found element or null

children

static function children(DOMNode $element,  
 $tag,  
 $namespace = self::ATOM)

Gets all immediate child elements 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 array found element or null

childContent

static function childContent(DOMNode $element,  
 $tag,  
 $namespace = self::ATOM)

Grab the text content of a DOM element child of the current element

Parameters

  • DOMElement $element - Element whose children we examine
  • string $tag - Tag to look up
  • string $namespace - Namespace to use, defaults to Atom

Returns

  • string content of the child, or null if there is no child.

childHtmlContent

static function childHtmlContent(DOMNode $element,  
 $tag,  
 $namespace = self::ATOM)

Returns the content of a child in HTML format, if it exists.

Parameters

  • DOMElement $element - Element whose children we examine
  • string $tag - Tag to look up
  • string $namespace - Namespace to use, defaults to Atom

Returns

  • string containing HTML representation, or null if there is no child.

getContent

static function getContent($element)

Get the content of an atom:entry-like object

Parameters

  • DOMElement $element The element to examine.

Returns

  • string unencoded HTML content of the element, like “This -&lt; is <b>HTML</b>.”

Todo

  • handle remote content
  • handle embedded XML mime types
  • handle base64-encoded non-XML and non-text mime types

textConstruct

static function textConstruct($el)

Construct the body of an activity stream activity.

Parameters

  • el

Returns

  • string with constructed text

validateUri

static function validateUri($uri)

Is this a valid URI for remote profile/notice identification?  Does not have to be a resolvable URL.

Parameters

  • string $uri

Returns

  • boolean

getFeedAuthor

static function getFeedAuthor(DOMElement $feedEl)

Given a feed as a parameter, determine the author

Parameters

  • feedEl

Returns

  • ActivityObject representing the author or NULL if we can’t find them

compareTypes

static function compareTypes($type,
$objects)

Compares the action type of multiple objects and returns true if they are the same type specified and false if not.

Parameters

  • type
  • objects

Returns

  • boolean

compareVerbs

static function compareVerbs($type,
$objects)

A synonym of compareTypes

Parameters

  • type
  • objects

Returns

  • result of self::compareTypes with the given parameters.

resolveUri

static function resolveUri($uri,  
$make_relative = false)

Parameters

  • uri - string
  • make_relative - boolean, default false

Returns

  • string uri - parsed uri string

findLocalObject

static function findLocalObject(array $uris,  
 $type = ActivityObject::NOTE)

Find an activity object from a given URI or URIs

Parameter

  • uris - array of uris
  • type - type of object we’re trying

Returns

  • array of retrieved ActivityObjects

Error States

  • if an ActivityObject isn’t found at that URI it will raise a ServerException

checkAuthorship

static function checkAuthorship(Activity $activity,
Profile $profile)

Check authorship by supplying a Profile as a default and letting plugins set it to something else if the activity’s author is actually someone else (like with a group or peopletag feed as handled in OStatus).

NOTE: Returned is not necessarily the supplied profile!  For example, the “feed author” may be a group, but the “activity author” is a person!

Parameters

  • Activity activity - activity object we’re testing authorship of
  • Profile profile - profile of the user we think generated this

Returns

  • Profile

Error States

  • If we have a URI mismatch this is logged but does not raise an exception
  • If we don’t retrieve a valid profile for the activity we raise a ServerException

typeToTitle

static public function typeToTitle($type)

Parameters

  • type

Returns

  • string

verbToTitle

static public function verbToTitle($verb)

Paramters

  • verb

Returns

  • string
static function getPermalink($element)
Get the permalink for an Activity object
static function getLink(DOMNode $element,  
 $rel,  
 $type = null)
Get the permalink for an Activity object
static function getLinks(DOMNode $element,  
 $rel,  
 $type = null)
Returns an array of links in the DOMNode and its children
static function child(DOMNode $element,  
 $tag,  
 $namespace = self::ATOM)
Gets the first child element with the given tag
static function children(DOMNode $element,  
 $tag,  
 $namespace = self::ATOM)
Gets all immediate child elements with the given tag
static function childContent(DOMNode $element,  
 $tag,  
 $namespace = self::ATOM)
Grab the text content of a DOM element child of the current element
static function childHtmlContent(DOMNode $element,  
 $tag,  
 $namespace = self::ATOM)
Returns the content of a child in HTML format, if it exists.
static function getContent($element)
Get the content of an atom:entry-like object
static function textConstruct($el)
Construct the body of an activity stream activity.
static function validateUri($uri)
Is this a valid URI for remote profile/notice identification? 
static function getFeedAuthor(DOMElement $feedEl)
Given a feed as a parameter, determine the author
static function compareTypes($type,
$objects)
Compares the action type of multiple objects and returns true if they are the same type specified and false if not.
static function compareVerbs($type,
$objects)
A synonym of compareTypes
static function resolveUri($uri,  
$make_relative = false)
static function findLocalObject(array $uris,  
 $type = ActivityObject::NOTE)
Find an activity object from a given URI or URIs
static function checkAuthorship(Activity $activity,
Profile $profile)
Check authorship by supplying a Profile as a default and letting plugins set it to something else if the activity’s author is actually someone else (like with a group or peopletag feed as handled in OStatus).
static public function typeToTitle($type)
static public function verbToTitle($verb)