praiadeseselle/site/modules/ProcessRSSFetch/ProcessRSSFetch.module

236 lines
7 KiB
Text
Raw Normal View History

<?php
/**
* ProcessWire External RSS Feed Fetch
*
* For ProcessWire 3.x
*
* @author Laegnur
*
*/
class ProcessRSSFetch extends Process
{
protected $items = null;
protected $replacements = array(
'<br />' => ' ',
'<br>' => ' ',
'</p>' => ' ',
"\n" => ' ',
"\r" => ' ',
);
public static function getModuleInfo()
{
return array(
'title' => 'ProcessWire RSS Fetch',
'summary' => 'ProcessWire External RSS Feed Fetch',
'version' => 001,
'author' => 'Laegnur',
'href' => 'https://laegnur.artabro.org/',
'icon' => 'rss'
);
}
public function ___execute()
{
$form = $this->modules->get("InputfieldForm");
$form->action = "./";
$form->method = "post";
$form->attr("id+name",'rssfetch-form');
$fieldText = $this->modules->get("InputfieldText");
$fieldText->label = "RSS feed";
$fieldText->name = 'rss';
$fieldText->description = "URL del feed RSS";
$fieldText->columnWidth = 100;
$fieldText->required = 1;
$form->append($fieldText);
$submit = $this->modules->get("InputfieldSubmit");
$submit->attr("value","Traer posts");
$submit->attr("id+name","submit");
$form->append($submit);
$out = $form->render();
if($this->input->post->submit)
{
set_time_limit(600);
$form->processInput($this->input->post);
if(!$form->getErrors())
{
$this->items = new WireArray();
$xmlData = $this->loadXmlData($this->input->post->rss);
$rss = simplexml_load_string($xmlData);
if(!$rss)
{
$msg = "Unable to load RSS feed at $url: \n";
foreach(libxml_get_errors() as $error) $msg .= $error . " \n";
$out .= $msg;
return $out;
}
foreach($rss->channel->category as $category)
{
$name = $this->sanitizer->pageNameTranslate($category);
if(($this->pages->findOne("template=etiqueta,name=$name") instanceof NullPage))
{
$t = new Page();
$t->template = 'etiqueta';
$t->parent = wire('pages')->get('/noticias/etiquetas/');
$t->name = $name;
$t->title = $category;
$t->save();
$this->log('Creada etiqueta: ' . $category, ['name' => 'rssfetch']);
}
else
{
$this->log('Etiqueta ya existente: ' . $category, ['name' => 'rssfetch']);
}
}
foreach($rss->channel->item as $item)
{
$title = '';
$date = '';
$img = '';
$content = '';
$category = array();
$tags = array();
foreach($item as $key => $value)
{
switch ($key)
{
case 'pubDate':
$date = $value;
break;
case 'title':
$title = $value;
break;
case 'description':
$content = $this->cleanText($value);
break;
case 'media:thumbnail':
$attributes = $key->attributes();
$img = $attributes['url'];
break;
case 'category':
$tags[] = $value;
break;
default:
break;
}
}
$out .= '[ '. "\n" . 'Data: ' . $date . "\n" . 'Titulo: ' . $title . "\n" . 'Contenido: ' . $content . "\n" . 'Img: ' . $img . "\n" . 'Etiquetas: ' . implode(', ', $tags) . "\n" . ']'. "\n";
/*$p = new Page();
$p->template = 'publicacion';
$p->parent = wire('pages')->get('/noticias/publicaciones/');
$p->addStatus(Page::statusUnpublished);
$p
$p->save();*/
}
}
return $out;
}
return $out;
}
public function install()
{
$page = new Page();
$page->template = "admin";
$page->name = "feed-fetch";
$page->title = "RSS Feed Fetch";
$page->save();
$page->process = 'ProcessRSSFetch';
$admin = $this->pages->get("id=2");
$page->parent = $admin;
$page->save();
}
public function uninstall()
{
$page = $this->pages->get("name=feed-fetch");
if(count($page)) $this->pages->delete($page, true);
}
/**
* Load XML data from either the RSS URL or the cache file
*
* @param string $url
* @return bool|string
*
*/
protected function loadXmlData($url)
{
$cacheFile = $this->config->paths->cache . $this->className() . '/' . md5($url) . '.xml.cache';
if(!is_file($cacheFile) || time() - filemtime($cacheFile) > 120)
{
$http = new WireHttp();
$this->wire($http);
$xmlData = $http->get($url);
if(empty($xmlData))
{
$this->error("Unable to load: $url");
return false;
}
else
{
@file_put_contents($cacheFile, $xmlData, LOCK_EX);
}
}
else
{
$xmlData = file_get_contents($cacheFile);
if($xmlData === false)
{
$this->error("Unable to load XML data cache");
return false;
}
}
return $xmlData;
}
/**
* Cleans text string from a feed before storage in our object
*
* @param string $value
* @return string
*
*/
protected function cleanText($value)
{
if(count($this->replacements))
{
$value = str_ireplace(array_keys($this->replacements), array_values($this->replacements), $value);
}
$value = trim(strip_tags($value));
if(mb_strlen($value, "UTF-8") > 2048)
{
$value = mb_substr($value, 0, 2048, "UTF-8");
}
$sanitizer = $this->wire('sanitizer');
$value = $sanitizer->unentities($value);
$value = $sanitizer->entities($value);
return $value;
}
}