Skip to content

Updated for consistency with php-mf2 v0.2.0 #3

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Oct 21, 2013
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 3 additions & 5 deletions mf2/Shim/Instagram.php → Mf2/Shim/Instagram.php
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
<?php
namespace mf2\Shim;
namespace Mf2\Shim;

use mf2\Shim;
use Mf2\Parser;

class Instagram extends Shim {
class Instagram extends Parser {

// TODO: parse() method that returns an 'items' array that corresponds to if instagram.com
// had proper microformats markup
Expand All @@ -18,6 +18,4 @@ public function parse() {

return array('items' => array_values(array_filter($mfs)));
}


}
87 changes: 87 additions & 0 deletions Mf2/Shim/Twitter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
<?php
namespace Mf2\Shim;

use Mf2;
use DateTime;
use DOMElement;
use Exception;

function parseTwitter($html, $url=null) {
$parser = new Twitter($html, $url, false);
return array_merge($parser->parseRelsAndAlternates(), $parser->parse());
}

class Twitter extends Mf2\Parser {
public function parseTweet(DOMElement $el, $parseReplies=true) {
$tweetTextEl = $this->query('.//p' . Mf2\xpcs('tweet-text'), $el)->item(0);

$authorNameEl = $this->query('.//*' . Mf2\xpcs('fullname'), $el)->item(0);
$authorNickEl = $this->query('.//*' . Mf2\xpcs('username'), $el)->item(0);
$authorPhotoEl = $this->query('.//*' . Mf2\xpcs('avatar'), $el)->item(0);

$publishedEl = $this->query('.//*' . Mf2\xpcs('_timestamp'), $el)->item(0);
$publishedTimestamp = $publishedEl->getAttribute('data-time');
try {
$publishedDateTime = DateTime::createFromFormat('U', $publishedTimestamp)->format(DateTime::W3C);
} catch (Exception $e) {
$publishedDateTime = '';
}

$urlEl = $this->query('.//*' . Mf2\xpcs('tweet-timestamp'), $el)->item(0);

$htmlTweetContent = '';
foreach ($tweetTextEl->childNodes as $node) {
$htmlTweetContent .= $node->C14N();
}

$tweet = array(
'type' => array('h-entry'),
'properties' => array(
'uid' => array(),
'name' => array($tweetTextEl->nodeValue),
'content' => array(array(
'value' => $tweetTextEl->nodeValue,
'html' => $htmlTweetContent
)),
'summary' => array($tweetTextEl->nodeValue),
'url' => array($urlEl->getAttribute('href')),
'published' => array($publishedDateTime),
'author' => array(
array(
'type' => array('h-card'),
'properties' => array(
'uid' => array(),
'name' => array($authorNameEl->nodeValue),
'nickname' => array($authorNickEl->nodeValue),
'photo' => array($authorPhotoEl->getAttribute('src')),
'url' => array('https://twitter.com/' . $authorNickEl->nodeValue)
)
)
)
)
);

if ($parseReplies) {
foreach ($this->query('//*' . Mf2\xpcs('permalink-replies') . '//*' . Mf2\xpcs('tweet')) as $reply) {
$tweet['properties']['comment'][] = $this->parseTweet($reply, false);
}
}

return $tweet;
}

/**
* Parse
*
* @return array
*/
public function parse() {
$items = array();

foreach($this->query('//div' . Mf2\xpcs('tweet', 'permalink-tweet')) as $node) {
$items[] = $this->parseTweet($node);
}

return array('items' => array_values(array_filter($items)));
}
}
23 changes: 23 additions & 0 deletions Mf2/functions.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

namespace Mf2;

function spaceSeparatedAttributeXpathSelector($needle, $haystack="@class") {
return 'contains(concat(" ", ' . $haystack . ', " "), " ' . $needle .' ")';
}

/**
* XPath Class Selector
*
* @return string
*/
function xpcs() {
$classnames = func_get_args();
if (count($classnames) == 1) {
return '[' . spaceSeparatedAttributeXpathSelector($classnames[0]) . ']';
} else {
return '[' . implode(' and ', array_map(function ($item) {
return spaceSeparatedAttributeXpathSelector($item);
}, $classnames)) . ']';
}
}
41 changes: 38 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,44 @@
php-mf2-shim
============

A parsing library for parsing pages from a few common non-mf2 sites into the mf2 structure
A parsing library for parsing pages from a few common non-mf2 sites into the mf2 structure.

## Warning!
## Installation

This code is very much in progress at the moment! Feel free to contribute to development, or feel free to stay away from it while I keep working on it.
Install php-mf2-shim with Composer by adding "mf2/shim": "0.2.*" to the require object in your composer.json and running `php composer.phar update`.

You could install it by just downloading /Mf2/functions.php, /Mf2/Shim/*.php and including those, but please use Composer. Seriously, it’s amazing.

## Usage

mf2-shim is PSR-0 autoloadable, so all you have to do to load it is:

* Include Composer’s auto-generated autoload file (/vendor/autoload.php)
* Call Mf2\parseTwitter() with the HTML (or a DOMDocument), and optionally the URL to resolve relative URLs against.

## Examples

```php
<?php

require 'vendor/autoload.php';

use Mf2;

$output = Mf2\Shim\parseTwitter($html, $url);
```

## Changelog

### v0.2.0 (BREAKING)

* Restructured things for consistency with php-mf2 v0.2.0
* Removed dependencies, now only depends on php-mf2
* Twitter parsing code tweet content is now an e-* dict containing html and value keys with raw and plaintext values, respectively

### v0.1.0 (2013-06-07)

* Initial tagged release
* MIT Licenesed
* Twitter tests passing, Instagram test failing
* Tweet permalink page parsing working including authors and reply tweets
11 changes: 3 additions & 8 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,12 @@
],
"require": {
"php": ">=5.3.0",
"mf2/mf2": "*",
"symfony/css-selector": "*"
"mf2/mf2": "0.2.*"
},
"require-dev": {
"phpunit/phpunit": "3.7.*"
},
"autoload": {
"psr-0": {
"mf2\\Shim": "",
"mf2\\Shim\\Twitter": "",
"mf2\\Shim\\Instagram": ""
}
"files": ["Mf2/functions.php", "Mf2/Shim/Twitter.php", "Mf2/Shim/Instagram.php"]
}
}
}
Loading