parser fixed, there's a connection problem to load articles into database

feature/article
Roxane ROSSETTO 1 year ago
parent 4e99917917
commit 12913ba1e7

@ -35,10 +35,10 @@ class ArticleGateway
*/ */
public function addArticle(Article $article){ public function addArticle(Article $article){
try { try {
$query = 'INSERT INTO Article VALUES (:id,:title,to_date(:date,"Dy, DD Mon YYYY"),:description,:guid,:link,:mediaContent,:provenance);'; $query = 'INSERT INTO Article VALUES (:id,:title,datePub,:description,:guid,:link,:mediaContent,:provenance);';
$this->con->executeQuery($query, array(':id' => array($article->getId(), PDO::PARAM_STR), $this->con->executeQuery($query, array(':id' => array($article->getId(), PDO::PARAM_INT),
':title' => array($article->getTitle(), PDO::PARAM_STR), ':title' => array($article->getTitle(), PDO::PARAM_STR),
':date' => array($article->getDate(), PDO::PARAM_STR), ':datePub' => array($article->getDate(), PDO::PARAM_STR),
':description' => array($article->getDescription(), PDO::PARAM_STR), ':description' => array($article->getDescription(), PDO::PARAM_STR),
':guid' => array($article->getGuid(), PDO::PARAM_STR), ':guid' => array($article->getGuid(), PDO::PARAM_STR),
':link' => array($article->getLink(), PDO::PARAM_STR), ':link' => array($article->getLink(), PDO::PARAM_STR),

@ -42,10 +42,17 @@ class Connection extends PDO
foreach ($parameters as $name => $value) { foreach ($parameters as $name => $value) {
$this->stmt->bindValue($name, $value[0], $value[1]); $this->stmt->bindValue($name, $value[0], $value[1]);
} }
if($this->stmt->execute()){
return true;
} else {
$error = $this->stmt->errorInfo();
throw new Exception("PDO error: ".$error[2]);
}
return $this->stmt->execute();
}catch (\PDOException $e){ }catch (\PDOException $e){
throw new Exception("PDO error"); throw new Exception("PDO error: ".$e->getMessage());
}catch (\Error $r){
throw new Error("executionQuery not possible : ".$r->getMessage());
} }
} }

@ -41,7 +41,10 @@ class FluxGateway
try{ try{
$query = 'SELECT * FROM Flux;'; $query = 'SELECT * FROM Flux;';
$this->con->executeQuery($query); $this->con->executeQuery($query);
return $this->con->getResults(); $results = $this->con->getResults();
print("result findAllFlux Gateway");
var_dump($results);
return $results;
}catch (\PDOException $e){ }catch (\PDOException $e){
throw new \Exception("PDO error"); throw new \Exception("PDO error");
} }

@ -11,11 +11,9 @@ require 'AltoRouter.php';
class FrontControleur class FrontControleur
{ {
public function __construct(){ public function __construct(){
<<<<<<< HEAD:fluxRSS/src/controleur/FrontControleur.php
$routeur = new AltoRouter(); $routeur = new AltoRouter();
=======
global $twig; global $twig;
@ -52,7 +50,6 @@ class FrontControleur
echo $twig->render('erreur.html', ['dVueEreur' => $dVueEreur]); echo $twig->render('erreur.html', ['dVueEreur' => $dVueEreur]);
} }
} }
>>>>>>> pre-master:fluxRSS/controleur/FrontControleur.php
} }

@ -4,26 +4,34 @@ namespace metier;
class Article class Article
{ {
private string $id; private int $id;
private string $title; private string $title;
private string $date; private string $date;
private string $description; private string $description;
private string $guid; private string $guid;
private string $link; private string $link;
private string $mediaContent; private string $mediaContent;
private string $provenance; private int $provenance;
/** /**
* @param string $id * @param int $id
* @param string $title * @param string $title
* @param string $date * @param string $date
* @param string $description * @param string $description
* @param string $guid * @param string $guid
* @param string $link * @param string $link
* @param string $mediaContent * @param string $mediaContent
* @param string $provenance * @param int $provenance
*/ */
public function __construct(string $id, string $title, string $date, string $description, string $guid, string $link, string $mediaContent, string $provenance) public function __construct(
int $id,
string $title,
string $date,
string $description,
string $guid,
string $link,
string $mediaContent,
int $provenance)
{ {
$this->id = $id; $this->id = $id;
$this->title = $title; $this->title = $title;

@ -32,4 +32,12 @@ class Flux
{ {
$this->flux = $flux; $this->flux = $flux;
} }
/**
* @return int
*/
public function getId(): int
{
return $this->id;
}
} }

@ -10,7 +10,7 @@ require_once "config/config.php";
class FluxModel class FluxModel
{ {
public function FindAllFlux(){ public function FindAllFlux(){
$gateway = new FluxGateway(new Connection('mysql:host= londres.uca.local ; dbname= dbrorossetto', 'rorossetto', 'tpphp')); $gateway = new FluxGateway(new Connection('mysql:host= londres.uca.local;dbname=dbrorossetto', 'rorossetto', 'tpphp'));
$data = array(); $data = array();
$result = $gateway->findAllFlux(); $result = $gateway->findAllFlux();
@ -21,7 +21,7 @@ class FluxModel
} }
public function addFlux(Flux $flux){ public function addFlux(Flux $flux){
$gateway = new FluxGateway(new Connection('mysql:host= londres.uca.local ; dbname= dbrorossetto', 'rorossetto', 'tpphp')); $gateway = new FluxGateway(new Connection('mysql:host= londres.uca.local;dbname=dbrorossetto', 'rorossetto', 'tpphp'));
$data = $this->findFlux($flux); $data = $this->findFlux($flux);
if ($data == array()) { if ($data == array()) {
$gateway->addFlux($flux); $gateway->addFlux($flux);
@ -29,26 +29,26 @@ class FluxModel
} }
public function addFluxBySrc(string $flux): Flux { public function addFluxBySrc(string $flux): Flux {
$gateway = new FluxGateway(new Connection('mysql:host= londres.uca.local ; dbname= dbrorossetto', 'rorossetto', 'tpphp')); $gateway = new FluxGateway(new Connection('mysql:host= londres.uca.local;dbname=dbrorossetto', 'rorossetto', 'tpphp'));
$newFlux = new Flux($flux); $newFlux = new Flux($flux);
$gateway->addFlux($newFlux); $gateway->addFlux($newFlux);
return $newFlux; return $newFlux;
} }
public function removeFlux(Flux $flux){ public function removeFlux(Flux $flux){
$gateway = new FluxGateway(new Connection('mysql:host= londres.uca.local ; dbname= dbrorossetto', 'rorossetto', 'tpphp')); $gateway = new FluxGateway(new Connection('mysql:host= londres.uca.local;dbname=dbrorossetto', 'rorossetto', 'tpphp'));
$gateway->removeFlux($flux); $gateway->removeFlux($flux);
} }
public function removeFluxBySrc(string $flux): Flux { public function removeFluxBySrc(string $flux): Flux {
$gateway = new FluxGateway(new Connection('mysql:host= londres.uca.local ; dbname= dbrorossetto', 'rorossetto', 'tpphp')); $gateway = new FluxGateway(new Connection('mysql:host= londres.uca.local;dbname=dbrorossetto', 'rorossetto', 'tpphp'));
$newFlux = new Flux($flux); $newFlux = new Flux($flux);
$gateway->removeFlux($newFlux); $gateway->removeFlux($newFlux);
return $newFlux; return $newFlux;
} }
public function findFlux(Flux $flux){ public function findFlux(Flux $flux){
$gateway = new FluxGateway(new Connection('mysql:host= londres.uca.local ; dbname= dbrorossetto', 'rorossetto', 'tpphp')); $gateway = new FluxGateway(new Connection('mysql:host= londres.uca.local;dbname=dbrorossetto', 'rorossetto', 'tpphp'));
$data = array(); $data = array();
$result = $gateway->findFlux($flux); $result = $gateway->findFlux($flux);
@ -59,7 +59,7 @@ class FluxModel
} }
public function findFluxBySrc(string $flux){ public function findFluxBySrc(string $flux){
$gateway = new FluxGateway(new Connection('mysql:host= londres.uca.local ; dbname= dbrorossetto', 'rorossetto', 'tpphp')); $gateway = new FluxGateway(new Connection('mysql:host= londres.uca.local;dbname=dbrorossetto', 'rorossetto', 'tpphp'));
$data = array(); $data = array();
$result = $gateway->findFluxBySrc($flux); $result = $gateway->findFluxBySrc($flux);

@ -21,26 +21,44 @@ class Parser
} }
public function parseArticles(Flux $flux): array public function parseArticles(Flux $flux): array
{ {
print("debut parseArticles******************</br>");
$dom = new DOMDocument(); $dom = new DOMDocument();
print("creation du dom</br>");
$tabArticle = array(); $tabArticle = array();
if ($dom->load($flux->getFlux())){ if ($dom->load($flux->getFlux())){
$items = $dom->getElementsByTagName('item'); $items = $dom->getElementsByTagName('item');
print("après avoir getElementByTagName</br>");
var_dump($items);
foreach ($items as $item) { foreach ($items as $item) {
$title = $item->getElementsByTagName('title')[0]->nodeValue; $title = $item->getElementsByTagName('title')[0]->nodeValue;
print("le titre </br>");
var_dump($title);
$date = $item->getElementsByTagName('pubDate')[0]->nodeValue; $date = $item->getElementsByTagName('pubDate')[0]->nodeValue;
print("la date </br>");
var_dump($date);
$guid = $item->getElementsByTagName('guid')[0]->nodeValue; $guid = $item->getElementsByTagName('guid')[0]->nodeValue;
$link = $item->getElementsByTagName('link')[0]->nodeValue; $link = $item->getElementsByTagName('link')[0]->nodeValue;
$description = $item->getElementsByTagName('description')[0]->nodeValue; $description = $item->getElementsByTagName('description')[0]->nodeValue;
$media = $item->getElementsByTagName('media:content'); $media = $item->getElementsByTagName('media:content');
print("media : </br>");
var_dump($media);
$mediaUrl = null; $mediaUrl = null;
if ($media->length > 0){ if ($media->length > 0){
$mediaUrl = $media->item(0)->getAttribute('url'); $mediaUrl = $media->item(0)->getAttribute('url');
} }
$tabArticle[] = new Article((int)null, $title, $date, $description, $guid, $link, $mediaUrl, $flux->getId()); $tabArticle[] = new Article(
(int)null,
$title,
$date,
$description,
$guid,
$link,
($mediaUrl !== null) ? (string)$mediaUrl : '',
$flux->getId());
print("Tableau de l'article");
var_dump($tabArticle);
} }
return $tabArticle; return $tabArticle;
} else { } else {
@ -55,10 +73,16 @@ class Parser
*/ */
public function parseAll($fluxes){ public function parseAll($fluxes){
$tabArticles = []; $tabArticles = [];
print("debut parseAll</br>");
foreach ($fluxes as $flux){ foreach ($fluxes as $flux){
var_dump($flux); $fluxx = new Flux($flux[0],$flux[1]);
$tabArticles[] =$this->parseArticles($flux); print("flux de parseAll</br>");
var_dump($fluxx);
$tabArticles[] = $this->parseArticles($fluxx);
print("fin parsqeAll</br>");
} }
var_dump($tabArticles);
return $tabArticles; return $tabArticles;
} }
@ -71,16 +95,20 @@ class Parser
$this->articleGateway->removeAllArticleForParser(); $this->articleGateway->removeAllArticleForParser();
$allItemFlux = $this->fluxGateway->findAllFlux(); $allItemFlux = $this->fluxGateway->findAllFlux();
var_dump($allItemFlux);
foreach ($allItemFlux as $ItemFlux){ foreach ($allItemFlux as $ItemFlux){
$tabFluxes[] = $ItemFlux[1]; $tabFluxes[] = $ItemFlux[1];
}
$allArticles = $this->parseAll($tabFluxes);
var_dump($allArticles);
foreach ($allArticles as $article) {
$this->articleGateway->addArticle($article);
} }
return $allArticles; print("tableau de flux</br>");
var_dump($tabFluxes);
$allTabArticles = $this->parseAll($allItemFlux);
foreach ($allTabArticles as $tabArticle) {
foreach ($tabArticle as $item) {
$this->articleGateway->addArticle($item);
}
}
} }
} }

Loading…
Cancel
Save