From ae0ed09682c7c2f3f3fe3581039fe5e04708bde6 Mon Sep 17 00:00:00 2001 From: timovn Date: Sat, 14 May 2016 21:38:33 +0200 Subject: [PATCH] [PHP] - Optimized SQL --- admin/commentaires.php | 10 +++-- admin/maintenance.php | 2 +- atom.php | 10 ++--- inc/conf.php | 2 +- inc/fich.php | 6 +-- inc/html.php | 5 ++- inc/sqli.php | 4 +- inc/them.php | 2 +- index.php | 48 +++++++++++++++++------- rss.php | 8 ++-- themes/default/template/commentaire.html | 2 +- 11 files changed, 61 insertions(+), 38 deletions(-) diff --git a/admin/commentaires.php b/admin/commentaires.php index a9492c1..b69b846 100755 --- a/admin/commentaires.php +++ b/admin/commentaires.php @@ -60,11 +60,15 @@ traiter_form_commentaire($comment, 'admin'); $param_makeup['menu_theme'] = 'for_article'; $article_id = $_GET['post_id']; - $article_title = get_entry($GLOBALS['db_handle'], 'articles', 'bt_title', $article_id, 'return'); - $query = "SELECT * FROM commentaires WHERE bt_article_id=? ORDER BY bt_id"; - + $query = "SELECT c.*, a.bt_title FROM commentaires AS c, articles AS a WHERE c.bt_article_id=? AND c.bt_article_id=a.bt_id ORDER BY c.bt_id"; $commentaires = liste_elements($query, array($article_id), 'commentaires'); + if (!empty($commentaires)) { + $article_title = $commentaires[0]['bt_title']; + } else { + $article_title = get_entry($GLOBALS['db_handle'], 'articles', 'bt_title', $article_id, 'return'); + } + $param_makeup['show_links'] = '0'; } diff --git a/admin/maintenance.php b/admin/maintenance.php index c87d3ff..679b801 100755 --- a/admin/maintenance.php +++ b/admin/maintenance.php @@ -658,7 +658,7 @@ function parse_html($content) { // get list of comments (comments that belong to selected articles only) if ($_GET['incl-comms'] == 1) { foreach ($data_array['articles'] as $article) { - $comments = liste_elements('SELECT * FROM commentaires WHERE bt_article_id = ? ', array($article['bt_id']), 'commentaires'); + $comments = liste_elements('SELECT c.*, a.bt_title FROM commentaires AS c, articles AS a WHERE c.bt_article_id = ? AND c.bt_article_id=a.bt_id', array($article['bt_id']), 'commentaires'); if (!empty($comments)) { $data_array['commentaires'] = array_merge($data_array['commentaires'], $comments); } diff --git a/atom.php b/atom.php index 6c17c84..5ada38a 100755 --- a/atom.php +++ b/atom.php @@ -61,14 +61,12 @@ function require_all() { $GLOBALS['db_handle'] = open_base(); $article_id = htmlspecialchars($_GET['id']); - $liste = liste_elements("SELECT * FROM commentaires WHERE bt_article_id=? AND bt_statut=1 ORDER BY bt_id DESC", array($article_id), 'commentaires'); + $liste = liste_elements("SELECT c.*, a.bt_title FROM commentaires AS c, articles AS a WHERE c.bt_article_id=? AND c.bt_article_id=a.bt_id AND c.bt_statut=1 ORDER BY c.bt_id DESC", array($article_id), 'commentaires'); if (!empty($liste)) { - $query = "SELECT * FROM articles WHERE bt_id=? AND bt_date<=".date('YmdHis')." AND bt_statut=1"; - $billet = liste_elements($query, array($article_id), 'articles'); - $xml .= 'Commentaires sur '.$billet[0]['bt_title'].' - '.$GLOBALS['nom_du_site'].''."\n"; - $xml .= ''."\n"; - $xml .= ''.$billet[0]['bt_link'].''; + $xml .= 'Commentaires sur '.$liste[0]['bt_title'].' - '.$GLOBALS['nom_du_site'].''."\n"; + $xml .= ''."\n"; + $xml .= ''.$liste[0]['bt_link'].''; foreach ($liste as $comment) { $dec = decode_id($comment['bt_id']); diff --git a/inc/conf.php b/inc/conf.php index 57121ea..15f124c 100755 --- a/inc/conf.php +++ b/inc/conf.php @@ -19,7 +19,7 @@ // GENERAL define('BLOGOTEXT_NAME', 'BlogoText'); define('BLOGOTEXT_SITE', 'http://lehollandaisvolant.net/blogotext/'); -define('BLOGOTEXT_VERSION', '3.3.9-1'); +define('BLOGOTEXT_VERSION', '3.3.10-1'); define('MINIMAL_PHP_REQUIRED_VERSION', '5.5'); // FOLDERS (change this only if you know what you are doing...) diff --git a/inc/fich.php b/inc/fich.php index f0be2ba..8e12334 100755 --- a/inc/fich.php +++ b/inc/fich.php @@ -319,9 +319,9 @@ function request_external_files($feeds, $timeout, $echo_progress=false) { function rafraichir_cache_lv1() { creer_dossier(BT_ROOT.DIR_CACHE, 1); - $arr_a = liste_elements("SELECT * FROM articles WHERE bt_statut = 1 ORDER BY bt_date DESC LIMIT 0, 20", array(), 'articles'); - $arr_c = liste_elements("SELECT * FROM commentaires WHERE bt_statut = 1 ORDER BY bt_id DESC LIMIT 0, 20", array(), 'commentaires'); - $arr_l = liste_elements("SELECT * FROM links WHERE bt_statut = 1 ORDER BY bt_id DESC LIMIT 0, 20", array(), 'links'); + $arr_a = liste_elements("SELECT * FROM articles WHERE bt_statut=1 ORDER BY bt_date DESC LIMIT 0, 20", array(), 'articles'); + $arr_c = liste_elements("SELECT c.*, a.bt_title FROM commentaires AS c, articles AS a WHERE c.bt_statut=1 AND c.bt_article_id=a.bt_id ORDER BY c.bt_id DESC LIMIT 0, 20", array(), 'commentaires'); + $arr_l = liste_elements("SELECT * FROM links WHERE bt_statut=1 ORDER BY bt_id DESC LIMIT 0, 20", array(), 'links'); $file = BT_ROOT.DIR_CACHE.'/'.'cache_rss_array.dat'; return file_put_contents($file, ' $arr_c, 'a' => $arr_a, 'l' => $arr_l)))).' */'); } diff --git a/inc/html.php b/inc/html.php index 584b003..3f9534a 100755 --- a/inc/html.php +++ b/inc/html.php @@ -153,7 +153,7 @@ function moteur_recherche() { function encart_commentaires() { mb_internal_encoding('UTF-8'); - $query = "SELECT c.bt_author, c.bt_id, c.bt_article_id, c.bt_content FROM commentaires c LEFT JOIN articles a ON a.bt_id=c.bt_article_id WHERE c.bt_statut=1 AND a.bt_statut=1 ORDER BY c.bt_id DESC LIMIT 5"; + $query = "SELECT a.bt_title, c.bt_author, c.bt_id, c.bt_article_id, c.bt_content FROM commentaires c LEFT JOIN articles a ON a.bt_id=c.bt_article_id WHERE c.bt_statut=1 AND a.bt_statut=1 ORDER BY c.bt_id DESC LIMIT 5"; $tableau = liste_elements($query, array(), 'commentaires'); if (isset($tableau)) { $listeLastComments = ''."\n"; return $listeLastComments; diff --git a/inc/sqli.php b/inc/sqli.php index fabffd7..620142b 100755 --- a/inc/sqli.php +++ b/inc/sqli.php @@ -233,8 +233,8 @@ function init_list_articles($article) { function init_list_comments($comment) { $comment['auteur_lien'] = (!empty($comment['bt_webpage'])) ? ''.$comment['bt_author'].'' : $comment['bt_author'] ; $comment['anchor'] = article_anchor($comment['bt_id']); - $comment['article_title'] = get_entry($GLOBALS['db_handle'], 'articles', 'bt_title', $comment['bt_article_id'], 'return'); - $comment['bt_link'] = get_blogpath($comment['bt_article_id'], $comment['article_title']).'#'.$comment['anchor']; +// $comment['article_title'] = get_entry($GLOBALS['db_handle'], 'articles', 'bt_title', $comment['bt_article_id'], 'return'); + $comment['bt_link'] = get_blogpath($comment['bt_article_id'], $comment['bt_title']).'#'.$comment['anchor']; $comment = array_merge($comment, decode_id($comment['bt_id'])); return $comment; } diff --git a/inc/them.php b/inc/them.php index 197c720..0e29aab 100755 --- a/inc/them.php +++ b/inc/them.php @@ -268,7 +268,7 @@ function afficher_index($tableau, $type) { $HTML_comms = ''; // get list comments if ($billet['bt_nb_comments'] != 0) { - $query = "SELECT * FROM commentaires WHERE bt_article_id=? AND bt_statut=1 ORDER BY bt_id LIMIT ? "; + $query = "SELECT c.*, a.bt_title FROM commentaires AS c, articles AS a WHERE c.bt_article_id=? AND c.bt_article_id=a.bt_id AND c.bt_statut=1 ORDER BY c.bt_id LIMIT ? "; $commentaires = liste_elements($query, array($billet['bt_id'], $billet['bt_nb_comments']), 'commentaires'); // parse & apply template comments diff --git a/index.php b/index.php index 751f921..9efd130 100755 --- a/index.php +++ b/index.php @@ -152,15 +152,14 @@ traiter_form_commentaire($comment, 'public'); // paramètre mode : quelle table "mode" ? if (isset($_GET['mode'])) { switch($_GET['mode']) { - case 'blog': - $where = 'articles'; - break; case 'comments': + $query = "SELECT c.*, a.bt_title FROM "; $where = 'commentaires'; break; case 'links': $where = 'links'; break; + case 'blog': default: $where = 'articles'; break; @@ -168,10 +167,25 @@ traiter_form_commentaire($comment, 'public'); } else { $where = 'articles'; } - $query .= $where.' '; - // paramètre de recherche uniquement dans les articles publiés : - $query .= 'WHERE bt_statut=1 '; + switch($where) { + case 'commentaires': + $query .= "commentaires AS c, articles AS a "; + break; + default: + $query .= $where.' '; + break; + } + + // paramètre de recherche uniquement dans les éléments publiés : + switch($where) { + case 'commentaires': + $query .= 'WHERE c.bt_statut=1 '; + break; + default: + $query .= 'WHERE bt_statut=1 '; + break; + } // paramètre de date "d" @@ -188,6 +202,9 @@ traiter_form_commentaire($comment, 'public'); case 'articles': $sql_date = "bt_date LIKE ? "; break; + case 'commentaires': + $sql_date = "c.bt_date LIKE ? "; + break; default: $sql_date = "bt_id LIKE ? "; break; @@ -210,7 +227,7 @@ traiter_form_commentaire($comment, 'public'); $sql_q = implode(array_fill(0, count($arr), '( bt_content || bt_title || bt_link ) LIKE ? '), 'AND '); break; case 'commentaires' : - $sql_q = implode(array_fill(0, count($arr), 'bt_content LIKE ? '), 'AND '); + $sql_q = implode(array_fill(0, count($arr), 'c.bt_content LIKE ? '), 'AND '); break; default: $sql_q = ""; @@ -229,14 +246,14 @@ traiter_form_commentaire($comment, 'public'); $array[] = '%, '.$_GET['tag']; break; case 'links' : - $sql_tag = "( bt_tags LIKE ? OR bt_tags LIKE ? OR bt_tags LIKE ? OR bt_tags LIKE ? )"; + $sql_tag = "( bt_tags LIKE ? OR bt_tags LIKE ? OR bt_tags LIKE ? OR bt_tags LIKE ? ) "; $array[] = $_GET['tag']; $array[] = $_GET['tag'].', %'; $array[] = '%, '.$_GET['tag'].', %'; $array[] = '%, '.$_GET['tag']; break; default: - $sql_tag = ""; + $sql_tag = " "; break; } } @@ -248,18 +265,24 @@ traiter_form_commentaire($comment, 'public'); case 'articles' : $sql_order = "ORDER BY bt_date DESC "; break; + case 'commentaires' : + $sql_order = "ORDER BY c.bt_id DESC "; + break; default: $sql_order = "ORDER BY bt_id DESC "; break; } - // paramètre de filtrage admin/public (pas un paramètre, mais ajouté quand même) + // paramètre de filtrage date (pas un paramètre, mais ajouté quand même) switch ($where) { case 'articles' : - $sql_a_p = "bt_date <= ".date('YmdHis')." AND bt_statut=1 "; + $sql_a_p = "bt_date <= ".date('YmdHis')." "; + break; + case 'commentaires' : + $sql_a_p = "c.bt_id <= ".date('YmdHis')." AND c.bt_article_id=a.bt_id "; break; default: - $sql_a_p = "bt_id <= ".date('YmdHis')." AND bt_statut=1 "; + $sql_a_p = "bt_id <= ".date('YmdHis')." "; break; } @@ -285,7 +308,6 @@ traiter_form_commentaire($comment, 'public'); } $query .= $glue.$sql_a_p.$sql_order.$sql_p; - $tableau = liste_elements($query, $array, $where); $GLOBALS['param_pagination'] = array('nb' => count($tableau), 'nb_par_page' => $GLOBALS['max_bill_acceuil']); afficher_index($tableau, 'list'); diff --git a/rss.php b/rss.php index b50eb7d..ab8cb81 100755 --- a/rss.php +++ b/rss.php @@ -62,13 +62,11 @@ function require_all() { $GLOBALS['db_handle'] = open_base(); $article_id = htmlspecialchars($_GET['id']); - $liste = liste_elements("SELECT * FROM commentaires WHERE bt_article_id=? AND bt_statut=1 ORDER BY bt_id DESC", array($article_id), 'commentaires'); + $liste = liste_elements("SELECT c.*, a.bt_title FROM commentaires AS c, articles AS a WHERE c.bt_article_id=? AND c.bt_article_id=a.bt_id AND c.bt_statut=1 ORDER BY c.bt_id DESC", array($article_id), 'commentaires'); if (!empty($liste)) { - $query = "SELECT * FROM articles WHERE bt_id=? AND bt_date<=".date('YmdHis')." AND bt_statut=1"; - $billet = liste_elements($query, array($article_id), 'articles'); - $xml .= 'Commentaires sur '.$billet[0]['bt_title'].' - '.$GLOBALS['nom_du_site'].''."\n"; - $xml .= ''.$billet[0]['bt_link'].''."\n"; + $xml .= 'Commentaires sur '.$liste[0]['bt_title'].' - '.$GLOBALS['nom_du_site'].''."\n"; + $xml .= ''.$liste[0]['bt_link'].''."\n"; $xml .= ''."\n"; $xml .= 'fr'."\n"; $xml .= ''.$GLOBALS['auteur'].''."\n"; diff --git a/themes/default/template/commentaire.html b/themes/default/template/commentaire.html index 2ba9d61..9d8bd80 100755 --- a/themes/default/template/commentaire.html +++ b/themes/default/template/commentaire.html @@ -11,7 +11,7 @@ {commentaire_contenu}