Commit ae7f1d25 authored by Corentin Bettiol's avatar Corentin Bettiol 💻

slightly improve efficiency with pdo

parent 6088891e
......@@ -3,16 +3,14 @@ class AdminManager extends Base
{
public function getBlogPosts()
{
$db = $this->dbConnect();
$req = $db->query('SELECT * FROM l3m_blog ORDER BY id DESC');
return $req;
$blogpost = new BlogManager(null, null, null, null, null, null);
return $blogpost->getAll();
}
public function getProjects()
{
$db = $this->dbConnect();
$req = $db->query('SELECT * FROM l3m_projects ORDER BY id DESC');
return $req;
$project = new ProjectManager(null,null,null,null,null);
return $project->getProjects();
}
public function getBlogPost($link)
......
......@@ -2,19 +2,46 @@
// connexion à la bdd
class Base
{
protected function dbConnect()
{
$db = new PDO('mysql:host=localhost;dbname='. dbName .';charset=utf8', dbUser, dbPass);
return $db;
}
# thanks https://www.matthecat.com/supprimer-les-accents-dune-chaine-en-php/ & http://php.net/manual/fr/transliterator.transliterate.php#110598
protected function slugify($text){
$slug = preg_replace('/&/', 'et', $text);
// we have a static connection to the db
protected static $db;
// we try to connect to db at each instantiation
function __construct(){
$this->dbConnect();
}
protected function dbConnect()
{
// verify if a connexion to the db already exist
if (!(self::$db instanceof PDO))
self::$db = new PDO('mysql:host=localhost;dbname='. dbName .';charset=utf8', dbUser, dbPass);
}
// thanks https://www.matthecat.com/supprimer-les-accents-dune-chaine-en-php/ & http://php.net/manual/fr/transliterator.transliterate.php#110598
protected function slugify($text, $table){
$slug = preg_replace('/&/', 'et', $text);
$slug = transliterator_transliterate("Any-Latin; NFD; [:Nonspacing Mark:] Remove; NFC; [:Punctuation:] Remove; Lower();", $slug);
$slug = preg_replace('/[-\s]+/', '-', $slug);
$slug = preg_replace('/-$/', '', $slug);
$slug = preg_replace('/[^a-z0-9-]+/', '', $slug);
return $slug;
// check if the slug already exist
$i = 1;
$old = $slug;
do{
$req = self::$db->prepare('SELECT slug FROM l3m_'. $table .' WHERE slug = ?');
$req->execute([$slug]);
$slugExist = $req->fetch();
if($slugExist){
$i++;
$slug = $old . "-" . $i;
}
}
while($slugExist);
return $slug;
}
}
\ No newline at end of file
......@@ -8,12 +8,12 @@ class BlogManager extends Base
private $id;
function __construct($time, $title, $content, $shortContent, $id){
parent::__construct(); // like antibiotics
$this->time = htmlspecialchars($time);
$this->title = htmlspecialchars($title);
$this->content = htmlspecialchars($content);
$this->shortContent = htmlspecialchars($shortContent);
$this->id = htmlspecialchars($id);
}
public function getPage($page)
......@@ -21,8 +21,7 @@ class BlogManager extends Base
$start = $page*5;
$stop = $start + 5;
$db = $this->dbConnect();
$req = $db->query('SELECT * FROM l3m_blog ORDER BY id DESC LIMIT '. $start .', '. $stop .'');
$req = self::$db->query('SELECT * FROM l3m_blog ORDER BY id DESC LIMIT '. $start .', '. $stop .'');
return $req;
}
......@@ -31,12 +30,11 @@ class BlogManager extends Base
{
$link = htmlspecialchars($link);
$db = $this->dbConnect();
$req = $db->prepare('SELECT * FROM l3m_blog WHERE slug = ?');
$req = self::$db->prepare('SELECT * FROM l3m_blog WHERE slug = ?');
$req->execute([$link]);
$post = $req->fetch();
if($post == false)
if(!$post)
return false;
$post['datetime'] = new DateTime($post['time']);
......@@ -44,10 +42,15 @@ class BlogManager extends Base
return $post;
}
public function getall()
{
$req = self::$db->query('SELECT * FROM l3m_blog ORDER BY id DESC');
return $req;
}
// todo vérifier qu'on poste pas un truc vide
public function update(){
$db = $this->dbConnect();
$req = $db->prepare('UPDATE l3m_blog SET time = :time, title = :title, content = :content, shortContent = :shortContent WHERE id = :id');
$req = self::$db->prepare('UPDATE l3m_blog SET time = :time, title = :title, content = :content, shortContent = :shortContent WHERE id = :id');
$req->execute([
'time' => $this->time,
'title' => $this->title,
......@@ -70,23 +73,9 @@ class BlogManager extends Base
$this->shortContent = 'Ce billet de blog ne dispose d\'aucune description.';
}
$this->slug = $this->slugify($this->title);
$db = $this->dbConnect();
$i = 1;
$slug = $this->slug;
do{
$req = $db->prepare('SELECT slug FROM l3m_blog WHERE slug = ?');
$req->execute([$this->slug]);
$slugExist = $req->fetch();
if($slugExist == true){
$i++;
$this->slug = $slug . "-" . $i;
}
}
while($slugExist != false);
$this->slug = $this->slugify($this->title, "blog");
$req = $db->prepare('INSERT INTO l3m_blog(time, title, slug, content, shortContent) VALUES(:time, :title, :slug, :content, :shortContent)');
$req = self::$db->prepare('INSERT INTO l3m_blog(time, title, slug, content, shortContent) VALUES(:time, :title, :slug, :content, :shortContent)');
$req->execute([
'time' => $this->time,
'title' => $this->title,
......@@ -100,8 +89,7 @@ class BlogManager extends Base
public function delete($link){
$link = htmlspecialchars($link);
$db = $this->dbConnect();
$req = $db->prepare('DELETE FROM l3m_blog WHERE slug = ?');
$req = self::$db->prepare('DELETE FROM l3m_blog WHERE slug = ?');
$req->execute([$link]);
}
}
\ No newline at end of file
......@@ -8,6 +8,7 @@ class ProjectManager extends Base
private $id;
function __construct($name, $link, $content, $shortContent, $id){
parent::__construct(); // like antibiotics
$this->name = htmlspecialchars($name);
$this->link = htmlspecialchars($link);
$this->content = htmlspecialchars($content);
......@@ -17,8 +18,7 @@ class ProjectManager extends Base
public function getProjects()
{
$db = $this->dbConnect();
$req = $db->query('SELECT * FROM l3m_projects ORDER BY id DESC');
$req = self::$db->query('SELECT * FROM l3m_projects ORDER BY id DESC');
return $req;
}
......@@ -27,12 +27,11 @@ class ProjectManager extends Base
{
$link = htmlspecialchars($link);
$db = $this->dbConnect();
$req = $db->prepare('SELECT * FROM l3m_projects WHERE slug = ?');
$req = self::$db->prepare('SELECT * FROM l3m_projects WHERE slug = ?');
$req->execute([$link]);
$project = $req->fetch();
if($project == false)
if(!$project)
return false;
return $project;
......@@ -40,8 +39,7 @@ class ProjectManager extends Base
// todo vérifier qu'on poste pas un truc vide
public function update(){
$db = $this->dbConnect();
$req = $db->prepare('UPDATE l3m_projects SET name = :name, link = :link, content = :content, shortContent = :shortContent WHERE id = :id');
$req = self::$db->prepare('UPDATE l3m_projects SET name = :name, link = :link, content = :content, shortContent = :shortContent WHERE id = :id');
$req->execute([
'name' => $this->name,
'link' => $this->link,
......@@ -59,23 +57,9 @@ class ProjectManager extends Base
if(strlen($this->shortContent) < 1){
$this->shortContent = 'Pas de description.';
}
$this->slug = $this->slugify($this->name);
$this->slug = $this->slugify($this->name, "projects");
$db = $this->dbConnect();
$i = 1;
$slug = $this->slug;
do{
$req = $db->prepare('SELECT slug FROM l3m_projects WHERE slug = ?');
$req->execute([$this->slug]);
$slugExist = $req->fetch();
if($slugExist == true){
$i++;
$this->slug = $slug . "-" . $i;
}
}
while($slugExist != false);
$req = $db->prepare('INSERT INTO l3m_projects(name, slug, link, content, shortcontent) VALUES(:name, :slug, :link, :content, :shortContent)');
$req = self::$db->prepare('INSERT INTO l3m_projects(name, slug, link, content, shortcontent) VALUES(:name, :slug, :link, :content, :shortContent)');
$req->execute([
'name' => $this->name,
'slug' => $this->slug,
......@@ -88,8 +72,7 @@ class ProjectManager extends Base
public function delete($link){
$link = htmlspecialchars($link);
$db = $this->dbConnect();
$req = $db->prepare('DELETE FROM l3m_projects WHERE slug = ?');
$req = self::$db->prepare('DELETE FROM l3m_projects WHERE slug = ?');
$req->execute([$link]);
}
}
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment