2014-05-26 21:45:10 +04:00
|
|
|
<?php
|
2016-08-04 21:06:53 +03:00
|
|
|
define('WORDPRESS_TYPE_ATOM', 1); // Content is of type ATOM
|
|
|
|
define('WORDPRESS_TYPE_RSS', 2); // Content is of type RSS
|
2014-07-14 21:12:01 +04:00
|
|
|
class WordPressBridge extends BridgeAbstract {
|
2014-05-26 21:45:10 +04:00
|
|
|
|
|
|
|
private $url;
|
|
|
|
|
2015-11-05 18:50:18 +03:00
|
|
|
public function loadMetadatas() {
|
|
|
|
|
|
|
|
$this->maintainer = "aledeg";
|
|
|
|
$this->name = "Wordpress Bridge";
|
2016-08-02 16:46:21 +03:00
|
|
|
$this->uri = "https://wordpress.org/";
|
2015-11-05 18:50:18 +03:00
|
|
|
$this->description = "Returns the 3 newest full posts of a Wordpress blog";
|
2016-08-04 21:06:53 +03:00
|
|
|
$this->update = "2016-08-04";
|
2015-11-05 18:50:18 +03:00
|
|
|
|
|
|
|
$this->parameters[] =
|
|
|
|
'[
|
|
|
|
{
|
|
|
|
"name" : "blog URL",
|
|
|
|
"required" : "true",
|
|
|
|
"identifier" : "url"
|
|
|
|
}
|
|
|
|
]';
|
|
|
|
}
|
|
|
|
|
2016-08-04 21:06:53 +03:00
|
|
|
// Returns the content type for a given html dom
|
|
|
|
function DetectContentType($html){
|
|
|
|
if($html->find('entry'))
|
|
|
|
return WORDPRESS_TYPE_ATOM;
|
|
|
|
if($html->find('item'))
|
|
|
|
return WORDPRESS_TYPE_RSS;
|
|
|
|
return WORDPRESS_TYPE_ATOM; // Make ATOM default
|
|
|
|
}
|
|
|
|
|
|
|
|
// Replaces all 'link' tags with 'url' for simplehtmldom to actually find 'links' ('url')
|
|
|
|
function ReplaceLinkTagsWithUrlTags($element){
|
|
|
|
// We need to fix the 'link' tag as simplehtmldom cannot parse it (just rename it and load back as dom)
|
|
|
|
$element_text = $element->outertext;
|
|
|
|
$element_text = str_replace('<link>', '<url>', $element_text);
|
|
|
|
$element_text = str_replace('</link>', '</url>', $element_text);
|
2016-08-04 21:40:38 +03:00
|
|
|
$element_text = str_replace('<link ', '<url ', $element_text);
|
2016-08-04 21:06:53 +03:00
|
|
|
return str_get_html($element_text);
|
|
|
|
}
|
|
|
|
|
2016-08-04 21:15:28 +03:00
|
|
|
function StripCDATA($string) {
|
|
|
|
$string = str_replace('<![CDATA[', '', $string);
|
|
|
|
$string = str_replace(']]>', '', $string);
|
|
|
|
return $string;
|
|
|
|
}
|
2016-08-02 16:46:21 +03:00
|
|
|
|
2016-08-04 21:15:28 +03:00
|
|
|
function ClearContent($content) {
|
|
|
|
$content = preg_replace('/<script.*\/script>/', '', $content);
|
|
|
|
$content = preg_replace('/<div class="wpa".*/', '', $content);
|
|
|
|
return $content;
|
|
|
|
}
|
2016-08-02 16:46:21 +03:00
|
|
|
|
2016-08-04 21:15:28 +03:00
|
|
|
public function collectData(array $param) {
|
2014-05-26 21:45:10 +04:00
|
|
|
$this->processParams($param);
|
|
|
|
|
|
|
|
if (!$this->hasUrl()) {
|
|
|
|
$this->returnError('You must specify a URL', 400);
|
|
|
|
}
|
|
|
|
|
2016-08-04 21:45:21 +03:00
|
|
|
$this->url = $this->url.'/feed/atom';
|
2016-06-26 00:17:42 +03:00
|
|
|
$html = $this->file_get_html($this->url) or $this->returnError("Could not request {$this->url}.", 404);
|
2016-08-04 21:06:53 +03:00
|
|
|
|
|
|
|
// Notice: We requested an ATOM feed, however some sites return RSS feeds instead!
|
|
|
|
$type = $this->DetectContentType($html);
|
|
|
|
|
|
|
|
if($type === WORDPRESS_TYPE_RSS)
|
|
|
|
$posts = $html->find('item');
|
|
|
|
else
|
|
|
|
$posts = $html->find('entry');
|
|
|
|
|
2016-08-04 21:45:21 +03:00
|
|
|
if(!empty($posts) ) {
|
|
|
|
$this->name = $html->find('title', 0)->plaintext;
|
|
|
|
$i=0;
|
2016-08-04 21:06:53 +03:00
|
|
|
|
|
|
|
foreach ($posts as $article) {
|
2014-05-26 21:45:10 +04:00
|
|
|
if($i < 3) {
|
2016-08-02 16:46:21 +03:00
|
|
|
|
2016-08-04 21:12:51 +03:00
|
|
|
$item = new \Item();
|
|
|
|
|
2016-08-04 21:06:53 +03:00
|
|
|
$article = $this->ReplaceLinkTagsWithUrlTags($article);
|
|
|
|
|
|
|
|
if($type === WORDPRESS_TYPE_RSS){
|
2016-08-04 21:12:51 +03:00
|
|
|
$item->uri = $article->find('url', 0)->innertext; // 'link' => 'url'!
|
|
|
|
$item->title = $article->find('title', 0)->plaintext;
|
2016-08-04 22:06:12 +03:00
|
|
|
$item->author = trim($this->StripCDATA($article->find('dc:creator', 0)->innertext));
|
2016-08-04 21:12:51 +03:00
|
|
|
$item->timestamp = strtotime($article->find('pubDate', 0)->innertext);
|
2016-08-04 21:06:53 +03:00
|
|
|
} else {
|
2016-08-04 21:12:51 +03:00
|
|
|
$item->uri = $article->find('url', 0)->getAttribute('href'); // 'link' => 'url'!
|
2016-08-04 21:15:28 +03:00
|
|
|
$item->title = $this->StripCDATA($article->find('title', 0)->plaintext);
|
2016-08-04 21:12:51 +03:00
|
|
|
$item->author = trim($article->find('author', 0)->innertext);
|
|
|
|
$item->timestamp = strtotime($article->find('updated', 0)->innertext);
|
2016-08-04 21:06:53 +03:00
|
|
|
}
|
2016-08-04 21:40:38 +03:00
|
|
|
|
2016-08-04 21:45:21 +03:00
|
|
|
$article_html = $this->file_get_html($item->uri);
|
2016-08-04 21:40:38 +03:00
|
|
|
|
2016-08-04 22:06:12 +03:00
|
|
|
// Attempt to find most common content div
|
|
|
|
if(empty($item->content)){
|
|
|
|
$article = $article_html->find('article', 0);
|
|
|
|
if(!empty($article)){
|
|
|
|
$item->content = $this->ClearContent($article->innertext);
|
|
|
|
}
|
2016-08-04 21:40:38 +03:00
|
|
|
}
|
2016-08-04 22:06:12 +03:00
|
|
|
|
|
|
|
// another common content div
|
|
|
|
if(empty($item->content)){
|
|
|
|
$article = $article_html->find('.single-content', 0);
|
|
|
|
if(!empty($article)){
|
|
|
|
$item->content = $this->ClearContent($article->innertext);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// for old WordPress themes without HTML5
|
|
|
|
if(empty($item->content)){
|
|
|
|
$article = $article_html->find('.post', 0);
|
|
|
|
if(!empty($article)){
|
|
|
|
$item->content = $this->ClearContent($article->innertext);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-08-04 21:12:51 +03:00
|
|
|
$this->items[] = $item;
|
2014-05-26 21:45:10 +04:00
|
|
|
$i++;
|
|
|
|
}
|
2016-08-02 16:46:21 +03:00
|
|
|
}
|
2016-08-04 21:45:21 +03:00
|
|
|
} else {
|
2014-05-26 21:45:10 +04:00
|
|
|
$this->returnError("Sorry, {$this->url} doesn't seem to be a Wordpress blog.", 404);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getName() {
|
|
|
|
return "{$this->name} - Wordpress Bridge";
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getURI() {
|
|
|
|
return $this->url;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getCacheDuration() {
|
|
|
|
return 3600*3; // 3 hours
|
|
|
|
}
|
|
|
|
|
|
|
|
private function hasUrl() {
|
|
|
|
if (empty($this->url)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
private function processParams($param) {
|
|
|
|
$this->url = $param['url'];
|
|
|
|
}
|
|
|
|
}
|